diff --git a/opengausss_orz/README.md b/opengausss_orz/README.md deleted file mode 100644 index 42cc9f42e8a7065079f52b428091040d0bca76f3..0000000000000000000000000000000000000000 --- a/opengausss_orz/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# openGauss运维辅助工具 - -基于Python的封装工具可以提供方便快捷的管理和运维操作,通过简单的命令就能够执行常见的openGauss管理任务。 - -## 安装和使用 - -### 安装 - -1. 确保安装python3 - -2. `pip install -r requirements.txt` - -3. 将orz.py和database.ini文件下载到自己电脑 - -4. 修改database.ini文件中数据库相关配置 - -5. 修改omm用户下.bashrc文件,设置命令别称 - - `vim ~/.bashrc` - - `alias orz = 'python3 /文件所在路径/orz.py'` - -6. `source ~/.bashrc` - -7. 运行requests文件安装所需包 - -### 使用 - -在omm用户下输入orz命令查看相关操作内容,根据提示进行使用。 \ No newline at end of file diff --git a/opengausss_orz/orz/README.md b/opengausss_orz/orz/README.md new file mode 100644 index 0000000000000000000000000000000000000000..77557d9c84ddb9012c2e2638a7ae5a7ed40bd196 --- /dev/null +++ b/opengausss_orz/orz/README.md @@ -0,0 +1,16 @@ +# openGauss运维辅助工具 + +基于Python的封装工具可以提供方便快捷的管理和运维操作,通过简单的命令就能够执行常见的openGauss管理任务。 + +## 安装 + +1. 下载orz文件夹 +2. 修改orz文件夹中database.ini文件中数据库相关配置 +3. 可以直接使用源码orz.py,也可以使用封装后的 orz + 1. 使用封装后orz + 1. 用omm用户运行文件夹中orz文件./orz 要执行的命令 + 2. 若无执行权限,用root用户执行chmod +x orz + + 2. 源码orz.py依赖python3.6 + + diff --git a/opengausss_orz/database.ini b/opengausss_orz/orz/database.ini similarity index 100% rename from opengausss_orz/database.ini rename to opengausss_orz/orz/database.ini diff --git a/opengausss_orz/orz/orz b/opengausss_orz/orz/orz new file mode 100644 index 0000000000000000000000000000000000000000..7a54b962337014ed5d403c3d38d401e79362cc89 Binary files /dev/null and b/opengausss_orz/orz/orz differ diff --git a/opengausss_orz/orz.py b/opengausss_orz/orz/orz.py similarity index 95% rename from opengausss_orz/orz.py rename to opengausss_orz/orz/orz.py index 59a3f88b52a9047c111be512ce85e062670f99a7..bbb024518d3567b14034eacb53c84b20401f8483 100644 --- a/opengausss_orz/orz.py +++ b/opengausss_orz/orz/orz.py @@ -7,12 +7,16 @@ import configparser # 读取配置文件 config = configparser.ConfigParser() -current_folder_path = os.path.dirname(os.path.abspath(__file__)) +current_folder_path = os.path.dirname(os.path.abspath(sys.executable)) ini_path = os.path.join(current_folder_path, "database.ini") -config.read(ini_path) -# 获取数据库参数 -database = config.get('connect', 'database') -port = config.getint('connect', 'port') + +if os.path.exists(ini_path): + config.read(ini_path) + # 获取数据库参数 + database = config.get('connect', 'database') + port = config.getint('connect', 'port') +else: + print("database.ini not exists") def execute_database_command(command): @@ -89,7 +93,7 @@ def data_log(limitnum=None, show_help=False): 若show_help为true,返回命令帮助 """ if show_help: - return "\n" + "命令说明:".rjust(30) + "查看数据节点日志的后limitnum行" + return "limit_num\n" + "命令说明:".rjust(30) + "查看数据节点日志的后limitnum行" if limitnum is None: # 如果限制数未提供,则提示错误信息 @@ -97,7 +101,13 @@ def data_log(limitnum=None, show_help=False): # 获取文件夹路径 command = f"SELECT current_setting('log_directory');" - folder_path = execute_cluster_command(command) + output = execute_database_command(command) + + # 分割文字为行 + lines = output.split('\n') + # 提取路径 + folder_path = lines[2].strip() + # 获取文件夹中的所有文件 files = os.listdir(folder_path) # 排序文件列表按照修改时间 @@ -121,7 +131,7 @@ def wal_log(limitnum=None, show_help=False): 若show_help为true,返回命令帮助 """ if show_help: - return "\n"+"命令说明:".rjust(30)+"查看WAL节点日志的后limitnum行" + return "limit_num\n"+"命令说明:".rjust(30)+"查看WAL节点日志的后limitnum行" if limitnum is None: # 如果限制数未提供,则提示错误信息 @@ -228,8 +238,15 @@ def switchover_cluster(show_help=False): if show_help: return "\n" + "命令说明:".rjust(30) + "switchover切换主库" all_clusters_status() - addr = input("请输入需要切换库的地址") - command = f"gs_ctl switchover -D {addr}" + command = f"show data_directory;" + output = execute_database_command(command) + + # 分割文字为行 + lines = output.split('\n') + # 提取路径 + path = lines[2].strip() + + command = f"gs_ctl switchover -D {path}" output = execute_cluster_command(command) print(output) command = f"gs_om -t refreshconf" @@ -249,8 +266,16 @@ def failover_cluster(show_help=False): if show_help: return "\n" + "命令说明:".rjust(30) + "failover切换主库" all_clusters_status() - addr = input("请输入需要切换库的地址") - command = f"gs_ctl failover -D {addr}" + + command = f"show data_directory;" + output = execute_database_command(command) + + # 分割文字为行 + lines = output.split('\n') + # 提取路径 + path = lines[2].strip() + + command = f"gs_ctl failover -D {path}" output = execute_cluster_command(command) print(output) command = f"gs_om -t refreshconf" @@ -300,7 +325,7 @@ def table_vacuum(tablename=None, show_help=False): # 如果表名未提供,则提示错误信息 return "Please provide a table name." - command = f"VACUUM (VERBOSE ANALYZE) {tablename};" + command = f"VACUUM VERBOSE ANALYZE {tablename};" output = execute_database_command(command) pretty_print(output) @@ -574,7 +599,7 @@ def database_size(show_help=False): if show_help: return "\n" + "命令说明:".rjust(30) + "查询数据库大小" - command = f"SELECT pg_database.datname AS 'database_name', " \ + command = f"SELECT pg_database.datname AS database_name, " \ f"pg_size_pretty(pg_database_size (pg_database.datname)) AS size_in_mb " \ f"FROM pg_database ORDER BY size_in_mb DESC;" output = execute_database_command(command) @@ -611,7 +636,7 @@ def schema_tablesize(show_help=False): 若show_help为true,返回命令帮助 """ if show_help: - return "tablename\n" + "命令说明:".rjust(30) + "查询指定表的存储位置" + return "\n" + "命令说明:".rjust(30) + "查看schema下各表数据量" command = f"SELECT relname,pg_size_pretty(pg_total_relation_size(relid)) " \ f"FROM pg_stat_user_tables " \ f"WHERE schemaname ='public' " \ @@ -855,7 +880,7 @@ def all_tables_indexes(show_help=False): return "\n" + "命令说明:".rjust(30) + "查看所有表的的索引大小" command = f"SELECT relname,pg_size_pretty(pg_indexes_size(relid)) as index_size " \ f"FROM pg_catalog.pg_statio_all_tables " \ - f"ORDER BY pg_total_indexes_size(relid) DESC;" + f"ORDER BY index_size DESC;" output = execute_database_command(command) pretty_print(output) @@ -1000,7 +1025,7 @@ def index_def(indexname=None, show_help=False): # 如果索引名未提供,则提示错误信息 return "Please provide an index name." - command = f"SELECT pg_get_indexdef('{indexname}');" + command = f"SELECT pg_get_indexdef(indexrelid) AS index_definition FROM pg_stat_user_indexes where indexrelname='{indexname}';" output = execute_database_command(command) pretty_print(output) @@ -1023,7 +1048,7 @@ def database_status(database_name=None, show_help=False): # 如果数据库名未提供,则提示错误信息 return "Please provide a database name." - command = f"FROM pg_stat_database WHERE datname = '{database_name}';" + command = f"SELECT * FROM pg_stat_database WHERE datname = '{database_name}';" output = execute_database_command(command) pretty_print(output) @@ -1101,7 +1126,6 @@ def parameter_workmem(show_help=False): pretty_print(output) -# def parameter_log(show_help=False): """ 查看日志的参数值, 并通过prettyTable格式化输出. @@ -1112,9 +1136,13 @@ def parameter_log(show_help=False): """ if show_help: return "\n" + "命令说明:".rjust(30) + "查看日志的参数值" - command = f"SELECT * FROM pg_settings WHERE name LIKE '%log%';" + command = f"SELECT name, setting FROM pg_settings WHERE name LIKE '%log%';" output = execute_database_command(command) - pretty_print(output) + output = output.strip() + lines = output.split('\n') + lines = [line for line in lines if "------" not in line] + output = '\n'.join(lines) + print(output) all_options = { diff --git "a/opengausss_orz/orz/\346\223\215\344\275\234\346\211\213\345\206\214.pdf" "b/opengausss_orz/orz/\346\223\215\344\275\234\346\211\213\345\206\214.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..59d21f3d61be2d0af3a6845f6e950ecb5fe959ca Binary files /dev/null and "b/opengausss_orz/orz/\346\223\215\344\275\234\346\211\213\345\206\214.pdf" differ diff --git a/opengausss_orz/requirements.txt b/opengausss_orz/requirements.txt deleted file mode 100644 index 3e0d7c8789a826aee411fb2eeef988eada8e1367..0000000000000000000000000000000000000000 --- a/opengausss_orz/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -prettytable==2.2.0 \ No newline at end of file