Mar 012013
 

One of my headless wireless devices have been freezing lately, and since it’s in a somewhat isolated location, I have no idea when it freezes.
So I wrote up a quick script to check to see whether it’s up and I’m running it every hour so that it alerts me when it goes down. It will also alert me once it comes back up, in case it comes back up by itself !

The script will also save the last state that the device was in so that it will only email me once when the device goes down.


#!/bin/bash
email=YOUR@EMAIL.HERE

ip=$1
if [[ -z $1 ]]; then
echo Usage: ./pingcheck.sh \
exit
fi
if [ ! -f /tmp/$1.status ]; then
touch /tmp/$1.status
fi

oldstatus=`cat /tmp/$1.status`
reply=`ping -c 1 $ip | grep 64\ bytes | wc -l`
echo $reply > /tmp/$1.status

if [[ $oldstatus -ne $reply ]]; then
if [[ $reply -eq 1 ]]; then
echo $1 is online | mail -s "Node is online" $email
else
echo $1 is offline | mail -s "Node is offline" $email
fi
fi

Share

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.