Posts Tagged ‘How to easily monitor your webserver using PHP’

In a new post to the CatsWhoCode.com blog Jean-Babtiste Jung walks you through the creation of a simple monitoring script written using just PHP that can tell you if your web site is up and responsive.

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.

They have a snippet of code (about 15 lines long) with a function you can call to check a remote host’s connection and check the returned data for a certain string. This can not only ensure that your site is responsive but also that it’s not responding incorrectly. You could even use this to hit a certain monitoring page of your site to check for certain things (like database connection problems).

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:

01.function check($host, $find) {
02.$fp = fsockopen($host, 80, $errno, $errstr, 10);
03.if (!$fp) {
04.echo "$errstr ($errno)\n";
05.} else {
06.$header = "GET / HTTP/1.1\r\n";
07.$header .= "Host: $host\r\n";
08.$header .= "Connection: close\r\n\r\n";
09.fputs($fp, $header);
10.while (!feof($fp)) {
11.$str .= fgets($fp, 1024);
12.}
13.fclose($fp);
14.return (strpos($str, $find) !== false);
15.}
16.}
17.
18.function alert($host) {
19.mail('youremail@gmail.com', 'Monitoring', $host.' down');
20.}
21.
22.$host = 'www.catswhoblog.com';
23.$find = 'Cats Who Code';
24.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, http://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”:

Setting up SMS alerts

Setting up SMS alerts

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.