Skip to content

Instantly share code, notes, and snippets.

@marshki
Last active July 22, 2023 15:43
Show Gist options
  • Save marshki/c7822c9b1b1be19df0781b98dc203f90 to your computer and use it in GitHub Desktop.
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.
#!/usr/bin/env bash
#
# capacitOR
#
# Emit warning if capacity on disk(s) is ninety percent (90%) or more.
#
# Author: M. Krinitz <mjk235 [at] nyu [dot] edu>
# Date: 22-Jul-2023
# License: MIT
# Set hostname, timestamp
host="$(hostname -f)"
timestamp="$(date +"%b %d %X")"
# Set threshold, e.g. 90%
limit=${1:-90}
# Assumed `df` columns are:
# "Filesystem" "1024-blocks" "Used" "Available" "Capacity" "Mounted on"
# df (limit to local filesystem; POSIX output; block size 1k)
# grep invert match
# read file line by line
# if capacity greater than or equal to threshold, do the dew
df --local --portability -k | grep -v '^Filesystem' | \
while read -r Filesystem _ _ _ Capacity _ ; do
[[ ${Capacity/\%/} -gt "${limit}" ]] && \
printf "%s\n" "On $host@$timestamp ${Filesystem} is ${Capacity} full"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment