Skip to content

Instantly share code, notes, and snippets.

@ncdc
Created February 28, 2014 16:29
Show Gist options
  • Save ncdc/9274147 to your computer and use it in GitHub Desktop.
Save ncdc/9274147 to your computer and use it in GitHub Desktop.
Custom rsyslog output program to send metrics to Graphite via Carbon
#!/bin/env ruby
require 'time'
require 'socket'
class GraphiteClient
def carbon
begin
@carbon ||= TCPSocket.new('localhost', 2003)
rescue => e
@carbon = nil
end
@carbon
end
def send(data)
begin
carbon.puts(data) if carbon
rescue => e
@carbon = nil
end
end
end
class MetricHandler
def initialize
@client = GraphiteClient.new
end
def run
while line = gets
begin
# 2014-02-27T14:50:22.011665-05:00
fields = line.split(' ')
timestamp = Time.parse(fields[0]).to_i
app = $1 if line =~ /app=([^ ]+)/
gear = $1 if line =~ /gear=([^ ]+)/
# skip if we don't have app and gear
next unless app and gear
fields.each do |field|
if field =~ /([^=]+)=(.+)/
key = $1
next if %w(app gear type).include?(key)
value = $2
@client.send "#{app}.#{gear}.#{key} #{value} #{timestamp}"
end
end
rescue => e
raise
end
end
end
end
MetricHandler.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment