#!/bin/bash
#
# ndo2db:		Starts the ndoutils ndo2db utility.
#
# chkconfig: - 30 74
# description:  This is a daemon which creates a socket that in then
#               reads from. The expectation is that Nagios will be writing
#               to this socket using nodutils ndomod brokering utility.
# processname: /usr/sbin/ndo2db
# config: /etc/nagios/ndo2db.cfg
# config: /etc/sysconfig/ndo2db
#
### BEGIN INIT INFO
# Provides: ndo2db
# Required-Start: $syslog
# Required-Stop:
# Default-Stop: 0 1 6
# Short-Description: Starts the ndo2db Daemon
# Description:  This is a daemon which creates a socket that in then
#               reads from. The expectation is that Nagios will be writing
#               to this socket using nodutils ndomod brokering utility.
### END INIT INFO

# Sanity checks.
[ -f /etc/nagios/ndo2db.cfg ] || exit 0
[ -x /usr/sbin/ndo2db ] || exit 0

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

NDO2DBUSER=nagios
NDO2DB_OPTIONS="-c /etc/nagios/ndo2db.cfg"
# Source an auxiliary options file if we have one, and pick up NDO2DB_OPTIONS.
[ -r /etc/sysconfig/ndo2db ] && . /etc/sysconfig/ndo2db


RETVAL=0
prog=ndo2db

start () {
    echo -n $"Starting $prog: "
    if [ "$(pidofproc ndo2db)" = "" ]; then
          rm -f /var/run/ndoutils/ndoutils.sock
          if [ ! -d /var/run/ndoutils  ] ; then
              mkdir -p /var/run/ndoutils
              chown $NDO2DBUSER:root /var/run/ndoutils
          fi
          daemon --user=$NDO2DBUSER /usr/sbin/ndo2db $NDO2DB_OPTIONS
          RETVAL=$?
    else
          success
          RETVAL=$?
    fi
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ndo2db
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc ndo2db
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ndo2db
    return $RETVAL
}

restart() {
    stop
    start
}

# See how we were called.
case "$1" in
    start)
	start
	RETVAL=$?
	;;
    stop)
	stop
	RETVAL=$?
	;;
    status)
	status ndo2db
	RETVAL=$?
	;;
    restart)
	restart
	RETVAL=$?
	;;
    reload)
	status ndo2db && restart || start
	RETVAL=$?
	;;
    condrestart)
	[ -e /var/lock/subsys/ndo2db ] && restart
	RETVAL=$?
	;;
    *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart}"
	RETVAL=1
	;;
esac
exit $RETVAL
