#!/bin/ksh93 # ALTRAN_PROLOG_BEGIN_TAG # This is an automatically generated prolog. # # Copyright (C) Altran ACT S.A.S. 2018,2019,2021. All rights reserved. # # ALTRAN_PROLOG_END_TAG # # IBM_PROLOG_BEGIN_TAG # This is an automatically generated prolog. # # 61haes_r720 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_get_interface_attributes.sh 1.14 # # Licensed Materials - Property of IBM # # Restricted Materials of IBM # # COPYRIGHT International Business Machines Corp. 2006,2015 # 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 # @(#) 7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_get_interface_attributes.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM #============================================================================ # # Name: KLIB_HACMP_get_interface_attributes # # Description: This is the main, FPATH function that is invoked by clmgr # to collect interface information. It invokes various # network related utilities, at both the SystemMirror and # AIX levels, and mines their output for all relevant info. # Special handling is required for service interfaces, since # it is important to pull the interface data for them from # the node they are currently active on, and not just use # local node. # # Inputs: See the "devDoc()" function at the bottom of this file. # # Outputs: The properties hash is populated. The only other outputs are # any error messages that might be needed. # # Returns: Zero if no errors are detected. Otherwise, an appropriate # non-zero value is returned. Refer to the "RETURN" section # of the "devDoc()" function at the bottom of this file for # the standard return code values/meanings for clmgr. # #============================================================================ function KLIB_HACMP_get_interface_attributes { . $HALIBROOT/log_entry "$0()" "$CL" : version=@(#) 7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_get_interface_attributes.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM : INPUTS: $* typeset interface=${1//\"/} # Get the "properties" associative array reference, and initialize it if [[ -z $2 ]] || [[ -n $CLMGR_LOGGING && $2 == "|" ]]; then interface= typeset -n properties=$1 else typeset -n properties=$2 fi [[ $CLMGR_LOGGING == 'med' ]] && set +x # Only trace param values #=================================== : Declare and initialize variables #=================================== for k in ${!properties[*]}; do unset properties[$k]; done typeset -i INDEX=0 rc=$RC_SUCCESS typeset key= INTERFACES= typeset -A attributes typeset -i matches=0 typeset -u attr= uc_key= typeset -A queries typeset -i i=0 typeset TEMPFILE="$TMPDIR/clmgr.KHgifa.cllsif.pcS" typeset LAST_IF_NODE="" : This ensures that the attributes displayed in queries are : identical to those used for add and modify operations. typeset -A ATTRMAP ATTRMAP=( [INTERFACENAME]="INTERFACE" ) #=================================================== : Check for any specified attribute=value pairs. : If any are found, use those to query the output. #=================================================== while (( $# > 0 )); do value="$1" if [[ $value == *=* ]]; then attr=${value%%=*} # "attr" makes this uppercase queries[$attr]="${value#*=}" fi shift done #================================================================ : Check for a defined cluster. No need to continue without one. #================================================================ CL=$LINENO isClusterDefined if (( $? != RC_SUCCESS )); then rc=$RC_MISSING_DEPENDENCY #======================================= : Make sure we have something to query #======================================= elif [[ -n $interface ]]; then [[ $interface != *,* ]] && unset INDEX if [[ $CLMGR_STACK == *KLIB_HACMP_get_service_ip_attributes* ]] then : No need to validate, since these interfaces were passed in : from the query service IP code, and are already validated. INTERFACES=${interface//,/ } else for interface in ${interface//,/ }; do CL=$LINENO KLIB_HACMP_is_known_interface $interface 2>>$CLMGR_TMPLOG if (( $? != RC_SUCCESS )); then if [[ $CLMGR_PROGNAME == "clmgr" ]]; then dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 '\nERROR: "%1$s" does not appear to exist!\n\n' "$interface" 1>&2 dspmsg -s $CLMGR_SET $CLMGR_MSGS 146 "Available Interfaces:\n\n" 1>&2 typeset available CL=$LINENO KLIB_HACMP_list_interfaces available for (( i=0; i<${#available[*]}; i++ )); do if [[ ${available[$i]} != *([[:space:]]) ]]; then print -u2 "\t${available[$i]}" fi done print -u2 "" fi rc=$RC_NOT_FOUND else INTERFACES="$INTERFACES $interface" fi done fi elif (( CLMGR_VERBOSE || ${#queries[*]} > 0 )); then print "$0()[$LINENO]($SECONDS): cllsif -pcS" >>$CLMGR_TMPLOG cllsif -pcS 2>>$CLMGR_TMPLOG |\ while read line do if [[ " $INTERFACES " != *\ ${line%%:*}\ * ]]; then INTERFACES="$INTERFACES ${line%%:*}" fi done else if [[ $CLMGR_PROGNAME == "clmgr" ]]; then dspmsg -s $CLMGR_SET $CLMGR_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2 fi rc=$RC_MISSING_INPUT fi (( $rc != RC_SUCCESS )) && unset INTERFACES typeset -A IFs NETSTAT IFCONFIG if [[ -z $CL_SNAPSHOT ]] then if [[ -s $TMPDIR/clmgr.KHgia.dat ]] then : Load the cached data eval "$(cat $TMPDIR/clmgr.KHgia.dat)" fi #=============================================================== # Collect some interface population data that can be used to # fill in the missing interface data for persistent/service # IPs. The cl_harvestIP_scripts only returns non-transient # data, from HACMPadapter. Since SIs/PIs are transient, can # be moved from interface to interface, they are not stored # in HACMPadapter. #=============================================================== if [[ -z ${!IFs[*]} ]] then : Adding interface data to the clmgr interface cache CL=$LINENO get_interfaces IFs for KEY in ${!IFs[*]} do print "IFs[$KEY]=\"${IFs[$KEY]}\"" >> $TMPDIR/clmgr.KHgia.dat done else : Loaded the interface data from the cache fi LAST_IF_NODE=$LOCAL_NODE if [[ ! -s $TMPDIR/clmgr.KHgifa.cllsif.pcS ]] then print "$0()[$LINENO]($SECONDS): cllsif -pcS" >>$CLMGR_TMPLOG : Caching the interface data cllsif -pcS >$TEMPFILE 2>>$CLMGR_TMPLOG print "$0()[$LINENO]($SECONDS): cllsif RC: $?" >>$CLMGR_TMPLOG else : The interface data has already been cached fi else touch $TEMPFILE touch $TMPDIR/clmgr.KHgia.dat fi #========================================================================= # Reset this variable in the ksh93 namespace table, which can sometimes # get scrambled when this routine is called during a cluster HTML report # generation. This is a workaround that resolves the problems that were # encountered. #========================================================================= unset LINE typeset LINE="" #================================================================ : Populate the return hash with the retrieved attributes/values #================================================================ for interface in $INTERFACES; do #===================================================== : Make sure to use the label of the interface, since : it might have been passed in as an IP address. #===================================================== while read LINE; do [[ $LINE != $interface:* ]] && continue : Handle possible IPv6 address typeset IPv6= if [[ $LINE == *\[* ]]; then IPv6=${LINE##*\[} IPv6=${IPv6%%\]*} LINE=${LINE/\[$IPv6\]/[${IPv6//:/_}]} fi print -- "$LINE" | IFS=: read name type current_network nettype \ attr nodename ipaddr haddr ifname \ globalname netmask hb_addr site \ prefix family rem if [[ $type == serv* && -z ${properties[NODE$INDEX]} ]]; then #======================================================== : Since a service IP can be on one of many nodes, it : is important to pull the data from the active node. : If no node is active, the IP will be labeled as down. #======================================================== typeset rg="" print "$0()[$LINENO]($SECONDS): clodmget -n -q \"value=$interface AND name=SERVICE_LABEL\" -f group HACMPresource" >>$CLMGR_TMPLOG rg=$(clodmget -n -q "value=$interface AND name=SERVICE_LABEL" -f group HACMPresource) print "$0()[$LINENO]($SECONDS): clodmget RC: $?; rg == \"$rg\"" >>$CLMGR_TMPLOG if [[ -n $rg ]]; then # : If RG_LOG is defined, it means this was invoked from some : clmgr code, like KLIB_HACMP_get_cluster_attributes. Used : as a performance optimization. # if [[ -n $RG_LOG && -s $RG_LOG ]]; then typeset CN=$(grep -p "NAME=\"$rg\"" $RG_LOG | grep "^CURRENT_NODE=") CN=${CN#*=} CN=${CN//\"/} if [[ -n $CN ]]; then [[ $nodename != $CN ]] && continue properties[NODE$INDEX]=$CN fi else typeset ND="" ST="" CN="" REM="" print "$0()[$LINENO]($SECONDS): LC_ALL=C clRGinfo $rg | grep -w ONLINE | read ND ST CN REM" >>$CLMGR_TMPLOG LC_ALL=C clRGinfo $rg 2>>$CLMGR_TMPLOG | grep -w ONLINE | read ND ST CN REM print "$0()[$LINENO]($SECONDS): clRGinfo CN == \"$CN\"" >>$CLMGR_TMPLOG if [[ $CN == "SECONDARY" ]] then CN=${REM%%[[:space:]]} fi properties[NODE$INDEX]=$CN fi fi unset rg fi : Handle possible IPv6 address ipaddr=${ipaddr//_/:} ipaddr=${ipaddr/\[/} ipaddr=${ipaddr/\]/} if [[ $interface == $name ]]; then interface=$name if [[ -n $IPv6 ]]; then properties[PREFIX$INDEX]=$prefix else properties[NETMASK$INDEX]=$netmask fi break elif [[ $interface == $ipaddr ]]; then interface=$ipaddr if [[ -n $IPv6 ]]; then properties[PREFIX$INDEX]=$prefix else properties[NETMASK$INDEX]=$netmask fi break fi done <$TEMPFILE [[ -z $interface ]] && continue if [[ -n $CL_SNAPSHOT ]] then # # NOTE: Initially I used "grep -p" here for paragraph extraction of # the ODM stanzas, but it was really buggy for some reason I # could not discover. I was forced to use this snippet of PERL # instead to get it to work reliably, and it is equivalent to # the "grep -p" I had tried before. As a hint for future # maintenance, the key to this code is the setting of the # special perl variable, "$/". # properties[NETMASK$INDEX]=$(perl -e "local \$/ = ''; open my \$fh, '<', \"$SNAPSHOTPATH/$CL_SNAPSHOT.odm\"; while (<\$fh>) { print \"\$_\" if (\$_ =~ /(^|\\B)HACMPadapter:(\\B|\$)/ && \$_ =~ /\"$interface\"/); } close \$fh;" | grep -w netmask | cut -f2 -d \") fi typeset f="" fields="" IF_DATA="" integer index=0 cmd_rc=$RC_UNKNOWN print "$0()[$LINENO]($SECONDS): cl_harvestIP_scripts -u $interface -J\|" >>$CLMGR_TMPLOG IF_DATA=$(cl_harvestIP_scripts -u $interface -J\| 2>>$CLMGR_TMPLOG) cmd_rc=$? print "$0()[$LINENO]($SECONDS): cl_harvestIP_scripts RC: $cmd_rc" >>$CLMGR_TMPLOG if (( $cmd_rc == RC_SUCCESS )); then print -- "$IF_DATA" |\ while read LINE; do if [[ $LINE == \#* ]]; then LINE=${LINE//\#/} for f in ${LINE//\|/ }; do fields[$index]=$f (( index++ )) done else index=0 IFS=\| for f in $LINE; do uc_key=${fields[$index]} (( index++ )) [[ -n ${ATTRMAP[$uc_key]} ]] && uc_key=${ATTRMAP[$uc_key]} if [[ $uc_key == NODE && ${properties[TYPE$INDEX]} == service ]] then #=================================================== : The current node for a service IP, if any, is set : earlier in this script. Do not overwrite it here. #=================================================== continue fi properties[$uc_key$INDEX]=$f done IFS="$ORIG_IFS" fi done fi if [[ -z $CL_SNAPSHOT ]] then #============================================================== : Determine the current status, plus its assigned MAC address #============================================================== if [[ -z ${NETSTAT[_${nodename//-/_}]} || -z ${IFCONFIG[_${nodename//-/_}]} ]] then if [[ $nodename == $LOCAL_NODE ]]; then NETSTAT[_${nodename//-/_}]=$(netstat -i) IFCONFIG[_${nodename//-/_}]=$(ifconfig -lu) else NETSTAT[_${nodename//-/_}]=$($CLRSH $nodename netstat -i) IFCONFIG[_${nodename//-/_}]=$($CLRSH $nodename ifconfig -lu) fi # : Writing the netstat and ifconfig data for node $nodename to a cache file # print "NETSTAT[_${nodename//-/_}]=\"${NETSTAT[_${nodename//-/_}]=}\"" >> $TMPDIR/clmgr.KHgia.dat print "IFCONFIG[_${nodename//-/_}]=\"${IFCONFIG[_${nodename//-/_}]=}\"" >> $TMPDIR/clmgr.KHgia.dat fi typeset IFDVC="" MTU="" NW="" REST="" typeset -u HWADDR="" FORMATTED_ADDR="" DATA="" print -- "${NETSTAT[_${nodename//-/_}]}" |\ grep -w "$interface" | read IFDVC MTU NW HWADDR REST if [[ -n $IFDVC ]] then print -- "${NETSTAT[_${nodename//-/_}]}" |\ grep "$IFDVC " | grep -vw "$interface" |\ read IFDVC MTU NW HWADDR REST for DATA in ${HWADDR//\./ }; do (( ${#DATA} == 1 )) && DATA="0$DATA" [[ -n $FORMATTED_ADDR ]] && FORMATTED_ADDR="$FORMATTED_ADDR:" FORMATTED_ADDR="$FORMATTED_ADDR$DATA" done properties[HWADDR$INDEX]=$FORMATTED_ADDR print -- "${IFCONFIG[_${nodename//-/_}]}" | grep -qw "$IFDVC" (( $? == 0 )) && properties[STATE$INDEX]="UP" || \ properties[STATE$INDEX]="DOWN" else : Likely a service IP, $interface, that is not currently in use properties[STATE$INDEX]="DOWN" fi unset IFDVC MTU NW HWADDR REST fi if [[ -z $CL_SNAPSHOT ]]; then #========================================================= : Attempt to ensure that the interface data is populated #========================================================= if [[ ${properties[TYPE$INDEX]} == @(service|persistent) && \ ${properties[NODE$INDEX]} != $LOCAL_NODE && \ $LAST_IF_NODE != ${properties[NODE$INDEX]} ]] then : Make sure to retrieve the interface info from the correct node CL=$LINENO get_interfaces IFs ${properties[NODE$INDEX]} LAST_IF_NODE=${properties[NODE$INDEX]} elif [[ -n $LAST_IF_NODE && $LAST_IF_NODE != $LOCAL_NODE ]] then : Need to reload the local interface information CL=$LINENO get_interfaces IFs LAST_IF_NODE=$LOCAL_NODE fi if [[ -z ${properties[INTERFACE$INDEX]} ]]; then typeset intf= for intf in ${!IFs[*]}; do if [[ " ${IFs[$intf]} " == *\ $ipaddr\ * ]]; then properties[INTERFACE$INDEX]=$intf break fi done fi fi #============================================================== : Even if there is no value for a given attribute, we want to : display a placeholder for it, so the customer always sees a : consistent, complete set of attributes. #============================================================== for key in ${_COLON_ATTR_ORDER[interface]}; do if [[ -z ${properties[$key$INDEX]} ]]; then properties[$key$INDEX]="" fi done #====================================================================== : Only display NETMASK for IPv4 addresses. Otherwise, display PREFIX. #====================================================================== if [[ ${properties[IPADDR$INDEX]} == *:* ]]; then unset properties[NETMASK$INDEX] else unset properties[PREFIX$INDEX] fi #================================================== : If this is not an Enterprise Edition cluster, : then remove all Enterprise-specific attributes. #================================================== CL=$LINENO isEnterprise (( $? != 1 )) && unset properties[SITENAME$INDEX] if [[ -n $CL_SNAPSHOT ]] then # : Remove the attributes that cannot be obtained from a snapshot # Meaning, they require a live cluster. # if [[ -z $CLMGR_ATTRS ]]; then for key in STATE HWADDR do unset properties[$key$INDEX] done fi fi (( INDEX++ )) [[ $INDEX == *0 ]] && (( INDEX++ )) done # End of the interfaces loop #========================================================== : If any searches/filters were specified, handle them now #========================================================== if (( $rc == RC_SUCCESS && ${#queries[*]} > 0 )); then CL=$LINENO search_properties queries properties rc=$? fi (( $rc == RC_SUCCESS )) && CL=$LINENO prune_indexes properties if [[ $CLMGR_STACK != *KLIB_HACMP_get_service_ip_attributes* ]] then rm -f $TMPDIR/clmgr.KHgia.dat rm -f $TEMPFILE fi log_return_msg "$rc" "$0()" "$LINENO" return $? } # End of "KLIB_HACMP_get_interface_attributes()" #============================================================================ # # Name: devDoc # # Description: This is a never-to-be-called, wrapper function that all the # clmgr FPATH functions implement in order to hide embedded # syntax from trace logging. This information is implemented # in POD format, and can be viewed in a number of ways using # POD tools. Some viewing suggestions for this function's POD- # formatted information are: # # perldoc # pod2text -c # pod2text -c --code # pod2html # # However, the more important use for this information is that # it is parsed by clmgr to display the syntax for this file's # operation. The information in the "SYNOPSIS" section is used # for this purpose. This feature was originally implemented # using the man page information. However, in a code review it # was pointed out that this approach had to be changed because # customers do not have to install the man pages! Therefore, a # built-in dependency on man page information would break the # automatic help feature of clmgr. So the SYNPOSIS section must # be used instead. # # IMPORTANT: As a result of this, it is imperative that the # information in this SYNOPSIS be kept in sync # with the man page information, which is owned # by the IDD team. # # Inputs: None. # # Outputs: None. # # Returns: n/a (not intended to be invoked) # #============================================================================ function devDoc { : <<'=cut' >/dev/null 2>&1 =head1 NAME KLIB_HACMP_get_interface_attributes =head1 SYNOPSIS clmgr query interface [,,,...] clmgr -v query interface NOTE: the aliases for "interface" are "in" and "if". =head1 DESCRIPTION Retrieves the configuration data for one or more interfaces. =head1 ARGUMENTS 1. interface [REQUIRED] [string] The label of one or more interfaces (comma or space separated) that are to be queried. 2. properties [REQUIRED] [hash ref] An associative array within which data about the queried objects can be returned to the caller. =head1 RETURN 0: no errors were detected; the operation appears to have been successful 1: a general error has occurred 2: a specified resource does not exist, or could not be found 3: some required input was missing 4: some detected input was incorrect in some way 5: a required dependency does not exist 6: a specified search failed to match any data =cut } # End of "devDoc()" #============================================================================== # The following, comment block attempts to enforce coding standards when this # file is edited via emacs or vim. This block _must_ appear at the very end # of the file, or the editor will not find it, and it will be ignored. #============================================================================== # Local Variables: # indent-tabs-mode: nil # tab-width: 4 # End: #============================================================================== # vim: tabstop=4 shiftwidth=4 expandtab #==============================================================================