Skip to content

Instantly share code, notes, and snippets.

@Siguza
Last active August 16, 2025 02:52
Show Gist options
  • Save Siguza/1eafbc41edea37844af2f5eca986ac62 to your computer and use it in GitHub Desktop.
Save Siguza/1eafbc41edea37844af2f5eca986ac62 to your computer and use it in GitHub Desktop.
[Unit]
Description=Alerts admin if storage is running low
Wants=storagealert.timer
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/storagealert
[Install]
WantedBy=multi-user.target
#!/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.
# If using systemd:
# 3. Put this script at /usr/local/sbin/storagealert with chmod 0755.
# 4. Put the storagealert.service and storagealert.timer files in /etc/systemd/system/.
# 5. Run:
# systemctl daemon-reload
# systemctl enable storagealert.timer
# systemctl start storagealert.timer
# If using cron:
# 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;
[Unit]
Description=Run storagealert every hour
Requires=storagealert.service
[Timer]
Unit=storagealert.service
OnCalendar=*-*-* *:00:00
[Install]
WantedBy=timers.target
@Siguza
Copy link
Author

Siguza commented Feb 21, 2025

You could've gotten my email address from so many places...
But no, I don't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment