Prometheus之Alertmanager配置詳解


配置介紹

alertmanager.yml


global:
  [ smtp_from: <tmpl_string> ] #發件人郵箱地址
  [ smtp_smarthost: <string> ] #郵箱SMTP地址
  [ smtp_auth_username: <string> ] #發件人登錄用戶名
  [ smtp_auth_password: <secret> ] #發件人登錄密碼或者授權碼
  [ smtp_require_tls: <bool> | default = true ] #是否需要TLS協議,默認true
  [ wechat_api_url: <string> | default = "https://qyapi.weixin.qq.com/cgi-bin/" ] #企業微信API地址
  [ wechat_api_secret: <secret> ] #企業微信API secret
  [ wechat_api_corp_id: <string> ] #企業微信corp id
  [ resolve_timeout: <duration> | default = 5m ] #在指定時間內沒有新的事件就發送恢復通知
 
route:  #route用來設置告警分發策略
  receiver: 'default-receiver' #設置接收人
  [ group_by: '[' <labelname>, ... ']' ] #采用那個標簽來作為分組。
  [ group_wait: <duration> | default = 30s ] #組告警等待時間。在等待時間結束后,如果有同組告警一起發出。
  [ group_interval: <duration> | default = 5m ] #兩組告警間隔時間。
  [ repeat_interval: <duration> | default = 4h ] #重復告警間隔時間,減少相同郵件的發送頻率。
  
receivers: #通知接收者列表


inhibit_rules: #抑制規則

target_match:        #target_ 新告警信息匹配規則
  [ <labelname>: <labelvalue>, ... ]
target_match_re:
  [ <labelname>: <regex>, ... ]
target_matchers:
  [ - <matcher> ... ]

source_match:         # source_ 已存在的告警信息
  [ <labelname>: <labelvalue>, ... ]
source_match_re:
  [ <labelname>: <regex>, ... ]
source_matchers:
  [ - <matcher> ... ]


[ equal: '[' <labelname>, ... ']' ]   #新告警與已有告警二者之間需要滿足的條件

#在有新的告警通知匹配到target_match和target_match_re規則時,若存在已經發出的告警通知滿足source_matcn或者source_match_re的匹配條件,並且與發送的告警與新的告警中由equal定義的標簽完全相同,則啟動抑制機制,新的告警不會發送;

#severity(告警驗證等級):CRITICAL(危急), WARNING(警告), INFO(信息)

email_config

# Whether to notify about resolved alerts.
[ send_resolved: <boolean> | default = false ]

# The email address to send notifications to.
to: <tmpl_string>

# The sender's address.
[ from: <tmpl_string> | default = global.smtp_from ]

# The SMTP host through which emails are sent.
[ smarthost: <string> | default = global.smtp_smarthost ]

# The hostname to identify to the SMTP server.
[ hello: <string> | default = global.smtp_hello ]

# SMTP authentication information.
[ auth_username: <string> | default = global.smtp_auth_username ]
[ auth_password: <secret> | default = global.smtp_auth_password ]
[ auth_secret: <secret> | default = global.smtp_auth_secret ]
[ auth_identity: <string> | default = global.smtp_auth_identity ]

# The SMTP TLS requirement.
# Note that Go does not support unencrypted connections to remote SMTP endpoints.
[ require_tls: <bool> | default = global.smtp_require_tls ]

# TLS configuration.
tls_config:
  [ <tls_config> ]

# The HTML body of the email notification.
[ html: <tmpl_string> | default = '{{ template "email.default.html" . }}' ]
# The text body of the email notification.
[ text: <tmpl_string> ]

# Further headers email header key/value pairs. Overrides any headers
# previously set by the notification implementation.
[ headers: { <string>: <tmpl_string>, ... } ]

消息分類發送

alertmanager.yml

global:
  smtp_from: '1304995320@qq.com' 
  smtp_smarthost: 'smtp.qq.com:465'
  smtp_auth_username: '1304995320@qq.com'
  smtp_auth_password: 'xxxxxxxxx'
  smtp_hello: '@qq.com'
  smtp_require_tls: false 
route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5s
  repeat_interval: 1m
  receiver: 'web.hook'  #默認的告警通知
  routes:
  - receiver: 'dingding' #critical級別的消息發給釘釘
    group_wait:10s
    match_re:
      severity:critical
  - receiver: 'wechat' #info級別的消息發給企業微信
    group_wait:10s
    match_re:
      severity:info
receivers:
- name: 'web.hook'
  email_configs:
    - to: '1304995320@qq.com'
      send_resolved: true
- name: 'dingding'
  webhook_configs:
    - url: 'http://192.168.174.105:8060/dingtalk/webhook1/send'
      send_resolved: true
- name: 'wechat'
  wechat_configs:
    - corp_id: 'xxxxxxxxxxxxxx'
      to_party:2
      agent_id: 1000006
      api_secret: xxxxxxxxxxxxxxxx
      send_resolved: true
inhibit_rules:
  - source_match:
      alertname: InstanceDown
      severity: 'critical'
    target_match:
      alertname: InstanceDown
      severity: 'critical'
    equal: ['alertname', 'dev', 'instance']

自定義消息模板

定義微信模板

/usr/local/alertmanager/template/message_template.templ 

{{ define "wechat.default.message" }}
{{ range $i,$alert :=.Alerts }}
===alertmanager 監控報警===
告警狀態: {{ .Status }}
告警級別: {{ $alert.Labels.severity }}
告警類型: {{ $alert.Labels.alertname }}
告警應用: {{ $alert.Annotations.summary }}
告警主機: {{ $alert.Lables.instance }}
告警主題: {{ $alert.Annotations.summary }}
觸發閾值: {{ $alert.Annotations.value }}
告警詳情: {{ $alert.Annotations.description }}
觸發時間: {{ $alert.StartsAt.Format "2006-01-02 15:04:05" }}

==========end===========
{{ end }}
{{ end }}

定義email模板

/usr/local/alertmanager/template/email_template.tmpl 

{{ define "email.default.html" }}
{{ range $i, $alert := .Alerts }}

===alertmanager 監控報警=== <br>

告警狀態: {{ .Status }} <br>
告警級別: {{ $alert.Labels.severity }} <br>
告警類型: {{ $alert.Labels.alertname }} <br>
告警應用: {{ $alert.Annotations.summary }} <br>
告警主機: {{ $alert.Labels.instance }} <br>
告警主題: {{ $alert.Annotations.summary }} <br>
觸發閾值: {{ $alert.Annotations.value }} <br>
告警詳情: {{ $alert.Annotations.description }} <br>
故障時間: {{ $alert.StartsAt.Local.Format "2006-01-02 15:04:05" }} <br>


===========end=========== <br>

{{ end }} <br>
{{ end }} <br>

alertermanager引用模板

alertmanager.yml 

global:
  smtp_from: '1304995320@qq.com' 
  smtp_smarthost: 'smtp.qq.com:465'
  smtp_auth_username: '1304995320@qq.com'
  smtp_auth_password: 'xxxxxxxxx'
  smtp_hello: '@qq.com'
  smtp_require_tls: false 
  
templates:   #引用模板
  - 'template/*.tmpl'


route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5s
  repeat_interval: 1m
  receiver: 'web.hook'  #默認的告警通知
  routes:
  - receiver: 'dingding' #critical級別的消息發給釘釘
    group_wait:10s
    match_re:
      severity:critical
  - receiver: 'wechat' #info級別的消息發給企業微信
    group_wait:10s
    match_re:
      severity:info
receivers:
- name: 'web.hook'
  email_configs:
    - to: '1304995320@qq.com'
      html: '{{ template "email.default.html" . }}'
      headers: { Subject: "[WARN] 報警郵件" }
      send_resolved: true
- name: 'dingding'
  webhook_configs:
    - url: 'http://192.168.174.105:8060/dingtalk/webhook1/send'
      send_resolved: true
- name: 'wechat'
  wechat_configs:
    - corp_id: 'xxxxxxxxxxxxxx'
      to_party:2
      agent_id: 1000006
      api_secret: xxxxxxxxxxxxxxxx
      send_resolved: true
inhibit_rules:
  - source_match:
      alertname: InstanceDown
      severity: 'critical'
    target_match:
      alertname: InstanceDown
      severity: 'critical'
    equal: ['alertname', 'dev', 'instance']

驗證模板信息

告警靜默

參考文檔

https://github.com/prometheus/alertmanager/tree/main/template


免責聲明!

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



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