alertmanager-0.18.0 调用企业微信api推送告警

1 申请企业微信账号并创建告警应用
https://work.weixin.qq.com

2 获取到以下信息
corp_id
api_secret
agent_id
to_party
alertmanager-0.18.0 调用企业微信api推送告警_第1张图片
在这里插入图片描述
↓↓↓↓↓↓↓↓↓↓↓↓↓↓
在这里插入图片描述
3 发消息试验
https://work.weixin.qq.com/api/devtools/devtool.php

alertmanager-0.18.0 调用企业微信api推送告警_第2张图片
alertmanager-0.18.0 调用企业微信api推送告警_第3张图片
json参考自 https://work.weixin.qq.com/api/doc#90000/90135/90236

{
   "toparty" : "1",
   "msgtype" : "text",
   "agentid" : 1000002,
   "text" : {
       "content" : "企业微信接口调试工具===测试"
   },
   "safe":0
}

4 参考官网修改alertmanager.yml配置
https://prometheus.io/docs/alerting/configuration/#wechat_config

# cat alertmanager.yml
global:
  resolve_timeout: 10s
  wechat_api_url: 'https://qyapi.weixin.qq.com/cgi-bin/'
  wechat_api_secret: 'xxxxxxxxxxxxxxxxxxxx'
  wechat_api_corp_id: 'xxxxxxxxxxxxxxxxxxxx'

templates:
  - 'wechat.message'

route:
  group_by: ['alertname']
  group_wait: 10s
  group_interval: 10s
  repeat_interval: 1m

  receiver: 'qiyeweixin'

receivers:
  - name: 'qiyeweixin'
    wechat_configs:
    - send_resolved: true
      agent_id: '1000002'
      message: '{{ template "wechat.message" . }}'     #注意不要丢了那个' . '
      to_party: '2'


inhibit_rules: 
  - source_match: 
      severity: CRITICAL 
    target_match: 
      severity: WARNING 
    equal: ['alertname', 'env', 'instance']

prometheus.yml

alertmanager-0.18.0 调用企业微信api推送告警_第4张图片
rules_node.yml

groups:
- name: node-rule
  rules:
  - alert: 'node_status'
    expr: up{job="node_test"} == 0
    for: 20s
    annotations:
      summary: "node {{ $labels.instance }} 挂了"
      description: "请立即查看!"

告警模板参考官网:
https://prometheus.io/docs/alerting/notifications/#alert

# vim wechat.message
接收器:{{ .Receiver }}
状态:{{ .Status }}
{{ range .Alerts }}
级别:{{ .Labels.severity }}
类型:{{ .Labels.alertname }}
主机:{{ .Labels.instance }}
标题:{{ .Annotations.summary }}
详情:{{ .Annotations.description }}
触发:{{ .StartsAt.Format "2006-01-02 15:04:05" }} CST
地址:{{ .GeneratorURL }}
{{ end }}
Alertmanager地址: {{ .ExternalURL }}

注:range+end 标识 Alerts的范围
注:微信对message有2048字节限制,超出不显示,以上模板除 range和end外,其他行可以随意删减以节省字符

5 报警测试

kill `pidof node_exporter`

alertmanager-0.18.0 调用企业微信api推送告警_第5张图片

你可能感兴趣的:(监控技术)