class Riemann::Tools::HttpJson

Public Class Methods

new() click to toggle source
# File bin/riemann-json-http, line 17
def initialize
  @uri = URI(opts[:http_url])
  @tag = opts[:tag]
  json_attrs = opts[:json_attributes]
  @regexps = json_attrs.map {|item| Regexp.new("^#{item}$") }
  @service_name = opts[:service_name]
  puts "JSON nodes: #{@regexps.to_s}"
  puts "Service name: #{@service_name}"
  puts "URL to try: #{@uri}"
end

Public Instance Methods

filter_attributes(hash, regexps, parent_name=nil, results = {}) click to toggle source
# File bin/riemann-json-http, line 51
def filter_attributes(hash, regexps, parent_name=nil, results = {})
  hash.each do |key, v|
    value = v || key
    new_parent = if parent_name
      "#{parent_name}.#{key}"
    else
      key
    end
    if value.is_a?(Hash) || value.is_a?(Array)
      filter_attributes(value, regexps, new_parent, results)
    else
      if matching_node(new_parent, regexps)
        results[new_parent] = value
      end
    end
  end
  results
end
get_response(uri) click to toggle source
# File bin/riemann-json-http, line 74
def get_response(uri)
  Net::HTTP.get(uri)
end
matching_node(name, regexps) click to toggle source
# File bin/riemann-json-http, line 70
def matching_node(name, regexps)
  regexps.map {|r| name.match(r) }.compact.size > 0
end
tick() click to toggle source
# File bin/riemann-json-http, line 28
def tick

  json = JSON.parse(get_response(@uri))
  attributes_to_report = filter_attributes(json, @regexps)

  attributes_to_report.each do |key, value|
    if !value.is_a? Numeric
      next
    end
    name = if @service_name
      "#{@service_name} #{key}"
    else
      key
    end
    report(
       :host    => opts[:event_host],
       :service => name,
       :metric  => value.to_f,
       :state   => "ok"
    )
  end
end