首批通过分布式安全可靠测评,为关键业务系统打造
OceanBase 集群日志收集
更新时间:2026-01-29 12:19:15
本文适用于独立部署 obdiag 的场景,使用 obdiag gather log 命令可以一键收集所属 OceanBase 集群的日志。
如果是 obd 部署的集群,可直接通过 obd 侧诊断命令组对所选集群进行信息收集,详细命令可参考 诊断工具命令组。
使用说明
obdiag gather log [options]
选项说明如下:
| 选项名 | 是否必选 | 数据类型 | 默认值 | 说明 |
|---|---|---|---|---|
| --from | 否 | string | 默认为空 | 日志收集的开始时间,格式为: yyyy-mm-dd hh:mm:ss。
说明若使用的 obdiag 为 V2.0.0 之前版本,配置该选项时无需加引号,否则会报错。V2.0.0 及之后版本不再限制配置该选项时是否加引号。 |
| --to | 否 | string | 默认为空 | 日志收集的结束时间,格式为: yyyy-mm-dd hh:mm:ss。
说明若使用的 obdiag 为 V2.0.0 之前版本,配置该选项时无需加引号,否则会报错。V2.0.0 及之后版本不再限制配置该选项时是否加引号。 |
| --since | 否 | string | 默认为空 | 日志收集最近的某段时间,格式为: <n> <m|h|d>,其中,n 表示待输入的时间数字,m 表示分钟,h 表示小时,d 表示天,例如 30m 表示收集最近 30 分钟的日志。 |
| --recent_count | 否 | int | 默认为空 | 仅收集最近指定数量的日志文件。配置该选项后,将忽略 --from、--to、--since 选项。 |
| --scope | 否 | string | all | 选择收集的 OceanBase 集群日志类型,可配置值为 observer、election、rootservice、all。 |
| --grep | 否 | string | 默认为空 | 选择查询的关键字。 |
| --store_dir | 否 | string | 默认为命令执行的当前路径 | 存储结果的本地路径。 |
| --temp_dir | 否 | string | /tmp |
远端节点在日志收集过程中产生的临时文件存储目录。 |
| --redact | 否 | string | 默认为空 | 选择脱敏规则,例如可配置为 all_sql,使用多个规则时使用英文逗号(,)分隔,编写规则参考本文 脱敏规则编写 介绍。 |
| -c | 否 | string | ~/.obdiag/config.yml |
配置文件路径。 |
| --inner_config | 否 | string | 默认为空 | obdiag 自用的配置。 固定样式:--inner_config key1=value1 --inner_config key2=value2。
说明支持通过该选项配置的参数可参见 系统配置文件(obdiag 自用的配置)章节。 |
| --config | 否 | string | 默认为空 | 需被 obdiag 诊断的集群的配置,固定样式:--config key1=value1 --config key2=value2。
说明支持通过该选项配置的参数可参见 obdiag 配置。 |
| --config_password | 否 | string | 默认为空 | obdiag 使用加密的配置文件时,需通过该选项传入对应的密码。
说明详情可参见 配置文件加密。 |
使用示例
方式一:无配置文件使用(开箱即用)
收集指定时间段的日志
obdiag gather log --from "2022-06-30 16:25:00" --to "2022-06-30 18:30:00" \ --config obcluster.servers.nodes[0].ip=xx.xx.xx.1 \ --config obcluster.servers.nodes[1].ip=xx.xx.xx.2 \ --config obcluster.servers.global.ssh_username=test \ --config obcluster.servers.global.ssh_password=****** \ --config obcluster.servers.global.home_path=/home/admin/oceanbase输出如下:
... ZipFileInfo: +-------------------+-----------+ | Node | LogSize | +===================+===========+ | xxx.xxx.xxx.xxx | 36.184M | +-------------------+-----------+ ... ZipFileInfo: +-------------------+-----------+ | Node | LogSize | +===================+===========+ | xxx.xxx.xxx.xxx | 44.176M | +-------------------+-----------+ ... Summary: +-------------------+-----------+----------+--------+-------------------------------------------------------------------------------+ | Node | Status | Size | Time | PackPath | +===================+===========+==========+========+===============================================================================+ | xxx.xxx.xxx.xxx | Completed | 36.762M | 19 s | obdiag_gather_pack_20220701183246/ob_log_xxx.xxx.xxx.xxx_20220701183247.zip | +-------------------+-----------+----------+--------+-------------------------------------------------------------------------------+ | xxx.xxx.xxx.xxx | Completed | 638.200M | 718 s | obdiag_gather_pack_20220701183246/ob_log_xxx.xxx.xxx.xxx_20220701183918.zip | +-------------------+-----------+----------+--------+-------------------------------------------------------------------------------+快捷收集最近一段时间的日志
# obdiag 到各被收集节点做了免密情况,收集最近一小时的日志 obdiag gather log --since 1h \ --config obcluster.servers.nodes[0].ip=xx.xx.xx.1 \ --config obcluster.servers.nodes[1].ip=xx.xx.xx.2 \ --config obcluster.servers.nodes[2].ip=xx.xx.xx.3 \ --config obcluster.servers.global.home_path=/home/admin/oceanbase # obdiag 到各被收集节点做了免密情况,收集最近一小时的日志,并且限制单个节点收集的信息不超过 300MB ,个数不超过5个,说明:如果超过 300MB 或者日志文件超过 5 个,则不会收集该节点的日志回来 obdiag gather log --since 1h \ --config obcluster.servers.nodes[0].ip=xx.xx.xx.1 \ --config obcluster.servers.nodes[1].ip=xx.xx.xx.2 \ --config obcluster.servers.nodes[2].ip=xx.xx.xx.3 \ --config obcluster.servers.global.home_path=/home/admin/oceanbase \ --inner_config obdiag.basic.file_number_limit=5 --inner_config obdiag.basic.file_size_limit=300M # obdiag 到各被收集节点做了免密情况,收集最近一小时的日志,并且限制单个节点收集的信息不超过 1GB ,个数不超过5个, 说明:如果超过 1GB 或者日志文件超过 5 个,则不会收集该节点的日志回来 obdiag gather log --since 1h \ --config obcluster.servers.nodes[0].ip=xx.xx.xx.1 \ --config obcluster.servers.nodes[1].ip=xx.xx.xx.2 \ --config obcluster.servers.nodes[2].ip=xx.xx.xx.3 \ --config obcluster.servers.global.home_path=/home/admin/oceanbase \ --inner_config obdiag.basic.file_number_limit=5 --inner_config obdiag.basic.file_size_limit=1G # obdiag 到各被收集节点做了免密情况下,收集最近 30 分钟的日志 obdiag gather log --since 30m \ --config obcluster.servers.nodes[0].ip=xx.xx.xx.1 \ --config obcluster.servers.nodes[1].ip=xx.xx.xx.2 \ --config obcluster.servers.nodes[2].ip=xx.xx.xx.3 \ --config obcluster.servers.global.home_path=/home/admin/oceanbase # 各 OBServer 节点信息不一致情况下,收集最近 30 分钟的日志 obdiag gather log --since 30m \ --config obcluster.servers.nodes[0].ip=xx.xx.xx.1 \ --config obcluster.servers.nodes[0].ssh_username=test1 \ --config obcluster.servers.nodes[0].ssh_password=****** \ --config obcluster.servers.nodes[0].home_path=/home/test1/oceanbase \ --config obcluster.servers.nodes[1].ip=xx.xx.xx.2 --config obcluster.servers.nodes[1].ssh_username=test2 \ --config obcluster.servers.nodes[1].ssh_password=****** \ --config obcluster.servers.nodes[1].home_path=/home/test2/oceanbase # 提供 sys 租户的连接信息,让 obdiag 自动帮你获取集群各节点的 IP 地址,收集最近一小时的日志 obdiag gather log --since 1h \ --config db_host=xx.xx.xx.xx \ --config db_port=xxxx \ --config tenant_sys.user=root@sys \ --config tenant_sys.password=*** \ --config obcluster.servers.global.ssh_username=test \ --config obcluster.servers.global.ssh_password=****** \ --config obcluster.servers.global.home_path=/home/admin/oceanbase过滤关键字收集
# 收集最近 30 分钟的日志, 并过滤关键字 "TRACE_ID" obdiag gather log --grep "TRACE_ID" \ --config db_host=xx.xx.xx.xx \ --config db_port=xxxx \ --config tenant_sys.user=root@sys \ --config tenant_sys.password=*** \ --config obcluster.servers.global.ssh_username=test \ --config obcluster.servers.global.ssh_password=****** \ --config obcluster.servers.global.home_path=/home/admin/oceanbase # 收集最近 30 分钟的日志, 并过滤多个关键字, 比如过滤 "AAAAA" 和 "BBBBB" obdiag gather log --grep "AAAAA" --grep "BBBBB" \ --config db_host=xx.xx.xx.xx \ --config db_port=xxxx \ --config tenant_sys.user=root@sys \ --config tenant_sys.password=*** \ --config obcluster.servers.global.ssh_username=test \ --config obcluster.servers.global.ssh_password=****** \ --config obcluster.servers.global.home_path=/home/admin/oceanbase # 收集指定时间的日志, 并过滤多个关键字, 比如过滤 "AAAAA" 和 "BBBBB" obdiag gather log --from "2022-06-30 16:25:00" --to "2022-06-30 18:30:00" --grep "AAAAA" --grep "BBBBB" \ --config db_host=xx.xx.xx.xx \ --config db_port=xxxx \ --config tenant_sys.user=root@sys \ --config tenant_sys.password=*** \ --config obcluster.servers.global.ssh_username=test \ --config obcluster.servers.global.ssh_password=****** \ --config obcluster.servers.global.home_path=/home/admin/oceanbase注意
如果采用多个关键字过滤,他们之间的关系是“与”的关系,收集的日志行同时满足多个 grep 中的关键字才会被收集。
方式二:有配置文件使用
需要确保已经在 obdiag 配置文件 ~/.obdiag/config.yml 中配置好需要收集节点的登录信息。相关的详细配置介绍,参见 obdiag 配置。
收集指定时间段的日志
obdiag gather log --from "2022-06-30 16:25:00" --to "2022-06-30 18:30:00"输出如下:
... ZipFileInfo: +-------------------+-----------+ | Node | LogSize | +===================+===========+ | xxx.xxx.xxx.xxx | 36.184M | +-------------------+-----------+ ... ZipFileInfo: +-------------------+-----------+ | Node | LogSize | +===================+===========+ | xxx.xxx.xxx.xxx | 44.176M | +-------------------+-----------+ ... Summary: +-------------------+-----------+----------+--------+-------------------------------------------------------------------------------+ | Node | Status | Size | Time | PackPath | +===================+===========+==========+========+===============================================================================+ | xxx.xxx.xxx.xxx | Completed | 36.762M | 19 s | obdiag_gather_pack_20220701183246/ob_log_xxx.xxx.xxx.xxx_20220701183247.zip | +-------------------+-----------+----------+--------+-------------------------------------------------------------------------------+ | xxx.xxx.xxx.xxx | Completed | 638.200M | 718 s | obdiag_gather_pack_20220701183246/ob_log_xxx.xxx.xxx.xxx_20220701183918.zip | +-------------------+-----------+----------+--------+-------------------------------------------------------------------------------+快捷收集最近一段时间的日志
# 收集最近一小时的日志 obdiag gather log --since 1h # 收集最近 30 分钟的日志 obdiag gather log --since 30m # 收集最近 30 分钟的日志 并且指定了被收集集群的配置文件 obdiag gather log --since 30m -c /root/config.yml过滤关键字收集
# 收集最近 30 分钟的日志, 并过滤关键字 "TRACE_ID" obdiag gather log --grep "TRACE_ID" # 收集最近 30 分钟的日志, 并过滤多个关键字, 比如过滤 "AAAAA" 和 "BBBBB" obdiag gather log --grep "AAAAA" --grep "BBBBB" # 收集指定时间的日志, 并过滤多个关键字, 比如过滤 "AAAAA" 和 "BBBBB" obdiag gather log --from "2022-06-30 16:25:00" --to "2022-06-30 18:30:00" --grep "AAAAA" --grep "BBBBB"注意
如果采用多个关键字过滤,他们之间的关系是“与”的关系,收集的日志行同时满足多个 grep 中的关键字才会被收集。
结果解读
命令执行后在指定目录下会生成一个文件夹,文件夹形如: obdiag_gather_pack_20240808105846
脱敏规则编写
在很多时候脱敏的规则是业务侧定义的,即由用户来定义什么是敏感数据,obdiag 在 2.5.0 版本开始支持自定义脱敏规则。obdiag 中关于脱敏规则的文件位于 ~/.obdiag/gather/redact/ 下,所有的 py 文件都是对应的规则,文件名即是规则名。
本次以 a_test 为例介绍如何编写脱敏规则。
创建规则文件
在
~/.obdiag/gather/redact/下创建一个 py 文件,在编写规则中,我们规定文件名即为规则名。此例中文件名为a_test.py,即创建~/.obdiag/gather/redact/a_test.py。创建统一类
为了方便后期的统一管理,需要创建一个继承 RedactBase 类的规则类,类名可自定义,一般与文件名一致即可,示例如下:
from handler.gather.plugins.redact import RedactBase class a_test(RedactBase):创建规则方法
我们的规则类需要实现 redact 方法,text 是输入的文件内容(未分行),若需要分行处理,则进行分行处理即可,如下:
text_lines = text.split('\n')将处理好后的信息以 string 的形式返回
需要注意的是分行处理会导致处理时间大幅度上升,非强需求下建议不要这么操作。
创建规则实例
按 obdiag 的规则需要设置一个规则名的变量用于 obdiag 内核的调用,按示例需要增加一个 a_test 的变量,具体实现如下:
a_test = a_test()
完整示例
from handler.gather.plugins.redact import RedactBase
import re
class a_test(RedactBase):
def redact(self, text):
text_lines = text.split('\n')
redacted_log = ''
for line in text_lines:
if 'test' in line:
redacted_log = redacted_log + re.sub('test', '******', line)
return redacted_log
a_test = a_test()