Skip to content

Instantly share code, notes, and snippets.

@zadunn
Created October 26, 2012 13:25
Show Gist options
  • Save zadunn/3958807 to your computer and use it in GitHub Desktop.
Save zadunn/3958807 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-handler'
require 'json'
require 'timeout'
class OpenTSDB < Sensu::Handler
# override filters from Sensu::Handler. not appropriate for metric handlers
def filter; end
def handle
tsd_server = settings['opentsdb']['server']
tsd_port = settings['opentsdb']['port']
host_name_len = settings['opentsdb']['hostname_length']
metrics = @event['check']['output']
check_name = @event['check']['name']
sock = TCPSocket.new(tsd_server, tsd_port)
begin
metrics.split("\n").each do |output_line|
mutated_output = ""
(metric_name, metric_value, epoch_time) = output_line.split("\t")
tokens = metric_name.split('.')
host_name = tokens[0..host_name_len].join('.')
short_metric = tokens[(host_name_len + 1)..-1].join('.')
mutated_output = "put #{short_metric} #{epoch_time} #{metric_value} check=#{check_name} host=#{host_name}"
timeout(3) do
sock.puts mutated_output
end
end
ensure
sock.flush
sock.close
end
rescue Timeout::Error
puts "opentsdb -- timed out while sending metrics"
rescue => error
puts "opentsdb -- failed to send metrics : #{error}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment