Class: RequestLogAnalyzer::Controller
- Inherits:
-
Object
- Object
- RequestLogAnalyzer::Controller
- Defined in:
- lib/request_log_analyzer/controller.rb
Overview
The RequestLogAnalyzer::Controller class creates a LogParser instance for the requested file format and connect it with sources and aggregators.
Sources are streams or files from which the requests will be parsed. Aggregators will handle every passed request to yield a meaningfull results.
-
Use the build -function to build a new controller instance.
-
Use the run! method to start the parser and send the requests to the aggregators.
Note that the order of sources can be imported if you have log files than succeed eachother. Requests that span over succeeding files will be parsed correctly if the sources are registered in the correct order. This can be helpful to parse requests from several logrotated log files.
Instance Attribute Summary collapse
-
#aggregators ⇒ Object
readonly
Returns the value of attribute aggregators.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
-
.build(options) ⇒ Object
Build a new controller.
-
.build_from_arguments(arguments) ⇒ Object
Builds a RequestLogAnalyzer::Controller given parsed command line arguments <tt>arguments<tt> A CommandLine::Arguments hash containing parsed commandline parameters.
Instance Method Summary collapse
-
#add_aggregator(agg) ⇒ Object
(also: #>>)
Adds an aggregator to the controller.
-
#add_filter(filter, filter_options = {}) ⇒ Object
Adds a request filter to the controller.
-
#aggregate_request(request) ⇒ Object
Push a request to all the aggregators (@aggregators).
-
#filter_request(request) ⇒ Object
Push a request through the entire filterchain (@filters).
-
#handle_progress(message, value = nil) ⇒ Object
Progress function.
-
#handle_source_change(change, filename) ⇒ Object
Source change handler.
-
#initialize(source, options = {}) ⇒ Controller
constructor
Builds a new Controller for the given log file format.
- #install_signal_handlers ⇒ Object
-
#run! ⇒ Object
Runs RequestLogAnalyzer 1.
Constructor Details
#initialize(source, options = {}) ⇒ Controller
Builds a new Controller for the given log file format. format Logfile format. Defaults to :rails Options are passd on to the LogParser.
-
:databaseDatabase the controller should use. -
:yamlYaml Dump the contrller should use. -
:outputAll report outputs get << through this output. -
:no_progressNo progress bar -
:silentMinimal output, only error
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/request_log_analyzer/controller.rb', line 243 def initialize(source, = {}) @source = source = @aggregators = [] @filters = [] @output = [:output] @interrupted = false # Register the request format for this session after checking its validity fail 'Invalid file format!' unless @source.file_format.valid? # Install event handlers for wrnings, progress updates and source changes @source.warning = lambda { |type, , lineno| @aggregators.each { |agg| agg.warning(type, , lineno) } } @source.progress = lambda { |, value| handle_progress(, value) } unless [:no_progress] @source.source_changes = lambda { |change, filename| handle_source_change(change, filename) } end |
Instance Attribute Details
#aggregators ⇒ Object (readonly)
Returns the value of attribute aggregators.
16 17 18 |
# File 'lib/request_log_analyzer/controller.rb', line 16 def aggregators @aggregators end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
16 17 18 |
# File 'lib/request_log_analyzer/controller.rb', line 16 def filters @filters end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/request_log_analyzer/controller.rb', line 16 def end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
16 17 18 |
# File 'lib/request_log_analyzer/controller.rb', line 16 def output @output end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
16 17 18 |
# File 'lib/request_log_analyzer/controller.rb', line 16 def source @source end |
Class Method Details
.build(options) ⇒ Object
Build a new controller. Returns a new RequestLogAnalyzer::Controller object.
Options
-
:afterDrop all requests after this date (Date, DateTime, Time, or a String in “YYYY-MM-DD hh:mm:ss” format) -
:aggregatorArray of aggregators (Strings or Symbols for the builtin aggregators or a RequestLogAnalyzer::Aggregator class - Defaults to [:summarizer]). -
:boringDo not show color on STDOUT (Defaults to false). -
:beforeDrop all requests before this date (Date, DateTime, Time or a String in “YYYY-MM-DD hh:mm:ss” format) -
:databaseDatabase file to insert encountered requests to. -
:debugEnables echo aggregator which will echo each request analyzed. -
:fileFilestring, File or StringIO. -
:format:rails, => ‘FORMATSTRING’, :merb, :amazon_s3, :mysql or RequestLogAnalyzer::FileFormat class. (Defaults to :rails). -
:mailEmail the results to this email address. -
:mailhostEmail the results to this mail server. -
:mailfromSet the Email sender address. -
:mailfrom_aliasSet the Email sender name. -
:mailsubjectEmail subject. -
:no_progressDo not display the progress bar (increases parsing speed). -
:output‘FixedWidth’, ‘HTML’ or RequestLogAnalyzer::Output class. Defaults to ‘FixedWidth’. -
:rejectReject specific => :value combination (expects a single hash). -
:report_widthWidth of reports in characters for FixedWidth reports. (Defaults to 80) -
:reset_databaseReset the database before starting. -
:selectSelect specific => :value combination (expects a single hash). -
:source_filesSource files to analyze. Provide either File, array of files or STDIN. -
:yamlOutput to YAML file. -
:silentMinimal output automatically implies :no_progress -
:sourceThe class to instantiate to grab the requestes, must be a RequestLogAnalyzer::Source::Base descendant. (Defaults to RequestLogAnalyzer::Source::LogParser)
Example
RequestLogAnalyzer::Controller.build(
:output => :HTML,
:mail => 'root@localhost',
:after => Time.now - 24*60*60,
:source_files => '/var/log/passenger.log'
).run!
Todo
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/request_log_analyzer/controller.rb', line 133 def self.build() # Defaults [:output] ||= 'FixedWidth' [:format] ||= :rails [:aggregator] ||= [:summarizer] [:report_width] ||= 80 [:report_amount] ||= 20 [:report_sort] ||= 'sum,mean' [:boring] ||= false [:silent] ||= false [:source] ||= RequestLogAnalyzer::Source::LogParser [:no_progress] = true if [:silent] # Deprecation warnings if [:dump] warn '[DEPRECATION] `:dump` is deprecated. Please use `:yaml` instead.' [:yaml] = [:dump] end # Set the output class output_args = {} output_object = nil if [:output].is_a?(Class) output_class = [:output] else output_class = RequestLogAnalyzer::Output.const_get([:output]) end output_sort = [:report_sort].split(',').map { |s| s.to_sym } output_amount = [:report_amount] == 'all' ? :all : [:report_amount].to_i if [:file] output_object = %w( File StringIO ).include?([:file].class.name) ? [:file] : File.new([:file], 'w+') output_args = { width: 80, color: false, characters: :ascii, sort: output_sort, amount: output_amount } elsif [:mail] output_object = RequestLogAnalyzer::Mailer.new([:mail], [:mailhost], subject: [:mailsubject], from: [:mailfrom], from_alias: [:mailfrom_name]) output_args = { width: 80, color: false, characters: :ascii, sort: output_sort, amount: output_amount } else output_object = STDOUT output_args = { width: [:report_width].to_i, color: ![:boring], characters: ([:boring] ? :ascii : :utf), sort: output_sort, amount: output_amount } end output_instance = output_class.new(output_object, output_args) # Create the controller with the correct file format if [:format].is_a?(Hash) file_format = RequestLogAnalyzer::FileFormat.load([:format].keys[0], [:format].values[0]) else file_format = RequestLogAnalyzer::FileFormat.load([:format]) end # Kickstart the controller controller = Controller.new([:source].new(file_format, source_files: [:source_files], parse_strategy: [:parse_strategy]), output: output_instance, database: [:database], # FUGLY! yaml: [:yaml], reset_database: [:reset_database], no_progress: [:no_progress], silent: [:silent] ) # register filters if [:after] || [:before] = {} [:after, :before].each do |filter| case [filter] when Date, DateTime, Time [filter] = [filter] when String [filter] = DateTime.parse([filter]) end end controller.add_filter(:timespan, ) end if [:reject] [:reject].each do |(field, value)| controller.add_filter(:field, mode: :reject, field: field, value: value) end end if [:select] [:select].each do |(field, value)| controller.add_filter(:field, mode: :select, field: field, value: value) end end # register aggregators [:aggregator].each { |agg| controller.add_aggregator(agg) } controller.add_aggregator(:summarizer) if [:aggregator].empty? controller.add_aggregator(:echo) if [:debug] controller.add_aggregator(:database_inserter) if [:database] && ![:aggregator].include?('database') file_format.setup_environment(controller) controller end |
.build_from_arguments(arguments) ⇒ Object
Builds a RequestLogAnalyzer::Controller given parsed command line arguments <tt>arguments<tt> A CommandLine::Arguments hash containing parsed commandline parameters.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/request_log_analyzer/controller.rb', line 20 def self.build_from_arguments(arguments) = {} # Copy fields [:database] = arguments[:database] [:reset_database] = arguments[:reset_database] [:debug] = arguments[:debug] [:yaml] = arguments[:yaml] || arguments[:dump] [:mail] = arguments[:mail] [:no_progress] = arguments[:no_progress] [:format] = arguments[:format] [:output] = arguments[:output] [:file] = arguments[:file] [:after] = arguments[:after] [:before] = arguments[:before] [:reject] = arguments[:reject] [:select] = arguments[:select] [:boring] = arguments[:boring] [:aggregator] = arguments[:aggregator] [:report_width] = arguments[:report_width] [:report_sort] = arguments[:report_sort] [:report_amount] = arguments[:report_amount] [:mailhost] = arguments[:mailhost] [:mailfrom] = arguments[:mailfrom] [:mailfrom_name] = arguments[:mailfrom_name] [:mailsubject] = arguments[:mailsubject] [:silent] = arguments[:silent] [:parse_strategy] = arguments[:parse_strategy] # Apache format workaround if arguments[:rails_format] [:format] = { rails: arguments[:rails_format] } elsif arguments[:apache_format] [:format] = { apache: arguments[:apache_format] } end # Handle output format casing if [:output].class == String [:output] = 'HTML' if [:output] =~ /^html$/i [:output] = 'FixedWidth' if [:output] =~ /^fixed_?width$/i end # Register sources if arguments.parameters.length == 1 file = arguments.parameters[0] if file == '-' || file == 'STDIN' .store(:source_files, $stdin) elsif File.exist?(file) .store(:source_files, file) else puts "File not found: #{file}" exit(0) end else .store(:source_files, arguments.parameters) end # Guess file format if ![:format] && [:source_files] [:format] = :rails3 # Default if [:source_files] != $stdin if [:source_files].class == String [:format] = RequestLogAnalyzer::FileFormat.autodetect([:source_files]) elsif [:source_files].class == Array && [:source_files].first != $stdin [:format] = RequestLogAnalyzer::FileFormat.autodetect([:source_files].first) end end end build() end |
Instance Method Details
#add_aggregator(agg) ⇒ Object Also known as: >>
Adds an aggregator to the controller. The aggregator will be called for every request that is parsed from the provided sources (see add_source)
288 289 290 291 |
# File 'lib/request_log_analyzer/controller.rb', line 288 def add_aggregator(agg) agg = RequestLogAnalyzer::Aggregator.const_get(RequestLogAnalyzer.to_camelcase(agg)) if agg.is_a?(String) || agg.is_a?(Symbol) @aggregators << agg.new(@source, ) end |
#add_filter(filter, filter_options = {}) ⇒ Object
Adds a request filter to the controller.
296 297 298 299 |
# File 'lib/request_log_analyzer/controller.rb', line 296 def add_filter(filter, = {}) filter = RequestLogAnalyzer::Filter.const_get(RequestLogAnalyzer.to_camelcase(filter)) if filter.is_a?(Symbol) @filters << filter.new(source.file_format, .merge()) end |
#aggregate_request(request) ⇒ Object
Push a request to all the aggregators (@aggregators). request The request to push to the aggregators.
314 315 316 317 318 |
# File 'lib/request_log_analyzer/controller.rb', line 314 def aggregate_request(request) return false unless request @aggregators.each { |agg| agg.aggregate(request) } true end |
#filter_request(request) ⇒ Object
Push a request through the entire filterchain (@filters). request The request to filter. Returns the filtered request or nil.
304 305 306 307 308 309 310 |
# File 'lib/request_log_analyzer/controller.rb', line 304 def filter_request(request) @filters.each do |filter| request = filter.filter(request) return nil if request.nil? end request end |
#handle_progress(message, value = nil) ⇒ Object
Progress function. Expects :started with file, :progress with current line and :finished or :interrupted when done. message Current state (:started, :finished, :interupted or :progress). value File or current line.
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/request_log_analyzer/controller.rb', line 264 def handle_progress(, value = nil) case when :started = CommandLine::ProgressBar.new(File.basename(value), File.size(value), STDERR) when :finished .finish = nil when :interrupted if .halt = nil end when :progress .set(value) end end |
#handle_source_change(change, filename) ⇒ Object
Source change handler
282 283 284 |
# File 'lib/request_log_analyzer/controller.rb', line 282 def handle_source_change(change, filename) @aggregators.each { |agg| agg.source_change(change, File.(filename, Dir.pwd)) } end |
#install_signal_handlers ⇒ Object
361 362 363 364 365 366 367 |
# File 'lib/request_log_analyzer/controller.rb', line 361 def install_signal_handlers Signal.trap('INT') do handle_progress(:interrupted) puts 'Caught interrupt! Stopping parsing...' @interrupted = true end end |
#run! ⇒ Object
Runs RequestLogAnalyzer
-
Call prepare on every aggregator
-
Generate requests from source object
-
Filter out unwanted requests
-
Call aggregate for remaning requests on every aggregator
-
Call finalize on every aggregator
-
Call report on every aggregator
-
Finalize Source
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/request_log_analyzer/controller.rb', line 328 def run! # @aggregators.each{|agg| p agg} @aggregators.each { |agg| agg.prepare } install_signal_handlers @source.each_request do |request| break if @interrupted aggregate_request(filter_request(request)) end @aggregators.each { |agg| agg.finalize } @output.header @aggregators.each { |agg| agg.report(@output) } @output. @source.finalize if @output.io.is_a?(File) unless [:silent] puts puts 'Report written to: ' + File.(@output.io.path) puts 'Need an expert to analyze your application?' puts 'Mail to [email protected] or visit us at http://railsdoctors.com' puts 'Thanks for using request-log-analyzer!' end @output.io.close elsif @output.io.is_a?(RequestLogAnalyzer::Mailer) @output.io.mail end end |