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連結
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuQjM2YTM3QTNy0SN2gzM1YTM1ATOwETM5EDMy0iMzETO4QTMvwVMxkTMwIzLcJzMxkDO0EzLcd2bsJ2Lc12bj5ycn9Gbi52YugTMwIzZtl2Lc9CX6MHc0RHaiojIsJye.png)
然後需要修改
/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端口: