/dev/null 2>&1 */ $outputFile = __FILE__ . '.json'; $pid = '/tmp/php-livestats.pid'; if(file_exists($pid)){ //process is already running. if(file_exists($outputFile)){ $data = file_get_contents($outputFile); echo $data; }else{ echo json_encode(array('error'=>'process is already running and no cache file exists.')); } die(); }else{ file_put_contents($pid, getmypid()); } $stats = array(); //Get machine name $stats['system'] = trim(`/bin/uname -n`); //timestamp this data. $stats['timestamp'] = time(); $stats['datetime'] = date('r',$stats['timestamp']); //Get CPUS $stats['processors'] = trim(`cat /proc/cpuinfo | grep processor | wc -l`); //Get the load average from the proc file sytem. $load = explode(' ',trim(file_get_contents('/proc/loadavg'))); $stats['loadavg']['last1min'] = $load[0]; $stats['loadavg']['last5min'] = $load[1]; $stats['loadavg']['last10min'] = $load[2]; $stats['loadavg']['processes'] = $load[3]; $stats['loadavg']['lastprocess'] = $load[4]; //Extract information about running apache processes using the cli utility PS on linux. $apacheProcesses = trim(`/bin/ps -u apache -F`); $apacheProcesses = explode("\n",$apacheProcesses); $cols = explode(" ",preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $apacheProcesses[0])); unset($apacheProcesses[0]); foreach($apacheProcesses as &$singleProcess){ $row = explode(" ",preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $singleProcess)); $tmp = array(); foreach($cols as $index => $col){ $tmp[$col] = $row[$index]; } $singleProcess = $tmp; unset($tmp); unset($row); } $stats['web-processes'] = $apacheProcesses; //echo '
'; var_dump($stats); die(); $json = json_encode($stats); file_put_contents($outputFile,$json); unlink($pid); die($json);