#!/bin/sh 

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

. ./srm_keys.sh

ESMID_VER=1
VERITAS_VXVM_ID=1
ESM_VXVM_VOLGRP_TAG=$ESMID_VER~$ID_SRM_KEY_VOLUME_GROUP~veritas~vxvm~custom
ESM_VXVM_VOL_TAG=$ESMID_VER~$ID_SRM_KEY_VOLUME~veritas~vxvm~custom

tmp_file_name=/tmp/GetVeritasVgInfo.sh.$$
tmp_file_name_vx=/tmp/GetVeritasVxprintInfo.sh.$$
tmp_out_file_name=/tmp/GetVeritasVgInfo.sh.out.$$

tmp_vol_file=/tmp/volinfo.tmp.$$
tmp_disk_file=/tmp/disk.tmp.$$
tmp_curdg_file=/tmp/curdg.tmp.$$
tmp_dmsz_file=/tmp/curdg_dmsz.tmp.$$
tmp_curdgfree_file=/tmp/curdg_free.tmp.$$
tmp_found_dg_file=/tmp/GetVginfo.founddg.$$

tmp_file_scratch=/tmp/GetVgInfoScratch.$$ 
tmp_file_scratch2=/tmp/GetVgInfoScratch2.$$ 
tmp_file_err=/tmp/ESM_SVM_Err.$$

sid=$SRMAGT_SCAN_ID
rootdg_vol_file=/tmp/GetVxvmVgInfo.rootdg.$$


# strips the slice number of non-enclosure named disk names.
strip_slice()
{
#        tst1=`echo $1 | sed -n '/c[0-9].*t*d[0-9].*s[0-9]/p'`
#        tst2=`echo $1`
#        if [ "$tst1" = "" ]
#        then
#              disk_name=$tst2
#        else
  disk_name=$1
  if echo $1 | grep -s "s[0-9]$" > /dev/null; then
              tst1=`echo "$1" | sed 's/s[0-9]$//'`
              disk_name=$tst1
        fi
}

dg_list=$@
found_dg_list=""

rm -f $tmp_file_name_vx
touch $tmp_file_name_vx

vxprint -htdvsGqQ > $tmp_file_name_vx 2>/dev/null
if [ $? -ne 0 ]; then
    # no volume groups, quit
    rm -f $tmp_file_name_vx
    exit 0
fi

rm -f $tmp_file_scratch
touch $tmp_file_scratch

rm -f $tmp_file_scratch2
touch $tmp_file_scratch2

rm -f $tmp_found_dg_file
touch $tmp_found_dg_file

rm -f $tmp_file_err
touch $tmp_file_err

rm -f $tmp_file_name
touch $tmp_file_name


grep "^[dm,dg,sd,v]" $tmp_file_name_vx | grep -v "PRIVATE" > $tmp_out_file_name

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
set cur_dg=""
set cur_vol=""
sv_list=""
disk_cnt=0
dm_sz=0
dg_free=0
vol_cnt=0

rm -f $tmp_vol_file
touch $tmp_vol_file

rm -f $tmp_disk_file
touch $tmp_disk_file

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

    ##############################
    # See if we have a Disk Group
    if    [ "$id" = "dg" ]
    then
        sv_list=""
        tmp_cur_dg=`echo $i | awk '{print $2}'`
        rm -f $tmp_curdg_file
        echo $tmp_cur_dg > $tmp_curdg_file
    
        dg_free=`expr $dm_sz - $dg_free`
    
        if [ "$cur_dg" = "" ]
        then
            cur_dg=$tmp_cur_dg
        else
            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
                # Get the esm id for the volume group
                vgesmid=`grep "^dg $cur_dg " $tmp_file_name_vx | awk '{print $6}'`
                vgesmid=$ESM_VXVM_VOLGRP_TAG~$vgesmid
                vgesmname=`./GetESMIdInfo.sh $ID_SRM_KEY_HOSTNAME`::$cur_dg
    
                echo $cur_dg >> $tmp_found_dg_file
                echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_VOLUME_GROUP\",\"$ID_SRM_KEY_ID=$vgesmid\",\c"
                echo "\"$ID_SRM_KEY_VOLUME_MGR=$VERITAS_VXVM_ID\",\"$ID_SRM_KEY_NAME=$vgesmname\",\c"
                echo "\"$ID_SRM_KEY_STATUS=1\",\"$ID_SRM_KEY_STATUS_DESC=\",\c"
                echo "\"$ID_SRM_KEY_CAPACITY=$dm_sz\",\"$ID_SRM_KEY_FREESPACE=$dg_free\",\c"
                echo "\"$ID_SRM_KEY_VOLUME_COUNT=$vol_cnt\",\"$ID_SRM_KEY_DISK_COUNT=$disk_cnt\",;"
    
                cat $tmp_disk_file
                cat $tmp_vol_file
            fi
            disk_cnt=0
            vol_cnt=0
            dm_sz=0
            dg_free=0
            rm -f $tmp_curdgfree_file
            echo $dg_free > $tmp_curdgfree_file
            cur_dg=$tmp_cur_dg
            rm -f $tmp_vol_file
            touch $tmp_vol_file
            rm -f $tmp_disk_file
            touch $tmp_disk_file
        fi
        id=""
        continue
    fi


    ####################################
    # See if we have a Disk Media Record
    if    [ "$id" = "dm" ]
    then
        did=`echo $i | awk '{print $3}'`
        strip_slice $did
        did=$disk_name
        if [ "%$did" = "%-" ]
        then
            continue
        fi


        dskesmid=`./GetESMGiduInfo.sh $did`
        if [ $? -ne 0 ]
        then
            # there was an error
            echo $dskesmid >> $tmp_file_err
        else
            echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_DISK\",\"$ID_SRM_KEY_ID=$dskesmid\",;" >> $tmp_disk_file
        fi

        disk_cnt=`expr $disk_cnt + 1`
        sz=`echo $i | awk '{print $6}'`
        psz=`echo $i | awk '{print $5}'`

        if [ "-" = "$sz" ] ; then
            continue
        fi
        if [ "-" = "$psz" ] ; then
            continue
        fi
        if [ "-" = "$dm_sz" ] ; then
            continue
        fi

        sz=`echo "((512*($sz+$psz))/1024)" | bc`
        dm_sz=`expr $dm_sz + $sz`
        rm -f $tmp_dmsz_file
        echo $dm_sz > $tmp_dmsz_file
    fi


    ####################################
    # See if we have a Sub Volume
    # Make a list of sub-volumes
    if    [ "$id" = "sv" ]
    then
        sv_name=`echo $i | awk '{print $4}'`
        sv_list=`echo $sv_list $sv_name`
        continue
    fi


    ####################################
    # See if we have a Volume
    if    [ "$id" = "v" ]
    then
        #Ignore sub volumes.
        xvname=`echo $i | awk '{print $2}'`
        found=0
        for sv in $sv_list
        do
            if [ "$sv" = "$xvname" ]
            then
                found=1
                break
            fi
        done
        if [ $found -eq 1 ]
        then
            continue
        fi
    
        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
    
            # Get the esm id for the volume group
            volrid=`vxprint -m -g $cur_dg -v $xvname 2>/dev/null | \
                    awk '$1 ~ /^rid=/ {print substr($1,index($1,"=")+1)}'`
                
            vgesmid=`grep "^dg $cur_dg " $tmp_file_name_vx | awk '{print $6}'`
            volesmid=$vgesmid~$volrid
            volesmid=$ESM_VXVM_VOL_TAG~$volesmid
    
        echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_VOLUME\",\"$ID_SRM_KEY_ID=$volesmid\",;" >> $tmp_vol_file
        vol_cnt=`expr $vol_cnt + 1`
    fi


    ####################################
    # See if we have a SubDisk
    if    [ "$id" = "sd" ]
    then
        sd_len=`echo $i | awk '{print $6}'`
        if [ "-" = "$sd_len" ] ; then
            continue
        fi

        sd_len=`echo "((512*$sd_len)/1024)" | bc`
        dg_free=`expr $dg_free + $sd_len`
        rm -f $tmp_curdgfree_file
        echo $dg_free > $tmp_curdgfree_file
    fi

    id=""
done < $tmp_out_file_name



#I dont know why I need to set the vars again?
cur_dg=`cat $tmp_curdg_file`
vol_cnt=`wc -l $tmp_vol_file | awk '{print $1}'`
disk_cnt=`wc -l $tmp_disk_file | awk '{print $1}'`
dm_sz=`cat $tmp_dmsz_file`

dg_free=`cat $tmp_curdgfree_file`
if [ "$vol_cnt" = "0" ]
then 
    dg_free=$dm_sz
else
    dg_free=`expr $dm_sz - $dg_free`
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    [ $found -eq 1 ]
then
        # Get the esm id for the volume group
        vgesmid=`grep "^dg $cur_dg " $tmp_file_name_vx | awk '{print $6}'`
        vgesmid=$ESM_VXVM_VOLGRP_TAG~$vgesmid
        vgesmname=`./GetESMIdInfo.sh $ID_SRM_KEY_HOSTNAME`::$cur_dg

        if [ $dg_free -lt 0 ]; then
            # hack to avoid neg free space....
            dg_free=$dm_sz
        fi


    echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_VOLUME_GROUP\",\"$ID_SRM_KEY_ID=$vgesmid\",\c"
    echo "\"$ID_SRM_KEY_VOLUME_MGR=$VERITAS_VXVM_ID\",\"$ID_SRM_KEY_NAME=$vgesmname\",\c"
    echo "\"$ID_SRM_KEY_STATUS=1\",\"$ID_SRM_KEY_STATUS_DESC=\",\c"
    echo "\"$ID_SRM_KEY_CAPACITY=$dm_sz\",\"$ID_SRM_KEY_FREESPACE=$dg_free\",\c"
    echo "\"$ID_SRM_KEY_VOLUME_COUNT=$vol_cnt\",\"$ID_SRM_KEY_DISK_COUNT=$disk_cnt\",;"

    cat $tmp_disk_file
    cat $tmp_vol_file
fi

# Check the disks that we could not find and try and fine a DG details 
# Using vxdisk
found_dg_list=`cat $tmp_found_dg_file`
rm -f $tmp_file_name
touch $tmp_file_name


vxdisk -o alldgs list | grep -v "^DEVICE" >> $tmp_file_name
for i in $dg_list
do
if [ ! -z "$found_dg_list" ]
then
    cnt=`echo $found_disk_list | grep -c $i`
    if [ $cnt -ne 0 ]
    then
        continue
    fi
fi

cnt=`grep -c "($i)" $tmp_file_name`
if [ $cnt -eq 0 ]
then
    continue
fi

# Get the esm id for the volume group
vgesmid=`grep "^dg $cur_dg " $tmp_file_name_vx | awk '{print $6}'`
vgesmid=$ESM_VXVM_VOLGRP_TAG~$vgesmid
vgesmname=`./GetESMIdInfo.sh $ID_SRM_KEY_HOSTNAME`::$i

echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_VOLUME_GROUP\",\"$ID_SRM_KEY_ID=$vgesmid\",\c"
echo "\"$ID_SRM_KEY_VOLUME_MGR=$VERITAS_VXVM_ID\",\"$ID_SRM_KEY_NAME=$vgesmname\",\c"
echo "\"$ID_SRM_KEY_STATUS=1\",\"$ID_SRM_KEY_STATUS_DESC=\",\c"
echo "\"$ID_SRM_KEY_DISK_COUNT=$cnt\",;"

for j in `fgrep "($i)" $tmp_file_name | awk '{printf("%s\n", $1)}'`
do
        if [ "-" = "$j" ] ; then
            continue
        fi
     strip_slice $j
    did=$disk_name

        dskesmid=`./GetESMGiduInfo.sh $did`
        if [ $? -ne 0 ]
        then
            # there was an error
            echo $dskesmid >> $tmp_file_err
        else    
            echo "\"$ID_SRM_KEY_TYPE=$ID_SRM_KEY_DISK\",\"$ID_SRM_KEY_ID=$dskesmid\",;"
        fi
done

done

cat $tmp_file_err


#Now cleanup the temp files 
rm -f $tmp_vol_file
rm -f $tmp_disk_file
rm -f $tmp_curdg_file 
rm -f $tmp_dmsz_file

rm -f $tmp_file_name
rm -f $tmp_file_name_vx
rm -f $tmp_out_file_name
rm -f $tmp_curdgfree_file
rm -f $tmp_found_dg_file
rm -f $tmp_file_scratch2
rm -f $tmp_file_scratch
rm -f $tmp_file_err
rm -f $rootdg_vol_file

exit 0
