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
  • @Matthew Please see the my other blog post. The link is in the text.
  • Matthew Herzog
    Do I need a shebang line for this?

    ./parser.rb:3: uninitialized constant LogParser (NameError)

    Same error on linux and leopard.
  • fana
    Hi, I just want to thank you for saving me time.
    I needed a script like yours quickly and didn't have enough time to code it myself...
blog comments powered by Disqus