alertmanager 通過企業微信告警&自定義告警模板


准備工作

  注冊企業微信很簡單也沒有審核, 百度一下即可。

創建部門

  創建部門需要記錄部門ID

1. 選擇通訊錄,添加部門。

 

 

 

 2. 查看部門ID

創建應用

1. 選擇應用管理, 自建 -> 創建應用。

 

 

 

 

 

 3. 查看 AgentID & Secret

 

 

 

 4. 企業微信查看 Secret

 

 

查看企業ID

  選擇我的企業拉倒底部可以查看

 

 

 

alertmanager 配置

配置文件

templates:
  - './template/alertmanager-wechat.tmpl'
route:                                      # 路由組
  group_by: ['alertname', 'app']                   # 
  group_wait: 30s
  group_interval: 40s
  repeat_interval: 1m
  receiver: wechat                 # 默認組


  
receivers:                            
- name: 'wechat'
  wechat_configs:
  - send_resolved: true
    message: '{{ template "wechat.default.message" . }}'
    to_party: '2'                                                  # 企業微信中,創建的接收告警的部門【告警機器人】的部門ID
    agent_id: '10qqq04'                                            # 企業微信中,創建的應用的 Agent ID
    api_secret: 'x-HmLE-Q3ubZcWRwwwwwwwwwwwvYvKJMACDI35IvFc0'      # 企業微信中,應用的Secret
    corp_id: 'ww31e8e1d84f2dcdae'                                  # 企業微信中,企業ID

模板文件

[root@ops alertmanager]# cat template/alertmanager-wechat.tmpl 
{{ define "wechat.default.message" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts -}}

=========  監控告警 =========
告警程序:     Alertmanager
告警類型:    {{ $alert.Labels.alertname }}
告警級別:    {{ $alert.Labels.severity }} 級
告警狀態:    {{ .Status }}
故障主機:    {{ $alert.Labels.instance }} {{ $alert.Labels.device }}
告警主題:    {{ .Annotations.summary }}
告警詳情:    {{ $alert.Annotations.message }}{{ $alert.Annotations.description}}
主機標簽:    {{ range .Labels.SortedPairs  }}  [{{ .Name }}: {{ .Value  | html }} ] {{- end }} 
故障時間:    {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
========= = end =  =========
{{- end }}
{{- end }}

{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts -}}

========= 告警恢復 =========
告警程序:     Alertmanager
告警主題:    {{ $alert.Annotations.summary }}
告警主機:    {{ .Labels.instance }}
告警類型:    {{ .Labels.alertname }}
告警級別:    {{ $alert.Labels.severity }} 級
告警狀態:    {{   .Status }}
告警詳情:    {{ $alert.Annotations.message }}{{ $alert.Annotations.description}}
故障時間:    {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
恢復時間:    {{ ($alert.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
========= = end =  =========
{{- end }}
{{- end }}
{{- end }}

效果演示

告警觸發

 

 

告警恢復

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM