-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-linux.sh
375 lines (300 loc) · 10.5 KB
/
setup-linux.sh
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/bin/bash
# Nagios Log Server Linux setup script
# Copyright 2014, Nagios Enterprises LLC.
# Released under the terms of the Nagios Software License:
# <https://assets.nagios.com/licenses/nagios_software_license.txt>
# Configure syslog on a Linux system to forward logs to a Nagios Log Server.
# Exit immediately if an untested command fails
set -e
# Print license information for this script.
license() {
cat <<-EOF
Nagios Log Server Linux setup script
Copyright 2014, Nagios Enterprises LLC.
License:
Nagios Software License <https://assets.nagios.com/licenses/nagios_software_license.txt>
EOF
}
# Print a usage message.
usage() {
cat <<-EOF
Usage: setup-linux.sh -s HOST [options...]
Options:
-h
Display this help text
-n
Assume defaults for all questions (for scripted installs)
-s HOST
Log server hostname or IP address (required)
-p PORT
Log server port (defaults to '5544')
-f "FILE"
Forward messages from this file to Log Server
Multiple files may be listed
e.g. -f "/path/to/file /path/to/another/file/*.log"
-t FILETAG
A tag for FILE, Log Server will see this as 'programname'
EOF
}
# Print an error message (passed as $1) followed by the usage instructions and
# exit with an error status.
usage_error() {
echo $1
usage
exit 1
}
# Convenience function for printing errors and exiting.
error() {
echo "ERROR:" "$@" >&2
exit 1
}
# Make sure we're running as root, error out if not.
we_need_to_be_root() {
if [ $(id -u) -ne 0 ]; then
error "This script needs to be run as root/superuser."
fi
}
# Pretty self-descriptive: we expect /sbin and /usr/sbin to be in PATH.
make_sure_path_is_good() {
path_is_ok() {
echo "$PATH" \
| awk 'BEGIN{RS=":"} {p[$0]++} END{if (p["/sbin"] && p["/usr/sbin"]) exit(0); exit(1)}'
}
if ! path_is_ok; then
echo "Your system \$PATH does not include /sbin and /usr/sbin. This"\
"could be the result of installing GNOME rather than creating a"\
"clean system."
echo "Adding /sbin and /usr/sbin to \$PATH."
PATH="$PATH:/sbin:/usr/sbin"
fi
unset -f path_is_ok
}
# syslog detection variables.
HAVE_SYSLOG_NG=
HAVE_RSYSLOG=
RSYSLOG_SPOOL_D=
SYSLOG_TYPE=
SYSLOG_SERVICE=
SYSLOG_CONF_D=
SYSLOG_CONF_F=
# Detect syslog system and set default locations.
detect_syslog_system() {
if which syslog-ng &> /dev/null; then
output=`syslog-ng --version`
regex='syslog-ng ([0-9]+\.[0-9]+\.[0-9]+)'
[[ $output =~ $regex ]]
HAVE_SYSLOG_NG="${BASH_REMATCH[1]}"
echo "Found syslog-ng $HAVE_SYSLOG_NG"
SYSLOG_TYPE='syslog-ng'
SYSLOG_SERVICE='syslog-ng'
SYSLOG_CONF_D='/etc/syslog-ng/'
SYSLOG_CONF_F='/etc/syslog-ng/syslog-ng.conf'
fi
if which rsyslogd &> /dev/null; then
output=`rsyslogd -v`
regex='rsyslogd ([0-9]+\.[0-9]+\.[0-9]+)'
[[ $output =~ $regex ]]
HAVE_RSYSLOG="${BASH_REMATCH[1]}"
echo "Detected rsyslog $HAVE_RSYSLOG"
SYSLOG_TYPE='rsyslog'
SYSLOG_SERVICE='rsyslogd'
SYSLOG_CONF_D='/etc/rsyslog.d/'
SYSLOG_CONF_F='/etc/rsyslog.conf'
detect_rsyslog_spool
fi
# Bail out if we don't have rsyslog. We could support syslog-ng easily
# enough with a bit of tweaking and special-cases...
if [ -z "$HAVE_RSYSLOG" -a -z "$HAVE_SYSLOG_NG" ]; then
error "No syslog service found."
elif [ -z "$HAVE_RSYSLOG" -a -n "$HAVE_SYSLOG_NG" ]; then
error "Found syslog-ng but not rsyslog. Automatic configuration of"\
"syslog-ng for Nagios Log Server is not currently supported."
fi
}
# Detect the local syslog spool directory.
detect_rsyslog_spool() {
if [ -d '/var/spool/rsyslog' ]; then
RSYSLOG_SPOOL_D='/var/spool/rsyslog'
# On Ubuntu systems we may need to change ownership.
# if [ ]
# chown -R syslog:adm $RSYSLOG_SPOOL_D
# fi
elif [ -d '/var/lib/rsyslog' ]; then
RSYSLOG_SPOOL_D='/var/lib/rsyslog'
else
error "No rsyslog spool directory found. Make sure rsyslog is"\
"installed and configured correctly."
fi
echo "Detected rsyslog work directory $RSYSLOG_SPOOL_D"
}
# Check if SELinux might cause problems, and notify the user if so.
check_for_selinux() {
if ! which getenforce &> /dev/null; then
echo "getenforce command not found, assuming SELinux is disabled."
return
fi
SELinux_MODE=$(getenforce 2>/dev/null)
if [ $SELinux_MODE = "Disabled" ]; then
echo "SELinux is disabled."
elif [ $SELinux_MODE = "Permissive." ]; then
echo "SELinux is permissive."
elif [ $SELinux_MODE = "Enforcing" ]; then
cat <<-EOF
==============================! WARNING !====================================
SELinux is enforcing. This may prevent $SYSLOG_TYPE from forwarding messages.
If log messages do not reach Log Server from this host, ensure SELInux is
configured to allow $SYSLOG_TYPE forwarding.
=============================================================================
EOF
fi
}
# Restart the syslog service so our configuration changes will be used.
restart_syslog_service() {
# First try verifying the configuration (rsyslog specific).
if ! rsyslogd -f $SYSLOG_CONF_F -N 1 &> /dev/null; then
error "$SYSLOG_TYPE configuration check failed."
else
echo "$SYSLOG_TYPE configuration check passed."
fi
# Try using 'service' first.
if which service &> /dev/null; then
echo "Restarting $SYSLOG_TYPE service with 'service'..."
if service $SYSLOG_TYPE restart; then
echo "Okay."
return 0
fi
fi
# Try the SysV init script directly.
if [[ -f "/etc/init.d/$SYSLOG_TYPE" ]]; then
echo "Restarting $SYSLOG_TYPE service with '/etc/init.d/$SYSLOG_TYPE'..."
if "/etc/init.d/$SYSLOG_TYPE" restart; then
echo "Okay."
return 0
fi
fi
# Lastly try systemctl
if which systemctl &> /dev/null; then
echo "restarting $SYSLOG_TYPE service with 'systemctl'..."
if systemctl restart $SYSLOG_TYPE; then
echo "Okay."
return 0
fi
fi
error "Unable to restart $SYSLOG_TYPE service. Please restart the"\
"$SYSLOG_TYPE service to use the new configuration."
}
# Default Log Server host:port
NLS_HOST=
NLS_PORT="5544"
# Send all local syslogs to the specified Nagios Log Server host:port.
setup_local_syslog_conf() {
# Since we're forwarding everything that makes it to rsyslog, name our
# conf file something that will sort at the end of the *.conf list.
NLS_SYSLOG_CONF_F="${SYSLOG_CONF_D}99-nagioslogserver.conf"
echo "Creating $NLS_SYSLOG_CONF_F..."
cat >$NLS_SYSLOG_CONF_F <<-EOF
### Begin forwarding rule for Nagios Log Server NAGIOSLOGSERVER
\$WorkDirectory $RSYSLOG_SPOOL_D # Where spool files will live NAGIOSLOGSERVER
\$ActionQueueFileName nlsFwdRule0 # Unique name prefix for spool files NAGIOSLOGSERVER
\$ActionQueueMaxDiskSpace 1g # 1GB space limit (use as much as possible) NAGIOSLOGSERVER
\$ActionQueueSaveOnShutdown on # Save messages to disk on shutdown NAGIOSLOGSERVER
\$ActionQueueType LinkedList # Use asynchronous processing NAGIOSLOGSERVER
\$ActionResumeRetryCount -1 # Infinite retries if host is down NAGIOSLOGSERVER
# Remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional NAGIOSLOGSERVER
*.* @@$NLS_HOST:$NLS_PORT # NAGIOSLOGSERVER
### End of Nagios Log Server forwarding rule NAGIOSLOGSERVER
EOF
}
# Poll messages from a file and forward them to Nagios Log Server.
setup_file_syslog_conf() {
for f in $NLS_FILE_PATH
do
echo "Processing $f file..."
if [ ! -r $f ]; then
echo "File $f does not exist"
fi
# Generate an identifier from the file path that can be embedded in a
# filename. Translate / to _ and remove any leading _.
NLS_FILE_ID=`echo $f | tr '/' '_' | sed 's/^_//'`
# Limit the file ID length so our filenames are within FS limits.
id_len=${#NLS_FILE_ID}
max_id_len=$((`getconf NAME_MAX $SYSLOG_CONF_D`-24))
if [ $id_len -gt $max_id_len ]; then
NLS_FILE_ID=${NLS_FILE_ID:$id_len-$max_id_len}
fi
# Our file watching configs need to sort before our syslog forwarding, or
# the *.* rule will send the file messages before we send them here and
# then stop processing them, resulting in duplicates in Log Server.
NLS_SYSLOG_CONF_F="${SYSLOG_CONF_D}90-nagioslogserver_$NLS_FILE_ID.conf"
echo "Creating $NLS_SYSLOG_CONF_F..."
cat >$NLS_SYSLOG_CONF_F <<-EOF
\$ModLoad imfile
\$InputFilePollInterval 10
\$PrivDropToGroup adm
\$WorkDirectory $RSYSLOG_SPOOL_D
# Input for $NLS_FILE_TAG
\$InputFileName $f
\$InputFileTag ${NLS_FILE_TAG}:
\$InputFileStateFile nls-state-$NLS_FILE_ID # Must be unique for each file being polled
# Uncomment the folowing line to override the default severity for messages
# from this file.
#\$InputFileSeverity info
\$InputFilePersistStateInterval 20000
\$InputRunFileMonitor
# Forward to Nagios Log Server and then discard, otherwise these messages
# will end up in the syslog file (/var/log/messages) unless there are other
# overriding rules.
if \$programname == '$NLS_FILE_TAG' then @@$NLS_HOST:$NLS_PORT
if \$programname == '$NLS_FILE_TAG' then ~
EOF
done
}
# Take care of the prereqs.
we_need_to_be_root
make_sure_path_is_good
detect_syslog_system
# Process our arguments and figure out what to do.
FILE_PATH_COUNT=0
FILE_TAG_COUNT=0
while getopts "hns:p:f:i:t:" opt; do
case $opt in
h ) license; usage; exit 0;;
n ) INTERACTIVE="False";;
s ) NLS_HOST="$OPTARG";;
p ) NLS_PORT="$OPTARG"
if [[ ! $NLS_PORT =~ ^[0-9]+$ ]] || [ $NLS_PORT -le 0 ]; then
usage_error "-$opt: '$OPTARG' is not a valid port number."
fi
;;
f ) NLS_FILE_PATH="$OPTARG"
if [ $FILE_PATH_COUNT -ne 0 ]; then
usage_error "-$opt: Only one file path glob may be given at a time."
fi
FILE_PATH_COUNT=1
;;
i|t ) NLS_FILE_TAG="$OPTARG"
if [ $FILE_TAG_COUNT -ne 0 ]; then
usage_error "-$opt: Only one file tag may be given at a time."
fi
FILE_TAG_COUNT=1
;;
? ) usage; exit 1;;
esac
done
shift $(($OPTIND - 1))
if [ -n "$*" ]; then usage_error "Unexpected extra arguments: '$*'"; fi
if [ -z "$NLS_HOST" ]; then usage_error "No log server host specified."; fi
echo "Destination Log Server: $NLS_HOST:$NLS_PORT"
if [ $((FILE_PATH_COUNT+FILE_TAG_COUNT)) -eq 1 ]; then
usage_error "File forwarding requires a file path and tag."
fi
# Now setup the local syslog configuration.
if [ $FILE_PATH_COUNT -eq 0 ]; then
setup_local_syslog_conf
else
setup_file_syslog_conf
fi
check_for_selinux
restart_syslog_service
echo "$SYSLOG_TYPE is running with the new configuration."
echo "Visit your Nagios Log Server dashboard to verify that logs are being received."