prometheus AlertManager 微信报警配置

发布时间:2020-07-12 14:27:24 作者:laihuadongcto
来源:网络 阅读:1686

准备工作:

获取企业×××的对外接口

企业×××的secret_api

企业信息ID corp_id

wechat_api_url: wechat对外接口https://qyapi.weixin.qq.com/cgi-bin/
wechat_×××: 企业×××("企业应用"-->"自定应用"[Prometheus]--> "Secret") Prometheus是本人自创建应用名称
wechat_api_corp_id: 企业信息("我的企业"--->"CorpID"[在底部])
to_party: 1值是组的ID 你可通过链接去定制报警信息接收人或者组(https://work.weixin.qq.com/ap...
agent_id: 企业×××("企业应用"-->"自定应用"[Prometheus]--> "AgentId") Prometheus是本人自创建应用名称


如果prometheus和alertmanager的配置文件是分开(不是helm安装)

Prometheus中AlertManager配置:

alerting 与 global同级

# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
- localhost:9093

rules配置文件加入到Prometheus配置文件中

rule_files:
- "/usr/local/prometheus/rules.yml"

prometheus rules配置
创建rule.yml文件
根据需求添加报警规则
groups:
- name: prometheus_go_goroutines
rules:
- alert: go_goroutines_numbers
expr: go_goroutines > 45
for: 15s
annotations:
summary: "prometheus的gorotine数据超过40!"

Prometheus AlertManager配置
alertmanager 配置文件,加入×××配置信息
global:
resolve_timeout: 2m
wechat_api_url: 'https://qyapi.weixin.qq.com/cgi-bin/'
wechat_×××: 'xxx'
wechat_api_corp_id: 'xxx'

route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 1h
receiver: 'wechat'
receivers:
- name: 'wechat'
wechat_configs:
- send_resolved: true
to_party: '1'
agent_id: '1000002'

===================================================================

如果是用helm安装的,那么我们的promethues和alertmanager的配置是在一个文件中的

vim  prometheus-operator-custom.yaml   # 修改配置

alertmanager: 下配置
config:
global:
# 每2分钟检查一次是否恢复
resolve_timeout: 3m
templates:
- '/etc/alertmanager/config/template_wechat.tmpl'
route:
# 将传入的报警中有这些标签的分为一个组.
group_by: ['wechat_alert']
# 指分组创建多久后才可以发送压缩的警报,也就是初次发警报的延时.
# # 这样会确保第一次通知的时候, 有更多的报警被压缩在一起.
group_wait: 15s
# 当第一个通知发送,等待多久发送压缩的警报
group_interval: 15s
# 如果报警发送成功, 等待多久重新发送一次
repeat_interval: 3m
receiver: 'wechat'
routes:
- receiver: 'wechat'
continue: true
receivers:
- name: 'wechat'
wechat_configs:
# 是否发送恢复告警
- send_resolved: true
# ×××公众号ID
corp_id: 'XXX'
# ×××应用密钥
×××: 'XXX'
# 可发送的用户名 可以多个 
#to_user: '@all'
# 部门ID 点击部门的时候 右下角的弹窗可以看到 比较隐蔽
to_party: '92'
agent_id: '1000010'
# 模板格式:
templateFiles:
template_wechat.tmpl: |-
{{ define "wechat.default.message" }}
{{ range .Alerts }}
=====start======
告警程序: k8s_prometheus_alert
告警级别: {{ .Labels.severity }}
告警类型: {{ .Labels.alertname }}
故障主机: {{ .Labels.name }}
告警阈值: {{ .Annotations.value }}
告警主题: {{ .Annotations.summary }}
# 时间默认UTC 所以后边加入28800e9 也就是多了啦8个小时 
触发时间: {{ (.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
======end======
{{ end }}
{{ end }}



报警规则配置文件:
helm安装配合文件合并,所以报警规则独立出一个文件,加载时多加载一个文件即可。

vim  rules-custom.yaml  # 编辑规则文件


additionalPrometheusRules:
- name: cpu1
groups:
- name: cpu load
rules:
- alert: pod cpu 超过1%
expr: (sum by(name)(rate(container_cpu_usage_seconds_total{image!=""}[5m]))*100) > 30
for: 1m
labels:
severity: critical
annotations:
value: "{{ $value }}"
description: The configuration of the instances of the Alertmanager cluster`{{$labels.service}}` are out of sync.
summary: "这是第一个组的第一个测试 OK"
# - alert: pod memcache 超过1%
# expr: (sum by(name)(rate(container_cpu_usage_seconds_total{image!=""}[5m]))*100) > 5
# for: 5m
# labels:
# severity: critical
# annotations:
# description: An unexpected number of Alertmanagers are scraped or Alertmanagers disappeared from discovery.
# summary: "这是第一个组的第二个测试"
- name: cpu2
groups:
- name: node load
rules:
- alert: 另一个group pod 超过 1%
expr: (sum by(name)(rate(container_cpu_usage_seconds_total{image!=""}[5m]))*100) > 30
for: 1m
labels:
severity: critical
annotations:
value: "{{ $value }}"
summary: "这是第二个组的测试 ok"



最后我们在加载的时候只需多加载一个配置文件:

可同时加载两个配置:
helm upgrade monitoring stable/prometheus-operator --version=5.0.3 --namespace=monitoring -f prometheus-operator-custom.yaml -f rules-custom.yaml


推荐阅读:
  1. 配置prometheus和alertmanager通信实现触发报警
  2. Prometheus-2.15.0部署

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

k8s age eu

上一篇:Custom contact

下一篇:性能测试策略整理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》