Last active
August 29, 2015 14:21
-
-
Save jderrett/8b577cd5aa62e6b554e8 to your computer and use it in GitHub Desktop.
Get N measurements for a Librato metric using the Ruby wrapper (and do some calcs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'librato/metrics' | |
Librato::Metrics.authenticate '[email protected]', 'token' | |
months_ago = 2 | |
metric_name = 'AWS.EC2.CPUUtilization' | |
resolution = 84600 # Daily | |
source_matcher = "us-east-1*" # Set to nil for all sources | |
next_time = Time.now.to_i - (months_ago * 730 * 3600) | |
results = {} | |
while next_time | |
puts "Querying for: #{next_time}" | |
opts = {start_time: next_time, resolution: resolution} | |
opts.merge!(source: source_matcher) if source_matcher | |
if resp = Librato::Metrics.get_metric(metric_name, opts) | |
next_time = resp['query'] ? resp['query']['next_time'] : nil | |
resp['measurements'].each do |source, measurements| | |
if results[source] | |
results[source] << measurements | |
results[source].flatten! | |
else | |
results[source] = measurements | |
end | |
end | |
else | |
next_time = nil | |
end | |
end | |
# Calc "average of average" for each source, for example | |
results.each do |source, measurements| | |
avg_of_avg = measurements.map {|m| m['value']}.reduce(:+) / measurements.size.to_f | |
puts "#{source}: #{avg_of_avg}" | |
end;nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment