#!/bin/sh
#
# Startup script for NDO2DB
#
# /etc/init.d/ndo2db
# and its symbolic link
# /usr/sbin/rcndo2db
#
### BEGIN INIT INFO
# Provides:         ndo2db
# Required-Start:   $syslog $remote_fs mysql
# Required-Stop:    $syslog $remote_fs mysql
# Default-Start:    3 5
# Default-Stop:     0 1 2 6
# Short-Description: Nagios Data Output Utilities
# Description: Automatic startup and shutdown of Nagios NDO2DB.
#   NDOUtils is an Nagios addon allowing you to store Nagios data (current status
#   information, state history, notification history, etc.) in a MySQL database.
### END INIT INFO

NDO2DB=/usr/sbin/ndo2db
test -x $NDO2DB || { echo "$NDO2DB not installed";
    if [ "$1" = "stop" ]; then exit 0;
    else exit 5; fi; }

NDO2DB_CONFIG=/etc/nagios/ndo2db.cfg
test -r $NDO2DB_CONFIG || { echo "$NDO2DB_CONFIG not existing";
    if [ "$1" = "stop" ]; then exit 0;
    else exit 6; fi; }

. /etc/rc.status
rc_reset


case "$1" in
    start)
        echo -n "Starting ndo2db "
        su - nagios -c "$NDO2DB -c $NDO2DB_CONFIG"
        rc_status -v
    ;;
    stop)
        echo -n "Shutting down ndo2db "
        su - nagios -c "killall -9 $NDO2DB"
        test -f /var/lock/ndo.sock && rm /var/lock/ndo.sock
        rc_status -v
    ;;
    try-restart|condrestart)
        if test "$1" = "condrestart"; then
            echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
        fi
        $0 status
        if test $? = 0; then
            $0 restart
        else
            rc_reset    # Not running is not a failure.
        fi
        # Remember status and be quiet
        rc_status
    ;;
    restart)
        $0 stop
        $0 start
        rc_status
    ;;
    force-reload)
        echo -n "Reload service ndo2db "
        /sbin/killproc -HUP $NDO2DB
        rc_status -v
        ## Otherwise:
        #$0 try-restart
        #rc_status
    ;;
    reload)
        echo -n "Reload service ndo2db "
        /sbin/killproc -HUP $NDO2DB
        rc_status -v
        ## Otherwise:
        #rc_failed 3
        #rc_status -v
    ;;
    status)
        echo -n "Checking for service ndo2db "
        /sbin/checkproc $NDO2DB
        rc_status -v
    ;;
    probe)
        test $NDO2DB_CONFIG -nt /var/run/ndo2db.pid && echo reload
    ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
        exit 1
    ;;
esac
rc_exit
