【问题标题】:Connecting to database from prometheus_exporter从 prometheus_exporter 连接到数据库
【发布时间】:2022-11-10 07:18:20
【问题描述】:

为了在 prometheus 中收集 Postgres 指标,使用了 postgres_exporter。不使用容器化,一切都在本地完成。硬件指标通过作业收集在 prometheus 中。要收集数据库指标,我需要将 prometheus_exporter 连接到数据库。告诉我如何配置与数据库的连接。 postgres_exporter 没有配置文件。是否可以通过环境变量来做到这一点?

普罗米修斯.yml:

scrape_configs:
     - job_name: postgresql
        static_configs:
          - targets: ['xx.xx.xx.xx:9187']
            labels:
              alias: postgres

【问题讨论】:

    标签: postgresql environment-variables prometheus grafana


    【解决方案1】:

    DATA_SOURCE_NAME="postgresql://user:password@host:port/dbname?sslmode=disable" 这里最后 dbname 是必须避免错误的。 对于我的情况,我发现现在可以了。

    DATA_SOURCE_NAME="postgresql://grafana:p@host:192.168.1.100/postgres?sslmode=disable"
    

    【讨论】:

      【解决方案2】:

      是否可以通过环境变量来做到这一点?

      对的,这是可能的。以下是支持的变量列表:https://github.com/prometheus-community/postgres_exporter#environment-variables

      如何配置与数据库的连接

      从控制台启动的简单示例:

      DATA_SOURCE_NAME="user=postgres host=database.example.com" postgres_exporter
      

      或者

      DATA_SOURCE_NAME="postgresql://user:password@host:port/dbname" postgres_exporter
      

      如果您从分发包中安装了导出器,则可能有一个 systemd 单元文件可以将其作为服务运行:

      # systemctl cat prometheus-postgres-exporter
      # /lib/systemd/system/prometheus-postgres-exporter.service
      
      [Unit]
      Description=Prometheus PostgreSQL Exporter
      Wants=network-online.target
      
      [Service]
      User=postgres
      ExecStart=/usr/sbin/postgres-exporter $OPTIONS
      
      # You can place environment variables in this file ⤋
      EnvironmentFile=/etc/default/prometheus-postgres-exporter
      
      [Install]
      WantedBy=multi-user.target
      

      【讨论】:

      • 我应该将 DATA_SOURCE_NAME="user=postgres host=database.example.com" postgres_exporter 放入 [SERVICE] 部分吗?
      • @MarinaVinograd 不,这是手动运行它的示例。查看我共享的单元文件,有EnvironmentFile 选项。您放入该文件的每个环境变量都将提供给ExecStart 执行的任何内容。任何用户通常都可以读取单元文件,因此保存数据库密码并不是最好的主意。虽然没有什么能阻止您使用敏感数据限制对 EnvironmentFile 的访问。
      • ts=2021-12-16T07:48:47.778Z caller=log.go:168 level=error err="打开与数据库的连接时出错(无法解析 DATA_SOURCE_NAME):pq:密码验证失败 fo
      • @MarinaVinograd 试试这种格式:DATA_SOURCE_NAME="postgresql://user:password@host:port/dbname"
      猜你喜欢
      • 2012-07-27
      • 2015-05-06
      • 2021-10-19
      • 2013-05-14
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 2016-11-21
      • 1970-01-01
      相关资源
      最近更新 更多