-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to check multiple values on optional flag #33
Conversation
Wow, this is a big one, and I'd like to take some time to review it properly. 😄 |
# Requires. | ||
require 'rubygems' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will break compatibility for old Rubies. 😢 Given the, er, quaint nature of many a Nagios server out there, I'm hesitant to pull this trigger. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really? I didint seen a required binding to the rubygems lib. Do you know what breaks in 1.8.7?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(pr/33) $ ruby --version
ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]
(pr/33) $ gem list
*** LOCAL GEMS ***
json (1.8.3)
(pr/33) $ ./check_http_json.rb
./check_http_json.rb:22:in `require': no such file to load -- json (LoadError)
from ./check_http_json.rb:22
(pr/33 *) $ grep -n rubygems ./check_http_json.rb
22:require 'rubygems'
(pr/33 *) $ ./check_http_json.rb
UNKNOWN: Insufficient or incompatible arguments.
Must specify target URI or file.
Must specify a desired element.
Must specify an expected result OR the warn and crit thresholds.
UNKNOWN: "./check_http_json.rb --help" for more information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i re-added the rubygems with an comment for ruby compatibility
require 'json' | ||
require 'net/http' | ||
require 'net/https' | ||
require 'uri' | ||
require 'optparse' | ||
require 'timeout' | ||
|
||
# Manage Nagios messages and exit code | ||
module Nagios |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the interest of using a module here, and I've no qualm with the code you've produced; however, I'm curious as to the why. As in: in this particular code base, why replace the otherwise functioning do_exit
function with a module at all?
Part of the reason that everything is a function is because I wanted to keep the code simple and readable by sysadmins who might not otherwise be programmers. If one is coming from a world of, say, Bash, functions are easy to digest. By introducing a self-referencing class, as well as the getter & setter pattern, you're also introducing more advanced (and mildly Ruby-specific) concepts.
Again, this is something that I was actively trying to avoid in this particular code base. 😄 I'm open to debate on the topic though!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this is a self instance which is required to hold the current state of the code level.
A module is accessible everywhere in the code, which reduces the complexity of usage ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a compromise we can replace the getter/setter magic by native functions for better readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The module is accessible everywhere, but so is a global variable, for example. I realise that this isn't The Ruby Way™, but it is simple, and that matters. Can you see a way to implement this functionality using variable instead of a module? If not, that's cool, but I'd like to explore that option.
@@ -88,7 +108,7 @@ def hash_flatten(hash, delimiter, prefix = nil, flat = {}) | |||
# http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT | |||
def nutty_parse(thresh, want, got, v, element) | |||
retval = 'FAIL' | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for cleaning up the trailing whitespace and stuff all over the place. 👍
I ran some manual tests (TODO: automated tests 😝) in the following versions:
After re-adding Beyond the line-specific commentary above, I'd like to address the output a little bit. I don't know if I have any answers, but it's worth addressing since the new functionality may need to have amplified output and/or additional explanation in the docs. Consider:
In the above example, the script (correctly) exits 0, but the OK message is empty. In every other instance (including other OK conditions) it contains something, which means this feels incorrect and could lead to a bad user experience. Consider:
Ok, but what about Consider:
This is true; however The user experience here is important. I'm open to any thoughts you have on how to make this easier / smoother for the poor sysadmins that have to interpret these results at 02:00. |
For the sake of completeness, the full output of the endpoint above is here: http://pastebin.com/fU6BRKNs |
Yes, this is difficult to catch or represent in a pretty way. We are limited on the nagios output "one" line -> so i decided its enough to send out the last warning. On critical it exits immediately. Same like the OK message, but we can output the element/threshold/result parameter here. This will help to understand that the given options are used to verify everything is ok
|
You make a good point. This is something we can play with later in any case, so let's not address it right now.
Yes - the OK message should definitely have some sort of positive confirmation in it. |
check_http_json.rb
Outdated
end | ||
# build ok message | ||
if options[:result_string] | ||
Nagios.ok = '%s does match %s', [element_message_name, options[:result_string]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In 1.8.7 this gets interpreted literally, as in:
(pr/33) $ ./check_http_json.rb -u http://jsonplaceholder.typicode.com/users -e 1.id -r 2
OK: %s does match %s1.id2
In 1.9.3 and 2.4.0:
(pr/33) $ ./check_http_json.rb -u http://jsonplaceholder.typicode.com/users -e 1.id -r 2
OK: ["%s does match %s", ["1.id", "2"]]
check_http_json.rb
Outdated
if options[:result_string] | ||
Nagios.ok = '%s does match %s', [element_message_name, options[:result_string]] | ||
elsif options[:result_regex] | ||
Nagios.ok = "'%' (regex) does match %s", [element_message_name, options[:result_regex]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue here with 1.8.7:
OK: %s does match %sFirst '1.id' (regex)2
1.9.3 and 2.4.0:
OK: ["%s does match %s", ["First '1.id' (regex)", "2"]]
Thanks for the conversation and reactivity on this PR. 💯 I think we're OK to merge from a code perspective; however, let's be sure to open an Issue to update the README to reflect the new functionality. Ok with you? |
LGTM. Lets merge ;-) |
Did some refactor to check multiple elements, which can be enabled by --element_regex_global.
Replaced the define do_exit by a Nagios module to handle the exit message and code.
Add rubocop definition.
Implements #32