Ping-per-second / Notify Script

As a sysadmin (probably like you), sometimes I need to whip up special-purpose mini-scripts to do simple monitoring tasks that I don’t want to bother our main monitoring system with… This is one of them…

I needed to monitor a network device (an appliance, actually) constantly and rapidly, to see if the interface was flapping up / down.  So, we settled on one ping per second, with a one-time email notification when it drops, and a one-time notification when it returns.

Below is that script;  all you have to do is copy the contents into a text file (watch out for line wrapping), change the “PINGDEST” and “EMAILADDRESSES” variables, flag it as executable (chmod 700 <scriptname>) , and you should be good to go!

#!/bin/bash

PINGDEST="192.168.1.12"
EMAILADDRESSES="jpavlov@yourLinuxGuy.com,questions@yourLinuxGuy.com"
BITFLAG=0
SENTMAIL=0

while true
do
   /bin/ping -q -c1 -W1 $PINGDEST > /dev/null 2>&1
   PINGRESULT="$?"
   if [ "$PINGRESULT" != "0" ]; then
      BITFLAG=1
   else
      BITFLAG=0
      sleep 1
   fi
   if [ "$BITFLAG" == "1" ] && [ "$SENTMAIL" == "0" ]; then
      echo "Ping to $PINGDEST FAILED! Generated by $0 on $HOSTNAME" |mailx -s "Ping failure to $PINGDEST"  $EMAILADDRESSES
      SENTMAIL=1
   elif [ "$BITFLAG" == "0" ] && [ "$SENTMAIL" == "1" ]; then
      echo "Ping to $PINGDEST resumed. Generated by $0 on $HOSTNAME" |mailx -s "Ping responding again on $PINGDEST"  $EMAILADDRESSES
      SENTMAIL=0
   fi
done

A goofy, single-purpose script, to be sure; but you never know, maybe it will help someone…

8)

Leave a Comment

Your email address will not be published.