Skip to content

Instantly share code, notes, and snippets.

@Depicus
Created December 26, 2014 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Depicus/e4e2a7464f9bd638e0ca to your computer and use it in GitHub Desktop.
Save Depicus/e4e2a7464f9bd638e0ca to your computer and use it in GitHub Desktop.
Quick (unfinished) script that I run on my PI at home to check sites are up.
<?php
date_default_timezone_set('Europe/London');
function sendmail($site,$contacts,$name,$date)
{
$to = $contacts;
$subject = 'Alert ' . $site . ' appears to be down - ' . $name;
$message = $site . ' is down, please check now ' .$date;
$headers = 'From: donotreply@depicus.com (Depicus Site Monitor)' . "\r\n" .
'Reply-To: donotreply@depicus.com (Do Not Reply)' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
function curlsite($site,$contacts,$name,$date)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://" . $site . "/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //follow up to 2 redirections - avoids loops
$data = curl_exec($ch);
curl_close($ch);
if (!$data) {
echo "Domain could not be found " . $site . "<br />";
sendmail($site, $contacts,'Domain not found',$date);
return;
}
else {
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if ($code == 200) {
//echo "Page Found " . $site . "<br />";
//sendmail($site, $contacts);
}
elseif ($code == 404) {
echo "Page Not Found " . $site . "<br />";
sendmail($site, $contacts,$name,$date);
return;
}
}
}
$sitestocheck = array( array( "site" => "www.depicus.com",
"contact" => "me@example.com,you@example.com",
"name" => "My Apple"
),
array( "site" => "blog.depicus.com",
"contact" => "me@gmail.com",
"name" => "View All Oranges",
),
array( "site" => "t.depicus.com",
"contact" => "you@hotmail.com",
"name" => "View All Oranges",
)
);
foreach ($sitestocheck as $i => $row)
{
$date = date('m/d/Y h:i:s a', time());
curlsite($row['site'],$row['contact'],$row['name'],$date);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment