#!/bin/bash

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

hname="$1"
sname="$2"
sstate="$3"
stype="$4"
sattempt="$5"
sout="$6"

nagios="/usr/local/monitoring/naemon"
commands="plugins"
nrpe="check_nrpe"
nrpe_port="9666"
shutcmd="shutdown"

sout_re='Failed to fetch.*Answer: 500|Unable to login to Adobe Rights Management. Login failed|Web server returned 500'

case $sstate in
	OK)   # Recovery
		exit 0
	;;

	WARNING)
	;;

	CRITICAL)
		case $sattempt in
			[3-5])
				#if [[ "$sout" =~ $sout_re ]]; then
					for attmpt in 1 2 3; do
						outp=`${nagios}/${commands}/${nrpe} -H $hname -p $nrpe_port -c $shutcmd`
						stat=$?

						echo -n "$(date): " >> /usr/local/monitoring/log/auto_restart.log
						echo -n ${nagios}/${commands}/${nrpe} -H $hname -p $nrpe_port -c $shutcmd >> /usr/local/monitoring/log/auto_restart.log
						echo ". Result: $outp" >> /usr/local/monitoring/log/auto_restart.log

						if [[ $outp =~ "No output available from command" ]]; then
							echo "The server $hname has been successfully sent a restart sequence on attempt $attmpt" | /bin/mail -s "Server Restarted" iam@Princeton.EDU epm-list@Princeton.EDU
							break
						fi
					done

					if ! echo "$outp" | grep -q "No output available from command"; then
						#echo "The server $hname has been sent a restart sequence but we are not positive it worked. The result was \"$outp\". Please check at your convenience." #| /bin/mail -s "Server *might have* Restarted" epm-list@Princeton.EDU iam@Princeton.EDU
						echo "The server $hname has been sent a restart sequence but we are not positive it worked. The result was \"$outp\". Please check at your convenience." | /bin/mail -s "Server *might have* Restarted" epm-list@Princeton.EDU iam@Princeton.EDU
					fi
				#fi

				exit $stat
			;;

			*)
				# 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
