Skip to content

Instantly share code, notes, and snippets.

@Packetslave
Created October 1, 2010 04:19
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 Packetslave/605724 to your computer and use it in GitHub Desktop.
Save Packetslave/605724 to your computer and use it in GitHub Desktop.
Script to export statseeker reports as PDF files
#!/usr/bin/env ruby
######################################################################
# Statseeker to PDF Report Generator
#
# Author: Brian Landers <blanders@sapient.com>
# Date: 2010-10-01
#
# Requirements:
#
# - ActionMailer 3.0 -- http://www.rubyonrails.org
# - wkhtmltopdf -- http://code.google.com/p/wkhtmltopdf
# - mechanize -- http://mechanize.rubyforge.org
#
# Usage:
#
# statseeker2pdf --WAN --weekly
# statseeker2pdf --WAN --daily
# statseeker2pdf --internet --weekly
# statseeker2pdf --internet --daily
#
# Use at your own risk. No warranty or guarantee for any purpose.
######################################################################
require 'date'
require 'tempfile'
require 'optparse'
require 'rubygems'
require 'action_mailer'
require 'mime/types'
require 'mechanize'
# ----------------------------------------------------------------------
STATSEEKER="statseeker.example.com"
USER="network"
PASS="network"
FROM="Statseeker <sapeadmin@example.com>"
RECIP="networkers@example.com"
# The statseeker groups to use for the "Internet" and "WAN" reports
INET="--All-Internet"
WAN="--All-WAN"
REPORTING_TOOL_ID = 3
DATESTAMP=Date.today.strftime
TIMEZONE="America/New_York"
# ----------------------------------------------------------------------
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: reporter.rb [options]"
opts.on("-d", "--daily", "Daily Report") do |d|
options[:time_title] = "Daily"
options[:orientation] = "Portrait"
options[:past] = "12h"
#options[:past] = 0 # uncomment this for a normal 24 hour day
options[:interval] = 300
end
opts.on("-w", "--weekly", "Weekly Report") do |d|
options[:time_title] = "Weekly"
options[:orientation] = "Landscape"
options[:past] = "6d"
options[:interval] = 600
end
opts.on("-W", "--WAN", "WAN Report") do |d|
options[:type] = WAN
options[:type_title] = "WAN"
end
opts.on("-I", "--internet", "Internet Report") do |d|
options[:type] = INET
options[:type_title] = "Internet"
end
end.parse!
title = "#{options[:time_title]} #{options[:type_title]} Utilization for #{DATESTAMP}"
a = Mechanize.new
a.auth( USER, PASS )
def find_group_id( mech, regexp )
mech.get("http://#{STATSEEKER}") do |page|
page.parser.xpath('//select[@name="group"]/option').each do |s|
if s.children[0].text =~ regexp
return s.attributes["value"].to_s
end
end
end
end
def find_interface_ids( mech, gid )
out = []
mech.get("http://#{STATSEEKER}/cgi/nimc02?report=#{REPORTING_TOOL_ID}&group=#{gid}") do |page|
page.parser.xpath('//select[@name="list"]/option').each do |o|
out << o.attributes["value"].to_s
end
end
return out
end
gid = find_group_id( a, /#{options[:type]}\n/ )
ints = find_interface_ids( a, gid )
# The base URL for the report
url = "http://#{STATSEEKER}/cgi/nim-report?rid=56861&command=Graph&mode=if"
# The list of interfaces to report on
url += "&" + ints.map { |i| "list=#{i}"}.join("&")
# Time options
url += "&" +
"tfc_fav=range+%3D+start_of_today+-+#{options[:past]}+to+now;&" +
"tz=#{TIMEZONE}&" +
"tfc=range+%3D+start_of_today+-+#{options[:past]}+to+now;"
# Graph format options
url += "&" +
"rtype=BitsPerSecond&graph_type=Line&db_type=Average&x_step=1&" +
"interval=#{options[:interval]}&" +
"y_height=100&y_gridlines=5&y_max=&y_max_power=1&x_gridlines=on&legend=on"
# Unused options
url += "&" +
"year=&month=&day=&hour=&minute=&duration=&wday_from=&wday_to=&time_from=&time_to="
tmp = Tempfile.new("statseeker")
system "/usr/local/bin/wkhtmltopdf --quiet \
--username #{USER} --password #{PASS} \
--title '#{title}' \
--orientation #{options[:orientation]} \
'#{url}' #{tmp.path}"
class ReportMailer < ActionMailer::Base
default :from => FROM
def report_email(path, title, type, period, datestamp)
attachments["#{type}-#{period}-#{datestamp}.pdf"] = File.read(path)
mail(:to => RECIP,
:subject => title) do |format|
format.text { render :text => "See attached report" }
format.html { render :text => "<p>See attached report" }
end
end
end
ReportMailer.report_email(tmp.path,
title,
options[:type_title],
options[:time_title],
DATESTAMP).deliver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment