webserver-log-analyser/main.rb

142 lines
3.2 KiB
Ruby

# frozen_string_literal: true
require 'date'
require_relative 'utils/http_codes'
require_relative 'utils/date_for_humans'
args = ARGV
if args[0] == '--parse' || args[0] == '-p'
puts("[!] Opening log file at #{args[1]}")
begin
logfile = File.open(args[1])
rescue Errno::ENOENT
warn "[x] The file #{args[1]} was not found."
exit 1
end
else
puts('[!] Log file path not provided, assuming default access.log')
begin
logfile = File.open('access.log')
rescue Errno::ENOENT
warn '[x] Default file access.log not found. Specify an alternative log file to parse with -p or --parse.'
exit 1
end
end
data = logfile.read
lines = data.split("\n")
# Gets the IP address from the string
# Note: sometimes the function can fetch the UA version as an IP address
def get_line_ip(line)
pattern = /\b(?:\d{1,3}\.){3}\d{1,3}\b/
line.match(pattern)[0] if line.match(pattern)
end
# TODO: Implement visits per month using this method
# Returns the date in the current line
def get_line_date(line)
pattern = /\[(.*?)\]/
match = line.match(pattern)[0] if line.match(pattern)
# [12/Apr/2023:13:56:41 +0100] -> 12/Apr/2023:13:56:41 +0100
Date.parse(match.gsub('[', '').gsub(']', ''))
end
# Gets the HTTP status code of the given log line
def get_line_code(line)
pattern = /\s(\d{3})\s/
line.match(pattern)[1] if line.match(pattern)
end
# Gets the user agent of the given log line
def get_line_browser(line)
pattern = /"([^"]*)"$/
user_agent = line.match(pattern)[0] if line.match(pattern)
user_agent.gsub('"', '')
end
# Gets the number of times an IP contacted the site
def times_appeared_single(ips, ip_to_check)
counter = 0
ips.each do |ip|
counter += 1 if ip == ip_to_check
end
counter
end
# Returns all unique IPs in a given list of IP addresses with duplicates
def sort_unique_ip(ips)
seen_ips = {}
unique_ips = []
ips.each do |ip|
unless seen_ips[ip]
unique_ips << ip
seen_ips[ip] = true
end
end
unique_ips
end
visit_counter = {}
client_errors = {}
browsers = {}
all_ips = []
lines.each do |line|
all_ips << get_line_ip(line)
end
unique_ips = sort_unique_ip(all_ips)
puts("There were a total of #{unique_ips.length} unique IPs who connected to our site.")
unique_ips.each do |ip|
visit_counter[ip] = times_appeared_single(all_ips, ip)
puts("IP #{ip} contacted our site #{times_appeared_single(all_ips, ip)} times.")
end
lines.each do |line|
ip = get_line_ip(line).to_s
code = get_line_code(line)
browser = get_line_browser(line)
# refactored from "ua" to "browser"
if is_client_err?(code.to_i)
if client_errors[ip]
client_errors[ip] += 1
else
client_errors[ip] = 1
end
end
# refactored from: "user_agents[ua]" to "browsers[browser]"
if browsers[browser]
browsers[browser] += 1
else
browsers[browser] = 1
end
end
top_browsers = browsers.sort_by { |_ua, count| -count }.first(5)
puts 'Top 5 browsers contacting the site:'
top_browsers.each do |ua, count|
puts "#{ua}: #{count} visits"
end
top_client_errors = client_errors.sort_by { |_ip, count| -count }.first(5)
puts 'Top 5 IPs with most client errors (400-499):'
top_client_errors.each do |ip, count|
puts "#{ip}: #{count} errors"
end