#!/bin/ksh
# Script to replace some of the itmquery function shipped with NetView 7.1.4
#
# It is assumed that the NetView system has ITM 5.1.x installed on it
# to provide access to the wdmlseng command. If this cannot be the case,
# something like ssh will be needed to run wdmlseng commands
# on remote systems, having queried the itm_servers.conf file
#
# No use is made of the /usr/OV/conf/itm_servers.conf file in this script
# One implication of this is that only the local TMR is supported.
#
# Two parameters are required - the name of a node to check and the 
# name (or partial name) of the Resource Model (RM) to check for.
# The node name is not used when running this script standalone.  When
# called by servmon, servmon passes a node name as the first parameter.
#
# This script can be used both as a discovery and as a status script 
# in servmon.conf
# servmon expects exit codes as follows:
#  2 = Normal   3 = Marginal  4 = Critical  0 = test cannot be run
#
# Source the Tivoli environment, if it exists
#
#set -x
if [ -f /etc/Tivoli/setup_env.sh ]
then
  . /etc/Tivoli/setup_env.sh
fi
#
RET_CODE=4
# get Selection Name passed by NetView
HOST="$1"
RM="$2"
#
# Find each Alive endpoint for each ITM MN gateway
#   and for each, see if the host matches a valid endpoint 
#   and whether the specified RM exists and is Running, eliminating duplicates
#
for i in `wdmmngcache -m all -l | grep -v ManagedNode | grep -v \| | grep -v + | grep -v Unreachable | grep -v DMEngineOff  | cut -f1 -d " " `
do
   shorthost=`echo "$HOST" | cut -f1 -d . `
   shortendpoint=`echo "$i" | cut -f1 -d . `
# Check whether short hostname is same as a shortname of a valid endpoint
# If so, check for running RM on Endpoint and return status code
   if [ "$shorthost" = "$shortendpoint" ]
   then
      RUN=` wdmlseng -e "$i" | grep "$RM" | grep Running| sort -u | cut -f1 -d : `
      if [ "$RUN" != "" ]
      then
         echo "$RUN" running on "$i" Endpoint
         RET_CODE=2
      else
         echo failure $RUN
         RET_CODE=4
      fi
   fi
done
exit $RET_CODE

