Created
August 3, 2012 22:12
-
-
Save xiongchiamiov/3252053 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
# Show prettier output on interrupt (ctrl+c) | |
trap("INT") do |s| | |
puts | |
exit(s) | |
end | |
ipLimit = 5 | |
urlLimit = 20 | |
OptionParser.new do |opts| | |
opts.banner = "Usage: #{$0.split('/').last} [options] [FILE]" | |
opts.on('-i', '--ip-limit NUM', Integer, "Only show the top NUM IPs. Defaults to #{ipLimit}.") do |num| | |
ipLimit = num | |
end | |
opts.on('-u', '--url-limit NUM', Integer, "Only show URLs hit more than NUM times. Defaults to #{urlLimit}.") do |num| | |
urlLimit = num | |
end | |
opts.on_tail('-h', '--help', 'Show this help dialog.') do | |
puts opts | |
exit 0 | |
end | |
end.parse! | |
file = ARGV[-1] | |
# These awk lines will change a little depending on your log format; here's ours: | |
# LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined | |
lines = `awk '{print $7}' #{file} \ | |
| sort \ | |
| uniq -c \ | |
| grep -v '-' \ | |
| sort -n \ | |
| tail -n #{ipLimit}`.split("\n") | |
lines.each do |line| | |
count, ip = line.split | |
puts "#{count} requests from #{ip}" | |
puts `grep '#{ip}' #{file} \ | |
| sed -r 's/([0-9\.]+), ([0-9\.]+)/\1,\2/' \ | |
| awk '{print $13}' \ | |
| sort \ | |
| uniq -c \ | |
| sort -n \ | |
| awk '{if ($1 > #{urlLimit}) {print}}'` | |
puts "\n" | |
puts '=' * 80 | |
puts "\n" | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2012, iFixit | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
Redistributions in binary form must reproduce the above copyright notice, this | |
list of conditions and the following disclaimer in the documentation and/or | |
other materials provided with the distribution. | |
Neither the name of the iFixit nor the names of its contributors may be used to | |
endorse or promote products derived from this software without specific prior | |
written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment