Prometheus配置alertmanager告警邮件通知

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: [email protected]
  smtp_smarthost: smtp.163.com:25
  smtp_auth_username: [email protected]
  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: [email protected]
    html: '{{ template "email.default.html" . }}'
templates:
- /etc/alertmanager/template/*.tmpl
5. 编写邮件模板

位置:/etc/alertmanager/templates/email.tmpl

{{ define "email.default.html" }}
{{ range .Alerts }}
告警实例: {{ .Labels.instance }} 
告警状态:{{ .Status }}
告警级别: {{ .Labels.severity }} 级
告警类型: {{ .Labels.alertname }}
触发阀值:{{ .Annotations.value }}
告警详情: {{ .Annotations.description }}
告警时间:{{ .StartsAt.Format "2006-01-02 15:04:05" }}
{{ end }}{{ end }}

你可能感兴趣的:(Prometheus配置alertmanager告警邮件通知)