#!/bin/bash

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

hname="$1"
sstate="$2"
sattempt="$3"
sout="$4"

case $sstate in
	OK)
	;;

	WARNING)
	;;

	CRITICAL)
		case $sattempt in
			1)
			;;

			2)
			;;

			*)
				asr=`echo $sout | sed -e 's/.*Current Requests=\([^[:space:]]\+\).*/\1/; s/,//g; s/\..*//'`
				echo "asr: %%${asr}%%" >> /usr/local/monitoring/log/cas_restart.log 2>&1

				if [[ "$asr" -gt "10000" ]]; then
					res=`/usr/local/monitoring/nagios/libexec/check_nrpe -p 9666 -t 60 -H $hname -c event_reset_iis 2>&1`
					#echo "$res" | /bin/mail -s "IIS restarted on $hname" jsophy@Princeton.EDU
					echo "res: $res" >> /usr/local/monitoring/log/cas_restart.log 2>&1
				fi
			;;

		esac
	;;

	UNKNOWN)
		#echo "Service state is unknown" >> /usr/local/monitoring/log/cas_restart.log
		#exit 1
	;;

	*)
		#echo "Invalid service state passed" >> /usr/local/monitoring/log/cas_restart.log
		#exit 1
	;;
esac

exit 0
