improve analyzer to allow you to specify a limit in minutes
This commit is contained in:
parent
d6d9a7fa09
commit
c8ec1ad2be
|
@ -15,6 +15,7 @@ class LogAnalyzer
|
||||||
TIME_FORMAT = "%d/%b/%Y:%H:%M:%S %Z"
|
TIME_FORMAT = "%d/%b/%Y:%H:%M:%S %Z"
|
||||||
|
|
||||||
def self.parse(line)
|
def self.parse(line)
|
||||||
|
|
||||||
result = new
|
result = new
|
||||||
_, result.time, result.ip_address, result.url, result.user_agent,
|
_, result.time, result.ip_address, result.url, result.user_agent,
|
||||||
result.route, result.status, result.bytes_sent, result.referer,
|
result.route, result.status, result.bytes_sent, result.referer,
|
||||||
|
@ -40,17 +41,17 @@ class LogAnalyzer
|
||||||
end
|
end
|
||||||
|
|
||||||
def parsed_time
|
def parsed_time
|
||||||
DateTime.strptime(time, TIME_FORMAT)
|
DateTime.strptime(time, TIME_FORMAT) if time
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :total_requests, :message_bus_requests, :filename,
|
attr_reader :total_requests, :message_bus_requests, :filenames,
|
||||||
:ip_to_rails_duration, :username_to_rails_duration,
|
:ip_to_rails_duration, :username_to_rails_duration,
|
||||||
:route_to_rails_duration, :url_to_rails_duration,
|
:route_to_rails_duration, :url_to_rails_duration,
|
||||||
:status_404_to_count, :from_time, :to_time
|
:status_404_to_count, :from_time, :to_time
|
||||||
|
|
||||||
def self.analyze(filename)
|
def self.analyze(filenames, args)
|
||||||
new(filename).analyze
|
new(filenames, args).analyze
|
||||||
end
|
end
|
||||||
|
|
||||||
class Aggeregator
|
class Aggeregator
|
||||||
|
@ -97,8 +98,8 @@ class LogAnalyzer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(filename)
|
def initialize(filenames, args={})
|
||||||
@filename = filename
|
@filenames = filenames
|
||||||
@ip_to_rails_duration = Aggeregator.new
|
@ip_to_rails_duration = Aggeregator.new
|
||||||
@username_to_rails_duration = Aggeregator.new
|
@username_to_rails_duration = Aggeregator.new
|
||||||
|
|
||||||
|
@ -107,41 +108,59 @@ class LogAnalyzer
|
||||||
|
|
||||||
@url_to_rails_duration = Aggeregator.new
|
@url_to_rails_duration = Aggeregator.new
|
||||||
@status_404_to_count = Aggeregator.new
|
@status_404_to_count = Aggeregator.new
|
||||||
|
|
||||||
|
@total_requests = 0
|
||||||
|
@message_bus_requests = 0
|
||||||
|
@limit = args[:limit]
|
||||||
end
|
end
|
||||||
|
|
||||||
def analyze
|
def analyze
|
||||||
@total_requests = 0
|
now = DateTime.now
|
||||||
@message_bus_requests = 0
|
|
||||||
File.open(@filename).each_line do |line|
|
|
||||||
@total_requests += 1
|
|
||||||
parsed = LineParser.parse(line)
|
|
||||||
|
|
||||||
@from_time ||= parsed.time
|
@filenames.each do |filename|
|
||||||
@to_time = parsed.time
|
File.open(filename).each_line do |line|
|
||||||
|
@total_requests += 1
|
||||||
|
parsed = LineParser.parse(line)
|
||||||
|
|
||||||
if parsed.url =~ /(POST|GET) \/message-bus/
|
next unless parsed.time
|
||||||
@message_bus_requests += 1
|
next if @limit && ((now - parsed.parsed_time) * 24 * 60).to_i > @limit
|
||||||
next
|
|
||||||
|
@from_time ||= parsed.time
|
||||||
|
@to_time = parsed.time
|
||||||
|
|
||||||
|
if parsed.url =~ /(POST|GET) \/message-bus/
|
||||||
|
@message_bus_requests += 1
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
@ip_to_rails_duration.add(parsed.ip_address, parsed.rails_duration)
|
||||||
|
|
||||||
|
username = parsed.username == "-" ? "[Anonymous]" : parsed.username
|
||||||
|
@username_to_rails_duration.add(username, parsed.rails_duration, parsed.route)
|
||||||
|
|
||||||
|
@route_to_rails_duration.add(parsed.route, parsed.rails_duration, parsed.is_mobile? ? "mobile" : "desktop")
|
||||||
|
|
||||||
|
@url_to_rails_duration.add(parsed.url, parsed.rails_duration)
|
||||||
|
|
||||||
|
@status_404_to_count.add(parsed.url,1) if parsed.status == "404"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ip_to_rails_duration.add(parsed.ip_address, parsed.rails_duration)
|
|
||||||
|
|
||||||
username = parsed.username == "-" ? "[Anonymous]" : parsed.username
|
|
||||||
@username_to_rails_duration.add(username, parsed.rails_duration, parsed.route)
|
|
||||||
|
|
||||||
@route_to_rails_duration.add(parsed.route, parsed.rails_duration, parsed.is_mobile? ? "mobile" : "desktop")
|
|
||||||
|
|
||||||
@url_to_rails_duration.add(parsed.url, parsed.rails_duration)
|
|
||||||
|
|
||||||
@status_404_to_count.add(parsed.url,1) if parsed.status == "404"
|
|
||||||
end
|
end
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
filename = ARGV[0] || "/var/log/nginx/access.log"
|
args = ARGV.dup
|
||||||
analyzer = LogAnalyzer.analyze(filename)
|
|
||||||
|
if args[0] == "--limit"
|
||||||
|
args.shift
|
||||||
|
limit = args.shift.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
filenames = args if args[0]
|
||||||
|
filenames ||= ["/var/log/nginx/access.log", "/var/log/nginx/access.log.1"]
|
||||||
|
|
||||||
|
analyzer = LogAnalyzer.analyze(filenames, limit: limit)
|
||||||
|
|
||||||
SPACER = "-" * 100
|
SPACER = "-" * 100
|
||||||
|
|
||||||
|
@ -230,7 +249,10 @@ class Column < String
|
||||||
end
|
end
|
||||||
|
|
||||||
puts
|
puts
|
||||||
puts "Analyzed: #{analyzer.filename}"
|
puts "Analyzed: #{analyzer.filenames.join(",")} on #{`hostname`}"
|
||||||
|
if limit
|
||||||
|
puts "Limited to #{DateTime.now - (limit.to_f / (60*24.0))} - #{DateTime.now}"
|
||||||
|
end
|
||||||
puts SPACER
|
puts SPACER
|
||||||
puts "#{analyzer.from_time} - #{analyzer.to_time}"
|
puts "#{analyzer.from_time} - #{analyzer.to_time}"
|
||||||
puts SPACER
|
puts SPACER
|
||||||
|
|
Loading…
Reference in New Issue