// you’re reading...

Scripts

Simple script to alert about inaccessible servers

Following is a simple script, written in PHP, which will send an E-mail alert to you when one or more of your Web servers is inaccessible.

<?php
/* things to edit/change */

$fileToFetch = "isalive.txt";
$mailTo = "shekhar@it4enterprise.com";
$mailFrom  = "alert@it4enterprise.com";

$websites = array(
"Website 1" => "http://www.website1.com",
"Website 2" => "http://www.website2.com",
"Website 3" => "http://www.website3.com"
);

/* end of things to edit/change */

foreach($websites as $name=>$value)
  if(!($fp = @fopen($value."/".$fileToFetch,"r")))
    mail($mailTo,"WEBSITE DOWN: ".$name,"WEBSITE DOWN: ".$name,"From: ".$mailFrom);

?>


How to use this script:

  1. I’m assuming that you would run this script on a Linux server.
  2. This server is (obviously) a different server/machine from the web servers that you are monitoring. It is also advisable that you use this script on a server which is not in the same network or with the same ISP as your web servers.
  3. Substitute the values for $mailFrom, $mailTo, $websites etc. as per your setup/requirement.
  4. Save the above code in a file called isalive.php in /opt directory (for example)  on the Linux server.
  5. Create a file called isalive.txt in your web site’s root directory such that it is accessible as http://www.yourwebsite.com/isalive.txt. A typical Linux command example: cd /var/www/html; touch isalive.txt
  6. To run this script every 15 minutes, you can add an entry to Cron (crontab -e) as:

0,15,30,45 * * * * php -q /opt/isalive.php >> /dev/null 2>&1

How the script works:

  1. It tries to fetch a file (can be a 0 byte file) called isalive.txt from your website. The name of this file is specified by the variable $fileToFetch.
  2. If the file is not found it sends an E-mail to an E-mail address specified by the variable $mailTo. The mail appears to come from an E-mail address specified by the variable $mailFrom.
  3. One or more websites (to monitor) are specified in the array variable called websites.

Why isalive.txt instead of home page

You may ask why cannot the script fetch the first page of your website and why you need to create isalive.txt. Remember this script is hitting your site frequently (15 mins). The first page of your website can be a heavy (in size) or slow (a page which is doing serveral database queries) page. Whereas isalive.txt can be as small as zero byte.

Todos

Obviously there can be more to be desired from this script (making it not so simple). For example:

  1. The script should be configurable to send only periodic alerts or reminders, once a server is down. Currently the script will keep sending you alert every 15 mins till the time the server is back online.
  2. Sending alerts through SMS, possibly through some free SMS gateway. Looking for any advice/suggestion here.
  3. Perhaps the script can record the time to fetch the file and alert if the server is slow (in terms of bandwidth,load etc.)

How it works for me

Very recently, I have started using this script to monitor some servers of my clients’. As said above, ideally, I should get this alert through SMS for a timely action. But I get my E-mails almost as quick as SMSes on my mobile. Thanks to Push E-mail. I have a Windows Mobile phone and a Zimbra (www.zimbra.com) based E-mail server and I’m using Z-Push (http://z-push.sourceforge.net/soswp/) to get Push-ed E-mails. Refer to an article on setting up Z-Push at http://pcquest.ciol.com/content/enterprise/2008/108070503.asp, written by me. With preliminary testing, this is indeed a simple and working script for me.

GD Star Rating
loading...
GD Star Rating
loading...
Simple script to alert about inaccessible servers, 10.0 out of 10 based on 1 rating
Share

Email This Post Email This Post Print This Post Print This Post Print This Post Post A Comment Tweet your comments/question to me @shekharg

Discussion

2 comments for “Simple script to alert about inaccessible servers”

  1. >>>I have a Windows Mobile phone and a Zimbra based E-mail server and I’m using Z-Push to get Push-ed E-mails

    Just wondering whether you got Z-Push working with the OpenSource version of Zimbra or with one of the commercial versions.

    GD Star Rating
    loading...

    Posted by Guy Joris | January 26, 2009, 9:18 pm
  2. Just wondering whether you got Z-Push working with the OpenSource version of Zimbra or with one of the commercial versions.

    I used it very successfully with the open source edition (OSE) of Zimbra 5.0

    GD Star Rating
    loading...

    Posted by Shekhar | January 26, 2009, 11:49 pm

Post a comment