Skip to content

Instantly share code, notes, and snippets.

@jtopjian
Last active June 19, 2020 00:15
Show Gist options
  • Select an option

  • Save jtopjian/39d70f58178c5ee70854 to your computer and use it in GitHub Desktop.

Select an option

Save jtopjian/39d70f58178c5ee70854 to your computer and use it in GitHub Desktop.
fcarbon
description "fcarbon"
author "Joe Topjian"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [016]
respawn
respawn limit 5 60
exec /usr/local/bin/fcarbon.rb
#!/usr/bin/env ruby
require 'logger'
require 'bunny'
require 'yaml'
require 'graphite-api'
require 'pp'
config = YAML.load_file('/etc/carbon/fcarbon.yaml')
g = GraphiteAPI.new( graphite: config['graphite_host'])
l = Logger.new('/var/log/fcarbon.log')
l.level = Logger.const_get config['log_level']
connections = {}
while true do
config['amqp_servers'].each do |location, info|
connection = nil
channel = nil
exchange = nil
queue = nil
begin
connection = Bunny.new(info['rabbit_host'])
connection.start
channel = connection.create_channel
exchange = channel.topic(info['topic'], :durable => false, :auto_delete => false)
queue = info['queue']
l.info("Connected to #{location}.")
rescue
l.info("Can't connect to #{location}. Will try again next time.")
next
end
begin
l.info("Entering event loop for #{location}.")
channel.queue(queue, :exclusive => false).bind(exchange).subscribe do |d, prop, pay|
metrics = pay.split("\n")
metrics.each do |metric|
l.debug("Processing metric #{metric}")
m = metric.split(/\s+/)
next if m.length < 3
time = m.pop.strip.to_i
value = m.pop.strip
rest = m.pop
# Because hostnames come in as server.domain.tld, Graphite will make a subdir for each .
# We want to change the hostname to server_domain_tld while still retaining other subdomains
# To do that, config['hostname_regex'] contains a regex to match
r = Regexp.new(config['hostname_regex'])
m = r.match(rest)
if m
host_metric = rest.sub(m[1], m[1].gsub('.', '_'))
else
host_metric = rest
end
g.metrics(
{ host_metric => value }, Time.at(time)
)
end
l.info("Exiting event loop for #{location}.")
connection.close
end
rescue
next
end
end
sleep 60
end
---
log_level: DEBUG
graphite_host: "localhost:2003"
amqp_servers:
yyc:
topic: metrics
queue: metrics
rabbit_host: "amqps://sensu:[email protected]:5672/sensu"
yeg:
topic: metrics
queue: metrics
rabbit_host: "amqps://sensu:[email protected]:5672/sensu"
hnl:
topic: metrics
queue: metrics
rabbit_host: "amqps://sensu:[email protected]:5672/sensu"
hostname_regex: "^(.+?\x5c.ca|.+?\x5c.com)\x5c..*$"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment