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/n/240285.html
微信扫一扫
支付宝扫一扫