#!/bin/ksh93 
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/cspoc/utilities/cl_vg_non_dhb_disks.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)09        1.3 src/43haes/usr/sbin/cluster/cspoc/utilities/cl_vg_non_dhb_disks.sh, hacmp.cspoc, 61haes_r714 10/24/07 14:44:17
###############################################################################
#
#   COMPONENT_NAME: CSPOC Command Line Interface
#
# Name:
#   cl_vg_non_dhb_disks
#
# Description:
#   Given the name of a locally vary'd on volume group, return the disks which
#   are not currently in use for disk heart beating.   Optionally, for those
#   disks which are in use, return the name of the disk heart beat network
#   they support.
#
# Arguments:
#   Volume group name, assumed vary'd on the current node.
#
# Return Values:
#   Available disks written to stdout in the form
#	hdiskname:pvid
#
################################################################################

PROGNAME=${0##*/}

export PATH=$(/usr/es/sbin/cluster/utilities/cl_get_path all)

[[ $VERBOSE_LOGGING == high ]] && set -x
[[ $VERBOSE_LOGGING == high ]] && version="1.3"

HA_DIR=es

#
:   Check for additional display requested
#
if [[ $1 == '-d' ]] 
then
    D_flag=true
    shift
else
    D_flag=false
fi

#
:   Pick up the given volume group name, $1
#
VG_name=$1

LOCALNODENAME=${LOCALNODENAME:-$(get_local_nodename)}

#
:   Generate lspv output, colon separated for easier parsing, and to match the
:   desired final output
#
PV_list=$(lspv | sed 's/  */:/g')

#
:   Find all the disks and logical volumes used for MNDHB in volume group $VG_name
#
LV_list=$(lsvg -lL $VG_name | tail +3 | cut -f1 -d' ')

for LV in $LV_list ; do
    #
    :	Find any MNDHB network on $LV
    #
    mndhb_network=$(odmget -q "identifier = /dev/$LV and type = diskhbmulti" HACMPadapter)
    if [[ -n $mndhb_network ]]
    then
	#
	:   Remember that this one is in use
	#
	hdisk_name=$(lslv -lL $LV | tail -1 | cut -f1 -d' ')
	hdisk_pvid=$(lspv | sed 's/  */:/g' | grep "$hdisk_name:" | cut -d: -f2)
	hdisk_net_name=$(print "$mndhb_network" | sed -n '/network =/s/^.* "\(.*\)".*/\1/p' | head -1)
	in_use_hdisk_list="$in_use_hdisk_list\n$hdisk_name $hdisk_pvid $hdisk_net_name"
    fi
done

#
:   Find all the disks used for two-node disk heart beat in volume group $VG_name
#
VG_disk_list=$(print "$PV_list" | grep  ":${VG_name}:" )

for hdisk_line in $VG_disk_list ; do
    #
    :	Find any two node disk heart beat network on $hdisk_name
    #
    hdisk_name=$(print $hdisk_line | cut -f1 -d':')
    hdisk_pvid=$(lspv | sed 's/  */:/g' | grep "$hdisk_name:" | cut -d: -f2)
    two_Node_network=$(odmget -q "nodename = $LOCALNODENAME and type = diskhb and interfacename like '*${hdisk_name}'" HACMPadapter)
    if [[ -n $two_node_network ]]
    then
	#
	:   Remember that this one is in use
	#
	hdisk_net_name=$(print "$two_node_network" | sed -n '/network =/s/^.* "\(.*\)".*/\1/p' | head -1)
	in_use_hdisk_list="$in_use_hdisk_list\n$hdisk_name $hdisk_pvid $hdisk_net_name"
    fi
done

#
:   Output any disks that were not found to be in use for two node or MNDHB
#
if [[ -z $in_use_hdisk_list && -n $VG_disk_list ]] 
then
    #
    :	No disks are in use - all are available
    #
    if [[ $D_flag == true ]]
    then
	print "# The following hdisks are available: "
    fi
    #
    :	Format of the output is "hdiskname:pvid"
    #
    print "$VG_disk_list" | cut -f1,2 -d':'

else
    #
    :	Some disks are in use.  Print the list of disks that are not
    #
    in_use_disk_pvids=$(print "$in_use_hdisk_list" | cut -f2 -d' ' | paste -s -d' ' - )
    header=false

    for hdisk_line in $VG_disk_list ; do
        hdisk_pvid=$(print $hdisk_line | cut -f2 -d':')
	#
	: Check each disk in the given volume group $VG_name to 
        : see if it is in use
	#
	if [[ $in_use_disk_pvids != ?(* )$hdisk_pvid?( *) ]]
	then
	    if [[ $header == false && $D_flag == true ]] 
	    then
		print "# The following hdisks are available: "
		header=true
	    fi
	    #
	    :	Show the disk name and PVID for this free disk.  Format is
	    :	"hdiskname:pvid"
	    #
	    print $hdisk_line | cut -f1,2 -d':'
	fi
    done

    #
    :	Does the caller want to know about the unavailable ones?
    #
    if [[ $D_flag == true && -n $in_use_hdisk_list ]]
    then
        #
        : Now, list the ones that were not available, and their owning network
        #
        print "# The following hdisks are not available: "
        print "# hdisk  network "
        print "$in_use_hdisk_list" | while read hdisk pvid network ; do
            print "# $hdisk $network"
        done
    fi
fi 
