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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.