Skip to content

Instantly share code, notes, and snippets.

@somebox
Created November 12, 2013 13:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save somebox/7430453 to your computer and use it in GitHub Desktop.
Save somebox/7430453 to your computer and use it in GitHub Desktop.
Graphing unicorn active/queued processes (via sockets)
#!/usr/bin/env ruby
require 'rubygems'
require 'raindrops'
require 'statsd'
#
# This script monitors the active and queued unicorn processes.
# It uses the raindrops gem to look at the unix domain socket.
# Stats are polled every second (for one minute) and pushed to
# statsd as a counter. This script runs once a minute.
#
# There is one parameter: the location of the socket
#
# The stats are viewable on the Graphite paths:
#
# stats.guages.rails.unicorns.[hostname].[app].[active,queued].[max,average]
#
# Example:
#
# $ unicorn-graphite location-app
#
# Graphite Settings:
GRAPHITE_HOST = "graphite"
GRAPHITE_PORT = 8125
# ---
@app = ARGV[0]
@socket = "/var/nginx/#{@app}/unicorn.sock"
@host =`/bin/hostname -s`.chomp
@namespace = "rails.unicorns.#{@host}.#{@app.gsub('.','-')}"
@summary= {}
def update_totals(name, value)
@summary[name] ||= {}
@summary[name][:total] ||= 0
@summary[name][:total] += value
@summary[name][:max] ||= 0
@summary[name][:max] = value if (value > @summary[name][:max])
end
def send_stats(name)
avg = @summary[name][:total]/60
max = @summary[name][:max]
statsd = Statsd.new(GRAPHITE_HOST, GRAPHITE_PORT)
statsd.gauge("#{@namespace}.#{name}.average", avg)
statsd.gauge("#{@namespace}.#{name}.max", max)
# puts "#{name} avg:#{avg} max:#{max}"
end
60.times do
# raindrops returns an array of arrays, which is weird:
stats = Raindrops::Linux.unix_listener_stats(@socket).first
stats = stats[1]
# puts "#{@host} - active: #{stats.active} queued: #{stats.queued}"
update_totals(:active, stats.active)
update_totals(:queued, stats.queued)
sleep(1)
end
# send result to statsd
send_stats(:active)
send_stats(:queued)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment