#!/bin/ksh93 # ALTRAN_PROLOG_BEGIN_TAG # This is an automatically generated prolog. # # Copyright (C) Altran ACT S.A.S. 2019,2021. All rights reserved. # # ALTRAN_PROLOG_END_TAG # # @(#) 7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_get_snapshot_attributes.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM # IBM_PROLOG_BEGIN_TAG # This is an automatically generated prolog. # # 61haes_r720 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_get_snapshot_attributes.sh 1.19 # # Licensed Materials - Property of IBM # # COPYRIGHT International Business Machines Corp. 2010,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 #============================================================================ # # Name: KLIB_HACMP_get_snapshot_attributes # # Description: This is the main, FPATH function that is invoked by clmgr # to collect snapshot metadata like cluster name, node list, etc. # # Inputs: snapshot Optional. The name of one or more cluster # snapshots to explicitly query. # properties Required. A reference to a hash, used to return # any retrieved information within. # # 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, above, for the standard return # code values/meanings for clmgr. # #============================================================================ function KLIB_HACMP_get_snapshot_attributes { . $HALIBROOT/log_entry "$0()" "$CL" : version="@(#) 7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_get_snapshot_attributes.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM" : INPUTS: $* # Depending on the invocation, the first parameter may be a snapshot # name or the properties array typeset snapshot=${1//\"/} # If there is no second formal parameter or this invocation is part of # a piped command (used during logging) then the first parameter is # the "properties" associative array reference if [[ -z $2 ]] || [[ -n $CLMGR_LOGGING && $2 == "|" ]]; then snapshot= 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 SNAPSHOTS= SNAPSHOT_INFO= typeset -i INDEX=0 rc=$RC_SUCCESS cmd_rc=$RC_UNKNOWN typeset key= typeset -i matches=0 typeset -u attr= uc_key= typeset -A queries typeset -i i=0 # clsnapshot can use a different path if one is specified in the # environment - set a default here, just in case [[ -z $SNAPSHOTPATH ]] && SNAPSHOTPATH=/usr/es/sbin/cluster/snapshots #=================================================== : 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 #======================================= : Make sure we have something to query #======================================= if [[ -n $snapshot ]]; then [[ $snapshot != *,* ]] && unset INDEX for snapshot in ${snapshot//,/ }; do CL=$LINENO KLIB_HACMP_is_known_snapshot "$snapshot" 2>/dev/null if (( $? != RC_SUCCESS )); then dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 '\nERROR: "%1$s" does not appear to exist!\n\n' "$snapshot" 1>&2 dspmsg -s $CLMGR_SET $CLMGR_MSGS 158 "Available Snapshots:\n\n" 1>&2 typeset available CL=$LINENO KLIB_HACMP_list_snapshots available for (( i=0; i<${#available[*]}; i++ )); do if [[ ${available[$i]} != *([[:space:]]) ]]; then print -u2 "\t${available[$i]}" fi done print -u2 "" rc=$RC_NOT_FOUND else SNAPSHOTS="$SNAPSHOTS $snapshot" fi done elif (( CLMGR_VERBOSE || ${#queries[*]} > 0 )); then typeset available CL=$LINENO KLIB_HACMP_list_snapshots available for (( i=0; i<${#available[*]}; i++ )); do SNAPSHOTS="$SNAPSHOTS ${available[$i]}" done else dspmsg -s $CLMGR_SET $CLMGR_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2 rc=$RC_MISSING_INPUT fi (( $rc != RC_SUCCESS )) && unset SNAPSHOTS #================================================================ : Populate the return hash with the retrieved attributes/values #================================================================ cd $SNAPSHOTPATH print "$0()[$LINENO]($SECONDS): ls -go" >>$CLMGR_TMPLOG # Always log commands SNAPSHOT_INFO=$(ls -go) print "$0()[$LINENO]($SECONDS): ls RC: $?; SNAPSHOT_INFO == \"SNAPSHOT_INFO\"" >>$CLMGR_TMPLOG # Always log command result for snapshot in $SNAPSHOTS; do properties[NAME$INDEX]=$snapshot properties[TYPE$INDEX]="ODM" DESC=$(clsnapshot -n $snapshot -j DSC) cmd_rc=$? if (( $cmd_rc != RC_SUCCESS )); then dspmsg -s $CLMGR_SET $CLMGR_MSGS 127 '\nERROR: the specified snapshot, "%1$s", could not be found.\n\n' "$snapshot" 1>&2 rc=$RC_NOT_FOUND break fi DESC=${DESC#$snapshot:} properties[DESCRIPTION$INDEX]="$DESC" properties[CLUSTER_NAME$INDEX]=$(grep -p HACMPcluster: "$SNAPSHOTPATH/$snapshot.odm" | grep -w name) properties[CLUSTER_NAME$INDEX]=${properties[CLUSTER_NAME$INDEX]##* = \"} properties[CLUSTER_NAME$INDEX]=${properties[CLUSTER_NAME$INDEX]%\"} typeset CLUSTER_TYPE=$(grep -p HACMPcluster: "$SNAPSHOTPATH/$snapshot.odm" | grep -w multi_site_lc) case ${CLUSTER_TYPE##*= } in -1) properties[CLUSTER_TYPE$INDEX]="NSC" ;; 0) properties[CLUSTER_TYPE$INDEX]="SC" ;; 1) properties[CLUSTER_TYPE$INDEX]="LC" ;; esac #======================================================== : Extract the cluster node names from this snapshot #======================================================== grep -p "HACMPnode:" "$SNAPSHOTPATH/$snapshot.odm" |\ grep "name = " | sort -u |\ while read LINE; do if [[ -n ${properties[NODES$INDEX]} ]]; then properties[NODES$INDEX]="${properties[NODES$INDEX]}," fi properties[NODES$INDEX]="${properties[NODES$INDEX]}${LINE##*= }" done properties[NODES$INDEX]=${properties[NODES$INDEX]//\"/} #======================================================== : Extract the communication path for the cluster nodes #======================================================== awk -v RS= '/object = "COMMUNICATION_PATH"/' "$SNAPSHOTPATH/$snapshot.odm" | grep "value =" | uniq |\ while read LINE; do if [[ -n ${properties[HOSTS$INDEX]} ]]; then properties[HOSTS$INDEX]="${properties[HOSTS$INDEX]}," fi LINE=${LINE##*= } LINE=${LINE//\"/} # The line can either be a hostname or fqdn - if the former, # try to convert it to the latter using "host" typeset HOSTNAME=$LINE if [[ $LINE != *\.* ]]; then typeset DATA="" if [[ $LINE == ${LOCAL_NODE%%\.*} ]]; then print "$0()[$LINENO]($SECONDS): LC_ALL=C host hostname" >>$CLMGR_TMPLOG # Always log commands DATA=$(LC_ALL=C host $(hostname)) rc=$? else # remote node - try clrsh print "$0()[$LINENO]($SECONDS): $CLRSH $LINE \"LC_ALL=C /usr/bin/host /usr/bin/hostname\"" >>$CLMGR_TMPLOG # Always log commands if ! DATA=$($CLRSH $LINE 'LC_ALL=C /usr/bin/host $(/usr/bin/hostname)' 2>>$CLMGR_TMPLOG) then : Cannot contact $LINE via clcomd. Try local : name resolution, and hope for the best. print "$0()[$LINENO]($SECONDS): LC_ALL=C host $LINE" >>$CLMGR_TMPLOG # Always log commands DATA=$(LC_ALL=C host $LINE 2>>$CLMGR_TMPLOG) rc=$? fi fi print "$0()[$LINENO]($SECONDS): host RC: $rc; DATA == \"$DATA\"" >>$CLMGR_TMPLOG # Always log command result typeset FQDN=${DATA%%+([[:space:]])*} if [[ $FQDN != *\.* ]]; then : The first component of the host output does : not look fully-qualified. This might mean there is an : incorrect /etc/hosts configuration. Checking aliases. typeset ALIASES=${DATA##*:} for FQDN in ${ALIASES//,/ }; do [[ $FQDN == *\.* ]] && break done if [[ $FQDN != *\.* || \ $FQDN == +([0-9])\.+([0-9])\.+([0-9])\.+([0-9]) ]] then : None of the aliases were fully-qualified either. : There is definitely something wrong with this : configuration. Defaulting to the output of the : hostname utility. HOSTNAME=${DATA%%+([[:space:]])*} else HOSTNAME=$FQDN fi else HOSTNAME=$FQDN fi fi properties[HOSTS$INDEX]="${properties[HOSTS$INDEX]}$HOSTNAME" done typeset SITE_LABEL="" if [[ ${properties[CLUSTER_TYPE$INDEX]} == @(S|L)C ]]; then grep -p "HACMPsite:" "$SNAPSHOTPATH/$snapshot.odm" |\ egrep -e "name = " -e "nodelist = " |\ while read LINE; do LINE=${LINE//\"/} if [[ $LINE == name\ =\ * ]]; then SITE_LABEL=${LINE##*= } if [[ -n ${properties[SITES$INDEX]} ]]; then properties[SITES$INDEX]="${properties[SITES$INDEX]}," fi properties[SITES$INDEX]="${properties[SITES$INDEX]}$SITE_LABEL" else properties[~$SITE_LABEL\#$INDEX]=${LINE##*= } properties[~$SITE_LABEL\#$INDEX]=${properties[~$SITE_LABEL\#$INDEX]// /,} fi done fi #==================================================== : Save the capture date, aka last modification time. # There are better, more accurate ways to do this, # but they are very expensive on performance. Think # in terms of 50 snapshots... #==================================================== properties[CAPTURE_DATE$INDEX]=$(grep "^capture_date=" "$SNAPSHOTPATH/$snapshot.odm") if [[ -n ${properties[CAPTURE_DATE$INDEX]} ]]; then properties[CAPTURE_DATE$INDEX]=${properties[CAPTURE_DATE$INDEX]#*=} fi if [[ ${properties[CAPTURE_DATE$INDEX]} == *([[:space:]]) ]]; then print -- "$SNAPSHOT_INFO" |\ while read LINE; do typeset -l ext=${properties[TYPE$INDEX]} if [[ $LINE == *\ $snapshot.$ext ]]; then LINE=${LINE%\ $snapshot.$ext} LINE=${LINE#*+([[:space:]])+([[:digit:]])} LINE=${LINE#*+([[:space:]])+([[:digit:]])+([[:space:]])} properties[CAPTURE_DATE$INDEX]=$LINE break fi done fi properties[CAPTURE_NODE$INDEX]=$(grep -p HACMPcluster: "$SNAPSHOTPATH/$snapshot.odm" | grep -w nodename) properties[CAPTURE_NODE$INDEX]=${properties[CAPTURE_NODE$INDEX]#*\"} properties[CAPTURE_NODE$INDEX]=${properties[CAPTURE_NODE$INDEX]%\"} if [[ -z ${properties[CAPTURE_NODE$INDEX]} ]]; then if [[ " ${properties[NODES$INDEX]//, } " != *\ $LOCAL_NODE\ * ]]; then properties[CAPTURE_NODE$INDEX]=${properties[NODES$INDEX]} else properties[CAPTURE_NODE$INDEX]=$LOCAL_NODE fi fi #======================================== : Check for any custom snapshot methods which were called : when creating this snapshot #======================================== if [[ -f "$SNAPSHOTPATH/$snapshot.info" ]]; then # Each call is preceeded with a banner which include the method # name - get the banner message but use a dummy name typeset EXEC_MSG=$(dspmsg -s 35 scripts 1 'Executing custom snapshot method: "%1$s"' foo) # strip the dummy name to create a "generic" banner EXEC_MSG=${EXEC_MSG%%:*} # now get every banner line and save the actual method name grep "^$EXEC_MSG:" "$SNAPSHOTPATH/$snapshot.info" |\ while read LINE; do LINE=${LINE##*: } LINE=${LINE//\"/} if [[ ${properties[METHODS$INDEX]} != *,$LINE,* && ${properties[METHODS$INDEX]} != *,$LINE ]] then properties[METHODS$INDEX]="${properties[METHODS$INDEX]},$LINE" fi done properties[METHODS$INDEX]=${properties[METHODS$INDEX]#,} 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[snapshot]}; do if [[ -z ${properties[$key$INDEX]} ]]; then properties[$key$INDEX]="" fi done # Don't display an empty "SITES=''" for non-site clusters if [[ ${properties[CLUSTER_TYPE$INDEX]} == "NSC" ]]; then unset properties[SITES$INDEX] fi # Don't display the attributes related to the compare action unset properties[SDIFF_OUTPUT_WIDTH$INDEX] unset properties[SDIFF_FLAGS$INDEX] unset properties[SNAPSHOTPATH$INDEX] unset properties[PAGER$INDEX] # List the available sections in this snapshot properties[AVAILABLE_SECTIONS$INDEX]=$(clsnapshot -b -n $SNAPSHOTPATH/$snapshot.odm | cut -f2 -d"=") # All done with this one (( INDEX++ )) [[ $INDEX == *0 ]] && (( INDEX++ )) done # for all snapshots #========================================================== : 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 log_return_msg "$rc" "$0()" "$LINENO" return $? } # End of "KLIB_HACMP_get_snapshot_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, even if the man pages are installed. # As a result of this, it is imperative that the information in # this SYNOPSIS be kept in sync with the man page information. # # Inputs: None. # # Outputs: None. # # Returns: n/a (not intended to be invoked) # #============================================================================ function devDoc { : <<'=cut' >/dev/null 2>&1 =head1 NAME KLIB_HACMP_get_snapshot_attributes =head1 SYNOPSIS clmgr query snapshot [,,...] clmgr -v query snapshot NOTE: the alias for "snapshot" is "sn". =head1 DESCRIPTION Retrieves the configuration data for one or more PowerHA snapshots. =head1 ARGUMENTS 1. snapshot [REQUIRED] [string] The label of one or more snapshots (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 #==============================================================================