X
Popular Searches

How to Monitor CPU Usage in Linux Over Time

top utility displaying current CPU usage for each running process

The top utility displays current CPU usage for each running process, but what if you wanted to monitor this over time and display it on a graph? There are a few utilities for this if your cloud provider doesn’t have one already.

As a side note, if you don’t have it installed already, the htop utility (pictured above) is a lot nicer to use than default top.

The Trivial Solution: Use Your Cloud Provider’s Graphs

This solution is by far the easiest to use, but it won’t be available for everyone. If you’re on AWS, CloudWatch makes monitoring CPU usage very easy.

From the CloudWatch Management Console, you select “Metrics” and then view metrics for EC2. The “CPUUtilization” metric displays your average CPU utilization:

In the CloudWatch Management Console, select "Metrics" and then view metrics for EC2.

Your average CPU utilization is measured in 5-minute increments, but you can enable extended monitoring for the instance and bump it up to 1-minute increments. Doing so does cost extra though. You’re also able to easily set alarms for when CPU usage gets too high as well.

If you’re on Google Cloud Platform, a graph appears under the “Monitoring” tab when you select an instance.

Graph that appears on Google Cloud Platform under the "Monitoring" tab when an instance is selected.

Azure has Azure Monitor, which displays similar info:

Azure Monitor displays similar info as Google Cloud Platform.

For most other cloud providers, they’ll likely have a graph like this as well.

Using /proc/loadavg

The best way to do this natively is to look at where top gets its information from. /proc/loadavg contains 1-minute, 5-minute, and 15-minute averages. You can log it with cat

cat /proc/loadavg/
1.71 1.32 1.38 2/97 6429

You can use this to generate a graph by printing each line into a comma-seperated CSV file, using some awk magic:

cat /proc/loadavg | awk '{print $1","$2","$3}' >> cpu.csv

Hook this up to a cron job running every minute, rotate logs with logrotate, and you’ve got yourself a jerry-rigged CPU monitor. You can import the CSV file into Excel, where it will be easy to graph the average CPU utilization on a line chart.

Note, the above command prints the 1-minute, 5-minute, and 15-minute averages. If you’re running it every minute, it’s not really necessary to print the 5- and 15-minute averages, because you can figure that out computationally.

Install sysstat

The sar utility is great for monitoring system performance. It’s included as part of sysstat, which is probably not installed by default on your system. You’ll have to get it from your distro’s package manager. For Debian-based systems like Ubuntu, it would be:

sudo apt-get install sysstat

Next, enable it by editing /etc/default/sysstat and setting “ENABLED” to true.

Doing so monitors your system and generates a report every 10 minutes, rotating them out after a week. You can modify this behavior by editing the sysstat crontab at /etc/cron.d/sysstat, or by changing rotation settings in the sysstat settings at /etc/sysstat/sysstat.

You can generate a real-time report with the following command:

sar -u 1 3

sysstat will collect background CPU usage data every minute, saving it to /var/log/sysstat/. You can then import this data for analysis, using either a spreadsheet program or a custom tool like sargraph, which displays a nice chart:

sargraph displays a chart of CPU usage.

You can also use command line utilities for plotting graphs like this, such as ttyplot, but none of them come close to being as easy to use (and as nice looking) as a GUI. The command line is beat on this one—charts are nicer.

Monit Can Alarm You If CPU Usage Is Too High

Monit logo

Monit is a open source monitoring suite for Unix that checks the health of your server and can be configured to send you notifications if your server’s CPU usage becomes dangerously high. Read our guide to setting it up to learn more.

Note that CloudWatch achieves the same thing out of the box with alarms, and it can operate on multiple different metrics, not just CPU usage.

Anthony Heddings Anthony Heddings
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon's AWS platform. He's written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.