How to easily monitor your web server using PHP
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('[email protected]', '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 [email protected].
Have you checked out the highly recommended Digging into WordPress book by Chris Coyier and Jeff Starr?