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.

  • http://96th.co.uk/ 96th

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

    • http://www.wprecipes.com Jean-Baptiste Jung

      What webhosting is it? That kind of info is always useful!

  • http://www.270labs.com/ Trav

    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

  • http://www.270labs.com/ Trav

    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.

  • http://www.codesquid.co.uk Codesquid

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

  • Pingback: How to easily monitor your webserver using PHP « R@j@. R.K

  • Ben

    @Trav

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

  • Ben

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

  • That Guy

    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.

  • http://www.digitalmediaminute.com Tom

    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.

  • http://www.wprecipes.com Jean-Baptiste Jung

    @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.

  • chrisweb

    very nice!

  • http://anime2.kokidokom.net Eugen R.

    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.

    • http://www.wprecipes.com Jean-Baptiste Jung

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

  • http://www.labelzone.co.uk Brady

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

  • http://mvphotomag.com Victor

    Very helpful. Thanks!

  • http://www.techiq.in rakesh solanki

    very helpful article, thanks for sharing.

  • http://www.motorward.com Motorward

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

  • Pingback: February 12, 2010 – Links | MarcJStuff

  • Pingback: Il punto della settimana #17 « Francesco Corsentino .net

  • Joe King

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

    • http://www.wprecipes.com Jean-Baptiste Jung

      Didn’t Hostgator allowed cron jobs, thanks for letting me know!

  • http://www.270labs.com/ trav

    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.

    • eric

      If your hoster is like mine you probably don’t have access to the cURL binary on the os anyway.

  • Pingback: Monitorer un serveur Web à distance à l’aide d’un script PHP » Blogue iTx | iTx Technologies: sites Web, Joomla, Wordpress, boutique en ligne, open source Montréal

  • http://www.evohosting.co.uk/ Tim

    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.

  • http://www.nadjifirmu.com Miroslav

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

  • Pingback: Блог-шоу - выпуск 36

  • http://blog.alert-info.net Zero

    It is strange but my local PHP engine goes down trying to execute this code

  • Pingback: uberVU - social comments

  • http://losefatpro.info Johnson Jia

    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.

  • http://www.lidakapsulu.org lida

    Very helpful. Thanks!

  • http://appcrunch.co.uk Josh

    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.

  • Pingback: [Web] 連結分享 | Geek is a Lift-Style.

  • http://lazaac.com/blog lazaac

    hi! thank for the info.. btw, can i use it on the cloud based server..?

  • http://www.righttrak.net Dave

    GoDaddy offers a Cron Manager with their hosting plans that allows you to set this up in seconds.

  • http://www.youtube.com/watch?v=568EIofG24k Jamie

    For SMS alerts, does it really have to be a Gmail account?

    • CynderR

      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

  • http://www.justcarl.me Carl

    Hi, very usefull :)
    Anyone knows one to keep a log of modified files within the server?

    Thanks

  • http://www.savitra.org Alex Savitra

    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.

    • http://www.micromall.ro Mihai

      Can you share the exact source code for doing that ? Thanks in advance.

  • LowJoe

    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 .= “”;

  • LowJoe

    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

  • http://glebpiatikow.su Gleb Piatikow

    Very nice script, thanks!

    I was just wondering, how to check HTTPS? Some of my sites run on ssl by default.

  • http://www.micromall.ro Mihai

    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.

  • CynderR

    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!!!’;

  • Jeroen

    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!