-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapache_log_report_2.rb
66 lines (50 loc) · 1.21 KB
/
apache_log_report_2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/ruby
#Apache log
#Jackson Hall
#CIT 383-001 & 002
#04/3/2018
header = <<-HEAD
-----------------------------------------------------
Statistics for the Apache log file access_log
-----------------------------------------------------
HEAD
if ARGV.empty?
raise ArgumentError, 'A file name must be provided'
exit
end
#defines the hashes
ip_hash = Hash.new(0)
url_hash = Hash.new(0)
status_hash = Hash.new(0)
begin
line_array = File.readlines(ARGV[0])
rescue SystemCallError, ArgumentError => e
puts ("Exception type: #{e.class}")
puts ("Message: #{e.message}")
exit
end
puts header
line_array.each do |line|
md = line.match(/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP.{6}(\d{3})/)
ip = md[1]
ip_hash[ip] += 1
url = md[2]
url_hash[url] += 1
status = md[3]
status_hash[status] += 1
end
puts "Frequency of Client IP Addresses:"
ip_hash.each do |k, v|
puts k.to_s.ljust(20) + " " + ("*" * v.to_i)
end
puts ""
puts "Frequency of URLs Accessed:"
url_hash.each do |k, v|
printf k.to_s.ljust(50) + " " + v.to_s + "\n"
end
puts ""
puts "HTTP Status Codes Summary:"
status_hash.each do |k, v|
v = (v.to_f / line_array.size) * 100
puts "#{k}" + ":" + " " + "#{v.round(2)}" + "%"
end