Creating a Site Poller

Published on 24th Mar, 2014

We all want our sites and applications to be up all the time. It's the truth. However, life is hard and sites go down. It's not about if the site will go down it's about when and what procedures we have in place in that eventuality.

I recently had a finicky site that just kept crashing. I wasn't sure why but it happened quite frequently. I found myself, throughout the course of the day, just hitting the page to see if it was up. When it went down I'd usually get an email from the site's owner, then I'd hop into the server logs to see what happened.

This is when I thought "There's got to be a better way to do this. I should be able to know the minute a site goes down." Enter site-polling. There are quite a few paid services out there that will monitor your applications for you and let you know when something's wrong. Not to discount any of them but what they do, on the whole, is quite simple:

  • Send a request to your site at regular intervals and log the site's response code (200, 301), response time, etc.
  • Based on the response, you are sent a notification.

Well that doesn't sound too hard. In fact, it's quite simple. Let's look at a way to poll a site using PHP's cURL library. As with most things related to coding, there's a great code sample on stackoverflow.

$url = 'http://site-in-question.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (200==$retcode) {
    // Everything Worked!
    exit();
} else {
     // Not Good.
}

That is the bare-minimum you need. That will tell you if your site returns a 200 status code. Now of course certain sites and links might return codes for redirects or a code other than 200 that is still acceptable, so you should make sure you know what codes indicate working and which ones mean trouble. But it's a pretty safe bet to say that you're looking for a 200 status code, this code means "OK".

Let's take it a step further so that we can have our very own uptime-monitor for free!

Just upload this bit of code to a server, set it up to send you a message with the PHPMailer class or another notification API of your choice:

require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();                                     // Set mailer to use SMTP
$mail->Host = 'mail.yourhost.com';                   // Specify main and backup server
$mail->SMTPAuth = true;                              // Enable SMTP authentication
$mail->Username = 'from@yourhost.com';               // SMTP username
$mail->Password = 'password';                        // SMTP password
$mail->Port = 26;                                    // Set the SMTP port number - 587 for authenticated TLS
$mail->setFrom('from@yourhost.com', 'Site Poller');  // Set who the message is to be sent from
$mail->addAddress('me@gmail.com', 'Richard Cagle');  // Add a recipient
$mail->WordWrap = 50;                                // Set word wrap to 50 characters
$mail->isHTML(true);                                 // Set email format to HTML
     
$mail->Subject = 'Site Status Error for My Site';
$mail->Body    = 'At ' . date("Y-m-d h:i:s") . " My site returned a " . $retcode . ". 
Attention may be needed"; if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; exit(); }

Then fire up a CRON job to run this script every so often and voila! You're monitoring your own applications. No fuss. Of course, if you don't have access to a webserver, it's really easy to set one up on your local machine using XAMPP or MAMP. These even have local mail clients you can configure, so as long as your own computer is up and running, you'll have a nice, reliable monitoring system.

Of course this brings up the point that the downtime monitoring application is only as reliable as the uptime of the server it's running on. A service that charges for website monitoring is likely to have the advantage of server redundancy and a high rate of reliability. They can also provide historical data for the status of your site over time, but if you're willing to fire up a MySQL database, you can easily set up this type of logging for yourself as well. For my use cases, the reliability of my home computer is more than sufficient, I encourage you to give it a try, you'll find it handy.

Keep Coding.

Richard Cagle

This article is my 24th oldest. It is 722 words long

comments powered by Disqus