Monitoring the availability of your site using PHP script

You might have subscribed for costly monitoring services, however, the following script will help you to keep a track of your site’s availability.

Create file /usr/sbin/monitor.php with the following contents.

#!/usr/bin/php -q
<?php
define ( “ TIMEOUT “ , 30 ) ;
define ( “ EMAIL “ , ‘ yourname@yourdomain.com ‘ ) ;
check ( “ http://domain1.com “ ) ;
check ( “ http://domain2.com “ ) ;
check ( “ http://domain3.com “ ) ;
check ( “ http://domain4.com “ ) ;
function check ( $url ){
$ch = curl_init () ;
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 ) ; // Return Page contents.
curl_setopt ( $ch , CURLOPT_URL , $url ) ;
curl_setopt ( $ch , CURLOPT_TIMEOUT , 30 ) ;
curl_setopt ( $ch , CURLOPT_DNS_CACHE_TIMEOUT , TIMEOUT ) ;
curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT , TIMEOUT ) ;
curl_setopt ( $ch , CURLOPT_HEADER , TIMEOUT ) ;
$result = curl_exec ( $ch ) ;
curl_close ( $ch ) ;
/ HTTP/1.1 200 OK”)
if ( strpos ( $result , “ 200 OK “ ) != 8 ){
mail ( EMAIL , “ Error in $url “ , $results ) ;
}
}
?>

Add this line in your Linux /etc/crontab or via cpanel to monitor eg: every 3 minutes

*/3 * * * * root /usr/sbin/monitor.php >> /dev/null 2>&1

Temporary connection issues or high load in the server may also trigger an alert.