Created
March 23, 2024 17:59
-
-
Save Siguza/1eafbc41edea37844af2f5eca986ac62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Setup: | |
# 0. Have a mail server set up, or something that provides `sendmail` with proper sender permissions. | |
# 1. Adjust the $drive, $buckets, $from and $to variables below to suit your needs. | |
# 2. Create /var/local/storagealert/$drive.txt and write 0 to it. | |
# 3. Put this script at /etc/cron.hourly/storagealert with chmod 0755. | |
set -e; | |
drive='PLACEHOLDER'; | |
buckets=('95' '90' '80'); | |
from='[email protected]'; | |
to='[email protected]'; | |
pct="$(df --output=pcent "/dev/$drive" | egrep -v '^Use%$')"; | |
if [ "$(wc -l <<<"$pct")" -ne '1' ]; then | |
echo '`df` returned multiple lines of output:' >&2; | |
echo "$pct" >&2; | |
exit 1; | |
fi; | |
if ! grep -Pq '^\s*\d+%$' <<<"$pct"; then | |
echo '`df` returned bad percentage:' >&2; | |
echo "$pct" >&2; | |
exit 1; | |
fi; | |
pct="$(sed -E 's#^[[:space:]]*([[:digit:]]+)%$#\1#' <<<"$pct")"; | |
old="$(cat "/var/local/storagealert/$drive.txt")"; | |
echo "$pct" >"/var/local/storagealert/$drive.txt"; | |
do_mail=false; | |
crossed=0; | |
for b in "${buckets[@]}"; do | |
if [ "$pct" -ge "$b" ] && [ "$old" -lt "$b" ]; then | |
do_mail=true; | |
crossed="$b"; | |
break; | |
fi; | |
done; | |
if "$do_mail"; then | |
sendmail -t <<EOF | |
From: $from | |
To: $to | |
Subject: Storage more than $crossed% full | |
Content-Type: text/plain; charset="US-ASCII" | |
Drive $drive is $pct% full. | |
EOF | |
fi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment