Skip to content

Instantly share code, notes, and snippets.

@ohlol
Created January 10, 2012 20:56
Show Gist options
  • Save ohlol/1591143 to your computer and use it in GitHub Desktop.
Save ohlol/1591143 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'sensu-handler'
require 'socket'
class Graphite < Sensu::Handler
def send_graphite(metric, msg)
host = 'graphite.host.com'
port = 2003
begin
sock = TCPSocket.open(host, port)
sock.puts "#{metric} #{msg} #{Time.now.to_i}"
sock.close
return true
rescue SocketError
return false
end
end
def handle
hostname = @event['client']['name'].split('.')[0]
checkname = @event['check']['name'].gsub(%r|[ \.]|, '_')
status = @event['action'].gsub(%r|[ \.]|, '_') # just in case
occurences = @event['occurences'] || 1
metric = "sensu.checks.#{hostname}.#{checkname}.#{status}"
begin
timeout(3) do
if send_graphite(metric, occurences)
puts 'graphite -- sent metric for ' + metric
else
puts 'graphite -- failed to send metric for ' + metric
end
end
rescue Timeout::Error
puts 'graphite -- timed out while sending for ' + metric
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment