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

【Prometheus】Grafana、Alertmanager集成

为了进一步增强完善监控系统,集成 Grafana、Alertmanager 和持久化存储是非常好的选择。以下是每个组件的集成步骤:

1. 集成 Grafana

Grafana 是一个开源的分析和监控平台,可以与 Prometheus 完美集成。

下载并安装 Grafana
cd /opt
sudo wget https://dl.grafana.com/oss/release/grafana-10.2.0.linux-amd64.tar.gz
sudo tar -zxvf grafana-10.2.0.linux-amd64.tar.gz
sudo mv grafana-10.2.0 grafana
启动 Grafana
cd /opt/grafana
./bin/grafana-server web
# 或后台启动
nohup ./bin/grafana-server web > /var/log/grafana.log 2>&1 &

默认情况下,Grafana 将在 http://localhost:3000 上运行,登录凭据是 admin/admin

在 Grafana 中添加 Prometheus 数据源
  1. 登录 Grafana Web UI。

  2. 导航到“Configuration” > “Data Sources”。

  3. 点击“Add data source”,然后选择 Prometheus。

  4. 在 URL 字段中输入你的 Prometheus 实例地址(例如:http://localhost:9090)。
    在这里插入图片描述

  5. 点击“Save & Test”。

2. 集成 Alertmanager

Alertmanager 处理由 Prometheus 发出的警报,并将它们发送给指定的通知接收器。

下载并安装 Alertmanager
cd /opt
sudo wget https://github.com/prometheus/alertmanager/releases/download/v0.26.0/alertmanager-0.26.0.linux-amd64.tar.gz
sudo tar -zxvf alertmanager-0.26.0.linux-amd64.tar.gz
sudo mv alertmanager-0.26.0.linux-amd64 alertmanager
配置 Alertmanager

编辑 simple.yml 文件来配置通知方式(如电子邮件、Slack等)。

global:resolve_timeout: 5m
route:receiver: 'default-receiver'
receivers:
- name: 'default-receiver'email_configs:- to: 'your-email@example.com' from: 'alertmanager@example.com'smarthost: 'smtp.example.com:587' # smtp.163.com:465auth_username: 'user' # 邮箱地址auth_password: 'password'

在这里插入图片描述

启动 Alertmanager
cd /opt/alertmanager
./alertmanager --config.file=simple.yml

默认监听端口为 9093,可以通过浏览器访问 http://<ECS_IP>:9093 查看 Alertmanager 页面。

配置 Prometheus 使用 Alertmanager

编辑 Prometheus 的配置文件 prometheus.yml 添加以下内容:

alerting:alertmanagers:- static_configs:- targets: ['localhost:9093']rule_files:- "alert_rules.yml"  # 这里是你定义告警规则的文件路径

确保你已经创建了 alert_rules.yml 并定义了一些告警规则。
alert_rules.yml示例:

groups:- name: instance-healthrules:# 实例宕机告警(持续5分钟无心跳)- alert: InstanceDownexpr: up == 0for: 5mlabels:severity: pageannotations:summary: "Instance {{ $labels.instance }} 已离线"description: "Prometheus 无法抓取实例 {{ $labels.instance }} 超过 5 分钟"# 高CPU使用率告警(适用于node_cpu_seconds_total指标)- alert: HighCpuUsageexpr: (100 - (avg by (instance) (rate(node_cpu_seconds_total{mode!="idle"}[5m])) * 100)) > 80for: 2mlabels:severity: warningannotations:summary: "实例 {{ $labels.instance }} CPU 使用率过高"description: "CPU 使用率超过 80% (当前值: {{ $value }}%)"# 高内存使用率告警- alert: HighMemoryUsageexpr: (node_memory_MemTotal_bytes - node_memory_MemFree_bytes - node_memory_Buffers_bytes - node_memory_Cached_bytes) / node_memory_MemTotal_bytes * 100 > 80for: 2mlabels:severity: warningannotations:summary: "实例 {{ $labels.instance }} 内存使用率过高"description: "内存使用率超过 80% (当前值: {{ $value }}%)"# 磁盘空间低告警(根分区)- alert: DiskWillFillSoonexpr: (node_disk_io_time_seconds_total{device!~"loop.*|ram.*"} offset 5m) < (node_disk_io_time_seconds_total{device!~"loop.*|ram.*"})for: 10mlabels:severity: warningannotations:summary: "实例 {{ $labels.instance }} 磁盘即将填满"description: "检测到磁盘写入速率下降,可能磁盘已满"# Pushgateway 数据未更新(长时间未推送)- alert: PushgatewayMissingDataexpr: absent(push_time_seconds{job="some_job"})for: 10mlabels:severity: warningannotations:summary: "Pushgateway 上 job=some_job 的数据未更新"description: "Pushgateway 没有接收到 job=some_job 的最新数据,请检查是否任务失败或未推送"- alert: FlinkTestexpr: (flink_taskmanager_job_task_operator_countgroup_failedjson{}) > 80for: 2mlabels:severity: warningannotations:summary: "实例 {{ $labels.deploymentId }} failedjson 过高"description: "failedjson超过 80 (当前值: {{ $value }}%)"
验证告警是否生效

启动 Prometheus 和 Alertmanager。
登录 Prometheus Web UI(默认地址:http://<ECS_IP>:9090)。
导航到 “Alerts” 页面查看告警状态。
在这里插入图片描述
登录 Alertmanager Web UI(默认地址:http://<ECS_IP>:9093)查看是否收到告警通知
在这里插入图片描述
登录邮箱,查看是否有收到邮件
在这里插入图片描述

3. 持久化存储

对于 Prometheus 来说,持久化数据可以通过本地磁盘或远程存储解决方案实现。这里我们介绍如何使用本地磁盘进行简单持久化。

修改 Prometheus 的启动命令以指定数据目录:

./prometheus --config.file=prometheus.yml --storage.tsdb.path="/data/prometheus" --web.listen-address=:9090

其中 /data/prometheus 是你希望保存 Prometheus 数据的目录,请确保该目录具有适当的权限以便 Prometheus 进程能够读写。

此外,对于更高级的持久化需求,比如高可用性设置或长期存储,可以考虑使用 Thanos 或 Cortex 等远程存储解决方案。

通过以上步骤,你应该能够成功地将 Grafana、Alertmanager 和持久化存储整合进你的监控体系中。

http://www.dtcms.com/a/267586.html

相关文章:

  • 小架构step系列05:Springboot三种运行模式
  • 理想汽车6月交付36279辆 第二季度共交付111074辆
  • 基于微信小程序的校园跑腿系统
  • MySQL——9、事务管理
  • Java-继承
  • 远程协助软件:Git的用法
  • STM32第15天串口中断接收
  • 数据结构:数组抽象数据类型(Array ADT)
  • oracle的内存架构学习
  • Hashcat 最快密码恢复工具实践指南
  • jvm架构原理剖析篇
  • C++ Qt 基础教程:信号与槽机制详解及 QPushButton 实战
  • virtualbox+vagrant私有网络宿主机无法ping通虚拟机问题请教
  • Apache 配置文件提权的实战思考
  • 数据库-元数据表
  • docker容器中Mysql数据库的备份与恢复
  • Java的AI新纪元:Embabel如何引领智能应用开发浪潮
  • 一文讲清楚React中setState的使用方法和机制
  • 应用标签思路参考
  • wsl查看磁盘文件并清理空间
  • Django跨域
  • 什么是单点登录SSO?有哪些常用的实现方式?
  • Android PNG/JPG图ARGB_8888/RGB_565‌解码形成Bitmap在物理内存占用大小的简单计算
  • SpringBoot系列—入门
  • ffplay6 播放器关键技术点分析 1/2
  • NumPy-核心函数np.matmul()深入解析
  • UI前端与数字孪生融合:为智能制造提供可视化生产调度方案
  • 分享一些服务端请求伪造SSRF的笔记
  • RAL-2025 | 触觉助力无人机空中探索!基于柔顺机器人手指的无人机触觉导航
  • 快讯|美团即时零售日订单已突破1.2亿,餐饮订单占比过亿