#!/bin/bash

# Naemon - Startup script for the Naemon monitoring daemon
#
# chkconfig:    - 85 15
# description:  Naemon is a service monitoring system
# processname:  naemon
# config:       ${sysconfdir}/naemon/naemon.cfg
# pidfile:      ${localstatedir}/cache/naemon/naemon.pid
#
### BEGIN INIT INFO
# Provides:          naemon
# Required-Start:    
# Required-Stop:    
# Should-Start:
# Should-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: start and stop Naemon monitoring server
# Description:       Naemon is is a service monitoring system
### END INIT INFO

# Source function library.
if [ -e /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
elif [ -e /lib/lsb/init-functions ]; then
  . /lib/lsb/init-functions
fi

prefix="/opt/openitc/nagios"
exec_prefix="${prefix}"
localstatedir="${prefix}/var"
exec="${exec_prefix}/bin/naemon"
prog="naemon"
sysconfdir="${prefix}/etc"
config="/etc/openitcockpit/nagios.cfg"
pidfile="${localstatedir}/nagios.lock"
pidfolder="`dirname $pidfile`"
user="nagios"
group="www-data"
checkconfig="false"

# Default nice for performance, change in sysconfig file not here!
NICELEVEL=0

test -e /etc/sysconfig/$prog && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

status() {
    [ ! -e $pidfile ] && return 1;
    PID=`cat $pidfile`;
    kill -0 $PID
    return $?
}

start() {
    test -x $exec || exit 5
    test -f $config || exit 6
    if [ "$checkconfig" = "false" ]; then
        export ORACLE_HOME="/usr/lib/oracle/11.1.0.1/client64/lib"
	export LD_LIBRARY_PATH="/usr/lib/oracle/11.1.0.1/client64/lib"
        export TNS_ADMIN="/usr/lib/oracle/11.1.0.1/client64/admin/net"
        $0 configtest > /dev/null
        retval=$?
        if [ "$retval" != "0" ]; then
            echo "Cowardly refusing to start, failed to verify config"
            echo "Run '$0 configtest' for more info"
            exit $retval
        fi
    fi
    echo -n "Starting $prog: "
    # Create PID folder if it does not exist
    if [ ! -d $pidfolder ]; then
        mkdir $pidfolder
        chown $user:$group $pidfolder
    fi
    # We need to _make sure_ the precache is there and verified
    # Raise priority to make it run better

    NICE_OPT=""
    if type start-stop-daemon >/dev/null 2>&1; then
        if [ $NICELEVEL != "0" ]; then NICE_OPT="--nicelevel $NICELEVEL"; fi
        start-stop-daemon $NICE_OPT --start --user $user --chuid $user:$group --name $prog --pidfile $pidfile --exec $exec -- -d $config
    else
        daemon --user=$user $exec -d $config
    fi
    retval=$?
    echo
    if [ -d /var/lock/subsys ]; then
      test $retval -eq 0 && touch $lockfile
    fi
    return $retval
}

stop() {
    echo -n "Stopping $prog: "
    if type start-stop-daemon >/dev/null 2>&1; then
        start-stop-daemon --stop --name ${prog} --pidfile ${pidfile} --exec ${exec}
    else
        killproc -p ${pidfile} ${exec}
    fi
    retval=$?
    echo
    test $retval -eq 0 && rm -f $lockfile
    return $retval
}


restart() {
    stop
    start
    return $?
}

reload() {
    echo -n "Reloading $prog: "
    pkill -HUP -u ${user} -f ${exec}
    retval=$?
    echo
}

force_reload() {
    restart
}

case "$1" in
    start)
        $1
        retval=$?
        ;;
    stop)
        $1
        retval=$?
        ;;
    restart)
        $1
        retval=$?
        ;;
    reload)
        status $prog || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        if status $prog; then
          PID=`cat $pidfile`;
          echo "naemon (pid $PID) is running..."
          exit 0
        else
          echo "naemon is stopped"
          exit 1
        fi
        ;;
    condrestart|try-restart)
        status $prog|| exit 0
        restart
        ;;
    configtest|check|checkconfig)
        if type runuser >/dev/null 2>&1; then
          runuser -s /bin/bash - $user -c "$corelimit >/dev/null 2>&1 ; $exec -vp $config"
        else
          /bin/su - -s /bin/sh $user -c "$corelimit >/dev/null 2>&1 ; $exec -vp $config"
        fi
        retval=$?
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
esac
exit $retval
