prometheus 通过企业微信接收告警 WeChat告警模版配置

实现WeChat 告警-准备工作

step 1: 访问网站 注册企业微信账号(不需要企业认证)。
step 2: 访问apps 创建第三方应用,点击创建应用按钮 -> 填写应用信息:
在这里插入图片描述
在这里插入图片描述
部门ID:to_party 需要发送的组
在这里插入图片描述
可以查看微信告警接口文档

prometheus 配置:
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      - localhost:9093

rule_files:
  - "rules.yml"

scrape_configs:
  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100']
    
rules.yml 配置:
groups:
- name: node
  rules:
  - alert: server_status
    expr: up{job="node"} == 0
    for: 15s
    annotations:
      summary: "机器 {{ $labels.instance }} 挂了"
alertmanger 配置:
[root@prometheus01 prometheus]# cat alertmanager.yml
global:
  # 每2分钟检查一次是否恢复
  resolve_timeout: 2m
  # SMTP的相关配置
  smtp_smarthost: smtp.263.net:587
  smtp_from: no-reply@xxx.com
  smtp_auth_username: no-reply@xxx.com 
  smtp_auth_password: xxx
# 自定义 通知的模板的 目录 或者 文件.
templates:
  - '/usr/local/prometheus/alertmanager/template/wechat.tmpl'
# 路由树的根节点, 每个传进来的报警从这里开始.
route:

  # 将传入的报警中有这些标签的分为一个组.
  # 比如, cluster=A 和 alertname=LatencyHigh 会分成一个组.
  group_by: ['alertname_wechat']
  
  # 指分组创建多久后才可以发送压缩的警报,也就是初次发警报的延时.
  # 这样会确保第一次通知的时候, 有更多的报警被压缩在一起.
  group_wait: 10s

  # 当第一个通知发送,等待多久发送压缩的警报
  group_interval: 10s

  # 默认的接收器
  receiver: 'wechat'

  # 如果报警发送成功, 等待多久重新发送一次
  repeat_interval: 1h
receivers:
#SMTP配置
#- name: 'web.hook'
#  email_configs:
#  - to: 'xxx@chtwm.com'
- name: 'wechat'
  wechat_configs:
  - corp_id: 'xxxxxxx'
    to_party: 'xx'
    agent_id: 'x'
    api_secret: 'xxxxxxxxxxxxxxxxxxxx'
    send_resolved: true

参数说明:
corp_id: 企业微信账号唯一 ID, 可以在我的企业中查看。
to_party: 需要发送的组。
agent_id: 第三方企业应用的 ID,可以在自己创建的第三方企业应用详情页面查看。
api_secret: 第三方企业应用的密钥,可以在自己创建的第三方企业应用详情页面查看。

WeChat告警模版配置
[xxx@xxx prometheus]# cat /usr/local/prometheus/alertmanager/template/wechat.tmpl
## 只告警,恢复后不发送消息
{{ define "wechat.default.message" }}
{{ range .Alerts }}
========start==========
告警程序:prometheus_alert
告警级别:{{ .Labels.severity }}
告警类型:{{ .Labels.alertname }}
故障主机: {{ .Labels.instance }}
告警主题: {{ .Annotations.summary }}
告警详情: {{ .Annotations.description }}
触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }}
========end==========
{{ end }}
{{ end }}
## 带恢复告警的模版 注:alertmanager.yml wechat_configs中加上配置send_resolved: true
{{ define "wechat.default.message" }}
{{ range $i, $alert :=.Alerts }}
===alertmanager监控报警===
告警状态:{{   .Status }}
告警级别:{{ $alert.Labels.severity }}
告警类型:{{ $alert.Labels.alertname }}
告警应用:{{ $alert.Annotations.summary }}
故障主机: {{ $alert.Labels.instance }}
告警主题: {{ $alert.Annotations.summary }}
触发阀值:{{ $alert.Annotations.value }}
告警详情: {{ $alert.Annotations.description }}
触发时间: {{ $alert.StartsAt.Format "2006-01-02 15:04:05" }}
===========end============
{{ end }}
{{ end }}

在这里插入图片描述

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值