#!/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"

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

	WARNING)
	;;

	CRITICAL)
		case $sattempt in
			2)
				res=`/usr/local/monitoring/princeton/clearpass_restart "$hname" 2>&1`
				stat=$?
				echo "$(date): $res" >> /usr/local/monitoring/log/clearpass_restart
				echo "$res" | mail -s "ClearPass services restarted by monitoring on $hname" dw10@Princeton.EDU drinker@Princeton.EDU igubenko@Princeton.EDU
				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
