Here is a simple PHP script I wrote that times getting a link using mtime with curl and emails if it does not meet my objective. I use it in production for checking the performance of a Content Delivery Network (CDN), but you can use it for any web server.
<?php
$heartbeat = "your.remote.server.com/file.html";
$delay = 2500;
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $heartbeat);
// header
curl_setopt($ch, CURLOPT_HEADER, 1);
// connection timeout
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $delay);
// store results as the return of curl_exec
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// curl timeout
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $delay);
// if HTML error 400 over over, fail
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
// set start time
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$output = curl_exec($ch);
//set end time
$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
curl_close($ch);
$pattern = '/Your_Pattern/';
$errno = preg_match($pattern, $output, $matches, PREG_OFFSET_CAPTURE);
if($errno == 0){
$to = "your.email@yourdomain.com";
$subject = "Problem with Server";
$body = "There is a problem with the Server\n\n";
$body .= "This script executed in " .$totaltime. " seconds.";
$header = "From: root@yourdomain.com\r\n";
$header .= "Reply-To: root@yourdomain.com\r\n";
$header .= "Return-Path: root@yourdomain.com\r\n";
$success = mail($to, $subject, $body, $header);
}
?>