Prometheus-v2.45.0 + 钉钉告警

目录

[1. 创建一个钉钉内部群](#1. 创建一个钉钉内部群)

[2. 添加自定义机器人](#2. 添加自定义机器人)

[3. 配置钉钉发送告警服务](#3. 配置钉钉发送告警服务)

[4. 配置alertmanager,接入dingding](#4. 配置alertmanager,接入dingding)

[5. 配置告警消息发送模板](#5. 配置告警消息发送模板)

[6. 修改配置规则文件](#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

bash 复制代码
[root@localhost ~]# cd /opt

解压软件包

bash 复制代码
[root@localhost opt]# tar xf prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz

将目录名字改成短点的

bash 复制代码
[root@localhost opt]# mv prometheus-webhook-dingtalk-2.1.0.linux-amd64 webhook-dingtalk
bash 复制代码
[root@localhost opt]# cd webhook-dingtalk/

拷贝配置文件并重命名

bash 复制代码
[root@localhost webhook-dingtalk]# cp config.example.yml config.yml

编辑配置文件

bash 复制代码
[root@localhost webhook-dingtalk]# vim config.yml
# 配置块,用于定义钉钉机器人的Webhook
targets:
  # 第一个Webhook配置,用于发送消息,但没有启用@提醒功能
  webhook1:
    url: https://oapi.dingtalk.com/robot/send?access_token=2a6adaa1f19ef7ac61e57410c9f2f6c9b00c417842a766d99765f644f80d48d0
    secret: SEC85d2833b679817a49b1197aa0f5699e6c0e000eb5cb90e74da756c32b48eee75

  # 第二个Webhook配置,用于发送消息,并启用所有用户的@提醒功能
  another_webhook_mention_all:
    url: https://oapi.dingtalk.com/robot/send?access_token=2a6adaa1f19ef7ac61e57410c9f2f6c9b00c417842a766d99765f644f80d48d0
    mention:
      all: true  # 启用所有用户的@提醒

  # 第三个Webhook配置,用于发送消息,并指定需要@提醒的手机号码
  webhook_mention_users:
    url: https://oapi.dingtalk.com/robot/send?access_token=2a6adaa1f19ef7ac61e57410c9f2f6c9b00c417842a766d99765f644f80d48d0
    mention:
      mobiles: ['130xxxxx151']  # 指定需要@提醒的手机号码

启动

bash 复制代码
[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

bash 复制代码
[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

bash 复制代码
[root@localhost webhook-dingtalk]# cd /opt/alertmanager/
[root@localhost alertmanager]# nohup ./alertmanager &

5. 配置告警消息发送模板

bash 复制代码
[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指定了规则文件位置

bash 复制代码
[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

bash 复制代码
[root@localhost alertmanager]# prometheus restart
相关推荐
云游16 小时前
大模型性能指标的监控系统(prometheus3.5.0)和可视化工具(grafana12.1.0)基础篇
grafana·prometheus·可视化·监控
qq_232045572 天前
非容器方式安装Prometheus和Grafana,以及nginx配置访问Grafana
nginx·grafana·prometheus
夜莺云原生监控2 天前
Prometheus 监控 Kubernetes Cluster 最新极简教程
容器·kubernetes·prometheus
SRETalk3 天前
Prometheus 监控 Kubernetes Cluster 最新极简教程
kubernetes·prometheus
川石课堂软件测试3 天前
JMeter并发测试与多进程测试
功能测试·jmeter·docker·容器·kubernetes·单元测试·prometheus
SRETalk3 天前
夜莺监控的几种架构模式详解
prometheus·victoriametrics·nightingale·夜莺监控
Ditglu.4 天前
使用Prometheus + Grafana + node_exporter实现Linux服务器性能监控
服务器·grafana·prometheus
SRETalk5 天前
监控系统如何选型:Zabbix vs Prometheus
zabbix·prometheus
睡觉z5 天前
云原生环境Prometheus企业级监控
云原生·prometheus
归梧谣5 天前
云原生环境 Prometheus 企业级监控实战
云原生·prometheus