if [ "CURRENT"−gt"THRESHOLD" ]; then
if [ ! -f "ALERT_SENT_FILE" ]; then
# Initial alert
send_alert "Disk Usage Alert for (hostname) - CURRENTMOUNT_POINT" "Warning: Disk usage for MOUNTPOINTexceededthresholdofTHRESHOLD%. Current usage: CURRENTFILE_SYSTEM\nTotal space: TOTALSPACE\nUsedspace:USED_SPACE\nFree space: FREE_SPACE"
else
# It's been more than 1 day since the last alert; check for sending a reminder
LAST_ALERT_TIME=(stat -c %Y "ALERTSENTFILE")if[(((date+LAST_ALERT_TIME)) -gt 86400 ]; then
# Send a reminder
send_alert "Reminder: Disk Usage Alert for (hostname)−CURRENT% used at MOUNTPOINT""ThisisareMOUNT_POINT is still above the threshold of THRESHOLDCURRENT%.\n\nFilesystem: FILESYSTEM\nTotalspace:TOTAL_SPACE\nUsed space: USEDSPACE\nFreespace:FREE_SPACE"
fi
fi
elif [ -f "ALERT_SENT_FILE" ]; then
# Disk usage has dropped below the threshold; clear the alert state
rm -f "ALERT_SENT_FILE"
# send an all-clear email
send_alert "Disk Usage All Clear for (hostname)−CURRENT% used at MOUNTPOINT""DiskusageforMOUNT_POINT is now below the alert threshold of THRESHOLDCURRENT%.\n\nFilesystem: FILESYSTEM\nTotalspace:TOTAL_SPACE\nUsed space: USEDSPACE\nFreespace:FREE_SPACE"
fi
Here's a thorough analysis of the script, and I've identified a few issues and suggestions for improvement:
1. Main bug:
The critical bug lies in the df output parsing. df -h formats the output with human-readable sizes (e.g., 1G, 2M), but the script assumes fixed column indices. This breaks when the size units change (e.g., from M to G). Instead, use df --output to specify exact columns and avoid parsing ambiguities:
a. Consistent quoting: Ensure consistent quoting throughout the script. For example, SUBJECT="$1" should be local subject="$1" (with local for scoping).
b. stat vs. find: Instead of stat and manual timestamp arithmetic, use find with a concise, efficient approach:
bash
if [ ! -f "$ALERT_SENT_FILE" ] || [ $(find "$ALERT_SENT_FILE" -mtime +1) ]; then
# ...
c. Simplify conditionals: Merge the if branches using a single, more readable condition:
bash
if [ "$CURRENT" -gt "$THRESHOLD" ]; then
if [ ! -f "$ALERT_SENT_FILE" ] || [ $(find "$ALERT_SENT_FILE" -mtime +1) ]; then
# Initial alert or reminder
send_alert ...
fi
elif [ -f "$ALERT_SENT_FILE" ]; then
# Disk usage dropped; clear alert and send all-clear
rm -f "$ALERT_SENT_FILE"
send_alert ...
fi
d. Error handling: Consider adding:
* A set -eu line at the top to enable strict error handling and unset variable checks.
* A trap to remove the $ALERT_SENT_FILE on script exit (e.g., trap 'rm -f "$ALERT_SENT_FILE"' EXIT).
Refactored script:
bash
#!/bin/bash
set -eu
# ...
DISK_STATS=$(df --output=source,size,used,avail,pcent "$MOUNT_POINT" | tail -1)
read -r FILE_SYSTEM TOTAL_SPACE USED_SPACE FREE_SPACE CURRENT <<< "$DISK_STATS"
CURRENT=${CURRENT%?}
send_alert() {
local subject="$1"
local message="$2"
echo -e "$message" | mail -s "$subject" "$RECIPIENT"
touch "$ALERT_SENT_FILE"
}
if [ "$CURRENT" -gt "$THRESHOLD" ]; then
if [ ! -f "$ALERT_SENT_FILE" ] || [ $(find "$ALERT_SENT_FILE" -mtime +1) ]; then
send_alert "Disk Usage Alert for $(hostname) - $CURRENT% used at $MOUNT_POINT" "..."
fi
elif [ -f "$ALERT_SENT_FILE" ]; then
rm -f "$ALERT_SENT_FILE"
send_alert "Disk Usage All Clear for $(hostname) - $CURRENT% used at $MOUNT_POINT" "..."
fi
These changes fix the main bug, improve robustness, and simplify the logic. Additionally, consider adding more informative error messages, logging, and potentially a mechanism to prevent multiple concurrent executions (e.g., using a lockfile).