prometheus + alertmanager + 企业微信 告警
2022-10-24 15:45:48
225
{{single.collect_count}}

禁止任何形式的转载,谢谢!

需求

其实本来是想要grafana实现企业微信报警的。
但是我下载的grafana7.0.0 里面没有wechat的type,网上有自己编译的,奈何我不会go语言。
alertmanager可以直接实现wechat报警

1.安装

1.1 Prometheus

1.2 Alertmanager

1.3 企业微信

2. 环境配置

官网教程
注意是yml结构的,严格控制缩进。

2.1 Prometheus

  • prometheus.yml

去除# - localhost:9093 前的#
去除# - "first_rules.yml" 前的# 也可以改个名字

# Alertmanager configurationalerting:alertmanagers:- static_configs:- targets: - localhost:9093# alertmanager的地址:端口号,默认端口号9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files:# - "first_rules.yml"# - "second_rules.yml"- "testrules.yml"# 规则yml的地址
  • 创建testrules.yml
    官网教程
    注意要utf-8 不然无法启动
groups:- name: test_alertrules:- alert: alert_name # 告警名称expr: up == 0 # 告警的触发条件,参考Prometheus高级查询来设定for: 5s # 满足告警条件持续时间多久后,才会发送告警annotations:# 解析项,详细解释告警信息summary: "一条test报警"# 告警的内容

2.2 Alertmanager

官网教程

  • alertmanager.yml
global:resolve_timeout: 5m # 在没有报警的情况下声明为已解决的时间# 配置邮件发送信息smtp_smarthost: 'smtp.xxx.cn:465' # 发送邮件的smtp地址端口号smtp_from: 'abc@xxx.cn' # 发送邮件的邮箱地址smtp_auth_username: 'abc@xxx.cn'# 发送邮件的邮箱用户名smtp_auth_password: 'password'# 发送邮件的邮箱密码smtp_require_tls: false #报警模板 templates: - 'wechat.tmpl' # 模板地址 #报警路由,树结构 route:group_by: ['alertname'] # 分组依据group_wait: 10s # 分组创建后的初始化等待发送时长group_interval: 10s # 发送之前的等待时长repeat_interval: 1m # 重复报警的间隔时长receiver: 'wechat'# 优先接收组的名称routes: # route的子节点 - receiver: mail match_re:serverity: mail #匹配到serverity时mail的使用邮件报警,这里的serverity是rules文件中的labels指定的#报警接收,树结构 receivers: - name: 'mail'# 子节点 - 邮件 email_configs: - to: 'abc@xxx.cn' send_resolved: true - name: 'wechat'# 子节点 - 微信 wechat_configs: - corp_id: 'aaa' # corpid to_party: '1'# 组ID to_user: '1'# 用户ID agent_id: '1000001'# agentid api_secret: 'abcd' # corp secret send_resolved: true# 告警解除后否发送通知
  • 创建wechat.tmpl
    注意要utf-8 不然无法启动
{{ define "wechat.default.message" }}{{ range $i, $alert :=.Alerts }}告警状态:{{ .Status }}告警级别:{{ $alert.Labels.severity }}告警类型:{{ $alert.Labels.alertname }}告警应用:{{ $alert.Annotations.summary }}告警主机:{{ $alert.Labels.instance }}告警物理机:{{ $alert.Labels.node }}告警详情:{{ $alert.Annotations.description }}触发阀值:{{index $alert.Annotations "value"}}#实际使用过程中 不加“”无法显示告警时间:{{ (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }} #UTC时间调整为北京时间+8:00恢复时间:{{ $alert.EndsAt.Format "2020-01-01 15:05:15" }}{{ end }}{{ end }}

3. 启动

3.1 Prometheus

启动后 浏览器 localhost:9090

3.2 Alertmanager

启动后 浏览器 localhost:9093

3.3 企业微信

前面两个都启动之后,如果服务符合报警需求(2.1 testrules.xml 中的rules的expr),企业微信会收到告警

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}