#!/usr/bin/env ruby # encoding: utf-8 $:.unshift(File.expand_path('../lib', File.dirname(__FILE__))) require 'request_log_analyzer' require 'cli/command_line_arguments' require 'cli/progressbar' require 'cli/tools' # Parse the arguments given via commandline begin arguments = CommandLine::Arguments.parse do |command_line| command_line.command(:install) do |install| install.parameters = 1 end command_line.command(:console) do |cons| cons.option(:database, :alias => :d, :required => true) cons.option(:format, :alias => :f) cons.option(:apache_format) end command_line.command(:strip) do |strip| strip.minimum_parameters = 1 strip.option(:format, :alias => :f, :default => 'rails3') strip.option(:output, :alias => :o) strip.switch(:discard_teaser_lines, :t) strip.switch(:keep_junk_lines, :j) end command_line.option(:format, :alias => :f) command_line.option(:apache_format) command_line.option(:rails_format) command_line.option(:file, :alias => :e) command_line.option(:mail, :alias => :m) command_line.option(:mailhost, :default => 'localhost') command_line.option(:mailsubject) command_line.option(:parse_strategy, :default => 'assume-correct') command_line.option(:yaml) command_line.option(:dump) # To be deprecated command_line.option(:aggregator, :alias => :a, :multiple => true) command_line.option(:database, :alias => :d) command_line.switch(:reset_database) # filtering options command_line.option(:select, :multiple => true, :parameters => 2) command_line.option(:reject, :multiple => true, :parameters => 2) command_line.option(:after) command_line.option(:before) command_line.switch(:boring, :b) command_line.option(:output, :alias => :o, :default => 'fixedwidth') command_line.option(:report_width, :default => CommandLine::Tools.terminal_width - 1) command_line.option(:report_amount, :default => 20) command_line.option(:report_sort, :default => 'sum,mean') command_line.switch(:debug) command_line.switch(:no_progress) command_line.switch(:silent) command_line.minimum_parameters = 1 end rescue CommandLine::Error => e puts "Request-log-analyzer, by Willem van Bergen and Bart ten Brinke - version #{RequestLogAnalyzer::VERSION}" puts "Website: http://railsdoctors.com" puts puts "ARGUMENT ERROR: " + e.message if e.message puts puts "Usage: request-log-analyzer [LOGFILES*] " puts puts "Input options:" puts " --after Only consider requests from or later." puts " --before Only consider requests before ." puts " --format , -f: Log file format: amazon_s3, apache, delayed_job, haproxy, merb, mysql," puts " postgresql, rack, rails, rails3, w3c." puts " --reject Only consider requests where does not match ." puts " --select Only consider requests where matches ." puts puts "Output options:" puts " --boring, -b Output reports without ASCII colors." puts " --database , -d: Creates an SQLite3 database of all the parsed request information." puts " --debug Print debug information while parsing." puts " --file Redirect output to file." puts " --mail Send report to an email address." puts " --mailhost Use the given server as the SMTP server for sending email." puts " --mailsubject Overwrite default mailsubject." puts " --no-progress Hide the progress bar." puts " --output Output format. Supports 'html' and 'fixed_width'." puts " --report-width Width of ASCII report. Defaults to terminal width." puts " --report-amount Maximum numer of results per report." puts " --silent Skip the commercials (includes --no-progress)." puts " --yaml Dump the results in YAML format in the given file." puts " --parse-strategy Legal values are 'assume-correct' or 'cautious'." puts puts "Examples:" puts " request-log-analyzer production.log" puts " request-log-analyzer mongrel.0.log mongrel.1.log --output HTML --mail root@localhost" puts " request-log-analyzer --format merb -d requests.db production.log" puts " request-log-analyzer mysql_slow_query.log --reject query /SQL_NO_CACHE/" puts puts "To install rake tasks in your Rails application, " puts "run the following command in your application's root directory:" puts puts " request-log-analyzer install rails" exit(0) end case arguments.command when :install CommandLine::Tools.install_rake_tasks(arguments.parameters[0]) when :console require 'cli/database_console' DatabaseConsole.new(arguments).run! when :strip RequestLogAnalyzer::LogProcessor.build(:strip, arguments).run! else unless arguments[:silent] STDERR.puts "Request-log-analyzer, by Willem van Bergen and Bart ten Brinke - version #{RequestLogAnalyzer::VERSION}" STDERR.puts "Website: http://railsdoctors.com" STDERR.puts end # Run the request_log_analyzer! RequestLogAnalyzer::Controller.build_from_arguments(arguments).run! end