How do you get server CPU usage and RAM usage with php? [duplicate]

99封情书 提交于 2019-12-30 03:05:09

问题


I want to get server CPU and RAM usage using php. The script should work on windows and linux.

How would I do that?


回答1:


The first function will return the Server Memory Usage:

function get_server_memory_usage(){

    $free = shell_exec('free');
    $free = (string)trim($free);
    $free_arr = explode("\n", $free);
    $mem = explode(" ", $free_arr[1]);
    $mem = array_filter($mem);
    $mem = array_merge($mem);
    $memory_usage = $mem[2]/$mem[1]*100;

    return $memory_usage;
}

This function will return the Server CPU Usage:

function get_server_cpu_usage(){

    $load = sys_getloadavg();
    return $load[0];

}



回答2:


I'd advise using PHP SNMP

http://www.php.net/manual/en/book.snmp.php

This will provide a unified solution for both Windows and Linux without have to mess around with exec commands.

You will of course need to install a Windows SNMP daemon/service on both your Windows and Linux servers

For Linux, just use Net-SNMP eg CentOS

sudo yum install net-snmp
sudo service snmpd start
sudo chkconfig snmpd on

Net-SNMP is also available for Windows:

http://www.net-snmp.org/



来源:https://stackoverflow.com/questions/22949295/how-do-you-get-server-cpu-usage-and-ram-usage-with-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!