View Full Version : Display CPU usage load

02-08-2006, 01:19 AM
I want to display the cpu usage % in image, like this:
any idea how can I do that? Thanks

02-08-2006, 01:35 AM
you need 4 different images, each one with 1 pixel width. all with the same height.
image1 is the left border of your bar.

image2 is a the bar increment, and on top and bottom a piece of the border, like this:

image3 is the same as image2 but without the bar increment, just the bottom and top pixels of the border:

image4 is the right border of the bar.
then for representing a bar 60% full you should put in the same html table cell:
image1 once;
image2 60times;
image3 40times;
image4 once;
remember to put on the img params: border="0"

This example is the simpliest, no need to create images in php ( requiring gd libraries etc etc), just loops. Of course there are more ways to solve this problem.

I this explanation I'm assuming you use one pixel bars for each percent unit, but you can always adjust proportions.

02-08-2006, 01:37 AM
you would need to exec() some linux command to find the current CPU usage and then either create an image or make a table/graph out of that data .. cat /proc/stat shows some cpu usage info but you would still have to parse that.

02-08-2006, 01:40 AM
well I just explained the construction, missed the part of obtaining data.
thks firepages.

02-08-2006, 01:59 AM
Thanks. Do you know what php code to use?
And maybe the width of the image | will just change depending on how much %?

02-08-2006, 02:36 AM
varying widths is also a good aproach, since my example contains images that can be extruded horizontally without vertical deformation, but you still need the 4 images.
No time to create php code, sorry.

02-08-2006, 03:24 AM
I tried making one. But I am having errors. Can you tell me whats wrong?

$uptime = exec('uptime');
$uptimeArray = split(",", $string);
array_pad($uptimeArray, 3);

echo '<img src="images/bar.jpg" alt="" width="' . $uptime . '" />';

02-08-2006, 04:23 AM
This works for me ...

$uptime = exec('uptime');
$uptime = explode(",",$uptime);
$uptime = explode(':',$uptime[2]);
echo '<img src="images/bar.jpg" alt="" width="' . $uptime[1] . '" />';

02-08-2006, 04:32 AM
# cat /proc/loadavg
might be useful as well ?

02-08-2006, 06:39 AM
Ill try it soon!