monitoring-plugins/restart_service

33 lines
980 B
Plaintext
Raw Normal View History

2021-10-06 15:59:56 +02:00
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
while getopts "s:t:a:S:" opt; do
case $opt in
s)
servicestate=$OPTARG
;;
t)
servicestatetype=$OPTARG
;;
a)
serviceattempt=$OPTARG
;;
S)
service=$OPTARG
;;
esac
done
if ( [ -z $servicestate ] || [ -z $servicestatetype ] || [ -z $serviceattempt ] || [ -z $service ] ); then
echo "USAGE: $0 -s servicestate -z servicestatetype -a serviceattempt -S service"
exit 3;
else
2021-10-07 10:47:15 +02:00
# Only restart on the third attempt of a critical event in soft state and in first attempt of a critical event in hard state
if [ $servicestate == "CRITICAL" ] && ( ( [ $serviceattempt -eq 3 ] && [ $servicestatetype == "SOFT" ] ) || ( [ $serviceattempt -eq 1 ] && [ $servicestatetype == "HARD" ] ) ); then
pidof systemd >/dev/null
if [ $? -eq 0 ]; then
2021-10-06 15:59:56 +02:00
sudo systemctl restart $service
else
sudo service $service restart
fi
fi
2021-10-07 10:47:15 +02:00
fi