Created
October 24, 2013 12:55
-
-
Save jrsouth/7136741 to your computer and use it in GitHub Desktop.
Monitor Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$search = 'Patient information'; // String to search for in the raw HTML | |
$url = 'http://leukaemialymphomaresearch.org.uk'; | |
$repeat_delay = 1; // Time in hours between repeat notifications (you will get notifications more often if the site repeatedly goes up and down) | |
$peak_start = 6; // Time to start peak monitoring | |
$peak_end = 11; // Time to start off-peak monitoring | |
$timeout_peak = 10; //timeout in seconds during peak times | |
$timeout_offpeak = 60; //timeout in seconds during off-peak times (to allow for slowdown due to backups etc.) | |
$email_recipients = Array( | |
"[email protected]", | |
"[email protected]", | |
"[email protected]", | |
"[email protected]", | |
"[email protected]", | |
"[email protected]", | |
"[email protected]", | |
); | |
// --------------------------------------------------------------------- // | |
// Do not change below this line // | |
// --------------------------------------------------------------------- // | |
$hour = date('G'); | |
$timeout = (($hour >= $peak_start && $hour < $peak_end) || $peak_start >= $peak_end)?$timeout_peak:$timeout_offpeak; | |
$email_subject = 'Website Monitoring: ' . $url; | |
$content = Array(); | |
$command = 'curl --max-time ' . $timeout . ' -s ' . $url . ' | grep -o -i "' . $search . '"'; | |
exec($command, $content); | |
$matches = count($content); | |
$lockfile = '/tmp/' . preg_replace('/\//','-',$url) . '.lock'; | |
if ($matches == 0) { | |
if (!file_exists($lockfile) || time() - filectime($lockfile) > $repeat_delay * 3600 ) { | |
touch($lockfile); | |
status_alert("SITE DOWN\n\n".$url." appears to be down. (No response after ".$timeout."sec)\nPlease check ASAP.\n\n[AUTOMATED EMAIL]"); | |
} | |
} else { | |
if (file_exists($lockfile)) { | |
unlink($lockfile); | |
status_alert("SITE UP\n\n".$url." appears to be back up\n\n[AUTOMATED EMAIL]"); | |
} | |
} | |
function status_alert ($message) { | |
global $email_recipients, $email_subject; | |
foreach($email_recipients as $email_recipient) { | |
mail($email_recipient, $email_subject, $message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Monitors a URL for the appearance of a given string. | |
// Best to run as a cron job: | |
// */5 * * * * /usr/bin/php -q /path/to/script/website_monitor.php | |
$search = 'nexus 5'; | |
// String to search page for (case-insensitive) | |
$url = 'https://play.google.com/store/devices?hl=en_GB'; | |
// Page to search -- searches raw source code (via curl piped through grep) | |
$repeat_delay = 24; | |
// Time in hours between repeat notifications | |
// --------------------------------------- | |
$content = Array(); | |
$command = 'curl -s ' . $url . ' | grep -o -i "' . $search . '"'; | |
exec($command, $content); | |
$matches = count($content); | |
if ($matches > 0) { | |
$lockfile = '/tmp/' . preg_replace('/\//','-',$url) . '.lock'; | |
if (!file_exists($lockfile) || time() - filectime($lockfile) > $repeat_delay * 3600 ) { | |
touch($lockfile); | |
mail('[email protected]', 'Nexus 5 Appears Avaiable', "The string 'Nexus 5' was found on Google Play UK's 'Devices' page $matches times.\n\n[AUTOMATED EMAIL]"); | |
mail('[email protected]', 'Nexus 5 Appears Avaiable', "The string 'Nexus 5' was found on Google Play UK's 'Devices' page $matches times.\n\n[AUTOMATED EMAIL]"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment