View Full Version : Detecting CPU load with PHP
07-01-2005, 06:46 PM
I am having a serious issue with one of my host's servers. It appears that something somewhere in a PHP script is at random intervals is placing an enormous load on the CPU and memory usage. I have tested and tested and tested every possible scenario that I can think of that could be causing this but I only get normal CPU activity.
What I need to be able to do is insert some bit of PHP code in all the suspect scripts that I can use to detect when the CPU usage jumps up to 99%. I can then dump all the relevant variable values to determine what exactly is the cause.
Bumping around in the dark has gotten me nowhere thus far.
07-02-2005, 06:53 AM
$stats = exec('uptime');
preg_match('/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/', $stats, $regs);
return ($regs.', '.$regs.', '.$regs);
Safe mode must be off
07-02-2005, 10:50 PM
Thanks! That is exactly what I needed. :thumbsup:
07-02-2005, 11:43 PM
Hmm... That didn't work for me, I took that and modified it for a percentage and ended up with:
$stats = explode(' ', substr(exec('uptime'), -14));
$av = round((($stats + $stats + $stats) / 3)*100);
echo ServerLoad() . '%';
07-05-2005, 02:58 AM
Hmm... That didn't work for me, I took that and modified it for a percentage and ended up with
If you do explode(' ', substr(exec('uptime'), -14)) you are going to end up with commas in each element of the $stats array.
07-05-2005, 07:16 PM
Umm... No. The uptime output is:
18:14 up 1:14, 2 users, load averages: 0.33 0.17 0.21
However, those load averages are number of processes, which means this is a bad way of detecting CPU load, and that any percentages are impossible.
07-14-2006, 10:59 PM
the first element $stats is your current cpu load, you don't need to divided by 3 to get average value.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.