Skip to content

Instantly share code, notes, and snippets.

@jeffsu
Created October 6, 2011 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffsu/1267954 to your computer and use it in GitHub Desktop.
Save jeffsu/1267954 to your computer and use it in GitHub Desktop.
Scrape HAProxy's status page with ruby and nokogiri
require 'open-uri'
require 'nokogiri'
URL = ARGV.shift
doc = Nokogiri::HTML(open(URL))
tables = doc.css(".tbl")
services = Hash.new
# every even table is a header
# every odd table is the status body
tables.each_with_index do |header_table,i|
next if i.odd?
status = {}
services[header_table.css('.pxname').children.to_s] = status
# new header per header_table
status_header = []
# body
header_table.next_sibling.css('tr').each_with_index do |tr,j|
# gather header
if tr['class'].match(/titr/)
if j == 1
status_header = tr.css('th').collect { |th| th.content.downcase }
end
else
server_status = {}
tr.css('td').each_with_index do |td, k|
# remove spans
td.children.css('span').each { |span| span.replace span.content }
text = td.content
if k == 0
status[text] = server_status
else
# -1 because server name has no th
server_status[status_header[k-1]] = text
end
end
end
end
end
puts services.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment