How can I get the CPU and Memory useage
Asked Answered
F

4

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.

Fenny answered 24/9, 2011 at 9:52 Comment(0)
L
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

Laurent answered 24/9, 2011 at 10:20 Comment(2)
There is three values what is that? Load Averages 0.48 0.76 0.43Fenny
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: en.wikipedia.org/wiki/Load_(computing)Laurent
M
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.

Mast answered 24/9, 2011 at 9:55 Comment(2)
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.Dependence
@Stephen Thanks for the heads up Stephen. I've quoted your comment in my answer. Just in case some people might miss it.Mast
D
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];

}
Danikadanila answered 3/4, 2014 at 18:45 Comment(1)
It will be very much great if you elaborate your answer with an example too.Cnut
S
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 .

Selfhood answered 2/12, 2012 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.