Skip to content

Instantly share code, notes, and snippets.

@piavlo
Created November 28, 2012 07:59
Show Gist options
  • Save piavlo/4159748 to your computer and use it in GitHub Desktop.
Save piavlo/4159748 to your computer and use it in GitHub Desktop.
{
"heroku": {
"auth_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"url": "http://example.com"
}
}
#!/usr/bin/env ruby
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'amqp'
require 'json'
require 'httparty'
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
require 'monkey_patch'
config = JSON.parse(File.open('/etc/sensu/config.json', 'r').read, {:symbolize_names => true})
config = config.deep_merge(JSON.parse(File.open('/etc/sensu/addons.json', 'r').read, {:symbolize_names => true}))
EventMachine.run do
AMQP.connect(config[:rabbitmq]) do |connection|
channel = AMQP::Channel.new(connection)
exchange = channel.topic("kpi_events", :auto_delete => false, :durable => true)
# Subscribers
channel.queue("", :exclusive => true) do |queue|
queue.bind(exchange, :routing_key => "kpi_events").subscribe do |metadata, payload|
begin
data = JSON.parse(payload)
check = data['check']
output = JSON.parse(check['output'])
output['auth_token'] = config[:heroku][:auth_token]
puts "#{config[:heroku][:url]}/widgets/%s" % check['name']
response = HTTParty.post("#{config[:heroku][:url]}/widgets/%s" % check['name'], :body => output.to_json)
puts response.inspect
rescue => e
puts e.message
end
end
end
stop_amqp = Proc.new { connection.close { EventMachine.stop } }
Signal.trap "TERM", stop_amqp
end
end
{
"handlers": {
"kpi_events": {
"type": "amqp",
"exchange": {
"type": "topic",
"name": "kpi_events",
"durable": "true",
"routing_key": "kpi_events"
}
}
}
}
{
"checks": {
"recent_price_updates": {
"command": "/etc/sensu/plugins/metric-recent-price-updates.rb",
"interval": 300,
"occurrences": 1,
"subscribers": ["products"],
"handlers": ["kpi_events"]
}
}
}
{
"checks": {
"storm_incoming": {
"command": "/etc/sensu/plugins/metric-storm-incoming.rb",
"interval": 300,
"subscribers": ["storm-master"],
"handlers": ["kpi_events"]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment