Table of Contents
- Key Performance Metrics to Monitor
- Command-Line Monitoring Tools
- Graphical Monitoring Tools
- Advanced Techniques for Performance Analysis
- Best Practices for Linux Performance Monitoring
- Conclusion
- References
Key Performance Metrics to Monitor
Before diving into tools, it’s critical to understand what to monitor. Linux performance hinges on four core resources: CPU, memory, disk, and network. Below are the key metrics for each:
CPU Utilization
The CPU is the “brain” of the system, executing instructions. Metrics to track:
- User CPU (%us): Time spent on user-space processes (e.g., applications).
- System CPU (%sy): Time spent on kernel-space processes (e.g., I/O, memory management).
- Idle CPU (%id): Time the CPU is unused. Low idle time (e.g., <10%) may indicate CPU saturation.
- Load Average: The average number of processes waiting for CPU time over 1, 5, and 15 minutes (e.g.,
0.8, 1.2, 0.9). A load average higher than the number of CPU cores suggests congestion. - Context Switches: The rate at which the kernel switches between processes. Frequent switches (e.g., >10k/sec) can increase overhead.
Memory Usage
RAM (random access memory) is where active data is stored for fast access. Metrics include:
- Used/Free Memory: Total RAM used by applications and the kernel.
- Buffers/Cache: Memory used by the kernel to cache disk data (temporarily freeable if needed).
- Swap Usage: Disk space used as “overflow” when RAM is full. High swap usage (e.g., >50%) indicates memory pressure.
- OOM (Out-of-Memory) Events: Occur when the kernel kills processes to free memory. Check logs (
dmesg | grep -i oom) for OOM kills.
Disk I/O Performance
Slow disk I/O can bottleneck even fast CPUs. Key metrics:
- I/O Operations Per Second (IOPS): Number of read/write operations (e.g., SSDs handle ~100k IOPS; HDDs ~100 IOPS).
- Throughput (MB/s): Data transferred per second (e.g., sequential reads on SSDs reach ~500 MB/s).
- Latency (ms): Time to complete an I/O request. High latency (>20ms) indicates slow disks or misconfigured storage.
- Disk Utilization (%util): Percentage of time the disk is busy. >80% utilization often causes queuing.
Network Throughput and Latency
Network issues can disrupt services (e.g., web servers, databases). Metrics to track:
- Bandwidth Usage (Mbps): Incoming (rx) and outgoing (tx) data rates.
- Packets Per Second (PPS): Number of packets transmitted/received (useful for detecting DDoS attacks).
- Errors/Drops: Corrupted packets (
rxerr,txerr) or dropped packets due to buffer overflow (rxdrop,txdrop). - TCP Connections: Active, established, or failed connections (e.g.,
ESTABLISHED,TIME_WAITstates).
Command-Line Monitoring Tools
Command-line tools are lightweight, scriptable, and available on nearly all Linux systems. Let’s explore the most essential ones.
Top and Htop: Real-Time Process Monitoring
Top is the oldest and most ubiquitous tool for real-time system monitoring. It displays CPU, memory, and process data in a dynamic terminal interface.
Example Output:
top - 14:30:00 up 2 days, 4:15, 2 users, load average: 0.85, 1.02, 0.98
Tasks: 230 total, 1 running, 229 sleeping, 0 stopped, 0 zombie
%Cpu(s): 12.3 us, 3.5 sy, 0.0 ni, 83.7 id, 0.0 wa, 0.0 hi, 0.5 si, 0.0 st
MiB Mem : 15988.4 total, 8923.1 free, 3245.2 used, 3820.1 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 12345.6 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 ubuntu 20 0 150000 50000 20000 R 25.0 0.3 2:30.45 python3
Key Columns:
%CPU: CPU usage per process.%MEM: Memory usage (RES = resident set size, actual RAM used).TIME+: Total CPU time consumed.
Htop is a modern alternative to top with a user-friendly interface, mouse support, and color-coded metrics. Install it via:
# Debian/Ubuntu
sudo apt install htop
# RHEL/CentOS
sudo yum install htop
Htop Features:
- Sort processes by CPU/memory with F6.
- Kill processes with F9.
- View CPU cores individually (press F2 to configure).
Vmstat: Virtual Memory Statistics
vmstat (virtual memory statistics) provides a high-level overview of CPU, memory, disk, and network activity.
Basic Usage:
vmstat 2 5 # Run every 2 seconds, 5 times total
Sample Output:
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 914520 12340 456780 0 0 12 45 567 1234 10 5 83 2 0
Key Fields:
r: Running processes (wait CPU).b: Blocked processes (wait I/O).si/so: Swap in/out (MB/s). Non-zero values indicate memory pressure.bi/bo: Disk read/write (blocks/s; 1 block = 512 bytes).wa: CPU time waiting for I/O (highwasuggests disk bottlenecks).
Iostat: Disk I/O Analysis
iostat (input/output statistics) focuses on disk performance, including IOPS, throughput, and latency. Install via sysstat package:
sudo apt install sysstat # Debian/Ubuntu
sudo yum install sysstat # RHEL/CentOS
Basic Usage:
iostat -x 2 # Detailed (-x) output every 2 seconds
Sample Output:
avg-cpu: %user %nice %system %iowait %steal %idle
8.50 0.00 3.20 1.80 0.00 86.50
Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util
sda 5.20 3.80 208.00 152.00 0.10 0.20 1.90 5.00 2.50 3.20 0.02 40.00 40.00 0.80 0.72
Key Fields:
r/s/w/s: Read/write IOPS.rkB/s/wkB/s: Read/write throughput (kB/s).r_await/w_await: Read/write latency (ms).%util: Disk utilization (avoid >80%).
Sar: System Activity Reporter
sar (system activity reporter) logs historical performance data, making it ideal for trend analysis. It’s part of sysstat and runs as a daemon (sadc) to collect data.
Basic Usage:
- View CPU usage for the past hour:
sar -u -f /var/log/sysstat/sa23(replacesa23with today’s log). - View memory usage:
sar -r 2 5(real-time, 2s intervals, 5 times).
Sample Output:
14:30:00 CPU %user %nice %system %iowait %steal %idle
14:30:02 all 9.80 0.00 4.20 1.50 0.00 83.00
Netstat and SS: Network Connections
netstat (network statistics) and ss (socket statistics) monitor network connections, ports, and protocols. ss is faster and more modern than netstat.
Netstat Example (list all TCP connections):
netstat -tuln # -t: TCP, -u: UDP, -l: listening, -n: numeric (no DNS)
SS Example (list established TCP connections):
ss -ti state established # -t: TCP, -i: info (e.g., cwnd, rtt)
Key Fields:
Local Address:Port: Service binding (e.g.,0.0.0.0:80= HTTP).State: Connection status (ESTABLISHED,LISTEN,TIME_WAIT).
Iftop: Network Bandwidth Monitoring
iftop (interface top) displays real-time bandwidth usage per network connection. Install via:
sudo apt install iftop # Debian/Ubuntu
sudo yum install iftop # RHEL/CentOS
Usage:
iftop -i eth0 # Monitor interface eth0
Output: A live table of connections sorted by bandwidth, with TX (transmit) and RX (receive) rates.
Graphical Monitoring Tools
Graphical tools simplify visualization, making them ideal for beginners or dashboards.
GNOME System Monitor
Preinstalled on GNOME desktops, this tool offers a GUI for CPU, memory, disk, and network metrics. Access via:
gnome-system-monitor
Features:
- Real-time graphs for resources.
- Process management (kill/renice processes).
- Disk and network tabs with per-interface stats.
Glances: A Modern, All-in-One Tool
Glances is a cross-platform (Linux/macOS/Windows) tool with a CLI and web-based GUI. It combines metrics from top, vmstat, iostat, and netstat into a single dashboard.
Installation:
pip install glances # Python-based
Usage:
- CLI:
glances - Web GUI:
glances -w(access viahttp://<IP>:61208).
Features:
- Color-coded alerts (green=normal, yellow=warning, red=critical).
- Supports remote monitoring (client-server mode).
Grafana + Prometheus: Enterprise-Grade Monitoring
For large-scale systems, Prometheus (time-series database) and Grafana (dashboard builder) provide powerful, customizable monitoring.
Workflow:
- Prometheus: Scrapes metrics from targets (e.g., Linux nodes via
node_exporter). - Grafana: Visualizes Prometheus data with dashboards (CPU, memory, disk, network).
Setup Steps:
- Install Prometheus and
node_exporter(exposes Linux metrics). - Configure Prometheus to scrape
node_exporter(port 9100). - Install Grafana, add Prometheus as a data source, and import a prebuilt dashboard (e.g., Node Exporter Full).
Advanced Techniques for Performance Analysis
Combining Tools for Deeper Insights
No single tool tells the whole story. For example:
- High CPU + Low I/O: Check
topfor CPU-heavy processes (e.g., a misbehaving Python script). - High I/O + Low CPU: Use
iostatto identify slow disks, theniotop(I/O-focusedtop) to find culprit processes.
Scripting and Automation
Automate monitoring with scripts. For example, a bash script to log CPU usage:
#!/bin/bash
while true; do
top -bn1 | grep "%Cpu" >> cpu_log.txt
sleep 60
done
Or use Python with psutil (cross-platform library):
import psutil
print(f"CPU Usage: {psutil.cpu_percent(interval=1)}%")
print(f"Memory Usage: {psutil.virtual_memory().percent}%")
Setting Up Alerts
Proactively detect issues with alerts:
- Prometheus Alertmanager: Triggers alerts (email, Slack) when metrics breach thresholds (e.g., CPU >90%).
- Nagios/Zabbix: Legacy tools for alerting on service availability and performance.
Best Practices for Linux Performance Monitoring
- Establish Baselines: Measure “normal” performance (e.g., idle CPU, average memory usage) to identify anomalies.
- Monitor Critical Metrics First: Focus on CPU load, memory swap, disk latency, and network errors.
- Automate: Use tools like Prometheus or Glances to avoid manual checks.
- Log Everything: Store metrics long-term (e.g., Prometheus retention) to analyze trends.
- Document and Iterate: Update dashboards and alerts as your system evolves.
Conclusion
Analyzing Linux performance is a blend of art and science. By mastering metrics (CPU, memory, disk, network) and tools (from top to Grafana), you can diagnose bottlenecks, optimize resources, and ensure system reliability. Start small—experiment with htop and iostat on a personal server—then scale to enterprise tools like Prometheus. Remember: proactive monitoring prevents downtime.
References
- Linux Man Pages (top, vmstat, iostat, etc.).
- Glances Documentation.
- Prometheus + Grafana Guide.
- Linux Performance Tuning Guide.
- Sysstat Tools (sar, iostat).