prometheus AlertManager 微信报警配置
admin
2023-03-03 05:40:50
0

准备工作:

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

企业×××的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


相关内容

热门资讯

农业农村“半年报”发布,这些看... 7月24日召开的国新办发布会上,农业农村部介绍2026年上半年农业农村经济运行情况。夏粮喜获丰收、常...
世界遗产+1!景德镇打卡地图,... 北京时间7月25日,在韩国釜山举行的第48届世界遗产大会上,由我国申报的“景德镇手工瓷业遗存”项目经...
有人呼吁白宫“封杀”中国开源大... 未来一段时间,将有多款中国开源模型发布。Kimi K3定于7月27日正式开源,阿里即将开源2.4万亿...
20名“高考状元”选择学医!此... 2026年香港高考的24名“状元”中,有20人明确表示将留港攻读医科。多年来,香港最顶尖的“学霸们”...
“抢人大战”,风向有变? 各地一场“抢老人”的大战,已然徐徐拉开大幕。最近,有着人口净流出压力的吉林,计划拿出3亿元发展旅居康...
石家庄一家不限量的爱心素食面馆... 7月24日正午时分,记者来到位于石家庄市新华区东明家居西二环店北门旁的“素面”小店,不大的店面里坐满...
中材科技申请风电叶片外补强辅助... 国家知识产权局信息显示,中材科技(伊吾)风电叶片有限公司申请一项名为“风电叶片外补强辅助固化装置”的...
为人工智能治理提供重要公共产品 图为2026世界人工智能大会暨人工智能全球治理高级别会议举办期间,在世博展览馆灵心巧手展位,参观者观...
泽连斯基称俄对乌多地发动大规模... 当地时间25日,乌克兰总统泽连斯基通报称,俄军于当日凌晨对乌克兰多地发动大规模空袭,使用2枚导弹、近...