Pinglog


 * 1) !/bin/bash


 * 1) pinglog
 * 2) purpose: ping target and log up and down change states
 * 3) author: Kenneth Burgener  March 2011

STATE="DOWN" TARGET="$1" SLEEP=1 LOGFILE="$0.log" DOT=true LASTUP=`date +%s`
 * 1) INITIALIZE VARIABLES

if [ "$1" = "" ] ; then echo "Usage: $0 [interval]" exit 1 fi if [ "$2" != "" ] ; then SLEEP=$2 fi
 * 1) CHECK COMMAND LINE PARAMETERS

function log { echo $1 >> $LOGFILE echo $1 }
 * 1) LOG TO FILE AND SCREEN

log "= PINGING $1 EVERY $SLEEP SECONDS... ="
 * 1) HEADER

ping -c 1 -w 1 $TARGET > /dev/null if [ $? -eq 0 ] ; then log "UP:   `date`" STATE="UP" else log "DOWN: `date`" STATE="DOWN" fi
 * 1) INITIAL STATE

while true ; do
 * 1) MAIN LOOP

ping -c 1 -w 1 $TARGET > /dev/null if [ $? -eq 0 ] ; then if [ "$STATE" = "DOWN" ] ; then log "UP: `date`" STATE="UP" log "OUTAGE: $(( `date +%s` - $LASTUP )) seconds" fi else if [ "$STATE" = "UP" ] ; then log "DOWN: `date`" STATE="DOWN" LASTUP=`date +%s` fi fi

if [ $DOT ] ; then echo -n "." fi

sleep $SLEEP

done