
#!/bin/ksh
# File	: rpt_cust.sh
# By	: Maarten.deBoer@Atos.net, 120620
# Subject	: Report items of FSOD customem accourding to .CONF-file
# 0.2	: Sort df + mod. -s at mailx
# 0.3	: Qtree+Quotas + lineistr + (/bin/ksh)
PGM="`basename $0|cut -d\. -f1`"
VERSION="0.3"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
MAILTO="."
SSH="/usr/bin/ssh -n"
HOSTNAME="`hostname |cut -d\. -f1`"

touch ${TMP}
echo "`date` ${PGM} v${VERSION} started" |tee -a ${LOG}

ls -1 $HOME/etc/${PGM}-* | while read FILE
do
  CUST="`echo ${FILE} | cut -d\- -f2|cut -d\. -f1`"
  echo "Report Customer status from ${CUST} [${FILE}] "|tee -a ${TMP}
  MAILTO="`grep '^mailto=' ${FILE}|cut -d\= -f2`"
  cat ${FILE}|grep -v '^#'|while read LINE
  do
# Linestr
    LINESTR="`echo ${LINE}| grep '^linestr='|cut -d\= -f2`"
    if [ "${LINESTR}" != "" ]; then
      echo ${LINESTR} |tee -a ${TMP}
    fi
# Filers
    FILER="`echo ${LINE}| grep '^filer='|cut -d\= -f2`"
    if [ "${FILER}" != "" ]; then
      echo -n "Filer ${FILER} =" |tee -a ${TMP}
      HOSTNAME="`${SSH} ${FILER} hostname`"
      if [ "${HOSTNAME}" = ${FILER} ]; then
        echo " OK"|tee -a ${TMP}
      else
        echo " NOT ok (filername not the same)"|tee -a ${TMP}
      fi
    fi
# Vfilers 
    FILER="`echo ${LINE}| grep '^filer-vfiler='|cut -d\= -f2|cut -d\/ -f1`"
    VFILER="`echo ${LINE}| grep '^filer-vfiler='|cut -d\= -f2|cut -d\/ -f2`"
    if [ "${VFILER}" != "" ]; then
      echo -n "Vfiler ${VFILER} =" |tee -a ${TMP}
      VHOSTNAME="`${SSH} ${FILER} vfiler run -q ${VFILER} hostname`"
      if [ "${VHOSTNAME}" = ${VFILER} ]; then
        echo " OK"|tee -a ${TMP}
      else
        echo " NOT ok (vfilername not the same)"|tee -a ${TMP}
      fi
    fi
# Volumes
    FILER="`echo ${LINE}| grep '^volumes='|cut -d\= -f2|cut -d\/ -f1`"
    VFILER="`echo ${LINE}| grep '^volumes='|cut -d\= -f2|cut -d\/ -f2`"
    if [ "${VFILER}" != "" ]; then
      echo  "Volumes of ${VFILER}/${VFILER} [Volumename & Used %] :" |tee -a ${TMP}
      ${SSH} ${FILER} vfiler run -q ${VFILER} df -g|grep -v snapshot|awk '{print $1,$5}'|sort -rnk 2|tee -a ${TMP}
    fi
# Qtree/Quotas
    FILER="`echo ${LINE}| grep '^quotas='|cut -d\= -f2|cut -d\/ -f1`"
    VFILER="`echo ${LINE}| grep '^quotas='|cut -d\= -f2|cut -d\/ -f2`"
    if [ "${VFILER}" != "" ]; then
      echo  -n "Quotas of ${FILER}/${VFILER} [Volumename, Qtreename, Used %   (Used KB / Quota KB)]  :" |tee -a ${TMP}
      ${SSH} ${FILER} vfiler run -q ${VFILER} quota report > ${TMP}.2
      grep 'quotas are off' ${TMP}.2 1> /dev/null 2>&1
      EC=${?}
      if [ ${EC} -eq 0 ]; then
        echo " Quotas are OFF !"|tee -a ${TMP}
      else
        # NewLine
        echo "" | tee -a ${TMP}
        cat ${TMP}.2|grep tree|grep -v '*'|awk '{print $3,$4,$5,$6}'|while read VOL QTREE USED QUOTA
        do
          let "PERC=( ${USED} *100 ) / ${QUOTA} "
          echo "${VOL} ${QTREE} ${PERC}%   (${USED} / ${QUOTA} )"|tee -a ${TMP}

        done
        echo ""|tee -a ${TMP}

      fi
      
    fi



  done  # LINE
  echo "" |tee -a ${TMP}
  HOSTNAME="`hostname |cut -d\. -f1`"
  echo "Produced by ${PGM} v${VERSION} at ${HOSTNAME} at `date`."|tee -a ${TMP}
  cat ${TMP}|mailx -s "FSOD custom report for ${CUST} [${PGM} v${VERSION}]" ${MAILTO}
#  cat ${TMP}|mailx -s "for ${CUST} [${PGM} v${VERSION}]" ${MAILTO}
  echo "Mailed to ${MAILTO} of ${FILE}"|tee -a ${LOG}
  rm ${TMP} ${TMP}.2
done  # ls -1

exit 0

