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
The bug in the code is that the script is not updating the timestamp of the alert_sent_file when the disk usage drops below the threshold. This means that the file is still present and the script will continue to send reminder emails until the file is manually deleted.
To fix this, the script should update the timestamp of the alert_sent_file when the disk usage drops below the threshold, so that the script knows when the last alert was sent and can send a reminder email after 1 day.
Here's the corrected code:
bash
#!/bin/bash
# Check if at least two arguments were provided (email and mount point)
if [ $# -lt 2 ]; then
echo "Usage: $0 EMAIL_ADDRESS MOUNT_POINT [THRESHOLD]"
exit 1
fi
# Email address to send notifications to (required first argument)
RECIPIENT="$1"
# The mount point to check, provided as the second argument
MOUNT_POINT="$2"
# Set the threshold as a percentage; default to 80 if not provided
THRESHOLD="${3:-80}"
# File used to indicate that an alert has been sent
ALERT_SENT_FILE="/var/tmp/alert_sent_${MOUNT_POINT//\//_}"
# Extract detailed disk usage statistics
DISK_STATS=$(df -h "$MOUNT_POINT" | grep -v Filesystem)
FILE_SYSTEM=$(echo "$DISK_STATS" | awk '{ print $1 }')
TOTAL_SPACE=$(echo "$DISK_STATS" | awk '{ print $2 }')
USED_SPACE=$(echo "$DISK_STATS" | awk '{ print $3 }')
FREE_SPACE=$(echo "$DISK_STATS" | awk '{ print $4 }')
CURRENT=$(echo "$DISK_STATS" | awk '{ print $5 }' | sed 's/%//g')
send_alert() {
SUBJECT="$1"
MESSAGE="$2"
echo -e "$MESSAGE" | mail -s "$SUBJECT" "$RECIPIENT"
touch "$ALERT_SENT_FILE" # Update file's modified timestamp
}
if [ "$CURRENT" -gt "$THRESHOLD" ]; then
if [ ! -f "$ALERT_SENT_FILE" ]; then
# Initial alert
send_alert "Disk Usage Alert for $(hostname) - $CURRENT% used at $MOUNT_POINT" "Warning: Disk usage for $MOUNT_POINT exceeded threshold of $THRESHOLD%. Current usage: $CURRENT%.\n\nFilesystem: $FILE_SYSTEM\nTotal space: $TOTAL_SPACE\nUsed space: $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 "$ALERT_SENT_FILE")
if [ $(($(date +%s) - $LAST_ALERT_TIME)) -gt 86400 ]; then
# Send a reminder
send_alert "Reminder: Disk Usage Alert for $(hostname) - $CURRENT% used at $MOUNT_POINT" "This is a reminder that disk usage for $MOUNT_POINT is still above the threshold of $THRESHOLD%. Current usage: $CURRENT%.\n\nFilesystem: $FILE_SYSTEM\nTotal space: $TOTAL_SPACE\nUsed space: $USED_SPACE\nFree space: $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 $MOUNT_POINT" "Disk usage for $MOUNT_POINT is now below the alert threshold of $THRESHOLD%. Current usage: $CURRENT%.\n\nFilesystem: $FILE_SYSTEM\nTotal space: $TOTAL_SPACE\nUsed space: $USED_SPACE\nFree space: $FREE_SPACE"
touch "$ALERT_SENT_FILE" # Update file's modified timestamp
fi
In this corrected version, the touch command is added at the end of the elif block that clears the alert state, so that the timestamp of the alert_sent_file is updated when the disk usage drops below the threshold. This ensures that the script will wait at least 1 day before sending a reminder email if the disk usage exceeds the threshold again.
“There is a bug in this code. Can you find it? Specifically: Once an alert has been sent….” Conversation with codestral-2405, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m850w3mjb2tv3xxh9cfvzhcd