Skip to main content

Lab 2.3 - Advanced System Monitoring

Objectives​

By the end of this lab, you will be able to:

  • Monitor system performance
  • Analyze resources (CPU, RAM, disk, network)
  • Use advanced monitoring tools
  • Create monitoring scripts
  • Configure alerts

Prerequisites​

  • Functional Linux system
  • Root or sudo access
  • Tools: htop, iotop, nethogs (optional but recommended)

Exercise 1: Tool Installation​

Step 1: Basic Tools​

# Update
sudo apt update

# Install monitoring tools
sudo apt install -y htop iotop nethogs sysstat

# Verify installation
which htop iotop nethogs sar

Exercise 2: CPU Monitoring​

Step 1: CPU Information​

# CPU Information
lscpu

# CPU Details
cat /proc/cpuinfo

# Number of CPUs
nproc

# Load average
uptime
cat /proc/loadavg

Step 2: CPU Usage​

# Top CPU processes
top -bn1 | head -20

# With htop
htop

# CPU Statistics
mpstat 1 5

# By CPU
mpstat -P ALL 1 5

Step 3: CPU Script​

# CPU monitoring script
cat > cpu-monitor.sh << 'EOF'
#!/bin/bash
echo "=== CPU Info ==="
lscpu | grep -E 'Model name|CPU\(s\)|Thread|Core'
echo
echo "=== Load Average ==="
uptime
echo
echo "=== Top 5 CPU ==="
ps aux --sort=-%cpu | head -6
EOF

chmod +x cpu-monitor.sh
./cpu-monitor.sh

Exercise 3: Memory Monitoring​

Step 1: Overview​

# Memory
free -h

# Details
vmstat -s

# By process
ps aux --sort=-%mem | head -10

Step 2: Continuous Monitoring​

# Watch memory
watch -n 1 'free -h'

# With top processes
watch -n 1 'free -h && echo && ps aux --sort=-%mem | head -5'

Exercise 4: Disk Monitoring​

Step 1: Disk Space​

# Available space
df -h

# By directory
du -sh /* 2>/dev/null | sort -h

# Top 10 largest directories
du -h /home 2>/dev/null | sort -rh | head -10

Step 2: Disk I/O​

# I/O Statistics
iostat -x 1 5

# By process (iotop)
sudo iotop

# Detailed statistics
cat /proc/diskstats

Step 3: Disk Script​

# Disk monitoring script
cat > disk-monitor.sh << 'EOF'
#!/bin/bash
echo "=== Disk Space ==="
df -h
echo
echo "=== Top 10 Directories /home ==="
du -h /home 2>/dev/null | sort -rh | head -10
EOF

chmod +x disk-monitor.sh
./disk-monitor.sh

Exercise 5: Network Monitoring​

Step 1: Network Interfaces​

# Interfaces
ip addr show
# or
ifconfig

# Statistics
ip -s link show

# By interface
cat /proc/net/dev

Step 2: Connections​

# Active connections
ss -tuln

# Listening
ss -tulpn

# By process
sudo netstat -tulpn

Step 3: Network Traffic​

# nethogs (by process)
sudo nethogs

# iftop (by connection)
sudo iftop

# Statistics
cat /proc/net/sockstat

Exercise 6: Complete Monitoring​

Step 1: Monitoring Script​

# Complete script
cat > system-monitor.sh << 'EOF'
#!/bin/bash
clear
echo "=== SYSTEM MONITORING $(date) ==="
echo

echo "=== CPU ==="
echo "Load: $(uptime | awk -F'load average:' '{print $2}')"
echo "Top CPU:"
ps aux --sort=-%cpu | head -4 | tail -3
echo

echo "=== MEMORY ==="
free -h | grep -E 'Mem|Swap'
echo "Top Memory:"
ps aux --sort=-%mem | head -4 | tail -3
echo

echo "=== DISK ==="
df -h / | tail -1
echo

echo "=== NETWORK ==="
ss -tun | grep ESTAB | wc -l
echo "Established connections"
EOF

chmod +x system-monitor.sh
./system-monitor.sh

Step 2: Continuous Monitoring​

# Watch every 2 seconds
watch -n 2 ./system-monitor.sh

# Or with while
while true; do
clear
./system-monitor.sh
sleep 2
done

Exercise 7: Automatic Alerts​

Step 1: Alert Script​

# Script with thresholds
cat > system-alert.sh << 'EOF'
#!/bin/bash
THRESHOLD_CPU=80
THRESHOLD_MEM=80
THRESHOLD_DISK=80

# CPU
CPU_IDLE=$(mpstat 1 1 | tail -1 | awk '{print $NF}')
CPU_USED=$(echo "100 - $CPU_IDLE" | bc)
if (( $(echo "$CPU_USED > $THRESHOLD_CPU" | bc -l) )); then
echo "ALERT: CPU at ${CPU_USED}%"
fi

# Memory
MEM_USED=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100}')
if [ "$MEM_USED" -gt "$THRESHOLD_MEM" ]; then
echo "ALERT: Memory at ${MEM_USED}%"
fi

# Disk
DISK_USED=$(df -h / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$DISK_USED" -gt "$THRESHOLD_DISK" ]; then
echo "ALERT: Disk at ${DISK_USED}%"
fi
EOF

chmod +x system-alert.sh
./system-alert.sh

Step 2: Cron for Monitoring​

# Add to crontab
crontab -e

# Add this line (every 5 minutes)
# */5 * * * * /path/to/system-alert.sh >> /var/log/system-alerts.log 2>&1

Exercise 8: Reports​

Step 1: Daily Report​

# Report script
cat > daily-report.sh << 'EOF'
#!/bin/bash
REPORT="/var/log/daily-report-$(date +%Y%m%d).txt"

{
echo "=== SYSTEM REPORT $(date) ==="
echo
echo "=== CPU ==="
lscpu | grep -E 'Model name|CPU\(s\)'
uptime
echo
echo "=== MEMORY ==="
free -h
echo
echo "=== DISK ==="
df -h
echo
echo "=== TOP PROCESSES ==="
ps aux --sort=-%cpu | head -6
echo
ps aux --sort=-%mem | head -6
} > "$REPORT"

echo "Report created: $REPORT"
EOF

chmod +x daily-report.sh
sudo ./daily-report.sh

Verification​

Checklist​

Verify that you can:

  • Monitor CPU, memory, disk, network
  • Use htop, iotop, nethogs
  • Create monitoring scripts
  • Configure alerts
  • Generate reports

Reflection Questions​

  1. Which tool to use for which type of monitoring?

    • CPU: top, htop, mpstat
    • Memory: free, vmstat
    • Disk: df, du, iotop
    • Network: ss, netstat, nethogs
  2. Why is regular monitoring important?

    • Detect issues before they become critical
    • Resource planning
    • Performance optimization

Next Steps​

  • Module 3: File System
  • Module 4: Process Management
  • Module 11: Advanced Monitoring and Logs

Lab created on: December 2024