icmp ping connection monitor script

This was one of those tasks that was quicker to write my own than it was to Google up one of the several hundred other identical monitoring scripts.

This script monitors whether or not a host is responding to ICMP and can be happily set to a resolution of 1 second. It’s written in bash and will happily run on my linux. It should run on yours, too.


./pingit 1 myHostname.dyndns.org > /var/log/myIspIsNotIdeal.log

#!/bin/sh
if [ $# -ne 2 ]
  then
  echo "Usage: $0 <timeout in seconds> <hostname to ping>"
  exit 1
fi
while true
do
  echo -n "$(date) "
  ping -q -c 1 -W $1 $2 > /dev/null
  case $? in
  1)
    echo "down"
    ;;
  0)
    echo "ok"
    sleep $1
    ;;
  2)
    echo "the damage report machine has exploded"
    ;;
  esac
done

Run it and redirect its output to a file for later grepping. Once I’m happy its bedded in and working, and I’ve finished tarting it about a bit, I’ll make it only output a line on failure.

If you’d prefer your monitoring to be slightly more capable than this, and also draw pretty rrdtool graphs of the results, then I recommend you check out Smokeping, which is in the Debian repositories and is very simple to set up.

One thought on “icmp ping connection monitor script

Comments are closed.