#! /bin/bash
#
# elxsnmpd     Start/Stop ELXSNMP
#
# chkconfig: 2345 95 65
# description: elxsnmpd is the snmp extension agent required to monitor \
#              Emulex OneConnect hardware
# processname: ELXSNMP 

# Source function library.
. /etc/init.d/functions

# See how we were called.
  
prog_name="OneCommand iSCSI SNMP Daemon"
prog_bin="/usr/sbin/ocmanager/ELXSNMP"
lock_file="/var/lock/subsys/ELXSNMP"
start() {
    flag_controller=0;
    controller_list="19A2:702 19A2:712 10DF:0722";
    
    for controller in $controller_list
    do
        FLAG_CHIP=`lspci -d $controller | awk -F " " '{print $1}' | head -1`
        if [[ -n $FLAG_CHIP ]]
        then
            flag_controller=1;
        fi
    done
    
    if [ $flag_controller = 0 ]
    then
        #Bug 140778 - silently succeed
        snmp_status=0
        #echo
        #echo "Emulex OneConnect not found."
        exit $snmp_status;
    fi
    
    if [ -e $lock_file ];then 
        echo "$prog_name already running"
        exit 0
    else
        lsmod | grep be2iscsi > /tmp/grep.tmp
        RETVAL=$?
        if [ $RETVAL -eq 1 ]
        then
            # do not start the elxsnmp daemon
            rm -rf /tmp/grep.tmp
            exit $RETVAL
        fi
        
        echo -n $"Starting $prog_name: "
        daemon $prog_bin
        RETVAL=$?
        echo
            [ $RETVAL -eq 0 ] && touch $lock_file
        
        return $RETVAL
    fi
}

stop() {
    echo -n $"Stopping $prog_name: "
    killproc $prog_bin
    RETVAL=$?
    echo
        [ $RETVAL -eq 0 ] && rm -f $lock_file
    return $RETVAL
}   

rhstatus() {
    status $prog_bin
}	

restart() {
    stop
    start
}   


case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  status)
    rhstatus
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac
