Prometheus是一個開源監控系統,可使用釘釘作為其中的告警渠道。本文將從選取3~5個與prometheus釘釘告警相關的方面,進行詳細的闡述。
一、告警配置
在Prometheus配置文件中可以定義告警規則,在告警規則中對告警進行設置,配置如下:
groups:
- name: alert.rules
rules:
- alert:InstanceDown
expr: up == 0
for: 1m
labels:
severity: warning
annotations:
summary: "Instance {{ $labels.instance }} is down"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minute."
其中,groups為告警組名,可以自定義,rules為告警規則內容,其中包含了告警的expr,如果expr判斷為true,則觸發告警。for為持續時間,如果在for的時間內,expr仍為true,則持續觸發告警。labels包含了告警的標籤信息,而annotations則包含了告警的更多文字描述。在prometheus.yml文件中進行配置即可。
二、告警消息
在Prometheus配置文件中可以定義告警消息,這些消息將被釘釘發送。告警消息模板可以是靜態的,也可以包含變量,如下所示:
templates:
- name: dingding
text: '{{ range .Alerts }}故障類型: {{ .Annotations.summary }}\n故障詳情: {{ .Annotations.description }}\n告警級別: {{ .Labels.severity }}\n告警狀態: {{ .Status }}\n告警時間: {{ .StartsAt }}\n告警實例: {{ .Labels.instance }}\n告警作業: {{ .Labels.job }}\n{{ end }}'
其中templates為模板名,可以自定義,text則為消息模板內容。Prometheus在告警觸發後,將解析消息模板,將告警信息填充至模板中,然後使用釘釘機械人將消息發送出去。
三、釘釘機械人設置
要將prometheus告警發送至釘釘,需要設置一個釘釘機械人,將其配置寫入消息模板中。
url: https://oapi.dingtalk.com/robot/send?access_token=your_access_token
其中your_access_token為自定義的access_token,可以通過釘釘管理後台獲取。在消息模板中填充此url即可實現告警發送至釘釘機械人。具體設置方法可以參考釘釘的官方文檔。
四、告警通道配置
Prometheus支持多種消息通道,包括郵件通道、Slack通道、Webhook通道、PagerDuty通道和釘釘通道等。在prometheus.yml文件中在每個告警規則配置中指定需要的告警通道即可。以釘釘告警為例:
- name: alertmanager
email_configs:
- to: 'test@example.com'
from: 'test@example.com'
smarthost: smtp.example.com:587
auth_username: 'test@example.com'
auth_identity: 'test@example.com'
auth_password: 'password'
send_resolved: true
text: '{{ range .Alerts }}{{ .Annotations.summary }}\n{{ .Annotations.description }}{{ end }}'
webhook_configs:
- url: 'http://localhost:5001/webhook/dingtalk'
send_resolved: true
其中webhook_configs為告警通道配置,url為通道地址,可以自定義,send_resolved為是否發送解決消息。
五、告警測試
在配置完Prometheus告警規則、消息模板、釘釘機械人和告警通道後,可以進行告警測試,測試方法如下:
curl -X POST http://localhost:9093/api/v1/alerts \
-H 'Content-Type: application/json' \
-d '[{
"labels": {
"alertname": "InstanceDown",
"instance": "localhost:9090",
"job": "prometheus"
},
"annotations": {
"description": "Prometheus instance localhost:9090 is down.",
"summary": "Prometheus instance down"
},
"generatorURL": "http://localhost:9090/graph?g0.expr=up+%3D%3D+0&g0.tab=1"
}]'
其中 curl命令中的json數據為模擬的告警內容,alertname為告警名,labels包含告警標籤信息,annotations包含告警說明信息,generatorURL為告警來源的URL。觸發後即可在釘釘中查看到相應的告警消息。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/240285.html
微信掃一掃
支付寶掃一掃