Skip to content

Instantly share code, notes, and snippets.

@ehayon
Last active February 14, 2022 16:34
Show Gist options
  • Save ehayon/6212309 to your computer and use it in GitHub Desktop.
Save ehayon/6212309 to your computer and use it in GitHub Desktop.
shell script for monitoring server disk usage
#!/bin/sh
EXCLUDE=( Filesystem devtmpfs tmpfs );
USAGE_THRESHOLD=85;
PREV_IFS=$IFS; # save the old IFS so we can restore it later
IFS="|"; # change the internal field separator to a pipe
EX="${EXCLUDE[*]}"; # join exclude partitions with pipe separator
IFS=$PREV_IFS; # restore IFS
df -h | grep -Ev "^${EX}" | awk '{print $5 " " $1}' | while read output;
do
percentage=$(echo $output | awk '{print $1}' | cut -d'%' -f1)
part=$(echo $output | awk '{print $2}')
hn=$(hostname)
if [ $percentage -gt $USAGE_THRESHOLD ]; then
echo "Running out of disk space on partition $part ($percentage%) on server $hn" |
mail -s "$hn running out of disk space on $part" <email-address>
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment