#!/bin/bash # df_watcher.sh # # This script will send email alerts when a provided limit has been reached # for partition percentage used on a host. # # Meryll Larkin 20150514 # # # put your SMS number or email address here or a space-delimited list emailaddr='' # what percentage (digits only) do you want to trigger email alerts? limit=80 emailflag=0 datestring=`date +%Y%m%d` now=`echo $datestring | tr '\n|\r' ' '` df -lk > /tmp/df_$now reportmount='' HOZTNAME=`hostname -s` if [ "$HOZTNAME" = '' ] then echo "Quitting, HOZTNAME not set." echo "" exit fi subject="$HOZTNAME Space Usage Alert" function get_totalspace { totaltotal=0 totalused=0 totalavail=0 twoLineRecord='' lastmount='xyz' while read label total used available percent mount do count=$((count+1)) if(`test "$twoLineRecord" != ''` ); then # This is for the case where Filesystem is on the previous line # So all labels are off kilter by one # fix that mount=$percent percent=$available available=$used used=$total total=$label label=$twoLineRecord twoLineRecord='' fi # change percent into a 2 digit number: percent=`echo $percent | sed 's/%//'` if( `test "$label" = "Filesystem"` ); then # ignore first line of df - it only contains column headers continue fi if( `test "$label" = "tmpfs"` ); then # ignore /dev/shm, /dev/vx/, tmpfs, swap continue fi if( `test "$mount" = "swap*"` ); then # ignore /dev/shm, /dev/vx/, tmpfs, swap continue fi if( `test "$mount" = "/dev/*"` ); then # ignore /dev/shm, /dev/vx/, tmpfs, swap continue elif(`test "$total" = ""` ); then # This is for the case where Filesystem is the only value on the line twoLineRecord=$label continue else if [ "$percent" -gt "$limit" ] ; then emailflag=1 reportmount=$'\n'"$HOZTNAME:$mount is ${percent}% full"$'\n'$reportmount fi fi done