forked from 100daysofdevops/100daysofdevops
-
Notifications
You must be signed in to change notification settings - Fork 27
/
apache_log_parser.py
36 lines (32 loc) · 900 Bytes
/
apache_log_parser.py
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
#!/usr/bin/env python
"""
USAGE:
logparsing_apache.py apache_log_file
This script takes apache log file as an argument and then generates a report, with hostname,
bytes transferred and status
"""
import sys
def apache_output(line):
split_line = line.split()
return {'remote_host': split_line[0],
'apache_status': split_line[8],
'data_transfer': split_line[9],
}
def final_report(logfile):
for line in logfile:
line_dict = apache_output(line)
print(line_dict)
if __name__ == "__main__":
if not len(sys.argv) > 1:
print (__doc__)
sys.exit(1)
infile_name = sys.argv[1]
try:
infile = open(infile_name, 'r')
except IOError:
print ("You must specify a valid file to parse")
print (__doc__)
sys.exit(1)
log_report = final_report(infile)
print (log_report)
infile.close()