Prometheus 整合 AlertManager的教程详解

简介

Alertmanager 主要用于接收 Prometheus 发送的告警信息,它很容易做到告警信息的去重,降噪,分组,策略路由,是一款前卫的告警通知系统。它支持丰富的告警通知渠道,可以将告警信息转发到邮箱、企业微信、钉钉等。这一节讲解利用AlertManager,把接受到的告警信息,转发到邮箱。

实验

准备

启动 http-simulator 度量模拟器:

docker run --name http-simulator -d -p 8080:8080 pierrevincent/prom-http-simulator:0.1

启动 Prometheus,为了方便更新配置,使用挂载配置文件的方式:

docker run --name prometheus -d -p 9090:9090 -v /Users/huanchu/Documents/prometheus-data:/prometheus-data \
    prom/prometheus --web.enable-lifecycle --config.file=/prometheus-data/prometheus.yml

启动添加了参数 --web.enable-lifecycle,让Prometheus支持通过web端点动态更新配置。

访问http://127.0.0.1:9090/targets ,Prometheus 自身的 metrics 和 http-simulator 的 metrics 处于up 状态 ,那么准备工作就做好了。

实验

实验1

告警配置

在prometheus-data文件夹下,创建告警配置文件 simulator_alert_rules.yml:

groups:
- name: simulator-alert-rule
 rules:
 - alert: HttpSimulatorDown
  expr: sum(up{job="http-simulator"}) == 0
  for: 1m
  labels:
   severity: critical

配置文件的意思是 http-simulator 服务up状态为 0 ,并且持续1分钟时,产生告警 ,级别为 “严重的”。

修改prometheus.yml,引用simulator_alert_rules.yml文件,prometheus.yml 内容如下:

global:
 scrape_interval: 5s
 evaluation_interval: 5s
 scrape_timeout: 5s
rule_files:
 - "simulator_alert_rules.yml"
scrape_configs:
 - job_name: 'prometheus'
  static_configs:
  - targets: ['localhost:9090']
 - job_name: 'http-simulator'
  metrics_path: /metrics
  static_configs:
  - targets: ['192.168.43.121:8080']

更新Prometheus配置:

curl -X POST http://localhost:9090/-/reload

访问http://127.0.0.1:9090/config,可以看到已经为更新了配置:

访问http://127.0.0.1:9090/rules,Rules 下出现了新添加的告警规则:

验证

访问http://127.0.0.1:9090/alerts ,Alerts 下 HttpSimulatorDown 为绿色,处于INACTIVE 状态,表示什么都没有发生。

关闭 http-simulator 服务:

docker stop http-simulator

访问http://127.0.0.1:9090/alerts,HttpSimulatorDown 变成黄色,处于 PENDING 状态,表示报警即将被激活。

一分钟后,HttpSimulatorDown 变成红色,处于 FIRING 状态,表示报警已经被激活了。

实验2

告警配置

在simulator_alert_rules.yml文件中增加告警配置:

- alert: ErrorRateHigh
  expr: sum(rate(http_requests_total{job="http-simulator", status="500"}[5m])) / sum(rate(http_requests_total{job="http-simulator"}[5m])) > 0.02
  for: 1m
  labels:
   severity: major
  annotations:
   summary: "High Error Rate detected"
   description: "Error Rate is above 2% (current value is: {{ $value }}"

配置文件的意思是 http-simulator 请求的错误率对2% ,并且持续1分钟时,产生告警 ,级别为 “非常严重的”

更新Prometheus配置:

curl -X POST http://localhost:9090/-/reload

验证

访问http://127.0.0.1:9090/alerts,ErrorRateHigh 为绿色的 INACTIVE 状态。

把 http-simulator 的错误率调到 10%

curl -H 'Content-Type: application/json' -X PUT -d '{"error_rate": 10}' http://localhost:8080/error_rate

稍等一会后,访问http://127.0.0.1:9090/alerts, 可以看到错误率已经大2%,ErrorRateHigh 为红色的 FIRING 状态,报警已经被激活了。

安装和配置AlertManager

通过docker 挂载文件的方式安装AlertManager,在本地创建文件夹 alertmanager-data 文件夹,在其中创建 alertmanager.yml,内容如下:

global:
 smtp_smarthost: 'smtp.163.com:25'
 smtp_from: 'xxxxx@163.com'
 smtp_auth_username: 'xxxxx@163.com'
 smtp_auth_password: 'xxxxx'

route:
 group_interval: 1m  #当第一个报警发送后,等待'group_interval'时间来发送新的一组报警信息
 repeat_interval: 1m  # 如果一个报警信息已经发送成功了,等待'repeat_interval'时间来重新发送他们
 receiver: 'mail-receiver'
receivers:
- name: 'mail-receiver'
 email_configs:
  - to: 'xxxxxx@163.com' 

启动 AlertManager:

docker run --name alertmanager -d -p 9093:9093 -v /Users/huanchu/Documents/alertmanager-data:/alertmanager-data \
    prom/alertmanager --config.file=/alertmanager-data/alertmanager.yml

在Prometheus目录下,修改prometheus.yml配置Alertmanager地址:

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

更新Prometheus配置:

curl -X POST http://localhost:9090/-/reload

访问http://127.0.0.1:9093,访问Alertmanager UI界面,可以看到接收到ErrorRateHigh告警:

邮箱会收到告警信息:

总结

以上所述是小编给大家介绍的Prometheus 整合 AlertManager的教程详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

(0)

相关推荐

  • 利用Prometheus与Grafana对Mysql服务器的性能监控详解

    概述 Prometheus是一个开源的服务监控系统,它通过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上.它提供了一个简单的网页界面.一个功能强大的查询语言以及HTTP接口等等.Prometheus通过安装在远程机器上的exporter来收集监控数据,这里用到了以下两个exporter: node_exporter – 用于机器系统数据 mysqld_exporter – 用于Mysql服务器数据 Grafana是一个开源的功能丰富的数据可视化平台,通常用于时序数据的可视化.它内置了

  • prometheus监控springboot应用简单使用介绍详解

    对于springboot应用,需要以下几个步骤 springboot应用开启endpoint,添加actuator的以来和promethus的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> &

  • 使用Python编写Prometheus监控的方法

    要使用python编写Prometheus监控,需要你先开启Prometheus集群.可以参考//www.jb51.net/article/148895.htm 安装.在python中实现服务器端.在Prometheus中配置请求网址,Prometheus会定期向该网址发起申请获取你想要返回的数据. 使用Python和Flask编写Prometheus监控 Installation pip install flask pip install prometheus_client Metrics P

  • 使用Python和Prometheus跟踪天气的使用方法

    开源监控系统 Prometheus 集成了跟踪多种类型的时间序列数据,但如果没有集成你想要的数据,那么很容易构建一个.一个经常使用的例子使用云端提供商的自定义集成,它使用提供商的 API 抓取特定的指标. 创建自定义 Prometheus 集成以跟踪最大的云端提供商:地球母亲. 开源监控系统 Prometheus 集成了跟踪多种类型的时间序列数据,但如果没有集成你想要的数据,那么很容易构建一个.一个经常使用的例子使用云端提供商的自定义集成,它使用提供商的 API 抓取特定的指标.但是,在这个例子

  • Prometheus 整合 AlertManager的教程详解

    简介 Alertmanager 主要用于接收 Prometheus 发送的告警信息,它很容易做到告警信息的去重,降噪,分组,策略路由,是一款前卫的告警通知系统.它支持丰富的告警通知渠道,可以将告警信息转发到邮箱.企业微信.钉钉等.这一节讲解利用AlertManager,把接受到的告警信息,转发到邮箱. 实验 准备 启动 http-simulator 度量模拟器: docker run --name http-simulator -d -p 8080:8080 pierrevincent/prom

  • SpringBoot整合MybatisPlus的教程详解

    Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 它已经封装好了一些crud方法,对于非常常见的一些sql我们不用写xml了,直接调用这些方法就行,但它也是支持我们自己手动写xml. 帮我们摆脱了用mybatis需要写大量的xml文件的麻烦,非常安逸哦 用过就不想用其他了,太舒服了 好了,我们开始整合整合 新建一个SpringBoot的工程 这里是我整合完一个最终的结构,可以参考一下 <?xml ve

  • SpringBoot整合MyBatis-Plus3.1教程详解

    一.说明 Mybatis-Plus是一个Mybatis框架的增强插件,根据官方描述,MP只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑.并且只需简单配置,即可快速进行 CRUD 操作,从而节省大量时间.代码生成,分页,性能分析等功能一应俱全,最新已经更新到了3.1.1版本了,3.X系列支持lambda语法,让我在写条件构造的时候少了很多的"魔法值",从代码结构上更简洁了. 二.项目环境 MyBatis-Plus版本: 3.1.0 SpringBoot版本:2.1.5 JDK

  • Spring整合MyBatis(Maven+MySQL)图文教程详解

    一. 使用Maven创建一个Web项目 为了完成Spring4.x与MyBatis3.X的整合更加顺利,先回顾在Maven环境下创建Web项目并使用MyBatis3.X,第一.二点内容多数是回顾过去的内容 . 1.2.点击"File"->"New"->"Other"->输入"Maven",新建一个"Maven Project",如下图所示: 1.2.请勾选"Create a si

  • Mac版PhpStorm之XAMPP整合apache服务器配置的图文教程详解

    选择在PhpStorm集成apache服务器,下面是我自己的亲测的步骤. 1.如何修改apache默认端口 xampp apache默认的http访问端口是80 修改完成后在xampp中重启apache. 2.配置流程 打开Settings / Preferences 找到 Build, Execution, Deployment 选项,点击 Deployment 添加如下: 因为我们修改了默认端口80,所以这里一定要指明端口号.目录是/Applications/XAMPP/xamppfiles

  • SpringBoot整合Swagger和Actuator的使用教程详解

    前言 本篇文章主要介绍的是SpringBoot整合Swagger(API文档生成框架)和SpringBoot整合Actuator(项目监控)使用教程. SpringBoot整合Swagger 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. Swagger 介绍 Swagger 是一套基于 OpenAPI 规范构建的开源工具,可以帮助我们设计.构建.记录以及使用 Rest API.Swagger 主要包含了以下三个部分: Swagger Editor:基于浏览器的编辑器,我们

  • es(elasticsearch)整合SpringCloud(SpringBoot)搭建教程详解

    注意:适用于springboot或者springcloud框架 1.首先下载相关文件 2.然后需要去启动相关的启动文件 3.导入相关jar包(如果有相关的依赖包不需要导入)以及配置配置文件,并且写一个dao接口继承一个类,在启动类上标注地址 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> &l

  • tomcat9 下载安装和配置+整合到eclipse的教程详解

    tomcat 官网 tomcat 相当于本地服务器,可以打开网页 下载到设置的位置,到此下载完成. 安装 1.解压下载的安装包 2.环境变量的配置,选择我的电脑,右键依次 属性–>高级–>环境变量–>系统变量,添加对CATALINA_HOME变量 对Path系统变量添加变量值 %CATALINA_HOME%\bin;%CATALINA_HOME%\lib\servlet-api.jar;%CATALINA_HOME%\lib\jsp-api.jar; 3.添加用户,进入D:\tomcat

  • Prometheus的安装和配置教程详解

    1. 从官网选择Prometheus版本进行下载 官网地址>> https://github.com/prometheus/prometheus/releases/ 2. 实验安排 在主机192.168.153.137上安装prometheus监控192.168.153.138上的mysql服务和主机状态 3. 上传软件包到137服务器并配置 3.1 将软件包解压到 /usr/local 目录下 tar xzf prometheus-2.24.1.linux-amd64.tar.gz -C /

随机推荐