Apache Log Quick Summary

Posted by Scott on May 10, 2007

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:
[ruby]
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}”
}
[/ruby]

Leave a Reply

Who Are These Guys?

Netphase, LLC. is a Charlotte-based web application development company specializing in really rapid application development on the Ruby on Rails framework.

Netphase was founded by Scott Nedderman and Chris Beck in 2007. Both Scott and Chris have been active Ruby on Rails developers for more than 3 years, and each has spent more than a decade designing, devloping and deploying web applications.

We have worked on projects of all sizes, and delivered successful sites to production for some of the biggest names in the business. For all of the gory details, check out our about page.

Bookmark This

del.icio.us Digg Furl reddit