前提条件:
1.部署好prometheus,alertmanage,测试机node_exporter
2.设置好告警规则
3.配置好邮件告警能用默认模版发送
1.编写alertmanager配置文件,alertmanager.yml
添加templates和邮件使能html模版
global: resolve_timeout: 5m smtp_from: '1633914619@qq.com' smtp_smarthost: 'smtp.qq.com:465' smtp_auth_username: '1633914619@qq.com' smtp_auth_password: 'mlaatibrycfnehac' smtp_require_tls: false smtp_hello: 'qq.com' route: group_by: ['alertname'] group_wait: 30s group_interval: 5m repeat_interval: 1h receiver: 'email' templates: - '/home/prometheus_tmpl/alertsend.tmpl' receivers: - name: 'web.hook' webhook_configs: - url: 'http://127.0.0.1:5001/' - name: 'email' email_configs: - to: '19539410809@139.com' html: '{{ template "email.to.html" . }}' send_resolved: true inhibit_rules: - source_match: severity: 'critical' target_match: severity: 'warning' equal: ['alertname', 'dev', 'instance']
templates: - '/home/prometheus_tmpl/alertsend.tmpl'
html: '{{ template "email.to.html" . }}'
2.在templates定义的路径下创建自定义邮件模版
[root@prometheus prometheus_tmpl]# cat alertsend.tmpl {{ define "email.to.html" }} {{ range .Alerts }} 告警程序: prometheus_alert <br> 告警级别: {{ .Labels.severity }} 级 <br> 告警类型: {{ .Labels.alertname }} <br> 故障主机: {{ .Labels.instance }} <br> 告警主题: {{ .Annotations.summary }} <br> 告警详情: {{ .Annotations.description }} <br> 触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }} <br> {{ end }} {{ end }}
define "email.to.html" 要跟 alertmanage.yml配置文件的要对应。
重启alertmanage组件。