
#!/bin/sh
# File	: fsod-accounting-v2.sh
# By	: Maarten.deBoer@Atos.net, 121011
# Subject	: Script to collect accounting for FSDO v2
#(0.2)	: Adding volume type (SM / SV)
#set -x
PGM="`basename $0|cut -d\. -f1`"
VERSION="0.2" 
TMP="/tmp/${PGM}.$$"
MAIL=""
MAILTO="maarten.deboer@atos.net"
HOSTNAME="`hostname|cut -d\. -f1`"
FILERS="${HOME}/etc/filers"
SSH="/usr/bin/ssh -n"
DATE="`date +%Y-%m-%d`"
DATI="`date +%Y-%m-%d_%H-%M`"
SSH="/usr/bin/ssh -n"
FILTER="[1234567890]*"
LOG="${HOME}/log/${PGM}.log"
MAXLOGSIZE=1024   # In K's
USD=""
CSV="${PGM}_${HOSTNAME}_${DATI}.csv"

# Functions
USAGE()
{
  echo "Usage: ${PGM} <options>"
  echo "  Version: ${VERSION}"
  echo "  options     :"
  echo "    -m|--mail : do send Mail"
  echo "    -e|--etc  : Etc/filers-file (${FILERS})"
  echo "    -f        : Filter filername (${FILTER})"
  echo "    -h|--help : this Help"
  echo "    -V        : show Version"
  echo "    -x        : set -x"
  echo "    --mailto  : change MAILTO address & do send mail (${MAILTO})"
}

# MAIN
# Check options
if [ $# -eq 0 ]; then
  echo "No option(s) given. So do not know what to do. Exiting..."; echo; USAGE; exit 1
fi
while [ ${#} -gt 0 ]
  do
  case ${1} in
    -f) FILTER=${2}; shift ;;
    -m | --mail) MAIL=1 ;;
    -e | --etc) FILERS=${2}; shift ;;
    --mailto) MAILTO=${2}; MAIL=1; shift ;;
    -u | --usd) USD=1;;
    -h | --help) USAGE; exit 1 ;;
    -V) echo "${PGM}: v${VERSION}"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option ${1} not known. Exiting..."; echo; USAGE; exit 1 ;;
  esac
    shift
done

# 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}
fi

echo "`date` ${PGM} (v${VERSION}) started."|tee -a ${LOG}
echo "ETC(FILERS)=${ETC}"
echo "FILTER=${FILTER}"
echo "MAIL=${MAIL}"
echo "MAILTO=${MAILTO}"

echo "# Filer;Volume;Total [kB];Used [kB];Aggr;Vfiler;Customer;SnapMirror-type;SnapVault-type;"|tee -a ${TMP}

# Start filer (loop)
for FILER in `cat ${FILERS}|grep -v \^#|awk -F\; '{print $1}'|sort|grep ${FILTER}`
# Get vfiler-volumes info
do
# Running vfilers
  echo "${FILER} vfiler status ..."
  ${SSH} ${FILER} vfiler status|grep running|awk '{print $1}'|while read VFILER
  do  
    ${SSH} ${FILER} vfiler status -a ${VFILER}|grep Path|awk -v FILER="${FILER}" -v VFILER="${VFILER}" -F\: '{print FILER";"VFILER";"$2";"}'>> ${TMP}.vols
  done  # VFILER
# DR vfilers
  echo "${FILER} vfiler DR status ..."
  ${SSH} ${FILER} vfiler status|grep 'DR'|awk '{print $1}'|while read VFILER
  do  
    ${SSH} ${FILER} vfiler status -a ${VFILER}|grep Path|awk -v FILER="${FILER}" -v VFILER="${VFILER}" -F\: '{print FILER";"VFILER"-dr;"$2";"}'>> ${TMP}.vols
  done  # VFILER

# SnapMirror & SnapVault status ($3) with Source ($1) & Destination ($2)
  echo "${FILER} SM status ..."
# Remove NDPM & nrst SM2T backups
  ${SSH} ${FILER} snapmirror status|grep ':'|egrep -iv 'nrst|ndmp'|awk '{print $1";"$2";"$3}' > ${TMP}.SM-status
  echo "${FILER} SV status ..."
  ${SSH} ${FILER} snapvault status|grep ':'|awk '{print $1";"$2";"$3}' > ${TMP}.SV-status

# Get all the volumes
  ${SSH} ${FILER} vol status|grep -v 'vol0 '|grep online|awk '{print $1}'|while read VOL
  do
    echo -n "${FILER};${VOL};"|tee -a ${TMP}
    echo -n "`${SSH} ${FILER} df -k ${VOL}|egrep -v 'reserve|total|snapshot'|awk '{print $2";"$3";"}'`"|tee -a ${TMP}
    AGGR="`${SSH} ${FILER} vol status ${VOL}|grep aggregate|cut -d\: -f2|sed s/\'//g`"
    VOL_VFILER="`grep "/vol/${VOL}" ${TMP}.vols|grep ${FILER}|cut -d\; -f2`"

# Get CUSTomer from vfiler-name. If no vfiler, then CUSTomer from VOLume
    if [ "${VOL_VFILER}" != "" ]; then
      VOL_CUST="`echo ${VOL_VFILER} |cut -d\- -f1|sed 's/nlnafv//g'|sed 's/[0-9]//g'|tr a-z A-Z`"
    else
      VOL_CUST="`echo ${VOL} |cut -d\_ -f2|sed 's/vfiler//g'|sed 's/[0-9]//g'|tr a-z A-Z`"
    fi
# Get SnapMirror & SnapVault Type ($3). When more lines => Unique
    SMTYPE="`grep ${VOL} ${TMP}.SM-status|cut -d\; -f3|sort -u`"
    SVTYPE="`grep ${VOL} ${TMP}.SV-status|cut -d\; -f3|sort -u`"
    echo -n "${AGGR};${VOL_VFILER};${VOL_CUST};${SMTYPE};${SVTYPE};"|tee -a ${TMP}

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


done  # for FILER

# Mail the info
if [ ${MAIL} ]; then
  cat ${TMP}|uuencode ${CSV}|mailx -s ":${HOSTNAME}: ${PGM} [${PGM} v${VERSION}]" ${MAILTO}
  echo "`date` ${PGM}: Mailed to ${MAILTO}."|tee -a ${LOG}
fi  # if [ ${MAIL} ]

# Cleanup
rm ${TMP} ${TMP}.vols ${TMP}.SM-status
echo "`date` ${PGM} (v$VERSION) finished."|tee -a $LOG
exit 0

