Skip to content

Instantly share code, notes, and snippets.

@netshade
Created January 5, 2012 17:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netshade/1566141 to your computer and use it in GitHub Desktop.
Save netshade/1566141 to your computer and use it in GitHub Desktop.
metrics from stdin to instrumental
#!/usr/bin/env ruby
# Provide data over STDIN to Instrumental
# Format of data:
# <metric name> <value> <unix timestamp>
# or
# <metric name> <value>
#
# Second form will assume that the time the
# metric is reported is when the event occurred.
#
# All data is .gauge'd
#
require 'rubygems'
begin
gem 'instrumental_agent'
rescue Gem::LoadError
puts "Requires the Instrumental Agent gem:\n"
puts ' gem install instrumental_agent'
exit 1
end
require 'instrumental_agent'
api_key = ENV['INSTRUMENTAL_TOKEN']
unless api_key
puts "Requires a token:\n"
puts ' input | INSTRUMENTAL_TOKEN=<your API key> ./stats_to_agent'
exit 1
end
collector = ENV['COLLECTOR'] || 'instrumentalapp.com:8000'
unless collector =~ /:\d+$/
puts "Must provide a port when defining collector:\n"
puts ' input | INSTRUMENTAL_TOKEN=<your API key> COLLECTOR=localhost:8000 ./stats_to_agent'
exit 1
end
I = Instrumental::Agent.new(api_key, :collector => collector)
I.synchronous = true
puts "Sending metrics to collector at #{collector}"
input = ARGF.read
input.each_line do |line|
metric, value, unix_ts = line.split(' ')
unix_ts ||= Time.now.to_i
I.gauge(metric, value, unix_ts)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment