#!/bin/bash

if [[ "$#" -ne "4" ]]; then
	echo "Usage: $0 "'$SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$'
	exit 1
fi

sstate="$1"
stype="$2"
sattempt="$3"

case $sstate in
	OK)   # Recovery - enable active checks
		
		exit $?
	;;

	WARNING)  # Restart
		/usr/bin/sudo /sbin/service gearmand restart
		exit $?
	;;

	CRITICAL) # Restart on first attempt, disable active checks on second
		case $sattempt in
			1)
				/usr/bin/sudo /sbin/service gearmand restart
				exit $?
			;;

			2)
				/usr/bin/sudo /sbin/service gearmand restart
				now=`date +%s`
				commandfile='/usr/local/monitoring/nagios/var/rw/nagios.cmd'
				/usr/bin/printf "[%lu] STOP_EXECUTING_SVC_CHECKS\n" $now > $commandfile

				/bin/mail -s "ACHTUNG!!!! NAGIOS CHECKING STOPPED DUE TO GEARMAN ERROR!!! AHHHHH!!!!!" epm-list@Princeton.EDU igubenko@Princeton.EDU 3474950942@txt.att.net <<%
Gearman server has encountered an error and is not functioning properly. Service checks have been stopped to prevent process overflowing. Please check ASAP!!!
%
				exit $?
			;;

			*)
				# Do nothing - too early to do anything
			;;
		esac
	;;

	UNKNOWN)
		echo "Service state is unknown"
		exit 1
	;;

	*)
		echo "Invalid service state passed"
		exit 1
	;;
esac

exit 0
