Skip to content

Instantly share code, notes, and snippets.

@jrsouth
Created October 24, 2013 12:55
Show Gist options
  • Save jrsouth/7136741 to your computer and use it in GitHub Desktop.
Save jrsouth/7136741 to your computer and use it in GitHub Desktop.
Monitor Scripts
<?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(
"james.south@gmail.com",
"jsouth@beatingbloodcancers.org.uk",
"kpryor@beatingbloodcancers.org.uk",
"obowden@beatingbloodcancers.org.uk",
"psaleh@beatingbloodcancers.org.uk",
"tmuirhead@beatingbloodcancers.org.uk",
"cbindika@beatingbloodcancers.org.uk",
);
// --------------------------------------------------------------------- //
// 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);
}
}
<?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('james.south@gmail.com', 'Nexus 5 Appears Avaiable', "The string 'Nexus 5' was found on Google Play UK's 'Devices' page $matches times.\n\n[AUTOMATED EMAIL]");
mail('missbeks@gmail.com', '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