#!/usr/bin/ksh
#####################################################################
#
#    Author : Adam Radage 
#
#
#    Date : 12/07/10
#
#    Usage : . /symdisk_size.ksh -s <Site>
#       or : . /symdisk_size.ksh -s <Site> -V
#       or : . /symdisk_size.ksh -V -s <Site>
#       or : . /symdisk_size.ksh -h
#
#    Description : A script to report on the DMX or VMAX disks
#
#
#####################################################################
# Version History
# ~~~~~~~~~~~~~~~
#
# Date        Version  Author           Description
# -------------------------------------------------------------------
# 16.04.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
# 10.02.2012   1.6      Adam Radage      Disk size report only
#####################################################################
#
#####################################################################
# 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       ./symdisk_size.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  2) if [ $1 = "-s" ]				# Site flag
      then
         SITE=$2
         VERB=1
         VMAX=""
         echo "Site is: $SITE"
         echo "Investigating DMX arrays..."
      else
        echo "Incorrect parameters have been issued. For help type ./symdisk_size.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  3 ) if [ $1 = "-s" ] && [ $3 = "-V" ]	# Site flag and VMAX
      then
         SITE=$2
         VERB=2
         VMAX="VMAX_"
         echo "Site is: $SITE"
         echo "Investigating VMAX arrays..."
      elif [ $1 = "-V" ] && [ $2 = "-s" ]
      then
         SITE=$3
         VERB=2
         VMAX="VMAX_"
         echo "Site is: $SITE"
         echo "Investigating VMAX arrays..."
      else
        echo "Incorrect parameters have been issued. For help type ./symdisk_size.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  * ) echo "Incorrect parameters have been issued. For help type ./symdisk_size.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 Disk_Group Disk_Size_(MB) Disk_Number">${SITE}_${VMAX}symdisk_size_${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
 if [[ $VERB -eq 2 ]]						# If VERB=2, list VMAX arrays
  then
    set -A CFG_LIST `${PREFIX}symcfg list|grep -i Local|grep -i VMAX|awk '{print $1}'`
 else
    set -A CFG_LIST `${PREFIX}symcfg list|grep -i Local|grep -iv VMAX|awk '{print $1}'`
 fi

# Run through each array and issue symaccess commands
for SYMNAME in ${CFG_LIST[@]}
 do
   echo "`date`. Processing $SYMNAME ..." | tee -a $LOGFILE

    set DISK_LIST ""

   # Obtain a list of disk group numbers
    set -A DISK_LIST `${PREFIX}symdisk -sid $SYMNAME list -by_diskgroup|grep "Disk Group"|grep -v "Name"|sed 's/://'|awk '{print $3}'` 

    for DISKGRP in ${DISK_LIST[@]}
     do

       echo "`date`. Checking Disk Group $DISKGRP ..." | tee -a $LOGFILE

	 # Obtain an array of disk sizes
       set DISK_SIZE 0
       set -A DISK_SIZE `${PREFIX}symdisk -sid $SYMNAME list -disk_group $DISKGRP|grep DF-|awk '{print $NF}'|sort -u`

      # Scroll through the disk sizes per disk group 
      for DISKSZ in ${DISK_SIZE[@]}
       do
       if [[ $DISKSZ -ne 0 ]] 
        then
         echo "`date`. Disk Group $DISKGRP Disk Size $DISKSZ" | tee -a $LOGFILE

         DISKSZ_NUM=0

         # Obtain the number of disks by size
         DISKSZ_NUM=`${PREFIX}symdisk -sid $SYMNAME list -disk_group $DISKGRP|grep DF-|grep ${DISKSZ}|wc -l`

         # Print out the list of sizes and number
         echo $SITE $SYMNAME $DISKGRP $DISKSZ $DISKSZ_NUM|sed 's/ -/ 0/'>>${SITE}_${VMAX}symdisk_size_${DATENOW}.txt

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

      fi
      done

    done

done

}

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

# Help data
 echo "Help information"
 echo " Specify the site name by typing 
 	. /symdisk_size.ksh -s <Site>"
 echo " Specify for VMAX arrays by typing 
      . /symdisk_size.ksh -s <Site> -V"
 echo " or 
      . /symdisk_size -V -s <Site>"
 echo " for help type 
      . /symdisk_size.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;;
2)   create_file					# -V set (VMAX arrays)
     get_info;;
esac

exit 0

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

