Tracking CPU use on a server

Discussion in 'PHP' started by wvccboy, Jan 13, 2008.

  1. #1
    Running some major-traffic sites on shared hosting accounts is pretty tricky.

    I need to track the CPU use on a shared hosting account, so I know when to shut down the site as soon as it hits the limit.

    It is on cPanel if that makes any difference.

    Any ideas on how this can be accomplished would be good.

    Thanks
     
    wvccboy, Jan 13, 2008 IP
  2. hostingcoupon

    hostingcoupon Peon

    Messages:
    447
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You want to track the USAGE of your scripts or the load of the server?
     
    hostingcoupon, Jan 13, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    If you're on a Linux server you can try this (Taken from vBulletin):

    
    $servertoobusy = false;
    if ($vbulletin->options['loadlimit'] > 0 AND PHP_OS == 'Linux' AND @file_exists('/proc/loadavg') AND $filestuff = @file_get_contents('/proc/loadavg'))
    {
    	$loadavg = explode(' ', $filestuff);
    	if (trim($loadavg[0]) > $vbulletin->options['loadlimit'])
    	{
    		$servertoobusy = true;
    	}
    }
    
    PHP:

    You can also give memory_get_usage() a try.


    EDIT:

    Perhaps you can find a cPanel log parser on hotscripts or Google.
     
    nico_swd, Jan 14, 2008 IP
  4. wvccboy

    wvccboy Notable Member

    Messages:
    2,632
    Likes Received:
    81
    Best Answers:
    1
    Trophy Points:
    250
    #4
    Thanks. It looks like that vBulletin code only works with the option (loadlimit)?

    I'll try the log parser.

    I just got what I was afraid of : suspension :( for CPU usage
     
    wvccboy, Jan 14, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    This loadlimit is just a variable. You'd have to replace $vbulletin->options['loadlimit'] with the actual value of the load limit that YOU want to set.
     
    nico_swd, Jan 14, 2008 IP
  6. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #6
    there is the vmstat command in linux. make use of it.

    I did a quick tweak for it to return cpu idle time (a percentage) at the time of checking.

    so command becomes:

    so if it returns 99, it means it is 99% percent idle, 1% busy

    so you can pass that to shell_exec() in a php script and echo the output, example:

    
    <?
    $idle = shell_exec("vmstat|awk {'print $15'}|tail -n 1");
    $busy = 100-$idle;
    $busy .= "%";
    echo "CPU is $busy busy";
    ?>
    
    PHP:
    good luck

    PS: the number '15' above corresponds to the column titled 'cpu id'. it may vary. so execute vmstat at the prompt at first and get the count right
     
    legend2, Jan 14, 2008 IP
  7. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Assuming you don't own the server, there's no single way to track cpu usage. Certain shared host providers do try to track CPU usage, but that's own down to more server side code (which you can't access or manipulate) based on time consumption.

    Jay
     
    jayshah, Jan 14, 2008 IP