Skip to content

33. System Monitoring

This chapter covers the essential command-line telemetry and diagnostic tools for real-time monitoring and historical performance analysis.

33.1 System Health at a Glance: uptime & Load Average

Section titled “33.1 System Health at a Glance: uptime & Load Average”

The uptime command gives the quickest initial pulse check of a Linux system:

Terminal window
uptime

Example output:

16:10:05 up 42 days, 3:14, 2 users, load average: 1.15, 0.85, 0.42

The three numbers represent the average number of processes requesting CPU time (running or waiting in the run queue) or blocked waiting for uninterruptible disk I/O over the last 1 minute, 5 minutes, and 15 minutes.

Load average must always be evaluated relative to the total number of CPU cores:

  • Check available cores: nproc or lscpu | grep "CPU(s):"
  • Load < CPU Cores: The system has idle capacity.
  • Load == CPU Cores: The system is at 100% capacity with no queueing.
  • Load > CPU Cores: Processes are queuing up and waiting for resources.
  • Trend Analysis: If the 1-min load is significantly higher than the 15-min load (4.50, 1.20, 0.30), load is rapidly spiking. If 1-min is lower (0.20, 1.50, 3.80), the system is cooling down.

[!WARNING] High load average does not always mean high CPU utilization! If processes are stuck waiting on slow or saturated storage (Disk I/O Wait), load average will spike while CPU utilization remains near 0%.

33.2 Real-Time Process & CPU Monitoring: top & htop

Section titled “33.2 Real-Time Process & CPU Monitoring: top & htop”

The universal process monitor present on every Linux machine.

Terminal window
top
  • %Cpu(s):
    • us (User): Time spent running unprivileged user processes.
    • sy (System): Time spent running kernel routines and system calls.
    • ni (Nice): Time spent running low-priority processes.
    • id (Idle): Unused CPU percentage (higher is healthier).
    • wa (I/O Wait): Time the CPU spent waiting for disk reads/writes. A high wa (> 10-20%) indicates a storage bottleneck!
    • si / so / st: Software interrupts / Hardware interrupts / Steal time (CPU stolen by hypervisor on virtual machines).
  • P: Sort processes by CPU usage.
  • M: Sort processes by Memory (RES) usage.
  • T: Sort processes by running Time.
  • k: Kill a process (prompts for PID and signal, default 15).
  • 1: Toggle display of individual CPU cores.
  • q: Quit.

An enhanced, colorful, and interactive alternative to top (install via apt install htop or dnf install htop).

  • Supports mouse scrolling and clicking.
  • Shows per-core visual gauges for CPU, Memory, and Swap.
  • Tree view (F5) shows parent/child process relationships (PPID).
  • Filter (F4) and search (F3) processes by name instantly.

Provides an instant snapshot of RAM and Swap:

Terminal window
free -h

Example:

total used free shared buff/cache available
Mem: 15Gi 3.2Gi 1.1Gi 240Mi 11Gi 11Gi
Swap: 4.0Gi 120Mi 3.9Gi
  • free vs available: free is unused RAM. available is the actual memory available for new applications without swapping, as it includes reclaimable buff/cache.
  • Low free is normal: The Linux kernel aggressively uses spare memory for file caching.

Reports summary information about processes, memory, paging, block I/O, and CPU activity over a sampling interval:

Terminal window
# Sample every 2 seconds, 5 times
vmstat 2 5

Example output:

procs --memory- swap-- --io- -system-- cpu--
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 122880 115340 184520 11520448 0 0 12 45 150 240 4 2 94 0 0
  • procs -> r (Run Queue): Number of processes runnable/waiting. If consistently higher than core count, CPU is saturated.
  • procs -> b (Blocked): Processes in uninterruptible sleep (usually waiting for disk I/O).
  • swap -> si / so (Swap-In / Swap-Out): Non-zero values here mean memory is critically low and the machine is constantly reading/writing to swap disk (Thrashing).
  • io -> bi / bo (Blocks In / Blocks Out): Raw disk read/write throughput in blocks per second.

iostat (part of the sysstat package) is the primary tool to identify whether storage drives are choking your system.

Terminal window
# Install sysstat if missing
sudo apt install sysstat -y # or dnf install sysstat
# Monitor extended disk stats (-x) in megabytes (-m) every 2 seconds
iostat -x -m 2

Key columns to inspect:

  • r/s and w/s: Read and write IOPS (I/O requests per second).
  • rMB/s and wMB/s: Throughput in Megabytes per second.
  • await (Average Wait Time): Average time (in milliseconds) for I/O requests to be serviced. NVMe SSDs should be < 1ms; SATA SSDs < 5ms. If await exceeds 20-50ms, disk is overloaded.
  • %util (% Utilization): The percentage of time the storage device was busy handling requests. If %util approaches 100%, the disk is completely saturated!

While top and iostat show real-time metrics, sar (System Activity Reporter) records historical performance metrics 24/7 in the background via a cron/systemd service.

When a server crashes at 2 AM, sar allows you to inspect what happened retroactively.

Terminal window
# Enable sysstat background collector
sudo systemctl enable --now sysstat
# View CPU utilization throughout the day (every 10 minutes)
sar -u
# View CPU utilization for a specific day in the past (e.g., the 2nd of the month)
sar -u -f /var/log/sysstat/sa02
# View historical Memory & Swap usage
sar -r
# View historical Network traffic (packets/sec, bytes/sec per interface)
sar -n DEV
# View historical Disk I/O
sar -d -p

33.6 Network Sockets & Diagnostic Logs: ss & dmesg

Section titled “33.6 Network Sockets & Diagnostic Logs: ss & dmesg”

Monitor socket states, queue sizes, and port bindings:

Terminal window
# Check listen queues (Send-Q vs Recv-Q)
ss -lnt
# Summary of total active socket connections
ss -s

If Recv-Q is full on a listening port, your application’s connection backlog is saturated and it is dropping incoming user requests.

The kernel prints hardware errors, storage drops, driver crashes, and OOM killer invocations to the ring buffer:

Terminal window
# View kernel logs with human-readable timestamps
sudo dmesg -T | tail -n 50
# Check if any process was killed by Out-Of-Memory Killer
sudo dmesg -T | grep -i -E "oom|out of memory|killed process"
# Check for disk or filesystem errors
sudo dmesg -T | grep -i -E "error|fail|sda|nvme"

33.7 The DevOps Troubleshooting Metric Matrix

Section titled “33.7 The DevOps Troubleshooting Metric Matrix”

| Symptom | Primary Tool | Key Metric to Inspect | Root Cause | ||||| | High Load, Low CPU % | top / vmstat | wa (IO wait) > 15%, b > 0 | Saturated disk or unresponsive network share | | High Load, High CPU % | top / htop | us or sy near 100% | Runaway application loop or kernel context switching | | Sluggish System, High Disk Activity | vmstat / free | so > 0, si > 0, swap usage high | System out of RAM, thrashing on swap file | | Disk Latency / Database Timeouts | iostat -x | %util near 100%, await > 20ms | Storage hardware throughput/IOPS ceiling hit | | Dropped TCP Connections | ss -lnt | Recv-Q > 0 on port | Application event loop blocked or worker pool full | | Random Container / Process Death | dmesg -T | Out of memory: Killed process | Kernel OOM killer invoked due to memory exhaustion |