check_logfiles: 3.7.5.1
[omd.git] / packages / nagios / skel / etc / init.d / nagios
blobe5e58e40dcaced9b46bb338625b87516e67f1e98
1 #!/bin/bash
3 # chkconfig: 345 99 01
4 # description: Nagios network monitoring daemon
6 ### BEGIN INIT INFO
7 # Provides: nagios
8 # Required-Start:
9 # Required-Stop:
10 # Default-Start: 2 3 5
11 # Default-Stop:
12 # Description: Nagios network monitoring daemon
13 ### END INIT INFO
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
37 cd ###ROOT###
38 . .profile
39 . lib/omd/init_profile
40 . etc/omd/site.conf
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
50 USR=###SITE###
51 GRP=###SITE###
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
57 # cmk -O.
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.
63 unset LANG
64 export LC_ALL=C
66 # OMD: create configuration file out of fragments
67 case "$1" in start|restart|reload|checkconfig|check)
68 merge-nagios-config \
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
73 esac
75 OPTIONS="-ud"
77 # Fetches the pid of the currently running nagios process of the given
78 # user.
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.
92 pidof_nagios() {
93 pgrep -u $OMD_SITE -o -fx "$BIN $OPTIONS $CFG_FILE" 2>/dev/null
97 verify_config() {
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
104 echo "done."
105 echo "$RESULT" >&2
107 return 0
108 else
109 if [ "$1" != "quiet" ]; then
110 echo "CONFIG ERROR! Aborted. Check your Nagios configuration."
112 echo "$RESULT" >&2
113 return 1
117 prep_start() {
118 if [ -f $CMD_FILE ]; then
119 rm -f $CMD_FILE
121 touch $PID_FILE
122 chown $USR:$GRP $PID_FILE
123 rm -f $CHECKRESULTS_DIR/*
126 nagios_wait_stop() {
127 pid=$(pidof_nagios) || {
128 echo -n 'not running...'
129 return 0
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
142 kill -TERM -$pid
143 while kill -0 $pid >/dev/null 2>&1; do
144 if [ $I = '60' ]; then
145 echo -ne "\nsending SIGKILL"
146 kill -9 $pid
147 elif [ $I = '70' ]; then
148 return 1
151 echo -n "."
152 I=$(($I+1))
153 sleep 1
154 done
155 # Is there another proc with ppid 1?
156 pid=$(pidof_nagios) || break
157 done
159 rm -f "$PID_FILE"
162 nagios_wait_start() {
163 prep_start
164 $BIN $OPTIONS $CFG_FILE
167 while ! pidof_nagios >/dev/null 2>&1; do
168 if [ $I = '10' ]; then
169 return 1
170 else
171 echo -n "."
172 I=$(($I+1))
173 sleep 1
175 done
178 if [ ! -f $BIN ]; then
179 echo "Nagios binary $BIN not found. Terminating..."
180 exit 1
183 case "$1" in start|restart|reload|checkconfig)
184 if [ ! -f $CFG_FILE ]; then
185 echo "Nagios configuration file $CFG_FILE not found. Terminating..."
186 exit 1
188 esac
190 __init_hook $0 $1 pre
191 case "$1" in
192 start)
193 echo -n "Starting nagios..."
194 if pidof_nagios >/dev/null 2>&1; then
195 echo 'Already running.'
196 exit 0
199 [ "$CORE_NOVERIFY" ] || verify_config quiet || exit 1
201 if nagios_wait_start; then
202 echo 'OK'
203 __init_hook $0 $1 post 0
204 exit 0
205 else
206 echo 'ERROR'
207 __init_hook $0 $1 post 1
208 exit 1
211 stop)
212 echo -n "Stopping nagios..."
213 if nagios_wait_stop; then
214 echo 'OK'
215 __init_hook $0 $1 post 0
216 exit 0
217 else
218 echo 'ERROR'
219 __init_hook $0 $1 post 1
220 exit 1
223 check|checkconfig)
224 if ! verify_config; then
225 exit 1
227 exit 0
229 status)
230 if pid=$(pidof_nagios 2>&1)
231 then
232 echo "Running ($pid)."
233 else
234 echo 'Not running.'
235 exit 1
238 restart)
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
244 echo 'OK'
245 exit 0
246 else
247 echo 'ERROR'
248 exit 1
252 reload|force-reload)
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
257 $0 start
258 exit $?
261 echo -n "Reloading nagios configuration (PID: $pid)... "
262 if kill -HUP $pid >/dev/null 2>&1; then
263 echo 'OK'
264 __init_hook $0 $1 post 0
265 exit 0
266 else
267 $0 restart
268 exit $?
273 echo "Usage: nagios {start|stop|restart|reload|status|checkconfig}"
274 exit 2
276 esac
278 # EOF