Prometheus + Grafana


Prometheus

ubuntu安裝prometheus非常簡單:

apt update
apt install prometheus
systemctl enable prometheus
systemctl enable prometheus-node-exporter

apt安裝prometheus和prometheus-node-exporter之后便帶有基本配置,無需修改。

確保開啟服務開啟:

systemctl status prometheus
systemctl status prometheus-node-exporter

順便使用它監控mongodb,安裝prometheus-mongodb-exporter

apt install prometheus-mongodb-exporter
systemctl enable prometheus-mongodb-exporter

此外由於mongodb開啟了密碼驗證,需要注意mongodb用戶的權限:mongodb_exporter github鏈接

然后需要修改 /etc/default/prometheus-mongodb-exporter 中的 ARGS 如下:

# ARGS='-mongodb.uri="mongodb://localhost:27017"'
ARGS='-mongodb.uri="mongodb://xxx:xxxxx@localhost:27017"'

mongodb URI格式如下:

mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[database][?options]]

如果 username 或 password 包含 @ : / % 四種符號需要使用 百分號編碼.

錯誤添加需要刪除時用db.getSiblingDB("admin").dropUser("mongodb_exporter")

然后重啟一下服務

systemctl restart prometheus-mongodb-exporter

Grafana

安裝:

sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
apt update
apt install grafana

配置:

配置文件為 /etc/grafana/grafana.ini,注意如下內容:

...
[server]
domain = www.xxxx.com
enforce_domain = true
root_url = %(protocol)s://%(domain)s/grafana
...
[security]
admin_password = xxxx

然后訪問 www.xxxx.com/grafana 登錄,用戶名admin,密碼為上面設置的admin_password。

然后按照 [這里][https://github.com/percona/grafana-dashboards] 配置數據源使用prometheus,並導入面板。一般導入這些即可:

(注意:json中的pmm-singlestat-panel可能需要替換為singlestat

使用Viewer角色用戶登錄查看

上述Dashboard配置好之后,不應繼續使用admin登錄系統。

在設置中“邀請”用戶,填寫自己的郵箱然后通過郵箱鏈接設置密碼,即可以自己的郵箱登錄grafana。


注:

使用prometheus監控兩台服務器,配置文件 /etc/prometheus/prometheus.yml 內容如下:

# Sample config for Prometheus.

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.
  evaluation_interval: 15s # By default, scrape targets every 15 seconds.
  # scrape_timeout is set to the global default (10s).

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
      monitor: 'example'

# Load and evaluate rules in this file every 'evaluation_interval' seconds.
rule_files:
  # - "first.rules"
  # - "second.rules"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s
    scrape_timeout: 5s

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
        - targets: ['localhost:9090']

  - job_name: "web-server"
    # If prometheus-node-exporter is installed, grab stats about the local
    # machine by default.
    static_configs:
      - targets: ['localhost:9100']

  - job_name: "worker-node1"
    static_configs:
      - targets: ['192.168.0.5:9100']

這個配置是沒問題的,在另一台機器 (192.168.0.5) 上安裝並啟用 prometheus-node-exporter 即可。

但如果你僅僅修改了某個job_name(而沒有修改ip),比如把web-server改為node,那么grafana界面中的singlestat panel將不能正確顯示,顯示“Only queries that return single...”,
這是因為singlestat只能顯示一個結果,而查詢語句查到了兩個結果。解決方式是刪除之前的數據系列:

  • 首先停止prometheus服務,傳入--web.enable-admin-api參數手動運行

  • 然后這樣刪除:

curl -X POST     -g 'http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]={instance="localhost:9100"}'

參考鏈接:Prometheus: Delete Time Series Metrcs

注2:上述情況的查詢語句可能是這樣的(可以在grafana中看到):

(可以用instance="xxxxx"或job="xxxxx")

清理數據參考鏈接:

https://prometheus.io/docs/prometheus/2.3/querying/api/#delete-series

https://www.alibabacloud.com/help/zh/doc-detail/56246.htm

prometheus 默認是9090端口:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM