I guess the question is, could I somehow calculate the CPU utilization percentage just by reading the /proc/stat once?
# head -1 /proc/stat
cpu 67891300 39035 6949171 2849641614 118251644 365498 2341854 0
I am thinking about summarizing the columns except the IOWait (I was reading somewhere that it is counted in the idle) and that would give me the 100% and each individual column could be turned into percentage by (column/100_percent)*100.
- user: normal processes executing in user mode
- nice: niced processes executing in user mode
- system: processes executing in kernel mode
- idle: twiddling thumbs
- iowait: waiting for I/O to complete
- irq: servicing interrupts
- softirq: servicing softirqs
- steal: involuntary wait
- guest: running a normal guest
- guest_nice: running a niced guest
Is this a viable approach or i am totally off the track?
You are on the right track,
top
uses this file for this purpose. But you need to read it more than once.Utilization is a is a measure of use over time. You can read it once providing you know the uptime in seconds of the host, then divide by that, but that would give you a utility rate of the host since the machine was booted.
If you want a rate over 5 seconds, you would read the file once, sleep for 5 seconds, read it again, obtain the difference of the calculations and divide by 5.
Your approach is correct. You can use /proc/stat as raw data and feed for example rrdtool with it. I've done myself something like this so I know 100% it is possible. You can nicely then graph the whole system load or each core individually.
Here is a working example of my own:
Reassuming -> You can do it, it is not hard just basic mathematics and my graphs are a live example of that. For collecting data I do a snapshot of /proc/stat to a temporary file localted on ramdisk and then I parse that file to collect data every 1 minute.
how I parse the data (fragment of bash script):
after You get data into the rrd database You can graph and sky is the limit :) http://oss.oetiker.ch/rrdtool/doc/rrdgraph.en.html
If you want oneliner it may looks like this:
The result:
97.17000000000000000000
How it works:
produces
( 1055057784 - 1055058055 )/100
then this line feed to bc. Will have something like-7.84000000000000000000
, then just wrap it up to100-$(value)
and feed to bc againThere:
sleep #
- 1 second delay, if you change this value then the intermediate result should be divided to the number of seconds.$5
- 5th field, according toman proc
and http://www.linuxhowtos.org/System/procstat.htmPlease let me know if this doesn't work for you.
Or if you're happy with just two digits after the point awk can do it all a lot simpler and more readable: