Why Proactive Monitoring Matters
Resource bottlenecks cause slow performance or crashes. Knowing how to identify the culprit (CPU, RAM, disk, network) allows targeted fixes. This guide covers htop (CPU/memory), iotop (disk I/O), netstat/iftop (network), and how to interpret them.
htop: Interactive Process Viewer
Install: sudo apt install htop. Run: htop. Columns: CPU% (process usage), MEM% (resident memory). Sort by CPU (F6). Identify high CPU processes (MySQL, PHP-FPM, Node). Check load average (top left: 1.25 0.90 0.75). If load > number of CPU cores, overloaded. Memory usage: bar graph, swap usage. Keys: F5 (tree view), F9 (kill process).
iotop: Disk I/O Monitoring
Install: sudo apt install iotop. Run: sudo iotop (needs root). Shows read/write rates per process. High I/O may indicate swap thrashing (see swap I/O) or database intensive queries. Columns: DISK READ, DISK WRITE. Sort by IO (left/right arrows). For MySQL, check if disk reads high (buffer pool too small).
netstat and iftop: Network Traffic
netstat -tunap: shows active connections (LISTEN, ESTABLISHED). Count connections per port: netstat -tn | grep :80 | wc -l. iftop: real-time bandwidth per connection: sudo apt install iftop, sudo iftop -i eth0. Shows which remote IPs consume bandwidth. Useful for detecting DDoS or unexpected outbound traffic.
Other Tools
dstat: all-in-one (CPU, disk, network, paging). vmstat 1: memory, swapping, CPU. sar (sysstat package): historical recording. nmon: interactive stats. bmon: bandwidth monitor. All available via apt.
Identifying Common Bottlenecks
High CPU but low I/O: CPU-bound app (PHP code, tight loops). Add more vCPU or optimize code. High I/O wait (wa in top): disk bottleneck. Check iotop for culprit (maybe swapping). High memory usage with swap I/O: need more RAM. High network traffic: check iftop for unexpected connections (DDoS).
Conclusion
Monitor htop, iotop, netstat regularly to catch issues early. Set up alerts (Prometheus + Grafana) for production. Know normal baselines to spot anomalies.