May 01 2009
A Simple Monitoring Script to monitor devices or hosts

At work we have a few routers, switches and servers that if they go down we want to know right away. The last thing we want is the CEO coming to our desk asking why the mail server is down or why the webserver it not responding.
I wrote a little script a very simple one that given a list of hosts and IP addresses will ping a host and if it is detected as down it will send you an email right away.
########## Instructions on how to use this monitor script ############
Make a directory called scripts
mkdir /scripts then inside create using your favorite text editor the following script
#!/bin/bash
# Ping Alert Monitor by Marco Maldonado V 1.0
# Create a hosts file and save it somewhere on your server
# specify a path and an email address where you want to send alerts
#
# Note: Your hosts file should have the IP address of the host followed by a space and the name
# of the server: Example Below
#
# 69.147.76.15 www.yahoo.com
# 209.85.165.147 wwww.google.com
#
# Path to your hosts file
HOSTS="/scripts/hosts"
# Email address where to send alerts, you can add more addresses followed by a comma
EMAIL="yourname@yourdomain,someaddres@gmail.com"
### Do not edit anything below ############
for myHost in `cat $HOSTS | cut -d' ' -f1` ; do
ping -c 1 $myHost > /dev/null
if [ $? -ne 0 ] ; then
echo “Server `grep $myHost $HOSTS` is down reported down at `date`” | mail -s “Ping Alert Server `grep $myHost $HOSTS` might be down” $EMAIL
fi ;
done
replace the email addresses listed there you can put only one or multiple email addresses separated by a comma. I put my gmail address because in case my mail server goes down i can still get a report to another address.
Save the script and make it executable chmod 755 /scripts/ping_alert.sh
Create a host file of the hosts you want to monitor starting with the IP address of the server and then some details about the server example
10.1.29.100 Exchange Server
120.12.45.2 External webserver www.yourdomain.com ( Phone support 212-453-### )
10.1.30.22 Cisco Catalist for Segment XYZ
10.1.30.33 Lexmark TCP IP Printer
save the file and name it hosts. You are almost done.
Now create a crontab entry on your crontab file that will monitor the above IP addresses by the minute.
here is a quick way to edit your crontab.
crontab -l > mycrontab.txt
this will dump your crontab into the mycrontab.txt file. Edit that file and add the following line to it.
* * * * * /scripts/ping_alert.sh >> /dev/null 2>&1
as an example here is my current crontab.
[root@penguincares scripts]# crontab -l > mycrontab.txt
[root@penguincares scripts]# more mycrontab.txt
* * * * * /usr/bin/php /opt/projects/mrtg/serverstats/update.php >> /dev/null 2>&1
* * * * * /scripts/ping_alert.sh >> /dev/null 2>&1
*/5 * * * * php /opt/projects/mrtg/cacti/poller.php > /dev/null 2>&1
Then to load this new crontab all you have to do is type
crontab mycrontab.txt and that will load the new changes that you added.
######### NOW WHAT ? #################
That’s it your crontab entry is monitoring those devices by sending a ping request it they go down
you will get an email right away telling you which IP address failed. You can test to see if it is working or not by putting a fake ip address and a server name next to it.
When the script detects the downed server you should recieve and emal saying the following
Subject:Ping Alert Server 216.21.3.33 Exchange Server might be down
Body: Server 216.21.3.33 Exchange Server is down reported down at Fri May 1 17:07:11 EDT 2009
This is a simple monitor not the greatest way to monitor a server but you can be sure you will know when a certain server or device fails. I will be adding other checks to this script in about a few more days, one of the checks i will add is to stop sending email right after 5 alerts. This is to prevent having hundreds of emails flooding your inbox.
I’ve been running the script for a few weeks and it has helped me catch problems on our network and people always ask me how do i know when things go down so fast :) well know you know.
Good luck Penguins
