实现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 }}