forked from phrawzty/check_http_json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_http_json.rb
executable file
·437 lines (365 loc) · 12.7 KB
/
check_http_json.rb
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env ruby
# Name: check_http_json.rb
# Author: https://github.com/phrawzty/rabbitmq-collectd-plugin/commits/master
# Description: Nagios plugin that makes an HTTP connection and parses the JSON result.
#
# Copyright 2012 Daniel Maher
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Requires.
require 'rubygems'
require 'json'
require 'net/http'
require 'uri'
require 'optparse'
require 'timeout'
# Herp derp.
options = {}
# Def jam.
# Display verbose output (if being run by a human for example).
def say (v, msg)
if v == true
puts '+ %s' % [msg]
end
end
# Manage the exit code explicitly.
def do_exit (v, code)
if v == true
exit 3
else
exit code
end
end
# As the results may be nested hashes; flatten that out into something manageable.
def hash_flatten(hash, prefix = nil, flat = {}, delimiter)
hash.keys.each do |key|
newkey = key
newkey = '%s%s%s' % [prefix, delimiter, key] if prefix
val = hash[key]
if val.is_a? Hash then
hash_flatten val, newkey, flat, delimiter
else
flat[newkey] = val
end
end
return flat
end
# Parse the nutty Nagios range syntax.
# http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT
def nutty_parse(thresh, want, got, v)
retval = 'FAIL'
# if there is a non-numeric character we have to deal with that
# got < want
if want =~ /^(\d+):$/ then
if got.to_i < $1.to_i then
retval = 'KO'
else
retval = 'OK'
end
end
# got > want
if want =~ /^~:(\d+)$/ then
if got.to_i > $1.to_i then
retval = 'KO'
else
retval = 'OK'
end
end
# outside specific range
if want =~ /^(\d+):(\d+)$/ then
if got.to_i < $1.to_i or got.to_i > $2.to_i then
retval = 'KO'
else
retval = 'OK'
end
end
# inside specific range
if want =~ /^@(\d+):(\d+)$/ then
if got.to_i >= $1.to_i and got.to_i <= $2.to_i then
retval = 'KO'
else
retval = 'OK'
end
end
# otherwise general range
if not want =~ /\D/ then
if got.to_i < 0 or got.to_i > want.to_i then
retval = 'KO'
else
retval = 'OK'
end
end
if retval == 'OK' then
say(v, '%s threshold not exceeded.' % [thresh])
elsif retval == 'KO' then
say(v, '%s threshold exceeded.' % [thresh])
else
say(v, '"%s" is a strange and confusing %s value.' % [want, thresh])
end
return retval
end
# Deal with a URI target.
def uri_target(options)
uri = URI.parse(options[:uri])
http = Net::HTTP.new(uri.host, uri.port)
# Timeout handler, just in case.
response = nil
begin
Timeout::timeout(options[:timeout]) do
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
end
rescue Timeout::Error
say(options[:v], 'The HTTP connection timed out after %i seconds.' % [options[:timeout]])
puts 'CRIT: Connection timed out.'
do_exit(options[:v], 2)
end
# We must get a proper response.
if not response.code.to_i == 200 then
puts 'WARN: Received HTTP code %s instead of 200.' % [response.code]
do_exit(options[:v], 1)
end
say(options[:v], "RESPONSE:\n---\n%s\n---" % [response.body])
# Make a JSON object from the response.
json = JSON.parse response.body
return json
end
# Deal with a file target.
def file_target(options)
# The file must exist and be readable.
state = nil
if not File.exist?(options[:file]) then
state = 'does not exist'
elsif not File.readable?(options[:file]) then
state = 'is not readable'
end
if state then
puts 'CRIT: %s %s.' % [options[:file], state]
do_exit(options[:v], 2)
end
# Make a JSON object from the contents of the file.
json = JSON.parse(File.read(options[:file]))
return json
end
# Parse cli args.
def parse_args(options)
optparse = OptionParser.new do |opts|
opts.banner = 'Usage: %s -u <URI> -e <element> -w <warn> -c <crit>' % [$0]
opts.on('-h', '--help', 'Help info.') do
puts opts
do_exit(true, 3)
end
options[:v] = false
opts.on('-v', '--verbose', 'Additional human output.') do
options[:v] = true
end
options[:uri] = nil
opts.on('-u', '--uri URI', 'Target URI. Incompatible with -f.') do |x|
options[:uri] = x
end
options[:file] = nil
opts.on('-f', '--file PATH', 'Target file. Incompatible with -u.') do |x|
options[:file] = x
end
options[:element_string] = nil
opts.on('-e', '--element ELEMENT', 'Desired element (ex. foo=>bar=>ish is foo.bar.ish).') do |x|
options[:element_string] = x
end
options[:element_regex] = nil
opts.on('-E', '--element_regex REGEX', 'Desired element expressed as regular expression.') do |x|
options[:element_regex] = x
end
options[:delimiter] = '.'
opts.on('-d', '--delimiter CHARACTER', 'Element delimiter (default is period).') do |x|
options[:delimiter] = x
end
options[:warn] = nil
opts.on('-w', '--warn VALUE', 'Warning threshold (integer).') do |x|
options[:warn] = x.to_s
end
options[:crit] = nil
opts.on('-c', '--crit VALUE', 'Critical threshold (integer).') do |x|
options[:crit] = x.to_s
end
options[:result_string] = nil
opts.on('-r', '--result STRING', 'Expected string result. No need for -w or -c.') do |x|
options[:result_string] = x
end
options[:result_regex] = nil
opts.on('-R', '--result_regex REGEX', 'Expected string result expressed as regular expression. No need for -w or -c.') do |x|
options[:result_regex] = x
end
options[:result_string_warn] = nil
opts.on('-W', '--result_warn STRING', 'Warning if element is [string]. -C is required.') do |x|
options[:result_string_warn] = x
end
options[:result_string_crit] = nil
opts.on('-C', '--result_crit STRING', 'Critical if element is [string]. -W is required.') do |x|
options[:result_string_crit] = x
end
options[:timeout] = 5
opts.on('-t', '--timeout SECONDS', 'Wait before HTTP timeout.') do |x|
options[:timeout] = x.to_i
end
end
optparse.parse!
return options
end
# Sanity check.
def sanity_check(options)
# In life, some arguments cannot be avoided.
error_msg = []
if not (options[:uri] or options[:file]) then
error_msg.push('Must specify target URI or file.')
end
if (options[:uri] and options[:file]) then
error_msg.push('Must specify either target URI or file, but not both.')
end
if not (options[:element_string] or options[:element_regex]) then
error_msg.push('Must specify a desired element.')
end
if options[:element_string] and options[:element_regex] then
error_msg.push('Must specify either an element string OR an element regular expression.')
end
if options[:delimiter].length > 1
error_msg.push('Delimiter must be a single character.')
end
if not ((options[:result_string] or options[:result_regex]) or (options[:warn] and options[:crit]) or (options[:result_string_warn] and options[:result_string_crit])) then
error_msg.push('Must specify an expected result OR the warn and crit thresholds.')
end
if options[:result_string] and options[:result_regex] then
error_msg.push('Must specify either a result string OR result regular expression.')
end
if error_msg.length > 0 then
# First line is Nagios-friendly.
puts 'UNKNOWN: Insufficient or incompatible arguments.'
# Subsequent lines are for humans.
error_msg.each do |msg|
puts msg
end
puts '"%s --help" for more information.' % [$0]
do_exit(true, 3)
end
end
# Run Lola Run.
# Choose your arguments wisely.
options = parse_args(options)
sanity_check(options)
# Set up the json object.
json = nil
# If the target is a URI.
if options[:uri] then
json = uri_target(options)
end
# If the target is a file.
if options[:file] then
json = file_target(options)
end
# Flatten that bad boy.
json_flat = hash_flatten(json, options[:delimiter])
# If the element is a string...
if options[:element_string] then
if not json_flat[options[:element_string]] then
puts 'WARN: %s not found in response.' % [options[:element_string]]
do_exit(options[:v], 1)
end
options[:element] = options[:element_string]
end
# If the element is a regex...
if options[:element_regex] then
json_flat.each do |k,v|
if k =~ Regexp.new(options[:element_regex]) then
say(options[:v], "Found %s as %s" % [options[:element_regex], k])
element_found = true
options[:element] = k
end
end
if not options[:element] then
puts 'UNKNOWN: %s not found in response.' % [options[:element_regex]]
do_exit(options[:v], 3)
end
end
say(options[:v], 'The value of %s is: %s' % [options[:element], json_flat[options[:element]]])
# If we're looking for a string...
if options[:result_string] then
if json_flat[options[:element]].to_s == options[:result_string].to_s then
puts 'OK: %s is %s' % [options[:element], json_flat[options[:element]]]
do_exit(options[:v], 0)
else
puts 'CRIT: %s is %s' % [options[:element], json_flat[options[:element]]]
do_exit(options[:v], 2)
end
end
# If we're looking for a regex...
if options[:result_regex] then
say(options[:v], 'Will match %s against \'%s\'' % [options[:element].to_s, options[:result_regex]])
if json_flat[options[:element]].to_s =~ Regexp.new(options[:result_regex]) then
puts 'OK: %s is %s' % [options[:element], json_flat[options[:element]]]
do_exit(options[:v], 0)
else
puts 'CRIT: %s is %s' % [options[:element], json_flat[options[:element]]]
do_exit(options[:v], 2)
end
end
# If we're specifying Critical + Warning strings...
if options[:result_string_warn] and options[:result_string_crit]
say(options[:v], '%s should not match against \'%s\', else CRIT' % [options[:element].to_s, options[:result_string_crit]])
say(options[:v], '%s should not match against \'%s\', else WARN' % [options[:element].to_s, options[:result_string_warn]])
if json_flat[options[:element]].to_s == options[:result_string_crit].to_s then
puts 'CRIT: %s matches %s' % [options[:element], json_flat[options[:element]]]
do_exit(options[:v], 2)
elsif json_flat[options[:element]].to_s == options[:result_string_warn].to_s then
puts 'WARN: %s matches %s' % [options[:element], json_flat[options[:element]]]
do_exit(options[:v], 1)
else
puts 'OK: %s does not match %s or %s' % [options[:element], options[:result_string_warn], options[:result_string_crit]]
do_exit(options[:v], 0)
end
end
# If we're dealing with threshold values...
# Numbahs only, brah.
if json_flat[options[:element]] =~ /\D/ then
say(options[:v], 'The value of %s contains non-numeric characters.' % [options[:element]])
puts 'UNKNOWN: Return value syntax failure.'
do_exit(options[:v], 3)
end
if options[:warn] then
warn = nutty_parse('Warning', options[:warn], json_flat[options[:element]], options[:v])
if warn == 'FAIL'
puts 'UNKNOWN: Warn threshold syntax failure.'
do_exit(options[:v], 3)
end
end
if options[:crit] then
crit = nutty_parse('Critical', options[:crit], json_flat[options[:element]], options[:v])
if crit == 'FAIL'
puts 'UNKNOWN: Critical threshold syntax failure.'
do_exit(options[:v], 3)
end
end
# Assemble the message in order of precedence.
msg = 'OK: '
exit_code = 0
if warn == 'KO' then
msg = 'WARN: '
exit_code = 1
end
if crit == 'KO' then
msg = 'CRIT: '
exit_code = 2
end
msg << '%s is %s' % [options[:element], json_flat[options[:element]]]
# Finally output the message and exit.
puts msg
do_exit(options[:v], exit_code)
exit(3)