#!/bin/bash
# 
# Copyright (c) 2011,2012,2013 Andreas Lang-Nevyjel init.at
#
#
# chkconfig: 345 99 01
# description: Icinga network monitor
#
# File : icinga
#
# Description: Starts and stops the Icinga monitor
#              used to provide network services status.
#

#
# RedHat stuff
# chkconfig: 2345 80 20
# description: icinga services
#
### BEGIN INIT INFO
# Provides:      icinga
# Required-Start: $network $syslog md-config-server
# Required-Stop: $network $syslog md-config-server
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description:   start icinga services
### END INIT INFO

export SYSTEMD_NO_WRAP="yes"
if [ -f /etc/rc.status ] ; then
    . /etc/rc.status
else
    . /etc/rc.status_suse
fi
. /etc/init.d/ics_tools.sh

status_icinga () {
    if test ! -f $IcingaRunFile; then
        echo -n "No lock file found in $IcingaRunFile"
        return 1
    fi
    IcingaPID=`head -n 1 $IcingaRunFile`
    if test -x $IcingaCGI/daemonchk.cgi; then
        if $IcingaCGI/daemonchk.cgi -l $IcingaRunFile; then
            return 0
        else
            return 1
        fi
    else
        if ps -p $IcingaPID > /dev/null 2>&1 ; then
            return 0
        else
            return 1
        fi
    fi
    return 1
}


killproc_icinga () {
    if test ! -f $IcingaRunFile; then
        echo "No lock file found in $IcingaRunFile"
        return 1
    fi
    IcingaPID=`head -n 1 $IcingaRunFile`
    kill $2 $IcingaPID
}


# Source function library
# Solaris doesn't have an rc.d directory, so do a test first
if [ -f /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
fi

if [ -f /etc/rc.status ] ; then
    . /etc/rc.status
else
    . /etc/rc.status_suse
fi
. /etc/init.d/ics_tools.sh

prefix=/opt/icinga
exec_prefix=${prefix}
IcingaBin=${exec_prefix}/bin/icinga
IcingaCfgFile=${prefix}/etc/icinga.cfg
IcingaStatusFile=${prefix}/var/status.dat
IcingaTempFile=${prefix}/var/icinga.tmp
IcingaRetentionFile=${prefix}/var/retention.dat
IcingaCommandFile=${prefix}/var/rw/icinga.cmd
IcingaVarDir=${prefix}/var
IcingaRunFile=${prefix}/var/icinga.lock
IcingaLockDir=/var/lock/icinga
IcingaLockFile=icinga
IcingaCGI=${exec_prefix}/sbin
ido2dbBin=${exec_prefix}/bin/ido2db
ido2dbCfg=${prefix}/etc/ndo2db.cfg
icingauser=idnagios
icingagroup=idg
#WSGI_PID=${prefix}/var/wsgi.pid
#WSGI_BIN=/usr/bin/uwsgi-init

mkdir -p ${IcingaLockDir}

rc_reset

# Check that icinga exists.
test -f $IcingaBin || { echo "Icinga not found" ; rc_failed ; rc_status -v ; rc_exit ; }

# Check that icinga.cfg exists.
test -f $IcingaCfgFile || { echo "Icinga not configured" ; rc_failed ; rc_status -v ; rc_exit ; }

# See how we were called.
case "$1" in
    start)
        echo -n "Testing config..."
        $IcingaBin -v $IcingaCfgFile > /dev/null 2>&1
        if [ $? -eq 0 ] ; then
            if [ "$(uname -p)" == "x86_64" ] ; then
                export LD_LIBRARY_PATH=/opt/cluster/lib64
            else
                export LD_LIBRARY_PATH=/opt/cluster/lib
            fi
            if is_redhat ; then
                status ${ido2dbBin} || rm -f ${prefix}/var/ndo.sock ${prefix}/var/ido.sock
            elif is_suse ; then
                checkproc ${ido2dbBin} || rm -f ${prefix}/var/ndo.sock ${prefix}/var/ido.sock
            else
                pidofproc ${ido2dbBin} > /dev/null
            fi
            # start IDO
            if [ -f ${ido2dbCfg} ] ; then
                grep broker ${IcingaCfgFile} > /dev/null && {
                    echo -n "Starting IDO2db utility"
                    $ido2dbBin -c $ido2dbCfg 
                    rc_status -v ; 
                    sleep 1
                }
            fi
            # start Icinga
            echo -n "Starting network monitor: icinga"
            if [ "`id -un`" = "$icingauser" ] ; then
                touch $IcingaVarDir/icinga.log $IcingaRetentionFile
            else
                su -l $Icinga -c "touch $IcingaVarDir/icinga.log $IcingaRetentionFile"
            fi
            rm -f $IcingaCommandFile
            chown -R ${icingauser}.${icingagroup} ${prefix}
            touch $IcingaRunFile
            chown $icingauser.$icingagroup $IcingaRunFile
            $IcingaBin -d $IcingaCfgFile
            [ ! -f $IcingaLockDir/$IcingaLockFile ] && touch $IcingaLockDir/$IcingaLockFile
            chown ${icingauser}.${icingagroup} $IcingaLockDir/$IcingaLockFile
            sleep 1
        else
            echo "Config error, start aborted, check your Icinga configuration"
            rc_failed
        fi
        #status_icinga icinga
        rc_status -v
        ;;

    stop)
        echo -n "Stopping network monitor: icinga"
        killproc_icinga icinga
        rm -f $IcingaStatusFile $IcingaTempFile $IcingaRunFile $IcingaLockDir/$IcingaLockFile $IcingaCommandFile
        for idx in $(seq 10) ; do
            if status_icinga > /dev/null ; then
                echo -n "."
            sleep 1
                else
            break
            fi
        done
        if status_icinga > /dev/null ; then
            echo -n " Warning, Icinga is still running ($idx)"
        else
            echo -n " done (after $idx checks)"
        fi
        rc_status -v
        sleep 1
        if [ -f ${ido2dbCfg} ] ; then
            grep broker ${IcingaCfgFile} > /dev/null && {
                echo -n "Stopping IDO2db utility"
                killall $(basename $ido2dbBin)
                
                rc_status -v
            }
        fi
        ;;

    status)
        echo -n "Checking for running icinga "
        status_icinga icinga
        rc_status -v
        if [ -f ${ido2dbCfg} ] ; then
            grep broker ${IcingaCfgFile} > /dev/null && {
                echo -n "Checking for running IDO2db utility "
                if is_redhat ; then
                    status ${ido2dbBin}
                elif is_suse ; then
                    checkproc ${ido2dbBin}
                else
                    pidofproc ${ido2dbBin} > /dev/null
                fi
                rc_status -v
            }
        fi
        ;;

    restart)
        $0 stop
        sleep 2
        $0 start
        ;;

    reload|force-reload)
        echo -n "Testing config..."
        $IcingaBin -v $IcingaCfgFile > /dev/null 2>&1 && {
            rc_status -v ; 
            IcingaPID=`head -n 1 $IcingaRunFile`
            if status_icinga > /dev/null ; then
                echo -n "Reloading icinga configuration..."
                killproc_icinga icinga -HUP
                rc_status -v
            else
                $0 start
            fi
        } || {
            rc_status -v ;
        }
        ;;

    check)
        echo "Testing config..."
        $IcingaBin -v $IcingaCfgFile 
        rc_status -v 
        ;;
    *)
        echo "Usage: icinga {start|stop|restart|reload|force-reload|status|check}"
        exit 1
        ;;

esac

rc_exit

