Trisummit Technologies

Automate Website Monitoring

Posted on | June 11, 2008 | No Comments

Some instability on one of the servers I manage has brought up the need to setup automated monitoring that will send email alerts in the event that a website is down. In looking at possible solutions, I was considering several alternatives. Two of them that stood out the strongest are as follows:

1. Use Powershell on a Windows Server.
2. Use a scripting language on one of our externally hosted Linux servers.

Using Powershell did look like a viable alternative. The only problem is that I have never used Powershell before and I decided that the learning curve to implement a solution was not one that I am ready to go through. Maybe later. Nevertheless, Dmitry Sotnikov has a good blog where he describes how he did it for the http://powergui.org website using Powershell.

Monitor Website Availability

What I choose to do instead was use Perl and the Crontab to set up monitoring.

I tested the setup on three different Linux Servers–all three are Red Hat Linux Enterprise Servers version 4.0 or 5.0. Below is the script that I created using Perl:

[[code]]czo2MTA6XCINCiRtYWlscHJvZ3BhdGg9XCcvdXNyL3NiaW4vc2VuZG1haWxcJzsNCg0KdXNlIGxpYiBcJy92YXIvd3d3L2NnaS1iaW4vbGl7WyYqJl19Ynd3dy1wZXJsLTUuNjkvbGliXCc7IA0KdXNlIExXUDo6U2ltcGxlOw0KDQokbG9jYXRpb24gPSBcImh0dHA6Ly9teXdlYnNpdGUuY29te1smKiZdfVwiOw0KDQppZihnZXQoJGxvY2F0aW9uKSkNCnsNCiAgICBwcmludCBcInN1Y2Nlc3NcIjsNCn1lbHNlDQp7DQogICAgIyBuZWVkIHRvIHN7WyYqJl19ZW5kIGVtYWlsDQogICAgb3BlbiAoTUFJTCwgXCJ8JG1haWxwcm9ncGF0aCAtdFwiKSBvciBkaWUgKFwiQ2FuXCd0IGFjY2VzcyAkbWFpbHB7WyYqJl19cm9ncGF0aFxcblwiKTsNCiAgICAkcmVjaXBpZW50X2FkZHJlc3MgPSBcImJyaWFuLm5ldHRsZXNcXEBkb21haW5uYW1lLmNvbVwiOw0KICAge1smKiZdfSBwcmludCBNQUlMIFwiVG86ICRyZWNpcGllbnRfYWRkcmVzc1xcblwiOw0KICAgIHByaW50IE1BSUwgXCJGcm9tOiBkb25vdHJlcGx5XFxAZHtbJiomXX1vbWFpbm5hbWUuY29tXFxuXCI7DQogICAgcHJpbnQgTUFJTCBcIlN1YmplY3Q6ICAkbG9jYXRpb24gRG93blxcblxcblwiOw0KICAgIHByaW50e1smKiZdfSBNQUlMIFwiJGxvY2F0aW9uIHJlcXVlc3QgdGltZWQgb3V0LiAgV2Vic2l0ZSBtYXkgYmUgZG93bi5cXG5cIjsNCiAgICBjbG9zZSAoTUF7WyYqJl19SUwpOw0KfQ0KXCI7e1smKiZdfQ==[[/code]]

So on the Linux server, I went into the directory where I store my crontab’d files and created a file called monitorwebsites.pland populated the file with the above script. I then tested the script by calling the following:

perl monitorwebsites.pl

It works well.

On one of the servers, I had an issue with use LWP::Simple;. The perl script would not run on that particular server as the LWP module was not loaded. I found some good instructions on how to load that module at the following url:

http://forums.dixiesys.com/index.php?showtopic=2588

The next thing I needed was to schedule the Perl script in the Crontab. If you don’t know what the Crontab is, that is a scheduling device built into Linux/Unix systems to run automated tasks. I found some great instructions on how to setup the Crontab on Linux machines at the following URL:

http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/

The first command here opens up the crontab so that you can add an entry. The second line below is the entry that calls on the script every fifteen minutes.

sudo crontab -e
*/15 * * * * perl /directoryforscripts/monitorwebsites.pl

This was pretty simple. Took me about 4 hours total to research, write, and implement this.

Comments

Leave a Reply