sensu-plugins / sensu-plugins-http

This plugin provides native HTTP instrumentation for monitoring and metrics collection, including: response code, JSON response, HTTP last modified, SSL expiry, and metrics via `curl`.
http://sensu-plugins.io
MIT License
30 stars 97 forks source link

Example in `check-http-json` does not work. #115

Open robertely opened 5 years ago

robertely commented 5 years ago

Trying this example:

#   Check that will verify http status, JSON validity, and that page.totalElements value is
#   greater than 10
#      ./check-http-json.rb -u http://my.site.com/metric.json --key page.totalElements --value-greater-than 10

I don't see where page.totalElements is setup. It does not exist in leaf so far as i see.

Command run:

/opt/sensu/embedded/bin/ruby /opt/sensu/embedded/bin/check-http-json.rb -u https://jsonplaceholder.typicode.com/todos/ --key page.totalElements --value-less-than 10

Returns:

CheckJson CRITICAL: key check failed: undefined method `to_f' for #<Array:0x000000013a9f70>
Did you mean?  to_s
               to_a
               to_h

Versions:

/opt/sensu/embedded/bin/ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
/opt/sensu/embedded/bin/gem list |grep 'sensu-plugins-http'
sensu-plugins-http (3.0.1)
/opt/sensu/embedded/bin/sensu-client --version
1.4.2
robertely commented 5 years ago

I'm realizing now that I miss-understood the example. I expected it to count elements on the page when really it was checking for a key page.totalElements

So I misunderstood that but adding the behavior I expected would maybe be not so bad.

We could perhaps detect the type on the key and make some reasonable decisions:

      if leaf.is_a?(Array)
        if config[:value]
          # For if you inexplicably wanted to pass in the string version of a ruby array on the command line
          raise "unexpected value for key: '#{leaf}.to_s' != '#{config[:value]}'" unless leaf.to_s == config[:value].to_s
          message += "equals '#{config[:value]}'"
        end
        if config[:valueGt]
          raise "unexpected count for key: '#{leaf.count}' not > '#{config[:valueGt]}'" unless leaf.count > config[:valueGt].to_f
          message += "greater than '#{config[:valueGt]}'"
        end
        if config[:valueLt]
          raise "unexpected count for key: '#{leaf.count}' not < '#{config[:valueLt]}'" unless leaf.count < config[:valueLt].to_f
          message += "less than '#{config[:valueLt]}'"
        end
      else
        if config[:value]
          raise "unexpected value for key: '#{leaf}' != '#{config[:value]}'" unless leaf.to_s == config[:value].to_s
          message += "equals '#{config[:value]}'"
        end
        if config[:valueGt]
          raise "unexpected value for key: '#{leaf}' not > '#{config[:valueGt]}'" unless leaf.to_f > config[:valueGt].to_f
          message += "greater than '#{config[:valueGt]}'"
        end
        if config[:valueLt]
          raise "unexpected value for key: '#{leaf}' not < '#{config[:valueLt]}'" unless leaf.to_f < config[:valueLt].to_f
          message += "less than '#{config[:valueLt]}'"
        end
      end

Additionally we could add support for the top level element by implementing a keyword.

  def deep_value(data, desired_key, parent = '')
    if desired_key == '.'
      return data
    end
    case data
    when Array
      data.each_with_index do |value, index|
...

Then something like this would work: check-http-json-sg.rb -u https://jsonplaceholder.typicode.com/todos/ --key . --value-greater-than 5

If your interested I could clean that up and make a pr