我正在部署 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 官方页面。
提前致谢。
这里是 Telegram 的“触发”和“已解决”警报模板。 提供的 Alertmanager 模板用于格式化警报的 Telegram 通知。它根据警报的状态(触发或已解决)自定义消息,并包含以下详细信息:
此模板确保警报简洁、视觉上清晰,并在 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%