header

Torsten Curdt’s weblog

Requests per Second from Log

While this is not nearly as fancy as Theo’s realtime version with DTrace this little script extracts your requests per second (and minutes) from Apache log files. Useful if you didn’t have monitoring set up already and want to know about the amount of request you were getting - in retrospect. As you most likely will have a log file it’s easy to extract the data from there. I’ve just use the log file parser from my other post and added some grouping and counting.


parser = LogParser.new

current_key = 0
count = 0

while STDIN.gets
  line = $_

  parsed_data = parser.parse_line(line)  

  parsed_data[:datetime] =~ %r{(\d{2})/(\w{3})/(\d{4}):(\d{2}):(\d{2})}
  day, month, year, hour, minute = $1, $2, $3, $4, $5

  key = Time.mktime(year, month, day, hour, minute, 0).to_i

  if key != current_key

    if count > 0
        # timestamp, request/minute, requests/second
        printf "%d %d %d\n", key, count, count/60
    end

    count = 0
    current_key = key
  end

  count = count + 1
end

Now just pipe in the log data and you get a nice aggregation that can easily be injected into a round robin database for example.

$ cat access.log | ./request.rb

Leave a Reply

Please copy the string hfrC20 to the field below: