Skip to content

Instantly share code, notes, and snippets.

@macros
Created March 9, 2010 22:04
Show Gist options
  • Save macros/327186 to your computer and use it in GitHub Desktop.
Save macros/327186 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'xml/libxml'
Gmeta
include XML::SaxParser::Callbacks
def initialize
@ganglia = {}
@grid = nil
@cluster = nil
@host = nil
@metric = nil
end
def on_start_element(element, attributes)
if element == "GANGLIA_XML"
@ganglia['version'] = attributes['VERSION']
end
if element == "GRID"
@grid = attributes['NAME']
@ganglia[@grid] = {}
end
if element == "CLUSTER"
@cluster = attributes['NAME']
@ganglia[@grid][@cluster] = { 'alive' => [] }
end
if element == "HOST"
@host = attributes['NAME']
if attributes['TN'].to_i < attributes['TMAX'].to_i * 4
@ganglia[@grid][@cluster]['alive'] << @host
end
@ganglia[@grid][@cluster][@host] = { 'metrics' => {} }
@ganglia[@grid][@cluster][@host]['ip'] = attributes['IP']
@ganglia[@grid][@cluster][@host]['last_update'] = attributes['REPORTED']
end
if element == "METRIC"
@metric = attributes['NAME']
@ganglia[@grid][@cluster][@host]['metrics'][@metric] = {}
@ganglia[@grid][@cluster][@host]['metrics'][@metric]['value'] = attributes['VAL']
@ganglia[@grid][@cluster][@host]['metrics'][@metric]['type'] = attributes['TYPE']
@ganglia[@grid][@cluster][@host]['metrics'][@metric]['units'] = attributes['UNITS']
@ganglia[@grid][@cluster][@host]['metrics'][@metric]['slope'] = attributes['SLOPE']
end
if element == "EXTRA_ELEMENT"
k = attributes['NAME'].downcase
v = attributes['VAL'].downcase
@ganglia[@grid][@cluster][@host]['metrics'][@metric][k] = v
end
end
def on_end_document
# puts @ganglia.inspect
end
end
lparser = XML::SaxParser.file('ganglia.xml')
lparser.callbacks = Gmeta.new
lx_start = Time.now
lparser.parse
lx_end = Time.now
puts "LibXML Parsing: #{lx_end - lx_start}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment