Last active
May 17, 2017 20:35
-
-
Save jderrett/462677bba0be40c66ab9 to your computer and use it in GitHub Desktop.
Ruby script to take a snapshot of a Librato chart
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
#!/usr/bin/env ruby | |
require 'faraday' | |
require 'json' | |
conn = Faraday.new(url: "https://metrics-api.librato.com/v1/") do |f| | |
f.basic_auth ENV['LIBRATO_USER'], ENV['LIBRATO_TOKEN'] | |
# f.response :logger | |
f.adapter Faraday.default_adapter | |
end | |
# Get list of spaces | |
resp = conn.get("spaces") | |
if resp.success? | |
spaces = JSON(resp.body)['spaces'] | |
else | |
raise resp.inspect | |
end | |
# Get space in question | |
space_name = "Librato Agent" | |
space = spaces.detect {|s| s['name'] == space_name} | |
raise "space #{space_name} not found" unless space | |
# Get charts on the space | |
resp = conn.get("spaces/#{space['id']}/charts") | |
if resp.success? | |
charts = JSON(resp.body) | |
else | |
raise resp.inspect | |
end | |
# Find the chart by name | |
chart_name = "Memory Free" | |
chart = charts.detect {|c| c['name'] == chart_name} | |
raise "chart #{chart_name} not found" unless chart | |
# Set up snapshot params | |
chart_id = chart['id'] | |
chart_type = 'line' | |
# chart_type = 'stacked' | |
source = "*" | |
duration = 3600 | |
end_time = Time.now.to_i | |
# chart_type = 'stacked' | |
params = { | |
subject: { | |
chart: { | |
id: chart_id, | |
source: source, | |
type: chart_type | |
} | |
}, | |
duration: duration, | |
end_time: end_time | |
} | |
# Take the snapshot | |
resp = conn.post do |req| | |
req.url "snapshots" | |
req.headers["Content-Type"] = 'application/json' | |
req.body = params.to_json | |
end | |
if resp.success? | |
payload = JSON(resp.body) | |
snapshot_href = payload['href'] | |
job_href = payload['job_href'] | |
else | |
raise resp.inspect | |
end | |
# Poll job until it is finished | |
max_poll_time = 30 | |
slept = 0 | |
job_state = nil | |
interval = 1 | |
until job_state == 'complete' || slept > max_poll_time | |
puts "Job state is: #{job_state}" | |
puts "sleeping #{interval}..." | |
sleep interval | |
# poll job | |
puts "Polling..." | |
resp = conn.get(job_href) | |
if resp.success? | |
job = JSON(resp.body) | |
job_state = job['state'] | |
else | |
raise resp.inspect | |
end | |
slept += interval | |
end | |
puts "Spent #{slept} seconds getting chart id #{chart_id}" | |
if job_state == 'complete' | |
# Retrieve finished snapshot with image URL | |
resp = conn.get(snapshot_href) | |
if resp.success? | |
snapshot_data = JSON(resp.body) | |
image_href = snapshot_data['image_href'] | |
puts "Chart: #{chart['name']}" | |
puts "Snapshot URL:\n" | |
puts image_href | |
else | |
raise resp.inspect | |
end | |
else | |
puts "Job status: #{job_state}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment