22

On a Linux box, I need to display the average CPU utilisation per hour for the last week. Is that information logged somewhere? Or do I need to write a script that wakes up every 15 minutes to copy /proc/loadavg to a logfile?

EDIT: I'm not allowed to use any tools other than those that come with Linux.

thornate
  • 4,902
  • 9
  • 39
  • 43

3 Answers3

28

You might want to check out sar (man page), it fits your use case nicely.

System Activity Reporter (SAR) - capture important system performance metrics at periodic intervals.

Example from IBM Developer Works Article:

Add an entry to your root crontab

# Collect measurements at 10-minute intervals
0,10,20,30,40,50   * * * *   /usr/lib/sa/sa1
# Create daily reports and purge old files
0                  0 * * *   /usr/lib/sa/sa2 -A

Then you can simply query this information using a sar command (display all of today's info):

root ~ # sar -A

Or just for a certain days log file:

root ~ # sar -f /var/log/sa/sa16

You can usually find it in the sysstat package for your linux distro

Brian Gianforcaro
  • 26,564
  • 11
  • 58
  • 77
  • 2
    I looked at sar. But as far as I can see it only shows the current CPU. I'd have to have it running every few seconds and do averages. Please tell me if I'm wrong, as it would be useful. – thornate Feb 10 '09 at 06:46
  • You set sar up to log to a file using the crontab example in my answer. You can then use sar to query that generated log, or a number of logs for the stats you want (load average). – Brian Gianforcaro Feb 10 '09 at 07:00
  • But does that not mean having sar running every few seconds? Wouldn't that be inefficient compared to just checking the /proc/loadavg log every 15 minutes? – thornate Feb 10 '09 at 07:03
  • 1
    @thornate check the man page for sar. It stores loadaverages. – Zoredache Feb 10 '09 at 08:04
  • 1
    I owe you a coffee, sir. – plushyObject Apr 12 '18 at 18:15
16

As far as I know it's not stored anywhere... It's a trivial thing to write, anyway. Just add something like

cat /proc/loadavg >> /var/log/loads

to your crontab.

Note that there are monitoring tools (like Munin) which can do this kind of thing for you, and generate pretty graphs of it to boot... they might be overkill for your situation though.

jay_aye_see_kay
  • 522
  • 5
  • 12
David Z
  • 128,184
  • 27
  • 255
  • 279
1

I would recommend looking at Multi Router Traffic Grapher (MRTG).

Using snmpd to read the load average, it will automatically calculate averages at any time interval and length, along with nice charts for analysis.

Someone has already posted a CPU usage example.

cmcginty
  • 113,384
  • 42
  • 163
  • 163