重庆思庄Oracle、Redhat认证学习论坛

标题: prometheus 监控PG [打印本页]

作者: 郑全    时间: 2024-9-28 16:26
标题: prometheus 监控PG

前提:
     已经安装好prometheus 监控端

1.安装 postgres_exporter
   1.1 下载 postgres_exporter
         wget https://github.com/prometheus-co ... .linux-amd64.tar.gz
   1.2 解压postgres_exporter
         tar -xvzf postgres_exporter-0.15.0.linux-amd64.tar.gz

        [root@dbserver ~]#
        [root@dbserver ~]# ls -ltr postgres_exporter-0.15.0.linux-amd64
        total 16344
        -rwxr-xr-x 1 1001 1002 16715804 Oct 27  2023 postgres_exporter
        -rw-r--r-- 1 1001 1002       70 Oct 27  2023 NOTICE
        -rw-r--r-- 1 1001 1002    11357 Oct 27  2023 LICENSE

        mv postgres_exporter-0.15.0.linux-amd64 /usr/local/postgres_exporter
    1.3 创建自启动服务
        vim /etc/systemd/system/postgres_exporter.service

        [Unit]
         Description=https://github.com/prometheus-community/postgres exporter
         After=network-online.target

         [Service]
         Type=simple
         User=root
         Group=root
          Environment=DATA_SOURCE_NAME=postgresql://postgres_exporter:postgres_exporter@localhost:5432/postgres?sslmode=disable
          ExecStart=/usr/local/postgres_exporter/postgres_exporter
          ExecReload=/bin/kill -HUP
          KillMode=process
          TimeoutStopSec=20s
          Restart=always

          [Install]
         WantedBy=default.target

        --让服务生效
        systemctl daemon-reload
        systemctl enable postgres_exporter
        systemctl start postgres_exporter
        systemctl status postgres_exporter
        systemctl stop postgres_exporter

     1.4 创建对应的监控用户

      目前生产中,主要试PG 10以上版本,下面试PG10以上版本执行脚本:

PSQL>

CREATE OR REPLACE FUNCTION __tmp_create_user() returns void as $$
BEGIN
  IF NOT EXISTS (
          SELECT                       -- SELECT list can stay empty for this
          FROM   pg_catalog.pg_user
          WHERE  usename = 'postgres_exporter') THEN
    CREATE USER postgres_exporter;
  END IF;
END;
$$ language plpgsql;

SELECT __tmp_create_user();
DROP FUNCTION __tmp_create_user();

ALTER USER postgres_exporter WITH PASSWORD 'postgres_exporter';
ALTER USER postgres_exporter SET SEARCH_PATH TO postgres_exporter,pg_catalog;

GRANT CONNECT ON DATABASE postgres TO postgres_exporter;
grant pg_monitor to postgres_exporter;

   1.5 测试安装情况
       http://192.168.133.200:9187/metrics 如果能正常,安装postgres_exporter 正常

2.在Prometheus侧添加agent节点
   找到前面的prometheus.yml配置文件,添加agent节点的信息,包括主机ip和端口号。
   vim /opt/prometheus/prometheus.yml

   - job_name: 'postgres_exp'
   static_configs:
      - targets: ['192.168.133.200:9187']

   添加后,可以在WEB界面测试

3.在GRAFANA 测添加 仪表盘
   ,之后,如果没有问题,就可以对PG进行监控。







欢迎光临 重庆思庄Oracle、Redhat认证学习论坛 (http://bbs.cqsztech.com/) Powered by Discuz! X3.2