#!/bin/sh
#
# Event handler script for restarting a service on the local machine
#
# Note: This script will only restart the service if the service is
#       retried 3 times (in a "soft" state) or if the service somehow
#       manages to fall into a "hard" error state.

# Args:
# $1 $SERVICESTATE$
# $2 $SERVICESTATETYPE$
# $3 $SERVICEATTEMPT$
# $4 $HOSTADDRESS$
# $4 init script name

state="$1"
type="$2"
attempt="$3"
host="$4"
starton="$5"
service="$6"

# What state is the service in?
case "${state}" in
	"${starton}")
	# Aha!  The service appears to have a problem - perhaps we should restart it
	# Is this a "soft" or a "hard" state?
		case "${type}" in
		# We're in a "soft" state, meaning that Nagios is in the middle of retrying the
		# check before it turns into a "hard" state and contacts get notified...
			SOFT)
			# What check attempt are we on?  We don't want to restart the service on the first
			# check, because it may just be a fluke!
				case "${attempt}" in
					# Wait until the check has been tried 3 times before restarting the service
					# If the check fails on the 4th time (after we restart the service), the state
					# type will turn to "hard" and contacts will be notified of the problem.
					# Hopefully this will restart the service successfully, so the 4th check will
					# result in a "soft" recovery.  If that happens no one gets notified because we
					# fixed the problem!
					3)
						# Call the init script to restart the HTTPD server
						/usr/lib/nagios/plugins/check_nrpe -n -H "${host}" -c "${service}"
					;;
				esac
			;;
			# The service somehow managed to turn into a hard error without getting fixed.
			# It should have been restarted by the code above, but for some reason it didn't.
			# Let's give it one last try, shall we?  
			# Note: Contacts have already been notified of a problem with the service at this
			# point (unless you disabled notifications for this service)
			HARD)
				/usr/lib/nagios/plugins/check_nrpe -n -H "${host}" -c "${service}"
			;;
	esac
	;;
esac

exit 0
