#!/usr/bin/ksh
###################################################################################
# This script to check the new array FA connectivty and device alias and vsan
###################################################################################
####################################################################
# $INPUT is in /tmp/input_sid contains switch port list from targetlun xls in usmcd01as01-fc1/2
# SID=symmetrix ID
# The $INPUT_FILE should have the following  without the # sign
# usucd08ac01-fc13/7
####################################################################
set -x
SID=0143
INPUT_FILE=/tmp/input_$SID
#cisco_site=NAMUCISCO
#key=/gsmsan04/CWS-dirs/.keys/${cisco_site}
Array_file=/gsmsan04/cob_scripts/thin_pool_config/array_install_list
TMP1=/tmp/tmp1.$$
OUT=/tmp/array_fa_details_${SID}_`date +"%m%d%y"`
CFG_DIR=/gsmsan04/cob_scripts/switch_config
Switch_file=$CFG_DIR/switch_list
EMAIL_LIST=shailesh.r.patel@citi.com,amit.shrestha@citi.com

####################
Main(){
set -x
if [[ -s $INPUT_FILE ]];then
     print "Symmetrix_FA_Name,FA_WWN,Switch_port,Fabric_name,Device_alias,VSAN,FA_Logged_in_speed" >$OUT
    
    port_list=`cat $INPUT_FILE|egrep -v "^#"`
    symm=`symcfg -sid $SID list |egrep Local |awk '{print $1}'`
    symm_name=`egrep $symm $Array_file |awk -F',' '{print $3}'`
    symcfg -sid $SID list -fa all -port |egrep "FA-" |awk '{print $1$2,tolower($3),$NF}'| sed -e "s/^/${symm_name}-/g" >$TMP1
    
     for switch in `cat $INPUT_FILE|egrep -v "^#"|awk -F'-' '{print tolower($1)}'|sort -du`
     do 
     SKEY=`egrep " $switch " $Switch_file | awk '{print $8}'`
     key=`print "/gsmsan04/CWS-dirs/.keys/$SKEY"`
     Fabric_name=`egrep " $switch " $Switch_file | awk '{print $2}'`
     ssh -K $key scriptuser@$switch " sh int brief " > /tmp/${switch}_interface_`date +"%m%d%y"`
     ssh -K $key scriptuser@$switch " sh flog d " > /tmp/${switch}_flogi_`date +"%m%d%y"`
     ssh -K $key scriptuser@$switch " sh device-alias d " |sed 's/://g' > /tmp/${switch}_devicealias_`date +"%m%d%y"`
    done
    
      for i in  $port_list ;do
      switch=`echo $i |awk -F'-' '{print tolower($1)}'`
      port=`echo $i |awk -F'-' '{print tolower($2)}'`
      Fabric_name=`egrep " $switch " $Switch_file | awk '{print $2}'`
      fa_wwn_switch=`cat /tmp/${switch}_flogi_`date +"%m%d%y"` |egrep "^${port} "|awk '{print $4}' |sed 's/://g'`
       if [[ -n $fa_wwn_switch ]];then
             logged_speed=`cat /tmp/${switch}_interface_`date +"%m%d%y"` |egrep "^${port} "|awk '{print $8}'`
             vsan=`cat /tmp/${switch}_flogi_`date +"%m%d%y"` |egrep "^${port} "|awk '{print $2}'`
             fa_name=`egrep ${fa_wwn_switch} $TMP1 |awk '{print $1}'`
             device_alias=`egrep ${fa_wwn_switch} /tmp/${switch}_devicealias_`date +"%m%d%y"` |awk '{print $3}'`
      else
            fa_wwn_switch=`print "Error_no_FA_logged_in_Switch"`
            vsan="";fa_name="Error";device_alias="";logged_speed="";
      fi

      print "${fa_name},${fa_wwn_switch},${switch}-${port},${Fabric_name},${device_alias},${vsan},${logged_speed}" >>$OUT

    done
    
     for switch in `cat $INPUT_FILE|awk -F'-' '{print tlower($1)}'|sort -du`
     do 
     cd /tmp/
        if  [[ -s /tmp/${switch}_interface_`date +"%m%d%y"` ]] ;then
           rm   /tmp/${switch}_interface_`date +"%m%d%y"`
       fi 

        if  [[ -s /tmp/${switch}_flogi_`date +"%m%d%y"` ]] ;then
           rm   /tmp/${switch}_flogi_`date +"%m%d%y"`
       fi 

        if  [[ -s /tmp/${switch}_devicealias_`date +"%m%d%y"` ]] ;then
           rm   /tmp/${switch}_devicealias_`date +"%m%d%y"`
       fi 

    done
    
    cat $OUT >/dev/null;uuencode $OUT $OUT.csv|mailx -s "$OUT"  ${EMAIL_LIST} 
    
     print " \n\n The Output file is  $OUT \n\n " 

else
    print "Error : $INPUT_FILE doesn't exist, Please check "

fi


}
Main
