Chapter 23: System Monitoring

In this chapter, we’ll explore system monitoring tools that help you analyze and optimize your Linux system’s resource usage. You’ll learn how to use tools like htop, btop, vmstat, and iostat to monitor CPU, memory, disk, and network activity. By the end of this chapter, you’ll be able to identify performance bottlenecks and keep your system running smoothly.


1. Why Monitor System Performance?

Monitoring system performance is essential for: - Identifying resource bottlenecks (e.g., high CPU or memory usage). - Diagnosing slow applications or services. - Optimizing resource allocation for better efficiency.


2. Interactive Process Monitoring with htop

htop is an interactive process viewer that provides a real-time overview of system resource usage.

Installation

$ sudo apt install htop  # Debian/Ubuntu
$ sudo dnf install htop  # Red Hat/Fedora

Basic Usage

  • Start htop: $ htop
  • Key Features:
  • CPU and Memory Usage: Displayed at the top of the screen.
  • Process List: Shows running processes with details like PID, user, and resource usage.
  • Sorting: Press F6 to sort processes by CPU, memory, or other criteria.
  • Kill a Process: Select a process and press F9 to send a signal (e.g., SIGKILL).

Example

Monitor processes and identify resource hogs:

$ htop

3. Advanced Monitoring with btop

btop is a modern, feature-rich system monitor with a sleek interface.

Installation

$ sudo apt install btop  # Debian/Ubuntu
$ sudo dnf install btop  # Red Hat/Fedora

Basic Usage

  • Start btop: $ btop
  • Key Features:
  • CPU, Memory, and Disk Usage: Displayed in real-time.
  • Network Activity: Shows incoming and outgoing traffic.
  • Customizable Interface: Press M to toggle menus and customize the display.

Example

Monitor system resources with a modern interface:

$ btop

4. System Statistics with vmstat

vmstat (Virtual Memory Statistics) provides a snapshot of system performance, including CPU, memory, and I/O activity.

Basic Usage

  • Display a summary of system activity: $ vmstat
  • Key Columns:
  • r: Number of processes waiting for CPU.
  • b: Number of processes in uninterruptible sleep.
  • swpd: Amount of swap memory used.
  • free: Amount of free memory.

si/so: Swap in/out (memory paging activity).

Monitor Continuously:$ vmstat 1 # Update every 1 second

Example

Monitor CPU and memory usage in real-time:

$ vmstat 1

5. Disk I/O Monitoring with iostat

iostat reports CPU and disk I/O statistics.

Installation

$ sudo apt install sysstat  # Debian/Ubuntu
$ sudo dnf install sysstat  # Red Hat/Fedora

Basic Usage

  • Display CPU and disk statistics: $ iostat
  • Key Columns:
  • %user: CPU usage for user processes.
  • %idle: CPU idle time.
  • tps: Transactions per second (disk I/O).
  • kB_read/s: Kilobytes read per second.

kB_wrtn/s: Kilobytes written per second.

Monitor Continuously:$ iostat 1 # Update every 1 second

Example

Monitor disk I/O activity:

$ iostat 1

6. Network Monitoring with nload

nload is a real-time network traffic monitor.

Installation

$ sudo apt install nload  # Debian/Ubuntu
$ sudo dnf install nload  # Red Hat/Fedora

Basic Usage

  • Start nload: $ nload
  • Key Features:
  • Incoming and Outgoing Traffic: Displayed in real-time.
  • Bandwidth Usage: Shows current, average, and peak usage.

Example

Monitor network traffic:

$ nload

7. Practical Examples

Identify High CPU Usage

Use htop to identify processes consuming the most CPU:

$ htop

Monitor Disk I/O

Use iostat to monitor disk activity:

$ iostat 1

Check Memory Usage

Use vmstat to check memory and swap usage:

$ vmstat 1

Monitor Network Traffic

Use nload to monitor incoming and outgoing traffic:

$ nload

Modern system monitoring extends beyond local commands. Prometheus collects metrics from system endpoints, storing them in a time-series database, while Grafana visualizes these metrics in dynamic dashboards. 

For log management, journalctl and logrotate help maintain system logs, rotating them on a schedule to prevent storage issues. By integrating these tools, you gain comprehensive insights into system health, enabling proactive issue resolution and a deeper understanding of professional monitoring workflows.


Practice Time!

Let’s put your new skills to the test: 
1. Use htop to identify the top CPU-consuming processes. 
2. Use vmstat to monitor memory and swap usage in real-time. 
3. Use iostat to check disk I/O activity. 
4. Use nload to monitor network traffic.


That’s it for Chapter 23! You’ve now learned how to monitor system performance using tools like htop, btop, vmstat, and iostat. In the next chapter, we’ll dive into networking in Linux—how to configure and troubleshoot network settings using tools like ifconfig, ip, and netstat. Until then, practice monitoring your system to become more comfortable with these tools.


Prev: Chapter 22 | Next: Chapter 24