#!/bin/ksh
#
# $Revision: 1.3 $ $Date: 2010-11-10 13:20:42-05 $
# $UUID: ca583930-f781-3100-b878-5542c05bace9 $
#
#<checkdrives: send mail if a filesystem gets too full
# Try to avoid depending on GNU software being installed.

PATH=/bin:/usr/bin
BLOCKSIZE=1m
BLOCK_SIZE=1048576
export PATH BLOCKSIZE BLOCK_SIZE
tag=${0##*/}

# Portability and configuration stuff here.
subject='drives getting full'
to='admin-urgent'
host=$(hostname | cut -f1 -d.)
work=$HOME/var/drives
max=96  # more than this percent == drive is too full.

# What df should we use?
case "$(uname -s)" in
    SunOS)    DF='/usr/xpg4/bin/df -F ufs' ;;
    FreeBSD)  DF='df -t ufs' ;;
    *)        DF='df' ;;
esac

# Real work starts here.  Run df, skip the header, kill %-sign,
# and list filesystems that are too full.
filesys=$($DF | sed -n -e '2,$p' |
          tr -d '%' | awk -v max=$max '$5 >= max {print $6}')

case "X$filesys" in
    X)  exit 0 ;;
    *)  ;;
esac

# Keep current and previous drive status.
if test ! -d $work; then
    mkdir -p $work 2> /dev/null

    if test ! -d $work; then
        echo "$host: $tag: mkdir $work failed" | mailx $to
        exit 1
    fi
fi

# Don't send the same message repeatedly.
cd $work
(echo $host; $DF $filesys) > cur

if test -f prev; then
    cmp -s cur prev || mailx -s "$host $subject" $to < cur
else
    mailx -s "$host $subject" $to < cur
fi

mv cur prev
exit 0
