Created
October 6, 2011 17:01
-
-
Save jeffsu/1267954 to your computer and use it in GitHub Desktop.
Scrape HAProxy's status page with ruby and nokogiri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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