#!/bin/ksh
# DO NOT EDIT THIS FILE
#pragma ident   "@(#)hastat_common	1.5 00/07/23  SMI"

# Define msg. file name and location
TEXTDOMAIN=hastat;  export TEXTDOMAIN
TEXTDOMAINDIR=/opt/SUNWcluster/locale;  export TEXTDOMAINDIR

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=`gettext "CURRENT MEMBERS OF THE CLUSTER"`
CONFIG_STATE_DESC=`gettext "CONFIGURATION STATE OF THE CLUSTER"`
UPTIME_STATE_DESC=`gettext "UPTIME OF NODES IN THE CLUSTER"`
LOGHOST_MASTERED_DESC=`gettext "LOGICAL HOSTS MASTERED BY THE CLUSTER MEMBERS"`
LOGHOST_BACKUP_DESC=`gettext "LOGICAL HOSTS WHICH HAVE BACKUP-NODES IN THE CLUSTER"`
LOGHOST_MAINT_DESC=`gettext "LOGICAL HOSTS IN MAINTENANCE STATE"` 
PRIVNET_STATUS_DESC=`gettext "STATUS OF PRIVATE NETS IN THE CLUSTER"` 
PUBNET_STATUS_DESC=`gettext "STATUS OF PUBLIC NETS IN THE CLUSTER"`
SERVICE_STATUS_DESC=`gettext "STATUS OF DATA SERVICES RUNNING IN THE CLUSTER"`
RECENT_ERROR_MESG_DESC=`gettext "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.
#
# ident   "@(#)hastat.shi 1.9   01/03/28 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
		lmsg1=`gettext "Cannot Get information on the Cluster."`
		lmsg2=`gettext "Please Check if this node is a part of ECS"`
 		printf "${lmsg1}\n${lmsg2}\n" 
 		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 

	lmsg=`gettext "Please Check if this node %s is configured as a part of ECS"`
 	printf "${lmsg}\n" "${localhost}"
 	exit 1

}


###
#
# hastat_data_service_registration_state
#
#     [4158924] Display all registered data services and their on/off state
#
#     Input:
#	NONE
#
#     Return:
#	0
#
hastat_data_service_registration_state()
{
	typeset clustname
	typeset ccdfile
	typeset allinfo
	typeset info
	typeset svc_status
	typeset svc
	set -A dslist ""
	integer reg_status=0
	integer i
	integer sizeof_svc

        printf "$(gettext 'Status Of Registered Data Services')\n"

	#
	# For each service that is ON in the Cluster.
	#
	clustname=$(cat ${CLUSTERETC}/conf/default_clustername)
	timed_run -q 3 clustm getstate ${clustname} >/dev/null 2>&1 
	if [ $? -ne 0 ]; then
		echo ""
		printf "       $(gettext \
		    'Unable to determine status because this node is not a cluster member').\n"
		return 0
	fi
	ccdfile=$(ccdadm ${clustname} -w)
	allinfo=$(scccd -f ${ccdfile} ${clustname} DS_INFO query ds_name "")

	# Determine if any DS' are registered
	let i=0
	for info in ${allinfo}; do
		svc=`echo ${info} | nawk -F: ' {print $2} '`
		svc_status=`haget -f service_is_on -s ${svc} 2>/dev/null`

		#
		# If ${svc_status} is 0, then ${svc} is registered and off
		# If ${svc_status} is 1, then ${svc} is registered and on
		# If ${svc_status} is empty, then ${svc} is not registered
		#
		if (( svc_status == 0 )); then

			# off state
			dslist[${i}]="${svc}:Off"
			(( i += 1 ))
		elif (( svc_status == 1 )); then

			# on state
			dslist[${i}]="${svc}:On"
			(( i += 1 ))
		fi
	done

	# Display on/off status if any registered data service exists
	if (( i > 0 )); then
		for info in ${dslist[*]}; do
			svc=$(echo ${info} | cut -d: -f1)
			svc_status=$(echo ${info} | cut -d: -f2)
			let sizeof_svc=$(echo ${svc} | wc -c | \
			    nawk '{print $1}')

			#
			# Nicely format output where the maximum length of
			# ${svc} can be 23 characters Otherwise, it won't look
			# "perfect", but it will still be readable.
			#
			if (( sizeof_svc < 8 )); then
				printf "       %s:\t\t\t%s\n" "${svc}" \
				    "${svc_status}"
			else
				if (( sizeof_svc < 16 )); then
					printf "       %s:\t\t%s\n" "${svc}" \
					    "${svc_status}"
				else
					if (( sizeof_svc < 23 )); then
						printf "       %s:\t%s\n" \
						    "${svc}" "${svc_status}"
					else
						printf "       %s: %s\n" \
						    "${svc}" "${svc_status}"
					fi
				fi
			fi
		done
	else
		printf "       $(gettext \
		    'No Data Services have been registered')\n"
		return 0
	fi

	return 0
}


write_status_down()
{
   typeset i

   i=$1

   printf "${CONFIG_STATE}\n" > ${TMPRUN}/hastat.$$.${i}
   lmsg=`gettext "     Configuration State on %s : Unknown"`
   printf "${lmsg}\n" "${i}" >> ${TMPRUN}/hastat.$$.${i}
   printf "${CONFIG_STATE}\n" >> ${TMPRUN}/hastat.$$.${i}
   printf "${MEMBERSHIP}\n" >> ${TMPRUN}/hastat.$$.${i}
   lmsg=`gettext "     %s is not a cluster member"` 
   printf "${lmsg}\n" "${i}" >> ${TMPRUN}/hastat.$$.${i}
   printf "${MEMBERSHIP}\n" >> ${TMPRUN}/hastat.$$.${i}   

}

#
# Initialise and parse command line arguments
#

integer count
TMPRUN=/var/opt/SUNWcluster/run

trap "/bin/rm -f $TMPSTATE $TMPERR $TMPDB $TMPRDB ${TMPRUN}/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
        lmsg=`gettext "%s must be executed as super-user."` 
        printf "${lmsg}\n" "${argv0}" >&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
        		lmsg=`gettext "interval must be a positive integer."` 
        		printf "${lmsg}\n" >&2
                        exit 1
                   fi
                   ;;
                m) msg_lines=$OPTARG
                   match=`/bin/expr "$msg_lines" : "[0-9][0-9]*"`
                   if [ $match -eq 0 ]; then
        		lmsg=`gettext "message_lines must be a positive integer."` 
        		printf "${lmsg}\n" >&2
        		exit 1
                   fi
                   ;;
                \?)lmsg=`gettext "Usage: %s [ -i interval ] [ -m message_lines ]"` 
        	   printf "${lmsg}\n" "${argv0}" >&2
                   exit 1
                   ;;
        esac
done

init

#
# Main routine for hastat
#
get_allnodenames

lmsg=`gettext "Getting Information from all the nodes ......"` 
printf "${lmsg}\n" 

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 ${TMPRUN}/hastat_error.$$.${i}
		/opt/SUNWcluster/bin/hastat_client ${i} \
			${msg_lines} >${TMPRUN}/hastat.$$.${i} || \
			echo $? > ${TMPRUN}/hastat_error.$$.${i} &
	done
	wait

	#
	# TRANSLATION_NOTE
	# the --- line underlines the text and should have the same length
	#
	lmsg1=`gettext "              HIGH AVAILABILITY CONFIGURATION AND STATUS "`
	
        printf "${lmsg1}\n"  
        echo "              -------------------------------------------"
	lmsg2=`gettext "LIST OF NODES CONFIGURED IN <%s> CLUSTER"`
        printf "\n${lmsg2}\n" "${clustname}" 
        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 ${TMPRUN}/hastat_error.$$.${i} ]; then
                echo ${CONFIG_STATE} > ${TMPRUN}/hastat.$$.${i}
   		lmsg=`gettext "     Configuration State on %s : Unknown (Unable to get information)"`
   		printf "${lmsg}\n" "${i}" >> ${TMPRUN}/hastat.$$.${i}
                echo ${CONFIG_STATE} >> ${TMPRUN}/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"

		# 4158924: Determine if any DS' are registered
		if [[ "${i}" == "${SERVICE_STATUS}" ]]; then
			hastat_data_service_registration_state
			echo ""
		fi

		is_log=0
		for j in ${allnodenames}
		do
		    #
		    #  only node can give the over all picture.
		    #
		    test=`/bin/sed -n /${i}/,/${i}/p ${TMPRUN}/hastat.$$.${j}| \
				grep -v ${i}`
		    if [ ! -z "${test}" ]; then
			/bin/sed -n /${i}/,/${i}/p ${TMPRUN}/hastat.$$.${j}| \
				grep -v ${i}
		    else
		       if [ "$i" != "${LOGHOST_MAINT}" ]; then
        		  lmsg=`gettext "     Cannot Get Information From %s"` 
        		  printf  "${lmsg}\n" "${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
        		        lmsg=`gettext "     None"` 
        		        printf  "${lmsg}\n\n" 
			fi
		fi
	done
	
	if [ $interval -eq 0 ]; then
                break
        fi
        echo "\n\n"
        sleep $interval

done  # Infinite loop

/bin/rm -rf ${TMPRUN}/hastat*
