Back to Blog

Essential monitoring: CPU, memory, disk I/O and network

Complete tutorial on top/htop/iotop in Ubuntu. Learn htop views, iotop sampling, iftop bandwidth, nmon tips.

Essential monitoring: CPU, memory, disk I/O and network

Essential Monitoring: CPU, Memory, Disk I/O, and Network

Monitoring system performance is a critical aspect of managing Linux servers and ensuring optimal operation. In a production environment, understanding resource usage, identifying bottlenecks, and ensuring system stability can significantly impact application performance and user experience. This tutorial will cover essential tools like top, htop, iotop, iftop, and nmon to monitor CPU, memory, disk I/O, and network performance on Ubuntu systems.

This guide aims to provide you with practical knowledge about these tools, their use cases, and how to interpret their output. By mastering these monitoring utilities, you will be better equipped to troubleshoot performance issues and optimize resource utilization effectively.

Prerequisites

  • Ubuntu Version: This tutorial is applicable for Ubuntu 18.04 and above.

  • Required Packages: Ensure you have the following installed:

    • htop
    • iotop
    • iftop
    • nmon

    You can install them using the command:

    sudo apt install htop iotop iftop nmon
    
  • Permissions: You will need sudo privileges to install packages and monitor certain system metrics.

  • Risks: Monitoring tools typically have minimal impact on performance; however, running them on heavily-loaded systems may introduce some overhead.

Core Concepts

Terminology

  • CPU Usage: Refers to the percentage of CPU capacity utilized by processes.
  • Memory Usage: Indicates how much of the available RAM is in use.
  • Disk I/O: Measures the read and write operations on disk drives.
  • Network I/O: The amount of data being sent and received over the network.

Architecture

Monitoring tools can provide a real-time view of system resources, helping in diagnosing performance issues. They gather metrics from various subsystems of the operating system and present them in a user-friendly format.

When to Use

  • htop: Use it for an interactive view of CPU and memory usage.
  • iotop: Ideal for monitoring disk I/O by processes.
  • iftop: Best for analyzing network bandwidth usage in real time.
  • nmon: A comprehensive tool for monitoring various resources including CPU, memory, disk I/O, and network.

Limits

While these tools provide valuable insights, they may not capture all historical data, relying instead on real-time metrics. For long-term monitoring, consider dedicated solutions like Grafana or Nagios.

Syntax/Commands

Command Description Flags/Parameters
htop Interactive process viewer -d <delay> (set refresh delay)
iotop Display I/O usage by processes -o (only show processes doing I/O)
iftop Display bandwidth usage on network interfaces -i <interface> (specify interface)
nmon Monitor CPU, memory, disks, and network -t (top view), -M (memory)

Practical Examples

1. Monitoring CPU and Memory with htop

htop
  • Launch htop for an interactive display of CPU and memory usage.
  • Use the F6 key to sort by different criteria, like CPU or memory usage.

2. Customizing htop Display

htop -d 5
  • Set the refresh rate to 5 seconds for a less frequent update.

3. Monitoring Disk I/O with iotop

sudo iotop
  • Open iotop to see real-time disk I/O by processes.

4. Displaying Only Processes Doing I/O

sudo iotop -o
  • Use the -o flag to filter and display only processes that are currently performing I/O operations.

5. Monitoring Network Bandwidth with iftop

sudo iftop -i eth0
  • Replace eth0 with your actual network interface to view real-time bandwidth usage.

6. Filtering Network Traffic in iftop

sudo iftop -i eth0 -F 192.168.1.0/24
  • Monitor only traffic to and from the specified subnet.

7. Comprehensive Monitoring with nmon

nmon
  • Launch nmon and use the keyboard shortcuts to toggle different metrics (e.g., c for CPU, m for memory).

8. Saving nmon Output for Analysis

nmon -f -s 60 -c 120
  • Save output to a file every 60 seconds for 120 iterations. Analyze later with nmon2csv.

Real-World Scenarios

Scenario 1: Diagnosing High CPU Usage

You notice that your web server is responding slowly. Launch htop to identify the processes consuming the most CPU. Once identified, you can decide to optimize the application or allocate more resources.

Scenario 2: Monitoring Disk Performance

After deploying a new database, you experience slow read/write operations. Use iotop to monitor disk I/O in real time and determine if a specific process is causing the bottleneck.

Scenario 3: Analyzing Network Traffic

When users report intermittent connectivity issues, iftop can help identify if a particular process is consuming excessive bandwidth, allowing you to take appropriate action.

Best Practices

  1. Regular Monitoring: Make it a habit to monitor system metrics regularly to catch issues before they escalate.
  2. Automate Alerts: Use tools like Nagios or Prometheus to automate monitoring and alerting based on thresholds.
  3. Optimize Configurations: Based on insights gained from monitoring, periodically review and optimize system configurations.
  4. Use Logging: Enable logging for monitoring tools to analyze performance trends over time.
  5. Limit Resource Consumption: Use nice and ionice commands to prioritize critical processes while using monitoring tools.

Common Errors

Error 1: Permission denied

Cause: You are trying to run iotop or iftop without sudo.

Fix: Run the command with sudo.

Error 2: Command not found

Cause: The required package is not installed.

Fix: Install the package using:

sudo apt install <package_name>

Error 3: Unable to open display

Cause: Running htop in a non-GUI session.

Fix: Ensure you are in a terminal that supports graphical applications or use a terminal multiplexer like tmux.

Error 4: No such device

Cause: Specified network interface does not exist.

Fix: Check available interfaces using:

ip link show

Related Commands

Command Description
top Basic process monitoring.
vmstat Reports on virtual memory statistics.
mpstat Reports CPU usage by individual cores.
sar Collects, reports, or saves system activity information.

Automation Script

Here is a simple script to monitor CPU and memory usage and log the output to a file.

#!/bin/bash

# Script to log CPU and Memory usage every minute

LOGFILE="/var/log/sys_monitor.log"

# Function to log metrics
log_metrics() {
    echo "----- $(date) -----" >> $LOGFILE
    echo "CPU Usage:" >> $LOGFILE
    mpstat 1 1 | tail -n +4 >> $LOGFILE
    echo "Memory Usage:" >> $LOGFILE
    free -h >> $LOGFILE
    echo "" >> $LOGFILE
}

# Main loop to log every minute
while true; do
    log_metrics
    sleep 60
done

Instructions for Use

  1. Save the script as monitor.sh.
  2. Make it executable:
    chmod +x monitor.sh
    
  3. Run the script with sudo to allow access to system metrics:
    sudo ./monitor.sh
    

Conclusion

Monitoring system performance is essential for maintaining optimal operation in Linux environments. Tools like htop, iotop, iftop, and nmon provide real-time insights into CPU, memory, disk I/O, and network usage. By leveraging these tools effectively, you can identify bottlenecks, optimize resource utilization, and maintain system stability.

As a next step, consider setting up automated monitoring solutions and learning about more advanced metrics collection tools like Prometheus and Grafana.

References