Skip to content

Instantly share code, notes, and snippets.

@Davidsoff
Created April 24, 2018 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Davidsoff/f1356570c872f95fb1983de6c505768a to your computer and use it in GitHub Desktop.
Save Davidsoff/f1356570c872f95fb1983de6c505768a to your computer and use it in GitHub Desktop.
cloud_frond request log analyser for IP frequency
class CloudFront < RequestLogAnalyzer::FileFormat::Base
extend RequestLogAnalyzer::FileFormat::CommonRegularExpressions
line_definition :access do |line|
line.header = true
line.footer = true
line.regexp = /^(#{timestamp('%Y-%m-%d %H:%M:%S')})\s(\w+)\s(\d+)\s(#{ip_address})\s(\w+)\s(\S+)\s(\S+)\s(\d+)\s(\S+)\s(\S+)\s(\S+)\s(\S+)\s(\w+)\s(\S+)\s(\S+)\s(\w+)\s(\d+)\s(\S+)\s(#{ip_address}|-)\s+(\S+)\s(\S+)\s(\w+)\s(\S+)/
line.capture(:timestamp).as(:timestamp)
line.capture(:edge_location)
line.capture(:bytes_sent).as(:traffic, unit: :byte)
line.capture(:remote_ip)
end
report do |analyze|
analyze.frequency category: :remote_ip, title: 'Most active clients'
end
class Request < RequestLogAnalyzer::Request
# Do not use DateTime.parse, but parse the timestamp ourselves to return a integer
# to speed up parsing.
def convert_timestamp(value, _definition)
"#{value[0, 4]}#{value[5, 2]}#{value[8, 2]}#{value[11, 2]}#{value[14, 2]}#{value[17, 2]}".to_i
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment