From 4c8645288c314c0a2f8da20955a3f3c640ed08ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Fri, 17 Aug 2018 11:06:21 +0200 Subject: [PATCH] Restart service only if it's managed --- xymon/xymon.libs.alert.sh | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/xymon/xymon.libs.alert.sh b/xymon/xymon.libs.alert.sh index 4e8baae..830d21b 100755 --- a/xymon/xymon.libs.alert.sh +++ b/xymon/xymon.libs.alert.sh @@ -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 + # 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}"