So I'm trying to fix the init script that comes with nagios.  It
fundamentally relies upon a program which doesn't come with the RPM,
wouldn't work if it did, and that cannot be compiled from source.  So...

When I start nagios, I want to capture the PID to sock away in a
runfile.  This was a little problematic because ps likes to add spaces
to the beginning of the PID so the right edges will line up in the
output.  "ps -C nagios", however, returns a line with the PID aligned to
the left, so I'm in like Flynn, right?  Wrong!

>From the command line, sudo /usr/bin/nagios -d /etc/nagios/nagios.cfg &&
ps -C nagios returns one line of output.  Inside of the init script,
though, I wind up with three lines, and I'll be damned if I know why.  I
munged around that by redirecting the output to a temp file and tailing
that temp file into the runfile, and that seems to be working.  But I
dislike kludges.

Does anyone else use nagios with an init script?  How did you make it
work?  Here's what I wound up with:

#!/bin/sh
#
# chkconfig: 345 99 01
# description: Nagios network monitor
#
# File : nagios
#
# Author : Jorge Sanchez Aymar ([EMAIL PROTECTED])
#
# Changelog :
#
# 1999-07-09 Karl DeBisschop <[EMAIL PROTECTED]>
#  - setup for autoconf
#  - add reload function
# 1999-08-06 Ethan Galstad <[EMAIL PROTECTED]>
#  - Added configuration info for use with RedHat's chkconfig tool
#    per Fran Boon's suggestion
# 1999-08-13 Jim Popovitch <[EMAIL PROTECTED]>
#  - added variable for nagios/var directory
#  - cd into nagios/var directory before creating tmp files on startup
# 1999-08-16 Ethan Galstad <[EMAIL PROTECTED]>
#  - Added test for rc.d directory as suggested by Karl DeBisschop
# 2000-07-23 Karl DeBisschop <[EMAIL PROTECTED]>
#  - Clean out redhat macros and other dependencies
# 2003-01-11 Ethan Galstad <[EMAIL PROTECTED]>
#  - Updated su syntax (Gary Miller)
# 2008-04-03 John Oliver
#  - Removed daemoncheck nonsense so script actually works
#
# Description: Starts and stops the Nagios monitor
#              used to provide network services status.
#

status_nagios ()
{

        if ps -p $NagiosPID > /dev/null 2>&1; then
                return 0
        else
                return 1
        fi
}


printstatus_nagios()
{

        if status_nagios $1 ; then
                echo "nagios (pid $NagiosPID) is running..."
        else
                echo "nagios is not running"
        fi
}


killproc_nagios ()
{

        kill $2 $NagiosPID

}


pid_nagios ()
{

        if test ! -s $NagiosRunFile; then
                echo "No lock file found in $NagiosRunFile"
                exit 1
        fi

        NagiosPID=`head -n 1 $NagiosRunFile`
}


# 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

prefix=/usr
exec_prefix=/usr
NagiosBin=/usr/bin/nagios
NagiosCfgFile=/etc/nagios/nagios.cfg
NagiosStatusFile=/var/log/nagios/status.dat
NagiosTempFile=/var/log/nagios/nagios.tmp
NagiosRetentionFile=/var/log/nagios/retention.dat
NagiosCommandFile=/var/log/nagios/rw/nagios.cmd
NagiosVarDir=/var/log/nagios
NagiosRunFile=/var/run/nagios.pid
NagiosLockDir=/var/lock/subsys
NagiosLockFile=nagios
NagiosCGIDir=/usr/lib/nagios/cgi
NagiosUser=nagios
NagiosGroup=nagios


# Check that nagios exists.
if [ ! -f $NagiosBin ]; then
    echo "Executable file $NagiosBin not found.  Exiting."
    exit 1
fi

# Check that nagios.cfg exists.
if [ ! -f $NagiosCfgFile ]; then
    echo "Configuration file $NagiosCfgFile not found.  Exiting."
    exit 1
fi

# See how we were called.
case "$1" in

        start)
                echo -n "Starting nagios:"
                $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
                if [ $? -eq 0 ]; then
                        su - $NagiosUser -c "touch
$NagiosVarDir/nagios.log $NagiosRetentionFile"
                        rm -f $NagiosCommandFile
#                       touch $NagiosRunFile
#                       chown $NagiosUser:$NagiosGroup $NagiosRunFile
                        $NagiosBin -d $NagiosCfgFile && ps -C nagios |
cut -d" " -f 1 > /tmp/nagios; tail -1 /tmp/nagios > $NagiosRunFile; rm
-f /tmp/nagios
                        if [ -d $NagiosLockDir ]; then touch
$NagiosLockDir/$NagiosLockFile; fi
                        echo " done."
                        exit 0
                else
                        echo "CONFIG ERROR!  Start aborted.  Check your
Nagios configuration."
                        exit 1
                fi
                ;;

        stop)
                echo -n "Stopping nagios: "

                pid_nagios
                killproc_nagios nagios

                # now we have to wait for nagios to exit and remove its
                # own NagiosRunFile, otherwise a following "start" could
                # happen, and then the exiting nagios will remove the
                # new NagiosRunFile, allowing multiple nagios daemons
                # to (sooner or later) run - John Sellens
                #echo -n 'Waiting for nagios to exit .'
                for i in 1 2 3 4 5 6 7 8 9 10 ; do
                    if status_nagios > /dev/null; then
                        echo -n '.'
                        sleep 1
                    else
                        break
                    fi
                done
                if status_nagios > /dev/null; then
                    echo ''
                    echo 'Warning - nagios did not exit in a timely
manner'
                else
                    echo 'done.'
                fi

                rm -f $NagiosStatusFile $NagiosTempFile $NagiosRunFile
$NagiosLockDir/$NagiosLockFile $NagiosCommandFile
                ;;

        status)
                pid_nagios
                printstatus_nagios nagios
                ;;

        restart)
                printf "Running configuration check..."
                $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
                if [ $? -eq 0 ]; then
                        echo "done"
                        $0 stop
                        $0 start
                else
                        #$NagiosBin -v $NagiosCfgFile
                        echo " FAILED!  Restart aborted.  Check your
Nagios configuration."
                        exit 1
                fi
                ;;

        reload|force-reload)
                printf "Running configuration check..."
                $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
                if [ $? -eq 0 ]; then
                        echo "done"
                        if test ! -f $NagiosRunFile; then
                                $0 start
                        else
                                pid_nagios
                                if status_nagios > /dev/null; then
                                        printf "Reloading nagios
configuration..."
                                        killproc_nagios nagios -HUP
                                        echo "done"
                                else
                                        $0 stop
                                        $0 start
                                fi
                        fi
                else
                        #$NagiosBin -v $NagiosCfgFile
                        echo " FAILED!  Reload aborted.  Check your
Nagios configuration."
                        exit 1
                fi
                ;;

        *)
                echo "Usage: nagios
{start|stop|restart|reload|force-reload|status}"
                exit 1
                ;;

esac

# End of this script

-- 
***********************************************************************
* John Oliver                             http://www.john-oliver.net/ *
*                                                                     *
***********************************************************************


-- 
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list

Reply via email to