How to Monitor CPU Usage in Linux Over Time

superior utility displaying current processor usage for each running process

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

Note, if you haven’t already installed it, the htop utility (illustrated above) is much more pleasant to use than the default top.

The trivial solution: use graphics from your cloud provider

This solution is by far the easiest to use, but it will not be available to everyone. If you’re using AWS, CloudWatch makes it easier to monitor CPU usage.

Of CloudWatch management console, select “Metrics”, then display the metrics for EC2. The “CPUUtilization” metric displays your average CPU usage:

Your average processor usage is measured in 5-minute increments, but you can enable extended instance monitoring and increase it in 1-minute increments. However, this costs an additional fee. You can also easily set alarms when the CPU usage is also too high.

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

Azure a Azure Monitor, which displays similar information:

Azure Monitor displays information similar to that of the Google Cloud Platform.

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

Using / proc / loadavg

The best way to do this in native mode is to look at where top gets its information. / proc / loadavg contains averages of 1 minute, 5 minutes and 15 minutes. You can connect with chat

chat / proc / loadavg /
1.71 1.32 1.38 2/97 6,429

You can use it to generate a graph by printing each line in a comma separated CSV file, using some awk magic:

cat / proc / loadavg | awk ‘{print $ 1 “,” $ 2 “,” $ 3}’ >> cpu.csv

Hang it on a Scheduled task run every minute, rotate logs with logrotateand you have a jerry-rigged processor monitor. You can import the CSV file into Excel, where it will be easy to graph the average CPU usage on a line graph.

Note, the above command prints the averages of 1 minute, 5 minutes and 15 minutes. If you run it every minute, you don’t really need to print the 5 and 15 minute averages, as you can calculate it by calculation.

Install sysstat

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

sudo apt-get install sysstat

Then activate it by editing / etc / default / sysstat and setting “ENABLED” to true.

This monitors your system and generates a report every 10 minutes, rotating them after a week. You can modify this behavior by modifying the sysstat crontab in /etc/cron.d/sysstat or by modifying the rotation parameters in the sysstat parameters in / etc / sysstat / sysstat.

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

sar -u 1 3

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

sargraph displays a graph of processor usage.

You can also use command line utilities to plot graphics like this, such as ttyplot, but none of them are as easy to use (and as beautiful) as a GUI. The command line is beaten on it – the graphics are nicer.

Monit can alarm you if the CPU usage is too high

Monit logo

Monit is an 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 processor usage becomes dangerously high. Read our configuration guide to learn more.

Note that CloudWatch does the same thing out of the box with alarms, and it can run on several different measures, not just on CPU usage.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.