V
V
vista1x2015-08-07 18:50:21
PHP
vista1x, 2015-08-07 18:50:21

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
  );
}


But the problem is that all this is not done so quickly.
Are there other ways to get the information I need in the shortest possible time?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Lebedev, 2015-08-07
@swanrnd

Write a standalone C++ console program and call it via PHP.
Windows Server and PHP have never meant high performance.

S
Stalker_RED, 2015-08-07
@Stalker_RED

The problem is that by itself it wmic cpu get loadpercentage 2>&1takes quite a long time. We need to look for a different approach.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question