prometheus + alertmanager + 企業微信 報警

禁止任何形式的轉載,謝謝!

需求

其實本來是想要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 configuration
alerting:
  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_alert
  rules:
  - 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: '[email protected]'           # 發送郵件的郵箱地址
  smtp_auth_username: '[email protected]'  # 發送郵件的郵箱用戶名
  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: '[email protected]'
     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 不然無法啓動
告警狀態:{{   .Status }}
告警級別:{{ $alert.Labels.severity }}
告警類型:{{ $alert.Labels.alertname }}
告警應用:{{ $alert.Annotations.summary }}
告警主機:{{ $alert.Labels.instance }}
告警物理機:{{ $alert.Labels.node }}
告警詳情:{{ $alert.Annotations.description }}
觸發閥值:{{ $alert.Annotations.value }}
告警時間:{{ $alert.StartsAt.Format "2020-01-01 15:05:05" }}
恢復時間: {{ $alert.EndsAt.Format "2020-01-01 15:05:15" }}

3. 啓動

3.1 Prometheus

啓動後 瀏覽器 localhost:9090

3.2 Alertmanager

啓動後 瀏覽器 localhost:9093

3.3 企業微信

前面兩個都啓動之後,如果服務符合報警需求(2.1 testrules.xml 中的rules的expr),企業微信會收到告警

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