歡迎來到普羅米修斯!
Prometheus是一個監控平台,通過從監控目標的抓取HTTP端點上獲取指標。
本指南將展示如何使用和安裝Promethues,配置和監視第一個資源。還將下載並安裝導出器Exporter,它是在主機和服務上公開時間序列數據的工具。第一個Exporter程序是Promethues本身,它提供大量關於內存使用、垃圾回收等等各種主機級別指標。
一、安裝Prometheus
1、下載Prometheus Server
官方下載地址:https://prometheus.io/download/
下載二進制包"prometheus-{version}.linux-amd64.tar.gz"即可!
2、安裝Prometheus Server
將二進制包解壓到指定目錄后,即可完成安裝。
root@localhost:~# mkdir /usr/local/promethus
root@localhost:~# tar xzvf prometheus-2.24.0.linux-amd64.tar.gz -C /usr/local/promethus/
3、配置Prometheus Server
在啟動Prometheus之前,需要先配置它。
Prometheus配置是YAML格式。在安裝完成后會附帶一個名為"prometheus.yml"的示例配置文件,這是一個很好的開始。
示例配置文件中有三個配置塊:
- global,全局配置塊,控制Prometheus Server的全局配置。
- rule_files,加載規則文件配置,rule_files塊指定要加載的任何規則文件的位置。
- scrape_configs,抓取配置,控制Prometheus要監視的資源。
root@localhost:/usr/local/promethus/prometheus-2.24.0.linux-amd64# vim prometheus.yml
# 全局配置(global)
global:
scrape_interval: 15s # 設置抓取間隔時間,默認是1分鍾。
evaluation_interval: 15s # 評估規則間隔時間,默認是1分鍾,Prometheus通過使用規則來創建新的時間序列或
# 者觸發警報。
# 抓取超時時間"scrape_timeout"在全局配置塊(global)中默認是10s。
# 警報管理器(Alertmanager)配置
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 加載規則(rule)並根據全局中的設置的'evaluation_interval'定期評估它們。
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# 抓取(scrape)配置
# 這里只有Prometheus本身。由於Prometheus還將關於自身的數據公開為HTTP端點,因此它可以抓取並監視其自身的
# 運行狀況。
scrape_configs:
# 作業名稱(job name)
# 作業名稱將作為標簽 `job=<job_name>` 添加到從該配置中提取的任何時間序列中。
- job_name: 'prometheus'
# 指標的路徑metrics_path默認是'/metrics'
# 訪問協議方案scheme默認是'http'。
# 靜態配置
# 監控目標(target):在本地端口9090上。
static_configs:
- targets: ['localhost:9090']
3、啟動Prometheus Server
切換到Prometheus目錄下指定配置文件運行即可,如果想要在全局運行,請使用全局路徑。
默認情況下Prometheus的時序數據存儲在"./data"目錄下,可以使用"--storage.tsdb.path"選項手動指定時序數據存儲目錄。
root@localhost:~# cd /usr/local/promethus/prometheus-2.24.0.linux-amd64
root@localhost:/usr/local/promethus/prometheus-2.24.0.linux-amd64# ./prometheus --config.file=./prometheus.yml --storage.tsdb.path=./data
4、訪問到WEB控制台
服務啟動完成后,可以通過"主機IP:9090"訪問到有關於Prometheus自身的狀態頁(WEB控制台)。給它幾秒鍾的時間它就會從自己的HTTP端點上采集數據。
可以訪問"主機IP:9090/metrics"來導航到指標端點來驗證Prometheus是否為自己提供了指標。
root@localhost:~# curl localhost:9090/metrics
go_gc_duration_seconds{quantile="0"} 9.521e-05
go_gc_duration_seconds{quantile="0.25"} 0.000166067
go_gc_duration_seconds{quantile="0.5"} 0.000210993
go_gc_duration_seconds{quantile="0.75"} 0.00023996
go_gc_duration_seconds{quantile="1"} 0.000354466
go_gc_duration_seconds_sum 0.01531654
go_gc_duration_seconds_count 72
...
二、在表達式瀏覽器中使用PromQL查詢時間序列
表達式瀏覽器(Expression Browser),Prometheus提供了一個WEB UI頁面,可以使用PromQL表達式來查詢和操作時序數據。
篩選時間序列
比如想要查看Prometheus采集指標的實際間隔時間,在WEB控制台的"Gtaph"頁的搜索欄輸入要搜索的指標"prometheus_target_interval_length_seconds",然后點擊"Execute"即可獲取到不同的時間序列數據。
如果只對0.99延遲的時間序列感興趣的話(滿足條件的時間序列),則可以使用"prometheus_target_interval_length_seconds{quantile="0.99"}",基於標簽值不同來篩選出特定的時間序列。
一行時間序列由多個"標簽"和左側的值組成。
如果想要統計指標的數量時,則可以使用表達式"count(prometheus_target_interval_length_seconds)"。
繪制圖形
Prometheus還支持使用圖形表達式來基於時間序列數據來自動繪制圖形。
在WEB控制台"http://主機IP:9090/graph"點擊"Graph"選項即可。
比如,想要繪制Prometheus創建數據塊的每秒速率的圖形,則可以使用表達式"rate(prometheus_tsdb_head_chunks_created_total[1m])"。
三、使用Exporter采集數據
添加一些新的監控目標(Target)用於演示。
添加一個節點導出器(Node Exporter)作為目標(Target),我們需要先安裝它。
使用Node Exporter可以監視Linux主機指標,它公開了各種各樣的硬件和內核相關指標。
官方下載地址:https://prometheus.io/download/#node_exporter
1、安裝Node Exporter
root@localhost:~# tar xzvf node_exporter-1.0.1.linux-amd64.tar.gz -C /usr/local/promethus/
root@localhost:~# cd /usr/local/promethus/node_exporter-1.0.1.linux-amd64/
2、啟動Node Exporter
這將會在主機上啟動一個HTTP端點,我們也可以直接訪問這個端點來獲取到指標。
root@localhost:/usr/local/promethus/node_exporter-1.0.1.linux-amd64# ./node_exporter --web.listen-address 127.0.0.1:8080
root@localhost:~# ss -alunpt |grep 8080
tcp LISTEN 0 128 127.0.0.1:8080 *:* users:(("node_exporter",pid=17413,fd=3))
root@localhost:~# curl localhost:8080/metrics
promhttp_metric_handler_requests_total{code="200"} 1
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0
...
3、配置Prometheus Server
在Prometheus Server配置文件的"scrape_configs"中,添加一個作業,目標指向Node Exporter。
root@localhost:~# cd /usr/local/promethus/prometheus-2.24.0.linux-amd64
root@localhost:/usr/local/promethus/prometheus-2.24.0.linux-amd64# vim prometheus.yml
scrape_configs:
...
# monitor linux node
- job_name: 'node'
static_configs:
- targets: ['localhost:8080'] # 指定目標,關聯Node Exporter
labels: # 添加一個標簽,用於識別數據。
group: 'production'
4、重啟Prometheus Server
root@localhost:/usr/local/promethus/prometheus-2.24.0.linux-amd64# ./prometheus --config.file=./prometheus.yml --storage.tsdb.path=./data
5、測試
打開瀏覽器,訪問到WEB控制台,輸入表達式"node_cpu_seconds_total"已獲取到關於主機CPU使用的相關指標的時序數據。此時已驗證Prometheus Server從Node Exporter上獲取到相關的監控數據。
四、配置Recoding Rules聚合數據
配置規則將抓取到的數據聚合到新的時間序列中。
盡管可以直接使用表達式從目標中獲取到數據,但是當聚合數千個時間序列的時候,查詢可能會變慢,所以為了提供查詢效率,Prometheus允許通過將表達式預錄成一個規則(rule),並每隔一段時間(由"evaluation_interval"控制)應用一次規則,將應用規則生成的數據寫入到新的時間序列中。
假設我們想要記錄5分鍾內所有CPU的每秒速率(node_cpu_seconds_total)的平均值,並且保留job、instance和mode標簽,則我們可以使用下面表達式
avg by (job, instance, mode) (rate(node_cpu_seconds_total[5m]))
控制台操作:
記錄規則(Recoding Rules)的使用
1、創建規則配置文件
root@localhost:~# cd /usr/local/promethus/prometheus-2.24.0.linux-amd64/
root@localhost:/usr/local/promethus/prometheus-2.24.0.linux-amd64# mkdir rules
root@localhost:/usr/local/promethus/prometheus-2.24.0.linux-amd64# vim rules/prometheus.rules.yml
groups:
- name: cpu-node # 規則名稱
rules:
- record: job_instance_mode:node_cpu_seconds:avg_rate5m # 規則記錄名稱
expr: avg by (job, instance, mode) (rate(node_cpu_seconds_total[5m])) # 生成規則所使用的表達式
2、配置Prometheus Server關聯規則文件
root@localhost:/usr/local/promethus/prometheus-2.24.0.linux-amd64# vim prometheus.yml
...
rule_files:
- "/usr/local/promethus/prometheus-2.24.0.linux-amd64/rules/prometheus.rules.yml"
...
3、重啟Prometheus Server
root@localhost:/usr/local/promethus/prometheus-2.24.0.linux-amd64# ./prometheus --config.file=./prometheus.yml --storage.tsdb.path=./data
4、訪問WEB控制台
在WEB控制台直接使用規則記錄的名稱"job_instance_mode:node_cpu_seconds:avg_rate5m"即可搜索到對應的時間序列。
五、使用Grafana展示數據
Grafana是一個開源的圖形化數據展示儀表盤應用。
Grafana支持Prometheus查詢。從Grafana 2.5.0 (2015-10-28)開始Prometheus可以作為它的數據源。
以下顯示了一個Grafana dashboard的一個示例,它查詢Prometheus的數據。
安裝(Installing)
官方下載地址:https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1
注:建議下載Open-Source版本。
1、下載安裝包
[root@localhost ~]# mkdir /usr/local/grafana
[root@localhost ~]# cd /usr/local/grafana/
[root@localhost grafana]# wget https://dl.grafana.com/oss/release/grafana-7.5.3.linux-amd64.tar.gz
2、解壓包直接啟動服務即可
[root@localhost grafana]# tar xzvf grafana-7.5.3.linux-amd64.tar.gz
[root@localhost grafana]# cd grafana-7.5.3
[root@localhost grafana-7.5.3]# bin/grafana-server
3、瀏覽器訪問
訪問地址:http://192.168.122.129:3000/login
默認情況下監聽端口是3000,默認登陸賬號密碼:admin:admin
修改密碼:
使用(Using)
1、創建一個Prometheus數據源
要在Grafana中創建一個Prometheus數據來源,請執行以下操作:
(1)點擊側邊欄中的"齒輪"打開配置菜單。
(2)點擊"Data Sources"數據源。
(3)點擊"Add data source"添加數據源。
(4)選擇"Prometheus"類型。
(5)設置適當的Prometheus Server URL(例如,http://localhost:9090/)
(6)根據需要調整其他數據源設置。
(7)然后點擊"Save & Test"保存資源配置。
2、創建一個Prometheus圖形
按照標准方法添加一個Grafana graph。如下:
(1)點擊側邊欄中的"+Dashboard"新建一個儀表盤。
(2)選擇數據源為"Prometheus",在左下角"Query"字段下。
(3)在"Query"字段下的"Metric"旁輸入要使用的查詢表達式,可以點擊"Metric"選擇並自動補全指標名稱。
(4)要格式化時間序列的圖例名稱,請輸入"Legend name"。例如,僅顯示返回查詢結果的方法"method"和狀態"status"標簽,則可以使用破折號間隔,可以使用圖例格式字符串"{{method}} - {{status}}"。
(5)右側"Visualization"可以選擇圖形,直到選擇一個可用的圖形即可。
3、導入預構建的儀表盤
Grafana提供預構建的儀表盤,由Grafana官方內置或第三方社區提供,可以直接復制編號將
預構建的儀表盤導入到Grafana。
訪問地址:https://grafana.com/grafana/dashboards
六、配置警報,發送報警通知到釘釘
警報的觸發是在Prometheus中配置警報規則(Alert Rules)決定的,當觸發警報時,會將警報發送到警報管理器(Alermanager)中,然后由Alertmanger將警報發送到電子郵箱、釘釘、企業微信等位置實現報警通知。
Alermanager具有抑制、沉默、分組警報的功能。
- 分組(group_by),分組將性質相似的警報分類並聚合為單個通知(配置路由時,允許根據警報的標簽分組警報)。
- 抑制(inhibit_rule),當某個問題警報產生並已發送,由該問題引起的一系列的其他警報允許被抑制(由配置抑制規則控制)。
- 沉默(silebces),忽略某些警報(在Alertmanager的Web控制台進行操作)。
釘釘
創建釘釘機器人,配置安全設置,獲取機器人URL。
在釘釘群創建一個釘釘機器人即可。
使用加簽的安全驗證消息方式,接受通知消息。
prometheus-webhook-dingtalk
prometheus-webhook-dingtalk是一個專用於Alertmanager通過Webhook發送警報通知到釘釘機器人的小程序,在Github開源。如果你熟悉Python也可以自己編寫一個推送程序。
地址:https://github.com/timonwong/prometheus-webhook-dingtalk
1、安裝依賴Nodejs
[root@localhost ~]# mkdir /usr/local/nodejs
[root@localhost ~]# cd /usr/local/nodejs
[root@localhost nodejs]# wget https://nodejs.org/dist/v16.0.0/node-v16.0.0-linux-x64.tar.xz
[root@localhost nodejs]# tar xf node-v16.0.0-linux-x64.tar.xz
[root@localhost nodejs]# vim /etc/profile
# nodejs
export PATH=/usr/local/nodejs/node-v16.0.0-linux-x64/bin:$PATH
[root@localhost nodejs]# source /etc/profile
[root@localhost nodejs]# node -v
v16.0.0
2、安裝依賴Yarn
[root@localhost nodejs]# npm install -g yarn
3、安裝依賴Go
[root@localhost nodejs]# yum -y install golang
4、下載解壓並編譯包
[root@localhost nodejs]# cd /usr/local/prometheus/
[root@localhost nodejs]# rz
[root@localhost prometheus]# unzip prometheus-webhook-dingtalk-master.zip
[root@localhost prometheus]# cd prometheus-webhook-dingtalk-master
[root@localhost prometheus-webhook-dingtalk-master]# make build
5、編寫自定義通知模板
該模板基於Go語言,是已經定義好的,通用的,直接引用即可。
注:如果想要學習如何編寫模板,則可以學習Go Template。
[root@localhost prometheus-webhook-dingtalk-master]# vim contrib/templates/template.tmpl
{{ define "dingding.default.message" }}
{{/* 告警通知 */}}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 -}}
====== 告警通知 ======
警報名稱: {{ $alert.Labels.alertname }}
嚴重程度: {{ $alert.Labels.severity }}
{{- end }}
警報分組: {{ $alert.Labels.group }}
警報標題: {{ $alert.Annotations.summary }}
詳細信息: {{ $alert.Annotations.description }}
故障時間: {{ dateInZone "2006.01.02 15:04:05" ($alert.StartsAt) "Asia/Shanghai" }}
{{ if gt (len $alert.Labels.instance) 0 -}}故障實例: {{ $alert.Labels.instance }}{{- end -}}
{{- end }}
{{- end }}
{{/* 恢復通知 */}}
{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 -}}
====== 恢復通知 ======
警報名稱: {{ $alert.Labels.alertname }}
嚴重程度: {{ $alert.Labels.severity }}
{{- end }}
警報分組: {{ $alert.Labels.group }}
警報標題: {{ $alert.Annotations.summary }}
詳細信息: {{ $alert.Annotations.description }}
故障時間: {{ dateInZone "2006.01.02 15:04:05" ($alert.StartsAt) "Asia/Shanghai" }}
恢復時間: {{ dateInZone "2006-01-02 15:04:05" ($alert.EndsAt) "Asia/Shanghai" }}
{{ if gt (len $alert.Labels.instance) 0 -}}故障實例: {{ $alert.Labels.instance }}{{- end -}}
{{- end }}
{{- end }}
{{- end }}
6、配置prometheus-webhook-dingtalk
[root@localhost prometheus-webhook-dingtalk-master]# cp config.example.yml config.yml
[root@localhost prometheus-webhook-dingtalk-master]# vim config.yml
## 請求超時時間
# timeout: 5s
## 不使用內置模板開關
#no_builtin_template: true
## 自定義模板路徑
templates:
- contrib/templates/template.tmpl
## 默認使用的消息模板
## 建議使用下方目標中配置的"message"
#default_message:
# title: '{{ template "legacy.title" . }}'
# text: '{{ template "legacy.content" . }}'
## 配置目標(釘釘機器人),以及配置機器人
## 釘釘機器人限制,每分鍾不允許推送超過20條消息,所以我們可以配置多個"target"
targets:
webhook: # 目標名稱
# 釘釘機器人URL
url: https://oapi.dingtalk.com/robot/send?access_token=eaf8cb9e05094aedce70782a926fa4efcc001ce012612d47f7d0341cd3c16dd6
# 安全設置:加簽方式的密鑰字符串
secret: SECcd860c24697a1c06995eab8edee08f0542c9f68bcf778f07dba4291dd0337b74
# 消息內容
message:
title: '{{ template "dingding.default.message" . }}' # 標題
text: '{{ template "dingding.default.message" . }}' # 內容
# @群組中指定手機號人(mobiles)或所有人(all)
#mention:
#all: true
#mobiles: ['156xxxx8827', '189xxxx8325']
7、啟動服務
[root@localhost prometheus-webhook-dingtalk-master]# nohup ./prometheus-webhook-dingtalk --web.listen-address="192.168.122.129:8060" --web.enable-ui --web.enable-lifecycle --config.file="config.yml" >./prometheus-webhook-dingtalk.log 2>&1 & echo $!>./prometheus-webhook-dingtalk.pid &
[root@localhost prometheus-webhook-dingtalk-master]# netstat -lnupt |grep 8060
tcp 0 0 192.168.122.129:8060 0.0.0.0:* LISTEN 35583/./prometheus-
8、通過程序日志獲取服務開放的API URL
[root@localhost prometheus-webhook-dingtalk-master]# tail -f prometheus-webhook-dingtalk.log
level=info ts=2021-04-27T08:00:01.779Z caller=main.go:117 component=configuration msg="Loading templates" templates=contrib/templates/legacy/template.tmpl
ts=2021-04-27T08:00:01.780Z caller=main.go:133 component=configuration msg="Webhook urls for prometheus alertmanager" urls=http://192.168.122.129:8060/dingtalk/webhook/send
9、訪問到WebUI
在瀏覽器中輸入"主機地址:端口/ui/"即可訪問到prometheus-webhook-dingtalk提供的一個WebUI控制台,可以在其中查看一些服務配置、狀態信息等。
10、手動推送往接口推送個消息測試一下
[root@localhost ~]# curl 'http://192.168.122.129:8060/dingtalk/webhook/send' -H 'Content-Type: application/json' -d '{"msgtype": "text","text": {"content": "我就是我, 是不一樣的煙火"}}'
OK
Alertmanger
1、安裝Alertmanger
二進制包下載地址:https://prometheus.io/download/
解壓包到指定目錄即可!
[root@localhost ~]# cd /usr/local/prometheus/
[root@localhost prometheus]# tar xzvf alertmanager-0.21.0.linux-amd64
2、配置Alertmanager
配置Alertmanager,關聯prometheus-webhook-dingtalk。
[root@localhost prometheus]# cd alertmanager-0.21.0.linux-amd64
[root@localhost alertmanager-0.21.0.linux-amd64]# vim alertmanager.yml
global:
resolve_timeout: 5m
route:
group_by: ['alertname'] # 根據標簽分組警報
group_wait: 10s # 一組警報的等待時間
group_interval: 10s # 一組警報發送的時間間隔
repeat_interval: 1m # 重新發送警報的時間間隔,這邊設置的間隔時間較小,便於測試
receiver: 'dingding' # 定義默認使用的通知消息接收器
receivers:
- name: 'dingding'
webhook_configs:
# prometheus-webhook-dingtalk提供的webhook1的API URL
- url: 'http://192.168.122.129:8060/dingtalk/webhook/send'
send_resolved: true # 允許發送恢復通知
# 抑制規則,具有相同標簽,已經發送了severity='critical'的警報,則新產生severity='warning'的警報不會發送
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
3、啟動服務
[root@localhost alertmanager-0.21.0.linux-amd64]# nohup ./alertmanager --config.file="alertmanager.yml" --storage.path="data/" --web.listen-address="192.168.122.129:9093" --web.timeout=0 >./alertmanager.log 2>&1 & echo $!>./alertmanager.pid
[root@localhost alertmanager-0.21.0.linux-amd64]# netstat -lnupt |grep alert
tcp6 0 0 :::9093 :::* LISTEN 32206/./alertmanage
tcp6 0 0 :::9094 :::* LISTEN 32206/./alertmanage
udp6 0 0 :::9094 :::* 32206/./alertmanage
4、訪問到WebUI
Alertmanager提供一個WebUI,可以在上面沉默警報,以及查看接收到的警報以及服務狀態、配置信息等。
Prometheus
1、配置Prometheus
配置Prometheus關聯到Alertmanager。
[root@localhost alertmanager-0.21.0.linux-amd64]# cd /usr/local/prometheus/prometheus-2.25.0.linux-amd64
[root@localhost prometheus-2.25.0.linux-amd64]# vim prometheus.yml
alerting:
alertmanagers:
- static_configs:
- targets:
- 192.168.122.129:9093 # 關聯Alertmanger警報管理器
rule_files:
- "alert_rules.yml" # 指定警報規則配置文件
2、編寫警報規則(alert_rules)
這邊編寫一個警報規則,用於測試,當檢測到主機的有效內存小於4GB時,則觸發警報。
[root@localhost prometheus-2.25.0.linux-amd64]# vim alert_rules.yml
groups:
- name: test
rules:
- alert: MemAvailableLess # 警報名稱
expr: node_memory_MemAvailable_bytes / 1024 / 1024 < 4096 # 有效內存小於4096MB
for: 1m # 每1分鍾檢查一次
labels:
severity: Warning # 設置該警報的嚴重程度為"Warning"
annotations: # 警報注解信息
summary: "Instance {{$labels.instance}} Available Memory Less!"
description: "{{$labels.instance}} of job {{$labels.job}} the available effective memory is less than 4096MB."
3、重載配置
向Prometheus進程發送HUP信號以重新加載配置。
[root@localhost prometheus-2.25.0.linux-amd64]# netstat -lnupt |grep prometheus
tcp6 0 0 :::9090 :::* LISTEN 30430/./prometheus
[root@localhost prometheus-2.25.0.linux-amd64]# kill -HUP 30430
4、可以在瀏覽器中查看已配置的警報
觀察警報發送到釘釘情況
1、查看prometheus-webhook-dingtalk-master日志
我們可以通過日志看到警報已經發送到了釘釘群中,以及其發送過程中是否出現問題!
[root@localhost prometheus-webhook-dingtalk-master]# tail -f prometheus-webhook-dingtalk.log
level=info ts=2021-04-27T08:58:30.669Z caller=entry.go:22 component=web http_scheme=http http_proto=HTTP/1.1 http_method=POST remote_addr=192.168.122.129:35580 user_agent=Alertmanager/0.21.0 uri=http://192.168.122.129:8060/dingtalk/webhook/send resp_status=200 resp_bytes_length=2 resp_elapsed_ms=279.554657 msg="request complete"
2、查看釘釘群
可以看到釘釘已經接收到了報警消息!