18

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

How would I do that?

CodeChap
  • 4,132
  • 6
  • 30
  • 40
Mehdi Homeyli
  • 409
  • 1
  • 4
  • 8
  • [`shell_exec`](http://www.php.net/manual/en/function.shell-exec.php) should do it or try `var_dump($_SERVER)` for minimal info – Rahil Wazir Apr 08 '14 at 22:21
  • is your question answered here? http://stackoverflow.com/questions/4705759/how-to-get-cpu-usage-and-ram-usage-without-exec – Stephen Crosby Apr 08 '14 at 22:24
  • there's a terminal command, `nice top` (or just `top`) that outputs your CPU usage and RAM. Combined with shell_exec() and a little parsing of the output should get what you're after. – roycable Apr 08 '14 at 22:24

2 Answers2

30

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];

}
Snoobih
  • 311
  • 2
  • 9
  • 4
    I suspect both of these will only work in *nix-like operating systems. – halfer Apr 08 '14 at 23:42
  • 4
    `sys_getloadavg()` returns `load` which is not necessarily CPU usage. It includes disk speeds, etc. – kouton Mar 24 '15 at 16:25
  • 1
    And how to get the current (not avg) cpu usage? – zypro Apr 20 '16 at 08:12
  • You can use `preg_split()` instead of `explode()` since there are multiple white spaces in the target row: `preg_split("/[\s]+/", $free_arr[1])`. This means you can also get rid of `array_filter()` and `array_merge()` too. http://stackoverflow.com/a/38085813/2418655 – dhaupin Jun 28 '16 at 20:39
  • I would caution against actually using this answer, because free does not actually return the active ram used, it returns all ram assigned, both inactive and active, which can be highly deceptive. The linux kernel keeps memory page data for rapid reuse that is not actually being actively used, and that's roughly what free tells you. The kernel always tries to use all the ram in a sense it can since that is the fastest, but most of what free reports is actually inactive stuff that the kernel will reuse if there is call for it. htop shows what is actually active. – Lizardx Aug 21 '16 at 20:52
  • You can get the data you need from free, but it requires more processing, I am not positive, but I believe the second row of output: -/+ buffers/cache: used is the actual active used ram, so you could take the total from row 1, and the used from row 2, or, you could take from row 2 used and then used+free for total. In terms of server performance issues, that's the data that actually matters, it's basically useless to know what the kernel has assigned but inactive. kernel ram reporting is tricky to understand and work with, however. Different free versions put this data in varying columns/rows – Lizardx Aug 21 '16 at 21:00
  • @halfer Where else? I guess you would have to open up a com to wim in svchost or something and fetch the value with an SQL query. Might as well use C# then. – Secko May 03 '17 at 14:19
  • If I am making too many calls to this function, Is there possibility of downgrading system performance? – laughing buddha May 25 '18 at 02:10
5

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/

Garreth McDaid
  • 2,427
  • 22
  • 34