nagios4/startup/default-init.in

289 lines
6.8 KiB
Plaintext
Raw Normal View History

2017-05-19 22:22:40 +02:00
#!/bin/sh
2017-05-19 23:37:19 +02:00
#
2017-05-19 22:22:40 +02:00
# chkconfig: 345 99 01
# description: Nagios network monitor
2017-05-19 23:37:19 +02:00
# processname: nagios
2017-05-19 22:22:40 +02:00
# File : nagios
#
# Author : Jorge Sanchez Aymar (jsanchez@lanchile.cl)
2017-05-19 23:37:19 +02:00
#
2017-05-19 22:22:40 +02:00
# Changelog :
#
# 1999-07-09 Karl DeBisschop <kdebisschop@infoplease.com>
# - setup for autoconf
# - add reload function
# 1999-08-06 Ethan Galstad <egalstad@nagios.org>
# - Added configuration info for use with RedHat's chkconfig tool
# per Fran Boon's suggestion
# 1999-08-13 Jim Popovitch <jimpop@rocketship.com>
# - added variable for nagios/var directory
# - cd into nagios/var directory before creating tmp files on startup
# 1999-08-16 Ethan Galstad <egalstad@nagios.org>
# - Added test for rc.d directory as suggested by Karl DeBisschop
# 2000-07-23 Karl DeBisschop <kdebisschop@users.sourceforge.net>
# - Clean out redhat macros and other dependencies
# 2003-01-11 Ethan Galstad <egalstad@nagios.org>
# - Updated su syntax (Gary Miller)
#
# Description: Starts and stops the Nagios monitor
# used to provide network services status.
#
2017-05-19 23:37:19 +02:00
### BEGIN INIT INFO
# Provides: nagios
# Required-Start: $local_fs $syslog $network
# Required-Stop: $local_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts and stops the Nagios monitoring server
# Description: Starts and stops the Nagios monitoring server
### END INIT INFO
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
# Our install-time configuration.
2019-04-18 17:09:18 +02:00
NagiosBin=@bindir@/@nagios_name@
2017-05-19 23:37:19 +02:00
NagiosCfgFile=@sysconfdir@/nagios.cfg
NagiosCfgtestFile=@localstatedir@/nagios.configtest
NagiosStatusFile=@localstatedir@/status.dat
NagiosRetentionFile=@localstatedir@/retention.dat
NagiosCommandFile=@localstatedir@/rw/nagios.cmd
2019-04-18 17:09:18 +02:00
NagiosRunFile=@subsyslockfile@
2017-05-19 23:37:19 +02:00
NagiosVarDir=@localstatedir@
2019-04-18 17:09:18 +02:00
NagiosCGIDir=@cgibindir@
2017-05-19 23:37:19 +02:00
NagiosUser=@nagios_user@
NagiosGroup=@nagios_grp@
checkconfig="true"
# Load any extra environment variables for Nagios and its plugins.
2017-05-19 22:22:40 +02:00
if test -f /etc/sysconfig/nagios; then
. /etc/sysconfig/nagios
fi
2017-05-19 23:37:19 +02:00
# 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 "${RAMDISK_DIR} type tmpfs"`
if [ "$ramdisk"X == "X" ]; then
2019-04-18 17:09:18 +02:00
if [ ! -d ${RAMDISK_DIR} ]; then
mkdir -p -m 0755 ${RAMDISK_DIR}
if [ -d ${RAMDISK_DIR} ]; then
NagiosUserUID=`id -u ${NagiosUser}`
NagiosGroupGID=`id -g ${NagiosGroup}`
mount -t tmpfs -o uid=${NagiosUserUID},gid=${NagiosGroupGID},size=${RAMDISK_SIZE}m tmpfs ${RAMDISK_DIR}
su ${NagiosUser} -c "mkdir -m 0755 ${RAMDISK_DIR}/checkresults"
else
echo "ERROR: Unable to create ramdisk directory (${RAMDISK_DIR})"
exit 1
fi
fi
2017-05-19 23:37:19 +02:00
fi
fi
check_config ()
2017-05-19 22:22:40 +02:00
{
2019-04-18 17:09:18 +02:00
if test "$checkconfig" != "true"; then
return 0
fi
echo -n "Running configuration check... "
@BIN_RM@ -f "$NagiosCfgtestFile";
2017-05-19 23:37:19 +02:00
if test -e "$NagiosCfgtestFile"; then
echo "ERROR: Could not delete '$NagiosCfgtestFile'"
exit 8
fi
2017-10-20 15:43:36 +02:00
if ! su $NagiosUser -c "touch $NagiosCfgtestFile"; then
echo "ERROR: Could not create or update '$NagiosCfgtestFile'"
exit 8
fi
2017-05-19 23:37:19 +02:00
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" > $NagiosCfgtestFile
2019-04-18 17:09:18 +02:00
@BIN_RM@ "$TMPFILE"
2017-05-19 23:37:19 +02:00
return 0
elif test "${ERR}" = "0"; then
# Write the errors to a file we can have a script watching for.
echo "WARNING: Warnings in config files - see log for details: $NagiosCfgtestFile" > $NagiosCfgtestFile
egrep -i "(^warning|^error)" "$TMPFILE" >> $NagiosCfgtestFile
2019-04-18 17:09:18 +02:00
@BIN_RM@ "$TMPFILE"
2017-05-19 23:37:19 +02:00
return 0
else
# Write the errors to a file we can have a script watching for.
echo "ERROR: Errors in config files - see log for details: $NagiosCfgtestFile" > $NagiosCfgtestFile
egrep -i "(^warning|^error)" "$TMPFILE" >> $NagiosCfgtestFile
cat "$TMPFILE"
exit 8
fi
}
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
status_nagios ()
{
2019-04-18 17:09:18 +02:00
if test -x $NagiosCGIDir/daemonchk.cgi; then
if $NagiosCGIDir/daemonchk.cgi -l $NagiosRunFile > /dev/null 2>&1; then
return 0
fi
2017-05-19 22:22:40 +02:00
else
2019-04-18 17:09:18 +02:00
if ps -p $NagiosPID > /dev/null 2>&1; then
return 0
fi
2017-05-19 22:22:40 +02:00
fi
return 1
}
2017-05-19 23:37:19 +02:00
printstatus_nagios ()
2017-05-19 22:22:40 +02:00
{
2017-05-19 23:37:19 +02:00
if status_nagios; then
2017-05-19 22:22:40 +02:00
echo "nagios (pid $NagiosPID) is running..."
else
echo "nagios is not running"
fi
}
killproc_nagios ()
{
2019-04-18 17:09:18 +02:00
@BIN_KILL@ -s "$1" $NagiosPID
2017-05-19 22:22:40 +02:00
}
pid_nagios ()
{
if test ! -f $NagiosRunFile; then
echo "No lock file found in $NagiosRunFile"
2019-04-18 17:09:18 +02:00
return 1
2017-05-19 22:22:40 +02:00
fi
NagiosPID=`head -n 1 $NagiosRunFile`
}
# Check that nagios exists.
if [ ! -f $NagiosBin ]; then
2017-05-19 23:37:19 +02:00
echo "Executable file $NagiosBin not found. Exiting."
2017-05-19 22:22:40 +02:00
exit 1
fi
# Check that nagios.cfg exists.
if [ ! -f $NagiosCfgFile ]; then
2017-05-19 23:37:19 +02:00
echo "Configuration file $NagiosCfgFile not found. Exiting."
2017-05-19 22:22:40 +02:00
exit 1
fi
2017-05-19 23:37:19 +02:00
2017-05-19 22:22:40 +02:00
# See how we were called.
case "$1" in
start)
2019-04-18 17:09:18 +02:00
echo -n "Starting nagios: "
2017-05-19 23:37:19 +02:00
2019-04-18 17:09:18 +02:00
check_config
2017-05-19 23:37:19 +02:00
if test -f $NagiosRunFile; then
NagiosPID=`head -n 1 $NagiosRunFile`
if status_nagios; then
2019-04-18 17:09:18 +02:00
echo "another instance of nagios is already running."
2017-05-19 23:37:19 +02:00
exit 0
fi
2017-05-19 22:22:40 +02:00
fi
2017-05-19 23:37:19 +02:00
2017-10-20 15:43:36 +02:00
su $NagiosUser -c "touch $NagiosVarDir/nagios.log $NagiosRetentionFile"
2019-04-18 17:09:18 +02:00
@BIN_RM@ -f $NagiosCommandFile
2017-05-19 23:37:19 +02:00
touch $NagiosRunFile
$NagiosBin -d $NagiosCfgFile
2019-04-18 17:09:18 +02:00
echo "done."
2017-05-19 22:22:40 +02:00
;;
stop)
2019-04-18 17:09:18 +02:00
echo -n "Stopping nagios: "
2017-05-19 22:22:40 +02:00
pid_nagios
2019-04-18 17:09:18 +02:00
if [ $? -eq 1 ]; then
exit 0
fi
2017-05-19 23:37:19 +02:00
killproc_nagios TERM
2017-05-19 22:22:40 +02:00
2017-05-19 23:37:19 +02:00
# 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
2017-05-19 22:22:40 +02:00
#echo -n 'Waiting for nagios to exit .'
2017-05-19 23:37:19 +02:00
for i in {1..90}; do
if status_nagios > /dev/null; then
echo -n '.'
sleep 1
else
break
fi
done
if status_nagios > /dev/null; then
2019-04-18 17:09:18 +02:00
echo ""
echo "Warning - nagios did not exit in a timely manner - Killing it!"
2017-05-19 23:37:19 +02:00
killproc_nagios KILL
else
2019-04-18 17:09:18 +02:00
echo "done."
2017-05-19 23:37:19 +02:00
fi
2019-04-18 17:09:18 +02:00
@BIN_RM@ -f $NagiosStatusFile $NagiosRunFile $NagiosCommandFile
2017-05-19 22:22:40 +02:00
;;
status)
pid_nagios
2019-04-18 17:09:18 +02:00
if [ $? -eq 1 ]; then
exit 3
fi
2017-05-19 23:37:19 +02:00
printstatus_nagios
2017-05-19 22:22:40 +02:00
;;
checkconfig)
2019-04-18 17:09:18 +02:00
check_config
2017-05-19 23:37:19 +02:00
2017-05-19 22:22:40 +02:00
if [ $? -eq 0 ]; then
2019-04-18 17:09:18 +02:00
echo "OK."
2017-05-19 22:22:40 +02:00
else
2019-04-18 17:09:18 +02:00
echo "CONFIG ERROR! Check your Nagios configuration."
2017-05-19 22:22:40 +02:00
exit 1
fi
;;
restart)
2019-04-18 17:09:18 +02:00
check_config
2017-05-19 23:37:19 +02:00
$0 stop
$0 start
2017-05-19 22:22:40 +02:00
;;
reload|force-reload)
2019-04-18 17:09:18 +02:00
check_config
2017-05-19 23:37:19 +02:00
if test ! -f $NagiosRunFile; then
$0 start
else
pid_nagios
if status_nagios > /dev/null; then
2019-04-18 17:09:18 +02:00
echo -n "Reloading nagios configuration... "
2017-05-19 23:37:19 +02:00
killproc_nagios HUP
echo "done"
2017-05-19 22:22:40 +02:00
else
2017-05-19 23:37:19 +02:00
$0 stop
$0 start
2017-05-19 22:22:40 +02:00
fi
fi
;;
2017-05-19 23:37:19 +02:00
configtest)
$NagiosBin -vp $NagiosCfgFile
;;
2017-05-19 22:22:40 +02:00
*)
2017-05-19 23:37:19 +02:00
echo "Usage: nagios {start|stop|restart|reload|force-reload|status|checkconfig|configtest}"
2017-05-19 22:22:40 +02:00
exit 1
;;
esac
2017-05-19 23:37:19 +02:00
2017-05-19 22:22:40 +02:00
# End of this script