Created
December 26, 2014 14:27
-
-
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.
This file contains hidden or 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 | |
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: [email protected] (Depicus Site Monitor)' . "\r\n" . | |
'Reply-To: [email protected] (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" => "[email protected],[email protected]", | |
"name" => "My Apple" | |
), | |
array( "site" => "blog.depicus.com", | |
"contact" => "[email protected]", | |
"name" => "View All Oranges", | |
), | |
array( "site" => "t.depicus.com", | |
"contact" => "[email protected]", | |
"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