2

I'm currently designing a graph system for a website that shows current CPU usage, you can see my work in progress here - http://nereus.rikkuness.net/admin/cpu2.php

It's working exactly as I want it to at the moment, but the problem is the way that I am polling for CPU usage is currently

sar 1 | sed -n '5p' | awk '{ print $8; }'

Which works fine but it takes around a second or so to reply with CPU usage which is making the graph not update as I would expect it to in the browser.

Any help on a command that will give me the same information but much faster would be massively appreciated.

Many thanks,

Steve

2
  • Where is the delay coming from? linux returning a value, or the response getting to the browser. Commented Dec 18, 2012 at 19:49
  • linux returning the value. Commented Dec 18, 2012 at 19:58

3 Answers 3

1

You can simply use sar 0. This will return an immediate result instead of waiting for a 1 second interval like you do with sar 1.\

Note that this will only return a single point in time value, so you might need to use javascript to track the previous values (i.e. add new value to array and pop old value off).

Sign up to request clarification or add additional context in comments.

4 Comments

You're kidding me... God damn this whole time I never even tried that. I had no idea the 1 meant a 1 second delay
@Steve The 1 actually means 1 second interval.
Well, now I changed it to sar 0 it shows a permenent usage of 10.67% and doesn't change if I stress the CPU. sar 1 showed change.
sar 0 gives incorrect information. sar 1 gives an average usage over 1 second, 0 doesn't give any average usage because it doesn't have time to work out an average.
1

How about using PHP's internal sys_getloadavg function? http://php.net/manual/en/function.sys-getloadavg.php

Additionally for just getting the current load something allong the following lines might suffice:

<?php
$output = shell_exec('cat /proc/loadavg');
$loadavg = substr($output,0,strpos($output," "));
?>

Comments

0

If you can relay on html5 you can use web sockets for your task.

WebSocket is a web technology providing full-duplex communications channels over a single TCP connection. The WebSocket API is being standardized by the W3C, and the WebSocket protocol has been standardized by the IETF as RFC 6455.

Since you are using linux, I guess that's not an issue, since Firefox and chrome are both supporting them (http://caniuse.com/#feat=websockets ).

5 Comments

@Steve in what way are you polling the information now? In either case, I disagree, I don't think web sockets is a good choice for this particular application.
Well, I have one PHP page that does this: $output = shell_exec("sar 0 | sed -n '4p' | awk '{ print $8; }'"); $value = 100 - $output; echo $value; and the other page, well, you can view the source on my original link and see what it does with that page
you go from you client to the server, which opens and closes a new conection every time. Also there is a data overhead from all the http headers. If you use web sockets, a permanent connection is established and the web server can send the request directly to the client.
This needs to be compatible with everyone in all browsers. I can't rely on HTML5.
OK, this answer was how you make your client server cominication more effective. Polling is always kinda slow, and you will get problems with your performance if too many users are connect. If you only have a very small number of users and must support all browsers, you should go with polling

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.