
#!/bin/sh
# File	: chk_fs (ex-chk_disks)
# By	: MaartenDeBoer.nl, 020122, LCSN
# Subject: Checks the level of disk usage
# Mod	: 021115, MdB, 1.2: $PGM used at mail.
# Mod:MdB,031021,1.3: -V added & other options & USAGE
# Mod:MdB,080820,1.4: chk_fs, $MAILCMD, touch $LOG, -n, MAXLOGSIZE, TMP.$$ & rm -r $TMP
# Mod:MdB,101203,1.5: SMSTO

#set -x

PGM="`basename $0|cut -d\. -f1`"
VERSION="1.5"
HOSTNAME="`hostname`"
LOG="/var/log/$PGM.log"
LOG="$HOME/log/$PGM.log"
ETC="/usr/local/etc/$PGM.$HOSTNAME"
ETC="$HOME/etc/$PGM.$HOSTNAME"
TMP="/tmp/$PGM.$$"
MAIL=1
MAXLOGSIZE=100   # In K's
SCRIPTVER="`echo $VERSION |cut -d= -f2|cut -d. -f1`"
MAILTO="maarten@agrarix.nl"
MAILTO="maarten.deboer@atosorigin.com"
SMSTO="+31622422326@sms.gin.nl"
SMSMSG=""

USAGE()
{
  echo "Usage: $PGM [-h] [-v] [-V] [-x] [-n] [--help] [--nomail] "
  echo "  Version: $VERSION"
  echo "  options:"
  echo "    -h : this help"
  echo "    -v : verbose"
  echo "    -V : Version"
  echo "    -x : set -x"
  echo "    -s : SMS (in stead of mail)"
  echo "    -m : do send Mail"
  echo "    -n : do NOT send mail"
  echo "    --help          : this help"
  echo "    --nomail        : do NOT send mail"
}

# Check options
while [ $# -gt 0 ]
do
  case $1 in
    --nomail) MAIL="" ;;
    -n) MAIL="" ;;
    -s) MAIL=""; SMS=1 ;;
    -m) MAIL=1 ;;
    -h | --help) USAGE; exit 1 ;;
    -v) VERBOSE=1 ;;
    -V) echo "$PGM: v$VERSION"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option $1 not known."; USAGE; exit 1;;
  esac
  shift
done

# If exitcode <> 0 the /var/log/ not writable
touch $LOG
if [ $? != 0 ]; then
  LOG="/tmp/$PGM.log"
  touch $LOG
  if [ $? != 0 ]; then
    echo "$PGM: $LOG is not writable. Exiting(2) ..."
    exit 2
  fi
fi
# Check & move LOG-file if longer then max.
touch $LOG
LOGSIZE=`du -ka $LOG | cut -f1`
if [ $LOGSIZE -ge $MAXLOGSIZE ]; then
  mv $LOG $LOG.old
  touch $LOG
  chmod 662 $LOG
  echo "`date +%Y%m%d_%H%M%S` $0: New $LOG file." | tee -a $LOG
fi
echo "`date +%Y%m%d_%H%M%S` $0: (v$VERSION) started." | tee -a $LOG

# find right mail program
if [ -f /usr/ucb/mail ]; then
  MAILCMD="/usr/ucb/mail"
fi
if [ -f /bin/mailx ]; then
  MAILCMD="/bin/mailx"
fi
if [ -f /bin/mail ]; then
  MAILCMD="/bin/mail"
fi
if [ -f /usr/bin/mailx ]; then
  MAILCMD="/usr/bin/mailx"
fi

rm -f $TMP 1> /dev/null 2>&1

# If local ETC-file, then use this file
if [ -f "${PGM}.etc" ]; then
  ETC="${PGM}.etc"
fi
if [ ! -f $ETC ]; then
  echo "$PGM: NO etc-file ($ETC) available. Exiting(3) ..." | tee -a $LOG
  exit 3
fi
if grep VERSION $ETC | grep $VERSION 1> /dev/null 2>&1; then 
  echo > /dev/null
else
  echo "$PGM: Version of etc-file is not equal to $VERSION. Exiting(3) ..." | tee -a $LOG
  exit 3
fi

export SMSMSG

cat $ETC | grep -v '#' | while read LINE
do
  FS="`echo $LINE | awk -F: '{print $1}'`"
  MAX_USAGE="`echo $LINE | awk -F: '{print $2}'`"
  ACT_USAGE="`df -k | awk '{print $5,$6,":"}' | grep \"$FS :\" | cut -d'%' -f1`"
  if [ "$ACT_USAGE" -gt "$MAX_USAGE" ]; then
    echo "The disk usage of $HOSTNAME:$FS is to high ($ACT_USAGE%) (max=$MAX_USAGE%) " | tee -a $TMP | tee -a $LOG
    SMSMSG="${SMSMSG} $HOSTNAME:$FS is to high (${ACT_USAGE}%)."
echo ${SMSMSG}
  fi

done

echo ${SMSMSG}

if [ -s $TMP ] ; then  # If size > 0
  if [ $MAIL ]; then
    echo "Options: MAIL=$MAIL MAILTO=$MAILTO" | tee -a $TMP
    $MAILCMD -s "$HOSTNAME:$PGM ($VERSION)" $MAILTO < $TMP
    echo "`date +%Y%m%d_%H%M%S` $0: Mailed to $MAILTO." | tee -a $LOG
  fi
  if [ $SMS ]; then
    echo "Options: SMS=$SMS SMSTO=$SMSTO" | tee -a $TMP
#    $MAILCMD -s "$HOSTNAME: ${SMSMSG}" $SMSTO < /dev/null
    echo "`date +%Y%m%d_%H%M%S` $0: SMS-ed ${SMSMSG} to $SMSTO." | tee -a $LOG
  fi
fi

rm -f $TMP
echo "`date +%Y%m%d_%H%M%S` $0: finished." | tee -a $LOG
exit 0

