
#!/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
#(1.6),250311	: Mod's; VER, noSMS
#set -x
PGM=`basename $0|cut -d\. -f1`
VER="1.6"
HOSTNAME=`hostname|cut -d\. -f1`
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 ${VER} |cut -d\= -f2|cut -d\. -f1`
#MAILTO="maarten@agrarix.nl"
MAILTO="maarten.deboer@atos.net"
#SMSTO="+31622422326@sms.gin.nl"
#SMSMSG=""

USAGE()
{
  echo "Usage: ${PGM} [-h] [-v] [-V] [-x] [-n] [--help] [--nomail] "
  echo "  Version: ${VER}"
  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${VER}"; 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` ${PGM} v${VER} New ${LOG} file." | tee -a ${LOG}
fi
echo "`date` ${PGM} v${VER} 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
VERSION=`grep VERSION ${ETC}|cut -d\= -f2`
if [ "${VERSION}" != "${VER}" ]; then 
  echo "  Version of ETC-file (${ETC}) is not equal to ${VER}. Exiting(3) ..." | tee -a ${LOG}
  exit 3
fi  # if VERSION

#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} (${VER})" ${MAILTO} < ${TMP}
    echo "  Mailed to ${MAILTO}." | tee -a $LOG
  fi  # MAIL
#  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  # TMP

rm -f ${TMP}
echo "`date` ${PGM} v${VER} finished." | tee -a ${LOG}
exit 0

