Answer the question
In order to leave comments, you need to log in
Get information about system load?
Given: server on Windows
Required: using PHP to get the following information: CPU usage (in %), RAM usage (in %)
Currently implemented as follows:
# cpu
function getCpuUsage()
{
exec("wmic cpu get loadpercentage 2>&1", $s);
return isset($s[1]) ? $s[1] : 0;
}
# ram
function getRam($command)
{
exec($command . " 2>&1", $s);
return isset($s[1]) ? round(((int)$s[1])/1024/1024, 2) : 0;
}
function getRamFree()
{
return getRam("wmic OS get FreePhysicalMemory 2>&1");
}
function getRamAll()
{
return getRam("wmic OS get TotalVisibleMemorySize 2>&1");
}
function getRamUsage()
{
$all = getRamAll();
$free = getRamFree();
$usage = $all - $free;
return array(
"free_gb" => $free,
"usage_gb" => $usage,
"usage_percent" => $usage * 100 / $all
);
}
Answer the question
In order to leave comments, you need to log in
Write a standalone C++ console program and call it via PHP.
Windows Server and PHP have never meant high performance.
The problem is that by itself it wmic cpu get loadpercentage 2>&1
takes quite a long time. We need to look for a different approach.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question