
#!/bin/sh
# File  : chk-set_vfiler_defaults.sh
# By    : Maarten.deBoer@Atos.net, 140805
# Subject       : Script to check & set default vfiler options
PGM="`basename $0|cut -d\. -f1`"
VER="0.1"
TMP="/tmp/${PGM}.$$"
SSH="/usr/bin/ssh -n"
FILERS="${HOME}/etc/filers"
HOSTNAME="`hostname|cut -d\. -f1`"
MAILTO="maarten.deboer@atos.net"
LOG="${HOME}/log/${PGM}.log"
FILER=""
VFILER=""
ETC=${HOME}/etc/vfiler_defaults
CHK=1
SETOPTIONS=""
FILTER="[1234567890]?"
MAIL=""
MAILTO="maarten.deboer@atos.net"


# Functions
USAGE()
{
  echo ""
  echo "Usage: ${PGM} <option(s)>"
  echo "  Version: ${VER}"
  echo "  options         :"
  echo "    -c            : Check only"
  echo "    -e            : set Etc/vfiler_default-file (${ETC})"
  echo "    --set         : SET the options"
  echo "    -f            : Filername"
  echo "    -m            : Mail result"
  echo "    -v            : Vfilername"
  echo "    -h | --help   : this help"
  echo "    -V            : Version"
  echo "    -x            : set -x"
  echo "    --mailto      : Mailto address"
}
## MAIN
# Check options
if [ ${#} -eq 0 ]; then
  echo "No option(s) given. So not to know what to do. Exiting..."; USAGE;
  exit 1
fi
while [ ${#} -ge 1 ]
  do
  case $1 in
    -c) CHK=1;;
    -e) ETC=${2}; shift ;;
    -f) FILER=${2}; shift ;;
    --set) SETOPTIONS=1;;
    -m) MAIL=1;;
    --mailto) MAILTO=${1}; MAIL=1; shift ;;
    -v) VFILER=${2}; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option $1 not known."; USAGE; exit 1 ;;
  esac
    shift
done

if [ "${VFILER}" = "" ]; then
  echo "NO Vfilername given. Exiting ..."
  USAGE
  exit 2
fi

if [ ! -f ${ETC} ]; then
  echo "Etc-file (${ETC}) NOT found. Exiting ..."
  USAGE
  exit 3
fi

if [ "${FILER}" = ""  ]; then
  echo "Please provide filername -f. Exiting ..."
  exit 3  
fi

grep ${FILER} ${FILERS} > /dev/null
EC=${?}
if [ ${EC} -gt 0 ]; then
  echo "Filer ${FILER} not found in ${FILERS}. Exiting ..."
  exit 4  
fi

ANSW="`${SSH} ${FILER} vfiler status ${VFILER}| grep running`"
if [ "${ANSW}" = "" ]; then
  echo "Vfiler ${VFILER} not found in at ${FILER}. Exiting ..."
  exit 5  
fi

echo "ETC=${ETC}"
echo "FILER=${FILER}"
echo "FILERS=${FILERS}"
echo "HOSTNAME=${HOSTNAME}"
echo "LOG=${LOG}"
echo "MAIL=${MAIL}"
echo "MAILTO=${MAILTO}"
echo "PGM=${PGM}"
echo "VER=${VER}"
echo "VFILER=${VFILER}"

touch ${TMP}
echo "`date` ${PGM} v${VER} started ${FILER} / ${VFILER}"|tee -a ${LOG}|tee -a ${TMP}
echo ""|tee -a ${TMP}

cat ${ETC}|grep -v ^#|grep '='|while read LINE
do
  OPTION="`echo ${LINE}|cut -d\= -f1`"
  VALUE="`echo ${LINE}|cut -d\= -f2`"
  RESULT="`${SSH} ${FILER} vfiler run -q ${VFILER} options ${OPTION}|awk '{print $2}'`"
  if [ "${RESULT}" != "${VALUE}" ]; then
    echo "${OPTION}=${RESULT} not OK. Should be ${VALUE}"|tee -a ${TMP}
    if [ ${SETOPTIONS} ]; then
      echo "`date` ${OPTION} will be set to ${VALUE} (${RESULT})"|tee -a ${LOG}
      ${SSH} ${FILER} vfiler run ${VFILER} options ${OPTION} ${VALUE}
    fi
  else
    echo "${OPTION}=${RESULT} = OK " >> ${TMP}
  fi
done  # cat ${ETC}

if [ ${MAIL} ]; then
  cat ${TMP}|mailx -s "@${HOSTNAME}: Checked vfiler options" ${MAILTO}
  echo "`date` Mailed to ${MAILTO}"|tee -a ${LOG}
fi

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

