#!/bin/ksh
# DO NOT EDIT THIS FILE
#pragma ident   "@(#)hastat_common	1.1 98/06/17  SMI"

MEMBERSHIP="HASTAT_CURRENT_MEMBERSHIP"
CONFIG_STATE="HASTAT_CONFIG_STATE:"
UPTIME_STATE="HASTAT_UPTIME_STATE:"
LOGHOST_MASTERED="HASTAT_LOGICAL_HOSTS_MASTERED"
LOGHOST_BACKUP="HASTAT_LOGICAL_HOSTS_BACKUP:"
LOGHOST_MAINT="HASTAT_LOGICAL_HOSTS_MAINTENANCE" 
PRIVNET_STATUS="HASTAT_PRIV_NET_STATUS" 
PUBNET_STATUS="HASTAT_PUBLIC_NET_STATUS"
SERVICE_STATUS="HASTAT_SERVICE_STATUS"
RECENT_ERROR_MESG="HASTAT_RECENT_ERR_MSGS"

MEMBERSHIP_DESC="CURRENT MEMBERS OF THE CLUSTER"
CONFIG_STATE_DESC="CONFIGURATION STATE OF THE CLUSTER"
UPTIME_STATE_DESC="UPTIME OF NODES IN THE CLUSTER"
LOGHOST_MASTERED_DESC="LOGICAL HOSTS MASTERED BY THE CLUSTER MEMBERS"
LOGHOST_BACKUP_DESC="LOGICAL HOSTS WHICH HAVE BACKUP-NODES IN THE CLUSTER"
LOGHOST_MAINT_DESC="LOGICAL HOSTS IN MAINTENANCE STATE" 
PRIVNET_STATUS_DESC="STATUS OF PRIVATE NETS IN THE CLUSTER" 
PUBNET_STATUS_DESC="STATUS OF PUBLIC NETS IN THE CLUSTER"
SERVICE_STATUS_DESC="STATUS OF SERVICES RUNNING ON LOGICAL HOSTS IN THE CLUSTER"
RECENT_ERROR_MESG_DESC="RECENT  ERROR MESSAGES FROM THE CLUSTER"


set -A List "${MEMBERSHIP}"  "${CONFIG_STATE}"  "${UPTIME_STATE}"  "${LOGHOST_MASTERED}"  "${LOGHOST_MAINT}"  "${PRIVNET_STATUS}"  "${PUBNET_STATUS}"  "${SERVICE_STATUS}"  "${RECENT_ERROR_MESG}" 

set -A DescList "${MEMBERSHIP_DESC}"  "${CONFIG_STATE_DESC}"  "${UPTIME_STATE_DESC}"  "${LOGHOST_MASTERED_DESC}"  "${LOGHOST_MAINT_DESC}"  "${PRIVNET_STATUS_DESC}"  "${PUBNET_STATUS_DESC}"  "${SERVICE_STATUS_DESC}"  "${RECENT_ERROR_MESG_DESC}" 

#
# Copyright 21 Apr 1997 Sun Microsystems, Inc.  All Rights Reserved.
#
# pragma ident   "@(#)hastat.sh	1.3 98/06/17  SMI"

# hastat.sh
#
# This Utility probes each node configured in the Cluster and Gives Me the 
# cluster perpective from each of the nodes.
#
# hastat [ -i interval ] [ -m message_lines ]
#
# hastat_common file, which has all display formats, is prefixed to 
# this file by the Makefile

init()
{
        set -a

        LD_LIBRARY_PATH=/opt/SUNWcluster/lib
        CLUSTERBIN=/opt/SUNWcluster/bin
        CLUSTERETC=/etc/opt/SUNWcluster
        CLUSTERVAR=/var/opt/SUNWcluster
        SSACLI=${CLUSTERBIN}/pdbssa
        PATH=$PATH:/usr/sbin:/usr/bin/:${CLUSTERBIN}:
        clustname=$(/bin/cat ${CLUSTERETC}/conf/default_clustername)
	if [ $? -ne 0 -o "${clustname}" = "" ]; then
		print "Cannot Get information on the Cluster."
		print "Please Check if this is node is a part of ECS"
		exit 1
	fi
        # export CDB and LOGfile path.
        cdbfile=${CLUSTERETC}/conf/${clustname}.cdb
        cdbfilter=${CLUSTERETC}/conf/cdb.filter
        tmpdir=`enmatch env.tmpdir`
        admindir=${CLUSTERVAR}/admindir
        TMPDIR=${tmpdir}
        TMPSTATE=$TMPDIR/hastat_tmpstate.$$
        TMPERR=$TMPDIR/hastat_tmperr.$$
        TMPDB=$TMPDIR/hastat_tmpdb.$$
        TMPRDB=$TMPDIR/hastat_tmprdb.$$
        localhost=`uname -n`

        set +a

}

# lookup a value in the pdb configuration file
enmatch() {
        ${CLUSTERBIN}/cdbmatch $* ${cdbfile}
}

get_allnodenames()
{
	integer i
	typeset name nodes

	
	nodes=`enmatch cluster.number.nodes`

	i=0
	set -a
	allnodenames=""
	set +a
	found=0
	while [ ${i} -lt ${nodes} ]
	do
		name=`enmatch cluster.node.${i}.hostname`
		allnodenames="${allnodenames} ${name}"
		if [ "${name}" = "${localhost}" ]; then
			found=1
		fi
		i=i+1
	done

	if [ ${found} -eq 1 ]; then
		return
	fi 

	print "Please Check if this node ${localhost} is configured as a part of ECS"
	exit 1

}

write_status_down()
{
   typeset i

   i=$1

   echo ${CONFIG_STATE} > /tmp/hastat.$$.${i}
   echo "     Configuration State on ${i} : Unknown" >> /tmp/hastat.$$.${i}
   echo ${CONFIG_STATE} >> /tmp/hastat.$$.${i}
   echo ${MEMBERSHIP} >> /tmp/hastat.$$.${i}
   echo "     ${i} not a cluster member" >>  /tmp/hastat.$$.${i}
   echo "${MEMBERSHIP}" >> /tmp/hastat.$$.${i}

}

#
# Initialise and parse command line arguments
#

integer count

trap "/bin/rm -f $TMPSTATE $TMPERR $TMPDB $TMPRDB /tmp/hastat* ; exit" 0 1 2 13 15

localhost=$(/bin/uname)
argv0=$(/bin/basename $0)
 
# check who we are - if we are not root, exit immediately
res=$(/bin/id)
if [ `/bin/expr "$res" : "uid=\([0-9]*\)("` != 0 ] ; then
        echo "$argv0 must be executed as super-user." >&2
        exit 1
fi

interval=0
count=0
msg_lines=7

while getopts i:m: c
do
        case $c in
                i) interval=$OPTARG
                   # verify that interval is integer
                   match=`/bin/expr "$interval" : "[0-9][0-9]*"`
                   if [ $match -eq 0 ]; then
                        echo interval must be a positive integer. >&2
                        exit 1
                   fi
                   ;;
                m) msg_lines=$OPTARG
                   match=`/bin/expr "$msg_lines" : "[0-9][0-9]*"`
                   if [ $match -eq 0 ]; then
                        echo message_lines must be a positive integer. >&2
                        exit 1
                   fi
                   ;;
                \?) echo "Usage: $argv0 [ -i interval ] [ -m message_lines ]" \
				>& 2
                   exit 1
                   ;;
        esac
done

init

#
# Main routine for hastat
#
get_allnodenames

echo "Getting Information from all the nodes ......"

while true; do

	for i in ${allnodenames}
	do
		/usr/sbin/ping -n ${i} 3 >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			write_status_down ${i}
			continue
		fi
		# Should call my client program.
		# For Now Calling directly
		rm -rf /tmp/hastat_error.$$.${i}
		/opt/SUNWcluster/bin/hastat_client ${i} \
			${msg_lines} >/tmp/hastat.$$.${i} || \
			echo $? > /tmp/hastat_error.$$.${i} &
	done
	wait

	echo "              HIGH AVAILABILITY CONFIGURATION AND STATUS "
	echo "              -------------------------------------------"
	echo ""
	echo "LIST OF NODES CONFIGURED IN <${clustname}> CLUSTER"
	echo "     ${allnodenames}"
	echo ""

	# we were able to ping the machine but could not get 
	# status # hence write few things about it if we 
	# have some error.
	for i in ${allnodenames}
	do
	   if [ -f /tmp/hastat_error.$$.${i} ]; then
		echo ${CONFIG_STATE} > /tmp/hastat.$$.${i}
		echo "     Configuration State on ${i} : Unknown (Unable to get information)" >> /tmp/hastat.$$.${i}
		echo ${CONFIG_STATE} >> /tmp/hastat.$$.${i}
	   fi
	done

	# Now Parse the Output accordingly.
	#
	m=0
	for i in ${List[*]}
	do
		#	
		# Time to Give Explanations of the Heading.
		#
		echo "${DescList[count]}\n"
		is_log=0
		for j in ${allnodenames}
		do
		    #
		    #  only node can give the over all picture.
		    #
		    test=`/bin/sed -n /${i}/,/${i}/p /tmp/hastat.$$.${j}| \
				grep -v ${i}`
		    if [ ! -z "${test}" ]; then
			/bin/sed -n /${i}/,/${i}/p /tmp/hastat.$$.${j}| \
				grep -v ${i}
		    else
		       if [ "$i" != "${LOGHOST_MAINT}" ]; then
			echo "     Cannot Get Information From ${j}"
		       fi
		    fi
		    if [ "$i" == "${LOGHOST_MAINT}"  ]; then
		 	if [ ! -z ${test} ]; then
				is_log=1
				echo ""
				break;
		 	else
				continue;
			fi
		    fi
		    echo ""
		done
		count=count+1
		if [ "${is_log}" == "0" ]; then
			if [ "${i}" == "${LOGHOST_MAINT}" ]; then
				echo "     None"
				echo ""
			fi
		fi
	done
	
	if [ $interval -eq 0 ]; then
                break
        fi
        echo "\n\n"
        sleep $interval

done  # Infinite loop

/bin/rm -rf /tmp/hastat*
