Skip to content

Instantly share code, notes, and snippets.

@radamanthus
Created August 1, 2014 04:20
Show Gist options
  • Save radamanthus/12f88d39abc870b83b21 to your computer and use it in GitHub Desktop.
Save radamanthus/12f88d39abc870b83b21 to your computer and use it in GitHub Desktop.
Hourly request counts on an Nginx log file
# Usage: ruby nginx-requests.rb logfilename 31 Jul 2014
logfilename = ARGV[0]
day = ARGV[1]
month = ARGV[2]
year = ARGV[3]
hours = (0..24).to_a
hours.each do |hour|
hour_str = sprintf("%02d", hour)
cmd = %{grep "#{day}/#{month}/#{year}:#{hour_str}:" #{logfilename} | wc -l}
puts cmd
result = `#{cmd}`
puts result
end
@johndalton
Copy link

The output here is different, and this isn't filtering for the day (although you could add that to the grep), but it only takes one pass over the input file:

cat <nginx logfile> | grep -oE '\[.*\]' | cut -c 14-15 | uniq -c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment