AlertManager 微信报警
准备
本次以加简单的redis报警为例
1 2 3 4 5 6 7
| # prometheus 10.0.42.61:9090
# alertmanager 10.0.42.61:9093 # 默认情况alertmanager同时会占用:9094端口
# kill 掉10.0.42.66:7001的redis实例
|
关联prometheus和alertmanager
- 创建redis.rules.yml文件(我放在了和prometheus.yml在同一路径下,当然也可以放到自定义的路径下,届时记得配置文件里写绝对路径),写入报警规则:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| groups: - name: alert.rules rules: - alert: Redis_is_down expr: redis_up == 0 for: 1m labels: region: {{ $labels.region }} env: 'test' level: emergency expr: redis_up == 0 annotations: description: 'cluster: {{ $labels.cluster }}, instance: {{ $labels.instance }}' value: '{{ $value }}' summary: Redis instance is down
|
1 2 3 4 5 6
| alerting: alertmanagers: - static_configs: - targets: - 10.0.42.61:9093
|
rules
配置文件路径加入到prometheus.yml
配置文件中
1 2
| rule_files: - 'redis.rules.yml'
|
- 和prometheus.yml在同一路径下创建报警消息模板 wechat.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| {{ define "wechat.default.message" }} {{ range $i, $alert :=.Alerts }} ===alertmanager监控报警=== 告警状态:{{ .Status }} 告警级别:{{ $alert.Labels.level }} 告警类型:{{ $alert.Labels.alertname }} 告警节点:{{ $alert.Labels.region }} 告警集群:{{ $alert.Labels.env }} 故障主机:{{ $alert.Labels.instance }} 告警主题:{{ $alert.Annotations.summary }} 触发阀值:{{ $alert.Annotations.value }} 告警详情:{{ $alert.Annotations.description }} 触发时间:{{ $alert.StartsAt.Format "2006-01-02 15:04:05" }} ===========end============ {{ end }} {{ end }}
|
- 重启prometheus,可以在10.0.42.61:9093看到下图结果

报警
企业微信报警
如果是企业微信报警,alertmanager机器需要可以访问公网
微信号申请,建立报警应用
- 微信企业号申请地址(https://work.weixin.qq.com/)
- 进入企业号注册页面,一些信息可以随便填写,但是管理员信息必须真实有效
- 创建应用(“应用管理”–>”应用”–>”自建”–>”创建应用”)
- 填写应用名称,图片,介绍等
- 成功后可以看到应用的AgentId,Secret等
- 很重要的一步:配置该报警选项的
可见范围

Alertmanager配置
alertmanager
配置文件,加入微信配置信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| global: resolve_timeout: 2m # 2min后查看是否恢复
route: group_by: ['alertname'] group_wait: 10s # 同组下的报警Pending的时间 group_interval: 10s # 同组下每条报警的间隔时间 repeat_interval: 1h # 重复触发报警的间隔时间 receiver: 'wechat' # 不触发routes的match,默认的receiver ## 微信发送模板 templates: - 'wechat.tmpl' receivers: - name: 'wechat' wechat_configs: - send_resolved: true to_party: "1" agent_id: "1000003" api_url: "https://qyapi.weixin.qq.com/cgi-bin/" api_secret: "exxxxxxxxxxxxxxxxxxxxxINI" corp_id: "wwxxxxxxxxxxxxxx9"
|
api_url
: wechat
对外接口https://qyapi.weixin.qq.com/cgi-bin/
api_secret
: 企业微信(“应用管理”–>”应用”–>”自建”–>”自定应用”–> “Secret”)
api_corp_id
: 企业信息(“我的企业”—>”CorpID”[在底部])
to_party
: 1
值是组的ID
,注意该ID的组织要配置在可见范围
里。你可通过链接去定制报警信息接收人或者组(“通讯录”–>“公司右边的三个点”–>“部门ID”)
agent_id
: 企业微信(“应用管理”–>”应用”–>”自建”–>”自定应用”–>”AgentId”)
NOTE: 更多细节请参考https://prometheus.io/docs/alerting/latest/configuration/#wechat_config
重启alertmanager
然后我就tm开始收报警了。。
