当前位置: 首页 > news >正文

prometheus + alertmanager 搭建告警通知

prometheus

下载prometheus-2.53.2

prometheus.yml文件修改

global:scrape_interval: 15sevaluation_interval: 15salerting:alertmanagers:- static_configs:- targets:- 127.0.0.1:9093rule_files:- "rules/rule-*.yml"scrape_configs:- job_name: "prometheus"static_configs:- targets: ["localhost:9090", "127.0.0.1:9104"]

其中127.0.0.1:9104是mysqld_exporter的metric地址

新建rules目录,并创建规则 rule-first.yml

groups:- name: InstanceDown_Rulerules:- alert: InstanceDown  # 告警名称expr: up == 0        # 告警条件for: 30s              # 告警触发前需要持续满足条件的时间labels:severity: critical # 告警的严重程度annotations:summary: "Instance {{ $labels.instance }} down"description: "Instance {{ $labels.instance }} has been down for more than 5 minutes."

启动Prometheus

prometheus --config.file=prometheus.yml --storage.tsdb.path=./data --web.enable-lifecycle

访问Prometheus

http://127.0.0.1:9090/

alertmanager

下载alertmanager-0.27.0

修改配置文件alertmanager.yml

route:group_by: ['instance']group_wait: 10sgroup_interval: 20s#repeat_interval: 1hreceiver: 'web.hook'
receivers:- name: 'web.hook'webhook_configs:- url: 'http://127.0.0.1:5001/alert/hook'send_resolved: true
#inhibit_rules:
#  - source_match:
#      severity: 'critical'
#    target_match:
#      severity: 'warning'
#    equal: ['instance']

其中 http://127.0.0.1:5001/alert/hook 是接收告警的钩子接口

启动alertmanager

alertmanager --config.file=alertmanager.yml

访问alertmanager

http://127.0.0.1:9093/

grafana

下载grafana-11.2.1

启动grafana

grafana-server

访问grafana

http://127.0.0.1:3000

mysqld_exporter

下载 mysqld_exporter-0.15.1

在mysqld_exporter根目录创建.my.cnf文件

[client]
user=root
password=root

user和password分别是MySQL数据库的用户和密码;mysqld_exporter需要安装与mysql_server同一个服务器上。

启动mysqld_exporter

mysqld_exporter

访问mysqld_exporter

http://127.0.0.1:9104/

编写hook

@PostMapping("/alert/hook")
public Response<String> alertHook(@RequestBody Map<String, Object> alertDataMap) {//TODO 在这里实现告警处理,发微信、邮件、钉钉都可以System.out.println(JSON.toJSONString(alertDataMap));return Response.ok("success");
}

以上使用Java编写接口,根据实际可以使用任何一种语言编写。

可以关闭mysqld_exporter,模拟服务宕机。/alert/hook接口收到的数据如下:

{"receiver": "web\\.hook","status": "firing","alerts": [{"status": "firing","labels": {"alertname": "InstanceDown","instance": "127.0.0.1:9104","job": "prometheus","severity": "critical"},"annotations": {"description": "Instance 127.0.0.1:9104 has been down for more than 5 minutes.","summary": "Instance 127.0.0.1:9104 down"},"startsAt": "2024-10-10T11:27:58.11Z","endsAt": "0001-01-01T00:00:00Z","generatorURL": "http://olive-my:9090/graph?g0.expr=up+%3D%3D+0&g0.tab=1","fingerprint": "106b3a6075af7628"}],"groupLabels": {"instance": "127.0.0.1:9104"},"commonLabels": {"alertname": "InstanceDown","instance": "127.0.0.1:9104","job": "prometheus","severity": "critical"},"commonAnnotations": {"description": "Instance 127.0.0.1:9104 has been down for more than 5 minutes.","summary": "Instance 127.0.0.1:9104 down"},"externalURL": "http://olive-my:9093","version": "4","groupKey": "{}:{instance=\"127.0.0.1:9104\"}","truncatedAlerts": 0
}
http://www.lryc.cn/news/457259.html

相关文章:

  • 爬虫案例——爬取腾讯社招
  • VAS1800Q奇力科技线性芯片电荷泵热处理
  • SQL Inject-基于报错的信息获取
  • redistemplate宇jedis区别
  • JavaWeb--09Servlet深入:JavaWeb三层架构---注册系统
  • 教育技术革新:SpringBoot在线教育系统开发指南
  • EasyAnimate
  • Unity实现自定义图集(五)
  • 2024年最佳平替电容笔对比:西圣、摩米士、倍思,哪款更适合你?
  • 关系型数据库索引操作
  • 深度学习基础—卷积神经网络示例
  • vite学习教程03、vite+vue2打包配置
  • Java | Leetcode Java题解之第461题汉明距离
  • ubuntu 开放 8080 端口快捷命令
  • 关于头文件为什么要加条件编译语句
  • strstr
  • C语言 | Leetcode C语言题解之第454题四数相加II
  • Leetcode 删除链表倒数第 N 个节点
  • [移植] tgi 编译
  • vue-element-admin后台集成方案
  • 40条经典ChatGPT论文指令,圈定选题和进行论文构思
  • 在不支持WSL2的Windows环境下安装Redis并添加环境变量的方法
  • 《Electron 基础知识》代码打开开发者工具DevTools
  • 小米R3G刷机OP
  • 移动机器人规划控制合集
  • Type-C接口桌面显示器的优势
  • 机器学习中的熵(Entropy)是什么?
  • JAVA基础:Lock不同的锁形式
  • 【LeetCode每日一题】——679.24 点游戏
  • 【Conda】Conda命令详解:高效更新与环境管理指南