Prometheus監控神器-Alertmanager篇(2)


本章主要對如何使用開源組件和Alertmanager組件集成警報通知。Kubernetes的警報集成后續會直接在配置文件講解,原理大同小異,此處僅對相關警報通知做集成。

警報通知接收器

前面一直是在Web UI 查看警報信息,現在開始使用接收器與Alertmanager集成,發送警報信息到 Email企業微信釘釘機器人,對於警報要求比較高的同學,可以根據下面提到的開源組件 【PrometheusAlert全家桶】 配置飛書、短信、語音電話等警報。

Email

前面已經講過,Alertmanager默認支持配置Email,也是最普通的方式,在Alertmanager組件中內置了SMTP協議。直接可以把前面的Alertmanager.yml中的SMTP部分截取出來,然后進行調整與配置

global:
  resolve_timeout: 5m
  # smtp配置
  smtp_from: "1234567890@qq.com" # 發送郵件主題
  smtp_smarthost: 'smtp.qq.com:465' # 郵箱服務器的SMTP主機配置
  smtp_auth_username: "1234567890@qq.com" # 登錄用戶名
  smtp_auth_password: "auth_pass" # 此處的auth password是郵箱的第三方登錄授權密碼,而非用戶密碼,盡量用QQ來測試。
  smtp_require_tls: false # 有些郵箱需要開啟此配置,這里使用的是163郵箱,僅做測試,不需要開啟此功能。
route:
  receiver: ops
  group_wait: 30s # 在組內等待所配置的時間,如果同組內,30秒內出現相同報警,在一個組內出現。
  group_interval: 5m # 如果組內內容不變化,合並為一條警報信息,5m后發送。
  repeat_interval: 24h # 發送報警間隔,如果指定時間內沒有修復,則重新發送報警。
  group_by: [alertname]  # 報警分組
  routes:
      - match:
          team: operations
        group_by: [env,dc]
        receiver: 'ops'
      - receiver: ops # 路由和標簽,根據match來指定發送目標,如果 rule的lable 包含 alertname, 使用 ops 來發送
        group_wait: 10s
        match:
          team: operations
# 接收器指定發送人以及發送渠道
receivers:
# ops分組的定義
- name: ops
  email_configs:
  - to: '9935226@qq.com,xxxxx@qq.com' # 如果想發送多個人就以 ','做分割,寫多個郵件人即可。
    send_resolved: true
    headers:
      from: "警報中心"
      subject: "[operations] 報警郵件"
      to: "小煜狼皇"

配置完成后,直接重啟Alertmanager組件,使配置生效,然后使用前面內存閾值觸發一次警報來看下發送結果。

收到的警報信息:

郵件警報信息

當警報接觸以后收到的恢復信息。

郵件恢復信息

企業微信

首先你要具有企業微信管理員的權限,如果沒有可以自己注冊一個,進行測試,我這里有自行注冊的企業微信

第一步登錄進入以后,在應用管理中新建應用。

企業微信應用

第二步,創建應用,信息填寫如下,上傳應用logo隨意。

企業微信應用信息

創建成功以后如下圖。

微信應用信息

這時候需要把 AgentIdSecret 記錄下來,對於你的這種Secret信息,最好管理好,我的用過就會刪除,所以不用擔心安全隱患。

ID key
AgentId 1000004
Secret F-fzpgsabmfiFt7_4QRQwWEl8eyx7evO12sRYe_Q5vA

第三步,現在我們來用新建的企業微信應用在Alertmanager配置,可以配置全局,也可以對單獨需要發送的接收器,因為警報需要分級,所以需要單獨處理,在這里使用的的單獨的配置,需要知道 企業ID ,以及 部門ID

企業ID 通過

企業ID

部門ID 通過通訊錄獲取

部門ID

# 企業微信配置
  wechat_configs:
  - corp_id: 'wwxxxxx' # 企業ID是唯一標識
    api_url: 'https://qyapi.weixin.qq.com/cgi-bin/' # 企業微信api接口,統一定義
    send_resolved: true # 設置發送警報恢復信息
    to_party: '2' # 部門id,比如我的叫警報組,因此顯示的是2,如果你DB組,就可能會是3,WEB組就是4,依次類推,另外需要接收警報的相關人員必須在這個部門里。
    agent_id: '1000004' # 新建應用的agent_id
    api_secret: 'F-fzpgsabmfiFt7_4QRQwWEl8eyx7evO12sRYe_Q5vA' # 新建應用的Secret

這時候我們重啟Alertmanager,然后使用之前的方式來觸發模擬警報,看看發送是不是已經沒有問題了,這時我們的企業微信中、Email都可以收到警報了,這里的警報已經被我用模塊處理過了。可讀性會更高。

cat wechat.tmpl
## wechat模板
{{ define "wechat.default.message" }}
{{ if gt (len .Alerts.Firing) 0 -}}
Alerts Firing:
{{ range .Alerts }}
警報級別:{{ .Labels.status }}

警報類型:{{ .Labels.alertname }}

故障主機: {{ .Labels.instance }}

警報主題: {{ .Annotations.summary }}

警報詳情: {{ .Annotations.description }}

⏱ : {{ (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
{{- end }}
{{- end }}

{{ if gt (len .Alerts.Resolved) 0 -}}
Alerts Resolved:
{{ range .Alerts }}
警報級別:{{ .Labels.status }}

警報類型:{{ .Labels.alertname }}

故障主機: {{ .Labels.instance }}

警報主題: {{ .Annotations.summary }}

警報詳情: {{ .Annotations.description }}

⏱ : {{ (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
⏲ : {{ (.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
{{- end }}
{{- end }}
{{- end }}

Firing警報:

企業微信警報信息

下面是Resolve的警報:

企業恢復信息

釘釘機器人(Webhook)

對於釘釘大家都已經很熟悉了,大部分企業都已經啟用釘釘辦公了,同時其推出的免費的webhook機器人也很受大家的歡迎。我們這里講一下借助第三方開源組件如何對釘釘集成警報功能。

首先需要在釘釘創建機器人,然后在白名單中添加關鍵字信息與ip限制等安全設置,這個只要你有群,你就可以在群里面建,非常簡單,這里就不做演示了

先把Prometheus-webhook-Dingtalk組件裝好。

mkdir -p /etc/prometheus-webhook-dingtalk/template/
cd /etc/prometheus-webhook-dingtalk/
wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v1.4.0/prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz
tar xf prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz
mv prometheus-webhook-dingtalk-1.4.0.linux-amd64/* /etc/prometheus-webhook-dingtalk/
mv prometheus-webhook-dingtalk /bin/
cat <<EOF> /lib/systemd/system/prometheus-webhook-dingtalk.service 
[Unit]
Description=prometheus-webhook-dingding
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/bin/prometheus-webhook-dingtalk --web.listen-address=":8070" --web.enable-ui --config.file="/etc/prometheus-webhook-dingtalk/config.yml"
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
## 啟動服務
systemctl enable prometheus-webhook-dingtalk.service
systemctl start prometheus-webhook-dingtalk.service

配置文件

## Request timeout
# timeout: 5s

## Customizable templates path
## Customizable templates path
templates:
        # - contrib/templates/legacy/template.tmpl
  # 自定義模板路徑
  - /etc/prometheus-webhook-dingtalk/template/default.tmpl

## 你還可以使用' default_message '覆蓋默認模板
## 下面的示例使用v0.3.0中的“legacy”模板
# default_message:
#   title: '{{ template "legacy.title" . }}'
#   text: '{{ template "legacy.content" . }}'

## Targets, previously was known as "profiles"
# 定義的webhook,釘釘創建的webhook token
targets:
# 如果有多個分組就可以在這里定義多個接口
  ops:
    url: https://oapi.dingtalk.com/robot/send?access_token=a4feed2322222222222222222222222
  web:
    url: https://oapi.dingtalk.com/robot/send?access_token=a4feed2325c1333333333333333333333

定義模板:

cd /etc/prometheus-webhook-dingtalk/template
cat default.tmpl
{{ define "__subject" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }} {{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}{{ end }}
{{ define "__alertmanagerURL" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver }}{{ end }}

{{ define "__text_alert_list" }}{{ range . }}
**Labels**
{{ range .Labels.SortedPairs }}> - {{ .Name }}: {{ .Value | markdown | html }}
{{ end }}
**Annotations**
{{ range .Annotations.SortedPairs }}> - {{ .Name }}: {{ .Value | markdown | html }}
{{ end }}
**Source:** [{{ .GeneratorURL }}]({{ .GeneratorURL }})
{{ end }}{{ end }}

{{/* Firing */}}

{{ define "default.__text_alert_list" }}{{ range . }}

**Trigger Time:** {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}

**Summary:** {{ .Annotations.summary }}

**Description:** {{ .Annotations.description }}

**Graph:** [📈 ]({{ .GeneratorURL }})

**Details:**
{{ range .Labels.SortedPairs }}{{ if and (ne (.Name) "severity") (ne (.Name) "summary") }}> - {{ .Name }}: {{ .Value | markdown | html }}
{{ end }}{{ end }}
{{ end }}{{ end }}

{{/* Resolved */}}

{{ define "default.__text_resolved_list" }}{{ range . }}

**Trigger Time:** {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}

**Resolved Time:** {{ dateInZone "2006.01.02 15:04:05" (.EndsAt) "Asia/Shanghai" }}

**Summary:** {{ .Annotations.summary }}

**Graph:** [📈 ]({{ .GeneratorURL }})

**Details:**
{{ range .Labels.SortedPairs }}{{ if and (ne (.Name) "severity") (ne (.Name) "summary") }}> - {{ .Name }}: {{ .Value | markdown | html }}
{{ end }}{{ end }}
{{ end }}{{ end }}

{{/* Default */}}
{{ define "default.title" }}{{ template "__subject" . }}{{ end }}
{{ define "default.content" }}#### \[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}\] **[{{ index .GroupLabels "alertname" }}]({{ template "__alertmanagerURL" . }})**
{{ if gt (len .Alerts.Firing) 0 -}}

![Firing-img](https://is3-ssl.mzstatic.com/image/thumb/Purple20/v4/e0/23/cf/e023cf56-0623-0cdf-afce-97ae90eabfda/mzl.uplmrpgi.png/320x0w.jpg)

**Alerts Firing**
{{ template "default.__text_alert_list" .Alerts.Firing }}
{{- end }}
{{ if gt (len .Alerts.Resolved) 0 -}}

![Resolved-img](https://is3-ssl.mzstatic.com/image/thumb/Purple18/v4/41/72/99/4172990a-f666-badf-9726-6204a320c16e/mzl.dypdixoy.png/320x0w.png)

**Alerts Resolved**
{{ template "default.__text_resolved_list" .Alerts.Resolved }}
{{- end }}
{{- end }}

{{/* Legacy */}}
{{ define "legacy.title" }}{{ template "__subject" . }}{{ end }}
{{ define "legacy.content" }}#### \[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}\] **[{{ index .GroupLabels "alertname" }}]({{ template "__alertmanagerURL" . }})**
{{ template "__text_alert_list" .Alerts.Firing }}
{{- end }}

{{/* Following names for compatibility */}}
{{ define "ding.link.title" }}{{ template "default.title" . }}{{ end }}
{{ define "ding.link.content" }}{{ template "default.content" . }}{{ end }}

在Aertmanager中配置警報

# 接收器指定發送人以及發送渠道
receivers:
# ops分組的定義
- name: ops
  email_configs:
  - to: '9935226@qq.com,10000@qq.com'
    send_resolved: true
    headers: { Subject: "[operations] 報警郵件"} # 接收郵件的標題
  # 釘釘配置
  webhook_configs:
  - url: http://localhost:8070/dingtalk/ops/send # 這里是在釘釘開源組件中的接口,如果單獨定義的receiver需要對應你的分組與釘釘機器人的webhook token
    # 企業微信配置
  wechat_configs:
  - corp_id: 'ww5421dksajhdasjkhj'
    api_url: 'https://qyapi.weixin.qq.com/cgi-bin/'
    send_resolved: true
    to_party: '2'
    agent_id: '1000002'
    api_secret: 'Tm1kkEE3RGqVhv5hO-khdakjsdkjsahjkdksahjkdsahkj'
# web
- name: web
  email_configs:
  - to: '9935226@qq.com'
    send_resolved: true
    headers: { Subject: "[web] 報警郵件"} # 接收郵件的標題
  webhook_configs:
  - url: http://localhost:8070/dingtalk/web/send

繼續使用上面的觸發模擬警報,此時會同時讓三個警報都接受到警報信息,我們這里只是為了調試,往往一個警報通知就可以滿足需求了,對於重要業務還是需要使用電話以及短信提醒。

釘釘Firing警報:

釘釘警報信息

釘釘Resolve警報:

釘釘恢復信息

警報通知模板

Prometheus 創建警報轉發給 Alertmanager,Alertmanager會根據不同的 Label 向不同的 Receiver 發送警報通知,如Email、釘釘、企業微信、飛書、短信等等。所有 Receiver都一個接收模板,然后通過模板格式化以后發送警報信息給 Receiver。
Alertmanager 自帶的模板是基於 Go 語言的 template 模板,用戶可以根據自己的需求去定義自己需要的模板,上面我給出的模板已經足夠大家的基礎使用了。

下面介紹下通常自定義模板中會需要用到的一些參數說明

名稱 數據類型 描述
Receiver string 接受警報通知的接收器名稱
Status string 警報狀態,例如:Firing或Resolved的通知
Alert Alert 警報通知的真實內容,警報中的所有列表
GroupLables KV 包含警報通知的組標簽
CommandLabels KV 所有警報的公共標簽,包含GroupLabels的所有標簽
CommandAnnotations KV 注釋,比如自定義的一些字符串
ExternalURL string 警報信息中的Alertmanager地址

上面說的KV類型是一組使用不標示標簽與注釋的Key/Value字符串對,可以在Alertmanager中的默認模板中看到其定義。 default.tmpl

其中郵件中所顯示的 View In AlertManager ,Receiver 與 ExternalURL的定義其實就是模板中的 .ExternalURL.Receiver

{{ define "__alertmanager" }}AlertManager{{ end }}

{{ define "__alertmanagerURL" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver | urlquery }}{{ end }}
...

在收到的郵箱警報中可以看到 View In AlertManager 的鏈接地址是:http://192.168.1.220:19093/#/alerts?receiver=ops

對於Alert的類型,警報列表的字段還包含了如下參數與定義、描述

名稱 數據類型 描述
Status string 定義警報狀態是已經解決或處於觸發狀態
Label KV 包含警報中的標簽
Annotations KV 警報的一組注釋
StartsAt time.Time 警報觸發時間
EndsAt time.Time 警報結束時間,只在警報結束的時間時設置
GeneratorURL string 警報規則的連接URL,也就是Prometheus中的Rules查詢地址

對於警報中的通知模板首先要熟悉go語言的template語法以及HTML簡單的基礎知識,然后把上面相關的元數據的一些信息了解清楚,就可以自己調整模板了,如果你實在懶的改,我調整好的模板可以直接拿去用,把對應的指標、標簽名字改一下就可以用了。

以下是我自己修改了一下的模板警報格式,大家可以看看,這個是通過官方的 default.tmpl 修改的。

Email模板警報信息

開源警報組件推薦

一個開源的第三方警報插件,針對釘釘機器人 webhook 做集成,Go語言編寫,現在迭代的已經很不錯了,可能有一些功能還是有些限制,比如針對 Markdown @某個人無法實現,原因是因釘釘自身API沒有支持這個功能。

這個開源組件是將Alertmanger Webhook 消息轉換為可以接收消息的企業微信機器人,也是go語言編寫,Alertmanager 默認已經集成企業微信配置,如果有特殊需求,需要使用企業微信機器人的可以看看這個。

如果有對短信、電話警報等其他需求的同學,推薦這個開源警報組件,Go語言編寫,Web框架是 Beego ,支持將收到的這些消息發送到釘釘,微信,飛書,騰訊短信,騰訊電話,阿里雲短信,阿里雲電話,華為短信,容聯雲電話等,這里就不細講了,如果配置有問題可以隨時咨詢我。

PrometheusAlert

wechat


免責聲明!

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



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