How to Monitor System Resources in Linux

Written by: Emmanuel Oyibo   |   Last updated: April 27, 2024

You have to monitor your Linux system to ensure it’s healthy and running smoothly. To spot problems early, you need to watch things like CPU, memory, disk space, network traffic, and more. This will help you figure out the issue and fix it quickly.

In this tutorial, we’ll learn how to keep tabs on our Linux system resources.

Types of Resources to Monitor

Let’s have a quick overview of some of the most important things you need to monitor on your Linux system:

  • CPU: This is the brain of the computer. It allows you to easily locate programs that are consuming too much resources or slowing your system down.
  • Memory: Your system actually needs enough memory to run well. So, you need to keep an eye on your system to know if your programs need more RAM or if memory is being wasted.
  • Disk: Watching disk space and activity is pretty essential. This can help you prevent issues like a slow system.
  • Network: You also have to monitor network traffic on your Linux system to help you spot strange activity or fix connection problems.

Tools for Monitoring System Resources

Linux comes with a good set of command line tools for monitoring your system. Let’s learn about some of these tool.

Using the top Command

The top command is a good way to get a quick, real-time look at your system. It displays a list that’s being updated regularly. The list provides information such as CPU usage, memory usage, and Process IDs (PIDs).

Here’s how to use the command:

$ top

Using the htop Command

The htop command is a more user-friendly and interactive alternative to top. You can see the information it presents, almost the same way as top.

However, htop offer a more visually-appealing format. Also, it comes with color-coding and easier navigation.

Now, let’s take a look at how to use the command:

$ htop

Using the vmstat Command

The vmstat command focuses on giving you a snapshot of your system’s virtual memory, processes, I/O, and CPU activity.

For example, you can display stats every 5 seconds, using the command as follows:

$ vmstat 1 5

Using the iostat Command

The iostat command is your go-to tool for in-depth disk input/output (I/O) monitoring. It allows you to see how much data is being read and written to your disk. You can see these metrics per device.

Now, let’s take a look at how to use the command. In the example below, the command displays the disk statistics every 2 seconds for 5 intervals:

$ iostat -d 2 5

Using the df Command

This command offers you a simple but essential overview of how much disk space is available and currently in use by your Linux system.

It’s a good habit to run this command regularly so that you can catch drives that are getting full before they cause problems.

You can view your systems disk usage in human readable format as follows:

$ df -h

Using the du Command

The du command (short for “disk usage”) goes a level deeper than df. It allows you to see how much space individual directories and their content are taking up.

You can look up the total size of each directory within your home folder with the command below:

$ du -sh /home/*

Using the free Command

This command offers a clear picture about how your system is using its memory (RAM). It’s pretty easy to use, making it great for fast memory check.

You can use the command to display memory usage in megabytes as follows:

$ free -m

Using the sar Command

The sar command (short for “System Activity Report”) collects, reports, and saves system performance data. It can show you historic data on CPU usage, memory, disk I/O, and more.

The command below shows CPU utilization every 2 seconds for 5 intervals:

$ sar -u 2 5

Using the mpstat Command

The mpstat command offers you the CPU utilization statistics in details. It’s pretty useful for multi-processor systems. You can see how much of each individual CPU core is in use.

Here’s how you can see the CPU stats for all processors every 2 seconds for 5 intervals:

$ mpstat -P ALL 2 5

Using the nmon Command

You can think of the nmon command as your system’s monitoring powerhouse. It can show you CPU, memory, disk, network, top processes, etc.

Below is how you can use the command:

$ nmon

Graphical and Advanced Monitoring Tools

While command line tools are powerful, there are situations where you may prefer a more visual way to monitor your system. Let’s learn about a few options.

The GNOME System Monitor

If you use the GNOME desktop environment, you already have the system monitor tool installed. It offers you a graphical overview of your system resources (CPU, memory, disk, network) with graphs and charts.

Also, it allows you to view running processes and control them (start, stop, change priority).

Glances, Nagios, and Zabbix for in-depth Monitoring

You can use tools like Glances, Nagios, and Zabbix for larger-scale monitoring and more complex setups.

Glances offers you a sleek interactive system overview on your terminal. Although, you can also access it via a web interface.

Nagios and Zabbix, on the other hand, are powerful enterprise-grade solutions that provide in-depth monitoring, alerting, and historic data analysis.

Grafana with Prometheus

Prometheus is a powerful time-series database that you can use to collect and query metrics. Another tool called Grafana pairs perfectly with it, providing you customizable dashboards and visualizations.

The combination allows you to tailor your monitoring perfectly to your specific needs and create beautiful, informative dashboards.

You can learn how to setup Prometheus and Grafana on Ubuntu in our other article.

About The Author

Emmanuel Oyibo

Emmanuel Oyibo

Emmanuel is a Technical Writer and DevOps/Software Engineer with over 4 years of experience in streamlining technical documentation and automating workflows. He specializes in writing about Linux, Python, and DevOps concepts, utilizing his expertise to create clear guides and documentation. Emmanuel holds a B.Sc in Biochemistry and is passionate about the power of technology for problem-solving.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment