#!/bin/ksh93 # ALTRAN_PROLOG_BEGIN_TAG # This is an automatically generated prolog. # # Copyright (C) Altran ACT S.A.S. 2017,2018,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_service_ip_attributes.sh 1.11 # # 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_service_ip_attributes.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM #============================================================================ # # Name: KLIB_HACMP_get_service_ip_attributes # # Description: This is the main, FPATH function that is invoked by clmgr # to collect service label information. It invokes various # network related utilities, at both the SystemMirror and # AIX levels, and mines their output for all relevant info. # # 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_service_ip_attributes { LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL" : version=@(#) 7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_get_service_ip_attributes.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM : INPUTS: $* typeset service_ip=${1//\"/} # Get the "properties" associative array reference, and initialize it if [[ -z $2 ]] || [[ -n $CLMGR_LOGGING && $2 == "|" ]]; then service_ip= 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 key= SERVICE_IPS= DATA= rg= typeset -i matches=0 rc=$RC_SUCCESS INDEX=0 typeset -u attr= uc_key= typeset -A queries typeset -i i=0 : This ensures that the attributes displayed in queries are : identical to those used for add and modify operations. typeset -A ATTRMAP ATTRMAP=( [SITENAME]="SITE" ) #=================================================== : 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 $service_ip ]]; then [[ $service_ip != *,* ]] && unset INDEX for service_ip in ${service_ip//,/ }; do CL=$LINENO KLIB_HACMP_is_known_service_ip "$service_ip" 2>/dev/null 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' "$service_ip" 1>&2 dspmsg -s $CLMGR_SET $CLMGR_MSGS 156 "Available Service IPs:\n\n" 1>&2 typeset available CL=$LINENO KLIB_HACMP_list_service_ip 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 SERVICE_IPS="$SERVICE_IPS $service_ip" fi done elif (( CLMGR_VERBOSE || ${#queries[*]} > 0 )); then print "$0()[$LINENO]($SECONDS): cllsif -cS | grep \":service:\" | cut -d: -f1 | sort -u" >>$CLMGR_TMPLOG # Always log commands DATA=$(cllsif -cS | grep ":service:") print "$0()[$LINENO]($SECONDS): cllsif RC: $?" >>$CLMGR_TMPLOG # Always log command result print -- "$DATA" | cut -d":" -f1 | sort -u |\ while IFS='=' read value rem; do [[ -n $SERVICE_IPS ]] && SERVICE_IPS="$SERVICE_IPS " SERVICE_IPS="$SERVICE_IPS$value" done print "$0()[$LINENO]($SECONDS): SERVICE_IPS == \"$SERVICE_IPS\"" >>$CLMGR_TMPLOG 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 SERVICE_IPS #================================================================ : Populate the return hash with the retrieved attributes/values #================================================================ typeset GSIPA_CHILDREN="" GSIPA_DATA_FILE="$TMPDIR/clmgr.KHgsia.attrs" integer PROC_COUNT=0 for service_ip in $SERVICE_IPS do ( # Everything within will run in a child process integer NEED_DATA=1 # : If IF_LOG is defined, it means this was invoked from some : other clmgr code, like KLIB_HACMP_get_cluster_attributes. : Used as a performance optimization. # if [[ -n $IF_LOG && -f $IF_LOG && -s $IF_LOG ]]; then typeset ATTR_DATA=$(grep -p 'TYPE="service"' $IF_LOG | grep -p "NAME=\"$service_ip\"") if [[ -n $ATTR_DATA ]]; then ATTR_DATA="$ATTR_DATA SITE=\"$(clodmget -q "ip_label=$service_ip AND function=shared" -f sitename -n HACMPadapter)\"" typeset PAIR="" VAL="" print -- "$ATTR_DATA" |\ while read PAIR; do VAL=${PAIR#*=} VAL=${VAL#\"} VAL=${VAL%\"} properties[${PAIR%%=*}$INDEX]="$VAL" done unset PAIR VAL NEED_DATA=0 # Make sure we don't load again, below! fi unset ATTR_DATA fi if (( NEED_DATA )); then typeset -A attributes CL=$LINENO KLIB_HACMP_get_interface_attributes $service_ip attributes for key in ${!attributes[*]}; do uc_key=$key [[ -n ${ATTRMAP[$uc_key]} ]] && uc_key=${ATTRMAP[$uc_key]} properties[$uc_key$INDEX]=${attributes[$key]} done unset attributes fi unset NEED_DATA if [[ -z ${properties[NODE$INDEX]} ]]; then print "$0()[$LINENO]($SECONDS): clodmget -n -q \"value=$service_ip AND name=SERVICE_LABEL\" -f group HACMPresource" >>$CLMGR_TMPLOG # Always log commands rg=$(clodmget -n -q "value=$service_ip AND name=SERVICE_LABEL" -f group HACMPresource) print "$0()[$LINENO]($SECONDS): clodmget RC: $?; rg == \"$rg\"" >>$CLMGR_TMPLOG # Always log command result if [[ -n $rg ]]; then # : If RG_LOG is defined, it means this was invoked from some : other 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 properties[NODE$INDEX]=$CN fi else typeset -A rgAttrs CL=$LINENO KLIB_HACMP_get_rg_attributes "$rg" rgAttrs 2>>$CLMGR_TMPLOG if [[ -n ${rgAttrs[CURRENT_NODE]} ]]; then properties[NODE$INDEX]=${rgAttrs[CURRENT_NODE]} fi unset rgAttrs fi fi fi if [[ -z ${properties[INTERFACE$INDEX]} && \ -n ${properties[NODE$INDEX]} ]] then typeset intf="" typeset -A IFs CL=$LINENO get_interfaces IFs ${properties[NODE$INDEX]} for intf in ${!IFs[*]}; do if [[ " ${IFs[$intf]} " == *\ $ipaddr\ * ]]; then properties[INTERFACE$INDEX]=$intf break fi done unset IFs 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[service_ip]}; 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 # : Collect all the data for service IP $service_ip in an eval-able : format in a variable. Then write it to the temporary data file : in a single write, which offers slightly better performance than : individual writes. # typeset PAIRS="" for KEY in ${!properties[*]} do PAIRS=${PAIRS:+$PAIRS$NL}"properties[$KEY]=\"${properties[$KEY]}\"" done print -- "$PAIRS" >>$GSIPA_DATA_FILE )& GSIPA_CHILDREN=${GSIPA_CHILDREN:+$GSIPA_CHILDREN }$! (( INDEX++ )) [[ $INDEX == *0 ]] && (( INDEX++ )) (( PROC_COUNT++ )) if (( PROC_COUNT >= CLMGR_MAX_PROCS )) then : Wait for this group of $PROC_COUNT processes to complete... wait_for_processes "$GSIPA_CHILDREN" "$TMPDIR/clmgr.KHgsia" (( rc |= $? )) GSIPA_CHILDREN="" PROC_COUNT=0 # Reset the counter (( rc != RC_SUCCESS )) && break # No need to continue fi done if [[ -n $GSIPA_CHILDREN ]]; then : Wait for this group of $PROC_COUNT processes to complete... wait_for_processes "$GSIPA_CHILDREN" "$TMPDIR/clmgr.KHgsia" (( rc |= $? )) GSIPA_CHILDREN="" fi # : Now that the data collections are all complete, check to see if we got : any results. If so, load the the results, then remove the data file. # if [[ -s $GSIPA_DATA_FILE ]] then if (( rc == RC_SUCCESS )) then eval $(cat $GSIPA_DATA_FILE) fi elif [[ -n $SERVICE_IPS ]] then # : If service IPs are defined in this cluster \(\$SERVICE_IPS is : populated\), but no data was collected for any of them \(the : \$GSIPA_DATA_FILE has zero bytes\), then something went wrong. : The data collection operation has failed and a non-zero return : code is warranted. # rc=1 fi rm -f $GSIPA_DATA_FILE # : 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 # : Remove the interface data file, created within KLIB_HACMP_get_interface_attributes # rm -f $TMPDIR/clmgr.KHgia.dat rm -f $TMPDIR/clmgr.KHgifa.cllsif.pcS log_return_msg "$rc" "$0()" "$LINENO" return $? } # End of "KLIB_HACMP_get_service_ip_attributes()" #============================================================================ # # Name: wait_for_processes # # Description: This function waits for a provided set of processes to # complete before returning. It will only wait for up to # $CLMGR_TIMEOUT seconds, though, then it will error out. # # Inputs: PROCESSES The set of process IDs to wait for. # DATAFILE The location and root name of the temporary # data files to use for this operation. If none # are provided, a default will be used: # $TMPDIR/clmgr.wfp.$$. # # Outputs: None. # # Returns: Zero if no errors are detected. Otherwise, an appropriate # non-zero value is returned. # #============================================================================ function wait_for_processes { typeset PROCESSES=$1 typeset DATAFILE=$2 integer RC=$RC_UNKNOWN if [[ -n $DATAFILE ]] && ! touch $DATAFILE 2>/dev/null then print "$0()[$LINENO]($SECONDS): Can't write to the specified data file, \"$DATAFILE\"." >>$CLMGR_TMPLOG DATAFILE="" # If no permission, or path problem, use default else rm -f $DATAFILE # Remove the touched file. Permissions established. fi [[ -z $DATAFILE ]] && DATAFILE="$TMPDIR/clmgr.wfp" # : Assuming the we have some child process IDs running, : wait for them to complete. # if [[ -n $PROCESSES ]]; then # : Set an alarm to prevent a "wait" hang. # [[ $CLMGR_TIMEOUT != +([0-9]) ]] && CLMGR_TIMEOUT=30 (( $CLMGR_TIMEOUT < 5 )) && CLMGR_TIMEOUT=5 ( sleep $CLMGR_TIMEOUT & alarmPID=$! print -n $alarmPID >$DATAFILE.alarm.pid.$$ wait $alarmPID # : Remove to signal the external watcher/reaper that the alarm : was tripped, and that the operation has timed out. # rm -f $DATAFILE.alarm.pid.$$ cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 187 'Warning: the current operation has exceeded its allotted execution time (%1$s seconds).\n' "$CLMGR_TIMEOUT" 1>&2 cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 470 "Attempting to terminate any remaining, active processes...\n\n" "$CLMGR_TIMEOUT" 1>&2 for pid in $PROCESSES; do kill -9 $pid 2>/dev/null; done )& typeset -i reaperPID=$! # : Wait for all the backgrounded processes to complete... # # If the timeout alarm fires, it will kill all these processes... which # will -look- like a happy completion. However, the absence of the alarm # temp file indicates what has really happened so that the failure can # be properly reported via a non-zero return code. # integer PCNT=$(echo $PROCESSES | wc -w) print "$0()[$LINENO]($SECONDS): Waiting for $PCNT processes to complete..." >>$CLMGR_TMPLOG wait $PROCESSES kill -9 $reaperPID $([[ -f $DATAFILE.alarm.pid.$$ ]] && cat $DATAFILE.alarm.pid.$$) 2>/dev/null [[ -f $DATAFILE ]] && rm -f $DATAFILE if [[ -f $DATAFILE.alarm.pid.$$ ]] then rm -f $DATAFILE.alarm.pid.$$ RC=$RC_SUCCESS else RC=$RC_ERROR fi else : No process IDs were provided to wait for RC=$RC_MISSING_INPUT fi return $RC } # End of "wait_for_processes()" #============================================================================ # # 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_service_ip_attributes =head1 SYNOPSIS clmgr query service_ip [,,,...] clmgr -v query service_ip NOTE: the alias for "service_ip" is "se". =head1 DESCRIPTION Retrieves the configuration data for one or more service IPs. =head1 ARGUMENTS 1. service_ip [REQUIRED] [string] The label of one or more service IPs (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. Optional Environment Variables ============================== CLMGR_TIMEOUT Can be externally set in the environment, but defaults to 30 seconds in this script. Sets the upper limit to wait for a given set of backgrounded processes to complete running. CLMGR_MAX_PROCS Can be externally set in the environment, but defaults to 10 processes. Sets the maximum number of concurrent, backgrounded processes that this script can launch at any given time. =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 #==============================================================================