3.Prometheus 监控技术与实践 --- Exporter

第3章 Exporter 
	在 prometheus 中,Exporter 是重要的组成部分,在实际监控样本数据的收集是由 Exporter 完成的,prometheus 服务器只需要定时从
这些 Exporter 提供的http服务获取监控数据即可。

3.1 概述 
	Exporter 本质上是将收集的数据转化为对应的文本格式,并提供 http 接口,供 prometheus 定期采集数据。

	3.1.1 Exporter类型 
		通常来说可以将 Exporter 分为两类:
			1.直接采集型
				这类 Exporter 内置了相应的应用程序,用于向 prometheus 直接提供 Target 数据支持。这样设计的好处是,可以更好的监控
			各自系统的内部运行状态,同时也更适合更多自定义监控指标的项目实施。

			2.间接采集型
				原始监控目标并不直接支持 prometheus,需要我们使用 prometheus 提供的 Client Labrary 编写该监控目标的监控采集程序,
			用户可以将该程序独立运行,去获取指定的各类监控数据值。

	3.1.2 文本数据格式 
		在 prometheus 监控环境中,所有返回监控样本数据的 Exporter 程序,均需要遵守 prometheus 规范,即基于文本的数据格式,其特点是
	具有更好的跨平台和可读写。

		访问 http://localhost:9090/metrics,可以看到 Exporter 收集的数据值转化成文本内容展示。prometheus 基于文本的格式是面向行的。
	以 # 开头的,通常是注释内容。这些样本数据说明如下:
		1.以 # HELP 开头的行,表示 mitric 的帮助与说明注释,可以包含当前监控指标名称和对应的说明信息
		2.以 # TYPE 开始的行,表示定义 metric 类型,可以包含当前监控指标名称和类型,类型有 Counter,Gauge,Histogram,Summary和 Untyped。
		3.其他非 # 开头的行,即监控样本数据
		4.其他一般性注释,方便阅读使用,会被 prometheus 忽略

		每一行的样本需要满足一下格式:
			metric_name [ label_name = "label_value", ... ] value [timestamp]

	3.1.3 获取Exporter 
		https://prometheus.io/docs/instrumenting/exporters/
		https://github.com/prometheus/

3.2 主机监控 
	对于主机中各项性能指标的监控,不同的内核会展示不同的监控指标(metric)。

	3.2.1 Linux主机监控 
		1.下载部署
		git clone https://github.com/prometheus/node_exporter.git
		cd node_exporter
		make
		./node_exporter <flags>

		2.与 Prometheus 集成
		可以在 prometheus 主机目录中,找到主配置文件,使用其中的静态配置功能 static_configs 采集 node_exporter 提供的数据。

		prometheus.yml 配置说明:	
			1.global
				在prometheus安装后,官方会提供一个默认的prometheus.yml文件,其中包含global的默认配置内容,主要有以下4个参数:
					a) scrape_interval,每次数据采集的时间间隔,默认为1s
					b) scrape_timeout,采集请求超时时间,默认为10s
					c) evaluation_interval,执行rules的频率,默认为1s
					d) external_labels,与外部系统通信时添加到任意时间序列或警告用的外部标签

			2.scrape_configs
				scrape_configs 主要用于配置被采集数据节点操作,每一个采集配置主要有以下几个参数:
					1.job_name
					2.scrape_interval
					3.scrape_timeout
					4.metrics_path
					5.honor_labels
					6.scheme
					7.params
					8.relabel_configs
					9.metric_relabel_configs
					10.sample_limit

			3.node_exporter 任务
				添加内容如下:
				vim prometheus.yml

				scrape_configs:
					- job_name: 'prometheus'
					static_configs:
					- targets: ['192.168.24.17:9090']
					- job_name: 'node_exporter'
					static_configs:
					- targets: ['192.168.24.17:9100']

				配置后,重启 prometheus 或者进行动态热加载操作。在浏览器中输入 http://192.168.24.17:9090,点击 "Status"中的
			"Targets"。

			//自定义配置文件
			docker run --name prometheus -p 9090:9090 -v /Users/weijianhua/Sites/config_file/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

		3.metrics 查看
			Node exporter 服务正常运行的时候,访问 http://192.168.24.17:9100/metrics,可以直接查看当前被监控主机的样本信息。

			1.cpu数据采集
				对cpu数据进行采集的主要监控指标是 node_cpu_seconds_total。可以用如下 PromQL 表达式进行查询:
				avg without(cpu,mode) (rate(node_cpu_seconds_total {mode="idle"} [1m] ))
				表达式计算每核cpu每秒的空闲时间,然后对主机上的所有cpu求平均值。

			2.内存信息采集	
				node_memory_xxx 开始

			3.磁盘数据采集
				node_disk_xxx 开始

			4.文件系统采集
				node_filesystem_xxx 开始

			5.网络采集
				node_network_xxx 开始

	3.2.2 Windows主机监控 

3.3 数据库监控 
	3.3.1 MySQL Server exporter 
		由于mysql 不提供任何端点。其工作原理是使用底层应用程序理解的语言,从目标应用程序中提取监控指标,然后通过rest公开这些指标。

		1.下载 mysqld_exporter 
		https://prometheus.io/download/#mysqld_exporter

		2.创建mysql授权用户
			连接到mysql服务器,创建一个用户,例如 "mysqld_expoter"。该用户需要拥有 process,select,replication client 授权,且为了避免有些高负载
		mysql 服务器过载,为用户设置最大连接数设置。
			create user 'mysqld_exporter'@'localhost' identified by '123456';
			grant PROCESS,REPLICATION CLIENT,SELECT ON *.* TO 'mysqld_exporter'@'localhost';
			flush PRIVILEGES;
			select Host,User from mysql.user; //核查用户是否已经创建完成

		3.配置数据库认证	
			mysqld_exporter 需要连接数据库服务器的用户名和密码,这里可以通过2种方式进行:
			1.使用环境变量	。创建一个名为"DATA_SOURCE_NAME"的环境变量,格式如下:
				export DATA_SOURCE_NAME = 'user:password@(hostname:3306)/'

			2.使用配置文件。创建一个名为 '.mysqld_exporter.cnf',并在该文件中输入用户名和密码。格式如下:
				user=用户名
				password=密码

				我们选第二种:
				vim .mysqld_exporter.cnf
				user=用户名
				password=密码

		4.启动 mysqld_exporter
			./mysqld_exporter --config.my-conf=".mysqld_exporter.conf" 

			默认监听 9104 端口。

			访问 http://localhost:9104/metrics

		5.与 prometheus 集成
			vim prometheus.yml 
			- job_name : 'mysqld_exporter'
				scrape_interval: 10s
				static_configs:
				-targets:['192.168.24.61:9104']

		6.metrics 查看
			mysql数据库的性能状态监控内容非常多,但至少应该关注4个选项:吞吐量,执行性能,连接情况和缓冲池使用情况。

			1.查询吞吐量
				show global status like 'Questions';
			2.查询执行性能
				show global status like 'Slow_queries';
			3.连接情况
				show variables like 'max_connections';
				show global status like 'Threads_connected';
			4.缓冲池使用情况
				show global status like 'Innodb_buffer_pool_reads';




	3.3.2 Redis exporter 
		1.下载redis_exporter
		https://github.com/oliver006/redis_exporter/releases/tag/v1.24.0

		查看
		http://192.168.31.139:9121/metrics

		2.与 prometheus 集成
		vim prometheus.yml
		  - job_name: 'redis_exporter'
		    static_configs:
		      - targets: ['192.168.31.139:9121']

		3.


3.4 Nginx监控 
	prometheus 官方提供了2个nginx使用的第三方 Exporter: Nginx metric library 和 nginx-vts-exporter。

	1.下载
	https://github.com/vozlt/nginx-module-vts/releases

	https://github.com/hnlq715/nginx-vts-exporter/releases

	2.启动
	./nginx-vts-exporter -nginx.scrape_uri http://localhost/status/format/json

	3.与 prometheus 集成
	vim prometheus.yml
	  - job_name: 'nginx_exporter'
	    static_configs:
	      - targets: ['192.168.31.139:9913']

3.5 Prometheus之黑盒监控 
	前面我们对 Exporter 的使用称为 "白盒监控",即需要把对应的 Exporter 程序安装到被监控的目标主机上,从而实现对主机各种资源极其状态的数据采集工作。但是由于
某些情况下,不是所有的 exporter 都能部署到被监控的主机环境中,最典型的就是监控全国网络质量的稳定性,通常用Ping操作,对选取的节点进行icmp操作,此时不可能在他人
应用环境中部署exporter程序。针对这样的场景,prometheus 社区提供了黑盒解决方案,Black Exporter 无需安装在被监控的目标环境中,用户只需要将其安装在与prometheus
和被监控目标互通的环境中,通过http,https,dns,tcp,icmp等方式对网络进行探测和监控,还可以探测 ssl 证书过期时间。
	
	3.5.1 软件安装与部署 
		1.下载 blackbox_exporter :
		https://prometheus.io/download/#blackbox_exporter

		默认监听 9115 端口。

		访问 http://192.168.31.139:9115

	3.5.2 配置文件 
		默认配置文件为 blackbox.yml

		modules:
			http_2xx:       #定义模块名称: http_2xx
				prober:http #定义探测类型: http
			http_post_2xx:  #模块为 http post 功能探测
				prober:http 
				http:
					method:POST
			tcp_connect:    #模块为 tcp 功能探测
				prober:tcp 
			...
			tcmp:			#模块为 icmp 功能探测
				prober:icmp

		1.http 探测
			检查http状态,可以使用http探测器。它可以生成http请求使用,如get或post方法,配置操作中可以定义请求超时时间,可以使用正则表达式进行相关匹配。

			vim blackbox.yml
			modules:
				http_2xx:
					prober:http
					timeout:8s
					http:
						valid_status_codes:[]  #留空,默认为 2xx状态码,如果想自定义 ['200','205']
						method:POST
			proferred_ip_protocol:"ipv4"  #强制使用ipv4
				ip_protocol_fallback: false  #ipv4失败,不回退到 ipv6

			配置完成后,通过 curl -XPOST http://192.168.31.139/-/reload 热加载。

		2.与 prometheus 集成
		- job_name: 'blackbox_http'
			metrics_path: /probe
			params:
				module: [http_2xx]
			static_configs:
				- targets:
					- www.12306.cn
					- www.baidu.cn
			relabel_configs:
				- source_lables: [__address__]
					target_label: __param_target
				- source_labels: [__param_target]
					target_label: instance
				- target_label: __address__
					replacement: localhost:9115

		配置文件中定义了一个名为 blackbox_http 的采集任务,通过 params 声明指定使用 blackbox.yml 文件中的模块 http_2xx,完成对指定探测目标(target)
	的探测。那么 prometheus 是如何与 Blackbox Exporter 进行关联集成的呢?这就用到了 relabel 机制,relabel_configs 覆盖目标的 __address__ 标签
	以指定导出器的主机名,进而简化了 prometheus 任务配置内容。在采集样本数据之前通过 relabel_configs 对采集任务进行动态设置过程如下:
		1.第一个重新标记(relabel)通过将 __address__ 标签(即当前目标的地址)写入 __param_target 标签来创建参数
		2.第二个 relabel 将获取 __param_target 的值,并覆写到 instance 标签中
		3.覆写 target 实例的 __address__ 标签值为 BlockBox Exporter 实例的访问地址,示例中向 192.168.31.139:9115 发送请求获取实例的 metrics 信息。

		通过curl模拟请求,可以看到输出 Metrics 信息:
		curl "http:///192.168.31.139:9115/probe?module=http_2xx&target=www.12306.cn"

		url地址中使用的探针是通过其中的 module 参数来指定的,而target 参数用来指定探测目标,探针所探测的结果通过 Metrics 的形式返回。从样本返回中,用户可以
	获取站点dns解析耗时,响应时间,响应状态码,probe_http_ssl 指示最终冲重定向是否使用ssl和指标 probe_success 为1的成功状态等相关监控指标。

		可以通过 http://localhost:9090/targets 查看。

		如监控目标过多,都写在 prometheus.yml 中,就显得过于臃肿。可以用下面的方法:
		mkdir -p /data/prometheus/targets/probes #创建存放json文件的目录
		vim /data/prometheus/targets/probes/http_probes.json #创建json文件,内容如下:
[
	{
		"targets":[
			"www.baidu.com",
			"www.12306.com",
			"www.qq.com"
		]
	}
]
		
		vim prometheus.yml
		- job_name: 'blackbox_http'
		  metrics_path: /probe
		  params:
		  	module: [http_2xx]
		  file_sd_configs:
		  	- files:
		  		- '/targets/probes/http_probes.json'
		  		refresh_interval: 5m
		  relabel_configs:
		  	- source_labels: [__address__]
		  	  target_label: __param_target
		  	- source_labels: [__param_target]
		  	  target_label: instance
		  	- target_label: __address__
		  	  replacement: 192.168.31.139:9115

		curl -X POST http://loclahost:9090/-/reload # 热加载配置

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值