Restart service only if it's managed

This commit is contained in:
Jeremy Gardais 2018-08-17 11:06:21 +02:00
parent 18e60cc89e
commit 4c8645288c
1 changed files with 23 additions and 14 deletions

View File

@ -45,17 +45,19 @@ debug_stdout="${temp_dir}/debug.stdout"
service_list="${temp_dir}/services.error.list"
# }}}
# Temp dir for this alert
# Temp dir for this alert {{{
if [ ! -d "${temp_dir}" ] ; then
mkdir -p -- "${temp_dir}"
fi
# }}}
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: ${BBHOSTNAME}${BBSVCNAME} error" > "${debug_stdout}"
# Check if host need to reboot
# Check if host need to reboot {{{
if echo "${BBALPHAMSG}" | grep -qE "\\&yellow Machine should be reboot.*" ; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test kernel — The host need to be rebooted." >> "${debug_stdout}"
fi
# }}}
# Check if a service need to restart
if echo "${BBALPHAMSG}" | grep -qE "\\&yellow The following processes.*" ; then
@ -69,46 +71,53 @@ if echo "${BBALPHAMSG}" | grep -qE "\\&yellow The following processes.*" ; then
case "${bin_path}" in
# Arpwatch
'/usr/sbin/arpwatch' )
process="arpwatch"
service_name="arpwatch"
;;
# Dbus
'/usr/bin/dbus-daemon' )
process="dbus"
service_name="dbus"
;;
# Postfix
'/usr/lib/postfix/sbin/pickup' | '/usr/lib/postfix/qmgr' | '/usr/lib/postfix/sbin/tlsmgr' )
process="postfix"
service_name="postfix"
;;
# Nginx
'/usr/sbin/nginx' )
process="nginx"
service_name="nginx"
;;
# Ntp
'/usr/sbin/ntpd' )
process="ntp"
service_name="ntp"
;;
# Systemd-journald
'/lib/systemd/systemd-journald' )
process="systemd-journald"
service_name="systemd-journald"
;;
# Systemd-logind
'/lib/systemd/systemd-logind' )
process="systemd-logind"
service_name="systemd-logind"
;;
# default
* )
process="NOT.MANAGED"
service_name="NOT.MANAGED"
;;
esac
# }}}
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: while bin_path loop — ${process} need to be restarted." >> "${debug_stdout}"
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: while bin_path loop — ssh -o StrictHostKeyChecking=no ${REMOTE_SSH_USER}@${BBHOSTNAME} sudo systemctl restart ${process}.service" >> "${debug_stdout}"
# Restart service if managed {{{
if [ "${service_name}" = "NOT.MANAGED" ]; then
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: while bin_path loop — service for ${bin_path} is not managed." >> "${debug_stdout}"
else
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: while bin_path loop — ${service_name} need to be restarted." >> "${debug_stdout}"
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: while bin_path loop — ssh -o StrictHostKeyChecking=no ${REMOTE_SSH_USER}@${BBHOSTNAME} sudo systemctl restart ${service_name}.service" >> "${debug_stdout}"
fi
# }}}
done < "${service_list}"
# Also restart xymon-client service
# Also restart xymon-client service {{{
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test service — xymon-client also need to be restarted." >> "${debug_stdout}"
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6s\e[m\n' "DEBUG: Test service — ssh -o StrictHostKeyChecking=no ${REMOTE_SSH_USER}@${BBHOSTNAME} sudo systemctl restart xymon-client.service" >> "${debug_stdout}"
# }}}
fi
[ "${DEBUG}" -eq "0" ] || rm -rf -- "${temp_dir}"