Hi,
I have coded some Ruby script to scrap some web data; then I merged that script with a small Sinatra application to "publish" some rest methods to fetch reports...
My Sinatra app is making a request to Nagios3 and navigate through some forms (3 steps in fact), this is just working fine.
Then under step3, I "press" the submit button and after a few seconds I have a huge html report with servicegroups availabilities. At this point I am using Nokogiri to extract just one field, this is working fine too for any servicegroup (it can manage any servicegroup dimension).
To have the /index with a list of servicegroups, I need to goto Step1 and Step2 only.
The Step3 is needed just for /check (build the report and fetch / print availability)
Well, all is just working fine so the question I'm making is like weird because the thing is I don't know how to dry it correctly.
I'm writting the #Initialize Mechanize stuff, Step1 and Step2 under before do filters. They are needed in both /index and /check, but in fact I don't know if this is the right place to do it.
Any code architectural tip :)
thanks in advance.
Francisco
require 'rubygems'
require 'sinatra'
require 'mechanize'
require 'nokogiri'
require 'logger'
configure :production do
enable :logging, :dump_errors
enable :inline_templates
disable :run
end
error do
e = request.env['sinatra.error']
puts e.to_s
puts e.backtrace.join("\n")
"Application Error!"
end
not_found do
"Page not found!"
end
before do
content_type 'text/html', :charset => 'utf-8'
# Initialize Mechanize
@agent = Mechanize.new
@agent.keep_alive = false
@agent.open_timeout = 3600
@agent.read_timeout = 3600
@agent.max_history = 0
@agent.log = Logger.new('mechanize.log')
@agent.auth('myusername', 'mysecretpass')
@page = @agent.get('http://nagios3/nagios/cgi-bin/avail.cgi')
# Step1 and Step2 are use in both /index and /check
# Step1 - Form1 (Select Report Type[hostgroups,hosts,hostgroups,servicegroups*,services])
@form1 = @page.forms.first
@form1.field_with(:name => 'report_type').options[2].select
@page = @agent.submit(@form1)
# Step2 - Form2 (Select Servicegroup)
@form2 = @page.forms.first
@total_services_list = @form2.field_with(:name => 'servicegroup').options
end
get '/' do
# When under /index we don't go further to Step3 - Form3 (generate report)
erb :index
end
get '/check/:servicegroup' do
@servicegroup = params[:servicegroup].to_i
# Step3 - Form3 (Select Report Options)
@form2.field_with(:name => 'servicegroup').options[@servicegroup].select
@page = @agent.submit(@form2)
@form3 = @page.forms.first
@form3.field_with(:name => 'timeperiod').options[7].select
@page = @agent.submit(@form3)
# Step4 - Extract Average from computed data
page_html = Nokogiri::HTML.parse(@page.body)
@total_hostservices_size = page_html.xpath("html/body/div[3]/table/tr[*]/td[2]").to_a.size
puts @average = page_html.xpath("html/body/div[3]/table/tr[#{@total_hostservices_size}]/td[2]").inner_text
erb :check, :layout => false
end
__END__
@@layout
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>Nagios Availability Checker</title></head>
<body>
<strong>Nagios Availability Checker</strong>
<hr />
<div>
<%= yield %>
</div>
<hr />
</body>
</html>
@@index
<h3>List Service Groups</h3>
<% @total_services_list.each_with_index do |name,id| %>
<a href="/check/<%= id %>"><%= id %> - <%= name %></a><br />
<% end %>
@@check
<%= @average %>