
#!/bin/ksh
set +x
#--------------------------------------------------------------------------#
#                                                                          #
# Name: make_volume_report.sh                                              #
#                                                                          #
# Description: Script collects volume inode or volume size and snap list   #
#              information per vfiler and sends information by mail.       #
#                                                                          #
# Version: 1.0                                                             #
#                                                                          #
# References:                                                              #
#                                                                          #
# Documentation:                                                           #
#                                                                          #
# Parameters: <flag> <customer code> [<email address>]                     #
#                                                                          #
# Usage: make_volume_report.sh <flag> <customer code> [<email address>]    #
#                                                                          #
# Global Description:                                                      #
#                                                                          #
# Author(s): Atos Origin                                                   #
#                                                                          #
# Change log:                                                              #
# aut   date          vers   comments                                      #
# RL    09-09-2009    1.0    Initial version Richard Loos                  #
#                                                                          #
#--------------------------------------------------------------------------#
#--------------------------------------------------------------------------#
# Read parameter                                                           #
#--------------------------------------------------------------------------#
CUSTOMERCODE=${2}

#--------------------------------------------------------------------------#
# Initialize variabels                                                     #
#--------------------------------------------------------------------------#
DATUM=`date '+%m-%d-%Y'`
CUSTOMER=`echo "${CUSTOMERCODE}"|tr [:lower:] [:upper:]`
DATADIR=${HOME}/data/out

#--------------------------------------------------------------------------#
# Handle flags of script
#--------------------------------------------------------------------------#
while getopts si: FLAG; do
  case $FLAG in
    s ) 
       #-------------------------------------------------------------------#
       # Collect snap list information per vfiler.                         #
       #-------------------------------------------------------------------#
       FILE=${DATADIR}/$DATUM-${CUSTOMER}-Snapshot-Reporting.rtf
       OFILE=$DATUM-${CUSTOMER}-Snapshot-Reporting.rtf
       > ${FILE}

       for FILER in $(cat ${HOME}/etc/filers)
       do
         for VFILER in `ssh ${FILER} vfiler status | grep ${CUSTOMERCODE} | awk '{ print $1 }'`
         do
           echo "" >> ${FILE}
           echo "List of volume size and snapshots of vfiler ${VFILER}:" >> ${FILE}
           ssh ${FILER} vfiler run ${VFILER} vol status > ${DATADIR}/vol${VFILER}
           for VOLUME in $(cat ${DATADIR}/vol${VFILER} | grep online | awk '{ print $1 }' | grep "${CUSTOMERCODE}" | grep -v ^vol0 | sort )
           do
             ssh ${FILER} vfiler run ${VFILER} df -g /vol/${VOLUME} >> ${FILE}
             ssh ${FILER} vfiler run ${VFILER} snap list ${VOLUME} >> ${FILE}
           done
           echo "" >> ${FILE}
           rm ${DATADIR}/vol${VFILER}
         done

       done >> ${FILE}

       #-------------------------------------------------------------------#
       # Send output as attachment in email.                               #
       # If script doesn't start on console, send mail to parameter $3     #
       #-------------------------------------------------------------------#
       if `tty -s`
       then
         cat ${FILE} | uuencode ${OFILE} | mailx -s "$DATUM-${CUSTOMER}-Snapshot-Reporting" gmnl-msscentral@atosorigin.com fsod@atosorigin.com
       else
         if [ -n "${3}" ]
         then
           cat ${FILE} | uuencode ${OFILE} | mailx -s "$DATUM-${CUSTOMER}-Snapshot-Reporting" ${3}
         fi
       fi

       rm ${FILE}
        ;;

    i ) 
       #-------------------------------------------------------------------#
       # Collect inode information per vfiler.                             #
       #-------------------------------------------------------------------#
       FILE=${DATADIR}/$DATUM-${CUSTOMER}-Inode-Reporting.rtf
       OFILE=$DATUM-${CUSTOMER}-Inode-Reporting.rtf
       > ${FILE}

       for FILER in $(cat ${HOME}/etc/filers)
       do
         for VFILER in `ssh ${FILER} vfiler status | grep ${CUSTOMERCODE} | awk '{ print $1 }'`
         do
           echo "" >> ${FILE}
           echo "List of volume inodes of vfiler ${VFILER}:" >> ${FILE}
           ssh ${FILER} vfiler run ${VFILER} df -i | grep -v "vol0_" >> ${FILE}
           echo "" >> ${FILE}
         done

       done >> ${FILE}

       #-------------------------------------------------------------------#
       # Send output as attachment in email.                               #
       # If script doesn't start on console, send mail to parameter $3     #
       #-------------------------------------------------------------------#
       if `tty -s`
       then
         cat ${FILE} | uuencode ${OFILE} | mailx -s "$DATUM-${CUSTOMER}-Inode-Reporting" gmnl-msscentral@atosorigin.com fsod@atosorigin.com
       else
         if [ -n "${3}" ]
         then
           cat ${FILE} | uuencode ${OFILE} | mailx -s "$DATUM-${CUSTOMER}-Inode-Reporting" ${3}
         fi
       fi

       rm ${FILE}

        ;;

    '?' ) echo "Invalid flag: make_volume_report.sh <[i|s]> <customer code>"
          exit 1
        ;;
  esac
done

