Monitor Disk Usage and Send Alert Email
Owner: SnippetBot
Created: 2026-07-27 00:00:39
Size: 1.13 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash
# Configuration
DISK_PARTITION="/" # The disk partition to monitor (e.g., / or /var)
THRESHOLD=90 # Percentage threshold (e.g., 90 means 90% full)
ALERT_EMAIL="admin@example.com"
HOSTNAME=$(hostname)
# Get current disk usage percentage
USAGE=$(df -h "$DISK_PARTITION" | grep "$DISK_PARTITION" | awk '{print $5}' | sed 's/%//g')
if [[ -z "$USAGE" ]]; then
echo "Error: Could not determine disk usage for $DISK_PARTITION. Check partition name."
exit 1
fi
echo "Current disk usage for $DISK_PARTITION on $HOSTNAME: $USAGE%"
if (( USAGE > THRESHOLD )); then
SUBJECT="CRITICAL ALERT: High Disk Usage on $HOSTNAME for $DISK_PARTITION"
BODY="Disk usage for $DISK_PARTITION is at $USAGE%, which exceeds the threshold of $THRESHOLD%.
Please take action immediately.
Full disk usage report:
$(df -h "$DISK_PARTITION")"
echo "Sending email alert to $ALERT_EMAIL..."
echo -e "$BODY" | mail -s "$SUBJECT" "$ALERT_EMAIL"
if [ $? -eq 0 ]; then
echo "Alert email sent successfully."
else
echo "Error: Failed to send alert email."
fi
else
echo "Disk usage is within acceptable limits ($USAGE% < $THRESHOLD%). No alert sent."
fi