2

At the moment I am using a python script to log the output from cat /proc/loadavg to a file. I then use gnuplot to periodically graph the CPU load for my viewing pleasure.

However, I really would like to have a graph that shows percentages of CPU usage (%user and %sys, etc.) instead. I can see that top can get to those numbers, since they are displayed at the third line of its output. However, I have not been able to figure out how or where top gets that information. Google is no help either, it just points me to either /proc/loadavg or problems people are having with high CPU loads.

Any hints on how I would be able to log those numbers (preferably as 5-minutes averages) to a file would be greatly appreciated.

Mausy5043
  • 740
  • 2
  • 10
  • 27
  • Have you seen this? http://www.cyberciti.biz/tips/how-do-i-find-out-linux-cpu-utilization.html – Flint May 11 '14 at 11:29
  • @Flint : Yes. I have sysstat installed. I can only get sar to hand me 10 minute averages. And mpstats numbers don't compare with those from top. – Mausy5043 May 11 '14 at 11:39
  • @Flint : Mmm, they do add up better than top, but mpstat is a bit of a strain on an embed system. Let me look into that further. – Mausy5043 May 11 '14 at 11:45

1 Answers1

1

RTFM: The manpage for top is a long read but eventually things became clear.

I'm now using this:

top -bn 1 |grep "Cpu(s)" | awk '{print "user: " $2+$6 "% syst: " $4+$12+$14+$16 "% wait: " $10 "% idle: "$8"%   Total: " $2+$4+$6+$8+$10+$12+$14+$16 "%"}'

Unfortunately it hardly ever perfectly totals at 100%

@boson:~ $ top -bn 1 |grep "Cpu(s)" | awk '{print "user: " $2+$6 "% syst: " $4+$12+$14+$16 "% wait: " $10 "% idle: "$8"%   Total: " $2+$4+$6+$8+$10+$12+$14+$16 "%"}'
user: 1.2% syst: 0.7% wait: 0.8% idle: 97.4%   Total: 100.1%
@boson:~ $

An alternative would be to install the sysstat package and use mpstat thusly:

@boson:~ $ mpstat | tail -1 | awk {'print "user: " $4+$5 "% syst: " $6+$8+$9+$10+$11+$12 "% wait: " $7 "% idle: " $13 "%   Total: " $4+$5+$6+$7+$8+$9+$10+$11+$12+$13 "%"}'
user: 1.18% syst: 0.66% wait: 0.76% idle: 97.39%   Total: 99.99%
@boson:~ $
Mausy5043
  • 740
  • 2
  • 10
  • 27