Chapter 15: Final Project - System Monitoring Script
Combine your skills to build a comprehensive system monitoring script that tracks system health, logs data, and sends alerts.
In this final project, you’ll use everything you’ve learned to build a complete system monitoring script. This script will track essential system metrics, log data to a file, and send alerts when certain thresholds are exceeded. This project is an excellent way to demonstrate practical scripting skills and reinforce your knowledge.
Step 1: Define the Monitoring Requirements
Determine which system metrics you want to monitor. For this project, we’ll track CPU usage, memory usage, and disk space. The script will log these metrics and send alerts if thresholds are exceeded.
Step 2: Setting Thresholds for Alerts
Define the thresholds for each metric. For example:
- CPU usage: 80%
- Memory usage: 80%
- Disk usage: 90%
Step 3: Writing the Monitoring Script
Let’s create a script named monitor.sh
to monitor these metrics, log the data, and send alerts:
#!/bin/bash
# Define thresholds
CPU_THRESHOLD=80
MEM_THRESHOLD=80
DISK_THRESHOLD=90
# Log file
LOG_FILE="/var/log/system_monitor.log"
# Function to check CPU usage
check_cpu() {
cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print 100 - $8}')
echo "CPU Usage: $cpu_usage%"
if (( $(echo "$cpu_usage > $CPU_THRESHOLD" | bc -l) )); then
echo "Alert: CPU usage is above $CPU_THRESHOLD%" | mail -s "CPU Usage Alert" [email protected]
fi
echo "$(date): CPU Usage: $cpu_usage%" >> "$LOG_FILE"
}
# Function to check memory usage
check_memory() {
mem_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
echo "Memory Usage: $mem_usage%"
if (( $(echo "$mem_usage > $MEM_THRESHOLD" | bc -l) )); then
echo "Alert: Memory usage is above $MEM_THRESHOLD%" | mail -s "Memory Usage Alert" [email protected]
fi
echo "$(date): Memory Usage: $mem_usage%" >> "$LOG_FILE"
}
# Function to check disk usage
check_disk() {
disk_usage=$(df / | grep / | awk '{print $5}' | sed 's/%//')
echo "Disk Usage: $disk_usage%"
if [ "$disk_usage" -gt "$DISK_THRESHOLD" ]; then
echo "Alert: Disk usage is above $DISK_THRESHOLD%" | mail -s "Disk Usage Alert" [email protected]
fi
echo "$(date): Disk Usage: $disk_usage%" >> "$LOG_FILE"
}
# Run checks
check_cpu
check_memory
check_disk
This script checks each metric, logs the data, and sends email alerts when thresholds are exceeded.
Step 4: Scheduling the Monitoring Script
To ensure the script runs regularly, schedule it with cron
. For example, to run the script every 10 minutes, add this entry to your crontab
:
*/10 * * * * /path/to/monitor.sh
This entry runs the script every 10 minutes, continuously monitoring the system and logging data.
Step 5: Reviewing the Logs
The log file /var/log/system_monitor.log
will contain a timestamped record of each metric check, allowing you to track system health over time.
# Example log entry
2024-01-01 12:00:00: CPU Usage: 70%
2024-01-01 12:00:00: Memory Usage: 65%
2024-01-01 12:00:00: Disk Usage: 85%
Summary and Reflection
In this final project, we applied various Bash scripting skills to create a comprehensive system monitoring script. This project covered scripting, error handling, logging, and automation. By using these techniques, you can ensure reliable monitoring of your system’s health and receive timely alerts for any potential issues.
Congratulations on completing the project! This system monitoring script is a valuable tool for any system administrator and a solid demonstration of your advanced Bash scripting capabilities.