-2

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:

25

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).

Neil
  • 2,425
  • 8
  • 36
  • 45
  • So this is exact output you get into stdout or file? CPU1 10% CPU2 20% CPU3 30% CPU4 40% – Danila Ladner May 08 '13 at 16:49
  • Why do you want one-liner? It's very difficult, the line will be very long. – cuonglm May 08 '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 08 '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 08 '13 at 16:55
  • Why not just grab the `/proc/loadavg`? Do you really care about the average of CPU usage? That isn't necessarily a useful value. If a single threaded process is going 100% on a single CPU, but the other three are idle, your system would show 25% usage, but that single app would be constrained. – Zoredache May 08 '13 at 16:58
  • 1
    See: http://stackoverflow.com/questions/9229333/how-to-get-overall-cpu-usage-e-g-57-on-linux – Zoredache May 08 '13 at 17:02
  • Load average will increase as disk I/O increases, and I need more granular information about that. In my situation it's more important to know overall CPU utilization rather than how many CPUs are pegged at 100%. – Neil May 08 '13 at 17:07

4 Answers4

1

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--------><----------Disks-----------><----------Network---------->
#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.

Edit:

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

enter image description here

ewwhite
  • 197,159
  • 92
  • 443
  • 809
  • 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 08 '13 at 16:52
  • There's `sar`... but probably not. – ewwhite May 08 '13 at 16:54
1

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 }'
cuonglm
  • 2,386
  • 2
  • 16
  • 20
  • 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 08 '13 at 17:10
1

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.

Neil
  • 2,425
  • 8
  • 36
  • 45
  • 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 08 '13 at 17:10
1

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.

Danila Ladner
  • 5,331
  • 22
  • 31