Website Watchdog

cybrax

Community Advocate
Community Support
Messages
764
Reaction score
27
Points
0
Here's a trick that every lone webmaster should find useful. A watchdog script to tell you when your web site becomes unavailable to vistors.

Most web hosts are pretty good with well over 90% server 'uptimes' are to be found across the Internet, however their responsibilty stops where your code begins (unless your being naughty) and some problems are beyond their control.

So as your site evolves and becomes more complex it becomes useful to automate the process of making routine performance and functionality checks. Thus freeing you up to get on with the more important tasks like writing new code or content and generally promoting your site.


Basic Watchdog
Suppose you have your web site called something.com hosted at 'A Hosting',
with the script below forming part of a simple static site with 'B Hosting ' and called using a cron job at daily intervals, or hourly if the host will permit.

So when the main site something.com over at 'A Hosting' goes offline then the watchdog site on 'B Hosting' sends an email alert to an external address and raises the alarm. Remember if your web site is offline, visitors cannot tell you and nor will your web host in many cases.

Code:
$ch = curl_init("http://www.something.com/favicon.ico");

curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

if ($retcode > 400 ){

// email message
$message = "Hi webmaster, your site is offline";


// Send message
mail('webmaster_of_something@gmail.com', 'WEBSITE offline', $message);

}

How it works:
The curl command is used to see if a particular file exits or not by first requesting the file then examaning the Info Http Code . Only the header information is moved across the web not the contents of the file being checked, so it's resource friendly. In this example the global favicon file is being used but could just as easily be a specific HTML or PHP file within the site.

Depending upon the value of the returned value an 'IF' statement (200=file found and 404= file not found) then calls the default mail routine for the 'B Hosting' server and sends an email to an independant address.

NB: the watchdog script has to be run from another server or it will never work!

Getting More Advanced
There are other ways of performing this same trick with regular PHP commands but cURL makes it fairly simple to get more creative.

Curl being capable of many functions including the ability to open FTP connections and populating input fields automatically therefore making it rather useful for checking if contact forms and database driven pages are functioning correctly and other things for instance such as logging page loading times to a file.

That last one incidently is not gripe but a useful feeback for any web page designer :)
so if anybody who reads this would like to add a little more code to this post go right ahead.


[inspired by Piotr GRD]
 
Top