#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,2018,2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# @(#)  7d4c34b 43haes/usr/sbin/cluster/cspoc/utilities/cl_ls_shared_vgs.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
###############################################################################
#
#   COMPONENT_NAME: CSPOC Command Line Interface
#
# Name:
#   cl_ls_shared_vgs
#
# Description:
#   Display a list of all the shared or concurrent volume groups known across the cluster.
#
# Arguments:
#   Flag -C or -c.   Default is '-C'.
#   -c means concurrent
#   -C means nonconcurrent
#   except that ECM in non-OAAN resource groups counts as nonconcurrent, and
#   ECM not in a resource group counts for both.
#
# Return Values:
#   Shared volume groups written to stdout, along with the owning resource
#   group and node list
#
################################################################################

PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"

[[ $VERBOSE_LOGGING == high ]] && set -x
[[ $VERBOSE_LOGGING == high ]] && version='1.17.1.2'

#
:	Parse options
#
header='false'
v_flag=""
oO_flag=""
r_flag=""
opt_str=':cCvnxfoOr'
while getopts $opt_str option
do
    case $option in
        c ) :   Look for all concurrent volume groups	;   type_flag="$type_flag -c"	;;
        C ) :   Look for all shared volume groups		;   type_flag="$type_flag -C"	;;
        x ) :   Look for nonconcurrent volume groups	;   type_flag="$type_flag -x"	;;
        O ) :   Look for CRITICAL volume groups		;   oO_flag="O"			;;
        o ) :   Look for non-CRITICAL volume groups		;   oO_flag="o"			;;
        n ) :   Skip header line				;   header='true'		;;
        f ) :   Look for force flag                         ;   type_flag="$type_flag -f"	;;
        v ) :   Only look at varyd on volume groups
            #
            v_flag='v'
            on_vg_list=$(/usr/es/sbin/cluster/utilities/cllsvg -v)
            if (( $? == 0 )); then
               on_vg_list=$(echo "$on_vg_list" | tr -s ' ' | cut -f2 -d' ')
               on_vg_list=$(print $on_vg_list)
            else
               on_vg_list=""
            fi
        ;;
        r ) :   Exclude repository volume group
            #
            r_flag="r"
            repository_pvids=$(/usr/es/sbin/cluster/utilities/clodmget -f repository -n HACMPsircol)
        ;;
        * ) :   Invalid option
            #
            export FPATH=/usr/es/sbin/cluster/cspoc
            nls_msg -2 4 26 "%s: Invalid option [%s].\n" "$PROGNAME" "$option"
        ;;
    esac
done

#
:	Set up some default values
#
if [[ -z $type_flag ]]
then
    #
    :   Default is non-concurrent
    #
    type_flag="-C"
fi

# Tells cllssharedvg to list volume groups known on two or more nodes.
# Change to '-s' to list all volume groups, including rootvg
# Leave off to require volume group be present on all nodes
type_flag="$type_flag -s"

no_rg_name='<'$(dspmsg -s 109 cspoc.cat 156 'Not in a Resource Group')'>'
#
:   Find all the nodes in the cluster
#
export ODMDIR=/etc/objrepos
NODE_LIST=$(/usr/es/sbin/cluster/utilities/clodmget -q 'object = COMMUNICATION_PATH' -f name -n HACMPnode | paste -s -d',' -)
if [[ -z $NODE_LIST ]]
then
    #
    :   If there are no nodes defined yet, we cannot determine shared VGs
    #
    dspmsg -s 21 cspoc.cat 6 "${PROGNAME}: The cluster does not appear to be configured - no nodes are defined. \n  Configure the cluster, nodes and networks then try this operation again.\n" $PROGNAME
    exit 1
fi
#
:   List all the shared volume groups, along with the owning resource group
#
/usr/es/sbin/cluster/utilities/cllssharedvg -n $NODE_LIST ${type_flag} >/tmp/.cspoc.cllssharedvg.$$
typeset -i rc=$?
if (( $rc != 0 )); then
    rm -f /tmp/.cspoc.cllssharedvg.$$
    exit $rc  # No need to continue. Some sort of error occurred.
fi

if [[ -f /tmp/.cspoc.cllssharedvg.$$ && -s /tmp/.cspoc.cllssharedvg.$$ ]]
then
    while read vg_name node_list pvid_list; do
        if [[ -n $vg_name ]]
        then
	    if [[ $vg_name == "rootvg" || $vg_name == "altinst_rootvg" || $vg_name == "caavg_private" || $vg_name == "old_rootvg" ]]
	    then
		continue
	    fi
            if [[ $v_flag != 'v' ||
                  $on_vg_list == @(?(* )$vg_name?( *)) ]]
            then
                #
                :   If we have a volume group name, find its owning resource group
                #
                rg_name=$(/usr/es/sbin/cluster/utilities/clodmget -q "name like '*VOLUME_GROUP' and value = $vg_name" -f group -n HACMPresource)
                if [[ -z $rg_name ]]
                then
                    #
                    :   If there is no owning resource group - perhaps because
                    :   the volume group has not been added to one yet - put out
                    :   the hopefully descriptive "<Not in a Resource Group>"
                    #
                    rg_name=$no_rg_name
                fi

                if [[ $oO_flag == 'O' ]]  # Looking for CRITICAL volume groups
                then
                    if [[ -z $(odmget -q "value = $vg_name and name = CRITICAL_VG" HACMPresource) ]]
                    then
                        #
                        :   Caller requested only CRITICAL volume groups,
                        :   and $vg_name is not so marked, so skip it
                        #
                        continue
                    fi

                elif [[ $oO_flag == 'o' ]]  # Looking for non-CRITICAL VGs
                then
                    #
                    :   The request for non-CRITICAL volume groups is
                    :   interpreted as asking for volume groups that
                    :   could be made CRITICAL.  That is, part of an
                    :   OAAN resource group, and not marked as CRITICAL.
                    #
                    if [[ $rg_name == $no_rg_name ]]
                    then
                        continue
                    fi
                    if [[ -z $(odmget -q "group = $rg_name and startup_pref = OAAN" HACMPgroup) ]]
                    then
                        #
                        :   Caller requested candidates for CRITICAL volume
                        :   groups, and $vg_name is not in an OAAN resource
                        :   group
                        #
                        continue
                    fi
                    if [[ -n $(odmget -q "value = $vg_name and name = CRITICAL_VG" HACMPresource) ]]
                    then
                        #
                        :   Caller requested only non-CRITICAL volume groups,
                        :   and $vg_name is critical, so skip it.
                        #
                        continue
                    fi
                fi
                if [[ $r_flag == 'r' ]]
                then
                    #
                    :	Wish to skip any repository volume groups
                    #
                    print $pvid_list | IFS=, read first_pvid rest_pvids
                    if [[ -z $rest_pvids ]]
                    then
                        repository_vg=""
                        for pvid in $repository_pvids
                        do
                            if [[ $first_pvid == ${pvid}* ]]
                            then
                                #
                                :   This particular VG consists of only a single
                                :   disk that is a repository disk.  Skip it.
                                #
                                repository_vg="true"
                                break
                            fi
                        done

                        if [[ $repository_vg == "true" ]]
                        then
                            continue
                        fi
                    fi
                fi
		if [[ $node_list != @(*,*) ]]
		then
		    #
		    :	This volume group is known on only a single node.
		    :	Make sure its not rootvg or something like that.
		    #
		    if [[ $rg_name == $no_rg_name ]]
		    then
			#
			:   Its not in a resource group - if it were, we 
			:   could handle it - check further to see if looks
			:   more like rootvg than not.
			#
			vg_attrs=$(cl_on_node -cspoc "-n $node_list" "lsattr -E -a auto_on,conc_capable -l $vg_name -F value" | cut -f2 -d' ' | paste -s -d',' - )
			if [[ $vg_attrs == 'y,n' ]]
			then
			    #
			    :	The volume group is auto varyon, and not concurrent capable.
			    :	This is not likely to be a volume group that PowerHA will
			    :	manage, so do not present it.
			    #
			    continue
			fi
		    fi
		fi

                if [[ $header != 'true' ]]
                then
                    #
                    :   The first time through, put out a header for the
                    :   volume group information
                    #
                    VG_header=$(dspmsg -s 109 cspoc.cat 120 'Volume Group')
                    integer VG_header_size=${#VG_header}
                    integer VG_name_len=15		    # LVM max VG name length
                    integer VG_header_print_size=$(( VG_header_size > VG_name_len ? VG_header_size : VG_name_len ))

                    RG_header=$(dspmsg -s 109 cspoc.cat 140 'Resource Group')
                    integer RG_header_size=${#RG_header}
                    integer RG_name_len=${#no_rg_name}	    # Due to limited space on SMIT panel
                    integer RG_header_print_size=$(( RG_header_size > RG_name_len ? RG_header_size : RG_name_len ))

                    NL_header=$(dspmsg -s 109 cspoc.cat 155 'Node List')

                    My_Lang=${LC_ALL:-$LANG}		    # LC_ALL overrides LANG
                    if [[ $My_Lang == C || $My_Lang == EN_US || \
                          $My_Lang == En_US || $My_Lang == en_US* ]]
                    then
                        #
                        :   Workable spacing for English
                        #
                        printf "#%-${VG_header_print_size}s %-${RG_header_print_size}s  %s\n" "$VG_header" "$RG_header" "$NL_header"
                    else
                        header=$(dspmsg -s 126 cspoc.cat 71 'Volume Group    Resource Group                 Node List\n')
                        printf '#%s\n' "$header"
                    fi
                    header='true'
                fi

                #
                :   Display the actual volume group information
                #
                printf " %-${VG_header_print_size}s %-${RG_header_print_size}s  %s\n" "$vg_name" "$rg_name" $node_list
            fi
        fi
    done </tmp/.cspoc.cllssharedvg.$$
fi
[[ -f /tmp/.cspoc.cllssharedvg.$$ ]] && rm -f /tmp/.cspoc.cllssharedvg.$$

if [[ $header != 'true' ]]
then
    if [[ $oO_flag == 'o' ]]
    then
        #
        :   In case no non critical volume groups were found, display proper warning
        #
        dspmsg -s 21 cspoc.cat 8 "${PROGNAME}: No shared volume groups were found that could be marked as CRITICAL.\n A volume group must be part of an OAAN resource group to be eligible to be mar
ked as CRITICAL\n" $PROGNAME

    elif [[ $oO_flag == 'O' ]]
    then
        #
        :   In case no critical volume groups were found, display proper warning
        #
        dspmsg -s 21 cspoc.cat 9 "${PROGNAME}: No critical volume groups found\n" $PROGNAME
    elif [[ $v_flag == 'v' && $on_vg_list == "" ]]
    then
        #
        :   In case no active volume groups were found, put out a message
        #
        dspmsg -s 21 cspoc.cat 10 "${PROGNAME}: No active volume groups found.\n" $PROGNAME    
    else
        #
        :   In case no shared volume groups were found, put out a message
        #
        dspmsg -s 21 cspoc.cat 7 "${PROGNAME}: No shared volume groups were found.\n Use CSPOC to configure shared volume groups first, then try this operation again.\n" $PROGNAME
    fi
    if [[ -z $CLMGR_PROGNAME || $CLMGR_PROGNAME != 'clmgr' ]]
    then
        exit 1
    else
        exit 0
    fi
fi
