Skip to content

Instantly share code, notes, and snippets.

@noahhl
Created March 3, 2013 21:38
Show Gist options
  • Select an option

  • Save noahhl/5078423 to your computer and use it in GitHub Desktop.

Select an option

Save noahhl/5078423 to your computer and use it in GitHub Desktop.
Send sar/sysstat metrics to a statsd server, store them as timers
#!/usr/bin/env ruby
require 'socket'
STATSD_HOST = '127.0.0.1'
STATSD_PORT = 8125
NAMESPACE = "sysstat"
@hostname = `hostname -s`.chomp
@statsd = UDPSocket.new
@statsd.connect STATSD_HOST, STATSD_PORT
def report(k, v)
puts "#{NAMESPACE}.#{@hostname}.#{k}:#{v}|ms" if ENV["DEBUG"]
unless ENV["NOSTATSD"]
@statsd.send "#{NAMESPACE}.#{@hostname}.#{k}:#{v}|ms", 0
end
end
switching_contexts = false
headings = []
ARGF.each do |line|
next if line.match(/^Linux/)
if line.strip.empty?
switching_contexts = true
else
if switching_contexts
switching_contexts = false
headings = line.split(" ")[1..-1].map(&:strip).reject(&:empty?).reject{|v| v.match(/AM|PM/)}
else
values = line.split(" ")[1..-1].map(&:strip).reject(&:empty?).reject{|v| v.match(/AM|PM/)}
prefix = nil
values.each_with_index do |v,i|
if i == 0 && (v.to_f.to_s != v && v.to_i.to_s != v)
prefix = v
else
report("#{headings[i]}#{".#{prefix}" if prefix}", v)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment