#!/bin/ksh
#############################################################################
#
# Script: vmax_portpairs.ksh
#
# Function: Display FA usage, number of logins, capacity mapped, etc
#
# Version History
# V1.0 - Original Script
# V2.0 - Added number of active logins (13/06/2012)
#
#############################################################################

PATH=${PATH}:/usr/symcli/bin

setrdcsid () {
clear
print "Which VMAX array are you allocating from? "
print
print "1) 0552 - Tier 3 Replicated to/from 0745 (CGC)"
print "2) 0678 - Tier 4 Non-Replicated"
print "3) 2230 - Tier 3 Replicated to 2231 (FDC)"
print "4) 3286 - Tier 3 Replicated from 2804 (FDC)"
print "5) 3073 - Tier 4 Non-Replicated"
print
print -n "Please enter 1, 2, 3, 4, 5 or q to quit: "
read RDCSID

case ${RDCSID} in
        1) SID=0552
	   MGTPORT=8G1;;
        2) SID=0678
	   MGTPORT=7E0;;
        3) SID=2230
	   MGTPORT=07E0;;
        4) SID=3286
	   MGTPORT=05F0;;
        5) SID=3073
	   MGTPORT=05E0;;
        *) exit;;
esac
}

setcgcsid () {
clear
print "Which VMAX array are you allocating from? "
print
print "1) 0745 - Tier 3 Replicated to/from 0552 (RDC)"
print "2) 0623 - Tier 4 Non-Replicated"
print
print -n "Please enter 1, 2 or q to quit: "
read CGCSID

case ${CGCSID} in
        1) SID=0745
	   MGTPORT=7E1;;
        2) SID=0623
	   MGTPORT=8F0;;
        *) exit;;
esac
}

setfdcsid () {
clear
print "Which VMAX array are you allocating from? "
print
print "1) 0879 - Tier 4 Non-Replicated"
print "2) 2231 - Tier 3 Replicated from 2230 (RDC)"
print "3) 2804 - Tier 3 Replicated to 3286 (RDC)"
print "4) 3004 - Tier 4 Non-Replicated"
print
print -n "Please enter 1, 2, 3, 4 or q to quit: "
read FDCSID

case ${FDCSID} in
        1) SID=0879
           MGTPORT=8G1;;
        2) SID=2231
           MGTPORT=07E0;;
        3) SID=2804
           MGTPORT=05F0;;
        4) SID=3004
           MGTPORT=05E0;;
        *) exit;;
esac
}


SITE=`uname -n | cut -c1-3`

if [ ${SITE} = "rdc" ]
then
        BASEDIR=/export/home/san/capacity
        LOGDIR=${BASEDIR}/log
	TMPDIR=${BASEDIR}/tmp
	PBRUN=pbrun
        setrdcsid
elif [ ${SITE} = "fdc" ]
then
        BASEDIR=/export/home/san/capacity
        LOGDIR=${BASEDIR}/log
	TMPDIR=${BASEDIR}/tmp
	PBRUN=pbrun
	setfdcsid
elif [ ${SITE} = "cgc" ]
then
        BASEDIR=/var/opt/san/ac
        LOGDIR=${BASEDIR}/../log
	TMPDIR=${BASEDIR}/../tmp
	PBRUN=
        setcgcsid
fi

EXCLUDEFILE=${BASEDIR}/${SID}_exclude_fa
EXCLUDEFA=`grep -v "#" ${EXCLUDEFILE}`

${PBRUN} symaccess -sid ${SID} list -type port -detail | grep -v Symmetrix | sed -e /^$/d > ${TMPDIR}/pptmp1

sed -e 's/            //' -e 's/------------//' ${TMPDIR}/pptmp1 > ${TMPDIR}/pptmp1sed
sed -e 's/Port    View/Port   View/' -e 's/Count   Count/Count  Count/' ${TMPDIR}/pptmp1sed  > ${TMPDIR}/pptmp1

head -3 ${TMPDIR}/pptmp1 > ${TMPDIR}/pphead1

#if [ "${EXCLUDEFA}" = "" ]
#then
	#grep ${SID} ${TMPDIR}/pptmp1 > ${TMPDIR}/pptmp2 
#else
	#grep ${SID} ${TMPDIR}/pptmp1 | egrep -v ${EXCLUDEFA} > ${TMPDIR}/pptmp2
#fi

grep ${SID} ${TMPDIR}/pptmp1 > ${TMPDIR}/pptmp2 

for PP in `cat ${TMPDIR}/pptmp2 | awk '{print $1}' | sed -e 's/C_//' -e 's/M_//'`
do
	DIRPORT=`echo ${PP} | cut -d_ -f2`
	typeset -i NUMCHARS PC DC
	NUMCHARS=`echo ${DIRPORT} | wc -c`
	let PC=${NUMCHARS}-1
	let DC=${NUMCHARS}-2
	DIR=`echo ${DIRPORT} | cut -c1-${DC}`
	PORT=`echo ${DIRPORT} | cut -c${PC}`
	NUMMAP=`${PBRUN} symcfg -sid ${SID} -dir ${DIR} -p ${PORT} -addr list 2>/dev/null | grep Mapped | awk '{print $3}'`
	if [ -z ${NUMMAP} ]
	then
		echo 0 >> ${TMPDIR}/pptmp3
	else
		echo ${NUMMAP} >> ${TMPDIR}/pptmp3
	fi
	typeset -i NUM
	NUM=`${PBRUN} symcfg -sid ${SID} -dir ${DIR} -p ${PORT} -addr list 2>/dev/null | grep Metamembers | awk '{print $3}'`
	if [ -z ${NUM} ]
	then
		echo 0 >> ${TMPDIR}/ppcap
	else
		if [ ${DIRPORT} = ${MGTPORT} ]
		then
			let NUM=NUM-32
		fi
		echo ${NUM} >> ${TMPDIR}/ppcap
	fi
	${PBRUN} symaccess -sid ${SID} list logins -dirport ${DIR}:${PORT} 2>/dev/null | grep Fibre | wc -l >> ${TMPDIR}/pplogins
	${PBRUN} symaccess -sid ${SID} list logins -dirport ${DIR}:${PORT} 2>/dev/null | grep Fibre | grep -v No | wc -l >> ${TMPDIR}/pploginsact
done

typeset -i DEV
typeset -i CAP

for DEV in `cat ${TMPDIR}/ppcap`
do
	let CAP=DEV*34526/1024
	echo ${CAP} >> ${TMPDIR}/ppgb
done


paste ${TMPDIR}/pptmp2 ${TMPDIR}/pptmp3  ${TMPDIR}/pplogins ${TMPDIR}/pploginsact ${TMPDIR}/ppgb > ${TMPDIR}/pptmp4 

sort -k 5n -k 3n -k 7n ${TMPDIR}/pptmp4 > ${TMPDIR}/ppsorted

for PPS in `cat ${TMPDIR}/ppsorted | awk '{print $1}'`
do
	grep ${PPS} ${TMPDIR}/pptmp2 >> ${TMPDIR}/pptmp5
	grep ${PPS} ${TMPDIR}/pptmp4 | awk '{print $4}' >> ${TMPDIR}/pptmp6
	grep ${PPS} ${TMPDIR}/pptmp4 | awk '{print $5}' >> ${TMPDIR}/pptmp10
	grep ${PPS} ${TMPDIR}/pptmp4 | awk '{print $7}' >> ${TMPDIR}/pptmp12
	grep ${PPS} ${TMPDIR}/pptmp4 | awk '{print $6}' >> ${TMPDIR}/pptmp14
done

cat ${TMPDIR}/pphead1 ${TMPDIR}/pptmp5 > ${TMPDIR}/pptmp7

echo Num > ${TMPDIR}/pphead2
echo Devices >> ${TMPDIR}/pphead2
echo ------- >> ${TMPDIR}/pphead2

echo Num > ${TMPDIR}/pphead3
echo Logins >> ${TMPDIR}/pphead3
echo ------ >> ${TMPDIR}/pphead3

echo GB > ${TMPDIR}/pphead4
echo Mapped >> ${TMPDIR}/pphead4
echo ------ >> ${TMPDIR}/pphead4

echo Active > ${TMPDIR}/pphead5
echo Logins >> ${TMPDIR}/pphead5
echo ------ >> ${TMPDIR}/pphead5

cat ${TMPDIR}/pphead2 ${TMPDIR}/pptmp6 > ${TMPDIR}/pptmp8

cat ${TMPDIR}/pphead3 ${TMPDIR}/pptmp10 > ${TMPDIR}/pptmp11

cat ${TMPDIR}/pphead4 ${TMPDIR}/pptmp12 > ${TMPDIR}/pptmp13

cat ${TMPDIR}/pphead5 ${TMPDIR}/pptmp14 > ${TMPDIR}/pptmp15

paste ${TMPDIR}/pptmp7 ${TMPDIR}/pptmp11 ${TMPDIR}/pptmp15 ${TMPDIR}/pptmp8 ${TMPDIR}/pptmp13 > ${TMPDIR}/pptmp9

clear

#print "Please choose a report"
#print
#print "   1) Port pairs available for allocations"
#print "   2) Full report including restricted/excluded FAs"
#print
#print -n "Please select 1 or 2: [1] "
#read REPLY
#
#case ${REPLY} in
#	1) clear
#	   print "The following pairs are available for allocations"
#	   print
#	   egrep -v ${EXCLUDEFA} ${TMPDIR}/pptmp9
#	   ;;
#	2) clear
#	   print "The following pairs are available for allocations"
#	   print
#	   egrep -v ${EXCLUDEFA} ${TMPDIR}/pptmp9
#	   print
#	   print "The following pairs are restricted/excluded from allocations"
#	   print "as defined by ${EXCLUDEFILE}"
#	   print
#	   head -3 ${TMPDIR}/pptmp9
#	   egrep ${EXCLUDEFA} ${TMPDIR}/pptmp9
#	   ;;
#	*) clear
#	   cat ${TMPDIR}/pptmp9
#           ;;
#esac

clear
if [ "${EXCLUDEFA}" = "" ]
then
	print "The following pairs are available for allocations"
	print
	cat ${TMPDIR}/pptmp9
else
	print "The following pairs are available for allocations"
	print
	egrep -v ${EXCLUDEFA} ${TMPDIR}/pptmp9
	print
	print "The following pairs are restricted/excluded from allocations"
	print "as defined by ${EXCLUDEFILE}"
	print
	head -3 ${TMPDIR}/pptmp9
	egrep ${EXCLUDEFA} ${TMPDIR}/pptmp9
fi


rm ${TMPDIR}/pptmp1
rm ${TMPDIR}/pptmp1sed
rm ${TMPDIR}/pptmp2
rm ${TMPDIR}/pptmp3
rm ${TMPDIR}/pptmp4
rm ${TMPDIR}/pptmp5
rm ${TMPDIR}/pptmp6
rm ${TMPDIR}/pptmp7
rm ${TMPDIR}/pptmp8
rm ${TMPDIR}/pptmp9
rm ${TMPDIR}/pptmp10
rm ${TMPDIR}/pptmp11
rm ${TMPDIR}/pptmp12
rm ${TMPDIR}/pptmp13
rm ${TMPDIR}/pptmp14
rm ${TMPDIR}/pptmp15
rm ${TMPDIR}/pphead1
rm ${TMPDIR}/pphead2
rm ${TMPDIR}/pphead3
rm ${TMPDIR}/pphead4
rm ${TMPDIR}/pphead5
rm ${TMPDIR}/ppsorted
rm ${TMPDIR}/pplogins
rm ${TMPDIR}/pploginsact
rm ${TMPDIR}/ppcap
rm ${TMPDIR}/ppgb
