Skip to content

Instantly share code, notes, and snippets.

@iulianu
Created March 24, 2015 08:39
Show Gist options
  • Save iulianu/91e2ba89b5ea49891ba0 to your computer and use it in GitHub Desktop.
Save iulianu/91e2ba89b5ea49891ba0 to your computer and use it in GitHub Desktop.
require 'zlib'
APACHE_LINE_RX = /^([0-9\.]+)\s+\S+\s+\S+\s+\[[^\]]+\]\s+\"[^\"]+\"\s+(\d+)\s+(\d+).*$/
def parse_apache_line(text_line)
if text_line =~ APACHE_LINE_RX
{:ip => $1, :bytes => $3.to_i}
else
nil
end
end
# Return value is a pair, key-value
def apache_bandwidth_map(text_line)
fields = parse_apache_line(text_line)
if fields.nil?
nil
else
{fields[:ip] => fields[:bytes]}
end
end
def apache_bandwidth_reduce(hash, pair)
key = pair.keys.first
old_val = hash[key] || 0
hash[key] = old_val + pair[key]
hash
end
def bandwidth_map(file_name_template)
pairs = []
Dir.glob(file_name_template) do |fname|
Zlib::GzipReader.open(fname) do |zstream|
zstream.each_line do |text_line|
pair = apache_bandwidth_map(text_line)
pairs << pair unless pair.nil?
end
end
end
reduced_hash = pairs.reduce({}) { |acc, pair| apache_bandwidth_reduce(acc, pair) }
end
## Beginning of main program
ip_to_query = ARGV.first
if ip_to_query.nil?
STDERR.puts("An IP address is needed as the first argument")
exit 1
end
bwm = bandwidth_map("*-access.log.gz")
puts bwm[ip_to_query] || "IP not found"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment