-
Notifications
You must be signed in to change notification settings - Fork 0
/
nagios
253 lines (226 loc) · 5.74 KB
/
nagios
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#! /bin/sh
# Written by Miquel van Smoorenburg <[email protected]>.
# Modified for Debian GNU/Linux
# by Ian Murdock <[email protected]>.
# Clamav version by Magnus Ekdahl <[email protected]>
# Nagios version by Sean Finney <[email protected]> and probably others
# nagios2 version by Marc Haber <[email protected]>
# nagios3 version by Alexander Wirt <[email protected]>
# nagios4 version by Stephen Gibson <[email protected]>
### BEGIN INIT INFO
# Provides: nagios4
# Required-Start: $local_fs $remote_fs $syslog $named $network $time
# Required-Stop: $local_fs $remote_fs $syslog $named $network
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: nagios host/service/network monitoring and management system
# Description: nagios is a monitoring and management system for hosts, services and networks.
### END INIT INFO
set -e
. /lib/lsb/init-functions
DAEMON=/usr/local/nagios/bin/nagios
NAME="nagios"
DESC="nagios monitoring daemon"
NAGIOSCFG="/usr/local/nagios/etc/nagios.cfg"
CGICFG="/usr/local/nagios/etc/cgi.cfg"
NICENESS=5
[ -x "$DAEMON" ] || exit 0
[ -r /etc/default/nagios ] && . /etc/default/nagios
# this is from madduck on IRC, 2006-07-06
# There should be a better possibility to give daemon error messages
# and/or to log things
log()
{
case "$1" in
[[:digit:]]*) success=$1; shift;;
*) :;;
esac
log_action_begin_msg "$1"; shift
log_action_end_msg ${success:-0} "$*"
}
check_started () {
#nagios4-core can be installed without -cgi
if [ -e $CGICFG ];
then
check_cmd=$(get_config nagios_check_command $CGICFG)
if [ ! "$check_cmd" ]; then
log 6 "unable to determine nagios_check_command from $CGICFG!"
return 6
fi
else
#use hardcoded default version
check_cmd="/usr/lib/nagios/plugins/check_nagios /var/cache/nagios/status.dat 5 '/usr/local/nagios/bin/nagios'"
fi
eval $check_cmd >/dev/null
if [ -f "$THEPIDFILE" ]; then
pid="$(cat $THEPIDFILE)"
if [ "$pid" ] && kill -0 $pid >/dev/null 2>/dev/null; then
return 0 # Is started
fi
fi
return 1 # Isn't started
}
#
# get_config()
#
# grab a config option from nagios.cfg (or possibly another nagios config
# file if specified). everything after the '=' is echo'd out, making
# this a nice generalized way to get requested settings.
#
get_config () {
if [ "$2" ]; then
set -- `grep ^$1 $2 | sed 's@=@ @'`
else
set -- `grep ^$1 $NAGIOSCFG | sed 's@=@ @'`
fi
shift
echo $*
}
check_config () {
if $DAEMON -v $NAGIOSCFG >/dev/null 2>&1 ; then
# First get the user/group etc Nagios is running as
nagios_user="$(get_config nagios_user)"
nagios_group="$(get_config nagios_group)"
log_file="$(get_config log_file)"
log_dir="$(dirname $log_file)"
return 0 # Config is ok
else
# config is not okay, so let's barf the error to the user
$DAEMON -v $NAGIOSCFG
fi
}
check_named_pipe () {
nagiospipe="$(get_config command_file)"
if [ -p "$nagiospipe" ]; then
return 1 # a named pipe exists
elif [ -e "$nagiospipe" ];then
return 1
else
return 0 # no named pipe exists
fi
}
if [ ! -f "$NAGIOSCFG" ]; then
log_failure_msg "There is no configuration file for Nagios 3."
exit 6
fi
THEPIDFILE=$(get_config "lock_file")
[ -n "$THEPIDFILE" ] || THEPIDFILE='/var/run/nagios/nagios.pid'
start () {
if [ "$ENABLED" = "no" ]; then
log_warning_msg "Not starting Nagios - set ENABLED to yes in /etc/defrault/nagios"
exit 0
fi
DIRECTORY=$(dirname $THEPIDFILE)
[ ! -d $DIRECTORY ] && mkdir -p $DIRECTORY
chown nagios:nagios $DIRECTORY
if ! check_started; then
if ! check_named_pipe; then
log_action_msg "named pipe exists - removing"
rm -f $nagiospipe
fi
if check_config; then
start_daemon -n $NICENESS -p $THEPIDFILE $DAEMON -d $NAGIOSCFG
ret=$?
else
log_failure_msg "errors in config!"
log_end_msg 1
exit 1
fi
else
log_warning_msg "already running!"
fi
return $ret
}
stop () {
killproc -p $THEPIDFILE
ret=$?
if [ `pidof nagios | wc -l ` -gt 0 ]; then
echo -n "Waiting for $NAME daemon to die.."
cnt=0
while [ `pidof nagios | wc -l ` -gt 0 ]; do
cnt=`expr "$cnt" + 1`
if [ "$cnt" -gt 15 ]; then
kill -9 `pidof nagios`
break
fi
sleep 1
echo -n "."
done
fi
echo
if ! check_named_pipe; then
rm -f $nagiospipe
fi
if [ -n "$ret" ]; then
return $ret
else
return $?
fi
}
status()
{
log_action_begin_msg "checking $DAEMON"
if check_started; then
log_action_end_msg 0 "running"
else
if [ -e "$THEPIDFILE" ]; then
log_action_end_msg 1 "$DAEMON failed"
exit 1
else
log_action_end_msg 1 "not running"
exit 3
fi
fi
}
reload () {
# Check first
if check_config; then
if check_started; then
killproc -p $THEPIDFILE $DAEMON 1
else
log_warning_msg "Not running."
fi
else
log_failure_msg "errors in config!"
log_end_msg 6
exit 6
fi
}
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop
log_end_msg $?
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME"
stop
if [ -z "$?" -o "$?" = "0" ]; then
start
fi
log_end_msg $?
;;
reload|force-reload)
log_daemon_msg "Reloading $DESC configuration files" "$NAME"
reload
log_end_msg $?
;;
status)
status
;;
check)
check
;;
*)
log_failure_msg "Usage: $0 {start|stop|restart|reload|force-reload|status}" >&2
exit 1
;;
esac
exit 0