快速监控 Oracle 数据库

使用 Cprobe 快速采集 Oracle 数据库监控指标:准备 Oracle 连接账号和权限,配置 oracledb 插件、测试采集输出、写入 Prometheus/VictoriaMetrics/夜莺,并导入参考 Grafana 仪表盘。

作者 秦晓辉@快猫星云

Oracle 数据库在行业内应用广泛,通常存放的非常重要的数据,监控是必不可少的,本文使用 Cprobe 采集 Oracle 监控数据,极致简单,分享给大家。

实现思路

用 Cprobe 监控 Oracle,本质是让 Cprobe 连接 Oracle 数据库,执行预置 SQL,生成监控指标,然后把这些指标写入时序库或夜莺。落地时要关注四件事:

  • Oracle 侧要有可连接的地址、SID 或 service,以及具备查询权限的账号。
  • Cprobe 侧要启用 oracledb 插件,并在 main.yamllink.toml 中配置连接信息。
  • 先用 -no-writer -no-httpd -plugins oracledb 在控制台验证采集结果。
  • 确认采集正常后,再配置 writer.yaml 写入 Prometheus、VictoriaMetrics 或夜莺。
步骤 关键文件或命令 目标
准备 Oracle Docker 示例或既有 Oracle 实例 拿到地址、端口、SID、账号
授权监控账号 CREATE USERGRANT SELECT 允许 Cprobe 查询必要视图
配置采集 conf.d/oracledb/main.yamllink.toml 指定目标和认证信息
验证采集 ./cprobe -no-writer -no-httpd -plugins oracledb 先确认能输出指标
写入 TSDB conf.d/writer.yaml 把指标送到远端存储

1. 安装配置 Oracle

简单起见,我使用 Docker 启动 Oracle,命令如下:

docker run -d --name oracle -p 1022:22 -p 18080:8080 -p 1521:1521 wnameless/oracle-xe-11g-r2

如上命令启动之后,Oracle 的监听端口是 1521,用户名/密码是 system/oracle,数据库 SID 是 xe,要监控 Oracle,首先得有账号连上去执行 SQL,所以这些连接信息得记住喽,待会要用。

如果是对既有的 Oracle 做监控,需要创建账号并分派权限,比如:

-- Create the monitoring user "cprobe"
CREATE USER cprobe IDENTIFIED BY <YOUR-PASSWORD>;

-- Grant the "cprobe" user the required permissions
GRANT CONNECT TO cprobe;
GRANT SELECT ON SYS.GV_$RESOURCE_LIMIT to cprobe;
GRANT SELECT ON SYS.V_$SESSION to cprobe;
GRANT SELECT ON SYS.V_$WAITCLASSMETRIC to cprobe;
GRANT SELECT ON SYS.GV_$PROCESS to cprobe;
GRANT SELECT ON SYS.GV_$SYSSTAT to cprobe;
GRANT SELECT ON SYS.V_$DATAFILE to cprobe;
GRANT SELECT ON SYS.V_$ASM_DISKGROUP_STAT to cprobe;
GRANT SELECT ON SYS.V_$SYSTEM_WAIT_CLASS to cprobe;
GRANT SELECT ON SYS.DBA_TABLESPACE_USAGE_METRICS to cprobe;
GRANT SELECT ON SYS.DBA_TABLESPACES to cprobe;
GRANT SELECT ON SYS.GLOBAL_NAME to cprobe;

这些权限来自本文示例中的采集 SQL 需求。生产环境里建议使用专门的监控账号,不要直接复用高权限业务账号。

2. 安装 Cprobe

Cprobe 是一个探针采集器,支持常见数据库、中间件的采集,比如 MySQL、Redis、MongoDB、Oracle、Kafka、ElasticSearch 等,最新版本是 v0.7.1,我们从 github releases 页面下载:

https://github.com/cprobe/cprobe/releases/tag/v0.7.1

我是 arm 的 linux,所以下载的是 cprobe-v0.7.1-linux-arm64.tar.gz,如果你是 x86 的,应该下载 amd64 那个包。如果你从 github 下载有困难,我这里提供一个国内的下载地址:

https://download.flashcat.cloud/cprobe-v0.7.1-linux-arm64.tar.gz
https://download.flashcat.cloud/cprobe-v0.7.1-linux-amd64.tar.gz

解压缩进入对应目录,执行 install 和 start 命令即可:

./cprobe -install
./cprobe -start
./cprobe -status

安装完成。

3. 配置 Cprobe 采集

首先配置要采集的 Oracle 的地址,进入刚才 cprobe 解压出的目录,编辑 conf.d/oracledb/main.yaml,修改如下:

global:
  scrape_interval: 15s
  external_labels:
    cplugin: 'oracle'

scrape_configs:
- job_name: 'oracle'
  static_configs:
  - targets:
    - 10.99.1.107:1521/xe # ip:port/service
  scrape_rule_files:
  - 'link.toml'
  - 'comm.toml'

上面的 IP 是我的 Oracle 的 IP,你要根据你的环境做调整。这个 main.yaml 又引用了 link.toml 和 comm.toml,在 link.toml 中配置认证信息,比如:

[global]
username = "system"
password = "oracle"
options = {}

comm.toml 的内容不用动。监控 Oracle 的原理就是连上去执行一堆 SQL,comm.toml 中提前帮你准备好了一堆 SQL,其实还有 cust.toml 有更多 SQL,看你需求,如果 cust.toml 中的监控项你也需要,那就把 cust.toml 也配置到 main.yaml 中,如下:

global:
  scrape_interval: 15s
  external_labels:
    cplugin: 'oracle'

scrape_configs:
- job_name: 'oracle'
  static_configs:
  - targets:
    - 10.99.1.107:1521/xe # ip:port/service
  scrape_rule_files:
  - 'link.toml'
  - 'comm.toml'
  - 'cust.toml'

下面我们测试一下,看看是否真的能够采集到数据:

./cprobe -no-writer -no-httpd -plugins oracledb

正常来讲,会输出很多指标,类似下面这样:

./cprobe -no-writer -no-httpd -plugins oracledb
2023-12-25T10:27:33.868Z        info    /Users/ulric/works/cprobe/lib/logger/flag.go:12 build version: 0.0.1-2023-12-25-08-39-11
2023-12-25T10:27:33.868Z        info    /Users/ulric/works/cprobe/lib/logger/flag.go:13 command-line flags
2023-12-25T10:27:33.868Z        info    /Users/ulric/works/cprobe/lib/logger/flag.go:20   -no-httpd="true"
2023-12-25T10:27:33.868Z        info    /Users/ulric/works/cprobe/lib/logger/flag.go:20   -no-writer="true"
2023-12-25T10:27:33.868Z        info    /Users/ulric/works/cprobe/lib/logger/flag.go:20   -plugins="oracledb"
2023-12-25T10:27:33.868Z        info    /Users/ulric/works/cprobe/lib/runner/runner.go:25       hostname: ulric-flashcat.local
2023-12-25T10:27:33.868Z        info    /Users/ulric/works/cprobe/lib/runner/runner.go:26       runtime.fd_limits: (soft=61440, hard=unlimited)
2023-12-25T10:27:33.868Z        info    /Users/ulric/works/cprobe/lib/runner/runner.go:27       runtime.vm_limits: (soft=unlimited, hard=unlimited)
>> __name__=oracledb_sessions_value cplugin=oracle instance=10.99.1.107:1521/xe job=oracle status=ACTIVE type=BACKGROUND  1703500053881 22.000000
>> __name__=oracledb_sessions_value cplugin=oracle instance=10.99.1.107:1521/xe job=oracle status=ACTIVE type=USER  1703500053881 1.000000
>> __name__=oracledb_resource_current_utilization cplugin=oracle instance=10.99.1.107:1521/xe job=oracle resource_name=processes  1703500053881 29.000000
>> __name__=oracledb_resource_limit_value cplugin=oracle instance=10.99.1.107:1521/xe job=oracle resource_name=processes  1703500053881 100.000000
...

上面的各个参数的含义:

  • -no-writer 表示不写入 TSDB,只是输出到控制台
  • -no-httpd 表示 Cprobe 不启动 HTTP 服务
  • -plugins oracledb 表示只启动 oracledb 插件,如果你还想启动其他插件,可以用逗号分隔,比如 -plugins oracledb,mysql,redis,如果想启动所有插件,就不加 -plugins 参数即可,默认就是启动所有插件

如果这里没有输出 Oracle 指标,优先检查 Oracle 网络连通性、用户名密码、SID/service、授权 SQL 和 main.yaml 中的 target 写法。不要急着配置远端写入,先把本地采集验证通。

4. 配置 Cprobe 写入 TSDB

监控数据采集到之后,需要写入 TSDB,Cprobe 在 conf.d 目录下提供 writer.yaml 配置文件,把其中 writers.url 部分改成你自己的时序库的 remote write 地址即可。

  • 如果时序库是 Prometheus,url 通常是:http://IP:9090/api/v1/write
  • 如果时序库是单机版本的 VictoriaMetrics,url 通常是:http://IP:8428/api/v1/write
  • 如果时序库是集群版本的 VictoriaMetrics,url 通常是:http://IP:8480/insert/0/prometheus/api/v1/write
  • 如果想把数据直接推给夜莺,url 通常是:http://IP:17000/prometheus/v1/write

配置完成之后,重启 Cprobe 即可:

./cprobe -restart

writer.yaml 的改动需要重启 Cprobe 才能生效。如果是修改各个插件目录下的配置,改完之后发给 HUP 信号给 Cprobe,Cprobe 会自动 reload 配置,不需要重启。比如:

kill -HUP `pidof cprobe`

5. 仪表盘

笔者整理了 Oracle 的 Grafana 仪表盘,分享给大家,不过 Oracle 的指标都是通过配置文件自定义 SQL 采集的,指标差别可能比较大,这个仪表盘只能是仅供参考了:

https://github.com/cprobe/cprobe/blob/main/conf.d/oracledb/doc/dash/grafana_oracledb_01.json

告警规则暂未整理,关注我,回头整理了再分享给大家。上文提到的方法如果你在实践过程中遇到问题,欢迎留言交流哈。

FAQ

Q1:Oracle 监控账号必须用 system/oracle 吗?

不是。Docker 示例里为了方便使用了 system/oracle,既有 Oracle 环境建议创建专门的 cprobe 监控账号,并按文中 SQL 授权必要视图。

Q2:为什么要先用 -no-writer -no-httpd 测试?

这样可以把问题限定在采集链路本身。如果控制台已经能输出 oracledb_ 相关指标,再去配置 TSDB 写入会更稳。

Q3:Grafana 仪表盘能直接用于所有 Oracle 环境吗?

只能作为参考。原文已经说明 Oracle 指标来自配置文件中的自定义 SQL,不同环境启用的 SQL 和指标可能不同,仪表盘需要按实际指标调整。

联系我们交流

延伸路径

继续看解决方案和产品对比

如果你正在做监控、可观测性或故障定位相关选型,建议从解决方案和产品对比继续往下看。

标签 Oracle监控
快猫星云 联系方式 快猫星云 联系方式
快猫星云 联系方式
快猫星云 联系方式
快猫星云 联系方式
快猫星云