Linux

How to Monitor CPU Usage in Linux Over Time

How to Monitor CPU Usage in Linux Over Time

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

For the record, if you haven’t installed it, the htop utility (picture above) is much better to use than the default top.

The Trivial Solution: Use Your Cloud Provider’s Graphs

So far this solution is the easiest to use, but it won’t be available to everyone. If you use AWS, CloudWatch makes monitoring CPU usage very easy.

From the Cloud Management Management Console, you select “Metrics” and then see the metrics for EC2. The “CPUUtilization” metric shows average CPU utilization:

1

Your average CPU utilization is measured in increments of 5 minutes, but you can enable additional monitoring for instances and increase it to 1 minute increment. Doing so requires an additional fee. You can also easily set an alarm when CPU usage is too high.

If you use the Google Cloud Platform, a graphic appears under the “Monitoring” tab when you select an instance.

2

Azure has Azure Monitor, which displays similar info:

3

For most other cloud providers, they will likely have graphics like this too.

Using /proc/loadavg

The best way to do this natively is to see where the information was obtained. / proc / loadavg contains an average of 1 minute, 5 minutes and 15 minutes. You can log in with the cat

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

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

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

Connect this to a cron job that runs every minute, turn the log with logrotate, and you have got your own jerry-rigged CPU monitor. You can import CSV files into Excel, where it will be easy to graph the average CPU utilization on the line chart.

Note that the above command prints an average of 1 minute, 5 minutes and 15 minutes. If you run it every minute, there is no need to print 5- and 15 minutes on average, because you can calculate it computationally.

Install sysstat

The sar utility is great for monitoring system performance. This is included as part of the system, which may not be installed by default on your system. You must get it from your distribution package manager. For Debian based systems like Ubuntu, it will be:

sudo apt-get install sysstat
Next, activate it by editing / etc / default / sysstat and setting “ENABLED” to true.

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

You can generate real-time reports 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 a spreadsheet program or a special tool such as Sargraph, which displays good graphics:

4

You can also use command-line utilities to plot graphics like this, like ttyplot, but nothing comes close to using them that are easy (and look good) like a GUI. The command line beats this one – the graphics are better.

Monit Can Give Your Alarm If CPU Usage Is Too High

5

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 CPU usage becomes very high. Read our guide to prepare it to learn more.

Note that CloudWatch achieves the same thing as an alarm, and can operate on a variety of different metrics, not just CPU usage.

Related posts

How to Install the Google Chrome Web Browser on Ubuntu 20.04

Linux

How to Install VirtualBox Guest Additions on Ubuntu 20.04

Linux

How to install Linux Kernel 5.7 on Ubuntu

Linux

How to install the NetBox Network Management and Documentation Tool on Ubuntu 20.04 LTS

Linux

How to Install Ubuntu on a Raspberry Pi

Linux

What is Fedora Linux?

InstallWorld

How to Install Ruby on Rails (RoR) on Debian 10

Linux

How to Install and Use TeamViewer on Linux

Linux

How to install headless CAC Strapi Node.js

Abim