-
Notifications
You must be signed in to change notification settings - Fork 8
/
http_log_parser.py
executable file
·58 lines (49 loc) · 1.9 KB
/
http_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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
import csv
import re
import os
from manager import PluginManager
DIRECTIVE_MAP = {
'%h': 'remote_host',
'%l': 'remote_logname',
'%u': 'remote_user',
'%t': 'time_stamp',
'%r': 'request_line',
'%>s': 'status',
'%b': 'response_size',
'%{Referer}i': 'referer_url',
'%{User-Agent}i': 'user_agent',
}
class LogLineGenerator:
def __init__(self, log_format=None, log_dir='logs'):
# LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
if not log_format:
self.format_string = '%h %l %u %t %r %>s %b %{Referer}i %{User-Agent}i'
else:
self.format_string = log_format
self.log_dir = log_dir
self.re_tsquote = re.compile(r'(\[|\])')
self.field_list = []
for directive in self.format_string.split(' '):
self.field_list.append(DIRECTIVE_MAP[directive])
def _quote_translator(self, file_name):
for line in open(file_name):
yield self.re_tsquote.sub('"', line)
def _file_list(self):
for file in os.listdir(self.log_dir):
file_name = "%s/%s" % (self.log_dir, file)
if os.path.isfile(file_name):
yield file_name
def get_loglines(self):
for file in self._file_list():
reader = csv.DictReader(self._quote_translator(file), fieldnames=self.field_list, delimiter=' ', quotechar='"')
for line in reader:
yield line
def main():
plugin_manager = PluginManager()
log_generator = LogLineGenerator()
for log_line in log_generator.get_loglines():
plugin_manager.call_method('process', args=log_line)
plugin_manager.call_method('report')
if __name__ == '__main__':
main()