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:
uptimeExample output:
16:10:05 up 42 days, 3:14, 2 users, load average: 1.15, 0.85, 0.42Understanding Load Average
Section titled “Understanding Load Average”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.
How to Interpret the Numbers
Section titled “How to Interpret the Numbers”Load average must always be evaluated relative to the total number of CPU cores:
- Check available cores:
nprocorlscpu | 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”1. top
Section titled “1. top”The universal process monitor present on every Linux machine.
topKey top Header Metrics:
Section titled “Key top Header Metrics:”%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 highwa(> 10-20%) indicates a storage bottleneck!si/so/st: Software interrupts / Hardware interrupts / Steal time (CPU stolen by hypervisor on virtual machines).
Essential top Interactive Shortcuts:
Section titled “Essential top Interactive Shortcuts:”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.
2. htop
Section titled “2. htop”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.
33.3 Memory Telemetry: free & vmstat
Section titled “33.3 Memory Telemetry: free & vmstat”1. free -h
Section titled “1. free -h”Provides an instant snapshot of RAM and Swap:
free -hExample:
total used free shared buff/cache availableMem: 15Gi 3.2Gi 1.1Gi 240Mi 11Gi 11GiSwap: 4.0Gi 120Mi 3.9Gifreevsavailable:freeis unused RAM.availableis the actual memory available for new applications without swapping, as it includes reclaimablebuff/cache.- Low
freeis normal: The Linux kernel aggressively uses spare memory for file caching.
2. vmstat (Virtual Memory Statistics)
Section titled “2. vmstat (Virtual Memory Statistics)”Reports summary information about processes, memory, paging, block I/O, and CPU activity over a sampling interval:
# Sample every 2 seconds, 5 timesvmstat 2 5Example 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 0What to Look For:
Section titled “What to Look For:”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.
33.4 Storage & Disk I/O: iostat
Section titled “33.4 Storage & Disk I/O: iostat”iostat (part of the sysstat package) is the primary tool to identify whether storage drives are choking your system.
# Install sysstat if missingsudo apt install sysstat -y # or dnf install sysstat
# Monitor extended disk stats (-x) in megabytes (-m) every 2 secondsiostat -x -m 2Key columns to inspect:
r/sandw/s: Read and write IOPS (I/O requests per second).rMB/sandwMB/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. Ifawaitexceeds 20-50ms, disk is overloaded.%util(% Utilization): The percentage of time the storage device was busy handling requests. If%utilapproaches 100%, the disk is completely saturated!
33.5 Historical Performance Tracking: sar
Section titled “33.5 Historical Performance Tracking: sar”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.
# Enable sysstat background collectorsudo 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 usagesar -r
# View historical Network traffic (packets/sec, bytes/sec per interface)sar -n DEV
# View historical Disk I/Osar -d -p33.6 Network Sockets & Diagnostic Logs: ss & dmesg
Section titled “33.6 Network Sockets & Diagnostic Logs: ss & dmesg”1. ss (Socket Statistics)
Section titled “1. ss (Socket Statistics)”Monitor socket states, queue sizes, and port bindings:
# Check listen queues (Send-Q vs Recv-Q)ss -lnt
# Summary of total active socket connectionsss -sIf Recv-Q is full on a listening port, your application’s connection backlog is saturated and it is dropping incoming user requests.
2. dmesg (Kernel Ring Buffer)
Section titled “2. dmesg (Kernel Ring Buffer)”The kernel prints hardware errors, storage drops, driver crashes, and OOM killer invocations to the ring buffer:
# View kernel logs with human-readable timestampssudo dmesg -T | tail -n 50
# Check if any process was killed by Out-Of-Memory Killersudo dmesg -T | grep -i -E "oom|out of memory|killed process"
# Check for disk or filesystem errorssudo 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 |