This repository was archived by the owner on Apr 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbundlefun.py
More file actions
589 lines (480 loc) · 18.4 KB
/
Copy pathbundlefun.py
File metadata and controls
589 lines (480 loc) · 18.4 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
#!/usr/bin/env python3
"""
An experiment in extracting key value pairs from files.
"""
__author__ = "David Morris gm3dmo@gmail.com"
__version__ = "0.1.0"
import re
import sys
import csv
import json
import gzip
import time
import shlex
import shutil
import pathlib
import sqlite3
import logging
import argparse
import datetime
import unicodedata
import configparser
import logging.config
from dataclasses import dataclass
logger = logging.getLogger(__name__)
@dataclass
class Report:
def __init__(self):
self.file_stats = {}
self.ghes_config = None
self.ghes_version = None
self.ghes_feature_version = None
self.lines_total = 0
self.lines_parsed = 0
self.lines_with_read_failed = 0
self.lines_with_parse_failed = 0
self.kv_pairs_extracted = 0
def get_ghes_config(self, gh_conf_file):
config = configparser.ConfigParser()
config.read(gh_conf_file)
self.ghes_config = config
self.get_ghes_version()
self.get_ghes_feature_version()
self.log_formats_file = 'log-formats.json'
def get_ghes_version(self):
self.ghes_version = self.ghes_config.get('core', 'package-version')
def get_ghes_feature_version(self):
# https://docs.github.com/en/enterprise-server@3.11/admin/overview/about-upgrades-to-new-releases
release, feature, patch = self.ghes_version.split('.')
self.ghes_feature_version = (f"{release}.{feature}")
def __str__(self):
return f'Report(file_stats={self.file_stats}, ghes_version={self.ghes_version}, ghes_feature_version={self.ghes_feature_version}, lines_total={self.lines_total}, lines_parsed={self.lines_parsed}, lines_with_read_failed={self.lines_with_read_failed}, lines_with_parse_failed={self.lines_with_parse_failed}, kv_pairs_extracted={self.kv_pairs_extracted})'
def parse_kv(text):
tokenizer = shlex.shlex(text, posix=True)
tokenizer.commenters = ""
tokenizer.whitespace_split = True
tokenizer.whitespace = " "
result = {}
for token in tokenizer:
if "=" in token:
logging.debug(f"""token: {token}""")
key, value = token.split("=", 1) # Split at the first equal sign only
result[key] = value
else:
logging.debug(f"""token: {token} has no = sign to split on""")
return result
def read_file_line_by_line(filename):
try:
with open(filename, 'r') as file:
for line_number, line in enumerate(file, start=1):
yield line_number, line, None
except Exception as e:
yield line_number, None, str(e)
def is_pypy3():
return shutil.which("pypy3")
def get_distinct_values(conn, table, column):
data = False
try:
c = conn.cursor()
query = f"""select distinct({column}) from {table}"""
data = c.execute(query)
except Exception as e:
logger.error(f"""{e}""")
return data
def create_connection(sqliteDB):
conn = None
try:
conn = sqlite3.connect(sqliteDB)
return conn
except Exception as e:
logger.error(f"""{e}""")
return conn
def get_create_view_text(table, column):
return f"""create view {table}_{column} as select {table}.'{column}', count() as count_of, round(100.0 * count() / (select count() from {table}), 2) as percentage from {table} group by {table}.'{column}';"""
def get_drop_view_text(table, column):
return f"""drop view percentage_of_{column};"""
def create_view(conn, create_view_sql):
try:
c = conn.cursor()
logger.info(f"""{create_view_sql}""")
c.execute(create_view_sql)
except Exception as e:
logger.error(f"""{e}""")
def drop_view(conn, drop_view_sql):
try:
c = conn.cursor()
logger.info(f"""{drop_view_sql}""")
c.execute(drop_view_sql)
except Exception as e:
logger.error(f"""{e}""")
def select_from_view(conn, query):
try:
c = conn.cursor()
logger.info(f"""select from view: {query}""")
c.execute(query)
except Exception as e:
logger.error(f"""{e}""")
def get_log_type(path):
log_type = path.stem.split(".")[0]
return log_type
def create_list_of_syslog_files_to_split(args):
log_list = []
count_of_log_types = {}
syslog_files = []
sqlite_db_chunk = args.sqlite_db_lines
logger.debug(f"""count_of_log_types: {count_of_log_types}""")
for lt in args.log_types:
count_of_log_types[lt] = 0
for log_directory in args.log_directories:
logger.debug(log_directory)
glob_string = f"""{log_directory}/*"""
for item in list(args.p.glob(glob_string)):
logger.debug(f"""{item.name}""")
if args.turbo == True and (str(item).endswith(".1") or str(item).endswith(".gz")):
logger.info(f"""Skipping {item} because turbo engaged""")
continue
if str(item).endswith(".csv"):
continue
if str(item).endswith(".backup"):
continue
if item.name.startswith("syslog"):
syslog_files.append(item)
logger.debug(f"{item.name}")
return syslog_files
def create_list_of_files_to_convert_to_csv(args):
log_list = []
count_of_log_types = {}
sqlite_db_chunk = args.sqlite_db_lines
logger.debug(f"""count_of_log_types: {count_of_log_types}""")
for lt in args.log_types:
count_of_log_types[lt] = 0
for log_directory in args.log_directories:
logger.debug(log_directory)
glob_string = f"""{log_directory}/*"""
for item in list(args.p.glob(glob_string)):
if str(item).endswith(".csv"):
continue
if str(item).endswith(".backup"):
continue
if str(item).startswith("system-logs/auth.log"):
continue
else:
log_type = get_log_type(item)
if log_type in args.log_types:
table_name = get_table_name(log_type)
if table_name == False:
table_name = log_type
processor = get_processor(log_type)
csv_file = f"""{item}.csv"""
log_list.append(
f"""{args.python_interpreter} {args.bin_dir}/{processor} {item} --log-type {log_type} --csv-file {csv_file}"""
)
logger.debug(f"""end count_of_log_types: {count_of_log_types}""")
return log_list
def create_list_of_csv_to_import_to_sqlite(args):
log_list = []
count_of_log_types = {}
sqlite_db_chunk = args.sqlite_db_lines
logger.debug(f"""count_of_log_types: {count_of_log_types}""")
for lt in args.log_types:
count_of_log_types[lt] = 0
for log_directory in args.log_directories:
logger.debug(log_directory)
glob_string = f"""{log_directory}/*"""
for item in list(args.p.glob(glob_string)):
logger.debug(f"""{item.name}""")
if str(item).endswith(".csv"):
continue
if str(item).endswith(".backup"):
continue
if item.name.startswith("syslog"):
continue
else:
log_type = get_log_type(item)
if log_type in args.log_types:
table_name = get_table_name(log_type)
if table_name == False:
table_name = log_type
processor = get_processor(log_type)
logger.debug(item)
csv_file = f"""{item}.csv"""
log_list.append(
f"""{args.python_interpreter} {args.bin_dir}/{processor} {item} --log-type {log_type} --csv-file {csv_file}"""
)
if count_of_log_types[log_type] == 0:
sqlite_db_chunk.append(f""".import {csv_file} {table_name}""")
count_of_log_types[log_type] += 1
logger.debug(
f"""====> {log_type}: zero count {count_of_log_types[log_type]}"""
)
else:
sqlite_db_chunk.append(
f""".import "|tail -n +2 {csv_file}" {table_name} """
)
count_of_log_types[log_type] += 1
logger.debug(
f"""----> {log_type}: count {count_of_log_types[log_type]}"""
)
logger.debug(f"""end count_of_log_types: {count_of_log_types}""")
sqlite_db_chunk.append(f"""EOF""")
return sqlite_db_chunk
def create_list_of_files_to_convert(args):
log_list = []
count_of_log_types = {}
syslog_files = []
logger.debug(f"""count_of_log_types: {count_of_log_types}""")
for lt in args.log_types:
count_of_log_types[lt] = 0
for log_directory in args.log_directories:
logger.debug(log_directory)
glob_string = f"""{log_directory}/*"""
for item in list(args.p.glob(glob_string)):
logger.debug(f"""{item.name}""")
if str(item).endswith(".csv"):
continue
if str(item).endswith(".backup"):
continue
if item.name.startswith("syslog"):
syslog_files.append(item)
logger.debug(f"{item.name}")
else:
log_type = get_log_type(item)
if log_type in args.log_types:
table_name = get_table_name(log_type)
if table_name == False:
table_name = log_type
processor = get_processor(log_type)
logger.debug(item)
csv_file = f"""{item}.csv"""
log_list.append(
f"""{args.python_interpreter} {args.bin_dir}/{processor} {item} --log-type {log_type} --csv-file {csv_file}"""
)
if count_of_log_types[log_type] == 0:
sqlite_db_chunk.append(f""".import {csv_file} {table_name}""")
count_of_log_types[log_type] += 1
logger.debug(
f"""====> {log_type}: zero count {count_of_log_types[log_type]}"""
)
else:
sqlite_db_chunk.append(
f""".import "|tail -n +2 {csv_file}" {table_name} """
)
count_of_log_types[log_type] += 1
logger.debug(
f"""----> {log_type}: count {count_of_log_types[log_type]}"""
)
logger.debug(f"""end count_of_log_types: {count_of_log_types}""")
logger.info(f"COUNTER: ")
sqlite_db_chunk.append(f"""EOF""")
return (log_list, sqlite_db_chunk, syslog_files)
def get_table_name(log_type):
p = pathlib.Path(__file__)
log_formats_file = p.parent / "log-formats.json"
kv_headers = {}
with open(log_formats_file) as json_file:
data = json.load(json_file)
if log_type in data:
kv_headers[log_type] = {}
table_name = False
if "table_name" in data[log_type]:
table_name = data[log_type]["table_name"]
return table_name
def get_temporal_column(log_type):
p = pathlib.Path(__file__)
log_formats_file = p.parent / "log-formats.json"
kv_headers = {}
if log_type == "hookshot":
log_type = "hookshot-go"
with open(log_formats_file) as json_file:
data = json.load(json_file)
if log_type in data:
temporal = False
if "temporal" in data[log_type]:
temporal = data[log_type]["temporal"][0]
return temporal
else:
return False
def get_view_facets(log_type):
p = pathlib.Path(__file__)
log_formats_file = p.parent / "log-formats.json"
kv_headers = {}
if log_type == "hookshot":
log_type = "hookshot-go"
with open(log_formats_file) as json_file:
data = json.load(json_file)
if log_type in data:
kv_headers[log_type] = {}
table_name = False
if "view_facets" in data[log_type]:
view_facets = data[log_type]["view_facets"]
return view_facets
else:
return False
def get_processor(log_type):
p = pathlib.Path(__file__)
log_formats_file = p.parent / "log-formats.json"
kv_headers = {}
with open(log_formats_file) as json_file:
data = json.load(json_file)
if log_type in data:
kv_headers[log_type] = {}
return data[log_type]["processor"]
if log_type == "syslog":
return "syslog-to-csv.py"
if log_type == "exceptions":
return "jsonl-to-csv.py"
else:
return "kv-to-csv.py"
def split_daemon(daemon):
return daemon.split("[")
def syslog_fieldnames():
return [
"line_number",
"line_length",
"extracted_date",
"unix_timestamp",
"real_date",
"hostname",
"daemon",
"wiped_line",
]
def slugify(value):
"""
Converts to lowercase, removes non-word characters (alphanumerics and
underscores) and converts spaces to hyphens. Also strips leading and
trailing whitespace.
"""
value = (
unicodedata.normalize("NFKD", value).encode("ascii", "ignore").decode("ascii")
)
value = re.sub("[^\w\s-]", "", value).strip().lower()
return re.sub("[-\s]+", "-", value)
def is_gzipped(path):
if path.suffix == ".gz":
return True
def open_file_handle(fn):
open_fh = gzip.open if is_gzipped(fn) else open
return open_fh
def get_wanted_kv_headers(logtype="sample", extract_type="core"):
p = pathlib.Path(__file__)
log_formats_file = p.parent / "log-formats.json"
logging.debug(f"""log_formats_file used: {log_formats_file}""")
kv_headers = {}
with open(log_formats_file) as json_file:
data = json.load(json_file)
if logtype in data:
logging.debug(
f"""logtype ({logtype}) is known and can be processed: {logtype}
extracting {len(data[logtype]['core'])} kv fields"""
)
kv_headers[logtype] = {}
kv_headers[logtype]["core"] = data[logtype]["core"]
kv_headers[logtype]["must"] = data[logtype]["must"]
else:
logging.error(f""": filetype is not known: {logtype}""")
return kv_headers
def sizer(size):
for x in ["bytes", "KB", "MB", "GB", "TB"]:
if size < 1024.0:
return "%3.1f %s" % (size, x)
size /= 1024.0
return size
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
if "log_time" in kw:
name = kw.get("log_name", method.__name__.upper())
kw["log_time"][name] = int((te - ts) * 1000)
else:
duration = te - ts
logger.info(
"%r executed in %2.2f ms " % (method.__name__, (te - ts) * 1000)
)
logger.info(f"""{str(datetime.timedelta(seconds=duration))}""")
return result
return timed
def parse_kv_pairs_two(text):
logger.debug(f"""parse_kv_pairs_two: ({text})""")
try:
tokenizer = shlex.shlex(text, posix=True)
except Exception:
raise
tokenizer.commenters = ""
tokenizer.whitespace_split = True
tokenizer.whitespace = " "
result = {}
try:
for token in tokenizer:
if "=" in token:
logger.debug(token)
result.update(dict(x.split("=", 1) for x in token.split(",")))
except ValueError:
error = tokenizer.token.splitlines()[0]
logger.debug("parsing problem tokenzer leader: " + tokenizer.error_leader())
logger.debug("partsing problem text: " + text)
return result
def get_csv_handle(output_filename, fieldnames, mode="w"):
csv_file = open(output_filename, mode=mode)
csv_writer = csv.DictWriter(
csv_file, delimiter=",", fieldnames=fieldnames, quoting=csv.QUOTE_NONNUMERIC
)
return csv_writer
def known_regexes(want_to_match):
kr = {
"mac_address": """/^([0-9A-F]{2}[-:]){5}[0-9A-F]{2}$/i""",
"numbers": """r'\d+""",
}
return kr[want_to_match]
def get_filesize(path):
return pathlib.Path(path).stat().st_size
def strip_regex(string_to_wipe, pattern_to_wipe=r"\d+"):
logger.debug(f"""Original: {string_to_wipe}""")
wiped_string = re.sub(pattern_to_wipe, "", string_to_wipe)
logger.debug(f"""Cleaned of ({pattern_to_wipe}): {wiped_string}""")
return wiped_string
def wipe_numbers_from_string(string_to_wipe, pattern_to_wipe=r"\d+"):
logger.debug(f"""Original: {string_to_wipe}""")
wiped_string = re.sub(pattern_to_wipe, "", string_to_wipe)
logger.debug(f"""Cleaned of: ({pattern_to_wipe}) ({wiped_string})""")
return wiped_string
def wipe_64charguids_from_string(
string_to_wipe, pattern_to_wipe=r"[\-@0-9a-fA-F']{24,64}"
):
logger.debug(f"""Original: {string_to_wipe}""")
wiped_string = re.sub(pattern_to_wipe, "", string_to_wipe)
logger.debug(f"""Cleaned of: ({pattern_to_wipe}) {wiped_string}""")
return wiped_string
def wipe_guids_from_string(
string_to_wipe,
pattern_to_wipe=r"[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}",
):
logger.debug(f"""Original: {string_to_wipe}""")
wiped_string = re.sub(pattern_to_wipe, "", string_to_wipe)
logger.debug(f"""Cleansed: {wiped_string}""")
return wiped_string
def fix_syslog_date(original_date, base_year=""):
"""
`original_date='Aug 15 08:38:22`
Look at the date format. There is no year!
Oh it gets worse.
If it's the first 1-9 days of the month:
```
Aug 5 08:38:22
```
Two spaces to trap the unwary.
"""
original_date = " ".join(original_date.split())
(m, d, t) = original_date.split(" ")
if base_year:
iso_year = base_year
logger.debug(f"""passed in via base-year: {iso_year}""")
else:
base_date_today = datetime.datetime.now()
iso_year = base_date_today.year
logger.debug(f"""defaulting to year: {iso_year}""")
real_date = f"""{iso_year} {m} {d} {t}"""
real_datetime_obj = datetime.datetime.strptime(real_date, "%Y %b %d %H:%M:%S")
rd = real_datetime_obj.isoformat()
return (real_date, rd, real_datetime_obj)