Skip to content

Instantly share code, notes, and snippets.

@gitaarik
Created November 11, 2022 13:08
Show Gist options
  • Save gitaarik/3781b702c8febdcabef2d0ccee8eec25 to your computer and use it in GitHub Desktop.
Save gitaarik/3781b702c8febdcabef2d0ccee8eec25 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Script that sends an alert email when any disk's used space is over a certain
# limit.
# The first parameter to this script is the min disk space usage percentage for
# when the alerts kicks in.
# The second parameter is the email address the alert message should be sent
# to.
#
# Can be configured in a crontab like this:
#
# 0 0 * * * send_full_disk_alert_mail 90 mail@example.com
#
# Based on: https://unix.stackexchange.com/a/84359/39618
message=$(df -h | awk -v ALERT="$1" '
NR == 1 {next}
$1 == "abc:/xyz/pqr" {next}
$1 == "tmpfs" {next}
$1 == "/dev/cdrom" {next}
1 {sub(/%/,"",$5)}
$5 >= ALERT {printf "%s is almost full: %d%%\n", $1, $5}
')
if [ -n "$message" ]; then
echo "$message" | mail -s "Alert: Almost out of disk space" "$2"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment