指标体系
Prometheus 兼容的指标采集、存储与查询。
Pigsty 使用 VictoriaMetrics 作为指标存储,完全兼容 Prometheus 生态。通过各类 Exporter 采集超过三千类监控指标。
指标采集架构
┌─────────────────────────────────────────────────────────────┐
│ VictoriaMetrics │
│ (Pull 模式) │
└───────────────────────────┬─────────────────────────────────┘
│ HTTP Scrape
┌───────────┬───────────┼───────────┬───────────┐
│ │ │ │ │
▼ ▼ ▼ ▼ ▼
┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐
│ :9630 │ │ :9631 │ │ :8008 │ │ :9101 │ │ :9100 │
│pg_exp │ │pgb_exp│ │patroni│ │haproxy│ │node_ex│
└───────┘ └───────┘ └───────┘ └───────┘ └───────┘
Exporter 列表
| Exporter | 端口 | 说明 |
|---|---|---|
| node_exporter | 9100 | 主机指标(CPU/内存/磁盘/网络) |
| pg_exporter | 9630 | PostgreSQL 数据库指标 |
| pgbouncer_exporter | 9631 | Pgbouncer 连接池指标 |
| pgbackrest_exporter | 9854 | pgBackRest 备份指标 |
| patroni | 8008 | Patroni 高可用指标 |
| haproxy | 9101 | HAProxy 负载均衡指标 |
核心指标分类
PostgreSQL 指标
| 类别 | 指标示例 | 说明 |
|---|---|---|
| 连接 | pg_stat_activity_count |
当前连接数 |
| 事务 | pg_stat_database_xact_commit |
事务提交数 |
| 查询 | pg_stat_statements_calls |
查询调用次数 |
| 锁 | pg_locks_count |
锁数量 |
| 复制 | pg_replication_lag_seconds |
复制延迟 |
| 存储 | pg_database_size_bytes |
数据库大小 |
| 缓存 | pg_stat_bgwriter_buffers_backend |
后台写入缓冲 |
主机指标
| 类别 | 指标示例 | 说明 |
|---|---|---|
| CPU | node_cpu_seconds_total |
CPU 使用时间 |
| 内存 | node_memory_MemAvailable_bytes |
可用内存 |
| 磁盘 | node_disk_io_time_seconds_total |
磁盘 IO 时间 |
| 网络 | node_network_receive_bytes_total |
网络接收字节 |
指标查询
PromQL 示例
# 当前连接数
pg_stat_activity_count{state="active"}
# 每秒事务数
rate(pg_stat_database_xact_commit[5m])
# 复制延迟(秒)
pg_replication_lag_seconds
# CPU 使用率
100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
# 可用内存百分比
node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes * 100
查询接口
# 通过 API 查询
curl 'http://localhost:9090/api/v1/query?query=pg_up'
# 范围查询
curl 'http://localhost:9090/api/v1/query_range?query=pg_up&start=2024-01-01T00:00:00Z&end=2024-01-02T00:00:00Z&step=1h'
指标存储
存储配置
prometheus_retention: 15d # 指标保留时间
prometheus_storage_size: 10GB # 存储空间限制
存储路径
/data/prometheus/ # VictoriaMetrics 数据目录
服务发现
Pigsty 使用文件服务发现(File SD),自动注册监控目标。
配置文件
/etc/prometheus/targets/
├── infra.yml # 基础设施目标
├── node.yml # 节点目标
├── pgsql.yml # PostgreSQL 目标
├── redis.yml # Redis 目标
└── ...
目标格式
# /etc/prometheus/targets/pgsql.yml
- labels:
cls: pg-meta
ins: pg-meta-1
ip: 10.10.10.10
job: pgsql
targets:
- 10.10.10.10:9630 # pg_exporter
- 10.10.10.10:9631 # pgbouncer_exporter
- 10.10.10.10:8008 # patroni
自定义指标
添加查询指标
通过 pg_exporter 自定义查询采集额外指标:
# /etc/pg_exporter/queries.yaml
pg_custom_metric:
query: "SELECT count(*) as value FROM my_table"
metrics:
- value:
usage: "GAUGE"
description: "Custom metric value"
添加 Exporter
在 haproxy_services 中添加自定义 Exporter 端口:
haproxy_services:
- name: custom-exporter
port: 9999
options:
- option httpchk GET /metrics
监控标签
所有 PostgreSQL 相关指标都带有以下标签:
| 标签 | 说明 | 示例 |
|---|---|---|
cls |
集群名称 | pg-meta |
ins |
实例名称 | pg-meta-1 |
ip |
实例 IP | 10.10.10.10 |
job |
任务类型 | pgsql |
使用标签进行过滤:
# 特定集群的连接数
pg_stat_activity_count{cls="pg-meta"}
# 特定实例的复制延迟
pg_replication_lag_seconds{ins="pg-test-2"}