#!/usr/bin/ksh
#####################################################################
#
#    Author : Adam Radage 
#
#
#    Date : 12/07/10
#
#    Usage : . /vmax_PoolID_detail.ksh -s <Site>
#       or : . /vmax_PoolID_detail.ksh -h
#
#    Description : A script to report on the VMAX PoolID details
#
#
#####################################################################
# Version History
# ~~~~~~~~~~~~~~~
#
# Date        Version  Author           Description
# -------------------------------------------------------------------
# 08.06.2010   1.1      Adam Radage      Initial version
# 09.07.2010   1.2      Adam Radage      Added Powerbroker check
# 09.07.2010   1.3      Adam Radage      Changed to function style
# 12.07.2010   1.4      Adam Radage      Amended Powerbroker check
# 12.07.2010   1.5      Adam Radage      Added VMAX -V option
# 18.08.2011   1.6      Adam Radage      New
#####################################################################
#
#####################################################################
# Variables
#####################################################################

# Variables for date & time to append to filenames to make them unique
 DATENOW=`date +"%d%m%y"`
 MONTH=`date +"%b%y"`

# General variables
 SYNTAX_NO=0
 PARAM_NO=$#
 SCRIPT=$(basename $0)
 LOGFILE="./logs/${SCRIPT}_${MONTH}.log"
 VERB=0

 EMCENV="/usr/storapi/bin/"
 PBENV="/opt/pb/bin/pbrun1"
 PBRUN="${PBENV} "

 # Check if Powerbroker is present, otherwise use full storapi path
 if [[ ! -e $PBENV ]]
  then
    PREFIX=$EMCENV
    echo "Powerbroker is not present, using path: ${PREFIX}"
  else
    PREFIX=$PBRUN
    echo "Powerbroker is present, using path: ${PREFIX}"
 fi

#####################################################################
# Functions
#####################################################################
function exit_error
{
  EXIT_CODE=$1
  EXIT_MESSAGE=$(echo $@ | cut -f 2- -d " ")

  echo "$(date):$SCRIPT:$EXIT_MESSAGE" 2>&1 >>./logs/${VMAX}log.${DATENOW}.log
  exit $EXIT_CODE
}

#####################################################################
function check_parameters
{

# Check to see whether the number of parameters is correct.

case $PARAM_NO in
  1 ) if [ $1 = "-h" ]				# Help
      then
        help_info
        VERB=0
      else
        echo "Incorrect parameters have been issued. For help type       ./vmax_PoolID_list.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  2) if [ $1 = "-s" ]				# Site flag
      then
         SITE=$2
         VERB=1
         VMAX="VMAX_"
         echo "Site is: $SITE"
         echo "Investigating VMAX arrays..."
      else
        echo "Incorrect parameters have been issued. For help type ./vmax_PoolID_list.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  * ) echo "Incorrect parameters have been issued. For help type ./vmax_PoolID_list.ksh -h"
      exit_error 1 " Incorrect parameters have been issued";;
esac
}

#####################################################################
function create_file
{

# Create empty headed results file for output file
  echo "Site SID PoolID">${SITE}_${VMAX}PoolID_list_${DATENOW}.txt
  echo "Site SID PoolID Total(GB) Used(GB) Unused(GB) BoundDevices FreeDevices">${SITE}_${VMAX}PoolID_detail_${DATENOW}.txt

}

#####################################################################
function get_info
{
 # Array variable to hold local array list 
 set CFG_LIST ""

 # Get list of local arrays and place in the array variable

  set -A CFG_LIST `${PREFIX}symcfg list|grep -i Local|grep -i VMAX|awk '{print $1}'`

  echo "`date`. Processing $SITE ..." | tee -a $LOGFILE

# Run through each array and issue symaccess commands
for SYMNAME in ${CFG_LIST[@]}
 do
    # Create an array for the POOLID list
    set POOLIDLIST ""

    set -A POOLIDLIST `${PREFIX}symcfg -sid ${SYMNAME} -pool -thin list|grep "FBA"|awk '{print $1}'`

    # Run through the POOLID list
    for POOLID in ${POOLIDLIST[@]}
      do

      # Create parameters for the POOLID details
      set POOLIDDETAIL ""
      set POOLDEVNUM ""
      set POOLFREEDEVNUM ""
      set POOLTOTAL ""
      set POOLUSED ""
      set POOLUNUSED ""

      echo $SITE $SYMNAME $POOLID>>${SITE}_${VMAX}PoolID_list_${DATENOW}.txt

      POOLIDDETAIL=`${PREFIX}symcfg -sid ${SYMNAME} -pool ${POOLID} show -thin -detail -gb | grep -v Enabled | grep -v Bound|grep GBs|grep -v Alloc|grep -v Stat`
      POOLDEVNUM=`${PREFIX}symdev -sid ${SYMNAME} list -tdev -bound|grep TDEV|awk '$NF > 30000'|awk '{a1 += $NF} END {print  a1/(34526)}'`
      POOLFREEDEVNUM=`${PREFIX}symdev -sid ${SYMNAME} list -noport -tdev -bound|grep 34526|grep TDEV|wc -l`

      # Collect Total, Allocated and Free capacities
      # Check number of records? 
      if [ `echo $POOLIDDETAIL|awk '{print $1}'` == "GBs" ]
        then
          POOLTOTAL=`echo $POOLIDDETAIL|awk '{print $2}'`
          POOLUSED=`echo $POOLIDDETAIL|awk '{print $3}'`
          POOLUNUSED=`echo $POOLIDDETAIL|awk '{print $4}'`
          echo $SITE $SYMNAME $POOLID $POOLTOTAL $POOLUSED $POOLUNUSED $POOLDEVNUM $POOLFREEDEVNUM>>${SITE}_${VMAX}PoolID_detail_${DATENOW}.txt
      else
          echo $SITE $SYMNAME $POOLID $POOLTOTAL $POOLUSED $POOLUNUSED $POOLDEVNUM $POOLFREEDEVNUM>>${SITE}_${VMAX}PoolID_detail_${DATENOW}.txt
          echo `echo $POOLIDDETAIL`>>${SITE}_${VMAX}PoolID_detail_${DATENOW}.txt
      fi
    done
done

   echo "`date`. Completed $SITE ..." | tee -a $LOGFILE
}

#####################################################################
function help_info
{

# Help data
 echo "Help information"
 echo " Specify the site name by typing 
 	. /vmax_PoolID_detail.ksh -s <Site>"
 echo " for help type 
      . /vmax_PoolID_detail.ksh -h"

}

#####################################################################
#####################################################################    
# Start of Main Script
#####################################################################

check_parameters $@

case $VERB in					# VERB not set to 0 so not -h option
1)   create_file					# -V not set (non-VMAX arrays)
     get_info;;
esac

exit 0

#####################################################################
#####################################################################

