Alertmanager对接钉钉
alertmanager 钉钉告警配置
1.安装prometheus-webhook-dingtalk
1.1 二进制包安装
# 二进制包下载 https://github.com/timonwong/prometheus-webhook-dingtalk/releases wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v0.3.0/prometheus-webhook-dingtalk-0.3.0.linux-amd64.tar.gz # 启动服务 ./prometheus-webhook-dingtalk --ding.profile="webhook1=https://oapi.dingtalk.com/robot/send?access_token={替换成自己的dingding token}"
1.2 docker镜像安装
前提是已经完成docker安装
docker pull timonwong/prometheus-webhook-dingtalk # 启动容器 docker run -d -p 8060:8060 --name webhook timonwong/prometheus-webhook --ding.profile="webhook1=https://oapi.dingtalk.com/robot/send?access_token={替换成自己的dingding token}
2.alertmanager中配置
cat alertmanager.yml
global: resolve_timeout: 5m route: receiver: webhook group_wait: 30s group_interval: 5m repeat_interval: 5m group_by: [alertname] routes: - receiver: webhook group_wait: 10s receivers: - name: webhook webhook_configs: - url: http://localhost:8060/dingtalk/webhook1/send send_resolved: true
完整配置:
cat alertmanager.yml
global: # 在没有报警的情况下声明为已解决的时间 resolve_timeout: 2m # 配置邮件发送信息 smtp_smarthost: ‘smtp.qiye.aliyun.com:465‘ smtp_from: ‘your_email‘ smtp_auth_username: ‘your_email‘ smtp_auth_password: ‘email_passwd‘ smtp_hello: ‘your_email‘ smtp_require_tls: false # 所有报警信息进入后的根路由,用来设置报警的分发策略 route: # 这里的标签列表是接收到报警信息后的重新分组标签,例如,接收到的报警信息里面有许多具有 cluster=A 和 alertname=LatncyHigh 这样的标签的报警信息将会批量被聚合到一个分组里面 group_by: [‘alertname‘, ‘cluster‘] # 当一个新的报警分组被创建后,需要等待至少group_wait时间来初始化通知,这种方式可以确保您能有足够的时间为同一分组来获取多个警报,然后一起触发这个报警信息。 group_wait: 30s # 当第一个报警发送后,等待‘group_interval‘时间来发送新的一组报警信息。 group_interval: 5m # 如果一个报警信息已经发送成功了,等待‘repeat_interval‘时间来重新发送他们 repeat_interval: 5m # 默认的receiver:如果一个报警没有被一个route匹配,则发送给默认的接收器 receiver: default # 优先使用default发送 # 上面所有的属性都由所有子路由继承,并且可以在每个子路由上进行覆盖。 routes: #子路由,使用email发送 - receiver: email match_re: serverity : email # label 匹配email group_wait: 10s receivers: - name: ‘default‘ webhook_configs: - url: http://localhost:8060/dingtalk/webhook1/send send_resolved: true # 发送已解决通知 - name: ‘email‘ email_configs: - to: ‘email@qq.com‘ send_resolved: true
Alertmanager对接微信
1.下载安装包
#下载安装包
wget https://github.com/prometheus/alertmanager/releases/download/v0.15.0-rc.1/alertmanager-0.15.0-rc.1.linux-amd64.tar.gz
#解压
tar -zxvf alertmanager-0.15.0-rc.1.linux-amd64.tar.gz
#修改名称
sudo mv alertmanager-0.15.0-rc.1.linux-amd64 /usr/local/alertmanager
cd /usr/local/alertmanager
1.1 配置alertmager
vim alertmanager.yml
global: resolve_timeout: 2m # 邮箱 smtp_smarthost: ‘smtp.qiye.aliyun.com:465‘ smtp_from: ‘your_email‘ smtp_auth_username: ‘your_email‘ smtp_auth_password: ‘passwd‘ smtp_hello: ‘your_email‘ smtp_require_tls: false # 微信模版 templates: - ‘/var/lib/alertmanager/wechat.tmpl‘ route: group_by: [‘alertname‘] group_wait: 30s group_interval: 5m repeat_interval: 5m receiver: wechat routes: - receiver: email match_re: serverity: email group_wait: 10s receivers: # 微信 - name: ‘wechat‘ wechat_configs: - corp_id: ‘企业ID‘ to_party: ‘部门ID‘ agent_id: ‘1000002‘ api_secret: ‘s2yqcDSLo6y81j6ONsztxUj_qeZQ3OoefCZzxDBqcrc‘ send_resolved: true # 钉钉 - name: ‘dingding‘ webhook_configs: - url: http://localhost:8060/dingtalk/webhook1/send send_resolved: true # email - name: ‘email‘ email_configs: - to: ‘email@test.com‘ send_resolved: true
参数说明:
corp_id: 企业微信账号唯一 ID, 可以在我的企业中查看。
to_party: 需要发送的组。
agent_id: 第三方企业应用的 ID,可以在自己创建的第三方企业应用详情页面查看。
api_secret: 第三方企业应用的密钥,可以在自己创建的第三方企业应用详情页面查看。
1.2 创建告警模板
vim wechat.tmpl
{{ define "wechat.default.message" }} {{ range $i, $alert :=.Alerts }} ========监控报警========== 告警状态:{{ .Status }} 告警级别:{{ $alert.Labels.severity }} 告警类型:{{ $alert.Labels.alertname }} 告警应用:{{ $alert.Annotations.summary }} 告警主机:{{ $alert.Labels.instance }} 告警详情:{{ $alert.Annotations.description }} 触发阀值:{{ $alert.Annotations.value }} 告警时间:{{ $alert.StartsAt.Format "2006-01-02 15:04:05" }} ========end============= {{ end }} {{ end }}
1.3 配置自启动服务
vim /lib/systemd/system/alertmanager.service
[Unit] Description=Alertmanager for Prometheus After=network-online.target [Service] Type=simple ExecStart=/usr/local/alertmanager/alertmanager --config.file=/usr/local/alertmanager/alertmanager.yml --storage.path=/usr/local/alertmanager/data/ Restart=on-failur ExecStop=/bin/kill -9 $MAINPID [Install] WantedBy=multi-user.target
2.启动服务
sudo systemctl daemon-reload sudo systemctl start alertmanager.service sudo systemctl status alertmanager.service