Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
wayyoungboy committed Aug 14, 2024
1 parent 3d36734 commit 8fc9a51
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 68 deletions.
17 changes: 16 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,22 @@ def get_node_config(self, type, node_ip, config_item):

class InnerConfigManager(Manager):

def __init__(self, stdio=None):
def __init__(self, stdio=None, inner_config_change_map=None):
if inner_config_change_map is None:
inner_config_change_map = {}
inner_config_abs_path = os.path.abspath(INNER_CONFIG_FILE)
super().__init__(inner_config_abs_path, stdio=stdio)
self.config = self.load_config_with_defaults(DEFAULT_INNER_CONFIG)
if inner_config_change_map != {}:
self.config = self._change_inner_config(self.config, inner_config_change_map)

def _change_inner_config(self, conf_map, change_conf_map):
for key, value in change_conf_map.items():
if key in conf_map:
if isinstance(value, dict):
self._change_inner_config(conf_map[key], value)
else:
conf_map[key] = value
else:
conf_map[key] = value
return conf_map
45 changes: 25 additions & 20 deletions core.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from handler.gather.gather_tabledump import GatherTableDumpHandler
from handler.gather.gather_parameters import GatherParametersHandler
from handler.gather.gather_variables import GatherVariablesHandler
from result_type import ObdiagResult
from telemetry.telemetry import telemetry
from update.update import UpdateHandler
from colorama import Fore, Style
Expand Down Expand Up @@ -200,7 +201,7 @@ def gather_function(self, function_type, opt):
config = self.config_manager
if not config:
self._call_stdio('error', 'No such custum config')
return False
return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config')
else:
self.stdio.print("{0} start ...".format(function_type))
self.set_context(function_type, 'gather', config)
Expand Down Expand Up @@ -267,7 +268,7 @@ def gather_obproxy_log(self, opt):
config = self.config_manager
if not config:
self._call_stdio('error', 'No such custum config')
return False
return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config')
else:
self.set_context_skip_cluster_conn('gather_obproxy_log', 'gather', config)
handler = GatherObProxyLogHandler(self.context)
Expand All @@ -282,33 +283,34 @@ def analyze_fuction(self, function_type, opt):
config = self.config_manager
if not config:
self._call_stdio('error', 'No such custum config')
return False
return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config')
else:
self.stdio.print("{0} start ...".format(function_type))
if function_type == 'analyze_log':
self.set_context(function_type, 'analyze', config)
handler = AnalyzeLogHandler(self.context)
handler.handle()
return handler.handle()
elif function_type == 'analyze_log_offline':
self.set_context_skip_cluster_conn(function_type, 'analyze', config)
handler = AnalyzeLogHandler(self.context)
handler.handle()
return handler.handle()
elif function_type == 'analyze_flt_trace':
self.set_context(function_type, 'analyze', config)
handler = AnalyzeFltTraceHandler(self.context)
handler.handle()
return handler.handle()
elif function_type == 'analyze_parameter_default':
self.set_context(function_type, 'analyze', config)
handler = AnalyzeParameterHandler(self.context, 'default')
handler.handle()
return handler.handle()
elif function_type == 'analyze_parameter_diff':
self.set_context_skip_cluster_conn(function_type, 'analyze', config)
handler = AnalyzeParameterHandler(self.context, 'diff')
handler.handle()
return handler.handle()
elif function_type == 'analyze_variable_diff':
self.set_context(function_type, 'analyze', config)
handler = AnalyzeVariableHandler(self.context, 'diff')
handler.handle()
return handler.handle()
# todo not support silent
elif function_type == 'analyze_sql':
self.set_context(function_type, 'analyze', config)
handler = AnalyzeSQLHandler(self.context)
Expand All @@ -319,13 +321,13 @@ def analyze_fuction(self, function_type, opt):
handler.handle()
else:
self._call_stdio('error', 'Not support analyze function: {0}'.format(function_type))
return False
return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='Not support analyze function: {0}'.format(function_type))

def check(self, opts):
config = self.config_manager
if not config:
self._call_stdio('error', 'No such custum config')
return False
return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config')
else:
self.stdio.print("check start ...")
self.set_context('check', 'check', config)
Expand All @@ -347,59 +349,62 @@ def check(self, opts):
observer_report_path = os.path.expanduser(observer_check_handler.report.get_report_path())
if os.path.exists(observer_report_path):
self.stdio.print("Check observer finished. For more details, please run cmd'" + Fore.YELLOW + " cat {0} ".format(observer_check_handler.report.get_report_path()) + Style.RESET_ALL + "'")
return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={})

def check_list(self, opts):
config = self.config_manager
if not config:
self._call_stdio('error', 'No such custum config')
return False
return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config')
else:
self.set_offline_context('check_list', 'check_list')
handler = CheckListHandler(self.context)
handler.handle()
return handler.handle()

def rca_run(self, opts):
config = self.config_manager
if not config:
self._call_stdio('error', 'No such custum config')
return False
return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config')
else:
self.set_context('rca_run', 'rca_run', config)
try:
handler = RCAHandler(self.context)
handler.handle()
handler.execute()
return handler.execute()
except Exception as e:
self.stdio.error("rca run Exception: {0}".format(e))
self.stdio.verbose(traceback.format_exc())
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="rca run Exception: {0}".format(e))

def rca_list(self, opts):
config = self.config_manager
if not config:
self._call_stdio('error', 'No such custum config')
return False
return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config')
else:
self.set_offline_context('rca_list', 'rca_list')
handler = RcaScenesListHandler(context=self.context)
handler.handle()
return handler.handle()

def update(self, opts):
config = self.config_manager
if not config:
self._call_stdio('error', 'No such custum config')
return False
return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config')
else:
self.stdio.print("update start ...")
self.set_offline_context('update', 'update')
handler = UpdateHandler(self.context)
handler.execute()
return handler.execute()

def config(self, opt):
config = self.config_manager
if not config:
self._call_stdio('error', 'No such custum config')
return False
return ObdiagResult(ObdiagResult.INPUT_ERROR_CODE, error_data='No such custum config')
else:
self.set_offline_context('config', 'config')
config_helper = ConfigHelper(context=self.context)
config_helper.build_configuration()
return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"msg": "config success"})
12 changes: 9 additions & 3 deletions diag_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,19 @@ def do_command(self):
obdiag.set_cmds(self.cmds)
ret = self._do_command(obdiag)
if isinstance(ret, ObdiagResult) is False:
ObdiagResult(code=ObdiagResult.SERVER_ERROR_CODE, data={"err_info": "The return value of the command is not ObdiagResult. Please contact thebase community."})
return
ROOT_IO.error('The return value of the command is not ObdiagResult. Please contact thebase community. The return value is: {0}'.format(ret))
ret = ObdiagResult(code=ObdiagResult.SERVER_ERROR_CODE, error_data={"err_info": "The return value of the command is not ObdiagResult. Please contact thebase community."})
ret.set_trace_id(self.trace_id)
telemetry.put_data()
ret.set_command(self.prev_cmd)
# if silent is true ,print ret
if ROOT_IO.silent:
ROOT_IO.set_silent(False)
ROOT_IO.print(ret.get_result())
ROOT_IO.set_silent(True)
if self.has_trace:
ROOT_IO.print('Trace ID: %s' % self.trace_id)
ROOT_IO.print('If you want to view detailed obdiag logs, please run: {0} display-trace {1}'.format(obdiag_bin, self.trace_id))
telemetry.put_data()
except NotImplementedError:
ROOT_IO.exception('command \'%s\' is not implemented' % self.prev_cmd)
except SystemExit:
Expand Down
13 changes: 9 additions & 4 deletions handler/analyzer/analyze_flt_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from common.tool import Util
from common.tool import DirectoryUtil
from common.tool import FileUtil
from result_type import ObdiagResult


class AnalyzeFltTraceHandler(object):
Expand Down Expand Up @@ -86,10 +87,10 @@ def init_option(self):
def handle(self):
if not self.init_option():
self.stdio.error('init option failed')
return False
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data='init option failed')
if not self.init_config():
self.stdio.error('init config failed')
return False
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data='init config failed')
local_store_parent_dir = os.path.join(self.gather_pack_dir, "obdiag_analyze_flt_result_{0}".format(TimeUtils.timestamp_to_filename_time(self.gather_timestamp)))
self.stdio.verbose("Use {0} as pack dir.".format(local_store_parent_dir))
analyze_tuples = []
Expand Down Expand Up @@ -119,8 +120,7 @@ def handle_from_node(node):
data = future.result()
tree.build(data)
# output tree
self.__output(local_store_parent_dir, tree, self.output)
return analyze_tuples
return self.__output(local_store_parent_dir, tree, self.output)

def __handle_from_node(self, node, old_files, local_store_parent_dir):
resp = {"skip": False, "error": ""}
Expand Down Expand Up @@ -346,6 +346,11 @@ def __output(self, result_dir, tree, output_terminal=60):
self.stdio.verbose('Result saved: {}'.format(os.path.abspath(filename)))
last_info = "For more details, please run cmd \033[32m' cat {0} '\033[0m\n".format(filename)
self.stdio.print(last_info)
result_info = ""
with open(filename, 'r', encoding='utf-8') as f:
for line in f:
result_info += line
return result_info

def parse_file(self, file):
self.stdio.verbose('parse file: {}'.format(file[1]))
Expand Down
11 changes: 8 additions & 3 deletions handler/analyzer/analyze_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from common.tool import FileUtil
from common.tool import TimeUtils
import common.ssh_client.local_client as ssh_client_local_client
from result_type import ObdiagResult


class AnalyzeLogHandler(BaseShellHandler):
Expand Down Expand Up @@ -125,10 +126,10 @@ def init_option(self):
def handle(self):
if not self.init_option():
self.stdio.error('init option failed')
return False
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init option failed")
if not self.init_config():
self.stdio.error('init config failed')
return False
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init config failed")
local_store_parent_dir = os.path.join(self.gather_pack_dir, "obdiag_analyze_pack_{0}".format(TimeUtils.timestamp_to_filename_time(TimeUtils.get_current_us_timestamp())))
self.stdio.verbose("Use {0} as pack dir.".format(local_store_parent_dir))
analyze_tuples = []
Expand Down Expand Up @@ -160,7 +161,11 @@ def handle_from_node(node):
FileUtil.write_append(os.path.join(local_store_parent_dir, "result_details.txt"), field_names[n] + ": " + str(summary_details_list[m][n]) + extend)
last_info = "For more details, please run cmd \033[32m' cat {0} '\033[0m\n".format(os.path.join(local_store_parent_dir, "result_details.txt"))
self.stdio.print(last_info)
return analyze_tuples
# get info from local_store_parent_dir+/result_details.txt
analyze_info = ""
with open(os.path.join(local_store_parent_dir, "result_details.txt"), "r", encoding="utf-8") as f:
analyze_info = f.read()
return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"result": analyze_info})

def __handle_from_node(self, node, local_store_parent_dir):
resp = {"skip": False, "error": ""}
Expand Down
19 changes: 13 additions & 6 deletions handler/analyzer/analyze_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import datetime
from colorama import Fore, Style

from result_type import ObdiagResult


class AnalyzeParameterHandler(object):
def __init__(self, context, analyze_type='default'):
Expand Down Expand Up @@ -67,14 +69,14 @@ def handle(self):
if self.analyze_type == 'default':
if not self.init_option_default():
self.stdio.error('init option failed')
return False
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init option failed")
else:
if not self.init_option_diff():
self.stdio.error('init option failed')
return False
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init option failed")
self.stdio.verbose("Use {0} as pack dir.".format(self.export_report_path))
DirectoryUtil.mkdir(path=self.export_report_path, stdio=self.stdio)
self.execute()
return self.execute()

def check_file_valid(self):
with open(self.parameter_file_name, 'r') as f:
Expand Down Expand Up @@ -167,10 +169,11 @@ def analyze_parameter_default(self):
fp.write(report_default_tb.get_string() + "\n")
self.stdio.print(report_default_tb.get_string())
self.stdio.print("Analyze parameter default finished. For more details, please run cmd '" + Fore.YELLOW + " cat {0} ".format(file_name) + Style.RESET_ALL + "'")
return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"result": report_default_tb.get_string(), "file_name": file_name})
else:
if self.parameter_file_name is None:
self.stdio.error("the version of OceanBase is lower than 4.2.2, an initialization parameter file must be provided to find non-default values")
return
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="the version of OceanBase is lower than 4.2.2, an initialization parameter file must be provided to find non-default values")
else:
sql = '''select substr(version(),8), svr_ip,svr_port,zone,scope,TENANT_ID,name,value,section,
EDIT_LEVEL, now(),'','' from GV$OB_PARAMETERS order by 5,2,3,4,7'''
Expand Down Expand Up @@ -262,6 +265,7 @@ def alalyze_parameter_diff(self):
file_name = self.export_report_path + '/parameter_diff_{0}.table'.format(date_format)
fp = open(file_name, 'a+', encoding="utf8")
is_empty = True
report_diff_tbs = []
for tenant, value_list in diff_parameter_dict.items():
if len(value_list) > 0:
report_diff_tb = PrettyTable(["name", "diff"])
Expand All @@ -279,17 +283,20 @@ def alalyze_parameter_diff(self):
fp.write(report_diff_tb.get_string() + "\n")
self.stdio.print(report_diff_tb.get_string())
is_empty = False
report_diff_tbs.append(report_diff_tb.get_string())
fp.close()
if not is_empty:
self.stdio.print("Analyze parameter diff finished. For more details, please run cmd '" + Fore.YELLOW + " cat {0} ".format(file_name) + Style.RESET_ALL + "'")
return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"result": report_diff_tbs, "file_name": file_name})
else:
self.stdio.print("Analyze parameter diff finished. All parameter settings are consistent among observers")
return ObdiagResult(ObdiagResult.SUCCESS_CODE, data={"result": "Analyze parameter diff finished. All parameter settings are consistent among observers"})

def execute(self):
try:
if self.analyze_type == 'default':
self.analyze_parameter_default()
return self.analyze_parameter_default()
elif self.analyze_type == 'diff':
self.alalyze_parameter_diff()
return self.alalyze_parameter_diff()
except Exception as e:
self.stdio.error("parameter info analyze failed, error message: {0}".format(e))
9 changes: 5 additions & 4 deletions handler/analyzer/analyze_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from handler.analyzer.sql.meta.sys_tenant_meta import SysTenantMeta
from handler.gather.gather_scenes import GatherSceneHandler
from common.command import get_observer_version
from result_type import ObdiagResult


class AnalyzeSQLHandler(object):
Expand Down Expand Up @@ -208,16 +209,16 @@ def handle(self):
self.start_time = time.time()
if not self.init_option():
self.stdio.error('init option failed')
return False
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init option failed")
if not self.init_inner_config():
self.stdio.error('init inner config failed')
return False
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init inner config failed")
if not self.init_config():
self.stdio.error('init config failed')
return False
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init config failed")
if not self.init_ob_version():
self.stdio.error('init ob version failed')
return False
return ObdiagResult(ObdiagResult.SERVER_ERROR_CODE, error_data="init ob version failed")
self.init_db_connector()
self.local_store_path = os.path.join(self.local_stored_parrent_path, "obdiag_analyze_sql_result_{0}_{1}.html".format(TimeUtils.timestamp_to_filename_time(self.from_timestamp), TimeUtils.timestamp_to_filename_time(self.to_timestamp)))
self.stdio.print("use {0} as result store path.".format(self.local_store_path))
Expand Down
Loading

0 comments on commit 8fc9a51

Please sign in to comment.