Overview and What You Will Learn
Linux resource monitoring is the habit of checking four resources in a fixed order (CPU, memory, disk I/O, network) and reading utilization, saturation and errors for each one. Done in order, it lets you name the bottleneck of a slow server in about five minutes instead of guessing.
By the end of this lab you will be able to:
- Read load average per CPU core and explain why it can be high while the CPUs are idle
- Use
free -hand trust theavailablecolumn instead offree - Spot swapping, OOM kills and memory pressure
- Diagnose disk bottlenecks with
iostat -x,pidstat -dandiotop - Find the processes and connections behind network load with
ss,nethogsandiftop - Run a repeatable 5-minute triage script and use PSI (pressure stall information) as a tie-breaker
Why This Matters in Production
A slow server almost always has one dominant bottleneck, and each bottleneck has a different fix. Adding CPU cores to a machine that is waiting on a saturated disk changes nothing. Imagine an on-call engineer at a streaming platform like Hotstar during a live match: latency climbs, dashboards show high load, and the instinct is to scale out. If the real cause is a log volume stuck at 100% disk utilization, every new node inherits the same problem. Engineers who diagnose correctly are the ones who have rehearsed these tools until the order of checks is automatic.
Core Principles
The most reliable framework is the USE method: for every resource, check Utilization (how busy it is), Saturation (how much work is queued) and Errors. The decision tree below applies it in the order that finds most problems fastest.
core count?} B -->|Yes| C{vmstat wa
above 10?} B -->|No| D{Available memory
low?} C -->|Yes| E[(Disk I/O bottleneck)] C -->|No| F[CPU saturation] D -->|Yes| G[Memory pressure] D -->|No| H[Check network
and the app] classDef start fill:#326CE5,color:#FFFFFF classDef check fill:#F1F5F9,color:#1E293B classDef bad fill:#EF4444,color:#FFFFFF class A start class B,C,D check class E,F,G,H bad
Two details make this tree accurate. First, on Linux the load average counts tasks that are running, runnable and also tasks in uninterruptible sleep (state D), which is usually waiting on disk. A high load with idle CPUs therefore points at I/O, not at CPU. Second, wa and st in vmstat tell you what the CPU is waiting on: wa is time spent waiting for I/O, and st is time stolen by the hypervisor on a cloud VM.
The memory fields are the most misread output in Linux monitoring:
| Field | Meaning |
|---|---|
total |
Physical RAM installed |
used |
Memory held by processes and the kernel |
free |
Memory that is completely unused |
buff/cache |
Disk cache the kernel can reclaim when needed |
available |
Realistic estimate of memory for new work (free plus reclaimable) |
$ free -h total used free shared buff/cache availableMem: 15Gi 4.2Gi 1.1Gi 312Mi 9.8Gi 10GiSwap: 2.0Gi 128Mi 1.9GiA server with 1.1 GiB free but 10 GiB available is healthy. Memory is only a problem when available approaches zero or the system starts swapping heavily.
Detailed Step-by-Step Practical Lab
How to Read Load Average Per CPU Core
Load average is the number of tasks wanting a CPU or waiting on I/O, averaged over 1, 5 and 15 minutes. Compare it with the number of cores: a load equal to the core count means the machine is fully used, and sustained load above it means work is queuing.
uptime# 10:23:45 up 15 days, load average: 1.23, 0.87, 0.65 # Core count to compare againstnproc # On a 4 core server: 1.00 = 25% busy, 4.00 = fully busy, 8.00 = 2x oversubscribed # Per-core CPU breakdown (sysstat package)mpstat -P ALL 1 3 # CPU and memory pressure at a glance, 5 one-second samplesvmstat 1 5# 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 0 1153000 83200 10280000 0 0 0 124 892 1823 12 3 84 1 0How to read the columns:
ris the run queue (tasks waiting for CPU). A value above the core count for several samples means CPU saturation.bis tasks blocked in uninterruptible sleep, normally waiting on I/O.siandsoare memory swapped in and out per second. Anything consistently above zero means swapping.usandsyare user and kernel CPU time.idbelow roughly 5 means the CPU is saturated.waconsistently above roughly 10 points to a disk (or network filesystem) bottleneck.stabove zero on a cloud VM means the hypervisor is taking CPU away from you.
Kernels from 4.20 onward also expose Pressure Stall Information, which reports how much time tasks actually stalled. It is the best tie-breaker when the numbers above disagree.
# "some" = at least one task stalled, "full" = all tasks stalledcat /proc/pressure/cpucat /proc/pressure/memorycat /proc/pressure/io# some avg10=0.00 avg60=0.00 avg300=0.00 total=0How to Check Memory Usage and Swap Pressure on Linux
Check available memory first, then swap activity, then the kernel log for OOM kills. Memory is only under pressure when available is low, swap is being used actively (si and so above zero in vmstat), or the OOM killer has fired.
# Human readable summaryfree -h # Refresh every 2 secondswatch -n2 free -h # Key kernel counters (MemAvailable is the one that matters)grep -E 'MemTotal|MemFree|MemAvailable|Buffers|^Cached|SwapTotal|SwapFree' /proc/meminfo # Has the OOM killer fired? (needs root when dmesg_restrict is on)sudo dmesg -T | grep -i -E 'out of memory|killed process' | tail -10# Out of memory: Killed process 2341 (node) total-vm:1048576kB, anon-rss:512000kB # Same question via the journal (kernel messages, persisted across reboots if journald stores them)journalctl -k -g 'out of memory' --no-pager | tail -10 # Top memory consumers (RSS in KiB)ps -eo pid,user,rss,comm --sort=-rss | head -10 # Resident memory of every node processps -o pid,rss,args -C nodeRememberRSS counts shared pages in every process that maps them, so adding up RSS across processes overstates real usage. Use
freefor the system total and RSS only to rank suspects.
How to Find Which Process Is Using Disk I/O
Start with iostat -x to find the busy device, then pidstat -d or iotop to find the process. The key signals are await (how long each request takes) and the queue depth, with %util as a supporting signal.
# Install the tools on Debian or Ubuntusudo apt install sysstat iotop # Extended device statistics, 5 one-second samples (the first sample is the average since boot)iostat -x 1 5# Device r/s w/s rkB/s wkB/s r_await w_await aqu-sz %util# xvda 2.34 15.67 18.70 45.20 1.10 4.23 0.07 12.5# xvdb 0.12 124.56 0.90 456.20 0.90 45.67 5.69 98.1 <- problemHow to read it (the numbers above are illustrative):
r_awaitandw_awaitare average request latency in milliseconds. Rising latency under steady load means the device is saturated or failing.aqu-szis the average queue length. A growing queue means requests arrive faster than the device serves them.%utilnear 100 means the device was busy almost all the time. It is a reliable saturation signal for a single spinning disk, but NVMe and cloud volumes serve many requests in parallel, so check latency and queue depth as well.- Older
sysstatversions print a singleawaitcolumn instead ofr_awaitandw_await.
# Per-process disk throughput, one line per process, 5 samplespidstat -d 1 5 # Interactive view of only the processes doing I/O right now (needs root)sudo iotop -oP # Non-interactive: accumulate I/O for 3 samples, 2 seconds apartsudo iotop -oPab -n 3 -d 2 # Disk space and inode exhaustion are separate from I/O loaddf -hdf -i # Where did the space go? (-x stays on one filesystem)sudo du -xh --max-depth=1 /var | sort -rh | head -10How to Monitor Network Usage and Connections
Check connection counts and socket state first, then interface errors, then bandwidth per interface, process and connection. Most network slowness on a server is connection exhaustion, retransmits or a saturated link, and each has its own command.
# Listening sockets and the process that owns each onesudo ss -tulpn # One-line summary of all socketsss -s # Count TCP connections by state (skip the header line)ss -tan | awk 'NR>1 {print $1}' | sort | uniq -c | sort -rn # Established connections to PostgreSQLss -t state established '( dport = :5432 or sport = :5432 )' # Interface counters: look for errors, drops and overrunsip -s link show eth0 # TCP retransmits since boot (a rising number means loss or congestion)nstat -az TcpRetransSegs # Bandwidth per interface, per process and per connectionsudo apt install nload nethogs iftopnload eth0sudo nethogs eth0sudo iftop -i eth0How to Use dstat or dool for All-in-One Monitoring
dstat prints CPU, disk, network, paging and system stats in one scrolling line per second, so you can see which resource spikes together. Many current distributions have replaced it with dool, a maintained fork that accepts the same flags, or ship it through the pcp package.
# Install whichever your distribution providessudo apt install dstat # Time, CPU, disk, network, paging, system and memory every seconddstat -tcdngym 1 # Same view with the maintained forkdool -tcdngym 1 # Example during an I/O spike# ----system---- ----total-cpu-usage---- -dsk/total- -net/total- ---paging--# time |usr sys idl wai stl| read writ| recv send| in out# 14-10 10:41:07| 12 3 20 65 0| 450M 120M|8.2k 123k| 0 0# ^ 65% wait = severe I/O bottleneckFor history instead of live data, enable the sysstat collector and read it back with sar:
# CPU, memory and disk history from todaysar -usar -rsar -d -pHow to Run a 5-Minute Production Diagnosis
Put the whole triage in one script so the first minute of every incident is the same command. The script below reports every resource in USE order and skips tools that are not installed.
# diagnose: first-pass triage for a slow Linux serverset -u section() { printf '\n=== %s ===\n' "$1"; }have() { command -v "$1" >/dev/null 2>&1; } section "Time and load ($(hostname))"dateuptimeecho "cores: $(nproc)" section "CPU, run queue and swap (vmstat)"vmstat 1 3 section "Pressure stall info"for r in cpu memory io; do echo "-- $r" cat /proc/pressure/$r 2>/dev/null || echo "PSI not available"done section "Memory"free -h section "Top CPU processes"ps -eo pid,user,pcpu,pmem,comm --sort=-pcpu | head -6 section "Top memory processes"ps -eo pid,user,pcpu,pmem,rss,comm --sort=-rss | head -6 section "Disk I/O"if have iostat; then iostat -x 1 2 | tail -n +4; else echo "iostat missing (apt install sysstat)"; fi section "Disk space and inodes"df -hT -x tmpfs -x devtmpfsdf -i -x tmpfs -x devtmpfs section "TCP connections by state"ss -tan | awk 'NR>1 {print $1}' | sort | uniq -c | sort -rn section "Recent OOM events"dmesg -T 2>/dev/null | grep -i -E 'out of memory|killed process' | tail -5 section "Failed services"systemctl list-units --state=failed --no-legend# Install it system-wide and run itsudo install -m 0755 diagnose.sh /usr/local/bin/diagnosesudo diagnoseProduction Best Practices & Common Pitfalls
- Monitor
available, notfree. Lowfreememory with a large cache is normal and healthy. - Record a baseline. Capture
saror your metrics system on a quiet day, because "load 6" means nothing without knowing what normal looks like. - Alert on saturation, not only utilization. Run queue length, I/O latency and PSI catch problems that a CPU percentage hides.
- Check disk space and inodes together. A full filesystem and an exhausted inode table both show up as "cannot write", but only
df -ireveals the second. - Remember
vmstatandiostatfirst lines. The first sample is an average since boot, so ignore it when diagnosing a current problem. - Collect evidence before restarting. Save the
diagnoseoutput first, because a restart often erases the symptom you are chasing.
Common MistakeTreating a high load average as a CPU problem. Load includes tasks stuck waiting on disk, so confirm with
vmstat(wa,b) andiostat -xbefore adding CPU.
TipCompare
cat /proc/pressure/iobefore and after a change. Ifsome avg10drops, you fixed the real bottleneck, whatever the utilization graphs said.
| Symptom | First check | What to look for |
|---|---|---|
| Slow, load high, CPU busy | vmstat 1 5 |
High us or sy, r above core count |
| Slow, load high, CPU idle | vmstat 1 5 |
High wa and b, then iostat -x |
| Out of memory errors | free -h |
available near zero, OOM entries in dmesg |
| Sudden slowness on a VM | vmstat 1 5 |
Non-zero st (noisy neighbour) |
| App cannot write files | df -h |
Filesystem at 100% |
| App cannot create files | df -i |
Inodes at 100% |
| Many timeouts | ss -s |
Huge counts in SYN-RECV, TIME-WAIT or CLOSE-WAIT |
Quick Reference & Troubleshooting Commands
| Task | Command |
|---|---|
| Load average | uptime |
| CPU breakdown | vmstat 1 5 |
| Per-core CPU | mpstat -P ALL 1 3 |
| Memory summary | free -h |
| Pressure stall info | cat /proc/pressure/io |
| Disk device stats | iostat -x 1 3 |
| Per-process disk I/O | pidstat -d 1 5 |
| Live I/O by process | sudo iotop -oP |
| Disk space and inodes | df -h and df -i |
| Open ports and owners | sudo ss -tulpn |
| Socket summary | ss -s |
| Interface errors | ip -s link show eth0 |
| All-in-one live view | dstat -tcdngym 1 |
| OOM history | journalctl -k -g 'out of memory' |