PingChecker
A simple shell script to send an Email if it can't ping a site.
Requires a Unix shell account at a site other than the one to ping
PingChecker consists of three files, pingchecker, the log file and the message file youaredown.
I use it on Loblolly.net to pingcheck LanRanger.net and vice versa. If one can't ping the other, it sends an email to my pager.
Use at your own risk and benefit if any.. Feel free to modify it any way you wish..
Question: since this is a Kinda Simple Shell Script does that mean it conforms to the KISS principle?
How it works..
1 - Pingchecker is started every 15 minutes by cron
2 - Pingchecker pings the site twice.
3 - If no error occurs in pinging the site, just touch the logfile (touch will change the date/time of the file)
4 - If an error occurs in pinging, it records it in the log file
5 - It then pings the mail server (or some other outside server to check for internet connection)
6 - If it can't ping the mail server, it records it to the log file
7 - Sends the message file to your email address.. Uses sendmail to put in a different return email address.
Could use mail to use the default return email address of your shell account.
Here's what the pingchecker shell script looks like
$cat pingchecker
# ping a site if no response then email a message
site="IP or name of site to ping"
logfile="/full pathname to/pingchecker_log"
pagefile="/full pathname to /youaredown"
pagesite="the name of the mail server after the @ in the email address"
ping -c 2 $site > /dev/null
#if 100% packet loss - a bad ping
if [ $? -gt 0 ]
then echo bad ping to $site on `date` >>$logfile
#can we connect to the mail server or some other outside server?
ping -c 2 $pagesite
# if we can't ping the outside, then record so
if [ $? -gt 0 ]
then echo bad ping to $pagesite on `date` >> $logfile
fi
# go ahead and email.. will get it sooner or later
cat $pagefile |sendmail -t
# instead of sendmail, you could use mail
#cat $pagefile | mail -s "site is down" you@youremailaddress
else touch "$logfile"
fi
Here's an example of the message file for use with Sendmail with the From, To and Subject
$cat youaredown
From: a_valid_email_address
TO: a_valid_email_address
Subject: URDOWN
We can not ping
your IP/name server..
And here's a sample of a log file, mine..
$cat pingchecker_log
bad ping to dns1.lanranger.net on Fri May 25 23:01:12 CDT 2001
bad ping to dns1.lanranger.net on Fri May 25 23:16:11 CDT 2001
bad ping to dns1.lanranger.net on Fri May 25 23:31:11 CDT 2001
bad ping to dns1.lanranger.net on Mon May 28 16:16:11 CDT 2001
bad ping to dns1.lanranger.net on Wed Jun 6 18:31:11 CDT 2001
bad ping to dns1.lanranger.net on Sat Jun 16 11:31:12 CDT 2001
bad ping to dns1.lanranger.net on Sat Jun 16 11:46:12 CDT 2001
bad ping to dns1.lanranger.net on Sat Jun 16 12:01:12 CDT 2001
bad ping to dns1.lanranger.net on Sat Jun 16 12:16:12 CDT 2001
bad ping to dns1.lanranger.net on Sat Jun 16 12:31:12 CDT 2001
bad ping to dns1.lanranger.net on Mon Jun 18 10:16:12 CDT 2001
Heres a line to put in your crontab to have it run every 15 minutes
Maintained by penguin@pingangel.com alias r david decker10,25,40,55 * * * * /full path name/pingchecker 1> /dev/null