通知方式
告警通知支持邮件、企业微信、钉钉、WebHook 等多种渠道。
需先配置通知方式,才能在告警规则中选择对应渠道接收告警消息。
邮件
1. 启用 SMTP 服务
在 ops.yaml 中添加以下环境变量,重启运维平台服务,以启用 SMTP 邮件功能。
ENV_SMTP_ENABLED: "true" # 启用 SMTP 邮件能力
ENV_SMTP_HOST: "smtp.example.com" # 替换为 SMTP 邮箱服务器地址
ENV_SMTP_PORT: "465" # 替换为 SMTP 邮箱服务器端口
ENV_SMTP_USER: "ops@example.com" # 替换为 SMTP 邮箱服务器帐号
ENV_SMTP_PASSWORD: "************" # 替换为 SMTP 邮箱服务器授权码
ENV_SMTP_SKIPVERIFY: "true" # 跳过对邮件服务器的 SSL/TLS 证书验证
ENV_SMTP_FROM_ADDR: "ops@example.com" # 替换为告警发送邮箱
ENV_SMTP_FROM_NAME: "HAP Alert" # 告警邮件标题
2. 在运维平台 --> 告警 --> 通知方式中选择 Email 进行编辑

3. 在 Addresses 下填写告警接收者的邮箱地址,支持通过 ; \n , 分隔符来输入多个电子邮件地址,以便将 通知发送给多个收件人。

4. 保存后再次进入编辑页面,点击 Test --> Send test notification,等待片刻后告警接收者的邮箱地址会收到一封测试邮件,代表邮件通知功能启用正常。

群机器人通知配置(企业微信/钉钉)
在配置企业微信或钉钉机器人前,需先定义告警消息模板,确保消息以合适的格式进行通知。
配置告警消息模板
1. 在 Notification Templates 页面点击 Add notification template 添加告警模板。

2. 自定义告警标题模板。
Template name 字段填写模板名称: custom_title
Template 内容如下:
{{ define "custom_title" -}}
{{ if gt (.Alerts.Firing | len) 0 }}🚨 {{ .Alerts.Firing | len }} Firing alerts. {{ end }}{{ if gt (.Alerts.Resolved | len) 0 }}✅ {{ .Alerts.Resolved | len }} Resolved alerts.{{ end }}
{{ end -}}
点击 Refresh 可以预览效果,如下图所示:

3. 自定义告警内容模板。
与上述添加告警模板方式相同,Template name 字段填写模板名称: custom_template
Template 内容如下:
{{ define "custom.alert_labels_and_annotations" -}}
{{ len .Alerts.Firing }} Firing alert(s)
{{ range .Alerts.Firing -}}
{{ template "alert.labels_and_annotations" . -}}
Alert fired at: {{ .StartsAt | date "2006-01-02 15:04:05 MST" }}
{{ end }}
{{ len .Alerts.Resolved }} Resolved alert(s)
{{ range .Alerts.Resolved -}}
{{ template "alert.labels_and_annotations" . -}}
Alert and Recovery Time:
- Alert fired at: {{ .StartsAt | date "2006-01-02 15:04:05 MST" }}
- Alert resolved at: {{ .EndsAt | date "2006-01-02 15:04:05 MST" }}
{{ end -}}
{{ end -}}
{{ define "alert.labels_and_annotations" }}
Alert labels: {{ len .Labels.SortedPairs }}
{{ range .Labels.SortedPairs -}}
- {{ .Name }} = {{ .Value }}
{{ end }}
Alert annotations: {{ len .Annotations.SortedPairs }}
{{ range .Annotations.SortedPairs -}}
- {{ .Name }} = {{ .Value }}
{{ end }}
{{ end -}}
如下图所示:
