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.
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
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.
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];
}
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 .
© 2022 - 2024 — McMap. All rights reserved.