#! /bin/sh

# ident "@(#)smond_ctl.sh 1.4	01/03/27 SMI"

CSNMP_PORT=161
SMOND_CONF_FILE=/var/opt/SUNWcluster/smond.conf

pname=$0
args=$*
numargs=$#
hostnames=
snmphost=
UID=`id|sed -e "s/uid=\([0-9][0-9]*\)..*/\1/"`

#
# Usage  
#
usage () {
	echo
        echo "usage: $pname { start | stop }"
        exit 1
}

# stop
#
stop () {

        #
        # Kill smond first
        #
        tlist=/var/opt/SUNWcluster/run/.smond_$$

        # 
        # with -9 to make sure
        #
        ps -ef | egrep smond |egrep -v egrep | egrep -v _ | \
		 egrep -v $$ | \
		nawk '{ print $2 }' > $tlist
        if [ -s $tlist ]; then
	    echo
	    echo "Stopping smond ..."
            kill -9 `cat $tlist` 2> /dev/null
        fi

        rm -f $tlist
}
#------------------------------------------------------------
#
# start
#
start() {

	pid=`ps -ef | egrep snmpd |egrep -v egrep | egrep -v _ | \
                nawk '{ print $2 }'`

	if [ -z "$pid" ]; then
		echo
		echo "SNMP daemon (snmpd) not running !"
		echo "Run CLUSTERHOME/init.snmpd and start snmpd first"
		echo "Then run this script."
		exit 2
	fi

        #
        # call stop to kill everything and start fresh
        #
        stop

        #
        # start smond
        #
	echo
	echo "Starting smond ..."
        CLUSTERHOME/smond -p $CSNMP_PORT -c $SMOND_CONF_FILE 1>/dev/console 2>&1 &
}


if [ $UID -ne 0 ]; then
	echo
        echo "You must be super-user to run this program."
        exit 1
fi

# Ensure conf file exists
#
if [ ! -f $SMOND_CONF_FILE ]; then
	echo
        echo smond_ctl: $SMOND_CONF_FILE does not exist - run smond_conf first
        exit 1
fi

#
# ignore signals while we're in this sensitive area
#
trap '' 1 2 3 15

# Check Args
#
case $1 in
'start')
        start;;
'stop')
        stop;;
*)
        usage;;
esac

