Prometheus+Grafana

安裝 postgres_exporter

  1. 下載 postgres_exporter
  2. 安裝 postgres_exporter
    • 雙擊下載的 MSI 文件,按照提示完成安裝。

  1. 建立postgres_exporter.yml(可選)

    參考postgres_exporter 說明文件

    • web.systemd-socket Use systemd socket activation listeners instead of port listeners (Linux only). Default is false
    • web.listen-address Address to listen on for web interface and telemetry. Default is :9187.
    • web.config.file Configuration file to use TLS and/or basic authentication. The format of the file is described in the exporter-toolkit repository. …

  1. 設置環境變數、開啟postgres_exporter:
    $env:DATA_SOURCE_NAME = "user=yourusername`n" + `
                            "password=yourpassword`n" + `
                            "host=localhost port=5432`n" + `
                            "dbname=yourdatabase sslmode=disable"
     
    .\postgres_exporter.exe --config.file="postgres_exporter.yml"

  1. 確認 postgres_exporter 是否正常工作

    • 在瀏覽器中訪問 http://localhost:9187/metrics

安裝 Prometheus

  1. 下載 Prometheus
  2. 解壓安裝包
    • 將下載的 ZIP 檔案解壓到您選擇的目錄,例如 C:\\Prometheus

  1. 配置 Prometheus
    • C:\Prometheus 目錄中創建一個名為 prometheus.yml 的配置檔案,並添加以下內容:
      global:
        scrape_interval: 15s
      scrape_configs:
        - job_name: 'postgres_exporter'
      	static_configs:
      	  - targets: ['localhost:9187']
    • 這裡的 targets 是您將要監控的 postgres_exporter 的地址,可以查看啟動的postgres_exporter紀錄的port。

  1. 啟動 Prometheus
    • 打開命令提示字元,導航到 C:\\Prometheus,運行以下命令啟動 Prometheus:

      .\prometheus.exe --config.file=prometheus.yml
    • 您可以在瀏覽器中訪問 http://localhost:9090 查看 Prometheus 的界面。


安裝 Grafana

  1. 下載 Grafana
  2. 安裝 Grafana
    • 解壓下載的檔案並按照說明進行安裝。安裝完成後,Grafana 通常會在端口 3000 上運行。
  3. 啟動 Grafana
    • 打開命令提示字元,導航到 Grafana 的安裝目錄,運行 grafana-server.exe 啟動服務。

  1. 配置 Grafana
    • 在瀏覽器中訪問 http://localhost:3000,使用預設帳號(admin/admin)登錄。
    • 添加數據源:選擇 “Configuration” > “Data Sources” > “Add data source”,選擇 Prometheus,並填入 URL 為 http://localhost:9090
    • 保存並測試數據源連接。

  1. 創建儀表板
    1. Grafana dashboards | Grafana Labs找一個模板 width:700px height:500px

  1. 創建儀表板
    1. 複製模板ID
    2. 回到自己的server,點左側功能表欄
    3. 單擊儀錶板圖示以導入儀錶板 ID 或上傳我們從 Grafana PostgreSQL 儀錶板下載的儀錶板 JSON 檔。 width:700px height:300px

Reference

  1. https://prometheus.io/download/
  2. https://github.com/prometheus-community/postgres_exporter/releases
  3. https://github.com/prometheus-community/postgres_exporter
  4. https://grafana.com/grafana/download?edition=oss&platform=windows
  5. Grafana dashboards | Grafana Labs
  6. 使用 Prometheus 和 Grafana 監控 PostgreSQL