prometheus的配置
1 在prometheus配置文件添加alertmanager地址
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- xxx.xxx.xxx.xxx:9093
#xxx
2 在prometheus配置文件添加报警规则
# 设置规则文件的位置
rule_files:
- "rules/*.yml"
# 编辑规则文件
vim rules/node_rules.yml
groups:
- name: node-rules
rules:
- alert: node-up
expr: up == 0
for: 15s
labels:
severity: 1
team: node
annotations:
summary: "{{$labels.instance}}Instance has been down for more than 5 minutes"
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
3 重启prometheus,加载配置
alertmanager的配置
1 alertmanager配置文件
vim alertmanager.yml
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.163.com:25'
smtp_from: '
[email protected]'
smtp_auth_username: '
[email protected]'
smtp_auth_password: 'WJEZTKXVFDVVVV'
smtp_require_tls: false
templates:
- '/usr/local/alertmanager/email.tmpl'
route:
group_by: ['alertname']
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
receiver: 'email'
receivers:
- name: 'email'
email_configs:
- to: '
[email protected]'
html: '{{ template "email.to.html" . }}'
send_resolved: true
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname']
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
2 编辑邮件模板文件
vim email.tmpl
{{ define "email.to.html" }}
{{ if gt (len .Alerts.Firing) 0 }}{{ range .Alerts }}
@告警: <br>
告警程序: prometheus_alert <br>
告警级别: {{ .Labels.severity }} 级<br>
告警类型: {{ .Labels.alertname }} <br>
故障主机: {{ .Labels.instance }} <br>
告警主题: {{ .Annotations.summary }} <br>
告警详情: {{ .Annotations.description }} <br>
触发时间: {{ .StartsAt }} <br>
{{ end }}
{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}{{ range .Alerts }}
@恢复: <br>
告警主机: {{ .Labels.instance }} <br>
告警主题: {{ .Annotations.summary }} <br>
恢复时间: {{ .EndsAt }} <br>
{{ end }}
{{ end }}
{{ end }}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
3 重启alertmanager
检查结果
在prometheus查看

因为在alertmanager配置里面的group_by 设置的 alertname,根据标签名alertname和值 进行分组,所以这三条报警信息应该在同一个组中,使用一条邮件发送

注释
alertmanager三个概念
分组Grouping 根据报警规则的标签和标签值 将相同的报警进行分组,合并多条报警到一个通知中,避免突发出现大量报警通知
抑制Inhibition 当某一条报警已经发送,停止重复发送由此报警 引发 的其他异常或故障的报警信息,例如mysql的从库挂了,已经发送了报警通知,那么从库的主从同步的报警信息就不要再重复发送了
静默Silences 提供了一个简单的机制,根据标签快速对报警进行静默处理,对传进来的报警进行匹配检查,如果接收到报警符合静默的配置,Alertmanager就不会发送报警通知,静默需要在WEB UI界面中设置临时屏蔽指定的报警通知
配置文件主要配置段
global 全局配置
template 报警信息模板,可以自定义email、企业微信的模板
route 在收到prometheus的报警后,将报警信息发送给指定的地址,Alertmanager归传入的报警信息进行处理,根据所定义的规则和配置进行匹配。对于路由可以理解为树状结构,设置的第一个route是根节点,往下就是子节点,每个报警传进来以后,会由上向下匹配,当匹配到节点后就会发送通知。
receiver 接收器 每一个接收器都需要设置一个全局唯一的名称,并且设置一个或者多个通知方式:邮件、微信、短信等等。
inhibit_rules 抑制器 可以指定在特定条件下需要忽略的报警
配置文件参数注释
vim alertmanager.yml
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.163.com:25' # 邮箱服务器的地址
smtp_from: '
[email protected]' # 报警邮件使用的邮箱
smtp_auth_username: '
[email protected]' # 邮箱用户
smtp_auth_password: 'WJEZTKXVFDVVVV' # 邮箱登录授权码
smtp_require_tls: false
templates:
- '/usr/local/alertmanager/email.tmpl' # 报警邮件模板文件
route:
group_by: ['alertname'] # 根据标签alertname分组
group_wait: 30s # 同组内,30秒内出现报警,在一个组内发出
group_interval: 5m # 在发送新的报警信息时,等待多长时间
repeat_interval: 1h # 如果已经发送报警信息,再次发送的时间间隔
receiver: 'email' # 指定报警信息的接收器
receivers:
- name: 'email' # 设置接收器的名称
email_configs:
- to: '
[email protected]' # 接收报警邮件的地址,逗号分割
html: '{{ template "email.to.html" . }}' # 使用的模板
send_resolved: true
inhibit_rules: # 抑制规则
- source_match: # 源标签报警出发时,抑制目标标签的报警
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'job', 'service'] # 报警中必须匹配这三个标签才会被抑制
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
多路由匹配示例:
route:
group_wait: 10s
group_interval: 30s
repeat_interval: 30m
receiver: "slack"
routes:
- receiver: "slack"
group_wait: 10s
match_re:
severity: critical|warning
continue: true
- receiver: "pager"
group_wait: 10s
match_re:
severity: critial
continue: true
receivers:
- name: "slack"
slack_configs:
- api_url: 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/xxxxxxxxxxxxxxxxxxxxxxxxxxx'
send_resolved: true
channel: 'monitoring'
text: "{{ range .Alerts }}<!channel> {{ .Annotations.summary }}\n{{ .Annotations.description }}\n{{ end }}"
- name: "pager"
webhook_configs:
- url: http://a.b.c.d:8080/send/sms
send_resolved: true
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
参考:
https://awesome-prometheus-alerts.grep.to/alertmanager
https://www.cnblogs.com/hahaha111122222/p/14247590.html
原网站版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_12227788/5522616