Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to output the following:

Average CPU utilization across all cores, over the last n seconds, in a single percentage value.

So if I have 4 CPUs and their combined user and system utilization over the last 10 seconds is:

# not actual output
CPU1 10%
CPU2 20%
CPU3 30%
CPU4 40%

I want to be able to get this output:


Since the average of those utilizations is 25%.

What is the simplest one-liner to output this value?

(Not being able to specify the duration is fine, as long as it's a reasonable default).

share|improve this question

closed as not constructive by ewwhite, Ward, RolandoMySQLDBA, mdpc, petrus May 9 '13 at 16:51

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

So this is exact output you get into stdout or file? CPU1 10% CPU2 20% CPU3 30% CPU4 40% – Danila Ladner May 8 '13 at 16:49
Why do you want one-liner? It's very difficult, the line will be very long. – cuonglm May 8 '13 at 16:52
A long line is fine. I want to put this in to tmux so it will periodically show average CPU consumption. – Neil May 8 '13 at 16:54
The CPU1 10% isn't real output. I'm saying that you can get something equivalent to that output from one of the many CPU utilization utilities in Linux. – Neil May 8 '13 at 16:55
See:… – Zoredache May 8 '13 at 17:02

If the output similar like that, you can save it to a file, then:

cat output.txt | cut -d" " -f2 | cut -d"%" -f1 | awk '{ total += $1; count++ } END { print total/count }'
share|improve this answer
Part of the question was to actually get a utility that would output CPU utilization either per-CPU, or average over all CPUs. sar does this. – Neil May 8 '13 at 17:10

You could parse the output of the collectl utility, which is designed for what you're doing.

Running with no options gives:

[root@mdmarra ~]# collectl 
waiting for 1 second sample...
#cpu sys inter  ctxsw KBRead  Reads KBWrit Writes   KBIn  PktIn  KBOut  PktOut 
  28   6  2760   2596      0      0  41857    205      1      6      0       5 
  25   3  1499    820      0      0     40      4      1      8      1       4 
  25   0  1669   1039      0      0      0      0      4     27      3      25 
  25   0  1460    856      0      0      0      0      3     16      2      17 

Let me see if I can get the output you're looking for.


If you're just trying to put this in a tmux session, why not use nmon? It's prettier.

enter image description here

share|improve this answer
Is there any output from a program that is likely installed on CentOS by default? Or by processing output from proc? collectl isn't widely deployed. – Neil May 8 '13 at 16:52
There's sar... but probably not. – ewwhite May 8 '13 at 16:54

You can do this easily with sar:

sar -u 10 1 | tail -n 1 | awk '{print $8}' | perl -nle 'printf "%1.2f\n", 100 - $_;'

Explanation follows:

sar -u 10 1 will show average CPU utilization for all CPUs over the last 10 seconds 1 time.

awk will grab the 8th column, which is the idle time.

perl is subtracting idle time from 100%, which is "CPU utilization" in general.

share|improve this answer
Does anyone know how to get sar to show CPU utilization over the last second that has already passed, so this program won't block for the duration while it calculates the numbers? – Neil May 8 '13 at 17:10

How about mpstat?

mpstat -P ALL 2 1 | grep "Average.*all" | awk '{print $3+$5}'

Will print average of all cpus for last 2 seconds.

I think sysstat is highly available on RHEL OS.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.