forked from evolix/ansible-roles
106 lines
3.1 KiB
Bash
Executable file
106 lines
3.1 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
: "${AUTOSYSADMIN_LIB:=/usr/local/lib/autosysadmin}"
|
|
source "${AUTOSYSADMIN_LIB}/common.sh" || exit 1
|
|
source "${AUTOSYSADMIN_LIB}/restart.sh" || exit 1
|
|
|
|
## Possible values for RUNNING :
|
|
## never => disabled
|
|
## always => enabled
|
|
## nwh-fr => enabled during non-working-hours in France
|
|
## nwh-ca => enabled during non-working-hours in Canada (not supported yet)
|
|
## custom => enabled if `running_custom()` function return 0, otherwise disabled.
|
|
|
|
# shellcheck disable=SC2034
|
|
RUNNING="nwh-fr"
|
|
|
|
## The name of the service, mainly for logging
|
|
service_name="nagios-nrpe-server"
|
|
## The SysVinit script name
|
|
sysvinit_script="${service_name}"
|
|
## The systemd service name
|
|
systemd_service="${service_name}.service"
|
|
|
|
is_service_alive() {
|
|
## this must return 0 if the service is alive, otherwise return 1
|
|
## Example:
|
|
pgrep -u nagios nrpe > /dev/null
|
|
}
|
|
|
|
## Action for SysVinit system
|
|
sysvinit_action() {
|
|
# Save service status before restart
|
|
timeout 2 "/etc/init.d/${sysvinit_script}" status | save_in_log_dir "${service_name}.before.status"
|
|
|
|
# Try to restart
|
|
timeout 20 "/etc/init.d/${sysvinit_script}" restart > /dev/null
|
|
rc=$?
|
|
if [ "${rc}" -eq "0" ]; then
|
|
log_action "Restart ${service_name}: OK"
|
|
else
|
|
log_action "Restart ${service_name}: failed"
|
|
fi
|
|
|
|
# Save service status after restart
|
|
timeout 2 "/etc/init.d/${sysvinit_script}" status | save_in_log_dir "${service_name}.after.status"
|
|
}
|
|
|
|
## Action for systemd system
|
|
systemd_action() {
|
|
# Save service status before restart
|
|
systemctl status "${systemd_service}" | save_in_log_dir "${service_name}.before.status"
|
|
|
|
# Try to restart
|
|
# systemctl (only for NRPE ?) sometimes returns 0 even if the service has failed to start
|
|
# so we check the status explicitly
|
|
timeout 20 systemctl restart "${systemd_service}" > /dev/null \
|
|
&& sleep 1 \
|
|
&& systemctl status "${systemd_service}" > /dev/null
|
|
rc=$?
|
|
if [ "${rc}" -eq "0" ]; then
|
|
log_action "Restart ${service_name}: OK"
|
|
else
|
|
log_action "Restart ${service_name}: failed"
|
|
fi
|
|
|
|
# Save service status after restart
|
|
systemctl status "${systemd_service}" | save_in_log_dir "${service_name}.after.status"
|
|
}
|
|
|
|
# Should we run?
|
|
if ! is_supposed_to_run; then
|
|
# log_global "${PROGNAME} is not supposed to run (RUNNING=${RUNNING})."
|
|
exit 0
|
|
fi
|
|
if is_service_alive; then
|
|
# log_global "${service_name} process alive. Aborting"
|
|
exit 0
|
|
fi
|
|
|
|
# Yes we do, so check for sysvinit or systemd
|
|
if is_debian_version "8" "<="; then
|
|
if ! is_sysvinit_enabled "*${sysvinit_script}*"; then
|
|
# log_global "${service_name} not enabled. Aborting"
|
|
exit 0
|
|
fi
|
|
|
|
# Let's finally do the action
|
|
pre_restart
|
|
sysvinit_action
|
|
post_restart
|
|
else
|
|
if ! is_systemd_enabled "${systemd_service}"; then
|
|
# log_global "${service_name} is disabled (or missing), nothing left to do."
|
|
exit 0
|
|
fi
|
|
if is_systemd_active "${systemd_service}"; then
|
|
# log_global "${service_name} is active, nothing left to do."
|
|
exit 0
|
|
fi
|
|
|
|
# Let's finally do the action
|
|
pre_restart
|
|
systemd_action
|
|
post_restart
|
|
fi
|