-
Notifications
You must be signed in to change notification settings - Fork 2
/
sendNscaWrapper.sh
executable file
·400 lines (368 loc) · 10.4 KB
/
sendNscaWrapper.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env bash
#
# NAME
# sendNscaWrapper.sh - custom script to send NSCA events to Nagios
# passive monitoring checks
# SYNOPSIS
# sendNscaWrapper.sh <script> <nagiosHostname> <svcdescription> [<options>]
#
# DESCRIPTION
# -c, --nscacfg <send_nsca config file>
# Specify send_nsca config file
# (default /etc/nagios/send_nsca.cfg)
# -m, --nscadelim <send_nsca field delimiter>
# Specify send_nsca field delimiter
# (default "\t")
# -t, --nscato <send_nsca timeout>
# Specify send_nsca timeout
# (default 10 seconds)
# -p, --nscaport <send_nsca nagios target port>
# Specify send_nsca target port
# (default 5667)
#
# -s, --sendnsca <send_nsca bin location>
# Specify send_nsca bin location
# By default send_nsca is searched in /usr/bin/send_nsca and then
# in the $PATH structure
# -n, --hostname <send_nsca sender hostname>
# Specify send_nsca sender hostname
# By default send_nsca will use the result of `hostname -f`
# -b, --bookmark <bookmark file>
# Specify a file to use as a bookmark, to contain last status
# and send events ONLY on status change or status != (OK/0)
# (if you want to send first event, empty the bookmark file)
# -q, --quiet
# Avoid printing any (non-debug) statement
# -h, --help
# Output this help text
# -d, --debug
# Enable debug statements
#
# LICENSE
#
# Copyright (C) 2012-2013 Emanuele 'Lele aka eldios' Calo'
# Mail address: lele at sshadm dot in
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/
#
# EXAMPLES
# [.. to be filled soon ..]
#
###############################################################################
# last blank line is used to determine end of documentation
###############################################################################
set -o errexit -o noclobber -o nounset -o pipefail
set -u
# Defaults
debug=false
cmdname="$(basename -- $0)"
directory="$(dirname -- "$0")"
sendnscaLocation=$(command -v send_nsca) || $(echo "")
quiet=false
nscaPort="" # default 5667
nscaTimeout="" # default 10 seconds
nscaDelim="" # default "\t"
nscaCfg="" # default /etc/nagios/send_nsca.cfg
nscaCfgDef="/etc/nagios/send_nsca.cfg" # default /etc/nagios/send_nsca.cfg location
hostname="" # default 'hostname -f'
svcdesc="" # mandatory
bookmarkfile="" # default not used
# Process parameters
params="$(getopt \
-o c:m:t:p:s:n:b:qhd \
-l nscacfg:,nscadelim:,nscato:,nscaport:,sendnsca:,hostname:,bookmark:,quiet,help,debug \
--name "$cmdname" -- "$@")"
# Custom errors
EX_CRITICAL="2"
EX_WARNING="1"
EX_OK="0"
EX_UNKNOWN="$EX_CRITICAL"
# helper methods
# Exit codes from /usr/include/sysexits.h, as recommended by
# http://www.faqs.org/docs/abs/HTML/exitcodes.html
EX_USAGE="64"
error()
{
# Output error messages with optional exit code
# @param $1...: Messages
# @param $N: Exit code (optional)
local -a messages=( "$@" )
# If the last parameter is a number, it's not part of the messages
local -r last_parameter="${@: -1}"
if [[ "$last_parameter" =~ ^[0-9]*$ ]]
then
local -r exit_code="$last_parameter"
unset messages[$((${#messages[@]} - 1))]
fi
echo "${messages[@]}"
exit "${exit_code:-$EX_UNKNOWN}"
}
outputDebug()
{
# outputdebug statement
# @param $1: debug statement text (mandatory)
# @param $2: debug status value (mandatory)
local message=("${1}");
local debug=("${2}");
local timestamp="$(date +%d-%m-%Y_%T_\(%z\))"
if ( "$debug" )
then
echo "$timestamp : $message"
fi
}
usage()
{
# Print documentation until the first empty line
# @param $1: Exit code (optional)
local line
while IFS= read line
do
if [ -z "$line" ]
then
exit "${1:-0}"
elif [ "${line:0:2}" == '#!' ]
then
# Shebang line
continue
fi
echo "${line:2}" # Remove comment characters
done < "$0"
}
# main code
if [ "$?" -ne "0" ]
then
usage "$EX_USAGE"
fi
eval set -- "$params"
unset params
while true
do
case "$1" in
-n|hostname)
hostname="${2-}"
shift 2
;;
-s|sendnsca)
sendnscaLocation="${2-}"
shift 2
;;
-b|bookmark)
bookmarkfile="${2-}"
shift 2
;;
-p|nscaport)
nscaPort="${2-}"
shift 2
;;
-t|nscato)
nscaTimeout="${2-}"
shift 2
;;
-m|nscadelim)
nscaDelim="${2-}"
shift 2
;;
-c|nscacfg)
nscaCfg="${2-}"
shift 2
;;
-q|quiet)
quiet=true
shift
;;
-d|debug)
debug=true
shift
;;
-h|--help)
usage
exit "$EX_OK"
;;
--)
shift
if [ -z "${1:-}" ]
then
echo "You must specify the script to be executed"\
"AND the Nagios monitoring server hostname"
usage
error "" "$EX_USAGE"
fi
if [ -z "${2:-}" ]
then
echo "You must specify the Nagios monitoring"\
" server hostname"
usage
error "" "$EX_USAGE"
fi
if [ -z "${3:-}" ]
then
echo "You must specify the Nagios monitoring"\
" passive service description"
usage
error "" "$EX_USAGE"
fi
if [ -n "${4:-}" ]
then
echo "Too many parameters"
usage
error "" "$EX_USAGE"
fi
break
;;
*)
usage
;;
esac
done
outputDebug 'Debug activated' "$debug"
# checking that everything is available and usable
outputDebug "sendnscaLocation -> $sendnscaLocation" "$debug"
if [[ ! -x "$sendnscaLocation" ]]
then
error "Cannot find/execute send_nsca Nagios plugin" "$EX_CRITICAL"
fi
extScript="${1}"
extScriptBin=$(command -v $extScript) || $(echo "")
outputDebug "extScript -> $extScriptBin [ $extScript ]" "$debug"
if [[ ! -x "$extScriptBin" ]]
then
error "Cannot find/execute specified script" "$EX_CRITICAL"
fi
nagiosHostname="${2}"
nagiosHostnameIP=""
isIP()
{
local isIPHostname="${1}"
# isIP="$(echo $isIPHostname | awk '/^\s*[0-2]{0,1}[0-9]{1,2}(\.[0-2]{0,1}[0-9]{1,2}){3}\s*$/ { print $1 }')"
isIP="$(echo $isIPHostname | awk '/^\s*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s*$/ { print $1 }')";
if [[ -n "$isIP" ]]
then
echo $isIP
else
echo ""
fi
}
if [[ ! "$(isIP $nagiosHostname)" ]]
then
nagiosHostnameIP=$(dig +short ${2}) || $(echo "")
outputDebug "nagiosHostname -> $nagiosHostname : $nagiosHostnameIP" "$debug"
if [[ ! "$nagiosHostnameIP" ]]
then
error "Cannot resolve specified Nagios Hostname" "$EX_CRITICAL"
fi
else
outputDebug "nagiosHostname -> $nagiosHostname" "$debug"
fi
svcdesc="${3}"
outputDebug "svcdesc: $svcdesc" "$debug"
sendNscaCMD="$sendnscaLocation -H $nagiosHostname"
if [[ -n "$nscaPort" ]]
then
outputDebug "nscaPort : $nscaPort" "$debug"
sendNscaCMD="$sendNscaCMD -p $nscaPort"
fi
if [[ -n "$nscaTimeout" ]]
then
outputDebug "nscaTimeout : $nscaTimeout" "$debug"
sendNscaCMD="$sendNscaCMD -t $nscaTimeout"
fi
if [[ -n "$nscaDelim" ]]
then
outputDebug "nscaDelim : $nscaDelim" "$debug"
sendNscaCMD="$sendNscaCMD -d \"$nscaDelim\""
fi
if [[ -z "$nscaCfg" ]]
then
outputDebug "nscaCfg set to default location $nscaCfgDef" "$debug"
nscaCfg="$nscaCfgDef"
fi
if [[ -r "$nscaCfg" ]]
then
outputDebug "nscaCfg : $nscaCfg" "$debug"
sendNscaCMD="$sendNscaCMD -c $nscaCfg"
else
echo "Cannot find/read specified send_nsca config file $nscaCfg"
exit "$EX_CRITICAL"
fi
# actual script execution
set +e
scriptOutput="$($extScript 2>&1)"
scriptStatus="$(echo ${PIPESTATUS[0]})"
set -e
outputDebug "scriptOutput : $scriptOutput" "$debug"
outputDebug "scriptStatus : $scriptStatus" "$debug"
if [[ -z "$hostname" ]]
then
hostname=$(hostname -f)
fi
outputDebug "hostname: $hostname" "$debug"
if [[ -z "$nscaDelim" ]]
then
nscaDelim="\t"
fi
outputDebug "nscaDelim: $nscaDelim" "$debug"
sendNscaMsg=""
sendNscaMsg="$sendNscaMsg""$hostname""$nscaDelim""$svcdesc""$nscaDelim"
sendNscaMsg="$sendNscaMsg""$scriptStatus""$nscaDelim""$scriptOutput"
outputDebug "sendNscaCMD : $sendNscaCMD" "$debug"
outputDebug "sendNscaMsg: $sendNscaMsg" "$debug"
sendEvent=true
if [[ -n "$bookmarkfile" ]]
then
outputDebug "bookmarkfile is $bookmarkfile" "$debug"
if [[ ! -f "$bookmarkfile" ]]
then
touch "$bookmarkfile"
fi
if [[ ! -r "$bookmarkfile" ]]
then
error "Cannot read bookmarkfile, check permission" "$EX_CRITICAL"
fi
lastStatus="$(cat $bookmarkfile)"
outputDebug "lastStatus is $lastStatus" "$debug"
if [[ "$lastStatus" -eq "$scriptStatus" && "$lastStatus" -eq "0" ]]
then
sendEvent=false
else
sendEvent=true
set +o noclobber
echo "$scriptStatus" > "$bookmarkfile"
set -o noclobber
fi
else
outputDebug "no bookmarkfile specified" "$debug"
fi
outputDebug "sendEvent is $sendEvent" "$debug"
if ( "$sendEvent" )
then
outputDebug "Event sent to $nagiosHostname" "$debug"
set +e
sendNscaOutput="$(echo -e $sendNscaMsg | $sendNscaCMD 2>&1)"
sendNscaStatus="$(echo ${PIPESTATUS[0]})"
set -e
outputDebug "sendNscaOutput: $sendNscaOutput" "$debug"
outputDebug "sendNscaStatus : $sendNscaStatus" "$debug"
if ( ! "$quiet" )
then
echo "$sendNscaOutput"
exit "$sendNscaStatus"
fi
else
outputDebug "Event NOT sent to $nagiosHostname" "$debug"
if ( ! "$quiet" )
then
echo "Event NOT sent as status is not changed"
exit "$EX_OK"
fi
fi