Page 1 of 2

Nagios Core 4.0.4 init script

Posted: Mon Mar 17, 2014 12:56 pm
by anton.c42
Nagios Core version: 4.0.4
OS: CentOS 6.5 (64-bit)

Looking at the changelog for version 4.0.4, I see that the init script was re-written. Previous to this version, I was able to start/stop/restart Nagios without any problem, but now it seems that it is required to have the Nagios user have a login shell (/bin/bash vs. /sbin/nologin). I have always had my daemon user's login shell disabled, as is good security practice.

I'm wondering why the init script now requires a login shell when it never did before? Is there a way to modify the init script so it doesn't?

Thanks!

Re: Nagios Core 4.0.4 init script

Posted: Mon Mar 17, 2014 1:02 pm
by tmcdonald
What command are you using to restart/start/stop Nagios? We've had some people report problems using /etc/init.d/ scripts so any additional info you can give us would help in identifying the issue.

As for the requirement of a shell, I will have to talk to the developers for an official answer.

Re: Nagios Core 4.0.4 init script

Posted: Mon Mar 17, 2014 1:18 pm
by anton.c42
I have used "service nagios start" and "/etc/init.d/nagios start", which are basically the same thing as far as I know. I have also restarted the server, and Nagios is set to start on system startup (using the chkconfig utility). All of these methods give the error "This account is currently not available." and Nagios fails to start. In the past, this error always showed up, but Nagios always started. I'm pretty sure this error was because I had the login shell set to "/sbin/nologin".When I now set the shell to "/bin/bash", the error doesn't appear and Nagios starts correctly.

If it's not too much trouble, an official answer would be nice. I'm not really comfortable with having a daemon user have a login shell, and I suspect most others wouldn't want this either. If I need to submit a bug report or feature request to get this fixed in the next version, I'd be willing to do that.

Thanks!

Re: Nagios Core 4.0.4 init script

Posted: Mon Mar 17, 2014 1:47 pm
by scottwilkerson
Would it be possible to post your current /etc/init.d/nagios ?

Thanks

Re: Nagios Core 4.0.4 init script

Posted: Mon Mar 17, 2014 2:38 pm
by anton.c42
Sure, here it is:

Code: Select all

#!/bin/sh
# 
# chkconfig: 345 99 01
# description: Nagios network monitor
# processname: nagios
# 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)
#
# Description: Starts and stops the Nagios monitor
#              used to provide network services status.
#
### BEGIN INIT INFO
# Provides:             nagios
# Required-Start:       $local_fs $syslog $network
# Required-Stop:        $local_fs $syslog $network
# Short-Description:    Starts and stops the Nagios monitoring server
# Description:          Starts and stops the Nagios monitoring server
### END INIT INFO

# Load any extra environment variables for Nagios and its plugins
if test -f /etc/sysconfig/nagios; then
        . /etc/sysconfig/nagios
fi

# Source function library
# Some *nix do not 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
elif [ -f /lib/lsb/init-functions ]; then
        . /lib/lsb/init-functions
fi

prefix=/opt/nagios
exec_prefix=${prefix}
NagiosBin=${exec_prefix}/bin/nagios
NagiosCfgFile=${prefix}/etc/nagios.cfg
NagiosStatusFile=${prefix}/var/status.dat
NagiosRetentionFile=${prefix}/var/retention.dat
NagiosCommandFile=${prefix}/var/rw/nagios.cmd
NagiosVarDir=${prefix}/var
NagiosRunFile=${prefix}/var/nagios.lock
NagiosLockDir=/var/lock/subsys
NagiosLockFile=nagios
NagiosCGIDir=${exec_prefix}/sbin
NagiosUser=nagios
NagiosGroup=nagios
checkconfig="true"

# Automate addition of RAMDISK based on environment variables
USE_RAMDISK=${USE_RAMDISK:-0}
if test "$USE_RAMDISK" -ne 0 && test "$RAMDISK_SIZE"X != "X"; then
        ramdisk=`mount |grep "$ramdiskdir type tmpfs"`
        if [ "$ramdisk"X == "X" ]; then
                mkdir -p -m 0755 ${RAMDISK_DIR}
                mount -t tmpfs -o size=${RAMDISK_SIZE}m tmpfs ${RAMDISK_DIR}
                mkdir -p -m 0755 ${RAMDISK_DIR}/checkresults
                chown -R $NagiosUser:$NagiosGroup ${RAMDISK_DIR}
        fi
fi

check_config() {
        TMPFILE=$(mktemp /tmp/.configtest.XXXXXXXX)
        su - $NagiosUser -c "$NagiosBin -vp $NagiosCfgFile" > "$TMPFILE"
        WARN=`grep ^"Total Warnings:" "$TMPFILE" |awk -F: '{print \$2}' |sed s/' '//g`
        ERR=`grep ^"Total Errors:" "$TMPFILE" |awk -F: '{print \$2}' |sed s/' '//g`

        if test "$WARN" = "0" && test "${ERR}" = "0"; then
                echo "OK - Configuration check verified" > /var/run/nagios.configtest
                chmod 0644 /var/run/nagios.configtest
                /bin/rm "$TMPFILE"
        return 0
        elif test "${ERR}" = "0"; then
                # We'll write out the errors to a file we can have a
                # script watching for
                echo "WARNING: Warnings in config files - see log for details: /var/run/nagios.configtest" > /var/run/nagios.configtest
                egrep -i "(^warning|^error)" "$TMPFILE" >> /var/run/nagios.configtest
                chmod 0644 /var/run/nagios.configtest
                /bin/rm "$TMPFILE"
        return 0
        else
                # We'll write out the errors to a file we can have a
                # script watching for
                echo "ERROR: Errors in config files - see log for details: /var/run/nagios.configtest" > /var/run/nagios.configtest
                egrep -i "(^warning|^error)" "$TMPFILE" >> /var/run/nagios.configtest
                chmod 0644 /var/run/nagios.configtest
                cat "$TMPFILE"
        exit 8
        fi
}

status_nagios ()
{

        if test -x $NagiosCGI/daemonchk.cgi; then
                if $NagiosCGI/daemonchk.cgi -l $NagiosRunFile; then
                        return 0
                else
                        return 1
                fi
        else
                if ps -p $NagiosPID > /dev/null 2>&1; then
                        return 0
                else
                        return 1
                fi
        fi

        return 1
}


printstatus_nagios()
{

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


killproc_nagios ()
{

        kill $2 $NagiosPID

}


pid_nagios ()
{

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

        NagiosPID=`head -n 1 $NagiosRunFile`
}

         

# 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:"
                if test "$checkconfig" = "true"; then
                        check_config
                fi
                if [ $? -eq 0 ]; then
                        su - $NagiosUser -c "touch $NagiosVarDir/nagios.log $NagiosRetentionFile"
                        rm -f $NagiosCommandFile
                        touch $NagiosRunFile
                        chown $NagiosUser:$NagiosGroup $NagiosRunFile
                        $NagiosBin -d $NagiosCfgFile
                        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 $NagiosRunFile $NagiosLockDir/$NagiosLockFile $NagiosCommandFile
                ;;

        status)
                pid_nagios
                printstatus_nagios nagios
                ;;

        checkconfig)
                if test "$checkconfig" = "true"; then
                        printf "Running configuration check..."
                        check_config
                fi

                if [ $? -eq 0 ]; then
                        echo " OK."
                else
                        echo " CONFIG ERROR!  Check your Nagios configuration."
                        exit 1
                fi
                ;;

        restart)
                if test "$checkconfig" = "true"; then
                        printf "Running configuration check..."
                        check_config
                fi

                $0 stop
                $0 start

                ;;

        reload|force-reload)
                if test "$checkconfig" = "true"; then
                        printf "Running configuration check..."
                        check_config
                fi

                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

                ;;

        configtest)
                su - $NagiosUser -c "$NagiosBin -vp $NagiosCfgFile"

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

esac
  
# End of this script

Re: Nagios Core 4.0.4 init script

Posted: Mon Mar 17, 2014 3:25 pm
by scottwilkerson
I believe I have found the problem, and tested it with a setup like yours. You should be able to use the following in your /etc/init.d/nagios

Code: Select all

#!/bin/sh
#
# chkconfig: 345 99 01
# description: Nagios network monitor
# processname: nagios
# 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)
#
# Description: Starts and stops the Nagios monitor
#              used to provide network services status.
#
### BEGIN INIT INFO
# Provides:             nagios
# Required-Start:       $local_fs $syslog $network
# Required-Stop:        $local_fs $syslog $network
# Short-Description:    Starts and stops the Nagios monitoring server
# Description:          Starts and stops the Nagios monitoring server
### END INIT INFO

# Load any extra environment variables for Nagios and its plugins
if test -f /etc/sysconfig/nagios; then
        . /etc/sysconfig/nagios
fi

# Source function library
# Some *nix do not 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
elif [ -f /lib/lsb/init-functions ]; then
        . /lib/lsb/init-functions
fi

prefix=/opt/nagios
exec_prefix=${prefix}
NagiosBin=${exec_prefix}/bin/nagios
NagiosCfgFile=${prefix}/etc/nagios.cfg
NagiosStatusFile=${prefix}/var/status.dat
NagiosRetentionFile=${prefix}/var/retention.dat
NagiosCommandFile=${prefix}/var/rw/nagios.cmd
NagiosVarDir=${prefix}/var
NagiosRunFile=${prefix}/var/nagios.lock
NagiosLockDir=/var/lock/subsys
NagiosLockFile=nagios
NagiosCGIDir=${exec_prefix}/sbin
NagiosUser=nagios
NagiosGroup=nagios
checkconfig="true"

# Automate addition of RAMDISK based on environment variables
USE_RAMDISK=${USE_RAMDISK:-0}
if test "$USE_RAMDISK" -ne 0 && test "$RAMDISK_SIZE"X != "X"; then
        ramdisk=`mount |grep "$ramdiskdir type tmpfs"`
        if [ "$ramdisk"X == "X" ]; then
                mkdir -p -m 0755 ${RAMDISK_DIR}
                mount -t tmpfs -o size=${RAMDISK_SIZE}m tmpfs ${RAMDISK_DIR}
                mkdir -p -m 0755 ${RAMDISK_DIR}/checkresults
                chown -R $NagiosUser:$NagiosGroup ${RAMDISK_DIR}
        fi
fi

check_config() {
        TMPFILE=$(mktemp /tmp/.configtest.XXXXXXXX)
        $NagiosBin -vp $NagiosCfgFile > "$TMPFILE"
        WARN=`grep ^"Total Warnings:" "$TMPFILE" |awk -F: '{print \$2}' |sed s/' '//g`
        ERR=`grep ^"Total Errors:" "$TMPFILE" |awk -F: '{print \$2}' |sed s/' '//g`

        if test "$WARN" = "0" && test "${ERR}" = "0"; then
                echo "OK - Configuration check verified" > /var/run/nagios.configtest
                chmod 0644 /var/run/nagios.configtest
                /bin/rm "$TMPFILE"
        return 0
        elif test "${ERR}" = "0"; then
                # We'll write out the errors to a file we can have a
                # script watching for
                echo "WARNING: Warnings in config files - see log for details: /var/run/nagios.configtest" > /var/run/nagios.configtest
                egrep -i "(^warning|^error)" "$TMPFILE" >> /var/run/nagios.configtest
                chmod 0644 /var/run/nagios.configtest
                /bin/rm "$TMPFILE"
        return 0
        else
                # We'll write out the errors to a file we can have a
                # script watching for
                echo "ERROR: Errors in config files - see log for details: /var/run/nagios.configtest" > /var/run/nagios.configtest
                egrep -i "(^warning|^error)" "$TMPFILE" >> /var/run/nagios.configtest
                chmod 0644 /var/run/nagios.configtest
                cat "$TMPFILE"
        exit 8
        fi
}

status_nagios ()
{

        if test -x $NagiosCGI/daemonchk.cgi; then
                if $NagiosCGI/daemonchk.cgi -l $NagiosRunFile; then
                        return 0
                else
                        return 1
                fi
        else
                if ps -p $NagiosPID > /dev/null 2>&1; then
                        return 0
                else
                        return 1
                fi
        fi

        return 1
}


printstatus_nagios()
{

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


killproc_nagios ()
{

        kill $2 $NagiosPID

}


pid_nagios ()
{

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

        NagiosPID=`head -n 1 $NagiosRunFile`
}

         

# 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:"
                if test "$checkconfig" = "true"; then
                        check_config
                fi
                if [ $? -eq 0 ]; then
                        touch $NagiosVarDir/nagios.log $NagiosRetentionFile
                        rm -f $NagiosCommandFile
                        touch $NagiosRunFile
                        chown $NagiosUser:$NagiosGroup $NagiosRunFile
                        $NagiosBin -d $NagiosCfgFile
                        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 $NagiosRunFile $NagiosLockDir/$NagiosLockFile $NagiosCommandFile
                ;;

        status)
                pid_nagios
                printstatus_nagios nagios
                ;;

        checkconfig)
                if test "$checkconfig" = "true"; then
                        printf "Running configuration check..."
                        check_config
                fi

                if [ $? -eq 0 ]; then
                        echo " OK."
                else
                        echo " CONFIG ERROR!  Check your Nagios configuration."
                        exit 1
                fi
                ;;

        restart)
                if test "$checkconfig" = "true"; then
                        printf "Running configuration check..."
                        check_config
                fi

                $0 stop
                $0 start

                ;;

        reload|force-reload)
                if test "$checkconfig" = "true"; then
                        printf "Running configuration check..."
                        check_config
                fi

                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

                ;;

        configtest)
                su - $NagiosUser -c "$NagiosBin -vp $NagiosCfgFile"

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

esac
 
# End of this script
Just 2 lines were changed, 84 & 186

Re: Nagios Core 4.0.4 init script

Posted: Mon Mar 17, 2014 4:21 pm
by anton.c42
Thank you so much for your quick replies and all your help. It works perfectly now!

Should I submit a bug report for this?

Re: Nagios Core 4.0.4 init script

Posted: Mon Mar 17, 2014 4:23 pm
by slansing
I believe Scott actually made the changes already, and is working on pushing them to the current, downloadable source.

Re: Nagios Core 4.0.4 init script

Posted: Mon Mar 17, 2014 4:23 pm
by millisa
I ran into this too - tested the fixes above (+1 more listed below) and it appears to correct the init script in the 4.0.4 tar.gz

One more entry, about 10 lines from the bottom:

Code: Select all

        configtest)
                su - $NagiosUser -c "$NagiosBin -vp $NagiosCfgFile"
change to

Code: Select all

        configtest)
              $NagiosBin -vp $NagiosCfgFile

Re: Nagios Core 4.0.4 init script

Posted: Mon Mar 17, 2014 4:33 pm
by anton.c42
Thanks millisa, I changed the line you suggested as well and now the config test function works.

Thank you all again for your help.