Detecting CPU load with PHP

Discussion in 'PHP' started by inderpal, Aug 21, 2006.

  1. #1
    Hi All,

    There is some serious issue with one of my servers. So, I thought of writing a small PHP script which gives me the CPU load just like the top command in linux environment? If any of you have done this, please provide me any pointers/directions.

    I would appreciate any help in this regard. Thanks in advance!!

    Best Regards,
    Inderpal Singh
     
    inderpal, Aug 21, 2006 IP
  2. Litewebsite

    Litewebsite Guest

    Messages:
    26
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I haven't tried this myself but this link seem to have what you are looking for forums.invisionpower.com/index.php?showtopic=156890 .
     
    Litewebsite, Aug 21, 2006 IP
  3. Brilliances

    Brilliances Active Member

    Messages:
    619
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I would think php-sysinfo would be a good starting point. I think it is open source so maybe you can contribute some additional functionality :)
     
    Brilliances, Aug 22, 2006 IP
  4. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You might be able to open '/proc/loadavg' and read out the contents. Here's what my box reports:

    0.04 0.02 0.00 1/92 7089
    Code (markup):
    First three numbers will be average load over the last 1, 5 and 15 minutes. I have no idea what the next two are, though the last number looks like the current highest (or next) process id.

    You might also be interested in '/proc/meminfo'.

    Cryo.
     
    Cryogenius, Aug 22, 2006 IP
  5. inderpal

    inderpal Active Member

    Messages:
    919
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    68
    #5
    Hi All,

    Thanks for all your help but I want the output exactly to be look like TOP command in linux.

    Best Regards,
    Inderpal Singh
     
    inderpal, Aug 22, 2006 IP
  6. mihd

    mihd Peon

    Messages:
    136
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    only works on linux, dont forget to give me some of that green rep :)

    
    	$uptime1 = @exec('uptime');
    	preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$uptime1,$avgs1);
    	$uptime1 = explode(' up ', $uptime1);
    	$uptime1 = explode(',', $uptime1[1]);
    	$uptime1 = $uptime1[0];
    	$start1=mktime(0, 0, 0, 1, 1, date("Y"), 0);
    	$end1=mktime(0, 0, 0, date("m"), date("j"), date("y"), 0);
    	$diff1=$end1-$start1;
    	$days1=$diff1/86400;
    	$percentage1=($uptime1/$days1) * 100;
    	$load1=$avgs1[2];
    PHP:
     
    mihd, Aug 22, 2006 IP
  7. Gordaen

    Gordaen Peon

    Messages:
    277
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You all do things the complicated way ;)

    <?php
    echo shell_exec('top -b -n 1');
    ?>
    PHP:
    You may want to format it or just return it as regular text :)
     
    Gordaen, Aug 22, 2006 IP