In order to make sure that your website is always available to the public, you have to monitor it. In this tutorial, I'll show you how you can easily create a monitoring script that will check your website availability and send an email or sms alert to you if it isn't.

Prerequisites

Maybe I’m stating the obvious, but the PHP script has to be on a different server than the one used for the website you’d like to monitor. If the script is hosted on the same server as your site, then it becomes pretty useless: In fact, if your server is down the script will not be able to run and will not let you know.
The best solution is of course a dedicated server, but a home server can be ok as well. Shared web hosting like those provided by Hostgator or WpWebHost have a low price, but most don’t allow you to set up cron jobs, so be careful if you plan to buy.

The last part of this tutorial will show you how to get sms alerts using Gmail. Please note that depending on your location and cellular phone provider, this part of the tutorial may not work.

1. Creating the monitoring script

The first part of this tutorial is to create the monitor script. Pick up your favorite text editor and create a file named monitor.php. The script is very simple: we only need two functions, one to test if a specific site is available, and the other to alert you by sending an email.

Paste the following in your monitor.php file:

function check($host, $find) {
    $fp = fsockopen($host, 80, $errno, $errstr, 10);
    if (!$fp) {
        echo "$errstr ($errno)\n";
    } else {
       $header = "GET / HTTP/1.1\r\n";
       $header .= "Host: $host\r\n";
       $header .= "Connection: close\r\n\r\n";
       fputs($fp, $header);
       while (!feof($fp)) {
           $str .= fgets($fp, 1024);
       }
       fclose($fp);
       return (strpos($str, $find) !== false);
    }
}

function alert($host) {
    mail('youremail@gmail.com', 'Monitoring', $host.' down');
}

$host = 'www.catswhoblog.com';
$find = 'Cats Who Code';
if (!check($host, $find)) alert($host);

The first function we created here, check(), takes two parameters: The first is the server you’d like to check availability (for example, www.catswhocode.com) and the second parameter is to find some text on the webpage. This second parameter is an additional security: In fact, by checking if a specific word is contained on the site homepage, we ensure that the site content hasn’t been modified, for example, after a hacking.

If the server isn’t available or if the text to find hasn’t been found, the alert() function is executed, and will send an email to the account of your choice.

Save the monitor.php file and upload it to your monitoring server.

2. Defining a cron job

At this point of the tutorial, we have a working monitoring script, but we have to type http://mymonitoringserver.com/monitor.php in a web browser to check our website, which makes our script almost useless.
The solution to that problem is to create a cron task to make the server execute monitor.php every hour. Open a SSH console to your monitor server and type the following:

0 * * * * /usr/local/bin/php -q /htdocs/www/monitor.php

If your PHP scripts do not have executable permissions, 644 or -rw-r–r– for example, then as part of the command portion of the cron line, you must specify the php interpreter and pass it the filename of the PHP script (including full path to the script from your home directory) that you want executed.

3. Setting up SMS alerts

Right now, we have a working monitoring PHP script, as well as a cron job that will execute the script every hour. If a problem happens, you’ll receive an email.
Due to the popularity of iPhones, Blackberries and other SmartPhones, a lot of people are able to receive emails everywhere they are. Though, some are still using cell phones with no email capability. In this optional step of the tutorial, let’s see how we can easily receive alerts by sms.

Doing so is quite easy. First you have to use Gmail. As it is a free service, you can create a dedicated account for your monitoring alerts. Once finished, login to your account and click on the “Settings” link located on the top right side of the browser window.
Then, select “Forwarding and POP/IMAP”:

As shown in the previous screenshot, the only thing you have to do is to check “Forward a copy of incomming mail” and fill the field with your phone number @ your provider service.
For example, If your phone provider is AT&T, you’ll have to type 0000000000@mobile.att.net.

 

22 Comments

  1. Posted February 8, 2010 at 7:17 pm | Permalink

    LOL, my webhosting has an uptime of over 476 days. Still Dugg.

  2. Posted February 8, 2010 at 8:48 pm | Permalink

    So many thanks for this article. I’ve been trying to figure this out for a few weeks now (in between my more important tasks), so this couldn’t have arrived at a better time. Using a monitoring service in the interim, but really glad I can now roll my own.

    BTW, I found the following resource helpful when messing around with the Cron job, which was the part I hadn’t done before. I didn’t know what the ‘-q’ flag was intended for, but after reading php.net/manual/en/features.commandline.php I discovered it’s to prevent headers from being emitted when the script is called- makes sense.

    Thanks again!

    Trav

  3. Posted February 8, 2010 at 10:37 pm | Permalink

    One other comment: I’ve noticed that it seems to stumble when it comes to dealing with .htaccess redirects; specifically, the 301’s in my site are causing a false positive. Wondering if you have any thoughts on troubleshooting.

  4. Posted February 9, 2010 at 1:06 am | Permalink

    A nice useful article! I wonder how other people do this kind of thing? Do they maybe use a similar script?

  5. Ben
    Posted February 9, 2010 at 2:55 pm | Permalink

    @Trav

    If you use CURL you can tell it to follow redirect headers.

  6. Ben
    Posted February 9, 2010 at 3:16 pm | Permalink

    It may be better to use CURL instead – then you can easily deal with redirects etc.

  7. That Guy
    Posted February 9, 2010 at 4:36 pm | Permalink

    So you would need two Web Servers.. Your Main one and one to Monitor it… And if the monitoring webserver were to go down too?

    Wouldn’t it be wiser to use a desktop application/service to check periodically? There are a few phone apps that will do this over cell/edge/3g.

  8. Posted February 9, 2010 at 5:10 pm | Permalink

    I assume this script checks one site and that I’d have to recreate it multiple times to check multiple sites. If that’s the case, as a non-programmer I’m wondering how hard it would be to create a script that monitors multiple sites that I own. NIce post either way.

  9. Posted February 9, 2010 at 5:44 pm | Permalink

    @Tom : Checking multiple sites is not hard at all. In the PHP script, you should list all sites to be checked in an array and then use a simple foreach loop to check them all.

  10. chrisweb
    Posted February 10, 2010 at 3:17 am | Permalink

    very nice!

  11. Posted February 11, 2010 at 1:41 pm | Permalink

    Or you could use a service like AreMySitesUp…. Had a seduction to insert an affiliate link there, but really, I’m telling just because such a service exists, not because anything else.

    • Posted February 11, 2010 at 5:25 pm | Permalink

      Affiliate link in comments = spam ;) But I agree with you that AreMySite up is a good (and quite cheap) monitoring service.

  12. Posted February 12, 2010 at 4:51 am | Permalink

    Cool, will look at doing this for the company blog. Has anybody ever used Pingdom for monitoring uptime?

    Now, who’s gonna write a tutorial for how to do this with ASP.NET ;)

  13. Posted February 12, 2010 at 10:30 am | Permalink

    Very helpful. Thanks!

  14. Posted February 12, 2010 at 12:43 pm | Permalink

    very helpful article, thanks for sharing.

  15. Posted February 12, 2010 at 2:04 pm | Permalink

    Nice tips, thanks, my PHP server is in need of some TLC

  16. Joe King
    Posted February 15, 2010 at 4:59 pm | Permalink

    “If the script is hosted on the same server than your site, it becomes pretty useless:”

    This should read,
    “If the script is hosted on the same server AS your site, then it becomes pretty useless:”

    Also, hostgator permits cron-jobs, but you have to ask them to turn it on!

  17. Posted February 25, 2010 at 3:15 pm | Permalink

    Playing around now with cURL, and there are a TON of options for configuring the kind of requests you send, as well as parsing the responses. For newcomers though, the basic script above by Jean-Baptiste is perfect for a simple easy-to-use monitor- even for more than one site (just put the sites in an array and loop through as mentioned.)

    I’ll post back if I’m able to work up something similarly simple with cURL.

  18. Posted March 15, 2010 at 3:13 pm | Permalink

    Nice article, shall pass this onto our customers, I think it’s good when people are monitoring their sites.

    We’re using a combination of Wormly.com for internal/external monitoring with SMS alerts, an internal monitoring system in our office and also we developed a Wordpress plugin for external monitoring (it pings all the services on every server every 30 seconds and updates via Ajax) so our clients can see our server status via our externally hosted status blog.

  19. Posted March 16, 2010 at 8:46 am | Permalink

    Nice idea to use Gmail free service for this monitoring.
    Good thinking
    :)

4 Trackbacks

  1. [...] In a new post to the CatsWhoCode.com blog Jean-Babtiste Jung walks you through the creation of a simple monitoring script written using just PHP that can tell you if your web site is up and [...]

  2. By February 12, 2010 – Links | MarcJStuff on February 12, 2010 at 2:26 pm

    [...] How to easily monitor your web server using PHP [...]

  3. [...] How To Easily Monitor Your Web Server Using PHP [...]

  4. [...] How to easily monitor your Web server using PHP [en] Partagez ce billet : [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting

  • Smashing Network
WordPress Appliance - Powered by TurnKey Linux