#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 1998,2005 
# 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 
# @(#)01 1.38 src/avs/fs/mmfs/ts/admin/mmlsnode.sh, mmfs, avs_rgpfs24, rgpfs240610b 10/3/05 11:59:22
##########################################################################
#
#  List the names of the nodes that belong to a GPFS nodeset.
#
#  Usage:  mmlsnode { -C nodesetId | -a | -n nodename }
#
#  where
#    nodesetId  is the name of the nodeset whose nodes are to be listed
#    -C   display the names of the nodes that belong to nodesetId
#    -a   display the names of all nodes in all GPFS nodesets
#    -n   display the names of the nodes that belong to the same nodeset
#         as nodename
#
##########################################################################

# Include global declarations and service routines.
. /usr/lpp/mmfs/bin/mmglobfuncs
. /usr/lpp/mmfs/bin/mmsdrfsdef

sourceFile="mmlsnode.sh"
[[ -n $DEBUG || -n $DEBUGmmlsnode ]] && set -x
$mmTRACE_ENTER "$*"

# Local work files.  Names should be of the form:
#   fn=${tmpDir}fn.${mmcmd}.$$

LOCAL_FILES=" "

# Local variables
nodesetId=$GLOBAL_ID
allOpt=""
nOpt=""
COpt=""
nodeName=""
usageMsg=184


################################
# Check the input arguments.
################################
[[ $arg1 = '-?' || $arg1 = '-h' || $arg1 = '--help' || $arg1 = '--' ]] && \
  syntaxError "help" $usageMsg

while getopts :an:C: OPT
do
  case $OPT in

    a) # display all nodesets in the sdr
       [[ -n $allOpt ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       allOpt="-a"
       [[ -n $nOpt ]] && \
         syntaxError "invalidCombination" $usageMsg "-a" "-n"
       [[ -n $COpt ]] && \
         syntaxError "invalidCombination" $usageMsg "-a" "-C"
       ;;

    n) # display the nodes in the nodeset of the specified node
       [[ -n $nOpt ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       nOpt="-n"
       nodeName=$OPTARG
       [[ -n $allOpt ]] && \
         syntaxError "invalidCombination" $usageMsg "-a" "-n"
       [[ -n $COpt ]] && \
         syntaxError "invalidCombination" $usageMsg "-C" "-n"
       ;;

    C) # display the nodes for the specified nodeset
       [[ -n $COpt ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       COpt="-C"
       nodesetId=$OPTARG
       [[ -n $allOpt ]] && \
         syntaxError "invalidCombination" $usageMsg "-a" "-C"
       [[ -n $nOpt ]] && \
         syntaxError "invalidCombination" $usageMsg "-C" "-n"
       ;;

    :) syntaxError "missingValue" $usageMsg $OPTARG
       ;;

    +[aCn]) syntaxError "invalidOption" $usageMsg "$OPT"
       ;;

    *) syntaxError "invalidOption" $usageMsg $OPTARG
       ;;

  esac
done

shift OPTIND-1
[[ $# -ne 0 ]] && syntaxError "extraArg" $usageMsg $1
[[ $argc -eq 0 ]] && allOpt="-a"   # assume -a if nothing else is specified


#######################################################################
# Set up trap exception handling and call the gpfsInit function.
# It will ensure that the local copy of the mmsdrfs and the rest of
# the GPFS system files are up-to-date.  There is no need to lock
# the sdr.
#######################################################################
trap pretrap2 HUP INT QUIT KILL
gpfsInitOutput=$(gpfsInit nolock)
setGlobalVar $? $gpfsInitOutput

# If the nodeset id was not explicitly specified,
# use the default nodeset returned from gpfsInit.
[[ $nodesetId = "." ]] && \
  nodesetId=$nsId

if [[ $nodesetId = 0 ]]
then
  print -u2 "$mmcmd: The nodeset can not be determined.  Use the -C <nodesetId> option,"
  print -u2 "or issue the command from a node that belongs to the desired nodeset."
  cleanupAndExit
fi


########################################################################
# If -n was specified, find the name of the nodeset that contains
# the specified node.  To do this reliably, we first convert the name
# into a node number.
########################################################################
if [[ -n $nOpt ]]
then

  # Find the IP address that corresponds to this node name.
  hostResult=$($host $nodeName)
  set -f ; set -- $hostResult ; set +f
  ipa=${3%%,*}          # Exclude everything after the first comma.
  if [[ -z $ipa ]]
  then
    # Invalid node name specified
    printErrorMsg 54 $mmcmd $nodeName
    cleanupAndExit
  fi

  # Find the nodeset to which this node number belongs.
  nodesetId=$($awk -F: '                 \
    /':$MEMBER_NODE:'/ {                 \
      if ($'$IPA_Field' == "'$ipa'") {   \
        { print $'$NODESETID_Field' }    \
        { exit }                         \
      }                                  \
    }                                    \
  ' $mmsdrfsFile)
  rc=$?
  if [[ $rc != 0 || -z $nodesetId ]]
  then
    if [[ $rc != 0 ]]
    then
      print -u2 "$mmcmd: A node name that is not valid was found in the mmsdrfs file."
    else
      print -u2 "$mmcmd: Node $nodeName does not belong to the GPFS cluster."
    fi
    cleanupAndExit
  fi

fi    #end of if [[ -n $nOpt ]]


########################################################################
# Create a list of the nodesets that will be displayed.
# If the user requested a specific nodeset, or if the -n option
# was specified, the list will consist of a single nodeset id.
# If the user specified -a, then the value of $nodesetId is set
# to the global id, and the call to getNodesetInfo will return
# the names of all nodesets.
########################################################################
nodesetList=$(getNodesetInfo $NODESETID_Field $nodesetId $mmsdrfsFile)
if [[ -z $nodesetList ]]
then
  if [[ $nodesetId = $GLOBAL_ID ]]
  then
    print -u2 "$mmcmd: There are no known GPFS nodesets."
  else
    print -u2 "$mmcmd: GPFS nodeset $nodesetId does not exist."
  fi
  cleanupAndExit
fi

clusterName=$($head -1 $mmsdrfsFile | $GETVALUE $CLUSTER_NAME_Field)

#########################################################
# Print out the node names for each nodeset in the list.
#########################################################
# Put out the header line.
print -- "GPFS nodeset    Node list"
print -- "-------------   -------------------------------------------------------"

for nodesetId in $nodesetList
do
  nodeList=$(getNodeList $NODE_NAME_Field $nodesetId $mmsdrfsFile)
  [[ $nodesetId = $HOME_CLUSTER ]] && nodesetId="${clusterName%%.*}"
  printf "%2s %-8s %4s" "$BLANKchar" "$nodesetId" "$BLANKchar"
  for nodeName in $nodeList
  do
    printf "%s " $nodeName
  done
  printf "\n"
done


#######################
# Cleanup and return.
#######################
cleanupAndExit 0

