Skip to content

Commit

Permalink
Merge pull request #37 from kohenkatz/patch-1
Browse files Browse the repository at this point in the history
Adding REGEX support for WARN and CRIT
  • Loading branch information
phrawzty authored Feb 11, 2019
2 parents b4384d6 + 6dd8ed2 commit 2ceffcc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Usage: ./check_http_json.rb -u <URI> -e <element> -w <warn> -c <crit>
-W, --result_warn STRING Warning if element is [string]. -C is required.
-U, --result_unknown STRING Unknown if element is [string]. -C is required.
-C, --result_crit STRING Critical if element is [string]. -W is required.
--result_warn_regex REGEX Warning if element matches REGEX. --result_crit_regex is required.
--result_unknown_regex REGEX Unknown if element matches REGEX. --result_crit_regex is required.
--result_crit_regex REGEX Critical if element matches REGEX. --result_warn_regex is required.
-p, --perf ELEMENT Output additional fields (performance metrics).
--perf_splitter CHARACTER Additional fields delimiter (default is comma).
-t, --timeout SECONDS Wait before HTTP timeout.
Expand All @@ -37,12 +40,12 @@ If a simple result of either string or regular expression (`-r` or `-R`) is spec
* A match is OK and anything else is CRIT.
* The warn / crit thresholds will be ignored.

If the warn and crit results (`-W` and `-C`) are specified:
If the warn and crit results (`-W` and `-C`) or regular expressions (`--result_warn_regex` and `--result_crit_regex`) are specified:

* A match is WARN or CRIT and anything else is OK.
* The warn / crit thresholds will be ignored.

Note that (`-r` or `-R`) and (`-W` and `-C`) are mutually exclusive.
Note that (`-r` or `-R`), (`-W` and `-C`), and (`--result_warn_regex` and `--result_crit_regex`) are mutually exclusive.

Note also that the response must be pure JSON. Bad things happen if this isn't the case.

Expand Down
44 changes: 40 additions & 4 deletions check_http_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,21 @@ def parse_args(options)
options[:result_string_crit] = x
end

options[:result_regex_warn] = nil
opts.on('--result_warn_regex REGEX', 'Warning if element matches REGEX. --result_crit_regex is required.') do |x|
options[:result_regex_warn] = x
end

options[:result_regex_unknown] = nil
opts.on('--result_unknown_regex REGEX', 'Unknown if element matches REGEX. --result_crit_regex is required.') do |x|
options[:result_regex_unknown] = x
end

options[:result_regex_crit] = nil
opts.on('--result_crit_regex REGEX', 'Critical if element matches REGEX. --result_warn_regex is required.') do |x|
options[:result_regex_crit] = x
end

options[:perf] = nil
opts.on('-p', '--perf ELEMENT', 'Output additional fields (performance metrics); comma-separated.') do |x|
options[:perf] = x
Expand Down Expand Up @@ -410,7 +425,7 @@ def sanity_check(options)
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
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]) or (options[:result_regex_warn] and options[:result_regex_crit])) then
error_msg.push('Must specify an expected result OR the warn and crit thresholds.')
end

Expand Down Expand Up @@ -506,13 +521,15 @@ def sanity_check(options)

# build ok message
if options[:result_string]
Nagios.ok = '%s does match %s' % [element_message_name, options[:result_string]]
Nagios.ok = '%s does match \'%s\'' % [element_message_name, options[:result_string]]
elsif options[:result_regex]
Nagios.ok = "'%s' (regex) does match %s" % [element_message_name, options[:result_regex]]
Nagios.ok = '\'%s\' (regex) does match \'%s\'' % [element_message_name, options[:result_regex]]
end

if options[:result_string_warn] && options[:result_string_crit]
Nagios.ok = '%s does not match %s or %s' % [element_message_name, options[:result_string_warn], options[:result_string_crit]]
Nagios.ok = '%s does not match \'%s\' or \'%s\'' % [element_message_name, options[:result_string_warn], options[:result_string_crit]]
elsif options[:result_regex_warn] && options[:result_regex_crit]
Nagios.ok = '%s does not match (REGEX) \'%s\' or \'%s\'' % [element_message_name, options[:result_regex_warn], options[:result_regex_crit]]
end

if options[:crit]
Expand Down Expand Up @@ -562,6 +579,25 @@ def sanity_check(options)
# check next element
next
end

# If we're specifying critical & warning regex...
if options[:result_regex_warn] && options[:result_regex_crit]
say(options[:v], '%s should not match against \'%s\' (REGEX), else CRIT' % [element, options[:result_regex_crit]])
say(options[:v], '%s should not match against \'%s\' (REGEX), else WARN' % [element, options[:result_regex_warn]])
msg = '%s matches %s' % [element, element_value]

case element_value.to_s
when Regexp.new(options[:result_regex_crit].to_s)
Nagios.critical = msg
when Regexp.new(options[:result_regex_warn].to_s)
Nagios.warning = msg
when Regexp.new(options[:result_regex_unknown].to_s)
Nagios.unknown = msg
end
# check next element
next
end


# If we're dealing with threshold values...

Expand Down

0 comments on commit 2ceffcc

Please sign in to comment.