4 # description: Nagios network monitoring daemon
10 # Default-Start: 2 3 5
12 # Description: Nagios network monitoring daemon
15 # Author: Lars Michelsen <lm@mathias-kettner.de>
17 # Notes for OMD init script requirements
18 # - Must handle omd config options like daemon enabling/disabling
19 # - When a daemon is disabled by omd config it needs
20 # to return an exit code of 5.
21 # - The init script must output an exit code of 2 when
22 # an unknown param is used.
23 # - In general the exit code for succeeded actions is
24 # 0 and for failed actions it is 1.
25 # - There are exceptions for the exit code handling:
26 # - When a service is already stopped and should be
27 # restarted/stopped, it should result in an exit code of 0.
28 # - When a service is already running and should be started
29 # this also should result in an exit code of 0.
30 # - When a restart is requested and the program is still not running
31 # the script should only execute a start
32 # - When a restart is requested and the program can not be stopped the
33 # script should terminate without starting the daemon
34 # - When a reload is requested and the program is not running
35 # the init script should execute a start instead
39 . lib
/omd
/init_profile
41 [ "$CONFIG_CORE" = "nagios" ] ||
exit 5
43 BIN
=###ROOT###/bin/nagios
44 CFG_FILE
=###ROOT###/tmp/nagios/nagios.cfg
45 STATUS_FILE
=###ROOT###/tmp/nagios/status.dat
46 CMD_FILE
=###ROOT###/tmp/run/nagios.cmd
47 PID_FILE
=###ROOT###/tmp/lock/nagios.lock
48 STATUS_DAT
=###ROOT###/tmp/nagios/status.dat
49 CHECKRESULTS_DIR
=###ROOT###/tmp/nagios/checkresults
53 # You can set the environment variable CORE_NOVERIFY=yes
54 # in order to supress a verification of the nagios configuration
55 # in case of start, restart or reload. This is in order to
56 # avoid duplicate effort when being called by cmk -R or
58 # export CORE_NOVERIFY=yes
60 # Make sure that check plugins do not run localized.
61 # check_icmp outputs performance data with german
62 # comma instead of dot and makes it unparsable.
66 # OMD: create configuration file out of fragments
67 case "$1" in start|restart|reload|checkconfig|check
)
69 ###ROOT###/etc/nagios/nagios.d/*.cfg \
70 ###ROOT###/etc/nagios/nagios.cfg \
71 > $CFG_FILE ||
rm -f $CFG_FILE
72 if [ $?
-ne 0 ]; then exit 1; fi
77 # Fetches the pid of the currently running nagios process of the given
80 # --ppid 1 in ps seem not to filter by direct ppid but by the whole
81 # parent process tree. So filter by hand again.
83 # Removed the filter "-P 1" (filters for ppid=1 processes) as on some
84 # distros, like Ubuntu 13.10 and newer, the processes will not be childs
85 # of PID 1, instead the process is child of an "upstart user session",
86 # which is visible via ps as "init --user". This will be the PPID until
87 # the user session ends, then the process will be moved to PPID=1.
88 # Strange one, but we try to simply ignore that... "-o" should make it.
90 # It returns 1 when no process can be found and echos the PID while
91 # returning 0 when a process can be found.
93 pgrep
-u $OMD_SITE -o -fx "$BIN $OPTIONS $CFG_FILE" 2>/dev
/null
98 if [ "$1" != "quiet" ]; then
99 echo -n "Running configuration check... "
101 RESULT
=$
($BIN -pv $CFG_FILE 2>&1)
102 if [ $?
-eq 0 ]; then
103 if [ "$1" != "quiet" ]; then
109 if [ "$1" != "quiet" ]; then
110 echo "CONFIG ERROR! Aborted. Check your Nagios configuration."
118 if [ -f $CMD_FILE ]; then
122 chown
$USR:$GRP $PID_FILE
123 rm -f $CHECKRESULTS_DIR/*
127 pid
=$
(pidof_nagios
) ||
{
128 echo -n 'not running...'
132 # wait until really stopped.
133 # it might happen that nagios has a subprocess which
134 # is left running and becomes ppid 1 after killing the
135 # main nagios process. So fetch the process id again
136 # multiple times to fetch new processes until all are gone.
138 while kill -0 $pid >/dev
/null
2>&1; do
139 # Send TERM to process group to kill the nagios process and also
140 # other processes started by this nagios process, for example
141 # check plugins which are currently running
143 while kill -0 $pid >/dev
/null
2>&1; do
144 if [ $I = '60' ]; then
145 echo -ne "\nsending SIGKILL"
147 elif [ $I = '70' ]; then
155 # Is there another proc with ppid 1?
156 pid
=$
(pidof_nagios
) ||
break
162 nagios_wait_start
() {
164 $BIN $OPTIONS $CFG_FILE
167 while ! pidof_nagios
>/dev
/null
2>&1; do
168 if [ $I = '10' ]; then
178 if [ ! -f $BIN ]; then
179 echo "Nagios binary $BIN not found. Terminating..."
183 case "$1" in start|restart|reload|checkconfig
)
184 if [ ! -f $CFG_FILE ]; then
185 echo "Nagios configuration file $CFG_FILE not found. Terminating..."
190 __init_hook
$0 $1 pre
193 echo -n "Starting nagios..."
194 if pidof_nagios
>/dev
/null
2>&1; then
195 echo 'Already running.'
199 [ "$CORE_NOVERIFY" ] || verify_config quiet ||
exit 1
201 if nagios_wait_start
; then
203 __init_hook
$0 $1 post
0
207 __init_hook
$0 $1 post
1
212 echo -n "Stopping nagios..."
213 if nagios_wait_stop
; then
215 __init_hook
$0 $1 post
0
219 __init_hook
$0 $1 post
1
224 if ! verify_config
; then
230 if pid
=$
(pidof_nagios
2>&1)
232 echo "Running ($pid)."
239 [ "$CORE_NOVERIFY" ] || verify_config quiet ||
exit 1
241 $0 stop ||
(echo "Unable to stop nagios. Terminating..." && exit 1)
242 echo -n "Starting nagios..."
243 if nagios_wait_start
; then
253 [ "$CORE_NOVERIFY" ] || verify_config quiet ||
exit 1
255 # Execute a start when nagios is not running
256 if ! pid
=$
(pidof_nagios
) 2>&1; then
261 echo -n "Reloading nagios configuration (PID: $pid)... "
262 if kill -HUP $pid >/dev
/null
2>&1; then
264 __init_hook
$0 $1 post
0
273 echo "Usage: nagios {start|stop|restart|reload|status|checkconfig}"