
#!/bin/ksh
set +x
#--------------------------------------------------------------------------#
# Name: atos_accounting.sh                                                 #
# Description: Extract volume space usage on FSOD filers                   #
# Version: 1.4                                                             #
# References: Richard Hensgens for ATOS Origin accounting purposes         #
# Documentation:                                                           #
# Parameters: none                                                         #
# Usage:  atos_accounting.sh                                               #
# Global Description:                                                      #
# Author(s): Atos Origin                                                   #
# Change log:                                                              #
# aut   date            vers    comments                                   #
# WR    01-12-2006      1.0     Initial version Wim Roelofs                #
# RL    18-09-2008      1.1     Update for vfilers Richard Loos            #
# RL    25-11-2009      1.2     Update for double volumes                  #
# MdB	10-12-2012	1.3	Mod. for ING
# MdB	22-04-2013	1.4	Mod. for ING with uuencode in stead of `mailx -a`
#                                                                          #
#--------------------------------------------------------------------------#
#--------------------------------------------------------------------------#
# Initialize variabels                                                     #
#--------------------------------------------------------------------------#
#set -x
DATE=`date '+%Y%m%d'`
DIR=${HOME}/data/out
FILE=${DIR}/accounting-NAS-NetApp
OFILE=Accounting-NAS-NetApp-ING
#MAILTO="receiver@nlxmssp1.bcklan.ao-srv.com maarten.deboer@atos.net"
MAILTO="receiver@nlxmssp1.bcklan.ao-srv.com"

#--------------------------------------------------------------------------#
# Collect 'df -k' information of all filers                                #
#--------------------------------------------------------------------------#
echo "Collect 'df -k' information of all filers" 
> ${FILE}.tmp
>${DIR}/vfilers.output

for FILER in $(cat ${HOME}/etc/filers)
do
  ssh ${FILER} df -k | grep -v "/vol0/" | grep -v Filesystem >> ${FILE}.tmp done

#--------------------------------------------------------------------------#
# Collect double lines                                                     #
#--------------------------------------------------------------------------#
echo "Collect double lines"
cat ${FILE}.tmp | sed 's/snap reserve/snap_reserve/g' > ${FILE}.tmp2 cat ${FILE}.tmp2 | grep vol | awk '{print $6}' | sort > ${DIR}/volumes.txt uniq -d ${DIR}/volumes.txt > ${DIR}/double_volumes.txt cat ${FILE}.tmp2 | egrep -v -f ${DIR}/double_volumes.txt > ${FILE}.output

for VOLUME in $(cat ${DIR}/double_volumes.txt) do
  TOTAL_CAP_DV=0
  USED_CAP_DV=0
  AVAIL_CAP_DV=0
  
  for CAPACITY in `grep "${VOLUME}$" ${FILE}.tmp2 | awk '{print $1";"$2";"$3";"$4}'`
  do
    VOLUME_1=`echo ${CAPACITY} | awk -F";" '{ print $1 }'`
    TOTAL_CAP=`echo ${CAPACITY} | awk -F";" '{ print $2 }' | cut -dK -f1`
    USED_CAP=`echo ${CAPACITY} | awk -F";" '{ print $3 }' | cut -dK -f1`
    AVAIL_CAP=`echo ${CAPACITY} | awk -F";" '{ print $4 }' | cut -dK -f1`
    #----------------------------------------------------------------------#
    # Count total, used and available capacity of double volumes           #
    #----------------------------------------------------------------------#
    let "TOTAL_CAP_DV"=${TOTAL_CAP_DV}+${TOTAL_CAP}
    let "USED_CAP_DV"=${USED_CAP_DV}+${USED_CAP}
    let "AVAIL_CAP_DV"=${AVAIL_CAP_DV}+${AVAIL_CAP}
  done
  echo "${VOLUME_1} ${TOTAL_CAP_DV}KB ${USED_CAP_DV}KB ${AVAIL_CAP_DV}KB ---% ${VOLUME}" >> ${FILE}.output

done
#--------------------------------------------------------------------------#
# Collect list of vfilers                                                  #
#--------------------------------------------------------------------------#
echo "Collect list of vfilers"
for FILER in $(cat ${HOME}/etc/filers)
do
  for VFILER in `ssh ${FILER} vfiler status | grep -v vfiler0 | grep running | awk '{print $1}'`
  do
    echo ${VFILER} >> ${DIR}/vfilers.output
  done
done

#--------------------------------------------------------------------------#
# Collect vfiler information and mail                                      #
#--------------------------------------------------------------------------#
echo "Collect vfiler information and mail "
for VFILER in $(cat ${DIR}/vfilers.output) do
  CUSTOMERCODE=`echo ${VFILER} | cut -c7-`
  cat ${FILE}.output | grep "_${CUSTOMERCODE}_" > ${FILE}-${VFILER}.txt
  echo ${DATE} >> ${FILE}-${VFILER}.txt

  SUBJECT="Accounting NAS NetApp ING: ${VFILER}"

  cat ${FILE}-${VFILER}.txt|uuencode ${OFILE}-${VFILER}.txt|mailx -s "${SUBJECT}" ${MAILTO}
  echo "`date` ${PGM} Mailed |${FILE}|${SUBJECT}| to ${MAILTO}"

done

#--------------------------------------------------------------------------#
# Remove temporary files                                                   #
#--------------------------------------------------------------------------#
#rm ${FILE}.tmp
#rm ${FILE}.tmp2
#rm ${DIR}/volumes.txt
#rm ${DIR}/double_volumes.txt

