phpsysinfo
One option is to use phpsysinfo
Description
phpsysinfo is a script that shows information about your system.
Source: in free translation - phpsysinfo
In addition to showing CPU usage, it shows up more than you need, such as:
- Memoriam
- Ethernet
- [...] (I left the link of the site for you to take a look)
PS.: It does not use the exec
, he reads all the information directly by /proc
.
Example of what he can do.
sys_getloadavg()
There is also the sys_getloadavg()
, that might be a good move.
Note
This function is not implemented on the Windows platform
Source - in English
function retornar_uso_cpu(){
$uso = sys_getloadavg();
return $uso[0];
}
<p><span class="description">Server CPU Usage: </span> <span class="result"><?= retornar_uso_cpu() ?>%</span></p>
Alternative to Windows
An option that works on Windows would be:
Note
This method is not so fast, so take care in the amount of calls.
If something goes wrong (e.g., not having enough access permissions), it returns 0.
<?php
function get_server_load() {
if (stristr(PHP_OS, 'win')) {
$wmi = new COM("Winmgmts://");
$server = $wmi->execquery("SELECT LoadPercentage FROM Win32_Processor");
$cpu_num = 0;
$load_total = 0;
foreach($server as $cpu){
$cpu_num++;
$load_total += $cpu->loadpercentage;
}
$load = round($load_total/$cpu_num);
} else {
$sys_load = sys_getloadavg();
$load = $sys_load[0];
}
return (int) $load;
}
?>
I’m working from a linux machine and I can’t test but try this and tell me the result:
$command ="C:\\wmic cpu get loadpercentage";
echo shell_exec($command);
Or with that echoecho shell_exec("$command 2>&1 ; echo $?" );
– zwitterion
@zwitterion returned the 'C: wmic' message is not recognized as an internal & #Xa; or external command, a operable program, or a batch file.'
– Augusto
I removed C: from command ai he recognized, but still came empty.
– Augusto
found a bug tbm
– zwitterion
try the command like this
$command ="C:\\wmic^ cpu^ get^ loadpercentage";
– zwitterion
returned empty too, but I tested on another machine and it worked, it is something left to activate on the server.
– Augusto
it returns that Loadpercentage but the percentage doesn’t even show.
– Augusto