Skip to content

Instantly share code, notes, and snippets.

@danshultz
Last active August 29, 2015 13:57
Show Gist options
  • Save danshultz/9439424 to your computer and use it in GitHub Desktop.
Save danshultz/9439424 to your computer and use it in GitHub Desktop.
Monitoring your unicorn workers with Sensu
# Monitor the unicorn processes on 10 second intervals
unicorn_monitor = Proc.new {
require 'socket'
require 'json'
require 'raindrops'
address = "0.0.0.0:8080"
socket = Raindrops::InetDiagSocket.new
udp = UDPSocket.new
while(true) do
begin
data = Raindrops::Linux.tcp_listener_stats(address, socket)
stat = data[address]
output = [:active, :queued].map { |t|
["#{Socket.gethostname}.unicorn.#{t}", stat.send(t), Time.now.to_i].join("\t")
}.join("\n") + "\n" # all graphite input must end in a newline
check = {
:handlers => ["metrics"],
:name => 'unicorn-metrics',
:status => 0,
:output => output,
:type => 'metric'
}
udp.send(check.to_json, 0, '0.0.0.0', 3030)
rescue # make sure thread doesn't accidently die
end
sleep(10)
end
}
Thread.new(&unicorn_monitor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment