def doCheck(self): security_mode_value = DefaultValue.getSecurityMode() if (security_mode_value == "on"): secMode = True else: secMode = False if (self.cluster.isSingleInstCluster()): if (secMode): finalresult = "" dbList = [] sqlList = [] sqlPath = os.path.realpath( os.path.join(os.path.split(os.path.realpath(__file__))[0], "../../lib/checkblacklist/")) sqlFileName = os.path.join(sqlPath, "GetTableSkew.sql") if (os.path.exists(sqlFileName)): try: with open(sqlFileName, "r") as fp: lines = fp.read() sqlList = lines.split("--sqlblock") sqlList.pop() except Exception as e: raise Exception(ErrorCode.GAUSS_502["GAUSS_50204"] % ("file:%s,Error:%s" % (sqlFileName, str(e)))) else: raise Exception(ErrorCode.GAUSS_502["GAUSS_50219"] % ("sql file:%s" % sqlFileName)) sqldb = "select datname from pg_database;" (status, result, error) = ClusterCommand.excuteSqlOnLocalhost( self.port, sqldb) if (status != 2): raise Exception(ErrorCode.GAUSS_513["GAUSS_51300"] % sqldb + (" Error:%s" % error)) recordsCount = len(result) for i in range(0, recordsCount): dbList.append(result[i][0]) dbList.remove("template0") dbList.remove("template1") for db in dbList: schemaTable = [] for sql in sqlList: sql = "set client_min_messages='error';\n" + sql ClusterCommand.excuteSqlOnLocalhost(self.port, sql, db) sql = "SELECT schemaname , tablename FROM " \ "PUBLIC.pgxc_analyzed_skewness WHERE " \ "skewness_tuple > 100000;" (status, result, error) = ClusterCommand.excuteSqlOnLocalhost(self.port, sql, db) if (status != 2): raise Exception(ErrorCode.GAUSS_513["GAUSS_51300"] % sql + (" Error:%s" % error)) else: for i in iter(result): schema = i[0] table = i[1] schemaTable.append("%s.%s" % (schema, table)) if (schemaTable): finalresult += "%s:\n%s\n" % ( db, "\n".join(schemaTable)) if (finalresult): self.result.rst = ResultStatus.WARNING self.result.val = "The result is not ok:\n%s" % finalresult else: self.result.rst = ResultStatus.OK self.result.val = "Data is well distributed" else: finalresult = "" sqlPath = os.path.realpath( os.path.join(os.path.split(os.path.realpath(__file__))[0], "../../lib/checkblacklist/")) sqlFileName = os.path.join(sqlPath, "GetTableSkew.sql") sqldb = "select datname from pg_database;" output = SharedFuncs.runSqlCmd(sqldb, self.user, "", self.port, self.tmpPath, "postgres", self.mpprcFile) dbList = output.split("\n") dbList.remove("template0") dbList.remove("template1") for db in dbList: db = db.replace("$", "\\$") cmd = "gsql -d %s -p %s -f %s" % ( db, self.port, sqlFileName) tmpout = "" output = SharedFuncs.runShellCmd(cmd, self.user, self.mpprcFile) if (output.find("(0 rows)") < 0): tmpresult = output.splitlines() idxS = 0 idxE = 0 for idx in range(len(tmpresult)): if (not tmpresult[idx].find("---+---") < 0): idxS = idx - 1 if (tmpresult[idx].find("row)") > 0 or tmpresult[ idx].find("rows)") > 0): idxE = idx for i in range(idxS, idxE): tmpout += "%s\n" % tmpresult[i] finalresult += "%s:\n%s\n" % (db, tmpout) if (finalresult): self.result.rst = ResultStatus.WARNING self.result.val = "Data is not well distributed:\n%s" \ % finalresult else: self.result.rst = ResultStatus.OK self.result.val = "Data is well distributed" else: self.result.rst = ResultStatus.NA self.result.val = "First cn is not in this host"
def doCheck(self): global g_result if (self.cluster.isSingleInstCluster()): sql1 = """create or replace FUNCTION get_antiempty_tables( OUT result_tables text ) returns text as $$ declare test_sql text; type cursor_type is ref cursor; cur_sql_stmts cursor_type; cur_test_sql_result cursor_type; test_sql_result int; result_tables text := ''; begin drop table if exists to_be_selected_check; create temp table to_be_selected_check as select 'select 1 from ' || nspname || '.' || relname || ' limit 1;' as stmt from pg_class c, pg_namespace n where c.relnamespace=n.oid and c.reltuples=0 AND n.nspname <> 'pg_catalog' AND n.nspname <> 'cstore' AND n.nspname <> 'information_schema' AND n.nspname <> 'schema_cur_table_col' AND n.nspname <> 'schema_cur_table' AND c.relkind='r' AND c.relpersistence='p' AND n.nspname !~ '^pg_toast'; open cur_sql_stmts for 'select stmt from to_be_selected_check'; loop fetch cur_sql_stmts into test_sql; exit when cur_sql_stmts%notfound; open cur_test_sql_result for test_sql; fetch cur_test_sql_result into test_sql_result; if not cur_test_sql_result%notfound and 0 = position( 'to_be_selected_check' in test_sql) then result_tables = result_tables || replace(replace(replace( test_sql, 'select 1 from ', ''), ' limit 1', ''), ';', CHR(10)); end if ; close cur_test_sql_result; end loop; close cur_sql_stmts; drop table if exists to_be_selected_check; return result_tables; end; $$ LANGUAGE 'plpgsql';""" sql2 = "select get_antiempty_tables();" sqldb = "select datname from pg_database;" security_mode_value = DefaultValue.getSecurityMode() if (security_mode_value == "on"): secMode = True else: secMode = False if (secMode): dbList = [] (status, result, error) = ClusterCommand.excuteSqlOnLocalhost( self.port, sqldb) if (status != 2): raise Exception(ErrorCode.GAUSS_513["GAUSS_51300"] % sqldb + (" Error:%s" % error)) recordsCount = len(result) for i in range(0, recordsCount): dbList.append(result[i][0]) dbList.remove("template0") finalresult = "" for db in dbList: tablelist = [] ClusterCommand.excuteSqlOnLocalhost(self.port, sql1, db) ClusterCommand.excuteSqlOnLocalhost( self.port, "set client_min_messages='error';create " "table to_be_selected_check(test int);", db) sql2 = "set client_min_messages='error';" + sql2 (status, result, error) = ClusterCommand.excuteSqlOnLocalhost(self.port, sql2, db) if (status != 2): raise Exception(ErrorCode.GAUSS_513["GAUSS_51300"] % sql2 + (" Error:%s" % error)) if (result and result[0][0]): for tmptable in result[0][0].splitlines(): if (db == "postgres" and tmptable.upper().startswith("PMK.")): pass else: tablelist.append(tmptable) if (tablelist): finalresult += "%s:\n%s\n" % ( db, "\n".join(tablelist)) g_result[db] = tablelist if (finalresult): self.result.val = "The result is not ok:\n%s" % finalresult self.result.rst = ResultStatus.NG else: self.result.val = "All table analyzed" self.result.rst = ResultStatus.OK else: # Get the database in the node, remove template0 output = SharedFuncs.runSqlCmd(sqldb, self.user, "", self.port, self.tmpPath, "postgres", self.mpprcFile) dbList = output.split("\n") dbList.remove("template0") finalresult = "" for db in dbList: tablelist = [] SharedFuncs.runSqlCmd(sql1, self.user, "", self.port, self.tmpPath, db, self.mpprcFile) output = SharedFuncs.runSqlCmd(sql2, self.user, "", self.port, self.tmpPath, db, self.mpprcFile) for tmptable in output.splitlines(): if (db == "postgres" and tmptable.upper().startswith( "PMK.")): pass else: tablelist.append(tmptable) if (tablelist): finalresult += "%s:\n%s\n" % (db, "\n".join(tablelist)) g_result[db] = tablelist if (finalresult): self.result.val = "Tables unanalyzed:\n%s" % finalresult self.result.rst = ResultStatus.NG else: self.result.val = "All table analyzed" self.result.rst = ResultStatus.OK else: self.result.rst = ResultStatus.NA self.result.val = "First cn is not in this host"
def main(): """ main function """ try: (opts, args) = getopt.getopt(sys.argv[1:], "p:S:f:s:d:h", ["help"]) except Exception as e: usage() GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50000"] % str(e)) if (len(args) > 0): GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50000"] % str(args[0])) port = "" sqlfile = "" outputfile = "" database = "" for (key, value) in opts: if (key == "-h" or key == "--help"): usage() sys.exit(0) elif (key == "-p"): port = value elif (key == "-S"): sqlfile = value elif (key == "-f"): outputfile = value elif (key == "-s"): snapid = value elif (key == "-d"): database = value else: GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50000"] % key) Parameter.checkParaVaild(key, value) # check parameter if (port == ""): GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"] % 'p' + ".") if (sqlfile == ""): GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"] % 'S' + ".") if (outputfile == ""): GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"] % 'f' + ".") if (database == ""): GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"] % 'd' + ".") try: output = {} exesql = "" if os.path.exists(sqlfile): with open(sqlfile, "r") as fp: lines = fp.readlines() for line in lines: exesql += line + "\n" (status, result, err_output) = \ ClusterCommand.excuteSqlOnLocalhost(port, exesql, database) cmd = "rm -rf %s" % sqlfile if (err_output != ""): output["status"] = status output["error_output"] = err_output GaussLog.exitWithError(ErrorCode.GAUSS_513["GAUSS_51300"] % exesql + "Errors:%s" % err_output) output["status"] = status output["result"] = result output["error_output"] = err_output g_file.createFileInSafeMode(outputfile) with open(outputfile, "w") as fp_json: json.dump(output, fp_json) (status, outpout) = subprocess.getstatusoutput(cmd) if status != 0: raise Exception(ErrorCode.GAUSS_514["GAUSS_51400"] % cmd + "Error:\n%s" % output) except Exception as e: GaussLog.exitWithError("Errors:%s" % str(e))