here is the uptime script i wrote for work:
i have removed our list of sites, and personal details, you will want to modify it i'm sure, but its a start.
it is of course running on a septate server than the ones monitored (my personal web hosting account actully). Includes email and txt notifications. Neither clean nor optimised, but it works
<?php
set_time_limit(600);
// human check http://www.**.com/**/uptime.php
function db_connect(){
$con = mysql_connect('localhost','**','**') or die(mysql_error());
mysql_query('SET SESSION wait_timeout = 600',$con); //yup need it
return $con;
}
$con = db_connect();
$sites[] = array(
'site'=>'**.com','port'=>'80');
$sites[] = array(
'site'=>'**.com','port'=>'80');
$sites[] = array(
'site'=>'**.com','port'=>'443');
$sites[] = array(
'site'=>'**.com','port'=>'443');
$sites[] = array(
'site'=>'**.com','port'=>'80');
$sites[] = array(
'site'=>'**.com','port'=>'443');
function check($site,$port){
$up = @fsockopen($site,$port,$errno,$errstr,30);
if($up || $errno==0){ //0 = temp error or local(no remote) erver at fault
return;
}else{
$out = array(
$site,$errno,$errstr);
return $out;
}
}
function send_mail($out){
$body = "Automated script found the following site(s) were not responding:\n\n";
$body_txt = "Site Fail:\n";
foreach($out as $o){
$body .= 'Site: '.$o[0]."\nError Number: ".$o[1]."\nError Message: ".$o[2]."\n\n";
$body_txt .= $o[0]."\n".$o[1].' '.$o[2]."\n\n";
}
$headers .= 'From: Script <**>'."\r\n";
mail("**",'Broken ** Site',$body,$headers);
log_fail($body_txt);
}
function log_fail($body_txt){
$con = db_connect(); //yes need it, times out otherwise due to 60sec sleep
$q = "SELECT fail_date from **.log WHERE fail_date + INTERVAL 1 hour < NOW() AND status='1' AND id='1' ";
//echo $q;
$result = mysql_query($q,$con) or die($q.mysql_error());
$num_rows = mysql_num_rows($result);
if($num_rows=='1'){ //send text
$q = "UPDATE **.log SET fail_date=NOW()";
$result = mysql_query($q) or die($q.mysql_error());
send_txt($body_txt);
}
}
function send_txt($body){
echo "<br><br>*************Trying to send txt**************************<br><br>";
$phone = array(
'##','##','##','##');
$username = '**';
$password = '**';
$source = '##';
$text = $body;
foreach($phone as $ph){
echo '<br><br>txt to: '.$ph.'<br>';
$destination = $ph;
$content = 'action=sendsms'.'&user='.rawurlencode($username).'&password='.rawurlencode($password).'&to='.rawurlencode($destination).'&from='.rawurlencode($source).'&text='.rawurlencode($text);
$smsglobal_response = file_get_contents('http://www.smsglobal.com.au/http-api.php?'.$content);
$explode_response = explode('SMSGlobalMsgID:',$smsglobal_response);
if(count($explode_response)==2){ //Message Success
$smsglobal_message_id = $explode_response[1];
//SMSGlobal Message ID
echo 'SMSGlobal Message ID: '.$smsglobal_message_id;
}else{ //Message Failed
echo 'Message Failed'.'<br />';
//SMSGlobal Response
echo $smsglobal_response;
}
}
}
function main_run($sites,$check){
flush();
echo '<hr>Round '.$check.'<br>Sever time: '.date("d/n/Y, G:i:s").'<br><br>';
foreach($sites as $s){
echo 'Checking: '.$s['site'].'<br>Status: ';
$c = check($s['site'],$s['port']);
if(is_array($c)){
$out[] = $c;
echo 'Broken --'.$c[1].' '.$c[2].'<br><br>';
}else{
echo 'Working<br><br>';
}
flush();
}
if(empty($out[0][0])&&$check=='1'){
echo '***************ALL Working************************<br><br>';
}elseif(!empty($out[0][0])&&$check=='1'){
echo '***************Something Broke, checking again************************<br><br>';
}elseif(!empty($out[0][0])&&$check=='2'){
send_mail($out);
echo '<br>Sending email<br>';
}
return $out;
}
if($_GET['action']=='stop'){
$q = "UPDATE **.log SET status='0', fail_date=NOW() where id='1' ";
$result = mysql_query($q) or die(mysql_error());
echo 'TXT sending is disabled for 24 hours';
exit();
}
if($_GET['action']=='start'){
$q = "UPDATE **.log SET status='1' where id ='1' ";
$result = mysql_query($q) or die(mysql_error());
echo 'TXT sending is re-enabled';
exit();
}
//every run check
//reeanble sending
$q = "UPDATE **.log SET status='1', fail_date=NOW() WHERE id='1' AND status='0' AND fail_date+ INTERVAL 24 hour < NOW() ";
$result = mysql_query($q) or die($q.mysql_error());
$check = '1';
$r = '';
//////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
if($check==1){
$r = main_run($sites,$check);
$check = '2';
}
if(!empty($r[0][0])){
sleep(90);
$r = main_run($sites,$check);
}
?>