Skip to content

Instantly share code, notes, and snippets.

@tormaroe
Created September 20, 2011 18:15
Show Gist options
  • Save tormaroe/1229845 to your computer and use it in GitHub Desktop.
Save tormaroe/1229845 to your computer and use it in GitHub Desktop.
A logfile stats aggregator hack
unless ARGV.length == 2
raise <<EOF
Usage: mywebapp_stats.rb folder filepattern
Example: mywebapp_stats.rb . AccountWeb.log*
EOF
end
$user_hash = {}
$page_hash = {}
$report_hash = {}
def main
log_dir = ARGV[0]
files_pattern = ARGV[1]
if File.directory?(log_dir)
Dir.chdir log_dir
else
raise "#{log_dir} is not a directory"
end
Dir.glob(files_pattern).each { |f| parse_file f }
report
end
### ANALYSE FILE STUFF ###############################################
def parse_file filepath
puts "Analysing file #{filepath}"
File.open(filepath, "r") do |f|
while (line = f.gets)
reg_count(/Loading report "(.*)"/, line, $report_hash)
reg_count(/\| (\d+)\/(.*)\/(PROD[123])/, line, $user_hash) do |m|
m[0][1].ljust(20) + m[0][2] + " ID:" + m[0][0]
end
reg_count(/ ([A-Za-z]+)\.aspx \(loading\.\.\)/, line, $page_hash)
end
end
end
def reg_count pattern, line, hash, &block
match = line.scan pattern
if match.length > 0
key = block.call(match) if block
inc_hash! key || match[0][0], hash
end
end
def inc_hash! k, h
n = h[k] || 0
h[k] = n + 1
end
### REPORT OUTPUT STUFF ###############################################
def report
report_hash "Page", "Hits", $page_hash
report_hash "User", "Hits", $user_hash
report_hash "Report", "Hits", $report_hash
end
def report_hash col_a, col_b, h
report_header col_a, col_b
h.
sort{|a,b| a[1] <=> b[1]}.
reverse.
each { |key, value| report_line(key, value) }
end
def report_header col_a, col_b
puts
report_line col_a, col_b
puts "-" * 60
end
def report_line a, b
puts a.ljust(50) + b.to_s.rjust(10)
end
main # run the program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment