
#!/bin/ksh
set +x
#--------------------------------------------------------------------------#
#                                                                          #
# Name: cdot_volgrow.sh                                                    #
#                                                                          #
# Description: Script to automate the resizing of volumes.                 #
#                                                                          #
#                                                                          #
# Version: 2.8                                                             #
#                                                                          #
# References:                                                              #
#                                                                          #
# Documentation:                                                           #
#                                                                          #
# Parameters: <none>                                                       #
#                                                                          #
# Usage: volgrow.sh                                                        #
#                                                                          #
# Global Description:                                                      #
#                                                                          #
# Author(s): AtoS                                                          #
#                                                                          #
# Change log:                                                              #
# aut   date            vers    comments                                   #
# RtB   04-05-2007      1.0     Initial version Ronald ten Broeke          #
# RL    06-11-2008      1.1     Update                                     #
# MdB	07-08-2009	1.2	NXP mod's and INODS added		   #
# MdB	27-08-2009	1.3	One LOGFILE = volgrow-shrink.log	   #
# MdB	29-09-2009	1.4	2>/dev/null added at "vol status" &	   #
#    				if [ `echo ${PCTGB} | grep '[0-9][0-9]'` ] #
#				&& [ ${PCTGB} -gt 89 ]; then		   #
#    				date +%Y%m%d corrected &		   #
#    				DEBUGLOG disabled		   	   #
# MdB	04-10-2009	1.5	DEBUGLOG enabled, because it looks 	   #
#				vol size + did not work accourding $LOG	   #
# MdB	09-05-2011	2.0	With USD messages about growed volumes	   #
# MdB	11-05-2011	2.1	Added AGGRMAX				   #
# MdB	12-05-2011	2.2	Changed LOGGER msg			   #
# MdB	13-05-2011	2.3	For 1 volume (due to an Incident)	   #
# MdB	13-05-2011	2.4	Vol's with fs_size_fixed=on can not grew   #
# RL	15-09-2011	2.5	Increase destination volume in SM first    #
# AH	19-11-2012	2.6	Bug fix for small volumes		   #
# MdB   08-03-2013      2.7     Add CI=                                    #
# RL	12-04-2013	2.8	Increase destination volume in SM first    #
# DM	22-09-2013	2.9	cdot adjustments
#                                                                          #
#--------------------------------------------------------------------------#
#--------------------------------------------------------------------------#
# Initialize variabels                                                     #
#--------------------------------------------------------------------------#

PGM="`basename $0|cut -d\. -f1`"
TMP="/tmp/${PGM}.$$"
HOSTNAME="`hostname`"
VERSION="2.9"

TIME=`date +%Y%m%d%H%M`
LOGFILE="${HOME}/log/${PGM}.log"
LOGFILE="${HOME}/log/volgrow-shrink.log"
EXCLFILE="${HOME}/etc/${PGM}.excl"
DEBUGLOG="${HOME}/log/${PGM}.debuglog"
FILTER="naf"
FILERS="${HOME}/etc/filers"
VSERVERS="${HOME}/etc/vservers"
LOGGER=""
MSGSTR=""
SSH="/usr/bin/ssh -n"
RESIZE=""
VOLFILTER="vol"

#--------------------------------------------------------------------------#
# Functions								   #
#--------------------------------------------------------------------------#
USAGE()
{
  echo "Usage: ${PGM} [-f <filter>] [-m] [-h] [-u] [-V] [-x] [--help] [--mail] [--mailto] [--usd]"
  echo "  Version: ${VERSION}"
  echo "  options    :"
  echo "    -f       : filter filername (${FILTER})"
  echo "    -l       : send (short)msg (to USD) via Logger"
  echo "    -r       : Resize"
  echo "    -v       : Volume"
  echo "    -h       : this help"
  echo "    -V       : Version"
  echo "    -x       : set -x"
  echo "    --help   : this help"
  echo "    --logger : send (short)msg (to USD) via Logger"
}

# Check options
while [ $# -gt 0 ]
  do
  case $1 in
    -f) FILTER=$2; shift ;;
    -v) VOLFILTER=$2; shift ;;
    -l | --logger) LOGGER=1;;
    -r) RESIZE=1;;
    -h | --help) USAGE; exit 1 ;;
    -V) echo "${PGM}: v${VERSION}"; exit 3 ;;
    -x)  set -x ;;
    -n | --nagios) NAGIOS=1;;
    *)  echo "Option $1 not known."; USAGE; exit 1 ;;
  esac
    shift
done

#--------------------------------------------------------------------------#
# $EXCLFILE should exist.
#--------------------------------------------------------------------------#
if [ ! -f "${EXCLFILE}" ]
then
   echo "${TIME}: ${EXCLFILE} doens't exist, create empty one"
   >${EXCLFILE}
fi

#--------------------------------------------------------------------------#
echo "`date +%Y%m%d_%H%M` ${PGM}: v${VERSION} @${HOSTNAME} Started with FILER=${FILTER} & VOLUME=${VOLFILTER}"| tee -a ${LOGFILE}
for FILER in `cat ${FILERS}|grep -v \^#|awk -F\; '{print $1}'|grep ${FILTER}`
do
  #------------------------------------------------------------------------#
  # Check filer for volumes that must be enlarged 			   #
  #------------------------------------------------------------------------#
  echo "Filer: ${FILER} ..."
  #7M# for VOLUME in `${SSH} ${FILER} vol status|grep ${VOLFILTER} 2>/dev/null | grep online | grep -v Volume | awk '{print $1}' | sort`
  for VOLUME in `${SSH} ${FILER} volume show 2>/dev/null | awk '!/Volume/&&/'"$VOLFILTER"'/&&/online/{print $2}' | sort`
  do
    if [ -z `grep "${FILER}:${VOLUME}" ${EXCLFILE}` ]; then
      PCTGB=`${SSH} ${FILER} df -g ${VOLUME} | grep -v File | grep -v snap | awk '{print $5}' | cut -d% -f1`
#      PCTINODES=`${SSH} ${FILER} df -i ${VOLUME} | grep -v File | awk '{print $4}' | cut -d% -f1`
      #--------------------------------------------------------------------#
      # If the volume is more than 89% filled				   #
      # Test if $PCTGB is number.					   #
      # If the volume is less than 80% filled, 				   #
      # more than 100GB and not 0GB used. 				   #
      #--------------------------------------------------------------------#
      if [ `echo ${PCTGB} | grep '[0-9][0-9]'` ] && [ ${PCTGB} -gt 89 ]; then
        #------------------------------------------------------------------#
        # Set variables                                                    #
        #------------------------------------------------------------------#
        VOLOLD=`${SSH} ${FILER} df -g ${VOLUME} | grep -v File | grep -v snap | awk '{print $2}' | cut -dG -f1`
        VOLUSED=`${SSH} ${FILER} df -g ${VOLUME} | grep -v File | grep -v snap | awk '{print $3}' | cut -dG -f1`

        #------------------------------------------------------------------#
        # Lets make NEWVOL 85%						   #
        #------------------------------------------------------------------#
        let "VOLNEW= ${VOLUSED} * 100 / 85"
        if [ "${VOLNEW}" -le "${VOLOLD}" ]; then
        let "VOLNEW= ${VOLNEW} + 2"
        fi
        let "VOLDIFF= ${VOLNEW} - ${VOLOLD}"

        #------------------------------------------------------------------#
        # Check if enough space at aggregate				   #
        #------------------------------------------------------------------#
        #7m#AGGR=`${SSH} ${FILER} vol status ${VOLUME} | grep agg | cut -d\' -f2`
        #AGGR=`${SSH} ${FILER} volume show ${VOLUME} -fields Aggregate |awk '/'"$VOLUME"'/{print $3}'`
	read VSERVER AGGR <<< $(${SSH} ${FILER} volume show ${VOLUME} -fields Vserver,Aggregate |awk '/'"$VOLUME"'/{print $1,$3}') 
        #7m#AGGRS=`${SSH} ${FILER} df -Ag ${AGGR} | grep -v Aggr | grep -v snap | awk '{print $2}' | cut -dG -f1`
        AGGRS=`${SSH} ${FILER} df -A -g ${AGGR} -fields total-space | awk '/active/&&!/snap/{print $4}'|  cut -dG -f1
        #7m#AGGRU=`${SSH} ${FILER} df -Ag ${AGGR} | grep -v Aggr | grep -v snap | awk '{print $3}' | cut -dG -f1`
        AGGRU=`${SSH} ${FILER} df -A -g ${AGGR} -fields used | awk '/active/&&!/snap/{print $4}'|  cut -dG -f1`
        #7m#AGGRPERC=`${SSH} ${FILER} df -Ag ${AGGR} | grep -v Aggr | grep -v snap | awk '{print $5}'`
        AGGRPERC=`${SSH} ${FILER} df -A -g ${AGGR} -fields percent-used-space | awk '/active/&&!/snap/{print $4}'`
        let "AGGRL= ${AGGRS} * 90 / 100"
        let "AGGRA= ${AGGRL} - ${AGGRU}"

        if [ "${VOLDIFF}" -lt "${AGGRA}" ]; then
          echo "`date +%Y%m%d_%H%M` ${PGM}: Volume ${FILER}:${VOLUME} on ${VSERVER} need to grow to ${VOLNEW}g (with ${VOLDIFF}) from ${VOLOLD}g. Used:${VOLUSED}g = ${PCTGB}% at Aggr=${AGGR} ${AGGRS}GB ${AGGRPERC}"|tee -a ${LOGFILE}|tee -a ${DEBUGLOG}
          if [ "${RESIZE}" ]; then
            #7m# ${SSH} ${FILER} vol size ${VOLUME} >> ${DEBUGLOG}
            ${SSH} ${FILER} volume size -vserver ${VSERVER} -volume ${VOLUME} >> ${DEBUGLOG}
            #--------------------------------------------------------------#
            # Test if filesys-size-fixed=true (volume show) 		   #
            # If so, resize can not be done and can be a part of a SM      #
            #--------------------------------------------------------------#
            #7m#SF="`${SSH} ${FILER} vol options ${VOLUME}| grep fs_size_fixed | cut -d\= -f2 | cut -d\, -f1`"
	    SF="`${SSH} ${FILER} volume show ${VOLUME} -fields filesys-size-fixed | awk '/'"$VOLUME"'/{print $3}'`"
            if [ "${SF}" = "true" ]; then
              #7m#SM=`${SSH} ${FILER} snapmirror status | grep ${VOLUME} | grep -v Source | grep -v "/vol/"`
              SM=`${SSH} ${FILER} snapmirror show | awk '$1 ~ /nlnaf/&&/'"$VOLUME"'/&&!/\vol\//'
              if [ "${SM}" != "" ]; then
                #7m#SM_SRC=`${SSH} ${FILER} snapmirror status | grep ${VOLUME} | grep -v Source | grep -v "/vol/" | awk '{print $1}'`
                #7m#SM_DST=`${SSH} ${FILER} snapmirror status | grep ${VOLUME} | grep -v Source | grep -v "/vol/" | awk '{print $2}'`
		read SM_SRC SM_DST <<< $(echo ${SM} | awk '{print $1,$3}')
		read SM_SRC_VSERVER SM_SRC_VOLUME <<< $(echo ${SM_SRC} | awk -F ":|-" '{print $1}')
                #7m#SM_SRC_FILER=`echo ${SM_SRC} | awk '{print $1}' | awk -F":" '{print $1}' | awk -F"-" '{print $1}'`
                #7m#SM_SRC_VOLUME=`echo ${SM_SRC} | awk '{print $1}' | awk -F":" '{print $2}'`

	        #7m#if [ "${SM_DST}" = "${FILER}:${VOLUME}" ]; then
	        if [ "${SM_DST}" = "${VSERVER}:${VOLUME}" ]; then
    	          #--------------------------------------------------------#
                  # Check if enough space at destination aggregate	   #
    	          #--------------------------------------------------------#
		### Ik kan niet raden waar de SM_SRC_VSERVER draait, dus SSH gaat niet werken
		### voorstel:
		  # Get SRC_FILER from list
		  SM_SRC_FILER=`grep $SM_SRC_VSERVER ${VSERVERS}
    	          #7m#AGGR_SRC=`${SSH} ${SM_SRC_FILER} vol status ${SM_SRC_VOLUME} | grep agg | cut -d\' -f2`
    	          AGGR_SRC=`${SSH} ${SM_SRC_FILER} volume show ${SM_SRC_VOLUME} -fields Aggregate |awk '/'"$SM_SRC_VOLUME"'/{print $3}'`
   	          #7m#AGGRS_SRC=`${SSH} ${SM_SRC_FILER} df -Ag ${AGGR_SRC} | grep -v Aggr | grep -v snap | awk '{print $2}' | cut -dG -f1`
   	          AGGRS_SRC=`${SSH} ${SM_SRC_FILER} df -A -g ${AGGR_SRC} -fields total-space | awk '/active/&&!/snap/{print $4}'|  cut -dG -f1
   	          #7m#AGGRU_SRC=`${SSH} ${SM_SRC_FILER} df -Ag ${AGGR_SRC} | grep -v Aggr | grep -v snap | awk '{print $3}' | cut -dG -f1`
   	          AGGRU_SRC=`${SSH} ${SM_SRC_FILER} df -A -g ${AGGR_SRC} -fields used | awk '/active/&&!/snap/{print $4}'|  cut -dG -f1`
   	          #7m#AGGRPERC_SRC=`${SSH} ${SM_SRC_FILER} df -Ag ${AGGR_SRC} | grep -v Aggr | grep -v snap | awk '{print $5}'`
   	          AGGRPERC_SRC=`${SSH} ${SM_SRC_FILER} df -A -g ${AGGR_SRC} -fields percent-used-space | awk '/active/&&!/snap/{print $4}'`
    	          let "AGGRL_SRC= ${AGGRS_SRC} * 90 / 100"
    	          let "AGGRA_SRC= ${AGGRL_SRC} - ${AGGRU_SRC}"

   	          if [ "${VOLDIFF}" -lt "${AGGRA_SRC}" ]; then
     	            echo "`date +%Y%m%d_%H%M` ${PGM}: Volume ${SM_SRC_VSERVER}:${SM_SRC_VOLUME} on ${SM_SRC_FILER} need to grow to ${VOLNEW}g (with ${VOLDIFF}) from ${VOLOLD}g. Used:${VOLUSED}g = ${PCTGB}% at Aggr=${AGGR_SRC} ${AGGRS_SRC}GB ${AGGRPERC_SRC}" | tee -a ${LOGFILE} | tee -a ${DEBUGLOG}

                    #7m#${SSH} ${FILER} vol size ${VOLUME} ${VOLNEW}g >> ${DEBUGLOG}
                    ${SSH} ${FILER} volume size -vserver ${VSERVER} -volume ${VOLUME} ${VOLNEW}g >> ${DEBUGLOG}
                    echo "Resized ${FILER}:${VOLUME} to ${VOLNEW}g"
                    MSGSTR="CI=${FILER} INFORMATION: Volume ${FILER}:${VOLUME} grew to ${VOLNEW}g (with ${VOLDIFF}) from ${VOLOLD}g. Used:${VOLUSED}g = ${PCTGB}% at Aggr=${AGGR} ${AGGRS}GB ${AGGRPERC} [${PGM} v${VERSION}]"

	             #7m#${SSH} ${SM_SRC_FILER} vol size ${SM_SRC_VOLUME} ${VOLNEW}g >> ${DEBUGLOG}
	             ${SSH} ${SM_SRC_FILER} volume size -vserver ${SM_SRC_VSERVER} -volume ${SM_SRC_VOLUME} ${VOLNEW}g >> ${DEBUGLOG}
                    echo "Resized ${SM_SRC_FILER}:${SM_SRC_VOLUME} to ${VOLNEW}g"
                    MSGSTR="CI=${FILER} INFORMATION: Volume ${SM_SRC_FILER}:${SM_SRC_VOLUME} grew to ${VOLNEW}g (with ${VOLDIFF}) from ${VOLOLD}g. Used:${VOLUSED}g = ${PCTGB}% at Aggr=${AGGR_SRC} ${AGGRS_SRC}GB ${AGGRPERC_SRC} [${PGM} v${VERSION}]"

	          fi
                fi
	      else
                MSGSTR="CI=${FILER} ATTENTION: Volume ${FILER}:${VOLUME} has fs_size_fixed=on. So can not be resized from ${VOLOLD}g to ${VOLNEW}g."
                echo "`date +%Y%m%d_%H%M` ${PGM}: ${MSGSTR}"|tee -a ${LOGFILE}
	      fi
            else
              #7m#${SSH} ${FILER} vol size ${VOLUME} ${VOLNEW}g >> ${DEBUGLOG}
              ${SSH} ${FILER} volume size -vserver ${VSERVER} -volume ${VOLUME} ${VOLNEW}g >> ${DEBUGLOG}
              echo "Resized ${FILER}:${VOLUME} to ${VOLNEW}g"
              MSGSTR="CI=${FILER} INFORMATION: Volume ${VSERVER}:${VOLUME} on ${FILER} grew to ${VOLNEW}g (with ${VOLDIFF}) from ${VOLOLD}g. Used:${VOLUSED}g = ${PCTGB}% at Aggr=${AGGR} ${AGGRS}GB ${AGGRPERC} [${PGM} v${VERSION}]"
            fi
          else
            echo "RESIZE-cmd: ${VSERVER}:${VOLUME} on ${FILER} to ${VOLNEW}g. NO resizing done. Option -r is needed for this."
          fi # RESIZE
        else
          echo "`date +%Y%m%d_%H%M` ${PGM}: Aggregate ${FILER}:${AGGR} does not have enough space to extend ${VOLUME} with ${VOLDIFF} GB" | tee -a ${LOGFILE}
          MSGSTR="CI=${FILER} ATTENTION: Aggregate ${FILER}:${AGGR} does not have enough space to extend ${VOLUME} with ${VOLDIFF} GB [${PGM} v${VERSION}]"
        fi
        if [ "${LOGGER}" ]; then
          #----------------------------------------------------------------#
          # To Nagios							   #
          # ------							   #
          # MINOR         Nagios-WARNING & SEV.3			   #
          # WARNING       Nagios-WARNING & SEV.3			   #
          # CRITICAL      Nagios-CRITICAL & SEV.2			   #
          # FATAL         Nagios-CRITICAL & SEV.2			   #
          # logger -t NOTIFY FSOD-SUPPORT <Nagios-SEV> "Text"	  	   #
          #----------------------------------------------------------------#
          NAGIOSSEV="MINOR"
          /usr/bin/logger -t NOTIFY FSOD-SUPPORT ${NAGIOSSEV} "${MSGSTR}"
          echo "logger: NOTIFY FSOD-SUPPORT ${NAGIOSSEV} ${MSGSTR}" | tee -a ${LOGFILE}
        fi  # logger
      fi  #
    fi
  done
done

echo "`date +%Y%m%d_%H%M` ${PGM}: v${VERSION} @${HOSTNAME} finished." | tee -a ${LOGFILE}
exit 0

