Created
March 24, 2015 09:22
-
-
Save AbhishekGhosh/377d39106d4f05b8ee40 to your computer and use it in GitHub Desktop.
nginx status php
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 | |
$serverUptime = getServerUptime(); | |
$nginxUptime = getNGINXUptime(); | |
$nginxStatus = getNGINXStatus($nginxUptime['totalSeconds']); | |
echo json_encode( | |
array( | |
"server_uptime"=>$serverUptime, | |
"nginx_uptime"=>$nginxUptime, | |
"nginx_status"=>$nginxStatus | |
) | |
); | |
function getServerUptime() | |
{ | |
exec("cat /proc/uptime",$systemResult,$retval); | |
$array = explode(" ",$systemResult[0]); | |
$uptime = $array[0]; | |
$idle = $array[1]; | |
$seconds = $uptime; | |
$days = floor($seconds/86400); | |
$hours = floor(($seconds-($days*86400)) / 3600); | |
$minutes = floor(($seconds - ($hours*3600) - ($days*86400)) / 60); | |
$seconds = $seconds - ($minutes*60) - ($hours*3600) - ($days*86400); | |
return array("days"=>$days, | |
"hours"=>$hours, | |
"minutes"=>$minutes, | |
"seconds"=>$seconds, | |
"totalSeconds"=>$uptime, | |
"idle"=>$idle); | |
} | |
function getNGINXUptime() | |
{ | |
exec("ps -ef | grep nginx",$output,$retval); | |
$pd=0; | |
foreach($output as $row) | |
{ | |
$exploded = explode(" ",$row); | |
if($exploded[0] == "root") | |
{ | |
$count = count($exploded); | |
for($i=1;$i<$count;$i++) | |
{ | |
if(strlen($exploded[$i])>1) | |
{ | |
$pd = $exploded[$i]; | |
break; | |
} | |
} | |
if($pd!=0) | |
break; | |
} | |
} | |
$cmd = "expr $(awk '{print $1}' FS=\. /proc/uptime) - $(awk '{printf (\"%10d\",$22/100)}' /proc/$pd/stat)"; | |
exec($cmd,$out,$retval); | |
$tatalSeconds = $out[0]; | |
$days = floor($tatalSeconds/86400); | |
$hours = floor(($tatalSeconds-($days*86400)) / 3600); | |
$minutes = floor(($tatalSeconds - ($hours*3600) - ($days*86400)) / 60); | |
$seconds = $tatalSeconds - ($minutes*60) - ($hours*3600) - ($days*86400); | |
$nginxUptime = array("days"=>$days, | |
"hours"=>$hours, | |
"minutes"=>$minutes, | |
"seconds"=>$seconds, | |
"totalSeconds"=>$tatalSeconds); | |
return $nginxUptime; | |
} | |
function getNGINXStatus($nginxUptime) | |
{ | |
$nginx_status = file_get_contents("https://thecustomizewindows.com/status"); | |
$tokens = array_map('trim',explode(" ",$nginx_status)); | |
foreach($tokens as $token) | |
{ | |
if(is_numeric($token)) | |
$stats[] = $token; | |
} | |
return array( | |
"activeConnections"=>$stats[0], | |
"accepts"=>$stats[1], | |
"handled"=>$stats[2], | |
"requests"=>$stats[3], | |
"reading"=>$stats[4], | |
"writing"=>$stats[5], | |
"waiting"=>$stats[6], | |
"averageRequestsPerConnection"=>$stats[3] / $stats[2], | |
"averageRequestsPerSecond"=> $stats[3] / $nginxUptime | |
); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment