Skip to content

Instantly share code, notes, and snippets.

@phobos182
Created October 17, 2012 17:58
Show Gist options
  • Save phobos182/3907048 to your computer and use it in GitHub Desktop.
Save phobos182/3907048 to your computer and use it in GitHub Desktop.
OpenTSDB Handler
#!/usr/bin/env ruby
#
# OpenTSDB TCP handler
# ===
#
# This handler sends metrics to a OpenTSDB server via
# TCP socket.
#
# Compatible checks should generate output in the format:
# metric.path.one timestamp value host=fqdn\n
# metric.path.two timestamp value tag=mytag\n
#
# OpenTSDB 'server' and 'port' must be specified in a config file
# in /etc/sensu/conf.d. See opentsdb.json for an example.
#
# Copyright 2012 Jeremy Carroll <http://carrollops.com>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-handler'
class OpenTSDB < Sensu::Handler
# override filters from Sensu::Handler. not appropriate for metric handlers
def filter; end
def handle
tsd_server = settings['tsd']['server']
tsd_port = settings['tsd']['port']
metrics = @event['check']['output']
begin
timeout(3) do
sock = TCPSocket.new(tsd_server, tsd_port)
sock.puts metrics
sock.close
end
rescue Timeout::Error
puts "tsd -- timed out while sending metrics"
rescue => error
puts "tsd -- failed to send metrics : #{error}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment