Monitor VPS Resources with htop and glances
Before scaling up your VPS or debugging performance issues, you need to identify which resource is the bottleneck. htop provides an interactive process-level view, while glances shows a comprehensive overview of CPU, RAM, disk, and network all in one screen. This article teaches you how to use both tools and interpret what you see.
htop: interactive process viewer
sudo apt install htop -y
htop
Common htop shortcuts:
F6— Sort by CPU/MEM/PID/TIMEF5— Tree view (view parent-child process relationships)F4— Filter by process nameF9— Kill selected processSpace— Tag multiple processes at onceu— Filter by user
Reading htop values
- Load Average (top right) — Average load over 1/5/15 minutes. If higher than the number of CPU cores, the system is overloaded.
- MEM bar — Green = used, blue = buffers, yellow = cache (high cache is normal and not a problem)
- Swp bar — Heavy swap usage (red) indicates insufficient RAM
glances: all-in-one monitor
sudo apt install glances -y
# Run in terminal mode
glances
# Run with web UI (access via browser on port 61208)
glances -w
glances displays comprehensive information on a single screen:
- CPU per-core usage + top processes
- Memory + swap
- Disk I/O read/write per second
- Network in/out per interface
- Open files, connections, sensors
glances --export csv --output /tmp/glances.csv logs metrics to CSV on every interval — perfect for debugging performance issues during specific time windows.
Essential built-in commands
# CPU usage per process
top -bn1 | head -20
# Detailed memory info
free -h
# Real-time disk I/O
iostat -xz 1
# Real-time network traffic
sar -n DEV 1
# View processes consuming the most disk I/O
sudo iotop -o
Verify Monitoring is Working Correctly
# Check load average (should be below the number of cores)
uptime
nproc # Number of CPU cores
# Check memory availability
free -h | awk '/^Mem:/ {print "Available:", $7}'
# Check disk I/O
iostat -d 1 3 | tail -10
# Check network bandwidth
ifstat -i eth0 1 5 2>/dev/null || ip -s link show eth0
iostat -x 1 and look at the %iowait column.
Common Issues & Troubleshooting
htop shows CPU at 100% constantly
Press F6 to sort by CPU and find the top consumer. If it's a PHP/Python worker, it might be stuck in an infinite loop or running a slow query:
# Find the top 5 CPU-consuming processes
ps aux --sort=-%cpu | head -6
# View stack trace of a suspicious process (requires strace)
sudo strace -p PID -c -f 2>&1 | head -20
RAM is full but I don't know which process is consuming it
The "used" value in free -h includes cache, which the kernel typically returns when needed. Check "available" instead:
# Find top memory consumers (RSS = actual physical RAM)
ps aux --sort=-%mem | awk 'NR<=6 {print $2, $4, $11}'
# View detailed memory breakdown for a process (e.g., PID 1234)
cat /proc/1234/status | grep -E "VmRSS|VmSwap|Name"
glances shows "CRITICAL" in red constantly
glances thresholds are set in /etc/glances/glances.conf — the defaults may be too sensitive. Adjust them to match your server:
sudo nano /etc/glances/glances.conf
# Add or modify the [cpu] section
[cpu]
careful=70
warning=80
critical=90
iotop command not found
Install it separately and run with sudo (iotop reads kernel counters):
sudo apt install iotop -y
# Run in non-interactive (batch) mode
sudo iotop -b -n 3 -o
Frequently Asked Questions
How are htop and top different?
top is a built-in tool on every Linux system but the UI is not user-friendly. htop requires separate installation but offers scrolling, filtering, process killing, clear color coding, and per-core CPU viewing. For daily operations, htop is significantly more practical.
Load Average is high but CPU usage is low — what causes this?
Load Average counts all processes in the run queue, including those waiting for I/O (disk/network). If %iowait is high in iostat, disk is the bottleneck, not CPU. Check disk I/O metrics and consider upgrading storage or implementing caching.
Can I access glances web UI if the VPS has no public IP?
Yes, use SSH tunneling: ssh -L 61208:localhost:61208 user@VPS_IP, then open http://localhost:61208 in your browser. This is more secure than exposing port 61208 publicly.