#!/bin/bash
#
# chkconfig: 35 97 35
# 
# description: snmpsa is used to start/stop snmp sub agent
#
### BEGIN INIT INFO
# Provides: snmpsa
# Required-Start: $local_fs $remote_fs $syslog
# Required-Stop:
# Default-Start:  3 5
# Default-Stop:
# Short-Description: snmpsa service
# Description: snmpsa is used to start / stop snmp sub agnt
### END INIT INFO


SBIND=/sbin/
PIDOF=${SBIND}pidof
LSMOD=${SBIND}lsmod
KILLPID=killpid
#environment variables 

MIBS=ALL
export MIBS

getpid () {
    p=`ps -ef |grep $1 |grep -v grep |awk '{print $2}'`
    echo $p
}

killpid () {
    p=`getpid $1`
    kill -9 $p
}

addcronjob ()
{
         # start crond service - in case its not up by default
         if [ -e "/etc/SuSE-release" ]; then
            /etc/init.d/cron start 2> /dev/null 1 > /dev/null
         else
            /etc/init.d/crond start 2>&1 > /dev/null
         fi;
 
         crontab -l | grep -v "^#" |grep -v "snmpsa_watchdog.sh" > /tmp/__snmpsacronjoblist__
         echo "* * * * * /usr/local/snmpsa/snmpsa_watchdog.sh 2>&1 >/dev/null" >> /tmp/__snmpsacronjoblist__  
         crontab /tmp/__snmpsacronjoblist__
         rm -f /tmp/__snmpsacronjoblist__
}

delcronjob ()
{
   crontab -l | grep -v "/usr/local/snmpsa/snmpsa_watchdog.sh" > /tmp/__snmpsacronjoblist__
   crontab /tmp/__snmpsacronjoblist__
   rm -f /tmp/__snmpsacronjoblist__
}

start_sa()
{
    # logf=/usr/local/snmpsa/logfile
    logf=/var/log/snmpsa.log
    mydate=`date`
    export `awk -v n="AgentAddress" '$0 ~ n {print " SA_ADDR=" $3}' /usr/local/snmpsa/conf/snmpsa.conf`
    echo "snmpsa smSubagent started $mydate" >>$logf
    /usr/local/snmpsa/bin/smSubagent >>$logf 2>>$logf
}

if [ ! -f /etc/init.d/functions ]
then
   # special for MontaVista
   # . /etc/init.d/init-functions
   PIDOF=getpid
   # /usr/bin/lsmod
   LSMOD=lsmod
   KILLPID=killpid
fi

case "$1" in 

"saonly")
	if [ "`$PIDOF snmpd`" != "" ]
	then
	    if [ "`$PIDOF smSubagent`" != "" ]
	    then
		echo "The SNMP subagent is already running."
		echo "To restart the subagent, run 'snmpsa restart'."
	    else
                start_sa
				sleep 1
				#addcronjob
	    fi
	else
	   echo "The SNMP subagent cannot start."
	   echo "The snmpd service needs to be running before starting the subagent."
	fi
	;;

"start")
	if [ "`$PIDOF snmpd`" != "" ]
	then
	    if [ "`$PIDOF smSubagent`" != "" ]
	    then
	        echo "The SNMP subagent is already running."
		echo "To restart the subagent, run 'snmpsa restart'."
	    else
		sleep 1
                start_sa
		#sleep 1
		#		addcronjob
	    fi
	else
	   echo "The SNMP subagent cannot start."
	   echo "The snmpd service needs to be running before starting the subagent."
	fi
	;;

"stop")
        if [ "`$PIDOF smSubagent`" != "" ]
	then

	    $KILLPID smSubagent
		#delcronjob
	else
	    echo "The SNMP subagent is not running."
	fi
	;;

"restart")
        if [ "`$PIDOF smSubagent`" != "" ]
	then
	    $KILLPID smSubagent
	fi
	if [ "`$PIDOF snmpd`" != "" ]
	then
            start_sa
    	else
	   echo "The SNMP subagent cannot start."
	   echo "The snmpd service needs to be running before starting the subagent."
	fi
	;;

"status")
        if [ "`$PIDOF smSubagent`" != "" ]
	then 
	    echo "The SNMP subagent is running."
	else
	    echo "The SNMP subagent is stopped."
	fi
	;;

*)
	echo snmpsa:Unknown Argument $arg
	echo Usage:snmpsa [start/stop/restart/saonly/status]
	exit 1
	;;
esac





