Last active
April 6, 2017 13:51
-
-
Save kidino/bc801c80d69c1cccfd28c61300a2d265 to your computer and use it in GitHub Desktop.
PHP Simple Uptime Monitoring. Can be used in cron or command line.
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 | |
error_reporting(E_STRICT | E_ALL); | |
$url_to_check = 'http://domain.zyz'; | |
$downtime_message = 'WEBSITE IS DOWN : '. $url_to_check; | |
$uptime_message = 'WEBSITE IS BACK ONLINE : '.$url_to_check; | |
$notify_email = 'me@domain.xyz'; | |
$from_ok = 'UPTIME OK'; | |
$from_error = 'UPTIME ERROR'; | |
$from_email = 'uptime-monitor@domain.xyz'; | |
$check_text = '<!-- secret text in website -->'; | |
$error_file = 'upmon-error.txt'; | |
$ok_file = 'upmon-ok.txt'; | |
date_default_timezone_set('Etc/UTC'); | |
// requires PHPMailer | |
require 'PHPMailerAutoload.php'; | |
$mail = new PHPMailer; | |
$mail->isSMTP(); | |
$mail->Host = 'email-smtp.us-east-1.amazonaws.com'; | |
$mail->SMTPAuth = true; | |
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead | |
$mail->Port = 587; | |
$mail->Username = 'AKIAIAR6CCUUABCDEFIQ'; | |
$mail->Password = 'Ana0bkUG/gYCaVTi+PHs8jUS8VsE89V0Fgqhj123456'; | |
$mail->addAddress($notify_email); | |
$webhtml = file_get_contents($url_to_check); | |
if ( strpos($webhtml, $check_text) === false) { | |
@unlink($ok_file); | |
$error_time = @file_get_contents($error_file); | |
$nowtime = time(); | |
if($error_time === false) { | |
file_put_contents($error_file, $nowtime); | |
$error_duration = 0; | |
} else { | |
$error_duration = $nowtime - $error_time; | |
} | |
// if you use this in cron, you will continue to get email | |
// while the website is still down. | |
$mail->setFrom($from_email, $from_error); | |
$mail->Subject = $downtime_message . " Duration : ".sectime($error_duration); | |
$body = $url_to_check . " has been down for ".sectime($error_duration); | |
$mail->msgHTML($body); | |
if (!$mail->send()) { | |
echo "DOWN Mailer Error\n"; | |
} | |
echo $url_to_check . " has been down for ".sectime($error_duration);."\n"; | |
} else { | |
@unlink($error_file); | |
$ok_time = @file_get_contents($ok_file); | |
$nowtime = time(); | |
if($ok_time === false) { | |
file_put_contents($ok_file, $nowtime); | |
$ok_duration = 0; | |
// if the website was down, you will get an email when it is back online | |
$mail->setFrom($from_email, $from_ok); | |
$mail->Subject = $uptime_message; | |
$body = $uptime_message; | |
if (!$mail->send()) { | |
echo "UP Mailer Error\n"; | |
} | |
} else { | |
$ok_duration = $nowtime - $ok_time; | |
} | |
echo $url_to_check ." OK Duration : ".sectime($ok_duration)."\n"; | |
} | |
function sectime($seconds) { | |
$t = round($seconds); | |
return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment