-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapache_log_report.rb
56 lines (44 loc) · 1.09 KB
/
apache_log_report.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
#!/usr/bin/ruby
#Apache log
#Jackson Hall
#CIT 383-001 & 002
#03/13/2018
header = <<-HEAD
-----------------------------------------------------
Statistics for the Apache log file access_log
-----------------------------------------------------
HEAD
puts header
line_array = File.readlines("access_log")
#defines the hashes
ip_hash = Hash.new(0)
url_hash = Hash.new(0)
status_hash = Hash.new(0)
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
#sorts each hash with key and frequency of each key
#IP Hash
puts "Frequency of Client IP Addresses:"
ip_hash.each do |k, v|
puts k.to_s.ljust(20) + " " + ("*" * v.to_i)
end
puts ""
#URL Hash
puts "Frequency of URLs Accessed:"
url_hash.each do |k, v|
printf k.to_s.ljust(50) + " " + v.to_s + "\n"
end
puts ""
#Status Hash
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