Last active
December 17, 2015 04:49
-
-
Save pkdavies/5553577 to your computer and use it in GitHub Desktop.
This is a simple check script we use on a number of servers to make sure PHP -> Apache -> Server is running ok.
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 | |
// start the output from scratch | |
ob_start(); | |
// some older PHP configs need this setting | |
date_default_timezone_set('Europe/London'); | |
// collect some other useful information | |
$json_array = array( | |
"status" => null, | |
"dt" => date("Y-m-d H:i:s"), | |
"load" => null, | |
"remote" => $_SERVER['REMOTE_ADDR'] | |
); | |
// check that the headers have not already been sent | |
if(!headers_sent()){ | |
// HTTP/1.1 | |
header( 'Cache-Control: no-store, no-cache, must-revalidate' ); | |
header( 'Cache-Control: post-check=0, pre-check=0', false ); | |
header( 'Pragma: no-cache' ); | |
// date in the past | |
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); | |
// load is useful | |
$load = sys_getloadavg(); | |
// check if you get a result | |
// windows servers will fail here | |
if (is_array($load)){ | |
if ($load[0] > 80) { | |
$status = "busy"; | |
} else { | |
$status = "ok"; | |
} | |
} else { | |
$status = "error"; | |
} | |
// collect some other useful information | |
$json_array["status"] = $status; | |
$json_array["load"] = $load; | |
// output any info, we'll only check for OK | |
echo json_encode($json_array); | |
// all ok | |
exit(0); | |
} | |
// if we are here there is something very wrong | |
$json_array["status"] = "emergency"; | |
echo json_encode($json_array); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment