Linux display average CPU load for last week
Asked Answered
S

3

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.

Stypsis answered 10/2, 2009 at 6:12 Comment(1)
not allowed? is this a schoo/college project?Mambo
A
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.

Animality answered 10/2, 2009 at 6:21 Comment(0)
U
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

Unpleasant answered 10/2, 2009 at 6:24 Comment(5)
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.Stypsis
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).Unpleasant
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?Stypsis
@Stypsis check the man page for sar. It stores loadaverages.Epictetus
I owe you a coffee, sir.Cornered
A
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.

Animality answered 10/2, 2009 at 6:21 Comment(0)
P
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.

Pedroza answered 10/2, 2009 at 6:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.