File rcnagios of Package nagios33
x
1
2
# Copyright (c) 1995-2001 SuSE GmbH Nuernberg, Germany.
3
# 2002 SuSE Linux AG Nuernberg, Germany.
4
# 2007 SuSE Linux GmbH Nuernberg, Germany.
5
#
6
# Author: Wolfgang Rosenauer, Lars Vogdt
7
#
8
#
9
# /etc/init.d/nagios
10
#
11
# and symbolic its link
12
#
13
# /usr/sbin/rcnagios
14
#
15
# System startup script for nagios
16
#
17
### BEGIN INIT INFO
18
# Provides: nagios
19
# Required-Start: $local_fs $remote_fs $syslog $network
20
# Required-Stop: $local_fs $remote_fs $syslog $network
21
# Should-Start: $time sendmail httpd2 xntpd $named cron ndo2db
22
# Should-Stop: sendmail ndo2db
23
# Default-Start: 3 5
24
# Default-Stop: 0 1 2 6
25
# Short-Description: Network monitor Nagios
26
# Description: Starts and stops the Nagios monitor
27
# used to provide network services status for hosts,
28
# services and networks.
29
### END INIT INFO
30
31
. /etc/rc.status
32
33
NAGIOS_BIN=/usr/sbin/nagios
34
NAGIOS_CFG=/etc/nagios/nagios.cfg
35
NAGIOS_SYSCONFIG=/etc/sysconfig/nagios
36
NAGIOS_CFG_ERR_LOG=/var/log/nagios/config.err
37
38
# Read config and log errors in logfile
39
function config_check () {
40
$NAGIOS_BIN -v $NAGIOS_CFG > /dev/null 2>&1
41
if [ $? -eq 0 ]; then
42
return 0
43
else
44
$NAGIOS_BIN -v $NAGIOS_CFG > $NAGIOS_CFG_ERR_LOG 2>&1
45
return 1
46
fi
47
}
48
49
# grab a config option
50
function get_var() {
51
if [ -n "$2" ]; then
52
set -- `grep ^$1 $2 | sed 's@=@ @' | tr -d '[:cntrl:]'`
53
else
54
set -- `grep ^$1 $NAGIOS_CFG | sed 's@=@ @' | tr -d '[:cntrl:]'`
55
fi
56
shift # remove first ARG => search-string
57
echo $*
58
}
59
60
# check some default files and directories
61
function check_files() {
62
# set default access rights for files and directories
63
for file in $check_result_path $log_file $state_retention_file $status_file; do
64
if [ -e $file ]; then
65
touch $file
66
chown $nagios_user:$nagios_cmdgrp $file
67
fi
68
done
69
chmod 660 $resource_file
70
chown $nagios_user:$nagios_cmdgrp $resource_file
71
# remove some perhaps left over files
72
for file in $command_file $lock_file $status_file $temp_file /var/lock/subsys/nagios; do
73
test -f $file && rm -f $file
74
done
75
}
76
77
# Check for missing binaries (stale symlinks should not happen)
78
# Note: Special treatment of stop for LSB conformance
79
test -x $NAGIOS_BIN || { echo "$NAGIOS_BIN not installed";
80
if [ "$1" = "stop" ]; then exit 0;
81
else exit 5; fi; }
82
83
# Check for existence of needed config file
84
test -r $NAGIOS_CFG || { echo "$NAGIOS_CFG not existing";
85
if [ "$1" = "stop" ]; then exit 0;
86
else exit 6; fi; }
87
88
# Check for existence of sysconfig file and read it
89
test -r $NAGIOS_SYSCONFIG || { echo "$NAGIOS_SYSCONFIG not existing";
90
if [ "$1" = "stop" ]; then exit 0;
91
else exit 6; fi; }
92
93
. $NAGIOS_SYSCONFIG
94
95
# set values for sysconfig vars
96
if [ -n "$NAGIOS_NICELEVEL" ]; then
97
NICELEVEL="-n $NAGIOS_NICELEVEL"
98
else
99
NICELEVEL="-n 0"
100
fi
101
if [ -z "$NAGIOS_TIMEOUT" ]; then
102
NAGIOS_TIMEOUT=10
103
fi
104
105
#
106
# get variables from config file
107
#
108
nagios_user="$(get_var nagios_user)"
109
lock_file="$(get_var lock_file)"
110
status_file="$(get_var status_file)"
111
log_file="$(get_var log_file)"
112
temp_file="$(get_var temp_file)"
113
state_retention_file="$(get_var state_retention_file)"
114
command_file="$(get_var command_file)"
115
resource_file="$(get_var resource_file)"
116
object_cache_file="$(get_var object_cache_file)"
117
check_result_path="$(get_var check_result_path)"
118
119
#
120
# use default values if above check doesn't work
121
#
122
: ${nagios_user:=nagios}
123
: ${nagios_cmdgrp:=nagcmd}
124
: ${resource_file:=/etc/nagios/resource.cfg}
125
# check ownership files
126
: ${check_result_path:=/var/spool/nagios}
127
: ${log_file:=/var/log/nagios/nagios.log}
128
: ${state_retention_file:=/var/log/nagios/retention.dat}
129
: ${status_file:=/var/log/nagios/status.dat}
130
# files to remove
131
: ${command_file:=/var/spool/nagios/nagios.cmd}
132
: ${lock_file:=/var/run/nagios.pid}
133
: ${object_cache_file:=/var/lib/nagios/objects.cache}
134
: ${temp_file:=/var/log/nagios/nagios.tmp}
135
136
# Reset status of this service
137
rc_reset
138
139
case "$1" in
140
start)
141
echo -n "Starting nagios "
142
config_check
143
if [ $? -eq 0 ]; then
144
# check if nagios is already running
145
NAGPID=$(pidof $NAGIOS_BIN)
146
if [ -n "$NAGPID" ]; then
147
echo "- Error: looks like nagios is still running with PID $NAGPID"
148
echo -n "- trying to kill previous nagios process"
149
kill -9 $NAGPID
150
sleep 2
151
fi
152
check_files
153
startproc $NICELEVEL -p $lock_file $NAGIOS_BIN -d $NAGIOS_CFG
154
else
155
echo "- Error in configuration files"
156
echo -n "- please read $NAGIOS_CFG_ERR_LOG"
157
rc_failed
158
fi
159
rc_status -v
160
;;
161
stop)
162
echo -n "Shutting down nagios "
163
# we have to wait for nagios to exit and remove its
164
# own Lockfile, otherwise a following "start" could
165
# happen, and then the exiting nagios will remove the
166
# new Lockfile, allowing multiple nagios daemons
167
# to (sooner or later) run - John Sellens
168
169
if checkproc $NAGIOS_BIN ; then
170
killproc -p $lock_file -TERM $NAGIOS_BIN
171
sleep 1
172
if [ -e $lock_file ]; then
173
echo "Warning - nagios did not exit in a timely manner. Waiting..."
174
while [ -e $lock_file ] && [ $NAGIOS_TIMEOUT -gt 0 ] ; do
175
sleep 1
176
NAGIOS_TIMEOUT=$[$NAGIOS_TIMEOUT-1]
177
echo -n '.'
178
[ $NAGIOS_TIMEOUT -eq 41 ] && echo
179
done
180
fi
181
if checkproc $NAGIOS_BIN ; then
182
killproc -p $lock_file -SIGKILL $NAGIOS_BIN
183
echo -n "Warning: nagios killed"
184
fi
185
else
186
echo -n "nagios not running"
187
rc_failed 7
188
fi
189
check_files
190
rc_reset
191
rc_status -v
192
;;
193
try-restart)
194
## Do a restart only if the service was active before.
195
## Note: try-restart is now part of LSB (as of 1.9).
196
## RH has a similar command named condrestart.
197
$0 status
198
if test $? = 0; then
199
$0 restart
200
else
201
rc_reset # Not running is not a failure.
202
fi
203
# Remember status and be quiet
204
rc_status
205
;;
206
restart)
207
$0 stop
208
$0 start
209
rc_status
210
;;
211
reload|force-reload)
212
echo -n "Reload service nagios "
213
config_check
214
if [ $? -eq 0 ]; then
215
echo -n "Passed configuration check - reloading..."
216
killproc -HUP -p $lock_file $NAGIOS_BIN
217
else
218
echo "- Error in configuration files"
219
echo -n "- aborting reload - please read $NAGIOS_CFG_ERR_LOG"
220
rc_failed
221
fi
222
rc_status -v
223
;;
224
status)
225
echo -n "Checking for nagios "
226
checkproc -p $lock_file $NAGIOS_BIN
227
rc_status -v
228
;;
229
check|check_verbose)
230
echo "Running configuration check..."
231
config_check
232
if [ $? -eq 0 ]; then
233
echo -n "- Passed configuration check"
234
test -f $NAGIOS_CFG_ERR_LOG && rm $NAGIOS_CFG_ERR_LOG
235
rc_reset
236
else
237
echo "- Error in configuration files"
238
if [ $1 == "check_verbose" ]; then
239
cat $NAGIOS_CFG_ERR_LOG
240
else
241
echo -n "- please read $NAGIOS_CFG_ERR_LOG"
242
fi
243
rc_failed
244
fi
245
rc_status -v
246
;;
247
*)
248
echo "Usage: $0 {start|stop|status|try-restart|restart|reload|check|check_verbose}"
249
exit 1
250
;;
251
esac
252
rc_exit
253