Prometheus-监控 Postgresql

一、部署

1 二进制方式部署

github 地址:https://github.com/prometheus-community/postgres_exporter

1.1 下载

可以从官方发布版本中找到多个平台的二进制安装包。
打开连接后,点击 Assets,即可看到下载列表。

本文档使用如下版本作为示例

curl -o postgres_exporter.tgz -L https://github.com/prometheus-community/postgres_exporter/releases/download/v0.13.1/postgres_exporter-0.13.1.linux-amd64.tar.gz

1.2 解压部署

tar -xf postgres_exporter.tgz postgres_exporter-0.13.1.linux-amd64/postgres_exporter
mv postgres_exporter-0.13.1.linux-amd64/postgres_exporter /usr/local/bin/

1.3 环境变量

这里的环境变量可以被 exporter 使用,并且同时适于二进制部署方式和 docker-compose 部署方式。

由以PG_开头的环境变量的设置将被相应的CLI标志覆盖(如果给定)。

变量名 描述
DATA_SOURCE_NAME 默认的旧式格式。接受URI形式和key=value形式的参数。URI可能包含要连接的用户名和密码。
DATA_SOURCE_URI DATA_SOURCE_NAME的替代方案,它只接受主机名而不包含用户名和密码组件。例如,my_pg_hostname or my_pg_hostname?sslmode=disable
DATA_SOURCE_URI_FILE 与上面相同,但从文件中读取URI。
DATA_SOURCE_USER 当使用 DATA_SOURCE_URI 时候, 使用这个变量指定用户名。
DATA_SOURCE_USER_FILE 同上,但是从文件中获取用户名。
DATA_SOURCE_PASS 当使用 DATA_SOURCE_URI 时候, 使用这个变量指定连接用的密码。
DATA_SOURCE_PASS_FILE 同上,但从文件中获取密码。
PG_EXPORTER_WEB_TELEMETRY_PATH 公共指标的路径。默认值为 /metrics
PG_EXPORTER_DISABLE_SETTINGS_METRICS 如果您不想获取 pg_settings,请使用该标志。值可以为true或false。默认值为false。
PG_EXPORTER_AUTO_DISCOVER_DATABASES (DEPRECATED) 是否动态发现服务器上的数据库。值可以为true或false。默认值为false。
PG_EXPORTER_CONSTANT_LABELS (DEPRECATED) 为所有的指标设置 lable, 提供一个 key=value的列表,用逗号分隔。
PG_EXPORTER_EXCLUDE_DATABASES (DEPRECATED) 启用 PG_EXPORTER_AUTO_DISCOVER_DATABASES 时要删除的数据库的列表,用逗号分隔。默认值为空字符串。
PG_EXPORTER_INCLUDE_DATABASES (DEPRECATED) 仅在启用PG_EXPORTER_AUTO_DISCOVER_DATABASES 时才包含的数据库的逗号分隔列表。默认值为空字符串,表示允许所有。
PG_EXPORTER_METRIC_PREFIX 用于postgres_expoter 导出的每个默认指标的前缀。默认值为 pg

1.4 配置 systemd

/etc/default/postgres-exporter.conf

DATA_SOURCE_URI=127.0.0.1:5432?sslmode=disable
DATA_SOURCE_USER=username
DATA_SOURCE_PASS=password
PG_EXPORTER_DISABLE_SETTINGS_METRICS=true

/etc/systemd/system/postgres-expoter.service

[Unit]
Description=The Postgresql Expoter 监控程序
After=network-online.target
Wants=network-online.target

[Service]
EnvironmentFile=-/etc/default/postgres-exporter.conf
ExecStart=/usr/local/bin/postgres_exporter --web.listen-address=:9187

KillSignal=SIGQUIT

Restart=always

RestartPreventExitStatus=1 6 SIGABRT

TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=1048576
LimitNPROC=1048576

[Install]
WantedBy=multi-user.target

2 docker-compose方式部署

目录构建:

.
├── auth
│   └── postgres_auth.yml
└── compose.yml

1 directory, 2 files

compose.yml

version: '3.9'
services:
  exporter:
    image: quay.io/prometheuscommunity/postgres-exporter
    command: --config.file=/auth/postgres_auth.yml
    environment:
      DATA_SOURCE_URI: 10.10.10.10:5432?sslmode=disable
      DATA_SOURCE_USER: 用户名
      DATA_SOURCE_PASS: 密码
      PG_EXPORTER_DISABLE_SETTINGS_METRICS: true
    volumes:
      - "./auth/postgres_auth.yml:/auth/postgres_auth.yml"
    ports:
      - "9188:9187"
    restart: always

auth/postgres_auth.yml

auth_modules:
  pgauth:
    type: userpass
    userpass:
      username: 用户名
      password: 密码
    options:
      sslmode: disable

这里所有被监控的 PostgreSQL 使用同一个用户名和密码

二、多目标监控

redis_exporter, mysql_exporter 一样,支持对多个 postgresql 的监控。

配置认证信息

连接 postgresql 的认证信息放到一个 yaml 文件中,并使用 exporter 的命令行启动参数 --config.file 指定。

示例文件 postgres_exporter.yml 内容:

如果所有被监控的Postgresql服务的认证用户密码都一样,则设置为一个即可。

auth_modules:
  pgmaster: # 设置一个任何名称,用于标记其中一个被监控的 postgresql
    type: userpass
    userpass:
      username: username
      password: password
    options:
      # options become key=value parameters of the DSN
      sslmode: disable
  pgslave1: # 设置一个任何名称,用于标记其中一个被监控的 postgresql
    type: userpass
    userpass:
      username: username
      password: password
    options:
      # options become key=value parameters of the DSN
      sslmode: disable

修改启动命令

/usr/local/bin/postgres_exporter --web.listen-address=:9187 --config.file=/postgres_exporter.yml

配置 Prometheus

prometheus.yml

scrape_configs:
  # 针对单个目标的配置
  - job_name: 'postgres_exporter'
    static_configs:
      - targets:
          - postgres.exporter1.host:9187
          - postgres.exporter2.host:9187

  # 针对多个目标的配置
  - job_name: 'postgres_exporter_targets'
    static_configs:
      - targets:
        - postgresql-host1:5432
        - postgresql-host2:5432
    metrics_path: /probe
    params:
      auth_module:
        - pgmaster
        - pgslave1
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: postgres.exporter.host:9187

三、Dashboard

Dashboard ID: 9628

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

五、配置告警规则

5.1 Postgresql down

Postgresql instance is down

  - alert: PostgresqlDown
    expr: pg_up == 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shark_西瓜甜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值