1,alertmanager.yml配置文件。
线上自用的一个alertmanager.yaml 配置文件(公司生产设定的也差不多,只不过是多了不同告警对象的路由以及webhook设置)
[root@cn-hz-21yunwei-devops alertmanager]# cat alertmanager.yml global: resolve_timeout: 5m #定义告警媒介-邮箱 smtp_smarthost: smtp.exmail.qq.com:465 smtp_from: alert@21yunwei.com smtp_auth_username: alert@21yunwei.com smtp_auth_password: ****** smtp_require_tls: false #定义告警媒介-企业微信 wechat_api_url: 'https://qyapi.weixin.qq.com/cgi-bin/' wechat_api_secret: '****' wechat_api_corp_id: '****' #定义自定义告警模板 templates: - '/usr/local/prometheus/server/alertmanager/template_alert/*.tmpl' route: group_by: ['alertname'] group_wait: 10s group_interval: 10s repeat_interval: 10m receiver: 'email' #receiver: '21yunwei.wechat' receivers: - name: 'email' email_configs: - to: 'admin@21yunwei.com' send_resolved: true html: '{{ template "email.default.html" . }}' #receivers: # - name: '21yunwei.wechat' # wechat_configs: # - to_user: 'admin' # agent_id: '*****' # send_resolved: true
2,报警模板记录
(1)企业微信报警模版:
[root@cn-hz-21yunwei-devops template_alert]# cat wechat.tmpl {{ define "__text_alert_list" }}{{ range . }} 告警程序:prometheus_alert 告警级别:{{ .Labels.serverity }} 告警类型:{{ .Labels.alertname }} 主机IP: {{ .Labels.instance }} 主机名: {{ .Labels.hostname }} 告警主题: {{ .Annotations.summary }} 告警描叙: {{ .Annotations.description }} 触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }} ------------------------ {{ end }}{{ end }} {{ define "__text_resolve_list" }}{{ range . }} 恢复程序:prometheus_alert 恢复类型:{{ .Labels.alertname }} 主机IP: {{ .Labels.instance }} 主机名: {{ .Labels.hostname }} 恢复描叙: {{ .Annotations.description }} 触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }} ------------------------ {{ end }}{{ end }} {{ define "wechat.default.message" }} {{ if gt (len .Alerts.Firing) 0 -}} 告警列表: ----------- {{ template "__text_alert_list" .Alerts.Firing }} {{- end }} {{ if gt (len .Alerts.Resolved) 0 -}} 恢复列表: {{ template "__text_resolve_list" .Alerts.Resolved }} {{- end }} {{- end }}
[root@cn-hz-21yunwei-devops template_alert]# cat email.tmpl {{ define "__text_alert_list" }}{{ range . }} 主机IP: {{ .Labels.instance }} 主机名: {{ .Labels.hostname }} 告警级别:{{ .Labels.serverity }} 告警类型:{{ .Labels.alertname }} 告警主题: {{ .Annotations.summary }} 告警描叙: {{ .Annotations.description }} 触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }} ------------------------ {{ end }}{{ end }} {{ define "__text_resolve_list" }}{{ range . }} 主机IP: {{ .Labels.instance }} 主机名: {{ .Labels.hostname }} 恢复类型:{{ .Labels.alertname }} 恢复描叙: {{ .Annotations.description }} 触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }} ------------------------ {{ end }}{{ end }} {{ define "email.default.html" }} {{ if gt (len .Alerts.Firing) 0 -}} 告警列表: --------------------- {{ template "__text_alert_list" .Alerts.Firing }} {{- end }} {{ if gt (len .Alerts.Resolved) 0 -}} 恢复列表: {{ template "__text_resolve_list" .Alerts.Resolved }} {{- end }} {{- end }}
转载请注明:21运维 » Prometheus集成alertmanager插件的 alertmanager.yml配置文件与报警模板–自用笔记