Scott
8/20/2006 12:19:00 AM
Wow, this can't be "The Ruby Way", but it works:
def nagios_data(data)
blocks = data.strip.split(/.*\}\n(?=\w+\s+\{)/)
blocks.map do |nagios_data|
data_points = nagios_data.split("\n")
# you can possibly use this as a hash key if they will be unique
# block_name = data_points[0][/\w+/]
data_points[1..-1].inject({}) do |values, data_point|
unless data_point =~ /\s+\}/
key, value = data_point.strip.sub(/#.*$/, "").split(/\s*=\s*/, 2)
values[key.intern] = value
end
values
end
end
end
require 'pp'
pp nagios_data(File.read("status.dat"))