#!/bin/sh
#
# Copyright 1999 Sun Microsystems, Inc. All Rights Reserved
#
# @(#)ss_ha_status.sh	3.5 06/14/99 Sun Microsystems, Inc.
#
# usage: ss_ha_status [ -Z ]
#

PATH="/opt/SUNWicg/SunScreen/bin:/usr/bin"
export PATH

HA_PROGNAME="ss_had"
SS_BINDIR=/opt/SUNWicg/SunScreen/bin
SS_LIBDIR=/opt/SUNWicg/SunScreen/lib
SS_VARDIR=/var/opt/SUNWicg/SunScreen
SS_ETCDIR=/etc/opt/SUNWicg/SunScreen
HA_FILE=${SS_ETCDIR}/.active/HA
HA_PIDFILE=${SS_VARDIR}/ss_had.pid
read SCREENNAME </etc/opt/SUNWicg/SunScreen/name

set_num=80	# SI18N_HA_SS_HA
LS()		# localize a string
{
	msg_num=$1; shift
	if [ -x ${SS_LIBDIR}/catgets ]
	then ${SS_LIBDIR}/catgets $set_num $msg_num "$*"
	else echo "$*\c"
	fi
}

unset SSADM_ISATTY

case $1 in
-Z) MACHINE=true;;	# print in a more machine readable form
"") MACHINE=false;;	# print in a more user readable form
 *) ustr=`LS 100 "Usage"`
    echo>&2 "usage: ssadm ha status [-Z]"; exit 3
esac

if [ -f $HA_PIDFILE ] && read pid <$HA_PIDFILE &&
   /usr/bin/ps -fp$pid | /usr/bin/fgrep $HA_PROGNAME >/dev/null
then :
else /bin/rm -f $HA_PIDFILE
     pid=
fi

#
# Print out the status of the HA configuration file
#
if $MACHINE
then if ${SS_LIBDIR}/ss_ha CHECK >/dev/null
     then echo "HA_STATUS ON"
     else echo "HA_STATUS OFF"
     fi
else ${SS_LIBDIR}/ss_ha CHECK
fi

#
# Print out the status of forwarding packets
#
if ${SS_LIBDIR}/ss_disable_send | fgrep ' ENABLED' >/dev/null
then mode=ACTIVE
else mode=PASSIVE
fi
if $MACHINE
then echo "HA_MODE $mode"
else fmt=`LS 110 "This host is in %s mode."`
     printf "$fmt\n" $mode
fi

#
# Print out the status of the HA daemon.
#
if $MACHINE
then if [ -n "$pid" ]
     then mode=ON
     else mode=OFF
     fi
     echo "HA_DAEMON $mode"
else if [ -n "$pid" ]
     then msg120=`LS 120 "The HA daemon is running with pid %d: HA is on."`
	  printf "$msg120\n" $pid
     else msg122=`LS 122 "The HA daemon is not currently running: HA is off."`
	  echo $msg122
     fi
fi

#
# in machine mode also print out the status of other HA hosts
#
if $MACHINE
then ACTIVE=false
     TOO_MANY_ACTIVE=false

     SSADM_TICKET_FILE=$SS_ETCDIR/.cluster_ticket
     export SSADM_TICKET_FILE

     exec <$HA_FILE
     IFS='" '
     while read type name number addr
     do	[ "$type" = "HA_HOST" ] || continue
	if [ "$number" -eq 1 ]
	then echo "PRIMARY_HOST $name"
	     if [ "$SCREENNAME" != "$name" ]
	     then exit 0
	     fi
	     remote=
	else remote="-r $addr -t 30"
	fi
	if Q=`${SS_BINDIR}/ssadm -n $remote lib/ss_disable_send 2>/dev/null`
	then case $Q in
	     *ENABLED*)
		 if $ACTIVE
		 then TOO_MANY_ACTIVE=true
		 fi
		 ACTIVE=true
		 echo "$name ACTIVE";;
	     *)
		 echo "$name PASSIVE";;
	     esac
	else
	     #
	     # error occured when running ssadm ss_disable_send command
	     #      
	     echo "$name NON-RESPONSIVE"
	fi
     done

     if $ACTIVE
     then :
     else msg130=`LS 130 "ERROR: None of the HA hosts reported being active."`
	  echo>&2 "ssadm ha: $msg130"
	  exit 1
     fi

     if $TOO_MANY_ACTIVE
     then msg131=`LS 131 "ERROR: More than one HA host reported being active."`
	  echo>&2 "ssadm ha: $msg131"
	  exit 2
     fi
fi
