Automate Daily Disk Usage Audits & Alerting on Threshold Breaches using Cron and Umask
This guide teaches system administrators how to schedule automated, daily disk usage audits on Linux servers using the cron scheduler and implement threshold alerting for proactive capacity management.
Direct Diagnostic & Remediation Summary
- Create Audit Script: Write a shell script (e.g., audit_disk.sh) containing disk checks and logging logic.
#!/bin/bash .... - Set Cron Job: Edit the crontab file to run the script daily at a specific time.
crontab -e. - Schedule Entry Example: Add the entry to run at 2:00 AM every day.
0 2 * /path/to/audit_disk.sh > /dev/null 2>&1
1. Executive Overview / Summary
This tutorial provides a comprehensive solution for monitoring disk space utilization on Linux systems automatically. By creating a dedicated shell script, we will check key filesystem mount points against predefined thresholds (e.g., 85% usage). This script is then scheduled using the system's cron job utility to run daily, ensuring timely alerts or logs are generated before critical storage failure occurs.
2. Problem Overview & Error Symptoms
Manually checking disk space is time-consuming and prone to human error. The primary symptom of neglect is unexpected system slowdown or outright failure due to insufficient storage capacity (e.g., running out of inodes or disk blocks). Common signs include:
- System services failing unexpectedly with 'No space left on device' errors.
- Slow file operations, particularly during logging or temporary file creation.
- Observed disk usage consistently approaching 90-100% across multiple partitions without prior warning.
These symptoms indicate a lack of proactive monitoring infrastructure.
3. Root Causes & Technical Mechanisms
df -h provide a snapshot but require manual execution. We leverage Bash scripting for logic encapsulation and cron}$ to establish reliable, time-based execution intervals. Umask control helps ensure that temporary files or script outputs retain appropriate permissions without being accidentally world-writable or overly restricted during automated runs.The mechanism involves:
- Scripting (Bash): Using
df -Pfor portable output, piping toawkorgrepto parse utilization percentages. - Threshold Logic: Implementing conditional statements (
if) to compare the numeric usage against a defined alert threshold (e.g., 85). - Scheduling (Cron): Using
crontab -eto map time units (minute, hour, day_of_month, month, day_of_week) to script execution. - Logging & Alerting: Redirecting output to a persistent log file and incorporating
mail}$or similar services for immediate notification.
4. Sequential Step-by-Step Solution Blocks
Create the Disk Audit Shell Script
First, create the executable script that performs the core logic. We will check for usage greater than 85% on all mounted filesystems.
nano /usr/local/bin/audit_disk.shPaste the following content into the editor, modifying the ALERT_THRESHOLD if necessary:
#!/bin/bash
# Configuration
LOGFILE="/var/log/disk_audit.log"
ALERT_RECIPIENT="[email protected]"
ALERT_THRESHOLD=85 # Percentage threshold for alerting
echo "--- $(date) --- Starting Disk Audit." >> $LOGFILE
# Use df -P for POSIX compliant output, filtering out temporary/virtual filesystems
df -P | awk 'NR>1 {
usage=$5;
sub(/%/, "", usage); # Remove the % sign for numeric comparison
filesystem=$6;
if (usage > '$ALERT_THRESHOLD') {
print "ALERT: Filesystem " filesystem " is at " usage "% capacity." >> "/tmp/disk_alert.txt";
}'
# Check if any alerts were generated
if [ -s /tmp/disk_alert.txt ]; then
MESSAGE="Disk Space Alert Detected! Reviewing critical partitions:
$(cat /tmp/disk_alert.txt)"
echo "$MESSAGE" >> $LOGFILE
mail -s "CRITICAL: Disk Usage Threshold Breach on $(hostname)" $ALERT_RECIPIENT $LOGFILE
fi
rm -f /tmp/disk_alert.txt
echo "--- $(date) --- Audit Complete." >> $LOGFILE
Save and make the script executable:
chmod +x /usr/local/bin/audit_disk.sh
Test Script Execution and Logging
Before scheduling, run the script manually to ensure it executes correctly, generates expected output in the log file, and attempts to send an email (if mail utilities are configured).
/usr/local/bin/audit_disk.shReview the generated log file:
tail -n 20 /var/log/disk_audit.logIf you are testing on a system with no high usage, verify that the 'within acceptable limits' message appears.
Schedule the Job using Cron
We use crontab -e for the current user (or sudo crontab -e for root). We schedule it to run early in the morning when system load is typically low.
crontab -eAdd the following line to the end of the file. This example runs the script every day at 3:00 AM:
# M H D M W Command
0 3 * * * /usr/local/bin/audit_disk.sh > /dev/null 2>&1Explanation of the cron schedule (0 3 *):
- Minute (0): The job runs on the hour mark (0 minutes past).
- Hour (3): The job runs at 3 AM.
- Day of Month (*): Every day of the month.
- Month (*): Every month.
- Day of Week (*): Every day of the week.
Review Umask and Permissions
While the script handles most permissions, it is crucial to ensure the user running cron jobs has necessary write access to the log directory (/var/log/) and that the system's umask does not interfere with temporary file creation.
- Verify Log Write Access: Ensure the user running cron belongs to a group that can write to
/var/log. - Check Umask (Advanced): Run
umaskrand ensure it is not overly restrictive, preventing script components from writing temporary files (though the script uses /tmp which is generally safe). - System Check: Confirm that the 'mail' utility package (e.g., mailutils on Debian/Ubuntu) is installed and configured to relay outbound emails.
No reader questions posted yet. Have you tried this solution or experienced an edge case? Post below.