目录
3. 作业 job 和实列 targets/instance
4. PrometheusQL(数据查询语言也是时序数据库使用语言)
一、prometheus 概述
1.简介
Prometheus 仅用键值方式存储时序式的聚合数据,不支持文本信息,其中的 “键” 称为指标(metric),通常意味着CPU速率、内存使用率或分区空闲比例等
同一指标可能适配到多个目标或设备、因而它使用 “标签”作为元数据,从而为metric添加更多的信息描述维度
Prometheus 每一份样本数据都包含了:
① 时序列标识:key+lables
② 当前时间序列的样本值value
③ 这些标签可以作为过滤器进行指标过滤及聚合运算,如何从上万的数据过滤出关键有限的时间序列,同时从有限的时间序列在特定范围的样本那就需要手动编写出时间序列的样本表达式来过滤出我们想要的样本数据
2. 指标类型
默认都是以双精度浮点型数据(服务器端无数据量类型数据)
① counter:计数器单调递增
② gauge:仪表盘:有起伏特征的
③ histogram:直方图
在一段时间范围内对数据采样的相关结果,并记入配置的bucket中,他可以存储更多的数据,包括样本值分布在每个bucket的数量,从而prometheus就可以使用内置函数进行计算:
计算样本平均值:以值得综合除以值的数量
计算样本分位值:分位数有助于了解符合特定标准的数据个数,例如评估响应时间超过1秒的请求比例,若超过20侧则进行告警等
④ summary:摘要,histogram的扩展类型,它是直接由监控端自行聚合计算出分位数,同时将计算结果响应给prometheus server的样本采集请求,因而,其分位数计算是由监控端完成
3. 作业 job 和实列 targets/instance
① job:能够接收 prometheus server 数据 scrape ,如 “mysql nodes” “mysql master slave”
② targets:每一个可以被监控的系统,成为 targets 多个相同的 targets 的集合(类)称为 job
③ instance:实例与 targets 类似
与 target 相比,instance 更趋近于一个具体可以提供监控数据的实例,而 targets 则更像一个对象、目标性质
4. PrometheusQL(数据查询语言也是时序数据库使用语言)
支持两种向量,同时内置提供了一组用于数据处理的函数
① 即时向量:最近以此时间戳上跟踪的数据指标,表示的是一个时间刻度;
即时向量选择器:返回0个1个或者多个时间序列上在给定时间戳上的各自的一个样本,该样本成为即时样本
② 时间范围向量:指定时间范围内所有时间戳上的数据指标,表示的是一组时间区间;
范围向量选择器:返回0个1个或多个时间序列上在给定时间范围内的各自的一组样本(范围向量选择器无法用于绘图)
二、部署 Prometheus
1.准备工作
服务器分配主机名 | 地址 | 安装包 |
---|---|---|
prometheus | 192.168.150.10 | prometheus-2.27.1.linux-amd64.tar.gz |
server1 | 192.168.150.100 | node_exporter-1.1.2.linux-amd64.tar.gz |
server2 | 192.168.150.200 | node_exporter-1.1.2.linux-amd64.tar.gz |
2.关闭防火墙机制
[root@prometheus ~]# systemctl stop firewalld
[root@prometheus ~]# systemctl disable firewalld
[root@prometheus ~]# setenforce 0
[root@prometheus ~]# vim /etc/resolv.conf
[root@prometheus ~]# ntpdate ntp1.aliyun.com #时间同步需要做,不然会报错
3.解包并启动服务
[root@prometheus ~]# rz -E #上传prometheus相关软件包
rz waiting to receive.
[root@prometheus ~]# ls
prometheus-2.27.1.linux-amd64.tar.gz ...
[root@prometheus ~]# tar zxvf prometheus-2.27.1.linux-amd64.tar.gz -C /usr/local/
[root@prometheus ~]# cd /usr/local/prometheus-2.27.1.linux-amd64/
[root@localhost prometheus-2.27.1.linux-amd64]# ./prometheus
[root@prometheus ~]# netstat -antp | grep 9090 #另开一个终端查看
tcp6 0 0 :::9090 :::* LISTEN 76461/./prometheus
tcp6 0 0 ::1:9090 ::1:40538 ESTABLISHED 76461/./prometheus
tcp6 0 0 ::1:40538 ::1:9090 ESTABLISHED 76461/./prometheus
4.部署监控其他节点
主配置文件解析
[root@prometheus ~]# cd prometheus-2.27.1.linux-amd64/
[root@prometheus ~]# vim prometheus.yml
my global config
global: #全局组件
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. #每隔多久抓取一次指标,不设置默认1分钟
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
#内置告警规则的评估周期
#scrape_timeout is set to the global default (10s).
# Alertmanager configuration #对接的altermanager(第三方告警模块)
alerting:
alertmanagers:
- static_configs:
- targets:
- alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files: #告警规则;告警规则可以使用yml规则去书写
- "first_rules.yml"
- "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs: #数据采集模块
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. ##对于所抓取的指标数据采集的来源在意job_name来定义
- job_name: 'prometheus' #对于指标需要打上的标签,对于PrometheusSQL(查询语句)的标签:比如prometheus{target='values'}
# metrics_path defaults to '/metrics' #收集数据的路径;展示使用metrics模式
# scheme defaults to 'http'. #默认抓取的方式是http
static_configs: #对于Prometheus的静态配置监听端口具体数据收集的位置 默认的端口9090
- targets: ['localhost:9090']
5.server 节点配置
这里配置了node1、node2两个节点
[root@node1 ~]# rz -E #上传node_exporter相关软件包
rz waiting to receive.
[root@node1 ~]# tar zxvf node_exporter-1.1.2.linux-amd64.tar.gz
[root@node1 ~]# cd node_exporter-1.1.2.linux-amd64
[root@node1 ~]# cp node_exporter /usr/local/bin
[root@node2 ~]# rz -E #上传node_exporter相关软件包
rz waiting to receive.
[root@node2 ~]# tar zxvf node_exporter-1.1.2.linux-amd64.tar.gz
[root@node2 ~]# cd node_exporter-1.1.2.linux-amd64
[root@node2 ~]# cp node_exporter /usr/local/bin
6.加入其他节点监控端
需要在192.168.150.10 prometheus服务端停止prometheus修改配置文件添加静态targets才能使得node1、node2节点加入
[root@prometheus prometheus-2.27.1.linux-amd64]# vim prometheus.yml
...
- job_name: 'nodes'
static_configs:
- targets:
- 192.168.150.10:9100 #第一个是主机的IP地址,没有配置node_exporter所以浏览器会down
- 192.168.150.100:9100 #另外两个配置了node_exporter,会有记录
- 192.168.150.200:9100
三、表达式浏览器
1.表达式浏览器常规使用
在prometheusUI控制台上可以进行数据过滤
命令解释:
irate:速率计算函数(灵敏度非常高)
node_cpu_seconds_total:node节点CPU使用总量(指标)
mode=“idle” 空闲指标(标签)
5m:过去的5分钟内,所有CPU空闲数的样本值,每个数值做速率运算
{mode=“idle”} :整体称为标签过滤器
avg:平均值
avg (irate(node_cpu_seconds_total{mode=‘idle’}[5m]):可以理解为CPU空闲量的百分比
by (instance):表示的是所有节点
四、部署service discovery服务
1.Prometheus的服务发现
(1)基于文件的服务发现;(定义一组资源“子”配置文件yaml格式 里面只存方需要采集的targets 信息,此种方式可以被pro动态获取到,而不需要重启)
(2)基于DNS的服务发现;(SRV形式)
(3)基于API的服务发现:Kubernetes、Consul、Azure、重新标记;target重新打标、metric重新打标
(4)基于K8S的服务发现
2.prometheus 服务发现机制
(1)Prometheus Server的数据抓取工作于Pull模型,因而,它必需要事先知道各Target
的位置,然后才能从相应的Exporter或Instrumentation中抓取数据;
(2)对于小型的系统环境来说,通过static_configs指定各Target便能解决问题,这也是
最简单的配置方法;每个Targets用一个网络端点(ip:port)进行标识;
(3)对于中大型的系统环境或具有较强动态性的云计算环境来说,静态配置显然难以适用;
因此,Prometheus为此专门设计了一组服务发现机制,以便于能够基于服务注册中心(服务总线)自动发现、检测、分类可被监控的各Target,以及更新发生了变动的Target指标抓取的生命周期;
(4)在每个scrape_interval期间,Prometheus都会检查执行的作业(Job);这些作业首先会根据
Job上指定的发现配置生成target列表,此即服务发现过程;服务发现会返回一个Target列表,其中包含一组称为元数据的标签,这些标签都以" meta_"为前缀;
(5)服务发现还会根据目标配置来设置其它标签,这些标签带有"“前缀和后缀,b包括"scheme”、" address"和" metrics path_",分别保存有target支持使用协议(http或https,默认为http) 、 target的地址及指标的URI路径(默认为/metrics) ;
(6)若URI路径中存在任何参数,则它们的前缀会设置为" param"这些目标列表和标签会返回给Prometheus,其中的一些标签也可以配置中被覆盖;
(7)配置标签会在抓取的生命周期中被重复利用以生成其他标签,例如,指标上的instance标签的默认值就来自于address标签的值;
(8)对于发现的各目标,Prometheus提供了可以重新标记(relabel)目标的机会,它定义在job配置段的relabel_config配置中,常用于实现如下功能。
3.静态配置发现
修改prometheus服务器上的配置为文件,指定targets的端口上面配置过
- job_name: 'nodes'
static_configs:
- targets:
- 192.168.150.100:9100
- 192.168.150.200:9100
4.动态发现
(1)动态发现(在prometheus节点上)
基于文件的服务发现仅仅略优于静态配置的服务发现方式,它不依赖于任何平台或第三方服务,因而也是最为简单和通用的实现方式。prometheus server定期从文件中加载target信息(pro-server pull指标发现机制-job_name
获取我要pul1的对象target)文件可以只用json和yaml格式,它含有定义的target列表,以及可选的标签信息,以下第一配置,能够将prometheus默认的静态配置转换为基于文件的服务发现时所需的配置;(rometheus会周期性的读取、重载此文件中的配置,从而达到动态发现、更新的操作)
先杀死prometheus的服务进程,再进行配置
node节点也要开启服务
[root@prometheus prometheus-2.27.1.linux-amd64]# netstat -natp | grep 9090
tcp6 0 0 :::9090 :::* LISTEN 77014/./prometheus
tcp6 0 0 ::1:47648 ::1:9090 ESTABLISHED 77014/./prometheus
tcp6 0 0 ::1:9090 ::1:47648 ESTABLISHED 77014/./prometheus
[root@prometheus prometheus-2.27.1.linux-amd64]# kill -9 77014
[root@prometheus files_sd]# cat prometheus.yml
# my global config
# Author: MageEdu <mage@magedu.com>
# Repo: http://gitlab.magedu.com/MageEdu/prometheus-configs/
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
file_sd_configs:
- files:
- targets/prometheus_*.yaml
refresh_interval: 2m
# All nodes
- job_name: 'nodes'
file_sd_configs:
- files:
- targets/nodes_*.yaml
refresh_interval: 2m
[root@prometheus targets]# cat nodes_centos.yaml
- targets:
- 192.168.150.100:9100
- 192.168.150.200:9100
labels:
app: node-exporter
job: node
[root@prometheus targets]# cat prometheus_server.yaml
- targets:
- 192.168.150.10:9090
labels:
app: prometheus
job: prometheus
(2)指定yml文件启动(在prometheus节点上)
[root@prometheus prometheus-2.27.1.linux-amd64]# ./prometheus --config.file=./file_sd/prometheus.yml
5.文件发现的作用
如果增加node或者prometheus服务端节点只需更改nodes_centos.yaml prometheus_server.yaml两个文件添加地址就行,不需要停止服务。
6.基于DNS自动发现
基于DNS的服务发现针对一组DNS域名进行定期查询,以发现待监控的目标,查询时使用的DNS服务器的客户端/etc/resolv.conf文件指定
该发现机制依赖于A、AAAA和SRV资源记录,且仅支持该类方法,尚不支持RFC6763中的高级DNS发现方式。
PS:
SRV: SRV记录的作用是指明某域名下提供的服务。
实例:_http._tcp.example.com.SRV 10 5 80. www.example.comSRV
后面项目的含义:
10 -优先级,类似MX记录
5 -权重
80-端口
www.example.com -实际提供服务的主机名。同时SRV可以指定在端口上对应哪个service
prometheus 基于DNS的服务中的SRV记录,让prometheus发现指定target上对应的端口对应的是exporter或instrumentation。
prometheus以pull形式 以http/https方式,拉取的对应被监控端的指标数据。
7.基于consul发现
相关概念:一款基于golang开发的开源工具,主要面向分布式,服务化的系统提供服务注册、服务发现和配置管理的功能,提供服务注册/发现、健康检查、Key/Value存储、多数据中心和分布式一致性保证等功能。
原理:通过定义json文件将可以进行数据采集的服务注册到consul中,用于自动发现同时使用prometheus做为client端获取consul上注册的服务,从而进行获取数据。
(1)准备条件
主机名 | 地址 | 服务部署 |
prometheus | 192.168.150.10 | prometheus,consul |
node1 | 192.168.150.100 | node_exporter |
node2 | 192.168.150.200 | node_exporter |
(2)安装服务
安装consul_1.9.0版本,并启动开发者模式(consul开发者模式,可以快速开启单节点的consul服务,具有完整功能,方便开发测试)
[root@prometheus ~]# cd /opt/
[root@prometheus opt]# rz -E
rz waiting to receive.
[root@prometheus opt]# ls
consul_1.9.0_linux_amd64.zip rh
[root@prometheus opt]# unzip consul_1.9.0_linux_amd64.zip -d /usr/local/bin/
[root@prometheus opt]# mkdir -p /consul/data
[root@prometheus opt]# mkdir /etc/consul
[root@prometheus opt]# cd /etc/consul/
[root@prometheus consul]# consul agent -dev -ui -data-dir=/consul/data/ -config-dir=/etc/consul/ -client=0.0.0.0
注:
agent -dev:运行开发模式
agent -server:运行server模式
-ui:ui界面
-data-dir:数据位置
/etc/consul:可以以文件形式定义各个services的配置,也可以基于api接口直接配置
-client:监听地址
(3)编辑/etc/consul目录下的prometheus-servers.json配置文件
[root@prometheus ~]# cd /etc/consul/
[root@prometheus consul]# vim prometheus-servers.json #在consul目录下创建一个prometheus-servers.json文件
{
"services": [
{
"id": "prometheus-server-node01",
"name": "prom-server-node01",
"address": "192.168.150.10",
"port": 9090,
"tags": ["prometheus"],
"checks": [{
"http": "http://192.168.150.10:9090/metrics",
"interval": "5s"
}]
}
]
}
[root@prometheus ~]# consul reload #重新加载文件
(4)创建consul自动发现的prometheus.yml文件
[root@prometheus ~]# cd /usr/local/prometheus-2.27.1.linux-amd64/
[root@prometheus prometheus-2.27.1.linux-amd64]# mkdir consul_sd
[root@prometheus prometheus-2.27.1.linux-amd64]# cd consul_sd/
[root@prometheus consul_sd]# rz -E #上传一个prometheus.yml的配置文件
[root@prometheus consul_sd]# ls
prometheus.yml
[root@prometheus consul_sd]# prometheus.yml
...
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
consul_sd_configs:
- server: "192.168.150.10:8500"
tags:
- "prometheus"
refresh_interval: 2m
# All nodes
- job_name: 'nodes'
consul_sd_configs:
- server: "192.168.150.10:8500"
tags:
- "nodes"
refresh_interval: 2m
[root@prometheus prometheus-2.27.1.linux-amd64]# ./prometheus --config.file=./consul_sd/prometheus.yml
(5)注册其他node节点
在Prometheus 的/etc/consul/目录下编辑nodes.json文件
[root@prometheus ~]# cd /etc/consul/
[root@prometheus consul]# vim nodes.json
{
"services": [
{
"id": "node_exporter-node01",
"name": "node01",
"address": "192.168.150.100",
"port": 9100,
"tags": ["nodes"],
"checks": [{
"http": "http://192.168.150.100:9100/metrics",
"interval": "5s"
}]
},
{
"id": "node_exporter-node02",
"name": "node02",
"address": "192.168.150.200",
"port": 9100,
"tags": ["nodes"],
"checks": [{
"http": "http://192.168.150.200:9100/metrics",
"interval": "5s"
}]
}
]
}
[root@prometheus consul]# consul reload #重新加载配置文件
注:此时两个node节点要运行起来
五、Grafana部署及模板展示
grafana是一款基于go语言开发的通用可视化工具,支持从不同的数据源加载并展示数据,可作为其数据源的部分储存系统如下所示:
TSDB:Prometheus、IfluxDB、OpenTSDB和Graphit
日志和文档存储:Loki和ElasitchSearch
分布式请求跟踪:Zipkin、Jaeger和Tenpo
SQL DB:Mysql、PostgreSQL和Microsoft SQL server
grafana基础默认监听于TCP协议的3000端口,支持集成其他认证服务,且能够通过/metrics输出内建指标;
数据源(Data Source):提供用于展示的数据的储存系统
仪表盘(Dashboard):组织和管理数据的可视化面板(Panel)
团队和用户:提供了面向企业组织层级的管理能力
1.Grafan部署步骤
[root@prometheus ~]# ls
grafana-7.3.6-1.x86_64.rpm
[root@prometheus ~]# yum install grafana-7.3.6-1.x86_64.rpm -y
[root@prometheus ~]# systemctl start grafana-server.service
[root@prometheus ~]# ss -antp | grep grafana-server
ESTAB 0 0 192.168.150.10:42106 34.120.177.193:443 users:(("grafana-server",pid=82053,fd=9))
LISTEN 0 128 :::3000 :::* users:(("grafana-server",pid=82053,fd=8))
2.浏览器登录grafana
默认用户名:admin;密码:admin
grafana默认配置文件目录 /etc/grafana/grafana.in
六、打标签
1.重新打标定义
对target重新打标是在数据抓取之前动态重写target标签的强大工具,在每个数据抓取配置中,可以定义多个relabel步骤,它们将按照定义的顺序依次执行。
对于发现的每个target,Prometheus默认会执行如下操作:
job的标签设定为其所属的job name的值;
_address_标签的值为该target的套接字地址":"
instance标签的值为_address_的值;
_scheme_标签的值为抓取该target上指标时使用的协议(http或https) ;
_metrics _path_标签的值为抓取该target上的指标时使用URI路径,默认为/metrics;
param_标签的值为传递的URL参数中第一个名称为的参数的值
重新标记期间,还可以使用该target上以"meta "开头的元标签;
各服务发现机制为其target添加的元标签会有所不同;
重新标记完成后,该target上以"_"开头的所有标签都会被移除;
若在relabel的过程中需要临时存储标签值,则要使用tmp标签名称为前缀进行保存,以避免同Prometheus的内建标签冲突。
2.relabel config
修改标签值、增加删除标签,通过调用不同参数实现自己的需求
source_labels:指定调用哪些已有的标签(可引用多个)在重新打标的时候会将这些标签对应的值给引用/提取并连接起来,例如: cpu指标{host=node1; host=node2 }
target_labels:与source_labels组合使用,可以指定使用哪个已有标签赋值给指定的新标签
separator:对应源标签的标签值使用什么连接符,默认为" ;"
regex:对于源标签,使用哪个正则表达式对源标签进行模式匹配、匹配后可以将对应的结果复制到target上,赋值方式:(引用所有正则表达式的内容进行赋值)
modulus:hash算法函数
replacement:把目标标签的值改为新的值
action <relabel_action>:表示重新打标的方式是什么,以及要实现什么功能
七、prometheus告警功能
Prometheus对指标的收集、存储同告警能力分属于Prometheus Server和AlertManager(通用的组件)两个独立的组件,前者仅负责基于"告警规则"生成告警通知,具体的告警操作则由后者完成。Alertmanager负责处理由客户端发来的告警通知客户端通常是Prometheus server,但它也支持接收来自其它工具的告警。Alertmanager对告警通知进行分组、去重后,根据路由规则将其路由到不同的receiver,如Email、短信或PagerDuty等。目前Alertmanager还不支持钉钉,那用户完全可以通过Webhook与钉钉机器人进行集成,从而通过钉钉接收告警信息。同时AltManager还提供了静默和告警抑制机制来对告警通知行为进行优化
PS:webhook是一个APr概念, webhoo是一种web回调或者http的push APT.Webhook作为一个轻量的事件处理应用
1.告警功能概述
prometheus对指标的收集、存储与告警能力分属于Prometheus serve和alertmanager两个独立的组件,pro-server只负责通过"告警规则"生成告警通知,具体告警操作是由alertmmanager完成。
2.告警规则
是由PromQL编写的布尔值表达式使用>< =与一个常用量值,比如80%进行比较,其返回值为true或false,prometheus-server对抓取到的指标序列与告警规则中做为比较的Prometheus匹配,则会把此样本值抓取过来作比较,若返回值为true则认为指标异常,不能满足false,则为正常值以上表达式为告警规则表达式。比如:筛选一个指标数据cpu使用率<0%系统异常
3.通知告警信息
一旦条件表达式为true了就会触发通知信息,送给altermanager,由alter借助特定服务的API或者访问入口,将此信息发出去一般称为告警媒介,也可以借助邮件进行告警SMTP。
4.prometheus监控系统的告警逻辑
route:告警路由,分组、分类分发告警消息给不同渠道
prometheus通过alter-rule规则,生成告警通知给altermanager,altermanager会生成本地的告警路由表(第一路由默认称为根路由,所有的告警信息都需要一个根路由,没有一个匹配项,则需要设置一个默认路由)为实现将特定的信息发送给特定的用户。
例如:按消息级别来看,严重、中等、普通级别,红色报警、蓝色报警,应用发送方
按分组:业务运维、系统运维、基础设施运维、k8s运维
5.告警功能
除了基本的告警通知能力外,Altermanager还支持对告警进行去重、分组、抑制。
6.静默、抑制、分组等功能
分组 (Grouping):将相似告警合并为单个告警通知的机制,在系统因大面积故障而触发告警潮时,分组机制能避免用户被大量的告警噪声淹没,进而导致关键信息的隐没;
抑制(Inhibition):系统中某个组件或服务故障而触发告警通知后,那些依赖于该组件或服务的其它组件或服务可能也会因此而触发告警,抑制便是避免类似的级联告警的一种特性,从而让用户能将精力集中于真正的故障所在;
静默(silent):是指在一个特定的时间窗口内,即便接收到告警通知,Alertmanager也不会真正向用户发送告警信息的行为;通常,在系统例行维护期间,需要激活告警系统的静默特性;
路由(route):用于配置Alertmanager如何处理传入的特定类型的告警通知,其基本逻辑是根据路由匹配规则的匹配结果来确定处理当前告警通知的路径和行为。
八、部署告警对接邮箱
在prometheus-server端定义告警规则,指定alertmanager的位置,将告警信息发送给alert处理。
1.安装alertmanager
[root@prometheus ~]# cd /opt/
[root@prometheus opt]# ls
alertmanager-0.21.0.linux-amd64.tar.gz
[root@prometheus opt]# tar zxf alertmanager-0.21.0.linux-amd64.tar.gz -C /usr/local/
[root@prometheus opt]# ln -s /usr/local/alertmanager-0.21.0.linux-amd64/ /usr/local/alertmanager
[root@prometheus opt]ln -s /usr/local/prometheus-2.27.1.linux-amd64/prometheus /usr/local/bin/
2.查看配置文件
[root@prometheus local]# cat /usr/local/alertmanager-0.21.0.linux-amd64/alertmanager.yml
global:
resolve_timeout: 5m
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 1h
receiver: 'web.hook'
receivers:
- name: 'web.hook'
webhook_configs:
- url: 'http://127.0.0.1:5001/'
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
3.修改alertmanager的配置文件
[root@prometheus ~]# cd /usr/local/alertmanager
[root@prometheus alertmanager]# cp alertmanager.yml alertmanager.yml.bak
global:
resolve_timeout: 5m
smtp_from: 12345678@qq.com
smtp_auth_username: 12345678@qq.com
smtp_auth_password: 邮箱开启第三方服务的授权码,不是邮箱密码!!!!!(会随机生成)
smtp_require_tls: false
smtp_smarthost: 'smtp.qq.com:465'
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 1h
receiver: 'email-test'
receivers:
- name: 'email-test'
email_configs:
- to: 12345678@qq.com
send_resolved: true
[root@prometheus ~]# cd /usr/local/alertmanager
[root@prometheus alertmanager]# ./alertmanager #启动服务
4.配置相关文件
[root@prometheus ~]# cd /usr/local/alertmanager
[root@prometheus alertmanager]# mkdir alert-config
[root@prometheus alertmanager]# cd alert-config/
[root@prometheus alert-config]# mkdir -p alert_rules targets
[root@prometheus alert-config]# ls
alert_rules targets
[root@prometheus alert-config]# cd alert_rules/
[root@prometheus alert_rules]# vim instance_down.yaml
groups:
- name: AllInstances
rules:
- alert: InstanceDown
# Condition for alerting
expr: up == 0
for: 20s
# Annotation - additional informational labels to store more information
annotations:
title: 'Instance down'
description: Instance has been down for more than 20 seconds.'
# Labels - additional labels to be attached to the alert
labels:
severity: 'critical'
[root@prometheus ~]# cd /usr/local/alertmanager/alert-config/targets/
[root@prometheus targets]# vim alertmanagers.yaml
- targets:
- 192.168.150.10:9093
labels:
app: alertmanager
[root@prometheus targets]# vim nodes-linux.yaml
- targets:
- 192.168.150.100:9100
- 192.168.150.200:9100
labels:
app: node-exporter
job: node
[root@prometheus targets]# vim prometheus-servers.yaml
- targets:
- 192.168.150.10:9090
labels:
app: prometheus
job: prometheus
5.配置prometheus启动文件
[root@prometheus ~]# cd /usr/local/alertmanager/alert-config/
[root@prometheus alert-config]# vim prometheus.yml
# my global config
# Author: MageEdu <mage@magedu.com>
# Repo: http://gitlab.magedu.com/MageEdu/prometheus-configs/
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- file_sd_configs:
- files:
- "targets/alertmanagers*.yaml"
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
- "rules/*.yaml"
- "alert_rules/*.yaml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
file_sd_configs:
- files:
- targets/prometheus-*.yaml
refresh_interval: 2m
# All nodes
- job_name: 'nodes'
file_sd_configs:
- files:
- targets/nodes-*.yaml
refresh_interval: 2m
- job_name: 'alertmanagers'
file_sd_configs:
- files:
- targets/alertmanagers*.yaml
refresh_interval: 2m
6.指定文件启动alertmanager
[root@prometheus ~]# cd /usr/local/alertmanager
[root@prometheus alertmanager]# ./alertmanager
7.指定文件启动prometheus
[root@prometheus ~]# cd /usr/local/prometheus-2.27.1.linux-amd64/
[root@prometheus prometheus-2.27.1.linux-amd64]# ./prometheus --config.file=./alert-config/prometheus.yml
浏览器查看状态
8.模拟故障
停掉node1和node2的服务