#!/bin/sh

# Simple dmonitor that pings a host and makes a report with a priority
# level related to the number of packets lost.

# Usage: esmping host times 

# Host is the host to ping, you should probably use a numerical IP
# address to avoid DNS dependency. times is the number of pings sent,
# optional and defaults to 100


# The dmonitor stuff is tricky. We need to "fork" off another esmping
# process that does the pinging for us. Then we need to do the reading
# and watch for the STAT string. The second esmping process will do
# the notifying of esmd should anything bad happen.

# Communication is done via a tmp file. The second esmping process,
# the pinger, appends it's findings to the tmp file. As it appends it
# also checks if the urgency is enough to warrent notifying the user.

# We just sit and read stdin. When we get the STAT command we echo the
# headers and cat the tmp file. Then we delete the tmp file.

# The forking is done by just running esmping again but with the
# environment variable ESMPINGTMPFILE set.


# before we do anything set umask to 700 so no-one can read anything
# of ours
umask 077

# EMPINGTMPFILE set?
if [ "$ESMPINGTMPFILE" ]
then

    LASTFORCECHECK=0

    while [ -f /var/run/esmd.pid ]
    do

	# set pk_count to the second argument or 10
	if [ "$2" ]
	then
	    PK_COUNT="$2"
	else
	    PK_COUNT="100"
	fi
	
	# get the number of packets lost
	PACKETLOSS=`ping $1 -q -c $PK_COUNT | grep "packet loss" | cut -f 7 -d " " | cut -f 1 -d "%"`
	
	# Now figure out the urgency. If 100 of the packets are lost set to
	# alert, if 50+ set to crit, if 10+ set to err, if 1+ set to warning,
	# if <1 set to info
	if [ "$PACKETLOSS" -eq "100" ]
	then
	    URGENCY=7
	elif [ "$PACKETLOSS" -gt "50" ]
	then
	    URGENCY=6
	elif [ "$PACKETLOSS" -gt "10" ]
	then
	    URGENCY=5
	elif [ "$PACKETLOSS" -gt "5" ]
	then
	    URGENCY=4
	elif [ "$PACKETLOSS" -gt "1" ]
	then
	    URGENCY=3
	else 
	    URGENCY=2
	fi
	
	# Don't print unless the urgency > 2
	if [ "$URGENCY" -gt "2" ]
	then
	    echo Pinged $1 $PK_COUNT times - $PACKETLOSS% packets lost >> $ESMPINGTMPFILE
	fi

	# urgency higher then highest urgency? 
	if [ -f $ESMPINGTMPFILE.pri ]
	then
	    if [ "$URGENCY" -gt "`cat $ESMPINGTMPFILE.pri`" ]
	    then
		echo $URGENCY > $ESMPINGTMPFILE.pri
	    fi
	else
	    echo $URGENCY > $ESMPINGTMPFILE.pri
	fi

	# urgency high enough to warn user?
	if [ "$URGENCY" -gt "4" ]
	then
	    # have we already sent one force check less than 30 minutes ago?
	    TMPDATE=`date +%s`
	    TMPDATE=`expr $TMPDATE - 1800`
	    if [ "$TMPDATE" -gt "$LASTFORCECHECK" ]
	    then
		LASTFORCECHECK=`date +%s`
		kill -SIGUSR1 `cat /var/run/esmd.pid`
	    fi
	fi

    done
else 
    # set ESMPINGTMPFILE
    export ESMPINGTMPFILE=/tmp/esmping.`date +%H%M%S` 

    # Run pinger. This is guaranteeded to work because the path always
    # includes the plugin directory first.
    esmping $1 $2 &

    # read from stdin
    while [ -f /var/run/esmd.pid ]
    do
	read $INPUT
    
	echo BEGIN 0.01
	# make sure the priority file exists
	touch $ESMPINGTMPFILE.pri
	if [ -f $ESMPINGTMPFILE.pri ]
	then
	    echo URGENCY `cat $ESMPINGTMPFILE.pri`
	else
	    echo URGENCY 0
	fi
	echo REPORT
	if [ -f $ESMPINGTMPFILE ]
	then
	    cat $ESMPINGTMPFILE
	else
	    echo No errors.
	fi
	echo DONE   

	# delete tmp files
	rm $ESMPINGTMPFILE 2> /dev/null
	rm $ESMPINGTMPFILE.pri 2> /dev/null
   done

fi

# on exit due to esmd exiting clean up tmp files
rm $ESMPINGTMPFILE 2> /dev/null
rm $ESMPINGTMPFILE.pri 2> /dev/null
