forked from awslabs/amazon-redshift-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.py
More file actions
1201 lines (1022 loc) · 44.9 KB
/
extract.py
File metadata and controls
1201 lines (1022 loc) · 44.9 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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import datetime
import gzip
import json
import logging
import os
import pathlib
import re
import redshift_connector
import threading
import time
import yaml
from tqdm import tqdm
from collections import OrderedDict
from contextlib import contextmanager
import boto3
from boto3 import client
import dateutil.parser
from util import init_logging, set_log_level, prepend_ids_to_logs, add_logfile, log_version
logger = None
g_disable_progress_bar = None
g_bar_format = '{desc}: {percentage:3.0f}%|{bar}| {n_fmt}/{total_fmt} [{elapsed}{postfix}]'
class Log:
def __init__(self):
self.record_time = ""
self.start_time = ""
self.end_time = ""
self.username = ""
self.database_name = ""
self.pid = ""
self.xid = ""
self.text = ""
def get_filename(self):
base_name = (
self.database_name + "-" + self.username + "-" + self.pid + "-" + self.xid + " (" + self.record_time.isoformat() + ")"
)
return base_name
def __str__(self):
return (
"Record time: %s, Start time: %s, End time: %s, Username: %s, Database: %s, PID: %s, XID: %s, Query: %s"
% (
self.record_time,
self.start_time,
self.end_time,
self.username,
self.database_name,
self.pid,
self.xid,
self.text,
)
)
def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.record_time == other.record_time
and self.start_time == other.start_time
and self.end_time == other.end_time
and self.username == other.username
and self.database_name == other.database_name
and self.pid == other.pid
and self.xid == other.xid
and self.text == other.text
)
def __hash__(self):
return hash((str(self.pid), str(self.xid), self.text.strip("\n")))
class ConnectionLog:
def __init__(self, session_initiation_time, end_time, database_name, username, pid):
self.session_initiation_time = session_initiation_time
self.disconnection_time = end_time
self.application_name = ""
self.database_name = database_name
self.username = username
self.pid = pid
self.time_interval_between_transactions = True
self.time_interval_between_queries = "transaction"
def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.session_initiation_time == other.session_initiation_time
and self.disconnection_time == other.disconnection_time
and self.application_name == other.application_name
and self.database_name == other.database_name
and self.username == other.username
and self.pid == other.pid
and self.time_interval_between_transactions
== other.time_interval_between_transactions
and self.time_interval_between_queries
== other.time_interval_between_queries
)
def __hash__(self):
return hash((self.database_name, self.username, self.pid))
def get_pk(self):
return hash((self.session_initiation_time, self.database_name, self.username, self.pid))
class SystemLog:
def __init__(self, start_time, end_time, database_name, user_id, pid, xid, text):
self.start_time = start_time
self.end_time = end_time
self.database_name = database_name
self.user_id = user_id
self.pid = pid
self.xid = xid
self.text = text
def __str__(self):
return (
"Start time: %s, End time: %s, User id: %s, PID: %s, XID: %s, Query: %s"
% (
self.start_time,
self.end_time,
self.user_id,
self.pid,
self.xid,
self.text,
)
)
def __eq__(self, other):
return (
isinstance(other, self.__class__)
and self.start_time == other.start_time
and self.end_time == other.end_time
and self.user_id == other.user_id
and self.pid == other.xid
and self.xid == other.xid
and self.text == other.text
)
def __hash__(self):
return hash((str(self.pid), str(self.xid), self.text.strip("\n")))
def retrieve_source_cluster_statement_text(
source_cluster_urls, databases, start_time, end_time, interface
):
statement_text_logs = {}
for database_name in databases:
with initiate_connection(
source_cluster_urls, interface, database_name
) as connection:
cursor = connection.cursor()
start_time_where = ""
end_time_where = ""
if start_time:
start_time_where = (
f"AND starttime > '{start_time.strftime('%Y-%m-%d %H:%M:%S')}' "
)
if end_time:
end_time_where = (
f"AND endtime < '{end_time.strftime('%Y-%m-%d %H:%M:%S')}' "
)
cursor.execute(
"SELECT starttime, endtime, userid, pid, xid, text, sequence "
"FROM SVL_STATEMENTTEXT "
f"WHERE userid>1 {start_time_where} {end_time_where}"
"ORDER BY xid, starttime, sequence;"
)
fetch_size = 10000
rows = cursor.fetchmany(fetch_size)
complete_log = None
for row in rows:
start_time = row[0].replace(tzinfo=datetime.timezone.utc)
end_time = row[1].replace(tzinfo=datetime.timezone.utc)
system_log = SystemLog(
start_time, end_time, database_name, row[2], row[3], row[4], row[5],
)
if row[6] == 0:
if complete_log is None:
complete_log = system_log
else:
if hash(complete_log) in statement_text_logs:
statement_text_logs[hash(complete_log)].append(complete_log)
else:
statement_text_logs[hash(complete_log)] = [complete_log]
complete_log = system_log
else:
if not complete_log is None:
complete_log.text = complete_log.text + system_log.text
rows = cursor.fetchmany(fetch_size)
if hash(complete_log) in statement_text_logs:
statement_text_logs[hash(complete_log)].append(complete_log)
else:
statement_text_logs[hash(complete_log)] = [complete_log]
return statement_text_logs
def combine_logs(audit_logs, statement_text_logs):
for audit_transaction in audit_logs:
for audit_query in audit_logs[audit_transaction]:
matching_statement_text_logs = statement_text_logs.get(hash(audit_query))
if matching_statement_text_logs:
statement_text_log = matching_statement_text_logs.pop()
if statement_text_log:
if statement_text_log.start_time:
audit_query.start_time = statement_text_log.start_time
if statement_text_log.end_time:
audit_query.end_time = statement_text_log.end_time
@contextmanager
def initiate_connection(cluster_urls, interface, database_name):
conn = None
if interface == "odbc":
cluster_split = cluster_urls["odbc"].split(";")
cluster_split[2] = "Database=" + database_name
cluster = ";".join(cluster_split)
try:
conn = pyodbc.connect(cluster, autocommit=True)
yield conn
finally:
if conn is not None:
conn.close()
elif interface == "psql":
try:
conn = redshift_connector.connect(
user=cluster_urls["psql"]["username"],
password=cluster_urls["psql"]["password"],
host=cluster_urls["psql"]["host"],
port=int(cluster_urls["psql"]["port"]),
database=database_name,
)
conn.autocommit = True
yield conn
finally:
if conn is not None:
conn.close()
def parse_log(
log_file, filename, connections, last_connections, logs, databases, start_time, end_time,
):
if "useractivitylog" in filename:
logger.debug(f"Parsing user activity log: {filename}")
parse_user_activity_log(log_file, logs, databases, start_time, end_time)
elif "connectionlog" in filename:
logger.debug(f"Parsing connection log: {filename}")
parse_connection_log(log_file, connections, last_connections, start_time, end_time)
elif "start_node" in filename:
logger.debug(f"Parsing start node log: {filename}")
parse_start_node_log(log_file, logs, databases, start_time, end_time)
def parse_connection_log(file, connections, last_connections, start_time, end_time):
for line in file.readlines():
line = line.decode("utf-8")
connection_information = line.split("|")
connection_event = connection_information[0]
event_time = datetime.datetime.strptime(
connection_information[1], "%a, %d %b %Y %H:%M:%S:%f"
).replace(tzinfo=datetime.timezone.utc)
pid = connection_information[4]
database_name = connection_information[5].strip()
if connection_information[7].strip() == 'IAM AssumeUser':
username = connection_information[6].strip()[4:]
else:
username = connection_information[6].strip()
application_name = connection_information[15]
if username != "rdsdb" and (not start_time or event_time >= start_time) and (not end_time or event_time <= end_time):
connection_log = ConnectionLog(event_time, end_time, database_name, username, pid)
if connection_event == "initiating session ":
connection_key = connection_log.get_pk()
# create a new connection
connections[connection_key] = connection_log
last_connections[hash(connection_log)]=connection_key
elif connection_event == "set application_name ":
if hash(connection_log) in last_connections:
connection_key = last_connections[hash(connection_log)]
if connection_key in connections:
# set the latest connection with application name
connections[connection_key].application_name = " ".join(application_name.split())
else:
# create new connection if there's no one yet with start
# time equals to start of extraction
connection_log.session_initiation_time = start_time
connection_key = connection_log.get_pk()
connections[connection_key] = connection_log
last_connections[hash(connection_log)] = connection_key
elif connection_event == "disconnecting session ":
if hash(connection_log) in last_connections:
connection_key = last_connections[hash(connection_log)]
if connection_key in connections:
# set the latest connection with disconnection time
connections[connection_key].disconnection_time = event_time
else:
# create new connection if there's no one yet with start
# time equals to start of extraction
connection_log.session_initiation_time = start_time
connection_log.disconnection_time = event_time
connection_key = connection_log.get_pk()
connections[connection_key] = connection_log
last_connections[hash(connection_log)] = connection_key
def parse_user_activity_log(file, logs, databases, start_time, end_time):
user_activity_log = Log()
datetime_pattern = re.compile(r"'\d+-\d+-\d+T\d+:\d+:\d+Z UTC")
fetch_pattern = re.compile(r"fetch\s+(next|all|forward all|\d+|forward\s+\d+)\s+(from|in)\s+\S+", flags=re.IGNORECASE)
for line in file.readlines():
line = line.decode("utf-8")
if datetime_pattern.match(line):
if user_activity_log.xid and is_valid_log(
user_activity_log, start_time, end_time
):
filename = user_activity_log.get_filename()
if filename in logs:
# Check if duplicate. This happens with JDBC connections.
prev_query = logs[filename][-1]
if not is_duplicate(prev_query.text, user_activity_log.text):
if fetch_pattern.search(prev_query.text) and fetch_pattern.search(user_activity_log.text):
user_activity_log.text = f"--{user_activity_log.text}"
logs[filename].append(user_activity_log)
else:
logs[filename].append(user_activity_log)
else:
logs[filename] = [user_activity_log]
databases.add(user_activity_log.database_name)
user_activity_log = Log()
line_split = line.split(" LOG: ")
query_information = line_split[0].split(" ")
user_activity_log.record_time = dateutil.parser.parse(
query_information[0][1:]
)
user_activity_log.username = query_information[4][5:]
user_activity_log.database_name = query_information[3][3:]
user_activity_log.pid = query_information[5][4:]
user_activity_log.xid = query_information[7][4:]
user_activity_log.text = line_split[1]
else:
user_activity_log.text += line
def is_valid_log(log, start_time, end_time):
"""If query doesn't contain problem statements, saves it."""
problem_keywords = [
"SPECTRUM INTERNAL QUERY",
"context: SQL",
"ERROR:",
"CONTEXT: SQL",
"show ",
"Undoing transaction",
"Undo on",
"pg_terminate_backend",
"pg_cancel_backend",
"volt_",
"pg_temp_",
]
potential_problem_keywords = [
"BIND"
]
not_problem_keywords = [
"BINDING"
]
if log.username == "rdsdb":
return False
if start_time and log.record_time < start_time:
return False
if end_time and log.record_time > end_time:
return False
if any(word in log.text for word in problem_keywords):
return False
if any(word in log.text for word in potential_problem_keywords) and not any(word in log.text for word in not_problem_keywords):
return False
return True
def is_duplicate(first_query_text, second_query_text):
dedupe_these = [
"set",
"select",
"create",
"delete",
"update",
"insert",
"copy",
"unload",
"with"
]
first_query_text = first_query_text.strip()
second_query_text = second_query_text.strip()
first_query_text_no_semi = first_query_text.replace(";", "")
second_query_tex_no_semi = second_query_text.replace(";", "")
second_query_comment_removed = second_query_text
first_query_comment_removed = first_query_text
if second_query_text.startswith("/*"):
second_query_comment_removed = second_query_text[second_query_text.find('*/')+2:len(second_query_text)].strip()
if first_query_text.startswith("/*"):
first_query_comment_removed = first_query_text[second_query_text.find('*/')+2:len(first_query_text)].strip()
return (
(
first_query_text_no_semi == second_query_tex_no_semi
and any(second_query_comment_removed.startswith(word) for word in dedupe_these)
) or ((second_query_comment_removed.lower().startswith('create')) and (first_query_comment_removed.lower().startswith('create')) and second_query_comment_removed.endswith(';'))
or ((second_query_comment_removed.lower().startswith('drop')) and (first_query_comment_removed.lower().startswith('drop')) and second_query_comment_removed.endswith(';'))
or ((second_query_comment_removed.lower().startswith('alter')) and (first_query_comment_removed.lower().startswith('alter')) and second_query_comment_removed.endswith(';'))
)
def parse_start_node_log(file, logs, databases, start_time, end_time):
start_node_log = Log()
datetime_pattern = re.compile(r"'\d+-\d+-\d+ \d+:\d+:\d+ UTC")
for line in file.readlines():
if datetime_pattern.match(line):
if start_node_log.xid and is_valid_log(
start_node_log, start_time, end_time
):
filename = start_node_log.get_filename()
if filename in logs:
# Check if duplicate. This happens with JDBC connections.
prev_query = logs[filename][-1]
if not is_duplicate(prev_query.text, start_node_log.text):
logs[filename].append(start_node_log)
else:
logs[filename] = [start_node_log]
databases.add(start_node_log.database_name)
start_node_log = Log()
line_split = line.split("LOG: statement: ")
# We only want to export statements, not errors or contexts
if len(line_split) == 2:
query_information = line_split[0].split(" ")
start_node_log.record_time = dateutil.parser.parse(
query_information[0][1:]
+ " "
+ query_information[1]
+ " "
+ query_information[2]
)
start_node_log.database_name = query_information[4].split("@")[1]
start_node_log.username = query_information[4][3:].split(":")[0]
start_node_log.pid = query_information[5][4:]
start_node_log.xid = query_information[7][4:]
start_node_log.text = line_split[1].strip()
else:
start_node_log.text += line
def connection_time_replacement(sorted_connections):
i = 0
min_init_time = sorted_connections[0]['session_initiation_time']
max_disconnect_time = sorted_connections[0]['disconnection_time']
empty_init_times = []
empty_disconnect_times = []
for connection in sorted_connections:
if connection['session_initiation_time'] == '':
empty_init_times.append(i)
elif min_init_time > connection['session_initiation_time']:
min_init_time = connection['session_initiation_time']
if connection['disconnection_time'] == '':
empty_disconnect_times.append(i)
elif max_disconnect_time == '' or ( max_disconnect_time and max_disconnect_time < connection['disconnection_time']):
max_disconnect_time = connection['disconnection_time']
i += 1
for init_time in empty_init_times:
sorted_connections[init_time]['session_initiation_time'] = min_init_time
for init_time in empty_disconnect_times:
sorted_connections[init_time]['disconnection_time'] = max_disconnect_time
return sorted_connections
"""
Remove single line comments
If a line comment is inside a block comment, then the line comment ends at the end of the comment
param query: the multiline query to remove single line comments from
return: a string of the update query lines
"""
def remove_line_comments(query):
removed_string = query
prev_location = 0
while True:
line_comment_begin = removed_string.find('--', prev_location)
prev_location = line_comment_begin
# no more comments to find
if line_comment_begin == -1:
break
#found_comment = True
linebreak = removed_string.find('\n', line_comment_begin)
start_comment = removed_string.find('/*', line_comment_begin, linebreak if linebreak != -1 else len(removed_string))
end_comment = removed_string.find('*/', line_comment_begin, linebreak if linebreak != -1 else len(removed_string))
if linebreak != -1:
if start_comment == -1 and end_comment != -1:
# if line comment is between start and end, then remove until end of comment
removed_string = removed_string[:line_comment_begin] + removed_string[end_comment:]
else:
# else remove up the end of line
removed_string = removed_string[:line_comment_begin] + removed_string[linebreak:]
else:
# reached end of query
if start_comment == -1 and end_comment != -1:
# if line comment is between start and end, then remove until end of comment
removed_string = removed_string[:line_comment_begin] + removed_string[end_comment:]
else:
# else remove up the end of line
removed_string = removed_string[:line_comment_begin]
return removed_string
def save_logs(logs, last_connections, output_directory, connections, start_time, end_time):
num_queries = 0
for filename, transaction in logs.items():
num_queries += len(transaction)
logger.info(
f"Exporting {len(logs)} transactions ({num_queries} queries) to {output_directory}"
)
is_s3 = True
if output_directory.startswith("s3://"):
output_s3_location = output_directory[5:].partition("/")
bucket_name = output_s3_location[0]
output_prefix = output_s3_location[2]
s3_client = boto3.client("s3")
archive_filename = "/tmp/SQLs.json.gz"
else:
is_s3 = False
archive_filename = output_directory + "/SQLs.json.gz"
logger.info(f"Creating directory {output_directory} if it doesn't already exist")
pathlib.Path(output_directory).mkdir(parents=True, exist_ok=True)
# transactions has form { "xid": xxx, "pid": xxx, etc..., queries: [] }
sql_json = {"transactions": OrderedDict()}
missing_audit_log_connections = set()
# Save the main logs and find replacements
replacements = set()
for filename, queries in tqdm(logs.items(), disable=g_disable_progress_bar, unit='files', desc='Files processed', bar_format=g_bar_format):
for idx, query in enumerate(queries):
try:
if query.xid not in sql_json['transactions']:
sql_json['transactions'][query.xid] = {"xid": query.xid,
"pid": query.pid,
"db": query.database_name,
"user": query.username,
"time_interval": True,
"queries": []}
query_info = {
"record_time": query.record_time.isoformat(),
"start_time": query.start_time.isoformat() if query.start_time else None,
"end_time": query.end_time.isoformat() if query.end_time else None
}
except AttributeError:
logger.error(f'Query is missing header info, skipping {filename}: {query}')
continue
query.text = remove_line_comments(query.text).strip()
if "copy " in query.text.lower() and "from 's3:" in query.text.lower(): #Raj
bucket = re.search(r"from 's3:\/\/[^']*", query.text, re.IGNORECASE).group()[6:]
replacements.add(bucket)
query.text = re.sub(
r"IAM_ROLE 'arn:aws:iam::\d+:role/\S+'",
f" IAM_ROLE ''",
query.text,
flags=re.IGNORECASE,
)
if "unload" in query.text.lower() and "to 's3:" in query.text.lower():
query.text = re.sub(
r"IAM_ROLE 'arn:aws:iam::\d+:role/\S+'",
f" IAM_ROLE ''",
query.text,
flags=re.IGNORECASE,
)
query.text = f"{query.text.strip()}"
if not len(query.text) == 0:
if not query.text.endswith(";"):
query.text += ";"
if "%" in query.text:
# Escape modulo operator in extract for replay - RR-411
query_info['text'] = query.text.replace("%", "%%")
else:
query_info['text'] = query.text
sql_json['transactions'][query.xid]['queries'].append(query_info)
if not hash((query.database_name, query.username, query.pid)) in last_connections:
missing_audit_log_connections.add((query.database_name, query.username, query.pid))
with gzip.open(archive_filename, 'wb') as f:
f.write(json.dumps(sql_json, indent=2).encode('utf-8'))
if is_s3:
dest = output_prefix + "/SQLs.json.gz"
logger.info("Transferring SQL archive to {dest}")
s3_client.upload_file(archive_filename, bucket_name, dest)
logger.info(f"Generating {len(missing_audit_log_connections)} missing connections.")
for missing_audit_log_connection_info in missing_audit_log_connections:
connection = ConnectionLog(
start_time,
end_time, # for missing connections set start_time and end_time to our extraction range
missing_audit_log_connection_info[0],
missing_audit_log_connection_info[1],
missing_audit_log_connection_info[2],
)
pk = connection.get_pk()
connections[pk] = connection
logger.info(
f"Exporting a total of {len(connections.values())} connections to {output_directory}"
)
# Save the connections logs
sorted_connections = connections.values()
connections_dict = connection_time_replacement([connection.__dict__ for connection in sorted_connections])
connections_string = json.dumps(
[connection.__dict__ for connection in sorted_connections],
indent=4,
default=str,
)
if is_s3:
s3_client.put_object(
Body=connections_string,
Bucket=bucket_name,
Key=output_prefix + "/connections.json",
)
else:
connections_file = open(output_directory + "/connections.json", "x")
connections_file.write(connections_string)
connections_file.close()
# Save the replacements
logger.info(f"Exporting copy replacements to {output_directory}")
replacements_string = (
"Original location,Replacement location,Replacement IAM role\n"
)
for bucket in replacements:
replacements_string += bucket + ",,\n"
if is_s3:
s3_client.put_object(
Body=replacements_string,
Bucket=bucket_name,
Key=output_prefix + "/copy_replacements.csv",
)
else:
replacements_file = open(output_directory + "/copy_replacements.csv", "w")
replacements_file.write(replacements_string)
replacements_file.close()
def get_cluster_log_location(source_cluster_endpoint):
""" Get the audit log location for the cluster via the API """
logger.debug(f"Retrieving log location for {source_cluster_endpoint}")
result = client("redshift").describe_logging_status(
ClusterIdentifier=source_cluster_endpoint.split(".")[0]
)
if not result["LoggingEnabled"]:
logger.warning(f"Cluster {source_cluster_endpoint} does not appear to have audit logging enabled. Please confirm logging is enabled.")
return None
location = "s3://{}/{}".format(result["BucketName"], result.get('S3KeyPrefix', ''))
logger.debug(f"Log location: {location}")
return location
def get_logs(log_location, start_time, end_time):
logger.info(f"Extracting and parsing logs from {log_location}")
logger.info(f"Time range: {start_time or '*'} to {end_time or '*'}")
logger.info(f"This may take several minutes...")
if log_location.startswith("s3://"):
match = re.search(r's3://([^/]+)/(.*)', log_location)
if not(match):
logger.error(f"Failed to parse log location {log_location}")
return None
return get_s3_logs(match.group(1), match.group(2), start_time, end_time)
else:
return get_local_logs(log_location, start_time, end_time)
def get_local_logs(log_directory_path, start_time, end_time):
connections = {}
last_connections = {}
logs = {}
databases = set()
unsorted_list = os.listdir(log_directory_path)
log_directory = sorted(unsorted_list)
for filename in tqdm(log_directory, disable=g_disable_progress_bar, unit='files', desc='Files processed', bar_format=g_bar_format):
if g_disable_progress_bar:
logger.info(f"Processing {filename}")
if "start_node" in filename:
log_file = gzip.open(
log_directory_path + "/" + filename, "rt", encoding="ISO-8859-1"
)
else:
log_file = gzip.open(log_directory_path + "/" + filename, "r")
parse_log(
log_file, filename, connections, last_connections, logs, databases, start_time, end_time,
)
log_file.close()
return (connections, logs, databases, last_connections)
def get_s3_logs(log_bucket, log_prefix, start_time, end_time):
connections = {}
logs = {}
last_connections = {}
databases = set()
conn = client("s3")
# get first set of
response = conn.list_objects_v2(Bucket=log_bucket,
Prefix=log_prefix
)
bucket_objects = response["Contents"]
if "NextContinuationToken" in response:
prev_key = response["NextContinuationToken"]
while True:
response = conn.list_objects_v2(Bucket=log_bucket,
Prefix=log_prefix,
ContinuationToken=prev_key
)
bucket_objects.extend(response["Contents"])
if "NextContinuationToken" not in response:
break
prev_key = response["NextContinuationToken"]
s3_connection_logs = []
s3_user_activity_logs = []
for log in bucket_objects:
filename = log["Key"].split("/")[-1]
if "connectionlog" in filename:
s3_connection_logs.append(log)
elif "useractivitylog" in filename:
s3_user_activity_logs.append(log)
logger.info("Parsing connection logs")
get_s3_audit_logs(
log_bucket,
log_prefix,
start_time,
end_time,
s3_connection_logs,
connections,
logs,
databases,
last_connections,
)
logger.info("Parsing user activity logs")
get_s3_audit_logs(
log_bucket,
log_prefix,
start_time,
end_time,
s3_user_activity_logs,
connections,
logs,
databases,
last_connections,
)
return (connections, logs, databases, last_connections)
def get_logs_in_range(audit_objects, start_time, end_time):
start_idx = None
end_idx = None
filenames = []
for index, log in list(enumerate(audit_objects)):
filename = log["Key"].split("/")[-1]
file_datetime = dateutil.parser.parse(filename.split("_")[-1][:-3]).replace(
tzinfo=datetime.timezone.utc
)
if start_time and file_datetime < start_time:
continue
if end_time and file_datetime > end_time:
# make sure we've started
if len(filenames) > 0:
filenames.append(log["Key"])
break
# start with one before the first file to make sure we capture everything
if len(filenames) == 0 and index > 0:
filenames.append(audit_objects[index - 1]["Key"])
filenames.append(log["Key"])
return filenames
def get_s3_audit_logs(
log_bucket,
log_prefix,
start_time,
end_time,
audit_objects,
connections,
logs,
databases,
last_connections,
):
s3 = boto3.resource("s3")
index_of_last_valid_log = len(audit_objects) - 1
log_filenames = get_logs_in_range(audit_objects, start_time, end_time)
logger.info(f"Processing {len(log_filenames)} files")
is_continue_parsing = True
curr_index = index_of_last_valid_log
last = curr_index
for filename in tqdm(log_filenames, disable=g_disable_progress_bar, unit='files', desc='Files processed', bar_format=g_bar_format):
file_datetime = dateutil.parser.parse(filename.split("_")[-1][:-3]).replace(
tzinfo=datetime.timezone.utc
)
curr_connection_length = len(connections)
curr_logs_length = len(logs)
log_object = s3.Object(log_bucket, filename)
log_file = gzip.GzipFile(fileobj=log_object.get()["Body"])
parse_log(
log_file, filename, connections, last_connections, logs, databases, start_time, end_time,
)
logger.debug(
f'First audit log in start_time range: {audit_objects[curr_index]["Key"].split("/")[-1]}'
)
return (connections, logs, databases, last_connections)
def get_connection_string(cluster_endpoint, username, odbc_driver):
cluster_endpoint_split = cluster_endpoint.split(".")
cluster_id = cluster_endpoint_split[0]
cluster_host = cluster_endpoint.split(":")[0]
cluster_port = cluster_endpoint_split[5].split("/")[0][4:]
cluster_database = cluster_endpoint_split[5].split("/")[1]
try:
response = client("redshift").get_cluster_credentials(
DbUser=username, ClusterIdentifier=cluster_id, AutoCreate=False,
)
cluster_odbc_url = (
"Driver={%s}; Server=%s; Database=%s; IAM=1; DbUser=%s; DbPassword=%s; Port=%s"
% (
odbc_driver,
cluster_host,
cluster_database,
response["DbUser"].split(":")[1],
response["DbPassword"],
cluster_port,
)
)
cluster_psql = {
"username": response["DbUser"],
"password": response["DbPassword"],
"host": cluster_host,
"port": cluster_port,
"database": cluster_database,
}
return {"odbc": cluster_odbc_url, "psql": cluster_psql}
except Exception as err:
logger.error("Failed to generate connection string: " + str(err))
return ""
def unload_system_table(
source_cluster_urls,
odbc_driver,
unload_system_table_queries_file,
unload_location,
unload_iam_role,
):
conn = None
if odbc_driver:
conn = pyodbc.connect(source_cluster_urls["odbc"])
else:
conn = redshift_connector.connect(
user=source_cluster_urls["psql"]["username"],
password=source_cluster_urls["psql"]["password"],
host=source_cluster_urls["psql"]["host"],
port=int(source_cluster_urls["psql"]["port"]),
database=source_cluster_urls["psql"]["database"],
)
conn.autocommit = True
unload_queries = {}
table_name = ""
query_text = ""
for line in open(unload_system_table_queries_file, "r"):
if line.startswith("--"):
unload_queries[table_name] = query_text.strip("\n")
table_name = line[2:].strip("\n")
query_text = ""
else:
query_text += line
unload_queries[table_name] = query_text.strip("\n")
del unload_queries[""]
cursor = conn.cursor()
for table_name, unload_query in unload_queries.items():
if table_name and unload_query:
unload_query = re.sub(
r"to ''",
f"TO '{unload_location}/system_tables/{table_name}/'",
unload_query,
flags=re.IGNORECASE,
)
unload_query = re.sub(
r"credentials ''",
f"CREDENTIALS 'aws_iam_role={unload_iam_role}'",
unload_query,
flags=re.IGNORECASE,
)
cursor.execute(unload_query)
logger.debug(f"Executed unload query: {unload_query}")
def validate_config_file(config_file):
if config_file["source_cluster_endpoint"]:
if (
not len(config_file["source_cluster_endpoint"].split(".")) == 6
or not len(config_file["source_cluster_endpoint"].split(":")) == 2
or not len(config_file["source_cluster_endpoint"].split("/")) == 2
or not ".redshift.amazonaws.com:" in config_file["source_cluster_endpoint"]
):
logger.error(
'Config file value for "source_cluster_endpoint" is not a valid endpoint. Endpoints must be in the format of <cluster-name>.<identifier>.<region>.redshift.amazonaws.com:<port>/<database-name>.'
)
exit(-1)
if not config_file["master_username"]:
logger.error(
'Config file missing value for "master_username". Please provide a value or remove the "source_cluster_endpoint" value.'
)
exit(-1)
else:
if not config_file["log_location"]:
logger.error(
'Config file missing value for "log_location". Please provide a value for "log_location", or provide a value for "source_cluster_endpoint".'
)
exit(-1)
if config_file["start_time"]: