1 |
#!/bin/bash |
2 |
|
3 |
# Nagios event handler to restart a service |
4 |
# To be used when check_service detects that a daemon is not running |
5 |
# with either a remaining PID file or a lock file |
6 |
# This handler should not be used when the init script does not |
7 |
# properly distinguish "stopped" daemons from crashed daemons (i.e., |
8 |
# the init script does not clean up when the service is stopped). |
9 |
|
10 |
if [ $# -lt 4 ]; then |
11 |
echo "Missing required arguments (4 required in total)" >&2 |
12 |
exit 1 |
13 |
fi |
14 |
|
15 |
MAX_RESTARTS=4 |
16 |
verb=1 |
17 |
|
18 |
# verify the file system state |
19 |
function try_restart() { |
20 |
if [ ! -f $1 ]; then |
21 |
[ $verb -gt 0 ] && echo "Cannot execute $1" |
22 |
return 1 |
23 |
fi |
24 |
|
25 |
# first try a restart |
26 |
$1 restart > /dev/null 2> /dev/null |
27 |
sleep 1 |
28 |
$1 status > /dev/null 2> /dev/null |
29 |
if [ $? -ne 0 ]; then |
30 |
# try a stop and start |
31 |
( $1 stop ; sleep 2 ; $1 start ) > /dev/null 2> /dev/null |
32 |
fi |
33 |
|
34 |
# log restart to syslog |
35 |
logger -p syslog.warning "Tried to restart $1" |
36 |
} |
37 |
|
38 |
|
39 |
case "$1" in |
40 |
OK) # service is fine, nothing to do |
41 |
;; |
42 |
WARNING) # service was stopped, nothing to do |
43 |
;; |
44 |
UNKNOWN) # not sure how to proceed here, so do nothing |
45 |
;; |
46 |
CRITICAL) # ACTION! |
47 |
if [ "$2" == "SOFT" ]; then |
48 |
try_restart "$4" |
49 |
elif [ "$2" == "HARD" ]; then |
50 |
if [ $3 -lt $MAX_RESTARTS ]; then |
51 |
try_restart "$4" |
52 |
fi |
53 |
fi |
54 |
;; |
55 |
esac |
56 |
|
57 |
exit 0 |
58 |
|