#!/bin/sh
#
# Copyright (C) 2002 Yixiong Zou (yixiong.zou@intel.com)
# Support:      linux-ha-dev@lists.tummy.com
# License:      GNU General Public License (GPL)
#
set +x

#
#	BasicSanityCheck has to run as root.
#	
#	Perhaps a better way to do this is to use the cl_status command...
#		-- AlanR
#	
NODE0=`uname -n`
NODE1=imalwaysdead.com

# make sure the agent and the subagent are dead
killall -9 hbagent
killall -9 snmpd

# make sure the snmpd is running and agentX support is turned on
GenerateSNMPDConf() {
	cat <<-! >$1
		rocommunity public
		master yes
		trap2sink localhost
		!
	chmod 644 $1
}

which snmpd
ret=$?
if test $ret != 0 
	then echo "snmpd file is not in the path, skip agent test..."
	exit $ret
fi

# start the snmpd manually with our own config file
SNMPDCONF=/var/run/testsnmpd.conf
SNMPPIDFILE=/var/run/testsnmpd.pid
SNMPD=`which snmpd`
GenerateSNMPDConf $SNMPDCONF
$SNMPD -C -c $SNMPDCONF -p $SNMPPIDFILE

#/etc/ha.d/init.d/snmpd status 
#ret=$?
#if test $ret != 0 
#	then echo "snmpd cannot be started correctly. skip agent test..."
#	exit $ret
#else 
#	echo "snmpd is already running, good"
#fi

# we want to wait for a couple seconds to let the master agent fully
# ready before we start the subagent.
sleep 3


# start the linux-ha snmp subagent
/usr/local/lib/heartbeat/hbagent -d &
sleep 1

# get the nodename for node0 and node1
# node0 should be the value of the localhost
# node1 should be "ImAlwaysDead.com"

export MIBS=ALL
#snmpwalk -v2c localhost -c public LinuxHA
node0=`snmpget -v2c localhost -c public LHANodeName.1 | sed -ne 's/LINUX-HA-MIB::LHANodeName.1 = STRING: //p'`
echo node0 = $node0
node1=`snmpget -v2c localhost -c public LHANodeName.2 | sed -ne 's/LINUX-HA-MIB::LHANodeName.2 = STRING: //p'`
echo node1 = $node1

ret=1
ret2=1
for name in $NODE0 $NODE1; do
	echo name = $name, node0 = $node0
	if test "$node0" = "$name"; then
		ret=0
		echo "found $node0"
		break
	fi
done

if test $ret = 0; then
	for name in $NODE0 $NODE1; do
		echo name = $name, node1 = $node1
		if test "$node1" = "$name"; then
			ret2=0
			echo "found $node1"
			break
		fi
	done
fi

if test $ret2 = 0; then
	echo "BasicSanityCheck for SNMP Subagent passed."
	exit 0
else 
	echo "BasicSanityCheck for SNMP Subagent failed."
	exit 1
fi
if
  [ -f $SNMPPIDFILE -a ! -z $SNMPPIDFILE ]
then
  kill `cat $SNMPPIDFILE`
  rm $SNMPPIDFILE
fi
