I making a webadmin system 开发者_如何学Pythonand I want to monitor the CPU and RAM usage of a process in PHP
Can anyone help me?
"Counters and Logs to Monitor" => http://support.microsoft.com/kb/300504
Save as CSV and read it() by PHP
[OR]
http://pecl.php.net/package/win32ps
[OR]
Use WMI:
<?PHP
#error_reporting(1);
$wmi = new COM("WinMgmts:{impersonationLevel=impersonate}") ;
$cpus = $wmi->ExecQuery("SELECT LoadPercentage FROM Win32_Processor");
foreach ($cpus as $cpu) :
echo $cpu->LoadPercentage . '%';
endforeach;
?>
Use this it works perfectly. Combine this with AJAX and a JAVASCRIPT timer so you get every xy seconds the usage of your cpu&ram.
function get_server_cpu_usage(){
$load = sys_getloadavg();
return $load[0];
}
function get_server_memory_usage(){
$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;
return $memory_usage;
}
echo '<h4>Server Memory usage: ' . number_format(get_server_memory_usage(), 2) . '%</h4><span style="width:' . get_server_memory_usage() . '%"></span<br>
<h4>Server CPU usage: ' . get_server_cpu_usage() . '% </h4><span style="width:' . get_server_cpu_usage() . '%"></span>';
精彩评论