alertmanager 邮件告警&自定义告警模板
准备工作
1. 准备163邮箱
2. 登录163邮箱 设置中开启 SMTP功能
3. 新增授权码,需要保存后面配置文件需要用到
alertmanage 配置
配置文件
global: #resolve_timeout: 5m smtp_smarthost: 'smtp.163.com:25' # 163 smtp 服务器地址 smtp_from: 'super@163.com' # 告警邮件地址 smtp_auth_username: 'super' # 认证用户名 smtp_auth_password: 'xxxx' # 认证秘钥 smtp_require_tls: false # 不启用 ssl templates: - './template/alertmanager-email.tmpl' # 读取自定义通知模板定义的文件。 route: # 路由组 group_by: ['alertname', 'app'] group_wait: 30s group_interval: 40s repeat_interval: 1m receiver: email-receiver # 发送给那个组 receivers: # 收件人组 - name: 'email-receiver' email_configs: - to: 'xx1@sina.com' # 收件人地址 html: '{{ template "email.to.html" . }}' # HTML模板文件正文 send_resolved: true inhibit_rules: - source_match: severity: 'critical' target_match_re: severity: '.*' equal: ['instance']
# 文章原文: https://www.cnblogs.com/yanshicheng/p/15548449.html
模板文件
https://www.cnblogs.com/yanshicheng/p/15548449.html
[root@ops alertmanager]# cat template/alertmanager-email.tmpl {{ define "email.to.html" }} {{- if gt (len .Alerts.Firing) 0 -}} {{- range $index, $alert := .Alerts -}} ========= <span style=color:red;font-size:36px;font-weight:bold;> 监控告警 </span>=========<br> <span style=font-size:20px;font-weight:bold;> 告警程序:</span> Alertmanager <br> <span style=font-size:20px;font-weight:bold;> 告警类型:</span> {{ $alert.Labels.alertname }} <br> <span style=font-size:20px;font-weight:bold;> 告警级别:</span> {{ $alert.Labels.severity }} 级 <br> <span style=font-size:20px;font-weight:bold;> 告警状态:</span> {{ .Status }} <br> <span style=font-size:20px;font-weight:bold;> 故障主机:</span> {{ $alert.Labels.instance }} {{ $alert.Labels.device }} <br> <span style=font-size:20px;font-weight:bold;> 告警主题:</span> {{ .Annotations.summary }} <br> <span style=font-size:20px;font-weight:bold;> 告警详情:</span> {{ $alert.Annotations.message }}{{ $alert.Annotations.description}} <br> <span style=font-size:20px;font-weight:bold;> 主机标签:</span> {{ range .Labels.SortedPairs }} <br> [{{ .Name }}: {{ .Value | html }} ]{{ end }}<br> <span style=font-size:20px;font-weight:bold;> 故障时间:</span> {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}<br> ========= = end = =========<br> <br> <br> <br> <br> <div> <div style=margin:40px> <p style=font-size:20px>运维部门</p> <p style=color:red;font-size:14px> (这是一封自动发送的邮件,请勿回复。) </p> </div> <div align=right style="margin:40px;border-top:solid 1px gray" id=bottomTime> <p style=margin-right:20px> Super Ops 开源运维平台 </p> <label style=margin-right:20px> {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 " }}<br> </label> </div> </div> {{- end }} {{- end }} {{- if gt (len .Alerts.Resolved) 0 -}} {{- range $index, $alert := .Alerts -}} ========= <span style=color:#00FF00;font-size:24px;font-weight:bold;> 告警恢复 </span>=========<br> <span style=font-size:20px;font-weight:bold;> 告警程序:</span> Alertmanager <br> <span style=font-size:20px;font-weight:bold;> 告警主题:</span> {{ $alert.Annotations.summary }}<br> <span style=font-size:20px;font-weight:bold;> 告警主机:</span> {{ .Labels.instance }} <br> <span style=font-size:20px;font-weight:bold;> 告警类型:</span> {{ .Labels.alertname }}<br> <span style=font-size:20px;font-weight:bold;> 告警级别:</span> {{ $alert.Labels.severity }} 级 <br> <span style=font-size:20px;font-weight:bold;> 告警状态:</span> {{ .Status }}<br> <span style=font-size:20px;font-weight:bold;> 告警详情:</span> {{ $alert.Annotations.message }}{{ $alert.Annotations.description}}<br> <span style=font-size:20px;font-weight:bold;> 故障时间:</span> {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}<br> <span style=font-size:20px;font-weight:bold;> 恢复时间:</span> {{ ($alert.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}<br> {{- end }} ========= = end = ========= <br> <br> <br> <br> <div> <div style=margin:40px> <p style=font-size:20px>运维部门</p> <p style=color:red;font-size:14px> (这是一封自动发送的邮件,请勿回复。) </p> </div> <div align=right style="margin:40px;border-top:solid 1px gray" id=bottomTime> <p style=margin-right:20px> Super Ops 开源运维平台 </p> <label style=margin-right:20px> {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 " }}<br> </label> </div> </div> {{- end }} {{- end }} {{- end }}
效果演示
告警邮件
告警恢复
作者:闫世成
出处:http://cnblogs.com/yanshicheng
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题或建议,请联系上述邮箱,非常感谢。