Created
August 4, 2011 20:51
-
-
Save eric/1126239 to your computer and use it in GitHub Desktop.
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 'cgi' | |
require 'uri' | |
require 'net/http' | |
require 'net/https' | |
USER = ARGV[0] | |
TOKEN = ARGV[1] | |
REGEX = ENV['REGEX'] ? Regexp.new(ENV['REGEX']) : /^(\S+)\s+(\S+)$/ | |
def submit_gauges(values) | |
url = URI.parse('https://metrics-api.librato.com/v1/metrics.json') | |
http = Net::HTTP.new(url.host, url.port) | |
if url.scheme == 'https' | |
require 'net/https' | |
http.use_ssl = true | |
end | |
req = Net::HTTP::Post.new(url.path) | |
req.basic_auth USER, TOKEN | |
data = prepare_gauges(values) | |
req.set_form_data(data) | |
http.start do |http| | |
res = http.request(req) | |
case res | |
when Net::HTTPSuccess, Net::HTTPOK | |
else | |
res.error! | |
end | |
end | |
end | |
def prepare_gauges(values) | |
result = {} | |
values.each_with_index do |(key, value), idx| | |
if key_prefix = ENV['KEY_PREFIX'] | |
key = "#{key_prefix}#{key}" | |
end | |
result["gauges[#{idx}][name]"] = key | |
result["gauges[#{idx}][value]"] = value | |
if ENV['SOURCE'] | |
result["gauges[#{idx}][source]"] = ENV['SOURCE'] | |
end | |
if ENV['PERIOD'] | |
now = Time.now | |
now = now - (now.to_i % ENV['PERIOD'].to_i) | |
result["gauges[#{idx}][measure_time]"] = now.to_i | |
end | |
end | |
result | |
end | |
$stdin.each_line do |line| | |
if line =~ REGEX | |
submit_gauges($1 => $2) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment