Skip to content

Instantly share code, notes, and snippets.

@ctrochalakis
Created June 25, 2010 10:17
Show Gist options
  • Save ctrochalakis/452674 to your computer and use it in GitHub Desktop.
Save ctrochalakis/452674 to your computer and use it in GitHub Desktop.
draft for parsing munin data
require 'eventmachine'
module Munin
include EM::Deferrable
Delimiter = "\n".freeze
End = '.'.freeze
def receive_data(data)
(@buffer||='') << data
while index = @buffer.index(Delimiter)
line = @buffer.slice!(0,index+1)
process_line line
end
end
def connection_completed
succeed
end
def process_line(line)
case line.strip
when /^#/ # comment
when End
handle_callback if @cb
@data = {}
# e.x load.value 1.3
when /^([^\.]+)\.value (.+)$/
@data ||= {}
@data[$1] = $2
else
raise "Don't know how to #{line}"
end
end
def handle_callback
@cb.call @data.size == 1 ? @data.values.first : @data
end
def method_missing(command, &block)
callback {
@cb = block
send_data "fetch #{command}"
send_data Delimiter
}
end
end
EM::run do
mun = EM.connect '127.0.0.1', 4949, Munin
mun.load { |load| puts load }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment