BASH
Monitor Server Disk Usage and Trigger Alerts
Proactively monitor disk space on your web server and send an email alert if usage exceeds a defined threshold, preventing critical storage issues.
#!/bin/bash
THRESHOLD=90 # Percentage
EMAIL="[email protected]"
DISK_USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//g')
if (( DISK_USAGE > THRESHOLD )); then
echo "Disk usage on $(hostname) is at ${DISK_USAGE}% which is above the ${THRESHOLD}% threshold. Please check the server." | mail -s "Disk Space Alert on $(hostname)" "$EMAIL"
echo "Alert sent: Disk usage at ${DISK_USAGE}%"
else
echo "Disk usage is normal: ${DISK_USAGE}%"
fi
How it works: This script checks the disk usage of the root filesystem using `df`. It extracts the percentage used and compares it against a predefined `THRESHOLD`. If the usage exceeds the threshold, it sends an email alert to the specified address, warning of potential disk space issues on the server.