【问题标题】:从 prometheus 导出器连接到数据库
【发布时间】:2022-01-17 13:34:54
【问题描述】:

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

prometheus.yml:

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

【问题讨论】:

    标签: postgresql environment-variables prometheus grafana


    【解决方案1】:

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

    是的,这是可能的。以下是支持的变量列表: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:密码验证失败佛
    • @MarinaVinograd 试试这个格式:DATA_SOURCE_NAME="postgresql://user:password@host:port/dbname"
    猜你喜欢
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 2014-07-14
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多