Monitoring Your Linux System with htop, atop, and glances
In production environments, development machines, or home servers, keeping an eye on resource consumption is vital. Without proper monitoring, CPU thrashing, memory exhaustion, or I/O bottlenecks may impair performance or even cause outages. In this article, we explore three powerful command-line utilities—htop, atop, and glances—to help you maintain control over your Linux systems.
Why Real-Time Monitoring Matters
- Proactive Issue Detection: Spot surges in CPU, memory, disk or network utilization before they escalate.
- Capacity Planning: Evaluate trends over time to inform hardware upgrades or scaling decisions.
- Performance Tuning: Identify runaway processes, memory leaks, or I/O hogs for targeted optimization.
- Security: Unexpected resource usage spikes may indicate intrusion or misbehaving software.
Overview of Tools
- htop: A user-friendly, colorful, interactive process viewer.
- atop: A more comprehensive system and process monitor that logs snapshots for historical analysis.
- glances: A cross-platform, Python-based tool providing an overview of CPU, memory, disk, network, and more in a single screen.
Installation
Debian/Ubuntu
- htop:
sudo apt install htop - atop:
sudo apt install atop - glances:
sudo apt install glances(orpip3 install glancesfor latest version)
RHEL/CentOS/Fedora
- htop:
yum install htopordnf install htop - atop:
yum install atopordnf install atop - glances:
pip3 install glances
htop in Depth
htop enhances the classic top utility with mouse support, color coding, and tree-view of process hierarchies.
Key Features
- Interactive sorting: Press
F6to sort by CPU, memory, or other columns. - Process tree: Toggle with
F5to visualize parent/child relationships. - Process management: Kill (
F9), renice (F7/F8) directly. - Customizable display: Users may add or remove columns from the setup menu (
F2).
Example Usage
htop --sort-key=PERCENT_CPU
atop in Depth
atop not only shows real-time metrics but also writes detailed logs to /var/log/atop/. This allows post-mortem analysis of past system states.
Key Features
- Comprehensive metrics: CPU, memory, disk latency, network bandwidth, even per-process I/O rates.
- Logging capability: Automatic snapshot every 10 minutes by default.
- Historical review:
atop -r /var/log/atop/atop_YYYYMMDDto replay logs. - Threshold alerts: Highlighting resources exceeding configurable limits.
Example Usage
sudo atop -w /tmp/atop.log 60 5
This writes five samples at 60-second intervals to /tmp/atop.log.
glances in Depth
glances provides a consolidated overview of system health. Its dashboard adapts to terminal width, showing only the most relevant stats.
Key Features
- Modular display: CPU, load, memory, swap, disk I/O, network I/O, file system, sensors, processes.
- API and web server mode:
glances -wlaunches a web interface. - Plugins: Extend glances with Python plugins for custom metrics.
- Alerts: Set warning/critical levels for resources.
Example Usage
glances --disable-plugin docker
Disables the Docker plugin for a leaner display.
Feature Comparison
| Feature | htop | atop | glances |
|---|---|---|---|
| Real-time UI | Yes | Yes | Yes |
| Logging | No | Yes | Limited (via external scripts) |
| Web UI/API | No | No | Yes |
| Plugin support | No | No | Yes |
Securing Remote Monitoring
When administering servers remotely, exposing monitoring tools over the internet can be risky. To encrypt traffic and limit access:
- Establish a VPN tunnel using OpenVPN to your network.
- Alternatively, adopt WireGuard for a lightweight, performant VPN.
- Restrict monitoring endpoints to VPN IP ranges via firewall rules (iptables, nftables).
Best Practices and Tips
- Automate log rotation for
atoplogs to conserve disk space (logrotateconfiguration). - Combine tools: Use htop for quick, interactive process checks atop for historical forensic analysis and glances for a holistic overview or web-based dashboard.
- Embed alerts into your existing monitoring stack (Nagios, Zabbix, Prometheus) by exporting metrics from glances API.
- Customize refresh intervals: Lower values (1–2 seconds) for interactive troubleshooting higher values (30–60 seconds) for long-term logging.
Conclusion
Maintaining high availability and optimal performance in Linux environments hinges on proactive monitoring. htop, atop, and glances each bring unique strengths: from interactive process management and deep logging to a unified dashboard and web interface. By mastering these tools and securing your remote access with a robust VPN solution, you’ll gain the visibility and control needed to keep systems running smoothly.
Leave a Reply