Skip to content

Instantly share code, notes, and snippets.

@shoaibi
Created June 25, 2015 15:55
Show Gist options
  • Select an option

  • Save shoaibi/8ff79cce2f57d5a7fad8 to your computer and use it in GitHub Desktop.

Select an option

Save shoaibi/8ff79cce2f57d5a7fad8 to your computer and use it in GitHub Desktop.
A simple url monitor that monitor the status of the url and search the page for provided string, sends email notifications if the check fails
#!/usr/bin/php
<?php
// Use https://uptimerobot.com/ instead
define('CONTACT_TO', 'me@my.com');
define('CONTACT_FROM', 'report@my.com');
$message = '';
$subject = '';
if (count($argv) > 3)
die("\n\t\t Script supports oly two parameters. Use like " . $argv[0] . " TEXT_PATTERN www.host.com\n\n\n");
$host = $argv[2];
$find = $argv[1];
if (!check())
alert();
function alert() {
global $host, $message, $subject;
$to = CONTACT_TO;
$headers = 'From: ' . CONTACT_FROM . "\r\n" .
'X-Mailer: PHP/' . phpversion();
/*
echo "\nTo: ". CONTACT_TO;
echo "\nFrom: ". CONTACT_FROM;
echo "\nSubject: ". $subject;
echo "\nMessage: \n". $message;
echo "\n\n\n";
*/
mail($to, $subject, $message, $headers);
}
function check() {
global $host, $find, $message, $subject;
$sh = curl_init($host);
curl_setopt($sh, CURLOPT_HEADER, 0);
curl_setopt($sh, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($sh);
$aCURLinfo = curl_getInfo($sh);
curl_close($sh);
$subject = "Status code not 200";
$message = "\n\nURL:";
$message .= "\n------------\n";
$message .= " $host";
$message .= "\n\n\nSearch:";
$message .= "\n------------\n";
$message .= " $find";
$message .= "\n\n\nHeader Details:";
$message .= "\n------------------------\n\n";
$message .= print_r($aCURLinfo, true);
if (!isset($aCURLinfo['http_code']) || ($aCURLinfo['http_code'] != 200))
return false;
else if (strstr($content, $find) === false) {
$subject = "Page Mismatch";
$message .= "\n\n\n";
$message .= "\nURL:";
$message .= "\n------------\n";
$message .= " $host";
$message .= "\n\n\nPage Content:";
$message .= "\n------------------\n\n";
$message .= print_r($content, true);
$message .= "\n\n";
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment