Created
January 11, 2011 16:58
-
-
Save IskanderHaziev/774711 to your computer and use it in GitHub Desktop.
parsing cloudfront logs
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
require 'rubygems' | |
require 'zlib' | |
require 'aws/s3' | |
def parse_cloudfront_line(line) | |
return if line.strip.match(/^#/) | |
bytes = line.split("\t")[5] | |
filename = line.split("\t")[13] | |
info = {:file => filename, :bytes => bytes} | |
info if filename.strip != '-' | |
rescue | |
nil | |
end | |
def parse_cloudfront_logs | |
object = AWS::S3::Bucket.objects('logs').first | |
usage_details = {} | |
open(object.url) do |f| | |
gz = Zlib::GzipReader.new(f) | |
gz.each_line do |line| | |
usage = parse_cloudfront_line(line) | |
next if usage.nil? | |
if usage_details[usage[:file]] | |
usage_details[usage[:file]] += usage[:bytes].to_i | |
else | |
usage_details[usage[:file]] = usage[:bytes].to_i | |
end | |
end | |
end | |
puts usage_details.inspect | |
end | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => '', | |
:secret_access_key => '' | |
) | |
parse_cloudfront_logs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment