Skip to content

Instantly share code, notes, and snippets.

@denzuko
Forked from noahhl/sar-statsd.rb
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save denzuko/9ac2ca423c9dc00d3f4c to your computer and use it in GitHub Desktop.

Select an option

Save denzuko/9ac2ca423c9dc00d3f4c to your computer and use it in GitHub Desktop.
#!/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