PHP Web server monitor
With this simple PHP script, you'll be able to monitor the availability of Web servers. Write a list of URLs to monitor and run the script from cron. If a site becomes unavailable, monitor PHP script will generate alert and send e-mail to the default e-mail address. Monitor script is also able to send e-mail message to the various e-mail address (e-mail address per URL).
<?
// check list of URLs (select URLs with small traffic - static content)
url_test('http://www.redips.net/my/img/b.gif');
url_test('http://raspored.hrt.hr/b.gif');
/**
* function checks Web server and sends an e-mail if there is a problem
* email_admin has a default value and function can be called with or without e-mail address
* script implies that the Web server does not work if there is no response within 10 seconds
*
* @param string $url URL that must be checked
* @param string $email_admin if there is a problem send e-mail notification to this address
*/
function url_test($url, $email_admin='your@email.address'){
// set timeout
$timeout = 10;
// init url session and set curl options
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
$http_respond = curl_exec($ch); // execute http request
// remove all html tags from the http response
$http_respond = trim(strip_tags($http_respond));
// get status from the http response
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// returned http code should be 200 otherwise there is a problem
if ($http_code != 200){
// prepare tested server name (component parameter added in PHP 5.1.2)
$http_server = parse_url($url, PHP_URL_HOST);
// and who was tester
$hostname = trim(`/bin/hostname`);
// get an error message from the URL request
$http_error = trim(curl_error($ch));
// prepare e-mail message
$email_message = "TEST URL: $url";
if ($http_code) $email_message .= "\n\nHTTP CODE: $http_code";
if ($http_respond) $email_message .= "\n\nHTTP RESPOND:\n$http_respond";
if ($http_error) $email_message .= "\n\nHTTP ERROR:\n$http_error";
// prepare e-mail header
$email_header = "Content-Type: text/plain; charset=\"iso-8859-2\"\n".
"Subject: http://$http_server problem!\n".
"From: monitor@$hostname\n".
"X-Priority: 1\n".
"Priority: Urgent\n".
"Importance: high";
// send e-mail message
error_log($email_message, 1, $email_admin, $email_header);
}
// close url session
curl_close($ch);
}
?>
I run monitor script every five minutes and here is my cron entry:
# PHP Web server monitor */5 * * * * /usr/bin/php /usr/local/scripts/monitor.php
Script was and is very useful for me, and I hope it will be for you.
Follow me on Twitter
Thanks, just what I needed!
I did not want something complicated and this does exactly what it has to do. A nice addition of the script might be that repeat messages are suppressed. But then again, that’s only needed if you’re website is down for a long time
I have this monitor script in production and if a Web site is down for an hour, then I will receive about 12 mail notifications (cron runs it every 5 minutes). Yes, it's annoying but it does the job. On the other hand if you want to suppress repeated messages, then PHP code will grow and this will no longer be a simple PHP monitor ...
Anyway, thank you for the comment.
Thanks for the script. Any idea how one could monitor other ports other than 80? I have been having trouble with dns on one machine. I have filed a support ticket but in the meantime I would like to get it fixed. Monitoring of email smtp service and dns would be great. It would somehow just have to open a connection rather than follow all the way through.
Thank you
KM
Hmm, you will have to start simple communication with the monitored service - something like "hello" for the smtp. If you have installed PERL, then the mon (Service Monitoring Daemon) will be excellent choice. I used it for LDAP and it works just fine. Please see the huge list of services that can be monitored with mon package.