#!/usr/bin/ksh
#####################################################################
#
#    Author : Adam Radage 
#
#
#    Date : 20/09/11
#
#    Usage : . /symcg_list.ksh -s <Site>
#       or : . /symcg_list.ksh -s <Site> 
#       or : . /symcg_list.ksh -h
#
#    Description : A script to report on the DMX or VMAX Composite Groups
#
#
#####################################################################
# Version History
# ~~~~~~~~~~~~~~~
#
# Date        Version  Author           Description
# -------------------------------------------------------------------
# 20.09.2011   1.1      Adam Radage      Initial version
#####################################################################
#
#####################################################################
# 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/pbrun"
 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       ./symcg_list.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/VMAX arrays..."
      else
        echo "Incorrect parameters have been issued. For help type ./symcg_list.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  * ) echo "Incorrect parameters have been issued. For help type ./symcg_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 RemSID CG Device Mode State Type">${SITE}_${VMAX}symcg_list_${DATENOW}.txt

}

#####################################################################
function get_info
{

 # Array variable to hold Composite Group names
 set CG_LIST ""

  set -A CG_LIST `${PREFIX}symcg list -v|grep "Composite Group Name"|awk '{print $NF}'`

# Run through each CG and issue symrdf commands
  for CG_NAME in ${CG_LIST[@]}
    do

     echo "$CG_NAME"
     set SYMM ""
     set REMOTE ""

     ${PREFIX}symrdf query -cg $CG_NAME|egrep " WD | RW | NR |Symmetrix ID"|sed 's/Remote Symmetrix ID/R RID/'|sed 's/Failed Over/Failed_Over/'|awk '{print $2, $4, $11, $NF}'>${VMAX}symcg_list_${CG_NAME}.temp

     while read LINE
       do

         case `echo $LINE|awk '{print $1}'` in
           "ID")        SYMM=`echo $LINE|awk '{print $2}'`;;
           "RID")       REMOTE=`echo $LINE|awk '{print $2}'`;;
           *)           TYPE=""
                        case `echo $LINE|awk '{print $3}'|cut -c 1` in
                          A) TYPE="Async";;
                          S) TYPE="Sync";;
                          E) TYPE="Semi-sync";;
                          C) TYPE="Adaptive Copy";;
                          *) TYPE="Unknown";;
                        esac
           LINE2=`echo $LINE|awk '{print $1, $3, $4}'`
           echo $SITE $SYMM $REMOTE $CG_NAME $LINE2 $TYPE>>${SITE}_${VMAX}symcg_list_${DATENOW}.txt
         esac

      done<${VMAX}symcg_list_${CG_NAME}.temp
      rm ${VMAX}symcg_list_${CG_NAME}.temp

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

   done

}

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

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

}

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

check_parameters $@

case $VERB in					# VERB not set to 0 so not -h option
1)   create_file
     get_info;;
esac

exit 0

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

