Created
January 10, 2012 20:56
-
-
Save ohlol/1591143 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 '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