Problem Statement
Compare different system monitoring tools: top, htop, vmstat, iostat, and sar. When should you use each tool?
Explanation
Top provides real-time process monitoring and system resource overview, showing CPU usage, memory usage, and processes sorted by resource consumption. It's the most common tool for quick system checks and identifying resource hogs. Use top for interactive process monitoring, finding high CPU/memory processes, and getting overall system health snapshot.
Htop is an enhanced top with color coding, mouse support, horizontal/vertical scrolling, tree view of processes, and easier process management. It's more user-friendly but not always installed by default. Use htop when available for better visualization and easier process navigation, especially in complex process hierarchies.
Vmstat (virtual memory statistics) reports process, memory, paging, block I/O, traps, and CPU activity. Run vmstat 5 for updates every 5 seconds. Focus on columns: r (processes waiting for CPU), b (blocked processes), swpd (swap usage), si/so (swap in/out), bi/bo (blocks in/out), us/sy/id/wa (CPU time breakdown). Use vmstat for identifying memory pressure, swap activity, and CPU bottlenecks over time.
Iostat (I/O statistics) reports CPU and I/O statistics for devices and partitions. Run iostat -x 5 for extended statistics every 5 seconds. Key metrics: %util (device utilization), await (average wait time), svctm (service time). Use iostat for diagnosing disk I/O bottlenecks, identifying slow disks, and understanding I/O patterns. Sar (System Activity Reporter) collects and reports system activity over time, storing historical data. Use sar for analyzing performance trends, post-mortem analysis, and capacity planning. Each tool provides different perspective on system performance.
Practice Sets
This question appears in the following practice sets: