[SATLUG] program or cron job to ping internal devices & email

Charles Hogan cd_satl at futuretechsolutions.com
Wed Jan 16 14:35:50 CST 2008


Tom Weeks wrote:
> On Wednesday 16 January 2008 11:07:48 Bruce Dubbs wrote:
>> twistedpickles wrote:
>>> Does know or have a program or cron job that can ping network devices and
>>> if down send out an email?
>> You can try some variation of:
>>
>> #!/bin/bash
>>
>> function pingit
>> {
>>   echo -n "Pinging $1 "
>>   s=`ping -w$2 -q $1`
>>   field=-f18
>>   echo $s | grep duplicate > /dev/null && field=-f20
>>   loss=`echo $s|cut -d" " $field`
>>   echo "--  $loss packet loss ($3)"
>> }
>>
>> if [ $# -eq 0 ]; then timeout=10; else timeout=$1; fi
>>
>>
>> pingit 172.24.0.3   $timeout phobos0
>> pingit 172.24.0.5   $timeout venus0
>> pingit 172.21.0.3   $timeout deimos0
>>
>> ---------
>>
>> Note that there are a couple variations of the ping program.  You want
>> the one from iputils: http://www.linux-foundation.org/en/Net:Iputils
> 
> I wrote something similar to this back in the day when I was graphing my ISPs 
> uptime (provided network availability).  
> 
> Matt.. one way of easily doing this yourself too is to rely on ping's exit 
> status in bash of $?.  After doing a couple of "ping -c 1 $target", and echo 
> (or save) $?, it will=0 if the ping was successful, or >=1 if it did not 
> reach the destination.
> 
> 
> Like this if successful:
> 	STATUS=$(ping -c 1 xcssa.org >/dev/null 2>&1;echo $? )
> 	echo $STATUS
> 	0
> or this if unsuccessful:
> 	STATUS=$(ping -c 1 xxx >/dev/null 2>&1;echo $? )
> 	echo $STATUS
> 	2
> 
> Based on this, and a little more scripting and time delays (sleep 30s) and 
> fileredirection (like "echo $STATUS >> ping-status_$(date +%Y-%m-%d)" ) you 
> can easily come up with a system to monitor systems.
> 
> However... if you're really not wanting to write something like this.. then 
> what they others were suggestions about both clientless (and client based) 
> monitoring systems like bbrother, nagios, etc are also good, ready to go 
> systems.
> 
> Tweeks

I've also done something similar with a rather ugly little perl script 
that I used when I was on an unstable DSL connection.  If it could not 
ping out, it would call a program to power cycle the DSL modem, but 
could be made to do almost anything.  If interested, I could look for 
it, and pass it along.

Charles



More information about the SATLUG mailing list