How to easily monitor your web server using PHP

by Jean. 47 Comments -

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.

Comments (47) - Leave yours

  1. Trav said:

    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

  2. Trav said:

    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.

  3. That Guy said:

    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.

  4. Tom said:

    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.

  5. Eugen R. said:

    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.

  6. Brady said:

    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 ;)

  7. Joe King said:

    “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!

  8. trav said:

    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.

  9. Tim said:

    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.

  10. Johnson Jia said:

    Well, i love this piece of code, actually most shared web hosting support cron jobs, even they don’t , you can use some free one like setcronjob.com.

  11. Josh said:

    Nice script, but instead of a cron job I used a header refresh:

    Of course you could also use a meta or javascript refresh as another alternative to a cron job.

    • CynderR said:

      Nope, not at all.
      If you want you can replace the email address with mail-to-sms gateway column for your provider from this list
      http://en.wikipedia.org/wiki/List_of_SMS_gateways

  12. Alex Savitra said:

    Great. My host doesn’t allow cron jobs. In order to start this script, I called it monitoringscript.gif, added a php handler to gif files, and now call it from my another site. So it works like this: every user that comes to my site #1, calls this script and it checks site #2.

  13. LowJoe said:

    Am I the only one that had to fix $str ?

    I needed to add this:

    $str = “”;

    Now it works fine. PHP didn’t like the sudden $str .= “”;

  14. LowJoe said:

    To clarify, I had to do this:

    } else {
    $str = “”; # <– I had to add this
    $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); # <– PHP didn't like this

  15. Mihai said:

    I’m not a php programmer, but I had to figure myself that first line of the monitor.php file must be “<?php" or else the file will output just text. And now a question: some of readers suggest that is a simple way to monitor several websites making minor changes to the file. Can you give us the entire source for this to work ? And finally, to give the right credits: the script works great.

  16. CynderR said:

    Thanks so much for the script!
    Got it up and running in under 5 min and its working great!
    For anyone trying to find a string to match there site (HTML tags included) just add
    echo ”.$str.”;
    to line 15 (right after the while loop) and then you can select any text on the page to search for
    IE. $find = ‘Welcome world!!!’;

  17. Jeroen said:

    Why do you setup a gmailaddress only to forward an email? You can just change the email of you monitor script an sent it directly to att, or sent two email like this:

    function alert($host) {
    mail(‘youremail@gmail.com’, ‘Monitoring’, $host.’ down’);
    mail(’555999966@txt.att.net’, ‘Monitoring’, $host.’ down’);
    }

    Note the @txt.att.net, this works for my prepaid phone, mobile.att.net does not work for me.
    Thanks for the script!

Leave a Reply

Your email address will not be published. Required fields are marked *

Please respect the following rules: No advertising, no spam, no keyword in name field. Thank you!