Prometheus-v2.45.0 + 钉钉告警
目录
1. 创建一个钉钉内部群
2. 添加自定义机器人
3. 配置钉钉发送告警服务
4. 配置alertmanager,接入dingding
5. 配置告警消息发送模板
6. 修改配置规则文件
续接上篇:https://blog.csdn.net/Lzcsfg/article/details/140851688
相关软件包链接:https://pan.baidu.com/s/1b3AzJBNtIt1PfjNi1eE7fg?pwd=jcj8
提取码:jcj8
1. 创建一个钉钉内部群
2. 添加自定义机器人
3. 配置钉钉发送告警服务
传入软件包prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
[root@localhost ~]# cd /opt
解压软件包
[root@localhost opt]# tar xf prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
将目录名字改成短点的
[root@localhost opt]# mv prometheus-webhook-dingtalk-2.1.0.linux-amd64 webhook-dingtalk
[root@localhost opt]# cd webhook-dingtalk/
拷贝配置文件并重命名
[root@localhost webhook-dingtalk]# cp config.example.yml config.yml
编辑配置文件
[root@localhost webhook-dingtalk]# vim config.yml
# 配置块,用于定义钉钉机器人的Webhook
targets:# 第一个Webhook配置,用于发送消息,但没有启用@提醒功能webhook1:url: https://oapi.dingtalk.com/robot/send?access_token=2a6adaa1f19ef7ac61e57410c9f2f6c9b00c417842a766d99765f644f80d48d0secret: SEC85d2833b679817a49b1197aa0f5699e6c0e000eb5cb90e74da756c32b48eee75# 第二个Webhook配置,用于发送消息,并启用所有用户的@提醒功能another_webhook_mention_all:url: https://oapi.dingtalk.com/robot/send?access_token=2a6adaa1f19ef7ac61e57410c9f2f6c9b00c417842a766d99765f644f80d48d0mention:all: true # 启用所有用户的@提醒# 第三个Webhook配置,用于发送消息,并指定需要@提醒的手机号码webhook_mention_users:url: https://oapi.dingtalk.com/robot/send?access_token=2a6adaa1f19ef7ac61e57410c9f2f6c9b00c417842a766d99765f644f80d48d0mention:mobiles: ['130xxxxx151'] # 指定需要@提醒的手机号码
启动
[root@localhost webhook-dingtalk]# ll
total 18744
-rw-r--r-- 1 3434 3434 11358 Apr 21 2022 LICENSE
-rw-r--r-- 1 3434 3434 1299 Apr 21 2022 config.example.yml
-rw-r--r-- 1 root root 265 Aug 2 02:32 config.yml
drwxr-xr-x 4 3434 3434 34 Apr 21 2022 contrib
-rwxr-xr-x 1 3434 3434 19172733 Apr 21 2022 prometheus-webhook-dingtalk[root@localhost webhook-dingtalk]# ./prometheus-webhook-dingtalk --config.file="config.yml"
# 后台启动命令使用 nohup ./prometheus-webhook-dingtalk --config.file="config.yml" &# 会监听8060端口
[root@localhost webhook-dingtalk]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=819,fd=3))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=819,fd=4))
LISTEN 0 4096 *:3000 *:* users:(("grafana",pid=6705,fd=10))
LISTEN 0 4096 *:9100 *:* users:(("node_exporter",pid=6303,fd=3))
LISTEN 0 4096 *:9093 *:* users:(("alertmanager",pid=6835,fd=8))
LISTEN 0 4096 *:9094 *:* users:(("alertmanager",pid=6835,fd=3))
LISTEN 0 4096 *:9090 *:* users:(("prometheus",pid=6873,fd=7))
LISTEN 0 4096 *:8060 *:* users:(("prometheus-webh",pid=6996,fd=3))
4. 配置alertmanager,接入dingding
[root@localhost webhook-dingtalk]# vim /opt/alertmanager/alertmanager.yml
receivers:- name: 'dingding' # 接收器的名字,定义为 'dingding'webhook_configs:- url: 'http://192.168.226.29:8060/dingtalk/webhook1/send'send_resolved: true # 当报警被解决时,也会发送通知route:receiver: 'dingding' # 默认的接收器是 'dingding'group_by: ['alertname'] # 报警分组的依据字段,这里按 'alertname' 分组group_wait: 30s # 第一个报警的等待时间,在此期间新的报警会被加入到同一组group_interval: 1m # 每组之间的最小等待时间repeat_interval: 5m # 重复报警发送的时间间隔templates:- /opt/alertmanager/tmpl/*.tmpl # 指定模板文件的路径,用于格式化报警通知的内容inhibit_rules:- source_match:severity: 'critical' # 源报警的匹配条件:严重性为 'critical'target_match:severity: 'warning' # 目标报警的匹配条件:严重性为 'warning'equal: ['alertname', 'dev', 'instance'] # 抑制规则生效的条件,只有当 'alertname'、'dev' 和 'instance' 三个字段相同时,规则才会生效
重启alertmanager
[root@localhost webhook-dingtalk]# cd /opt/alertmanager/
[root@localhost alertmanager]# nohup ./alertmanager &
5. 配置告警消息发送模板
[root@localhost alertmanager]# vim /opt/alertmanager/tmpl/email.tmpl
{{ define "__subject" }}[告警] {{ .Status | toUpper }}: {{ .CommonLabels.alertname }}{{ end }}{{ define "__description" }}
告警名称: {{ .CommonLabels.alertname }}
主机: {{ .CommonLabels.instance }}
告警级别: {{ .CommonLabels.severity }}
开始时间: {{ .StartsAt }}
结束时间: {{ .EndsAt }}
描述: {{ .CommonAnnotations.description }}
{{ end }}{{ define "__message" }}
{"msgtype": "markdown","markdown": {"title": "{{ template "__subject" . }}","text": "## {{ template "__subject" . }}\n\n{{ template "__description" . }}"}
}
{{ end }}{{ define "dingtalk.default.message" }}{{ template "__message" . }}{{ end }}
6. 修改配置规则文件
在prometheus中的prometheus.yml指定了规则文件位置
[root@localhost alertmanager]# cd /opt/prometheus
[root@localhost prometheus]# vim ./rules/disk.yml
groups:- name: 'disk-usage-alerts'# 这是告警组的名称rules:- alert: DiskSpaceUsageHigh# 这是告警规则的名称expr: 100 - (node_filesystem_avail_bytes / node_filesystem_size_bytes * 100) > 10# 告警表达式检查磁盘使用率是否超过10%for: 30s# 如果条件持续30秒,则触发告警labels:severity: 'critical'# 为告警添加标签,指定严重性为“关键”annotations:summary: '磁盘使用率过高: {{ $labels.instance }}'# 摘要注释,描述告警的内容,包括实例名称description: '磁盘使用率超过阈值: {{ $value }}%'# 描述注释,给出具体的磁盘使用率
重启prometheus
[root@localhost alertmanager]# prometheus restart