alertmanager-0.18.0 調用企業微信api推送告警

1 申請企業微信賬號並創建告警應用
https://work.weixin.qq.com

2 獲取到以下信息
corp_id
api_secret
agent_id
to_party
在這裏插入圖片描述
在這裏插入圖片描述
↓↓↓↓↓↓↓↓↓↓↓↓↓↓
在這裏插入圖片描述
3 發消息試驗
https://work.weixin.qq.com/api/devtools/devtool.php

在這裏插入圖片描述
在這裏插入圖片描述
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

在這裏插入圖片描述
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`

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章