Class: CheckGraphite::Command

Inherits:
Object
  • Object
show all
Includes:
NagiosCheck
Defined in:
lib/check_graphite.rb

Instance Method Summary collapse

Instance Method Details

#checkObject



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
# File 'lib/check_graphite.rb', line 24

def check
  uri = URI(URI.encode("#{options.endpoint}?target=#{options.metric}&from=-#{options.from}&format=json"))
  req = Net::HTTP::Get.new(uri.request_uri)

  # use basic auth if username is set
  if options.username
    req.basic_auth options.username, options.password
  end

  res = Net::HTTP.start(uri.host, uri.port, :use_ssl => 'https' == uri.scheme) { |http|
    http.request(req)
  }

  raise "HTTP error code #{res.code}" unless res.code == "200"
  raise "no data returned for target" if res.body == "[]"

  datapoints = JSON(res.body).map { |e| e["datapoints"] }.reduce { |a, b| a + b }
  datapoints = datapoints.slice(
    options.dropfirst,
    (datapoints.size - options.dropfirst - options.droplast)
  )

  # Remove NULL values. Return UNKNOWN if there's nothing left.
  datapoints.reject! { |v| v.first.nil? }
  raise "no valid datapoints" if datapoints.size == 0

  sum = datapoints.reduce(0.0) {|acc, v| acc + v.first }
  value = sum / datapoints.size
  store_value options.name, value
  store_message "#{options.name}=#{value}"
end