|NO.Z.00338|——————————|^^ 失败 ^^|——|KuberNetes&运维.V55|——|Alertmanager.v02|微信告警.v02|模板|
一、查看微信告警信息
### --- 查看微信告警配置是否生效
### --- 通过企业微信接收告警信息
~~~ ——>http://alert.test.com/——>status配置是否生效——>Config



二、Prometheus自定义告警模板
### --- 微信告警模板
[root@k8s-master01 Alarm]# vim wechat.tmpl
{{ define "wechat.default.message" }}
{{ if gt (len .Alerts.Firing) 0 -}}
Alerts Firing:
{{ range .Alerts }}
告警级别:{{ .Labels.severity }}
告警类型:{{ .Labels.alertname }}
故障主机: {{ .Labels.instance }}
Job名称: {{ .Labels.job }}
告警详情: {{ .Annotations.message }}
触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }}
{{- end }}
{{- end }}
{{ if gt (len .Alerts.Resolved) 0 -}}
Alerts Resolved:
{{ range .Alerts }}
告警级别:{{ .Labels.severity }}
告警类型:{{ .Labels.alertname }}
故障主机: {{ .Labels.instance }}
Job名称: {{ .Labels.job }}
告警主题: {{ .Annotations.message }}
触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }}
恢复时间: {{ .EndsAt.Format "2006-01-02 15:04:05" }}
{{- end }}
{{- end }}
告警链接:'http://alert.test.com'
{{- end }}
### --- 移动到挂载目录下
[root@k8s-master01 Alarm]# cp wechat.tmpl /etc/alertmanager/config/
三、在alertmanager-secrets下定义模板参数
### --- 定义模板参数
[root@k8s-master01 Alarm]# vim alertmanager-secrets.wechat.yaml
# ~~~ 增加微信告警模板:
templates:
- '/etc/alertmanager/config/*.tmpl' # 模板来源
# ~~~ 设置发送消息的参数
- name: 'wechat'
wechat_configs:
- send_resolved: true
to_tag: '1'
agent_id: '1000002' # 此行下添加如下内容
message: '{{template"wechat.default.message".}}' # 添加内容:添加这行参数内容;让其消息生效
### --- 重新触发alertmanager-secrets
[root@k8s-master01 Alarm]# kubectl replace -f alertmanager-secrets.wechat.yaml -n monitoring
secret/alertmanager-main replaced
四、查看收到的告警信息

五、创建alertmanager.yaml的secret(使用热更新的方案更新alertmanager的secrets)
### --- 创建alertmanager.yaml的secret
kubectl create secret generic alertmanager-main --from-file=alertmanager.yaml -n monitoring
### --- 之后更改alertmanager.yaml可以使用热加载去更新k8s的secret
kubectl create secret generic alertmanager-main --from-file=alertmanager.yaml -n monitoring --dry-run -o yaml | kubectl replace -f -
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通