Last active
August 3, 2025 15:13
-
-
Save marshki/c7822c9b1b1be19df0781b98dc203f90 to your computer and use it in GitHub Desktop.
Bash snippet to emit warning if capacity on disk(s) is ninety percent (90%) or more.
This file contains hidden or 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
#!/usr/bin/env bash | |
# | |
# capacitOR | |
# | |
# Emit email if disk capacity is >= 90%. | |
# | |
# Author: M. Krinitz <mjk235 [at] nyu [dot] edu> | |
# Date: 26-JUL-2025 | |
# License: MIT | |
df="/usr/bin/df" | |
timestamp="$(date +"%Y-%m-%d %H:%M:%S")" | |
host="$(hostname -f)" | |
email_recipients="" | |
log_file="/var/log/capacitOR.log" | |
alerts="" | |
# Set disk capacity threshold, e.g. 90%. | |
limit=${1:-90} | |
# Assumed `df` columns are: | |
# "Filesystem" "1024-blocks" "Used" "Available" "Capacity" "Mounted on" | |
# If df commaned fails, exit. | |
if ! df_output="$($df --local --portability -k | grep -v '^Filesystem')"; then | |
printf "%s\n" "$timestamp - Error: df parsing failed. Exiting." >> "$log_file" | |
exit 1 | |
fi | |
# Iterate through df_output, checking if capactiy >= limit. | |
# Accumulate warnings in alerts. | |
while IFS=" " read -r Filesystem _ _ _ Capacity _ ; do | |
capacity="${Capacity%\%}" | |
if [[ "$capacity" -ge "$limit" ]]; then | |
alerts=$(printf "%s\n" "On $host ${Filesystem} is ${Capacity} full." "$alerts") | |
fi | |
done <<< "$df_output" | |
# If any alerts present, send them via email. | |
if [[ -n "$alerts" ]]; then | |
alerts=$(printf "%s\n" "Please reduce disk capacity on the following drive(s):" \ | |
"$alerts" "Message sent: $timestamp") | |
subject="Disk capacity at $limit% on $host" | |
printf "%s\n" "$alerts" | mail -s "$subject" "$email_recipients" | |
# Log actions. | |
printf "%s\n" "$timestamp - Alerts sent for impacted filesystems on $host" >> "$log_file" | |
else | |
printf "%s\n" "$timestamp - No disk capacity alerts triggered on $host" >> "$log_file" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment