This is a Unix shell script which will monitor the online status (network availability) of another system, using the humble-but-ever-powerful ping command. If ping finds the other system, a notation is written in the log file. If the system is not found (i.e. ping returns no trace) an administrative alert is sent via SMS to the sysadmin's mobile.
The LAN is fairly typical, connecting through a firewall-protected gateway system and out through a firewalled router box, with a small DMZ betwixt the two, occupied by a Unix/Apache web server..
A couple of caveats to start off with. The system depends on one box (web server) monitoring another box (gateway), and vice versa. So both the boxes need independent web access to use my account at the online SMS Server at messagenet.com.au. If they both go down or if the router connection to the web goes belly-up, then no dice, no SMS.
For background on setting up the SMS aspects, see Sending SMS and Email Messages including using the third-party sendEmail function.
The following script is based on one I devised for a DHCP Email Alerter. It runs every five minutes from a cron job on the web server and looks like this in its draft version 1 format..
/sbin/ping -c1 gateway > /dev/null
if [ $? -ne 0 ]
then
/sc/gateway/gatewaydownemail.sh
else
echo `date +%H:%M.%S` "gateway is on line.." >> /sc/gateway/gatewayuptime
fi
Note that /sc is a directory that I have created for my custom scripts and is in the system path.
The first line of the script hits the destination system gateway with a single ping and discards any unwanted output to /dev/null. Note that the full paths to all files must be used, as the script runs from cron.
The if-then-else loop then tests for a response. If there is no response to the ping request, the script runs a second script, gatewaydownemail.sh which sends an administrative alert (see the sendEmail script under Sending SMS and Email Messages).
A positive response causes the script to print the time, followed by "gateway is on line.." into the log file gatewayuptime. This isn't strictly necessary but it indicates the script is working and the log file can be deleted as required.
Script final version
An issue with the script so far is that it runs every five minutes to enable the timely alerting of a downed (or unavailable) system, but it will keep sending an SMS every time it runs. This may be desirable, which is one of the reasons I have left it in. However a slight modification will make the script send only a single SMS message..
if [ -f /sc/gateway/go ]
then
/sbin/ping -c1 gateway > /dev/null
if [ $? -ne 0 ]
then
/sc/gateway/gatewaydownemail.sh
rm /sc/gateway/go
fi
else
echo
fi
An extra test has been added. The script now looks in the directory containing the logs and searches for a zero-byte size file called go (cunningly created before the script ran).
If the script finds the /sc/gateway/go file, it will continue with the ping. If it doesn't find the go file, it will else out to echo nothing and finish. So the script runs as long as go exists.
However if the ping test failed to get a response, after the administrative alert is sent the script deletes /sc/gateway/go. Thus subsequent alerts will not be sent until the sysadmin recreates the go file (touch /sc/gateway/go).
Finally the script no longer writes to a log file, but see /var/logs/cron to check in with the regular execution of the script as a cron job.
- A.