Chapter 11.1 - System Monitoring
Learning Objectives
By the end of this chapter, you will be able to:
- Monitor CPU, memory, disk
- Use top, htop, iotop
- Understand system metrics
- Use vmstat and iostat
- Configure basic alerts
Monitoring Tools
top - Real-Time
# Interactive interface
top
# Options
top -u user1 # By user
top -p 1234 # Specific process
top -d 2 # Update every 2 seconds
Displayed metrics:
- Load average
- CPU usage
- Memory (RAM, swap)
- Processes
htop - Enhanced Version
# Colored interface
htop
# Install if necessary
sudo apt install htop
Advantages:
- Intuitive interface
- Easy navigation
- Progress bars
- Filtering
iotop - I/O by Process
# Monitor disk I/O
sudo iotop
# Options
sudo iotop -o # Only active
sudo iotop -a # Accumulated
System Metrics
CPU
# CPU usage
top
htop
# CPU statistics
cat /proc/stat
mpstat # sysstat
# Load average
uptime
cat /proc/loadavg
# Interpretation:
# < number of CPUs: OK
# = number of CPUs: Full utilization
# > number of CPUs: Overload
Memory
# Overview
free -h
# Details
cat /proc/meminfo
# By process
ps aux --sort=-%mem | head
# Statistics
vmstat -s
Disk
# Disk space
df -h
# Usage by directory
du -sh /home/*
# I/O statistics
iostat -x 1
# By process
sudo iotop
Advanced Tools
vmstat - System Statistics
# Statistics every second
vmstat 1
# Important columns:
# r: Processes waiting for CPU
# b: Blocked processes
# swpd: Swap used
# free: Free memory
# si/so: Swap in/out
# us/sy/id: CPU user/system/idle
iostat - I/O Statistics
# Install
sudo apt install sysstat
# I/O statistics
iostat -x 1
# Columns:
# %util: Disk utilization
# r/s, w/s: Reads/writes per second
# rkB/s, wkB/s: KB/s
sar - System Activity Reporter
# CPU history
sar -u
# Memory history
sar -r
# I/O history
sar -b
# Real-time
sar -u 1 10 # CPU, 1 second, 10 times
Monitoring Scripts
Basic Monitoring
#!/bin/bash
# Simple monitoring script
echo "=== CPU ==="
top -bn1 | grep "Cpu(s)" | awk '{print $2}'
echo "=== Memory ==="
free -h | grep Mem
echo "=== Disk ==="
df -h / | tail -1
echo "=== Load Average ==="
uptime
Monitoring with Alerts
#!/bin/bash
# Monitoring with alerts
THRESHOLD_CPU=80
THRESHOLD_MEM=80
THRESHOLD_DISK=80
# CPU
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//')
if (( $(echo "$CPU > $THRESHOLD_CPU" | bc -l) )); then
echo "ALERT: CPU at ${CPU}%"
fi
# Memory
MEM=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100}')
if [ "$MEM" -gt "$THRESHOLD_MEM" ]; then
echo "ALERT: Memory at ${MEM}%"
fi
# Disk
DISK=$(df -h / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$DISK" -gt "$THRESHOLD_DISK" ]; then
echo "ALERT: Disk at ${DISK}%"
fi
Monitoring Cron
Scheduled Task
# /etc/cron.d/monitoring
# Monitoring every 5 minutes
*/5 * * * * root /usr/local/bin/monitor.sh >> /var/log/monitoring.log 2>&1
Script with Logging
#!/bin/bash
# /usr/local/bin/monitor.sh
LOG="/var/log/monitoring.log"
DATE=$(date '+%Y-%m-%d %H:%M:%S')
{
echo "=== $DATE ==="
echo "CPU: $(top -bn1 | grep 'Cpu(s)' | awk '{print $2}')"
echo "Memory: $(free -h | grep Mem | awk '{print $3"/"$2}')"
echo "Disk: $(df -h / | tail -1 | awk '{print $5}')"
echo "Load: $(uptime | awk -F'load average:' '{print $2}')"
echo "---"
} >> "$LOG"
Summary
In this chapter, you learned:
Tools: top, htop, iotop, vmstat, iostat, sar
Metrics: CPU, memory, disk, load average
Monitoring: Automatic scripts, alerts
Cron: Regular monitoring scheduling
Practice: Continuous monitoring, problem detection
Next Steps
Chapter 11.2: Log Management
Chapter 11.3: Monitoring Crons and Alerts
Chapter created on: December 2024