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
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/check_graphite.rb', line 27
def check
uri = URI(URI.encode("#{options.endpoint}?target=#{options.metric}&from=-#{options.from}&format=json"))
req = Net::HTTP::Get.new(uri.request_uri)
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"
if res.body == "[]"
if options.send("ignore-missing")
store_value options.name, 0
store_message "#{options.name} missing - ignoring"
return
else
raise "no data returned for target"
end
end
datapoints = JSON(res.body).map { |e| e["datapoints"] }.reduce { |a, b| a + b }
datapoints = datapoints.slice(
options.dropfirst,
(datapoints.size - options.dropfirst - options.droplast)
)
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
|