配置alertmanager钉钉告警
[root@prometheus ~]# wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v1.4.0/prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz
[root@prometheus ~]# tar xf prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz -C /usr/local/
[root@prometheus ~]# ln -sv /usr/local/prometheus-webhook-dingtalk-1.4.0.linux-amd64/ /usr/local/Prometheus_webhook-dingtalk
[root@prometheus ~]# mkdir -p /usr/local/Prometheus_webhook-dingtalk/template
[root@prometheus ~]# vim /usr/local/Prometheus_webhook-dingtalk/template/template.tmpl
{{ define "__subject" }}[Linux 基础监控告警:{{ .Alerts.Firing | len }}] {{ end }}
{{ define "__text_list" }}{{ range . }}
{{ range .Labels.SortedPairs }}
{{ if eq .Name "instance" }}> 实例: {{ .Value | html }}{{ end }}
{{ end }}
{{ range .Labels.SortedPairs }}
{{ if eq .Name "serverity" }}> 告警级别: {{ .Value | html }}{{ end }}
{{ if eq .Name "hostname" }}> 主机名称: {{ .Value | html }}{{ end }}
{{ end }}
{{ range .Annotations.SortedPairs }}
{{ if eq .Name "description" }}> 告警详情: {{ .Value | html }}{{ end }}
{{ end }}
触发时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }}
{{"============================"}}
{{ end }}{{ end }}
{{ define "ding.link.title" }}{{ template "__subject" . }}{{ end }}
{{ define "ding.link.content" }}
{{ if gt (len .Alerts.Firing) 0 }}#### [{{ .Alerts.Firing | len }}]【Linux 报警触发】
{{ template "__text_list" .Alerts.Firing }}{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}#### [{{ .Alerts.Resolved | len }}]【Linux 报警恢复】
{{ end }}
{{ end }}
[root@prometheus ~]# cp /usr/local/Prometheus_webhook-dingtalk/config.example.yml /usr/local/Prometheus_webhook-dingtalk/config.example.yml.bak
[root@prometheus ~]# vim /usr/local/Prometheus_webhook-dingtalk/config.example.yml
## Request timeout
# timeout: 5s
## Uncomment following line in order to write template from scratch (be careful!)
#no_builtin_template: true
## Customizable templates path
#templates:
# - contrib/templates/legacy/template.tmpl
templates:
- /usr/local/Prometheus_webhook-dingtalk/template/*.tmpl
## You can also override default template using `default_message`
## The following example to use the 'legacy' template from v0.3.0
#default_message:
# title: '{{ template "legacy.title" . }}'
# text: '{{ template "legacy.content" . }}'
## Targets, previously was known as "profiles"
targets:
webhook:
url: https://oapi.dingtalk.com/robot/send?access_token=xxx
# secret for signature
# secret: SECxxxxxxxxxx
message:
title: '{{ template "ding.link.title" . }}'
text: '{{ template "ding.link.content" . }}'
[root@prometheus ~]# nohup /usr/local/Prometheus_webhook-dingtalk/prometheus-webhook-dingtalk --ding.profile=ops_dingding=“自己的webhook” &
- 修改alertmanager.yml配置(添加接收者receivers)
[root@prometheus ~]# vim /usr/local/Prometheus_alertmanager/alertmanager.yml
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.163.com:25' # smtp地址
smtp_from: 'xxx@163.com' # 谁发邮件
smtp_auth_username: 'xxx@163.com' # 邮箱用户
smtp_auth_password: 'xxxxx' # 邮箱客户端授权密码
smtp_require_tls: false
templates: # 指定邮件模板的路径,可以使用相对路径,template/*.tmpl的方式
- '/usr/local/Prometheus/alertmanager/template/*.tmpl'
route: # route用来设置报警的分发策略
group_by: ["alertname"] # 分组名
group_wait: 30s # 当收到告警的时候,等待三十秒看是否还有告警,如果有就一起发出去
group_interval: 30s # 发送警告间隔时间
repeat_interval: 20m # 重复报警的间隔时间
receiver: Node_warning # 设置默认接收人,如果想分组接收,把下面这段的注释去掉
# routes: # 可以指定哪些组接收哪些消息
# - receiver: 'Node_warning'
# continue: true
# group_wait: 10s
# match_re:
# service: mysql|cassandra # 所有service=mysql或者service=cassandra的告警分配到数据库接收端
# - receiver: 'MySQL_warning'
# group_wait: 10s
# match_re: # 根据标签分组,匹配标签dest=szjf的为fping-receiver组
# serverity: warning
receivers: # 定义接收者,将告警发送给谁
- name: 'Node_warning'
email_configs:
- to: 'xxx@126.com'
webhook_configs:
- url: http://127.0.0.1:8060/dingtalk/ops_dingding/send #警报被解决之后是否通知
send_resolved: true
[root@prometheus ~]# systemctl restart alertmanager