1. Docker部署alertmanager
docker run --name prometheus-alertmanager \
-e TZ='Asia/Shanghai' \
-p 0.0.0.0:9093:9093 \
-v /etc/alertmanager:/etc/alertmanager/ \
-itd ubuntu/prometheus-alertmanager:latest
2. 修改Prometheus的配置,连接alertmanager
位置:/etc/prometheus/prometheus.yml
修改以下内容,targets填入alertmanager的ip和端口
# 告警配置
alerting:
alertmanagers:
- follow_redirects: true
scheme: http
timeout: 10s
api_version: v2
static_configs:
- targets: ['192.168.7.254:9093']
3. Prometheus配置热重载
curl -X POST http://192.168.7.254:9090/-/reload
4. 编写配置文件
位置:/etc/alertmanager/alertmanager.yml
注意:这里用的是163的邮箱测试的,开启tls加密一直没调通,不开tls可以,不开tls的smtp端口是25
global:
smtp_from: your_email@163.com
smtp_smarthost: smtp.163.com:25
smtp_auth_username: your_email@163.com
smtp_auth_password: your_授权码
smtp_require_tls: false
route:
receiver: team-X-mails
group_by:
- alertname
group_wait: 1s
group_interval: 5s
repeat_interval: 5m
receivers:
- name: team-X-mails
email_configs:
- send_resolved: true
to: your_receiver_email@163.com
html: '{{ template "email.default.html" . }}'
templates:
- /etc/alertmanager/template/*.tmpl
5. 编写邮件模板
位置:/etc/alertmanager/templates/email.tmpl
{{ define "email.default.html" }}
{{ range .Alerts }}
告警实例: {{ .Labels.instance }} <br>
告警状态:{{ .Status }} <br>
告警级别: {{ .Labels.severity }} 级 <br>
告警类型: {{ .Labels.alertname }} <br>
触发阀值:{{ .Annotations.value }} <br>
告警详情: {{ .Annotations.description }} <br>
告警时间:{{ .StartsAt.Format "2006-01-02 15:04:05" }} <br>
{{ end }}{{ end }}