Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Detecting CPU load with PHP

    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.

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    function ServerLoad()
    {

    $stats exec('uptime');

    preg_match('/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/'$stats$regs);

    return (
    $regs[1].', '.$regs[2].', '.$regs[3]);



    Safe mode must be off

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks! That is exactly what I needed.

  • #4
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Hmm... That didn't work for me, I took that and modified it for a percentage and ended up with:
    PHP Code:
    <?php

     
    function ServerLoad() 


    $stats explode(' 'substr(exec('uptime'), -14)); 
    $av round((($stats[0] + $stats[1] + $stats[2]) / 3)*100);
    return (
    $av); 


    }

    echo 
    ServerLoad() . '%';

    ?>

  • #5
    Regular Coder
    Join Date
    Jan 2005
    Posts
    470
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Error 404
    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.

  • #6
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Umm... No. The uptime output is:
    Code:
    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.

  • #7
    New to the CF scene
    Join Date
    Sep 2003
    Location
    ca
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the first element $stats[0] is your current cpu load, you don't need to divided by 3 to get average value.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •