
#!/bin/ksh
set +x
#--------------------------------------------------------------------------#
#                                                                          #
# Name: atos_accounting.sh                                                 #
#                                                                          #
# Description: Extract volume space usage on FSOD filers                   #
#                                                                          #
# Version: 1.2                                                             #
#                                                                          #
# 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                  #
#                                                                          #
#--------------------------------------------------------------------------#
#--------------------------------------------------------------------------#
# Initialize variabels                                                     #
#--------------------------------------------------------------------------#
set -x
DATE=`date '+%Y%m%d'`
DIR=${HOME}/data/out
FILE=${DIR}/accounting-NAS-NetApp
OFILE=Accounting-NAS-NetApp
RECIPIENT="receiver@nlxmssp1.bcklan.ao-srv.com"
RECIPIENT="maarten.deboer@atos.net"

#--------------------------------------------------------------------------#
# 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                                                     #
#--------------------------------------------------------------------------#
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                                                  #
#--------------------------------------------------------------------------#
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                                      #
#--------------------------------------------------------------------------#
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
  cat ${FILE}-${VFILER}.txt | uuencode ${OFILE}-${VFILER}.txt | mailx -s "Accounting NAS NetApp: ${VFILER}" "${RECIPIENT}"
done

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

