Apache Log Quick Summary

Sometimes I set up a quick site for a client and want to summarize hits without setting up awstats. So, I wrote a quick and dirty script to sum up the hits per day in Apache’s access_log file.

Example:

$ cklog access_log

04-15-2007: 21
04-16-2007: 2134
04-17-2007: 304
04-18-2007: 6960
04-19-2007: 951
04-20-2007: 412

Here’s the script:

require 'date'
 
daily = Hash.new
File.open(ARGV[0] || "access_log", "r") do |file|
  while line = file.gets
    if line =~ /(\d{2}\/\w{3}\/\d{4}).*GET\s([^\?\s]+)/
      date = Date.strptime $1, '%d/%b/%Y'
      daily[date] = 0 if daily[date].nil?
      daily[date] + 1
    end
  end
end
 
daily.sort.each {|d, f| 
  puts "#{d.strftime '%m-%d-%Y'}: #{f}"
}

Leave a Reply