5

I want to know the memory and CPU usage in php, because I'm using cronejobs sometimes the CPU is overloaded so in this case I don't wan to start more process, I just want to skip this cron.

Mokus
  • 10,174
  • 18
  • 80
  • 122

4 Answers4

10

I think better way is get load avarage, because it depends not only on CPU, but on HDD speed also.

Here is a doc: http://php.net/manual/en/function.sys-getloadavg.php

neworld
  • 7,757
  • 3
  • 39
  • 61
  • There is three values what is that? Load Averages 0.48 0.76 0.43 – Mokus Sep 24 '11 at 11:18
  • First number is computed during last minute, second is computed during last 5 minutes and last numbers is computed during last 15 minutes. These numbers determine how much occupy a cores. So if you have single core and this numbers below 1.0 then your system is full responsibility. All info is here: http://en.wikipedia.org/wiki/Load_(computing) – neworld Sep 24 '11 at 12:22
6

Take a look at this library http://phpsysinfo.sourceforge.net/

Demo: http://phpsysinfo.sourceforge.net/phpsysinfo/index.php?disp=dynamic

Edit: This was taken from Stephen's comment. I put it here just so that it gets enough exposure for people to read.

Just a note: this library isn't too snappy. I couldn't find a way to just return CPU or just ram, it returns EVERYTHING, hardware list, network stats, hd usage, os info, pretty much everything you can think of. This takes it about 4 seconds to complete so it may not be the best option if you want a constantly updating value using ajax or something of the sort.

Kemal Fadillah
  • 9,760
  • 3
  • 45
  • 63
  • 1
    Just a note: this library isn't too snappy. I couldn't find a way to just return CPU or just ram, it returns EVERYTHING, hardware list, network stats, hd usage, os info, pretty much everything you can think of. This takes it about 4 seconds to complete so it may not be the best option if you want a constantly updating value using ajax or something of the sort. – Stephen Smith Mar 05 '13 at 23:34
  • @Stephen Thanks for the heads up Stephen. I've quoted your comment in my answer. Just in case some people might miss it. – Kemal Fadillah Mar 06 '13 at 07:22
2

How to get Memory and CPU usage in PHP

Perfect simple examples.

-- Memory in procentage % ($mem[2]/1000 for used memory in MB)

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

-- CPU in procentage %

function get_server_cpu_usage(){

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

}
nohelphere
  • 21
  • 2
1

I recently have some issue to get CPU load and i feel like sharing it .

Here was my solution witch help me out :

My situation :

I've use Zend Framework 1.2 to build monitoring application and i want to get cpu load and show it on the page . after doing some research i found out i could use COM Object and query the Win OS with wmi so i put these code to my init function :

    /* Initialize action controller here */
    $this->wmi = new COM('winmgmts:{impersonationLevel=impersonate}//./root/cimv2');

    if (!is_object($this->wmi)) {
        throw new GetInfoException('This needs access to WMI. Please enable DCOM in php.ini and allow the current user to access the WMI DCOM object.');
    }
  • You could use it anywhere you want , i have use it in the init function beacuse of Zend structure .

And i added an Action and an function to use that wmi and get cpu load any time i want by calling that function .

public function cpuloadAction()
{
    echo json_encode($this->getLoad());
    exit();
}

private function getLoad() {

    // Time?
    if (!empty($this->settings['timer']))
        $t = new LinfoTimerStart('Load Averages');

    $load = array();
    foreach ($this->wmi->ExecQuery("SELECT LoadPercentage FROM Win32_Processor") as $cpu) {
        $load[] = $cpu->LoadPercentage;
    }
    //return round(array_sum($load) / count($load), 2) . "%";
    return (int)round(array_sum($load) / count($load), 2);
}
  • beacuse i what real time data i put these code in a function Otherwise you could write it in single non Object Oriented PHP file.

Hope it help .