add event_handlers to restart services
[mirror/dsa-nagios.git] / dsa-nagios-checks / event_handlers / dsa-eventhandler-restart-service
1 #!/bin/sh
2 #
3 # Event handler script for restarting a service on the local machine
4 #
5 # Note: This script will only restart the service if the service is
6 #       retried 3 times (in a "soft" state) or if the service somehow
7 #       manages to fall into a "hard" error state.
8
9 # Args:
10 # $1 $SERVICESTATE$
11 # $2 $SERVICESTATETYPE$
12 # $3 $SERVICEATTEMPT$
13 # $4 init script name
14
15 state="$1"
16 type="$2"
17 attempt="$3"
18 service="$4"
19
20 # What state is the service in?
21 case "${state}" in
22         OK)
23         # The service just came back up, so don't do anything...
24         ;;
25         WARNING)
26         # We don't really care about warning states, since the service is probably still running...
27         ;;
28         UNKNOWN)
29         # We don't know what might be causing an unknown error, so don't do anything...
30         ;;
31         CRITICAL)
32         # Aha!  The service appears to have a problem - perhaps we should restart it
33         # Is this a "soft" or a "hard" state?
34                 case "${type}" in
35                 # We're in a "soft" state, meaning that Nagios is in the middle of retrying the
36                 # check before it turns into a "hard" state and contacts get notified...
37                         SOFT)
38                         # What check attempt are we on?  We don't want to restart the service on the first
39                         # check, because it may just be a fluke!
40                                 case "${attempt}" in
41                                         # Wait until the check has been tried 3 times before restarting the service
42                                         # If the check fails on the 4th time (after we restart the service), the state
43                                         # type will turn to "hard" and contacts will be notified of the problem.
44                                         # Hopefully this will restart the service successfully, so the 4th check will
45                                         # result in a "soft" recovery.  If that happens no one gets notified because we
46                                         # fixed the problem!
47                                         3)
48                                                 # Call the init script to restart the HTTPD server
49                                                 sudo /etc/init.d/${service} restart
50                                         ;;
51                                 esac
52                         ;;
53                         # The service somehow managed to turn into a hard error without getting fixed.
54                         # It should have been restarted by the code above, but for some reason it didn't.
55                         # Let's give it one last try, shall we?  
56                         # Note: Contacts have already been notified of a problem with the service at this
57                         # point (unless you disabled notifications for this service)
58                         HARD)
59                                 sudo /etc/init.d/${service} restart
60                         ;;
61         esac
62         ;;
63 esac
64
65 exit 0