-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
executable file
·63 lines (56 loc) · 1.95 KB
/
parse.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
59
60
61
62
63
#!/usr/bin/env python3
import ciso8601
import re
def parseLine(line):
fields = [
"type",
"timestamp",
"alb",
"client_ip",
"client_port",
"backend_ip",
"backend_port",
"request_processing_time",
"backend_processing_time",
"response_processing_time",
"lb_status_code",
"backend_status_code",
"received_bytes",
"sent_bytes",
"request_verb",
"request_url",
"request_proto",
"user_agent",
"ssl_cipher",
"ssl_protocol",
"target_group_arn",
"trace_id",
"domain_name",
"chosen_cert_arn",
"matched_rule_priority",
"request_creation_time",
"actions_executed",
"redirect_url",
"last_field",
]
# credit: https://gist.github.com/jweyrich/8d53a7bf5bad7b5958423cb4e538ab20
regex = r"([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \"([^ ]*) ([^ ]*) (- |[^ ]*)\" \"([^\"]*)\" ([A-Z0-9-]+) ([A-Za-z0-9.-]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([-.0-9]*) ([^ ]*) \"([^\"]*)\" ($|\"[^ ]*\")(.*)"
matches = re.search(regex, line)
res = {}
if matches:
for i, field in enumerate(fields):
value = matches.group(i+1)
if field == "timestamp":
res['timestamp'] = ciso8601.parse_datetime(value)
continue
if field == "redirect_url":
value = value.replace('"', '')
continue
if field == "last_field":
res['error_reason'] = value.split('"')[1]
res['target_ip'] = value.split('"')[3].split(":")[0]
res['target_port'] = value.split('"')[3].split(":")[1]
res['target_status_code_list'] = str(value.split('"')[5])
continue
res[field] = value
return res