#!/bin/sh

#This will spit out the volume-groups and associated volumes
#in veritas volume manager envinonment.
#Anish Khanzode

. ./srm_keys.sh

VERITAS_VXVM_ID=1
ESMID_VER=1
ESM_VXVM_VOL_TAG=$ESMID_VER~$ID_SRM_KEY_VOLUME~veritas~vxvm~custom
sid=$SRMAGT_SCAN_ID

VXVOL_PATH=/dev/vx/rdsk
PATH=/usr/sbin:/usr/bin

# enumeration of the volume types. If you add new volume types, also check and change
# GetVolumeInfo.sh and GetSDSVolumeInfo.sh and GetVxvmVolumeInfo.sh together.
RaidTypeSimple=1
RaidTypeSpanning=2
RaidTypeConcat=3
RaidTypeStripe=100
RaidTypeMirror=101
RaidTypeRAID5=105
RaidMirAndStr=110
RaidTypeSwap=1000
RaidTypeTrans=2001
RaidTypeSoftP=2002
RaidSubMir=2003
RaidLogging=2004
RaidOther=2000

# this file is created in StartScan.sh and populated in the vxvm and sds volume scripts
vm_volume_map_file=/tmp/StartScan.vmvolinfo.$sid

slice_char="s"
dg_list=$@

#--------------------------------------------------------------------------
# Functions
#--------------------------------------------------------------------------

#--------------------------------------------------------------------------
# s_prog()
#   Returns calling script name.  
#--------------------------------------------------------------------------
s_prog() {
  basename $0
}

#--------------------------------------------------------------------------
# create_tmpfile()
#   Creates a tmp file name.
#
#   Arg_1: unique identifier, optional, defaults to PID
#--------------------------------------------------------------------------
create_tmpfile() {
  temp_file="/tmp/`s_prog`_$$_${1:-$$}.tmp"
  rm -f $temp_file
  touch $temp_file
  echo $temp_file
}

#--------------------------------------------------------------------------
# rm_tmpfile()
#   Delete tmp file(s).  Deletes all files with this PID if no argument is
#   given
#
#   Arg_1: unique identifier
#--------------------------------------------------------------------------
rm_tmpfile() {
  rm -f /tmp/`s_prog`_$$_${1:-""}*.tmp
}


tmp_file_name=`create_tmpfile`
if [ "$dg_list" ]; then
    for dg in $dg_list; do
        vxprint -htvpsdGqQ -g $dg >> $tmp_file_name
        echo "" >> $tmp_file_name
    done
else
    vxprint -htvpsdGqQ > $tmp_file_name
fi
if [ ! -s $tmp_file_name ] 
then
    rm_tmpfile
    exit 0
fi


hostedby=`./GetESMIdInfo.sh $ID_SRM_KEY_HOSTNAME`

tmp_out_file_name=`create_tmpfile out`
grep "^[dm,dg,sd,v,pl]" $tmp_file_name | grep -v "PRIVATE" > $tmp_out_file_name

rootdg_vol_file=`create_tmpfile rootdg`
grep rootdg /etc/mnttab | awk '{ print substr($1, index($1, "rootdg/") + 7, length($1) - (index($1, "rootdg/") + 6)) }' > $rootdg_vol_file

#Now we process the volume-group and other details
is_subvolume=0
cur_dg=""
next_cur_dg=""
cur_vol=""
dc_cnt=0
curvol_type=$RaidTypeSimple
pl_cnt=0
stripe=0
cur_vol_swap=0

sv_list=""
tmp_recent_curdg=`create_tmpfile reccurdg`
tmp_curdg_file=`create_tmpfile curdg`
tmp_curvol_file=`create_tmpfile curvol`
tmp_curvol_type_file=`create_tmpfile curvoltype`
tmp_curvol_len_file=`create_tmpfile curvollen`
tmp_curvol_swap_file=`create_tmpfile curvolswap`
tmp_next_curdg=`create_tmpfile nextcurdg`
tmp_stripe_file=`create_tmpfile isStripe`
tmp_dc_file=`create_tmpfile disk`
tmp_curvol_sv_cnt_file=`create_tmpfile svcnt`

sv_cnt=0
echo $sv_cnt > $tmp_curvol_sv_cnt_file
while read i
do
id=`echo $i | awk '{print $1}'`

if	[ "$id" = "dg" ]
then
	next_cur_dg=`echo $i | awk '{print $2}'`
	echo $next_cur_dg > $tmp_curdg_file
	id=""
	continue
fi

#Make a list of sub-volumes
#Count number of sub volumes if there are more that means 
#It must be a mirror volume
if	[ "$id" = "sv" ]
then
	sv_name=`echo $i | awk '{print $4}'`
	sv_list=`echo $sv_list $sv_name`
	sv_cnt=`expr $sv_cnt + 1`
	echo $sv_cnt > $tmp_curvol_sv_cnt_file
	continue
fi

if	[ "$id" = "v" ]
then
	#Ignore sub volumes.
	vname=`echo $i | awk '{print $2}'`
	if [ "$cur_dg" = "" ]
	then
		cur_dg=$next_cur_dg
	fi
	found=0
	for sv in $sv_list
	do
		if [ "$sv" = "$vname" ]
		then
			found=1
			break
		fi
	done
	if [ $found -eq 1 ]
	then
		is_subvolume=1
		continue
	fi
	is_subvolume=0
	sv_list=""

	sw=`echo $i | awk '{print $7}'`
	if [ "$sw" = "swap" ]
	then
		curvol_type=$RaidTypeSimple
	fi

	len=$tst_len
        tst_len=`echo $i | awk '{print $6}'`
        if [ "-" = "$tst_len" ] ; then
            continue
        fi

        tst_len=`echo "((512*$tst_len)/1024)" | bc`
        echo $tst_len > $tmp_curvol_len_file
	
	fs=0
	echo $vname > $tmp_curvol_file

	if	[ "$cur_vol" = "" ]
	then
		cur_vol=$vname
		id=""
		cur_dg=$next_cur_dg
		continue
	else
		if [ "$cur_dg" = "rootdg" ]
		then
			xvname="/dev/vx/dsk/"$cur_vol
			if [ -f $rootdg_vol_file ]
			then
			    rcnt=`grep -c -w $cur_vol $rootdg_vol_file`
			    if [ $rcnt -gt 0 ]
			    then
				xvname="/dev/vx/dsk/rootdg/"$cur_vol
			    fi
			fi
		else
			if [ "$cur_vol_swap" -eq 1 ]
			then
				xvname="/dev/vx/dsk/"$cur_vol
			else
				xvname=`echo "/dev/vx/dsk/"$cur_dg"/"$cur_vol`
			fi
		fi
		found=1
		if	[ "$dg_list" != "" ]
		then
			found=0
			for dg_ind in $dg_list
			do
				if	[ "$dg_ind" = "$cur_dg" ]
				then
					found=1
					break
				fi
			done
		fi 
    	if [ $curvol_type = $RaidTypeStripe ]
		then
			if [ $sv_cnt -gt 0 ]
			then
				curvol_type=$RaidMirAndStr
			fi
		fi
		sv_cnt=0
		vol_name=`echo $xvname | awk -F"/" '{ if ($NF <= 0) print $0; else { j = NF; print $j} }'`
		if	[ $found -eq 1 ]
		then
            volrid=`vxprint -m -g $cur_dg -v $vol_name 2>/dev/null | \
                     awk '$1 ~ /^rid=/ {print substr($1,index($1,"=")+1)}'`
            vgesmid=`grep "^dg $cur_dg " $tmp_file_name | awk '{print $6}'`
            volesmid=$vgesmid~$volrid
            volesmid=$ESM_VXVM_VOL_TAG~$volesmid

            echo $xvname"="$volesmid >> $vm_volume_map_file
            echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_VOLUME\",\"$ID_SRM_KEY_ID=$volesmid\",\c"
			echo "\"$ID_SRM_KEY_CAPACITY=$len\",\"$ID_SRM_KEY_FREESPACE=$fs\",\c"
			echo "\"$ID_SRM_KEY_HIERARCHY_TYPE=1\",\c"
			if [ "$cur_vol_swap" -eq 1 ]
			then
				echo "\"$ID_SRM_KEY_VOLUME_TYPE_DESC=SWAP_Volume\",\c"
                echo "\"$ID_SRM_KEY_VOLUME_TYPE=$RaidTypeSwap\",\c"
            else
                echo "\"$ID_SRM_KEY_VOLUME_TYPE=$curvol_type\",\c"
			fi
			echo "\"$ID_SRM_KEY_NAME=$hostedby::$VXVOL_PATH/$cur_dg/$vol_name\",\"$ID_SRM_KEY_DISK_CHUNK_COUNT=$dc_cnt\",\c"
            echo "\"$ID_SRM_KEY_VOLUME_MGR=$VERITAS_VXVM_ID\",;"

            # we have to put in the esm id for the disk chunks here    
			while read g
            do
                diskchk=`echo $g | cut -d',' -f2 | cut -d= -f2 | cut -d'"' -f1`
                esmid=$ESMID_VER"~"$ID_SRM_KEY_DISK_CHUNK"~"$volesmid"~"$diskchk
      
                echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_DISK_CHUNK\",\"$ID_SRM_KEY_ID=$esmid\",;"                            
            done < $tmp_dc_file
		fi
		dc_cnt=0	
		pl_cnt=0
   		stripe=0
   		echo $stripe > $tmp_stripe_file
		curvol_type=$RaidTypeSimple
        tmp_dc_file=`create_tmpfile disk`
		cur_vol=$vname
		echo $cur_dg > $tmp_recent_curdg      
		echo $next_cur_dg > $tmp_next_curdg 
		cur_dg=$next_cur_dg

		if [ "$sw" = "swap" ]
		then
			cur_vol_swap=1
		else
			cur_vol_swap=0
		fi
		echo $cur_vol_swap > $tmp_curvol_swap_file
	fi
fi

if	[ "$id" = "sd" ]
then
	if [ "$cur_vol" = "" ]
	then
		continue
	fi

	cname=`echo $i | awk '{print $2}'`
	tdname=`echo $i | awk '{print $8}'`
	if [ "%$tdname" = "%-" ]
	then
		continue
	fi

	# check to see if last 3 chars of cname = "-B0" for which we can ignore the entry.
	cname_chars=`echo -n "$cname" | wc -c | sed 's/ //g' `
	lastseg=`expr $cname_chars - 3`
	chkB0=`echo -n "$cname" | cut -b $lastseg-$cname_chars`
	if [  "$chkB0" != "-B0" ]         
	then
		echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_DISK_CHUNK\",\"$ID_SRM_KEY_ID=$cname\",;" >> $tmp_dc_file
		dc_cnt=`expr $dc_cnt + 1`
	else
		continue 
    fi	
	id=""
fi

if	[ "$id" = "pl" ]
then
if [ "$cur_vol" = "" ]
then
    continue
fi

if [ "$is_subvolume" -eq 1 ]
then
	continue
fi

layout=`echo $i | awk '{print $7}'`
pltype=`echo $i | awk '{print $5}'`
if [ "%$pltype" != "%ACTIVE" ]
then
	continue
fi
pl_cnt=`expr $pl_cnt + 1`
#fcopy=`echo $i | awk '{print $8}'`

if [ "$layout" = "CONCAT" ]
then
	if [ $pl_cnt -gt 1 ]
	then
		curvol_type=$RaidMirAndStr
	else
		curvol_type=$RaidTypeSpanning
	fi
fi

if [ "$layout" = "RAID" ]
then
	curvol_type=$RaidTypeRAID5
fi

if [ "$layout" = "STRIPE" ]
then
    curvol_type=$RaidTypeStripe
	stripe=1
	echo $stripe > $tmp_stripe_file
fi

echo $curvol_type > $tmp_curvol_type_file
fi

done < $tmp_out_file_name

#Dump the last volume only if we have ONE
xvname=`cat $tmp_curvol_file`
if [ "%$xvname" = "%" ]
then
	#Now cleanup the temp files 
    rm_tmpfile
	exit 0
fi

# account for the last disk group having no volumes
tmp_cur_dg=`cat $tmp_next_curdg`
cur_dg=`cat $tmp_curdg_file`
tst_dg=`cat $tmp_recent_curdg`

check_dg=`vxprint -g $tmp_cur_dg -vq | grep "^v  $xvname " 2>/dev/null`
if [ "$check_dg" != "" ]
then
    cur_dg=$tmp_cur_dg
else
        check_dg=`vxprint -g $tst_dg -vq | grep "^v  $xvname " 2>/dev/null`
	if [ "$check_dg" != "" ]
	then
		cur_dg=$tst_dg
	fi
fi

curvol_type=`cat $tmp_curvol_type_file`
cur_vol_swap=`cat $tmp_curvol_swap_file`
sv_cnt=`cat $tmp_curvol_sv_cnt_file`
stripe=`cat $tmp_stripe_file`
#if [ $curvol_type = $RaidTypeStripe ]
#then
#	 if [ $sv_cnt -gt 0 ]
#	then
#		curvol_type=$LAYERED_C
#	fi
#fi

len=`cat $tmp_curvol_len_file`

if [ "$cur_dg" = "rootdg" ]
then
    vname="/dev/vx/dsk/"$xvname
	if [ -f $rootdg_vol_file ]
	then
	    rcnt=`grep -c -w $xvname $rootdg_vol_file`
	    if [ $rcnt -gt 0 ]
	    then
		vname="/dev/vx/dsk/rootdg/"$xvname
	    fi
	fi
else
		vname=`echo "/dev/vx/dsk/"$cur_dg"/"$xvname`
fi


dc_cnt=`wc -l $tmp_dc_file | awk '{print $1}'`
vol_name=`echo $vname | awk -F"/" '{ if ($NF <= 0) print $0; else { j = NF; print $j} }'`

found=1
if	[ "$dg_list" != "" ]
then
	found=0
	for dg_ind in $dg_list
	do
		if	[ "$dg_ind" = "$cur_dg" ]
		then
			found=1
			break
		fi
	done
fi 

if	[ "$found" -eq 1 ]
then
    # Construct the esm id. Get the id's for volume group and volume
    volrid=`vxprint -m -g $cur_dg -v $vol_name 2>/dev/null | \
            awk '$1 ~ /^rid=/ {print substr($1,index($1,"=")+1)}'`

    vgesmid=`grep "^dg $cur_dg " $tmp_file_name | awk '{print $6}'`
    volesmid=$vgesmid~$volrid
    volesmid=$ESM_VXVM_VOL_TAG~$volesmid

    echo $vname"="$volesmid >> $vm_volume_map_file
        
    echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_VOLUME\",\"$ID_SRM_KEY_ID=$volesmid\",\c"
	echo "\"$ID_SRM_KEY_CAPACITY=$len\",\"$ID_SRM_KEY_FREESPACE=$fs\",\c"
	echo "\"$ID_SRM_KEY_HIERARCHY_TYPE=1\",\c"
	if [ "$cur_vol_swap" -eq 1 ]
	then
		echo "\"$ID_SRM_KEY_VOLUME_TYPE_DESC=SWAP_Volume\",\c"
        echo "\"$ID_SRM_KEY_VOLUME_TYPE=$RaidTypeSwap\",\c"
    else
        echo "\"$ID_SRM_KEY_VOLUME_TYPE=$curvol_type\",\c"
	fi
	echo "\"$ID_SRM_KEY_NAME=$hostedby::$VXVOL_PATH/$cur_dg/$vol_name\",\"$ID_SRM_KEY_DISK_CHUNK_COUNT=$dc_cnt\",\c"
        echo "\"$ID_SRM_KEY_VOLUME_MGR=$VERITAS_VXVM_ID\",;"
        # we have to put in the esm id for the disk chunks here    
        while read g
        do
            diskchk=`echo $g | cut -d',' -f2 | cut -d= -f2 | cut -d'"' -f1`
            esmid=$ESMID_VER"~"$ID_SRM_KEY_DISK_CHUNK"~"$volesmid"~"$diskchk
            
            echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_DISK_CHUNK\",\"$ID_SRM_KEY_ID=$esmid\",;"                            
        done < $tmp_dc_file
fi

#Now cleanup the temp files 
rm_tmpfile
exit 0
