Friday, October 28, 2016

CPU Analysis PHP

<?php 
header("Content-Type:text/plain") ;

$free = shell_exec('free');
$free = (string)trim($free);
$free_arr = explode("\n", $free);
$mem = explode(" ", $free_arr[1]);
$mem = array_filter($mem);
$mem = array_merge($mem);
$memory_usage = $mem[2]/$mem[1]*100;


print "Report for host: ".gethostname()." ".$_SERVER['SERVER_ADDR']."   timestamp: ".date("Y:m:d:h:i:s a")." year/month/date/hour/minute/seconds ".PHP_EOL.PHP_EOL;
$loadArray = sys_getloadavg();
$cpu = $loadArray[2];
$memoryMsg = "Memory ".$memory_usage." percent in use.".PHP_EOL;
print PHP_EOL."++++++++++++ operating system profile +++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
print         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
 echo 'Operating System: '.php_uname('s').PHP_EOL;
 echo 'Release Name: '.php_uname('r').PHP_EOL;
 echo 'Version: '.php_uname('v').PHP_EOL;
 echo 'Machine Type: '.php_uname('m').PHP_EOL;
print         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;


if(file_exists('/proc/cpuinfo')) {
 $retval=file_get_contents('/proc/cpuinfo');
 $mycount=explode(PHP_EOL, $retval);
 $cores = round( (count($mycount)/26), 0, PHP_ROUND_HALF_DOWN);
 $load = $cpu/$cores;
 $load = $load*100;


 print PHP_EOL."++++++++++++ hardware profile +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
 print         "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL;
 print "CPU has ".$cores." cores".PHP_EOL;
 print PHP_EOL.'Memory:'.PHP_EOL.$free.PHP_EOL;
 print PHP_EOL."+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++".PHP_EOL.PHP_EOL;

 print "CPU load ".$load." percent averaged over last 15 minutes. [ sys_getloadavg() is ".$loadArray[0]." ".$loadArray[1]." ".$loadArray[2]." ]".PHP_EOL;
 print $memoryMsg;
 print PHP_EOL."Detailed CPU Information ( /proc/cpuinfo ) below.".PHP_EOL;
 print $retval.PHP_EOL;
} else {
 print "CPU load ".$cpu.PHP_EOL;
 print $memoryMsg;
}

print PHP_EOL."Uptime information  below.".PHP_EOL;
exec('uptime', $retval); 
foreach($retval as $ret) {
 print $ret.PHP_EOL;
}  


?>

Thursday, October 6, 2016

Unix/Linux how to run a command in background

nohup myFooCommand > /dev/null 2>&1 &