Cover V12, I10
oct2003.tar

Listing 2 The status script

#!/bin/ksh

#####################################################################
# 2003 January - HACMP Status Screen
#
#  Written by Arni Snorri Eggertsson - arnie@gormur.com
#
#####################################################################
#  GLOBAL CONFIG Variables
#####################################################################

COMMUNITY="public"
MIB="/usr/sbin/cluster/hacmp.defs"
# If you use HACMP/ES uncomment the latter and comment out the prior
# MIB="/usr/es/sbin/cluster/hacmp.defs"   

HOST="localhost"
CLFINDRES="/usr/sbin/cluster/utilities/clfindres"

SED="/usr/bin/sed"
AWK="/usr/bin/awk"
TR="/usr/bin/tr"
SNMPINFO="/usr/sbin/snmpinfo"
RM="/usr/bin/rm"
SORT="/usr/bin/sort"
JOIN="/usr/bin/join"
CUT="/usr/bin/cut"

#####################################################################
# Constants used for varius return results
#####################################################################

TMPFILE1=/tmp/statustmp.1.$$
TMPFILE2=/tmp/statustmp.2.$$
TMPFILE3=/tmp/statustmp.3.$$

SNMPKEYS="nodeName clusterName clusterState clusterSubState \
clusterNumNodes nodeId NodeState nodeNumIf nodeName clusterId"

 
CLUSTERSTATEARRAY[2]="up"
CLUSTERSTATEARRAY[4]="down"
CLUSTERSTATEARRAY[8]="unkown"
CLUSTERSUBSTATEARRAY[16]="unstable"
CLUSTERSUBSTATEARRAY[64]="error"
CLUSTERSUBSTATEARRAY[32]="stable"
CLUSTERSUBSTATEARRAY[8]="unknown"
CLUSTERSUBSTATEARRAY[128]="reconfig"
NODESTATEARRAY[2]="up"
NODESTATEARRAY[4]="down"
NETSTATEARRAY[2]="up"
NETSTATEARRAY[4]="down"
NETSTATEARRAY[32]="joining"
NETSTATEARRAY[64]="leaving"

#  Just want to make sure I have the TMPFILES inplace

trap 'GOAWAY ${ESTATUS}' EXIT

touch ${TMPFILE1}
touch ${TMPFILE2}
touch ${TMPFILE3}

#####################################################################

function line  {
 echo "-------------------------------------------------------------"
}

#####################################################################
# This is how I query the cluster with SNMP
#####################################################################

function snmp_cmd { 
 ${SNMPINFO} -m dump -v -c ${COMMUNITY} -h ${HOST} -o ${MIB} $*
}

#####################################################################
# This function translates SNMP return reults into environment variables.
#####################################################################

function get_info {
  snmp_cmd ${1} | ${TR} -d "\"" | ${TR} "." " " | ${AWK} '{ print \
    toupper($1)  "[" $2 "]=" $4 }'   
}

#####################################################################
# This part does most of the work in this script.
#####################################################################

function gather_information  {
  for CHECK in ${SNMPKEYS}
  do
    get_info ${CHECK} >> ${TMPFILE1}
  done

  # Then I load all the variables into our environment.
  . ${TMPFILE1}
}

#####################################################################
#  Getting network information is a bit tricky so I decided to take 
#  that with a different approach.
#####################################################################

function print_node_network {
  snmp_cmd addrState | ${SED} 's/addrState\.//' | ${SED} ' s/\./ /' | while \
    read A B C D ; do
  echo ${A} ${B} ${C} ${NETSTATEARRAY[${D}]}
  done | ${SORT} -t ' ' +1 > ${TMPFILE2}
  echo "\n IF Label              IP Address        Status"   
  snmp_cmd addrLabel | ${SED} -e 's/addrLabel\.//' | ${SED} -e 's/\./ /'  \
    | ${SED} -e 's/"//g'  | \
    ${CUT} '-d ' -f 1,2,4 | ${SORT} -t ' ' +1 | ${JOIN} -1 2 -2 2 -o 2.1 \
      1.3 1.2 2.4 - ${TMPFILE2} | \
    ${SORT} -n | grep ^${1} | ${AWK} '{printf(" %-20s  %-16s  %-5s \n",$2, \
      $3, $4 )}'
}

#####################################################################
#  Now it's just printing the information about the cluster on-screen 
#####################################################################

function print_report {
  ${CLFINDRES} | cut -c 1-42 > ${TMPFILE3}
  line
  echo "  Information screen about an HACMP cluster. "
  line
  echo " Cluster name : ${CLUSTERNAME[0]}     Cluster ID   : ${CLUSTERID[0]} " 
  echo " Cluster state   : ${CLUSTERSTATEARRAY[${CLUSTERSTATE[0]}]}\
    Cluster substate   : ${CLUSTERSUBSTATEARRAY[${CLUSTERSUBSTATE[0]}]} " 
  echo " Number of cluster nodes : ${CLUSTERNUMNODES[0]} " 
  line
  IDNUM=1
  while [[ $IDNUM -le ${CLUSTERNUMNODES[0]} ]]
  do
    echo " Node name : ${NODENAME[$IDNUM]}\
    Node state : ${NODESTATEARRAY[${NODESTATE[$IDNUM]}]}    Number of \
      IF's on node : ${NODENUMIF[$IDNUM]} "
    echo  " Services running on this node: "
    egrep -i "${NODENAME[$IDNUM]}|GroupName" ${TMPFILE3} | cut -c 1-34 | \
      awk '{ print "   " $0 }'  
    print_node_network $IDNUM
    line
    IDNUM=$((${IDNUM}+1))
  done
}

#####################################################################
#  This function is called when the EXIT trap is fired, this makes 
#  sure TMPFILES are deleted.
#####################################################################

function GOAWAY {
  ${RM} -f ${TMPFILE1}
  ${RM} -f ${TMPFILE2}
  ${RM} -f ${TMPFILE3}
  exit ${i}
}


#####################################################################
# This get's the party started.
#####################################################################
gather_information
print_report