#!/bin/sh 
#
# $Id$
#
# use to check whether ser is still responding; if not mail an alert
#


NOTIFY=sr@iptel.org
SIPSAK=/home/srouter/sipsak/sipsak
SIPURI=sip:sipsak@iptel.org
LOCKDIR=/var/lock
LOCKFILE=serresponse
LOCK_TIMEOUT=240
TMP=/tmp/serresponse.$$
MAILCOMMAND=/bin/mail
HOSTN=`hostname`
SMSSEND=/home/sms/smstools/bin/putsms
SMSDEVICE=/dev/ttyS0
SMSNUMBERS="491795061546"

############################

LOCKF=$LOCKDIR/$LOCKFILE
TMP2=$TMP.dns
TMP3=$TMP.ips
SIPSAKCMD="$SIPSAK -v -s $SIPURI"
SIPSAKNCMD="$SIPSAK -v -n -s $SIPURI"
SMSCMD="$SMSSEND -d$SMSDEVICE -b9600"

if [ -e $LOCKF ] ; then
	find $LOCKDIR -name $LOCKFILE -amin +$LOCK_TIMEOUT -exec rm {} ';'
	if [ ! -e $LOCKF ] ; then
		echo "This is a reminder !!!" > $TMP
		echo "The lockfile $LOCKF" >> $TMP
		echo "was just removed because ist was older then $LOCK_TIMEOUT minutes." >> $TMP
		echo "But if you receive this mail the cause of this error still exists or respawned." >> $TMP
	fi
fi

if [ ! -e $LOCKF ] ; then
	if [ ! -x $SIPSAK ] ; then
		echo "serresponse did not find the required sipsak executable $SIPSAK" >> $TMP
	else
		date >> $TMP2
		echo "  $SIPSAKCMD" >> $TMP2
		echo "produced this output:" >> $TMP2
		$SIPSAKCMD >> $TMP2 2>&1
		if [ $? -eq 3 ] ; then
			grep "Connection refused" $TMP2
			if [ $? -eq 0 ] ; then
				sleep 15
			fi
			date >> $TMP3
			echo "  $SIPSAKNCMD" >> $TMP3
			echo "produced this output:" >> $TMP3
			$SIPSAKNCMD >> $TMP3 2>&1
			if [ $? -le 1 ] ; then
				echo "ser did not responses (fast enough) on the sipsak requests with fqdn in Via" >> $TMP
				echo "but the test with IPs in Via succeeded." >> $TMP
				echo "" >> $TMP
				echo "Sending this alert is stopped for $LOCK_TIMEOUT minutes." >>$TMP
				echo "If you want to re-enable alerts sooner, please remove the lock file" >> $TMP
				echo "$LOCKF @ $HOSTN" >> $TMP
				echo "(you presumably need to be root to do this)" >> $TMP
				echo "" >> $TMP
				echo "Command output of sipsak with fqdn in Via follows:" >> $TMP
				cat $TMP2 >> $TMP
			else
				echo "ser did not responses (fast enough) on requests with fqdn in Via" >> $TMP
				echo "but also requests with IPs in Via failed." >> $TMP
				echo "" >> $TMP
				echo "Sending this alert is stopped for $LOCK_TIMEOUT minutes." >>$TMP
				echo "If you want to re-enable alerts sooner, please remove the lock file" >> $TMP
				echo "$LOCKF @ $HOSTN" >> $TMP
				echo "(you presumably need to be root to do this)" >> $TMP
				echo "" >> $TMP
				echo "First command output with fqdn in Via:" >> $TMP
				cat $TMP2 >> $TMP
				echo "" >> $TMP
				echo "Second command output with IPs in Via:" >> $TMP
				cat $TMP3 >> $TMP
			fi
			rm -f $TMP3
			rm -f $TMP2
		elif [ $? -eq 2 ] ; then
			echo "The ser response test failed due to a local error on" >> $TMP
			echo "host $HOSTN ." >> $TMP
			echo "" >> $TMP
			echo "Sending this alert is stopped for $LOCK_TIMEOUT minutes." >>$TMP
			echo "If you want to re-enable alerts sooner, please remove the lock file" >> $TMP
			echo "$LOCKF @ $HOSTN" >> $TMP
			echo "(you presumably need to be root to do this)" >> $TMP
			echo "" >> $TMP
			echo "Command output of sipsak with fqdn follows:" >> $TMP
			cat $TMP2 >> $TMP
			rm -f $TMP2
		else
			rm -f $TMP2
			rm -f $TMP
		fi
	fi
	
	if [ -e $TMP ] ; then
		$MAILCOMMAND -s "serresponse failed" $NOTIFY < $TMP
		rm -f $TMP
		touch $LOCKF
		for i in $SMSNUMBERS; do
			$SMSCMD $i "serresponse failed. please check your emails for details"
		done
	fi
fi