-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryCombinedLogFormat.py
More file actions
452 lines (393 loc) · 13.2 KB
/
QueryCombinedLogFormat.py
File metadata and controls
452 lines (393 loc) · 13.2 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###################
# This script extracts, filters and parses combined log format (apache
# and nginx default access.log format) with a easy and fast language
# syntax
# Copyright (C) 2024 QueryCombinedLogFormat
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###################
"""
This script extracts, filters and parses combined log format (apache
and nginx default access.log format) with a easy and fast language
syntax
"""
__version__ = "0.0.1"
__author__ = "Maurice Lambert"
__author_email__ = "mauricelambert434@gmail.com"
__maintainer__ = "Maurice Lambert"
__maintainer_email__ = "mauricelambert434@gmail.com"
__description__ = """
This script extracts, filters and parses combined log format (apache
and nginx default access.log format) with a easy and fast language
syntax
"""
__url__ = "https://github.com/mauricelambert/QueryCombinedLogFormat"
# __all__ = []
__license__ = "GPL-3.0 License"
__copyright__ = """
QueryCombinedLogFormat Copyright (C) 2024 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
"""
copyright = __copyright__
license = __license__
print(copyright)
from ipaddress import ip_address, IPv4Address
from csv import writer, DictReader, QUOTE_ALL
from collections import Counter, defaultdict
from typing import List, Dict, Callable
from json import dumps, JSONEncoder
from sys import argv, stderr, exit
from re import compile as regex
from _io import TextIOWrapper
from datetime import datetime
from fnmatch import fnmatch
from gzip import GzipFile
from glob import iglob
regex_parser = regex(
r"""(?xs)
(?P<ip>(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\s+
"?(?P<client_identity>(\\"|[^"])*?)"?\s+
"?(?P<user_id>(\\"|[^"])*?)"?\s+
\[(?P<datetime>\d{2}/\w+/\d{4}(:\d{2}){3}\s+[+-]\d{4})\]\s+"
(?P<method>[^\s]+)\s+
(?P<url>(\\"|[^"])+)\s+
HTTP/(?P<version>\d\.\d)"\s+
(?P<status>\d+)\s+
(?P<size>(\d+|-))\s+"
(?P<referrer>(\\"|[^"])*?)"\s+"
(?P<user_agent>(\\"|[^"])*?)"\s*
"""
)
class ConditionalParser:
def __init__(self):
self.pos = 0
self.tokens = []
def tokenize(self, input_string):
operators = ["&", "|", "~", "=", ">", ">=", "<", "<=", "(", ")", "!"]
self.tokens = []
current_token = ""
next_parsed = False
for i, char in enumerate(input_string):
if next_parsed:
next_parsed = False
continue
if char.isspace():
if current_token:
self.tokens.append(current_token)
current_token = ""
elif char in operators:
if char in (">" or "<") and input_string[i + 1] == "=":
next_parsed = True
char += "="
if current_token:
self.tokens.append(current_token)
current_token = ""
self.tokens.append(char)
else:
if char == "\\" and (input_string[i + 1].isspace()):
char = input_string[i + 1]
next_parsed = True
current_token += char
if current_token:
self.tokens.append(current_token)
return self.tokens
def parse(self, input_string):
self.tokenize(input_string)
self.pos = 0
return self.parse_expression()
def parse_expression(self):
left = self.parse_term()
while self.pos < len(self.tokens) and self.tokens[self.pos] in [
"and",
"or",
"&",
"|",
]:
op = self.tokens[self.pos]
self.pos += 1
right = self.parse_term()
left = {
"op": "and" if op in ["and", "&"] else "or",
"left": left,
"right": right,
}
return left
def parse_term(self):
if self.tokens[self.pos] == "(":
self.pos += 1
expr = self.parse_expression()
if self.pos < len(self.tokens) and self.tokens[self.pos] == ")":
self.pos += 1
return expr
else:
raise ValueError("Missing closing parenthesis")
else:
return self.parse_condition()
def parse_condition(self):
if self.pos + 2 < len(self.tokens):
field = self.tokens[self.pos]
op = self.tokens[self.pos + 1]
value = self.tokens[self.pos + 2]
if op in ["~", "=", ">", ">=", "<", "<=", "!"]:
self.pos += 3
return {"field": field, "op": op, "value": value}
raise ValueError("Invalid condition at position " + str(self.pos))
class Dumper(JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat()
elif isinstance(obj, IPv4Address):
return str(obj)
return super().default(obj)
def compare(ask, op, value):
if op != "~":
if isinstance(value, int):
ask = int(ask)
elif isinstance(value, float):
ask = float(ask)
elif isinstance(value, datetime):
ask = datetime.fromisoformat(ask)
elif isinstance(value, IPv4Address):
ask = ip_address(ask)
elif isinstance(value, str):
ask = ask.casefold()
value = value.casefold()
if op == "~":
return fnmatch(str(value), ask)
elif op == "=":
return ask == value
elif op == ">":
return value > ask
elif op == ">=":
return value >= ask
elif op == "<":
return value < ask
elif op == "<=":
return value <= ask
elif op == "!":
return value != ask
def evaluate(parsed_expr, data):
if "left" in parsed_expr and "right" in parsed_expr:
if parsed_expr["op"] == "and":
return evaluate(parsed_expr["left"], data) and evaluate(
parsed_expr["right"], data
)
elif parsed_expr["op"] == "or":
return evaluate(parsed_expr["left"], data) or evaluate(
parsed_expr["right"], data
)
else:
field = parsed_expr["field"].lower()
op = parsed_expr["op"]
value = parsed_expr["value"]
if field not in data:
raise ValueError("Invalid field name " + repr(field))
return compare(value, op, data[field])
return False
def get_file(filename):
file = base_file = open(filename, "rb")
magic = file.read(2)
file.seek(0)
if magic == b"\x1f\x8b":
return GzipFile(fileobj=file), base_file, False
elif magic == b'"i':
file.close()
file = base_file = open(filename, "r")
return DictReader(file, quoting=QUOTE_ALL), base_file, True
return file, base_file, False
def print_help():
print(
"USAGES: QueryCombinedLogFormat [-s|--statistics] [-d|--to-db] <log_path> <queries>...",
file=stderr,
)
print("\tRequest example: method = POST", file=stderr)
print("\tRequest example: status ~ 50?", file=stderr)
print("\tRequest example: size >= 60000000", file=stderr)
print(
"\tRequest example: user_agent ~ *Version/6.0\\ Mobile* and ip = 66.249.73.135",
file=stderr,
)
print(
"\tRequest example: (METHOD = post or url ~ *admin*) & (ip > 91.0.0.0 | referrer ~ *://*)",
file=stderr,
)
print(
"\tField names: ip, client_identity, user_id, datetime, method, url, version, status, size, referrer, user_agent",
file=stderr,
)
print(
"\tOperators: = (equal case insensitive), ~ (match glob syntax), ! (not equal), >, <, >=, <=",
file=stderr,
)
print(
"\tInter expression: and (& works too), or (| works too)", file=stderr
)
print(
"\tParenthesis can be use to prioritize expression, default priority: left to right",
file=stderr,
)
print(
"\tEscape character: \\, it's working only with space and operators.",
file=stderr,
)
print("\tlog_path can be a glob syntax: ", file=stderr)
def get_line(globsyntax: str):
for filename in iglob(globsyntax, recursive=True):
file, base_file, text = get_file(filename)
for line in file:
yield line if text else line.decode("latin-1")
base_file.close()
def parse_line(line: str):
if isinstance(line, str):
values = regex_parser.fullmatch(line).groupdict()
values["datetime"] = datetime.strptime(
values["datetime"], "%d/%b/%Y:%H:%M:%S %z"
)
else:
values = line
values["datetime"] = datetime.fromisoformat(values["datetime"])
values["ip"] = ip_address(values["ip"])
values["version"] = float(values["version"])
values["status"] = int(values["status"])
values["size"] = int(values["size"]) if values["size"] != "-" else 0
return values
def parse_command_line():
statistics = False
if "-s" in argv:
argv.remove("-s")
statistics = True
elif "--statistics" in argv:
argv.remove("--statistics")
statistics = True
to_db = False
if "-d" in argv:
argv.remove("-d")
to_db = True
elif "--to-db" in argv:
argv.remove("--to-db")
to_db = True
if len(argv) < 3 or (len(argv) < 2 and (statistics or to_db)):
print_help()
return None, None, None, None
return statistics, to_db, argv[1], argv[2:]
def build_query_parsers(queries: List[str]):
query_parser = {}
for index, query in enumerate(queries):
index += 1
parser = ConditionalParser()
query_parser[index] = parser.parse(query)
return query_parser
def prepare(queries: List[str], to_db: bool):
query_parser = build_query_parsers(queries)
statistics_counters = defaultdict(Counter)
if not query_parser:
query_parser["*"] = "*"
evaluate = lambda *x: True
else:
evaluate = globals()["evaluate"]
file = csvfile = None
if to_db:
file = open(
"access_log_db_"
+ datetime.now().strftime("%Y%m%d_%H%M%S")
+ ".csv",
"w",
newline="",
)
csvfile = writer(file, quoting=QUOTE_ALL)
csvfile.writerow(
[
"ip",
"client_identity",
"user_id",
"datetime",
"method",
"url",
"version",
"status",
"size",
"referrer",
"user_agent",
]
)
return (
evaluate,
parse_line,
statistics_counters,
query_parser,
file,
csvfile,
)
def terminate(
statistics_counters: Dict[str, Dict[str, int]], dbfile: TextIOWrapper
):
if dbfile:
dbfile.close()
if statistics_counters:
print("LABEL".rjust(16), "|", "COUNT".rjust(9), "|", "VALUE")
for label, counter in statistics_counters.items():
for value, count in counter.most_common():
print(label.rjust(16), "|", str(count).rjust(9), "|", value)
def mainloop(
globsyntax: str,
query_parser: Dict[int, str],
statistics_counters: Dict[str, Dict[str, int]],
evaluate: Callable,
parse_line: Callable,
to_db: bool,
statistics: bool,
csvfile: TextIOWrapper,
):
for line in get_line(globsyntax):
values = parse_line(line)
for index, parsed_expr in query_parser.items():
if evaluate(parsed_expr, values):
if statistics:
for key, value in values.items():
statistics_counters[key][value] += 1
else:
print(
line.strip()
if isinstance(line, str)
else dumps(line, cls=Dumper)
)
if to_db:
csvfile.writerow(list(values.values()))
def main():
statistics, to_db, globsyntax, queries = parse_command_line()
if not globsyntax or (not queries and not (statistics or to_db)):
return 1
(
evaluate,
parse_line,
statistics_counters,
query_parser,
dbfile,
csvfile,
) = prepare(queries, to_db)
mainloop(
globsyntax,
query_parser,
statistics_counters,
evaluate,
parse_line,
to_db,
statistics,
csvfile,
)
terminate(statistics_counters, dbfile)
return 0
if __name__ == "__main__":
exit(main())