0

Hi

i want to display runtime server load using php code. I find the below code from other website but i dont know i am going on the correct way.

Pleae help me.

function get_server_load() 
{
    $load=array();
    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
    {
        $load = sys_getloadavg();
    }
    return $load;
}
echo implode(' ',get_server_load());
2
Contributors
1
Reply
19
Views
1 Year
Discussion Span
Last Post by rch1231
0

This may be over simplfying it but I used the following to give me the load average depending on the OS .

For Linux

<?php
$output = shell_exec('w | head -1 ');
echo "<pre>$output</pre>";
?>

For Windows

<?php
     echo system('typeperf "\Processor(*)\% Processor Time"');
?>

I noticed you were checking a windows system after I had added the Linux script so I added the WIndows one.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.