-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_log.py
executable file
·41 lines (37 loc) · 994 Bytes
/
parse_log.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
37
38
39
40
41
#!/usr/bin/python
'''
USAGE: parse_log.py filename
'''
import sys
def dictify_logline(line):
'''
returns a dictionary with the relevant pieces of info from the apache log
'''
split_line = line.split()
return {'remote_host':split_line[0],'status':split_line[8],'bytes_sent':split_line[9]}
def generate_log_report(logfile):
''' returns a dictinary of format [remote host] -> bytes sent '''
report_dict = {}
for line in logfile:
line_dict = dictify_logline(line)
#print line_dict
try:
bytes_sent = int(line_dict['bytes_sent'])
except ValueError:
continue
report_dict.setdefault(line_dict['remote_host'],[]).append(bytes_sent)
return report_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 = generate_log_report(infile)
print log_report
infile.close()