AlertManager 发送 Telegram 消息

问题描述 投票:0回答:1

我正在部署 Alertmanager,以便使用此部署 yaml 向 Telegram 发送消息:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: alertmanager
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: alertmanager
  template:
    metadata:
      name: alertmanager
      labels:
        app: alertmanager
    spec:
      containers:
      - name: alertmanager
        image: prom/alertmanager:latest
        args:
          - "--config.file=/etc/alertmanager/config.yml"
          - "--storage.path=/alertmanager"
        ports:
        - name: alertmanager
          containerPort: 9093
        volumeMounts:
        - name: config-volume
          mountPath: /etc/alertmanager
        - name: alertmanager
          mountPath: /alertmanager
      volumes:
      - name: config-volume
        configMap:
          name: alertmanager-config
      - name: alertmanager
        emptyDir: {}

这是 config.yaml:

kind: ConfigMap
apiVersion: v1
metadata:
  name: alertmanager-config
  namespace: monitoring
data:
  config.yml: |-
    global:
    route:
      receiver: webhook_receiver
      group_by: [alertname, job]
      group_wait: 10s
      repeat_interval: 5m
      routes:
        - receiver: webhook_receiver
          match:
            severity: critical
        - receiver: telegram_receiver
          match:
            severity: critical

    receivers:
    - name: webhook_receiver
      webhook_configs:
        - url: 'https://webhook.site/90befe7e-2a54-4d9a-8a0c-12258dafbb71'
    - name: telegram_receiver
      telegram_configs:
      - api_url: 'https://api.telegram.org'
        bot_token: 'YYYYYYYYYY:xxxxxxxxxxxxxxxxxxxxxxx'
        chat_id: -YYYYYYYYYYYYYYY

Webhook 可以正确接收警报,但 Telegram 机器人却不能。是不是该功能已启用? 我关注了 de Alert Manager 官方页面。

提前致谢。

prometheus telegram telegram-bot prometheus-alertmanager
1个回答
0
投票

这里是 Telegram 的“触发”和“已解决”警报模板。 提供的 Alertmanager 模板用于格式化警报的 Telegram 通知。它根据警报的状态(触发或已解决)自定义消息,并包含以下详细信息:

  1. 严重性指示器:使用表情符号直观地表示警报的严重性(🔴表示严重,🟠表示警告,⚪表示其他严重性)。
  2. 动态标题:显示清晰且具有描述性的警报名称(例如“警报”)。
  3. 警报状态:
    • 🔥 触发活动警报。
    • ✅ 解决了警报。
  4. 警报详情:
    • 列出与警报相关的所有实例(instance)。
    • 包括标题和描述等自定义注释,以提供有关警报的有意义的摘要和详细信息。

此模板确保警报简洁、视觉上清晰,并在 Telegram 通知中提供可操作的信息。

模板

{{ define "telegram.default.message" }}
{{ if eq .Status "firing" }}
{{ if eq .CommonLabels.severity "critical" }}🔴 *Alert:* {{ .CommonLabels.alertname }}
{{ else if eq .CommonLabels.severity "warning" }}🟠 *Alert:* {{ .CommonLabels.alertname }}
{{ else }}⚪️ *Alert:* {{ .CommonLabels.alertname }}
{{ end }}
*Status:* 🔥 FIRING
*Severity:* {{ if eq .CommonLabels.severity "critical" }}🔴 **{{ .CommonLabels.severity | title }}**{{ else if eq .CommonLabels.severity "warning" }}🟠 **{{ .CommonLabels.severity | title }}**{{ else }}⚪️ **{{ .CommonLabels.severity | title }}**{{ end }}
{{ else if eq .Status "resolved" }}
{{ if eq .CommonLabels.severity "critical" }}🟢 *🚌 TRANSPORT Alert:* {{ .CommonLabels.alertname }}
{{ else if eq .CommonLabels.severity "warning" }}🟢 *🚌 TRANSPORT 
Alert:* {{ .CommonLabels.alertname }}
{{ else }}⚪️ *Alert:* {{ .CommonLabels.alertname }}
{{ end }}
*Status:* ✅ RESOLVED
*Severity:* {{ if eq .CommonLabels.severity "critical" }}🟢 **{{ .CommonLabels.severity | title }}**{{ else if eq .CommonLabels.severity "warning" }}🟢 **{{ .CommonLabels.severity | title }}**{{ else }}⚪️ **{{ .CommonLabels.severity | title }}**{{ end }}
{{ end }}
{{ range .Alerts }}
*Instance:* {{ .Labels.instance }}
  - *Title:* {{ .Annotations.title }}
  - *Description:* {{ .Annotations.description }}
{{ end }}
{{ end }}

通知示例

射击:

🟠 Alert: HighMemoryUsage>90%

Status: 🔥 FIRING
Severity: 🟠 Warning

Instance: server-1:9100
  - Title: WARNING High Memory Usage on server-1:9100
  - Description: Memory usage on the instance is 53.67%

已解决:

🟢 Alert: HighMemoryUsage>90%

Status: ✅ RESOLVED
Severity: 🟢 Warning

Instance: server-1:9100
  - Title: WARNING High Memory Usage on server-1:9100
  - Description: Memory usage on the instance is 64.59%
© www.soinside.com 2019 - 2024. All rights reserved.