-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support redact * support redact * support redact * build test package * build test package * build test package * build test package * build test package * build test package * delete test package * delete test package * delete test package * format
- Loading branch information
1 parent
c36da7e
commit 5c09dff
Showing
10 changed files
with
252 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: UTF-8 -* | ||
# Copyright (c) 2022 OceanBase | ||
# OceanBase Diagnostic Tool is licensed under Mulan PSL v2. | ||
# You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
# You may obtain a copy of Mulan PSL v2 at: | ||
# http://license.coscl.org.cn/MulanPSL2 | ||
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
# See the Mulan PSL v2 for more details. | ||
|
||
""" | ||
@time: 2024/09/19 | ||
@file: import_modules.py | ||
@desc: | ||
""" | ||
import os | ||
|
||
from common.tool import DynamicLoading | ||
|
||
|
||
class ImportModulesException(Exception): | ||
pass | ||
|
||
|
||
# 实现模块导入,要求module_name为模块名和需要导入的对象名,module_file_path为模块文件路径 | ||
|
||
|
||
def import_modules(module_file_dir, stdio): | ||
stdio.verbose("import_modules input: module_file_dir->{0}".format(module_file_dir)) | ||
try: | ||
module_files = [] | ||
module_list = {} | ||
for root, dirs, files in os.walk(module_file_dir): | ||
if root == module_file_dir: | ||
module_files = files | ||
for module_file in module_files: | ||
module_name = os.path.basename(module_file)[:-3] | ||
DynamicLoading.add_lib_path(module_file_dir) | ||
module = DynamicLoading.import_module(os.path.basename(module_file)[:-3], None) | ||
if not hasattr(module, module_name): | ||
stdio.error("{0} import_module failed".format(module_name)) | ||
continue | ||
module_list[module_name] = getattr(module, module_name) | ||
return module_list | ||
except Exception as e: | ||
stdio.error("import_modules failed: {0}".format(e)) | ||
raise ImportModulesException("import_modules failed: {0}".format(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: UTF-8 -* | ||
# Copyright (c) 2022 OceanBase | ||
# OceanBase Diagnostic Tool is licensed under Mulan PSL v2. | ||
# You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
# You may obtain a copy of Mulan PSL v2 at: | ||
# http://license.coscl.org.cn/MulanPSL2 | ||
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
# See the Mulan PSL v2 for more details. | ||
|
||
""" | ||
@time: 2024/09/18 | ||
@file: __init__.py | ||
@desc: | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import os | ||
import shutil | ||
import zipfile | ||
|
||
from common.import_module import import_modules | ||
import multiprocessing as mp | ||
|
||
|
||
class Redact: | ||
def __init__(self, context, input_file_dir, output_file_dir): | ||
self.context = context | ||
self.stdio = context.stdio | ||
self.redacts = {} | ||
self.input_file_dir = input_file_dir | ||
self.output_file_dir = output_file_dir | ||
self.module_dir = os.path.expanduser('~/.obdiag/gather/redact') | ||
self.inner_config = self.context.inner_config | ||
|
||
# init all redact | ||
# import all redact module | ||
self.all_redact = [] | ||
try: | ||
self.stdio.print("Importing redact modules...") | ||
self.all_redact = import_modules(self.module_dir, self.stdio) | ||
self.stdio.verbose("Imported redact module {0}".format(self.all_redact)) | ||
except Exception as e: | ||
self.stdio.error(f"Error importing redact modules: {e}") | ||
raise e | ||
|
||
def check_redact(self, input_redacts): | ||
for input_redact in input_redacts: | ||
if not input_redact in self.all_redact: | ||
self.stdio.error("Redact {0} not found".format(input_redact)) | ||
raise Exception(f"Redact {input_redact} not found") | ||
else: | ||
self.stdio.verbose(f"Redact {input_redact} found") | ||
self.redacts[input_redact] = self.all_redact[input_redact] | ||
|
||
def redact_files(self, input_redacts): | ||
self.stdio.verbose("redact_files start") | ||
self.check_redact(input_redacts) | ||
# check self.redacts | ||
if not self.redacts or len(self.redacts) == 0: | ||
self.stdio.error("No redact found") | ||
return False | ||
# create dir to save the files after redact | ||
if not os.path.exists(self.output_file_dir): | ||
os.makedirs(self.output_file_dir) | ||
# use threading to redact the files | ||
files_name = os.listdir(self.input_file_dir) | ||
self.stdio.verbose(files_name) | ||
# unzip the log file | ||
for zip_file in files_name: | ||
if ".zip" in zip_file: | ||
self.stdio.verbose("open zip file: {0}".format(os.path.join(self.input_file_dir, zip_file))) | ||
with zipfile.ZipFile(os.path.join(self.input_file_dir, zip_file), 'r') as zip_ref: | ||
# Extract all files to the current directory | ||
zip_ref.extractall(self.input_file_dir) | ||
gather_log_files = [] | ||
for file_name in os.listdir(self.input_file_dir): | ||
if "zip" not in file_name and "result_summary.txt" not in file_name: | ||
log_dir = os.path.join(self.input_file_dir, file_name) | ||
for log_file in os.listdir(log_dir): | ||
gather_log_files.append(os.path.join(log_dir, log_file)) | ||
self.stdio.verbose("result_log_files add {0}".format(os.path.join(log_dir, log_file))) | ||
file_queue = [] | ||
max_processes = int(self.inner_config.get('gather').get('redact_processing_num')) or 3 | ||
self.stdio.verbose("max_processes: {0}".format(max_processes)) | ||
semaphore = mp.Semaphore(max_processes) | ||
for file_name in gather_log_files: | ||
if "result_summary.txt" in file_name: | ||
continue | ||
self.stdio.verbose("inport file name: {0}".format(file_name)) | ||
self.stdio.verbose("output file name: {0}".format(file_name.replace(self.input_file_dir, self.output_file_dir))) | ||
semaphore.acquire() | ||
file_thread = mp.Process(target=self.redact_file, args=(file_name, file_name.replace(self.input_file_dir, self.output_file_dir), semaphore)) | ||
file_thread.start() | ||
file_queue.append(file_thread) | ||
for file_thread in file_queue: | ||
file_thread.join() | ||
# zip the dir by node | ||
subfolders = [f for f in os.listdir(self.output_file_dir) if os.path.isdir(os.path.join(self.output_file_dir, f))] | ||
for subfolder in subfolders: | ||
subfolder_path = os.path.join(self.output_file_dir, subfolder) | ||
zip_filename = os.path.join(self.output_file_dir, f"{subfolder}.zip") | ||
with zipfile.ZipFile(zip_filename, 'w') as zipf: | ||
for root, dirs, files in os.walk(subfolder_path): | ||
for file in files: | ||
file_path = os.path.join(root, file) | ||
zipf.write(file_path, os.path.relpath(file_path, subfolder_path)) | ||
self.stdio.verbose("delete the dir: {0}".format(subfolder_path)) | ||
shutil.rmtree(subfolder_path) | ||
self.stdio.print(f"{subfolder} is zipped on {zip_filename}") | ||
return True | ||
|
||
def redact_file(self, input_file, output_file, semaphore): | ||
try: | ||
input_file = os.path.abspath(input_file) | ||
output_file = os.path.abspath(output_file) | ||
dir_path = os.path.dirname(output_file) | ||
log_content = "" | ||
if not os.path.exists(dir_path): | ||
os.makedirs(dir_path) | ||
with open(input_file, 'r', encoding='utf-8', errors='ignore') as file: | ||
log_content = file.read() | ||
for redact in self.redacts: | ||
log_content = self.redacts[redact].redact(log_content) | ||
with open(output_file, 'w', encoding='utf-8', errors='ignore') as file: | ||
file.write(log_content) | ||
except Exception as e: | ||
self.stdio.error(f"Error redact file {input_file}: {e}") | ||
finally: | ||
semaphore.release() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: UTF-8 -* | ||
# Copyright (c) 2022 OceanBase | ||
# OceanBase Diagnostic Tool is licensed under Mulan PSL v2. | ||
# You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
# You may obtain a copy of Mulan PSL v2 at: | ||
# http://license.coscl.org.cn/MulanPSL2 | ||
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
# See the Mulan PSL v2 for more details. | ||
|
||
""" | ||
@time: 2024/09/19 | ||
@file: all_sql.py | ||
@desc: | ||
""" | ||
import re | ||
|
||
|
||
class all_sql: | ||
def __init__(self): | ||
pass | ||
|
||
def redact(self, text): | ||
patterns = [ | ||
(r'stmt:"(.*?[^\\])", stmt_len', 'stmt:"<SQL_QUERY_REDACTED>", stmt_len'), | ||
(r'ps_sql:"(.*?[^\\])", is_expired_evicted', 'ps_sql:"<SQL_QUERY_REDACTED>", is_expired_evicted'), | ||
(r'ps_sql:"(.*?[^\\])", ref_count:', 'ps_sql:"<SQL_QUERY_REDACTED>", ref_count:'), | ||
(r'origin_sql=(.*?[^\\]), ps_stmt_checksum', 'origin_sql=<SQL_QUERY_REDACTED>, ps_stmt_checksum'), | ||
(r'get_sql_stmt\(\)=(.*?[^\\]), route_sql_=', 'get_sql_stmt()=<SQL_QUERY_REDACTED>, route_sql_='), | ||
(r'multi_stmt_item={(.*?[^\\])\}', 'multi_stmt_item={<SQL_QUERY_REDACTED>}'), | ||
] | ||
log_content = text | ||
# 遍历所有模式并进行替换 | ||
for pattern, replacement in patterns: | ||
log_content = re.sub(pattern, replacement, text, flags=re.DOTALL) | ||
return log_content | ||
|
||
|
||
all_sql = all_sql() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters