This script keeps watch over your disk space so you never get caught by a full partition. It runs df -P for clean, single-line POSIX output, then loops over each filesystem and extracts the "Use%" column, stripping the trailing percent sign so it can be compared numerically. Pseudo and network filesystems such as tmpfs, overlay, and squashfs are filtered out because their usage figures are meaningless for capacity planning. Any filesystem above the THRESHOLD (85% by default) is appended to an alert body together with its mount point and used/total figures. If the body is non-empty, the script pipes a formatted message to the mail command and sends it to ALERT_EMAIL. All key values are overridable through environment variables, making it easy to reuse across hosts. Schedule it from cron every fifteen minutes for continuous, low-cost monitoring without external tooling.
Disk Usage Threshold Alert
Emails an administrator whenever any mounted filesystem climbs above a configurable usage percentage.
1 views
Share
Script
Download .shbash
#!/usr/bin/env bash
#
# disk_usage_alert.sh
# Sends an email when any mounted filesystem exceeds a usage threshold.
# Designed to be run from cron, e.g.: */15 * * * * /opt/scripts/disk_usage_alert.sh
#
set -euo pipefail
# ---- Configuration -----------------------------------------------------------
THRESHOLD="${THRESHOLD:-85}" # Alert when usage % is greater than this.
ALERT_EMAIL="${ALERT_EMAIL:-root@localhost}"
HOSTNAME_FQDN="$(hostname -f 2>/dev/null || hostname)"
# Filesystem types to skip (pseudo / network filesystems).
EXCLUDE_FS="tmpfs|devtmpfs|squashfs|overlay|udf|iso9660"
# ---- Collect filesystems over the threshold ----------------------------------
# df -P -> POSIX output (one line per FS, no wrapping).
# We parse the "Use%" column and strip the trailing '%'.
alert_body=""
while read -r filesystem size used avail usep mount; do
use_pct="${usep%\%}" # Remove the '%' sign.
# Skip the header row and any non-numeric value.
[[ "$use_pct" =~ ^[0-9]+$ ]] || continue
if (( use_pct > THRESHOLD )); then
alert_body+="${mount} (${filesystem}) is at ${use_pct}% used [${used}/${size}]"$'\n'
fi
done < <(df -P -x tmpfs -x devtmpfs 2>/dev/null | grep -vE "${EXCLUDE_FS}")
# ---- Notify if there is anything to report -----------------------------------
if [[ -n "${alert_body}" ]]; then
subject="[DISK ALERT] ${HOSTNAME_FQDN}: filesystem over ${THRESHOLD}%"
{
echo "Disk usage threshold (${THRESHOLD}%) exceeded on ${HOSTNAME_FQDN}:"
echo
echo "${alert_body}"
echo "Checked at: $(date '+%Y-%m-%d %H:%M:%S %Z')"
} | mail -s "${subject}" "${ALERT_EMAIL}"
echo "Alert sent to ${ALERT_EMAIL}."
else
echo "OK: all filesystems are below ${THRESHOLD}%."
fiHow to run
Review the script first, then download or copy it and run it in your environment.
You might also like
bashlinuxIntermediate
CPU Load Average Alert
Warns by email when the one-minute load average per CPU core exceeds a configurable ratio.
bashlinuxAdvanced
Clean up old AMIs and snapshots
Deregister your old AMIs past a retention window and delete their backing EBS snapshots.
bashlinuxIntermediate
Sync a Directory to a Remote Host via rsync over SSH
Mirrors a local directory to a remote server securely over SSH.