From 513afa1e45b0c4d640b51dd2f40b937849597ab1 Mon Sep 17 00:00:00 2001 From: zzh Date: Tue, 29 Oct 2024 11:48:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8C=87=E5=AE=9A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/gs_checkse | 36 +++++++++---- script/gspylib/common/Common.py | 1 + script/gspylib/common/ErrorCode.py | 4 +- script/gspylib/common/ParameterParsecheck.py | 3 +- script/local/LocalCheckSE.py | 57 ++++++++++++-------- 5 files changed, 65 insertions(+), 36 deletions(-) diff --git a/script/gs_checkse b/script/gs_checkse index 81f3737..7fa2b98 100644 --- a/script/gs_checkse +++ b/script/gs_checkse @@ -116,18 +116,19 @@ class CmdOptions(): """ def __init__(self): - self.hostnamestr = "" - self.itemstr = "" - self.hostlistfile = "" + self.hostnamestr = DefaultValue.EMPTY_STR + self.itemstr = DefaultValue.EMPTY_STR + self.hostlistfile = DefaultValue.EMPTY_STR self.hostnameList = [] - self.outputfile = "" - self.logFile = "" - self.localLog = "" + self.outputfile = DefaultValue.EMPTY_STR + self.logFile = DefaultValue.EMPTY_STR + self.localLog = DefaultValue.EMPTY_STR + self.database = DefaultValue.EMPTY_STR self.set = False self.detail = False self.detail_all = False self.item_detail = [] - self.confFile = "" + self.confFile = DefaultValue.EMPTY_STR self.localMode = False self.skip_cgroup_set = False @@ -172,6 +173,7 @@ General options: -h Name of the host to connect to. -X Configuration file of the cluster. --detail Show detailed information. + --database Specify the database to check. -o Save the result to the specified file. -l Path of log file. -? --help Show help information for this utility, and exit the command line mode. @@ -274,6 +276,8 @@ def parseCommandLine(): g_opts.itemstr = ParaDict.get("itemstr") if (ParaDict.__contains__("show_detail")): g_opts.detail = ParaDict.get("show_detail") + if (ParaDict.__contains__("database")): + g_opts.database = ParaDict.get("database") def getUserInfo(): @@ -386,6 +390,14 @@ def setLogFile(): UserUtil.check_path_owner(g_opts.logFile) +def setDatabase(): + """ + Set database name + """ + if(g_opts.database == DefaultValue.EMPTY_STR): + g_opts.database = DefaultValue.DEFAULT_DB_NAME + + def checkItems(): """ """ @@ -423,6 +435,8 @@ def checkParameter(): ############################################ setLogFile() ########################################## + setDatabase() + ########################################## # set items ############################################ checkItems() @@ -522,11 +536,11 @@ def performCheckorSetSE(action_item, flag_str, success_mesg, item_num, if command == "": command = Local_CheckSe if (configFile != ""): - cmd = "%s -t %s -X '%s' -l '%s' %s" % ( - command, action_item, configFile, g_opts.localLog, parameters) + cmd = "%s -t %s -X '%s' -l '%s' --database=%s %s" % ( + command, action_item, configFile, g_opts.localLog, g_opts.database, parameters) else: - cmd = "%s -t %s -l '%s' %s" % ( - command, action_item, g_opts.localLog, parameters) + cmd = "%s -t %s -l '%s' --database=%s %s" % ( + command, action_item, g_opts.localLog, g_opts.database, parameters) (status, output, outputMap) = getCmdOutput(cmd) parRes = "" detail_msg = "" diff --git a/script/gspylib/common/Common.py b/script/gspylib/common/Common.py index 04076f8..80ad622 100644 --- a/script/gspylib/common/Common.py +++ b/script/gspylib/common/Common.py @@ -244,6 +244,7 @@ class DefaultValue(): CAP_WIO = "CAP_SYS_RAWIO" CAP_ADM = "CAP_SYS_ADMIN" + EMPTY_STR = "" # env parameter MPPRC_FILE_ENV = "MPPDB_ENV_SEPARATE_PATH" SUCCESS = "Success" diff --git a/script/gspylib/common/ErrorCode.py b/script/gspylib/common/ErrorCode.py index 3c1a425..a2a413b 100644 --- a/script/gspylib/common/ErrorCode.py +++ b/script/gspylib/common/ErrorCode.py @@ -291,7 +291,9 @@ class ErrorCode(): 'GAUSS_50501': "[GAUSS-50501] : Shared_buffers must be less than " "shmmax. Please check it.", 'GAUSS_50502': "[GAUSS-50502] : Failed to obtain %s information.", - 'GAUSS_50503': "[GAUSS-50503] : %s, program exists abnormally." + 'GAUSS_50503': "[GAUSS-50503] : %s, program exists abnormally.", + 'GAUSS_50504': "[GAUSS-50504] : Failed to execute the command: %s.\n" + "ERROR: %s" } ########################################################################### diff --git a/script/gspylib/common/ParameterParsecheck.py b/script/gspylib/common/ParameterParsecheck.py index 0e98d87..b7c299c 100644 --- a/script/gspylib/common/ParameterParsecheck.py +++ b/script/gspylib/common/ParameterParsecheck.py @@ -98,7 +98,7 @@ gs_checkos = ["-?", "--help", "-V", "--version", "-h:", "-f:", "-o:", "-i:", "--detail", "-l:", "-X:"] gs_checkse = ["-?", "--help", "-V", "--version", "-h:", "-f:", "-o:", - "-i:", "--detail", + "-i:", "--detail", "--database=", "-l:", "-X:"] gs_expansion = ["-?", "--help", "-V", "--version", "-U:", "-G:", "-L", "-X:", "-h:", "--sep-env-file=", "--time-out="] @@ -346,6 +346,7 @@ class Parameter(): "-p": "port", "--dn-port": "dn-port", "--dn-ip": "dn-ip", + "--database": "database", "--interval": "interval", "--threshold": "threshold", "--check-count": "check_count", diff --git a/script/local/LocalCheckSE.py b/script/local/LocalCheckSE.py index 11760df..1f5c1da 100644 --- a/script/local/LocalCheckSE.py +++ b/script/local/LocalCheckSE.py @@ -176,11 +176,11 @@ def getDatabaseInfo(data, sql_query): output : Instantion """ port = int(getValueFromFile('port')) - cmd = f"gsql -d postgres -p '{port}' -r -c \"{sql_query}\"" + database = g_opts.database + cmd = f"gsql -d {database} -p '{port}' -r -c \"{sql_query}\"" status, output = subprocess.getstatusoutput(cmd) if status != 0: - raise Exception((ErrorCode.GAUSS_505["GAUSS_50502"] % "ConnectionConfiguration") + - ("The cmd is : %s" % cmd)) + raise Exception(ErrorCode.GAUSS_505["GAUSS_50504"] % (cmd, output)) if "ERROR:" in output: raise Exception(ErrorCode.GAUSS_513["GAUSS_51300"] % output) value = extractRowsCount(output) @@ -3556,27 +3556,34 @@ def setAdminPrivileges(data): result = AdminPrivileges() result.db = [] try: - for item in data[0].db: - sql_query = """ALTER ROLE %s NOCREATEROLE;""" %(item.split("|")[0].strip()) - getDatabaseInfo(result, sql_query) - for item in data[1].db: - sql_query = """ALTER ROLE %s NOCREATEDB;""" %(item.split("|")[0].strip()) - getDatabaseInfo(result, sql_query) - for item in data[2].db: - sql_query = """ALTER ROLE %s NOAUDITADMIN;""" %(item.split("|")[0].strip()) - getDatabaseInfo(result, sql_query) - for item in data[3].db: - sql_query = """ALTER ROLE %s NOMONADMIN;""" %(item.split("|")[0].strip()) - getDatabaseInfo(result, sql_query) - for item in data[4].db: - sql_query = """ALTER ROLE %s NOOPRADMIN;""" %(item.split("|")[0].strip()) - getDatabaseInfo(result, sql_query) - for item in data[5].db: - sql_query = """ALTER ROLE %s NOPOLADMIN;""" %(item.split("|")[0].strip()) - getDatabaseInfo(result, sql_query) + alter_role(data, result) except Exception as e: + data = AdminPrivileges() data.errormsg = e.__str__() +def alter_role(data, result): + """ + function : Alter role + input : Instantion, Instantion + output : NA + """ + role_actions = [ + ("NOCREATEROLE", 0), + ("NOCREATEDB", 1), + ("NOAUDITADMIN", 2), + ("NOMONADMIN", 3), + ("NOOPRADMIN", 4), + ("NOPOLADMIN", 5) + ] + + for action, index in role_actions: + for item in data[index].db: + role_name = item.split("|")[0].strip() + if not role_name: + continue + sql_query = f"ALTER ROLE {role_name} {action};" + getDatabaseInfo(result, sql_query) + def setEnableSeparationOfDuty(data): """ function : Set Enable Separation Of Duty @@ -4744,6 +4751,7 @@ class CmdOptions(): self.user = "" self.extrachecklist = [] self.logFile = "" + self.database = "" self.confFile = "" self.mtuValue = "" self.hostname = "" @@ -4796,7 +4804,7 @@ def usage(): """ Usage: python3 --help | -? - python3 LocalCheckSE -t action [-l logfile] [-X xmlfile] [-V] + python3 LocalCheckSE -t action [-l logfile] [-X xmlfile] [-V] [--database=database] Common options: -t The type of action. -s the path of MPPDB file @@ -4804,6 +4812,7 @@ Common options: -? --help Show this help screen. -X --xmlfile = xmlfile Cluster config file --ntp-server NTP server node's IP. + --database=database Specify the database to check. -V --version """ print(usage.__doc__) @@ -4818,7 +4827,7 @@ def parseCommandLine(): try: opts, args = getopt.getopt(sys.argv[1:], "t:s:l:X:V?", ["help", "log-file=", "xmlfile=", - "MTUvalue=", "hostname=", + "MTUvalue=", "hostname=", "database=", "ntp-server=", "version"]) except Exception as e: usage() @@ -4852,6 +4861,8 @@ def parseCommandLine(): g_opts.mtuValue = value elif (key == "--hostname"): g_opts.hostname = value + elif (key == "--database"): + g_opts.database = value Parameter.checkParaVaild(key, value)