Show use of Processor

Asked

Viewed 833 times

6

I am trying to show the use of the server processor with PHP. Server is Windows with IIS. I’ve tried to use :

exec("wmic /node:localhost path Win32_Processor where DeviceID='CPU0' get LoadPercentage", $out);

exec("wmic cpu get loadpercentage", $out);

And a few more ways, but it always comes back empty. Does anyone know if you need any special permission to do this, or has any other idea ?

  • 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 echo echo shell_exec("$command 2>&1 ; echo $?" );

  • @zwitterion returned the 'C: wmic' message is not recognized as an internal & #Xa; or external command, a operable program, or a batch file.'

  • I removed C: from command ai he recognized, but still came empty.

  • found a bug tbm

  • try the command like this $command ="C:\\wmic^ cpu^ get^ loadpercentage";

  • returned empty too, but I tested on another machine and it worked, it is something left to activate on the server.

  • it returns that Loadpercentage but the percentage doesn’t even show.

Show 2 more comments

2 answers

6


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 had seen this phpsysinfo, it shows number of processes, and which ones are active, but it doesn’t show percentage of processor usage.

  • On the php website it says sys_getloadavg() does not work on the Windows platform. If it were Linux it would probably have worked.

  • This last alternative apparently works, but still returns 0, I think it lacks some permission...

2

First check where the wmic executable is.

  1. Go to windows cmd and write where wmic.
  2. In my case I received this reply: C:\Windows\System32\wbem\WMIC.exe
  3. Then replace your path, if different from mine, in this command: echo shell_exec("C:\\Windows\\System32\\wbem\\WMIC.exe cpu get loadpercentage");
  4. You will have the answer in browser loadpercentage 19 exactly equal to the answer in the terminal window.
  5. Other sweethearts.
  • I tested with WMIC CPU GET NAME and returned the name, but the loadpercentage continues to return empty. It will be possible to loadpercentage be disabled or something ?

  • here on my returned empty tbm. Even when I run on windows terminal it comes empty. After I tried more times and he replied well. In terminal and browser.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.