#!/usr/bin/ksh
#####################################################################
#
#    Author : Adam Radage 
#
#
#    Date : 12/07/10
#
#    Usage : . /symrdf_list.ksh -s <Site>
#       or : . /symrdf_list.ksh -s <Site> -V
#       or : . /symrdf_list.ksh -V -s <Site>
#       or : . /symrdf_list.ksh -h
#
#    Description : A script to report on the DMX or VMAX RDF devices
#
#
#####################################################################
# Version History
# ~~~~~~~~~~~~~~~
#
# Date        Version  Author           Description
# -------------------------------------------------------------------
# 10.11.2010   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/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       ./symrdf_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 arrays..."
      else
        echo "Incorrect parameters have been issued. For help type ./symrdf_list.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 ./symrdf_list.ksh -h"
        exit_error 1 " Incorrect parameters have been issued"
      fi;;
  * ) echo "Incorrect parameters have been issued. For help type ./symrdf_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 Device Mode State Type">${SITE}_${VMAX}symrdf_list_${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

   ${PREFIX}symrdf -sid $SYMNAME list|egrep " WD | RW | NR "|sed 's/Failed Over/Failed_Over/'|awk '{print $1, $7, $NF}'>${VMAX}symrdf_list_${SYMNAME}.temp

   while read LINE
    do
      TYPE=""
#      MODE=`echo $LINE|awk '{print $2}'|cut -c 1`
#print "Mode is $MODE\n"
#      case $MODE in
      case `echo $LINE|awk '{print $2}'|cut -c 1` in
        A) TYPE="Async";;
        S) TYPE="Sync";;
        E) TYPE="Semi-sync";;
        C) TYPE="Adaptive Copy";;
        *) TYPE="Unknown";;
      esac

      echo $SITE $SYMNAME $LINE $TYPE>>${SITE}_${VMAX}symrdf_list_${DATENOW}.txt
   done<${VMAX}symrdf_list_${SYMNAME}.temp
   rm ${VMAX}symrdf_list_${SYMNAME}.temp

   echo "`date`. Completed $SYMNAME ..." | tee -a $LOGFILE

done

}

#####################################################################
function help_info
{

# Help data
 echo "Help information"
 echo " Specify the site name by typing 
 	. /symrdf_list.ksh -s <Site>"
 echo " Specify for VMAX arrays by typing 
      . /symrdf_list.ksh -s <Site> -V"
 echo " or 
      . /symrdf_list -V -s <Site>"
 echo " for help type 
      . /symrdf_list.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

#####################################################################
#####################################################################

