#!/usr/bin/ksh
##################################################################################################
# This script provided SRDF/A query for the  respective Device group - Shailesh Patel V1.1 - 3/1/13
##################################################################################################
set -x
LOG=/gsmsan04/cob_logs/srdfa_dg_query_logs
TMP1=/tmp/tmp1.$$
TMP2=/tmp/tmp2.$$
TMP3=/tmp/tmp3.$$
TMP4=/tmp/tmp4.$$
TMP5=/tmp/tmp5.$$
TMP6=/tmp/tmp6.$$
TMP7=/tmp/tmp7.$$
#email_list=shailesh.r.patel@citigroup.com
email_list=cishia.san.provisioning@imcnam.ssmb.com,shailesh.r.patel@citi.com,dl.hds.tech.refresh@imcnam.ssmb.com
######################
Usage()
{
set -x
#tput clear
[[ -n "$1" ]] && print "\n\n$(basename $0): $@ \n "
print "\n\n $(basename $0) -g <device_group> \n"
print " arguments: "
print "            -g    <device_group>                       : Indicates  Device group name to run the query for the SRDF/A status on  Storage array  \n "
print " \n\n  sample usage is $(basename $0) -g <dg_name>   \n"
print "\t The Output file is ${LOG}/query_<dg_name>_<mmddyy> \n"
print "\t This script  provide the SRDF/A status for the Device group  \n"
exit 1

}

os_type()
{
set -x
#os=`/usr/bin/uname`
#osver=`/usr/bin/uname -a|awk '{print $3}'`
os=`/bin/uname`
osver=`/bin/uname -a|awk '{print $3}'`
   case $os in


                SunOS) export PATH=/usr/symcli/bin:/usr/xpg4/bin:$PATH
                       if [[ "$osver" = "5.8" ]];then
                            wgrep=`echo "grep -Ew "`
                       else
                            wgrep=`echo "grep -w "`
                       fi
                       MAIL=mailx
                       export wgrep ;;
                  AIX) export PATH=/usr/symcli/bin:$PATH
                       wgrep=`echo "egrep -w "`
                       MAIL=mailx
                       export wgrep  ;;
                  Linux) export PATH=/usr/symcli/bin:$PATH
                       wgrep=`echo "egrep -w "`
                       MAIL=mail
                       export wgrep  ;;


  esac

tag=`id |egrep uid | sed 's/=/ /g'|awk '{print $2}'|awk -F'(' '{print $1}'`

#email_list=`id |egrep uid | sed 's/=/ /g'|awk '{print $2}'|awk -F'(' '{print $2}'|awk -F')' '{print $1}'`

#if [[ "$tag" -eq 0 ]];then

  if [[ "$os" = Linux ]];then
     vpbrun=""
  else
    vpbrun="pbrun"
 fi

host=`hostname`
}



Arg()
{
set -x
        [[ $# = 0 ]] && Usage
        while getopts :g: OPT; do
                case $OPT in
                         g) group=$OPTARG ;;
                        \?) Usage $@ ;;
                esac
        done
shift OPTIND-1

Check_Group

}

Date()
{
set -x
print "\n*******************************************************************************************"
print " `hostname`: $1  : `date +"%D,%T"` "
print "*******************************************************************************************\n"
}

Email()
{
set -x
cat $OUT |tr -d '\015' |$MAIL -s " SRDF/A sync status for DG=$group as of `date +"%m%d%y"` " $email_list
}

Check_Group()
{
set -x

  $vpbrun symdg show $group >/dev/null 2>&1
    if [[ $? -ne 0 ]];then
          Usage " Device Group \" $group\" doesn't exists on $host. "
    fi

OUT=${LOG}/query_${group}_`date +"%m%d%y_%H%M"`

#exec  >>$OUT 2>&1

$vpbrun symdg show  $group |tr -d '\015' >$TMP1

rdf_type=`cat $TMP1 |$wgrep "RDF Type" |awk -F':' '{print $2}' |sed 's/^ //g'`
if [[ "$rdf_type" = "R2" ]]; then
      #sw_rdf=""
      src=R2;tgt=RBCV
 elif [[ "$rdf_type" = "R1" ]];then
       #sw_rdf="-rdf"
       src=R1;tgt=BCV
   else
       src=STD;tgt=BCV
fi
#print ":$sw_rdf:"
}

Check_Association(){
set -x
sversion=`$vpbrun symcli |egrep "^Symmetrix" |awk '{print substr($7,1,2)}'`
$vpbrun symmir -g $group  query $check |tr -d '\015' > $TMP2
 if [[ -s $TMP2 ]];then
  
   if [[ ${sversion} = "V8" ]];then
       cat $TMP2 |egrep "^DEV" |egrep -v " \.| N/A" |awk '{print $2":"$5":"substr($7,1,1)}' |sed 's/X/Associated_in_DG/g'|$wgrep "Associated_in_DG" >$TMP3
       cat $TMP2 |egrep "^DEV" |egrep  " \.| N/A"|awk '{print $2":"$5":BCV_Not_Associated_in_DG_Error"}' | $wgrep "BCV_Not_Associated_in_DG_Error" >$TMP4
    else
       cat $TMP2 |egrep "^DEV" |awk '{print $2":"$5":"$6}' |sed 's/\*/Associated_in_DG/g'|$wgrep "Associated_in_DG" >$TMP3
       cat $TMP2 |egrep "^DEV" |egrep -v "\*"|awk '{print $2":"$5":BCV_Not_Associated_in_DG_Error"}' | $wgrep "BCV_Not_Associated_in_DG_Error" >$TMP4
   fi
        if [[ -s $TMP4 ]];then
           Date "Error: Following ${tgt}s are not associated to DG: $group." >>$TMP6
           print "${src}:${tgt}:Comments" >>$TMP6
           cat $TMP4 >>$TMP6
           cat $TMP3 >>$TMP6
        else
           Date " All the ${src}-->${tgt} are present in the DG: $group as of" >>$TMP5
           print "${src}:${tgt}:Comments" >> $TMP5
           cat $TMP3 >> $TMP5
        fi
fi
}

Main(){
set -x

for dg in  $group
 do
  rdf_type=` cat $TMP1 |egrep "Group Type"|awk  '{print $4}'`
  if [[ "$rdf_type" = "RDF1" ]];then
       check=""
       src=R1;tgt=BCV
       Check_Association
       Date " Status of BCV<----R1 for DG=$dg when seen from R1 side"  >>$TMP5 
       $vpbrun symmir -g $dg query    >>$TMP5 2>&1
       Date "Status of R1---->R2 for DG=$dg when seen from R1 side"    >>$TMP5
       $vpbrun symrdf -g $dg query -rdfa -rdfg all   >>$TMP5  2>&1
       check=" -rdf "
       src=R2;tgt=RBCV
       Check_Association
       Date "Status of R2-------->RBCV for DG=$dg when seen from R1 side"   >>$TMP5
       $vpbrun symmir -g $dg query  -rdf    >>$TMP5 2>&1
  
  elif [[ "$rdf_type" = "RDF2" ]];then
       check=" -rdf "
       src=R1;tgt=BCV
       Check_Association
       Date " Status of BCV<----R1 for DG=$dg when seen from R2 side"    >>$TMP5
       $vpbrun symmir -g $dg query  -rdf   >>$TMP5 2>&1
       Date "Status of R2<----R1 for DG=$dg when seen from R2 side"   >>$TMP5
       $vpbrun symrdf -g $dg query -rdfa -rdfg all  >>$TMP5 2>&1 
       check=""
       src=R2;tgt=RBCV
       Check_Association
       Date "Status of R2-------->RBCV for DG=$dg when seen from R2 side"  >>$TMP5
       $vpbrun symmir -g $dg query   >>$TMP5 2>&1 
  fi


done

  >$OUT
 if [[ -s $TMP6 ]];then
   cat $TMP6 >$OUT
 fi
   cat $TMP5 >>$OUT
>$TMP5

Email

}


os_type
Arg $@
Main


