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

Prometheus监控实战之Blackbox_exporter黑盒监测

1 Blackbox_exporter应用场景

blackbox_exporter是Prometheus官方提供的exporter之一,可以提供HTTP、HTTPS、DNS、TCP以及ICMP的方式对网络进行探测。

1.1 HTTP 测试

定义 Request Header信息

判断 Http status / Http Respones Header / Http Body内容

1.2 TCP 测试

业务组件端口状态监听

应用层协议定义与监听

1.3 ICMP 测试

主机探活机制

1.4 POST 测试

接口联通性

1.5 SSL证书过期时间

2 Blackbox_exporter安装

下载地址:https://prometheus.io/download/

2.1 下载安装包

# wget ​ ​https://github.com/prometheus/blackbox_exporter/releases/download/v0.22.0/blackbox_exporter-0.22.0.linux-amd64.tar.gz​​

2.2 解压并重命名

# tar xf blackbox_exporter-0.22.0.linux-amd64.tar.gz# mv blackbox_exporter-0.22.0.linux-amd64 /usr/local/blackbox_exporter

2.3 授权

# chown -R root.root /usr/local/blackbox_exporter

2.4 使用systemd进行管理blackbox_exporter服务

# cat >/etc/systemd/system/blackbox_exporter.service <<EOF[Unit]Description=Blackbox ExporterWants=network-online.targetAfter=network-online.target[Service]User=rootExecStart=/usr/local/blackbox_exporter/blackbox_exporter --config.file=/usr/local/blackbox_exporter/blackbox.ymlRestart=on-failureWantedBy=default.targetEOF

2.5 添加开机自启动

# systemctl daemon-reload# systemctl enable blackbox_exporterCreated symlink from /etc/systemd/system/default.target.wants/blackbox_exporter.service to /etc/systemd/system/blackbox_exporter.service.

2.6 启动Blackbox_exporter

# systemctl start blackbox_exporter

3 blackbox.yml配置文件详解

官方解释: ​ ​https://github.com/prometheus/blackbox_exporter/blob/master/CONFIGURATION.md

3.1 默认配置

运行blackbox exporter时,需要用户提供探针的配置信息,这些配置信息可能是以下几种信息:

  • 自定义的HTTP头信息
  • 探测时需要的一些 TSL(秘钥证书) 配置
  • 探针本身的验证行为

在 blackbox exporter每一个探针配置称为一个 module,并且以YAML配置文件的形式提供给blackbox exporter,每一个module主要包含以下配置内容:

  • 探针类型(prober)
  • 验证访问超时时间(timeout)
  • 当前探针的具体配置项

3.1.1 探针类型: http https tcp dns icmp

prober: <prober_string>

必选

3.1.2 超时时间

[timeout: <duration>]

默认单位:秒

3.1.3 探针的详细配置,最多只能配置其中一个

[ http: <http_probe> ][ tcp: <tcp_probe> ][ dns: <dns_probe> ][ icmp: <icmp_probe> ]

3.2 <http_probe>可配置参数

3.2.1 此探针接受的状态代码

[ valid_status_codes: <int>, ... | default = 2xx ]

默认为2xx。

3.2.2 此探针接受的HTTP版本

[ valid_http_versions: <string>, ... ]

3.2.3 探针将使用的HTTP方法

[ method: <string> | default = "GET" ]

3.2.4 为探针设置的HTTP标头

headers:
[ <string>: <string> ... ]

3.2.5 探针是否将遵循任何重定向

[ no_follow_redirects: <boolean> | default = false ]

3.2.6 如果存在SSL,则探测失败

[ fail_if_ssl: <boolean> | default = false ]

3.2.7 如果不存在SSL,则探测失败

[ fail_if_not_ssl: <boolean> | default = false ]

3.2.8 如果响应主体与正则表达式匹配,则探测失败

fail_if_body_matches_regexp:[ - <regex>, ... ]

3.2.9 如果响应主体与正则表达式不匹配,则探测失败

fail_if_body_not_matches_regexp:[ - <regex>, ... ]

3.2.10 如果响应头与正则表达式匹配,则探测失败。对于具有多个值的标头,如果*至少一个*匹配,则失败

fail_if_header_matches:[ - <http_header_match_spec>, ... ]

3.2.11 如果响应头与正则表达式不匹配,则探测失败。对于具有多个值的标头,如果* none *不匹配,则失败

fail_if_header_not_matches:[ - <http_header_match_spec>, ... ]

3.2.12 HTTP探针的TLS协议的配置

tls_config:[ <tls_config> ]

3.2.13 目标的HTTP基本身份验证凭据

basic_auth:[ username: <string> ][ password: <secret> ]

3.2.14 目标的承载令牌

[ bearer_token: <secret> ]

3.2.15 目标的承载令牌文件

[ bearer_token_file: <filename> ]

3.2.16 用于连接到目标的HTTP代理服务器。

[ proxy_url: <string> ]

3.2.17 HTTP探针的IP协议(ip4,ip6)

[ preferred_ip_protocol: <string> | default = "ip6" ]
[ ip_protocol_fallback: <boolean> | default = true ]

3.2.18 探针中使用的HTTP请求的主体。

body: [ <string> ]<http_header_match_spec>header: <string>,regexp: <regex>,[ allow_missing: <boolean> | default = false ]

4 常用几种应用场景

4.1 HTTP检测(监控网站状态)

4.1.1 blackbox配置

# vim http_code.yamlmodules:http_2xx:prober: httphttp:method: GEThttp_post_2xx:prober: httphttp:method: POST

4.1.2 Prometheus配置

  - job_name: 'blackbox_http_2xx'metrics_path: /probeparams:module: [http_2xx]static_configs:- targets:- https://www.baidu.com- https://www.yangxingzhen.comrelabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__#blackbox exporter 所在节点replacement: 10.10.50.24:9115

针对每1个探针服务(如http_2xx)定义一个采集任务,并且直接将任务的采集目标定义为我们需要探测的站点。在采集样本数据之前通过relabel_configs对采集任务进行动态设置。

  • 根据Target实例的地址,写入__param_target 标签中,__param_<name>形式的标签表示,在采集任务时会在请求目标地址中添加<name>参数,等同于params的设置;
  • 获取__param_target的值,并覆写到instance标签中;
  • 覆写Target实例的__address__标签值为BlockBox Exporter实例的访问地址。

4.1.3 Prometheus热加载配置文件

# curl -X POST http://10.10.50.24:9090/-/reload

4.1.4 访问blackbox_exporter

浏览器输入http://10.10.50.24:9115

4.1.5 访问Prometheus的UI查看下targets

浏览器输入http://10.10.50.24:9090

 查询数据

4.1.6  定义HTTP请求

HTTP服务通常会以不同的形式对外展现,有些可能就是一些简单的网页,而有些则可能是一些基于REST的API服务。对于不同类型的HTTP的探测需要管理员能够对HTTP探针的行为进行更多的自定义设置,包括:HTTP请求方法、HTTP头信息、请求参数等。对于某些启用了安全认证的服务还需要能够对HTTP探测设置相应的Auth支持。对于HTTPS类型的服务还需要能够对证书进行自定义设置。

如下所示,这里通过method定义了探测时使用的请求方法,对于一些需要请求参数的服务,还可以通过headers定义相关的请求头信息,使用body定义请求内容:

1)blackbox.yaml

# vim /usr/local/blackbox_exporter/blackbox.ymlmodules:http_2xx:prober: httphttp:method: GEThttp_post_2xx:prober: httptimeout: 5shttp:method: POSTheaders:Content-Type: application/jsonbody: '{"user_login": "admin" "user_pass": "Aa123456"}'
# systemctl restart blackbox_exporter 

2)prometheus配置:

# 添加以下内容- job_name: 'blackbox_http_post_2xx'metrics_path: /probeparams:module: [http_post_2xx]static_configs:- targets:- http://10.10.50.24:30080/wp-login.phprelabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__#blackbox exporter 所在节点replacement: 10.10.50.24:9115

3)保存配置并热加载配置文件

# curl -X POST ​ ​http://10.10.50.24:9090/-/reload

4)访问Prometheus的UI查看下targets,浏览器输入http://10.10.50.24:9090

查询数据

5)如果HTTP服务启用了安全认证,Blockbox Exporter内置了对basic_auth的支持,可以直接设置相关的认证信息即可: 

http_basic_auth_example:prober: httptimeout: 5shttp:method: POSTheaders:Host: "login.example.com"basic_auth:username: "username"password: "mysecret"

 6)对于使用了Bear Token的服务也可以通过bearer_token配置项直接指定令牌字符串,或者通过bearer_token_file指定令牌文件。

7)对于一些启用了HTTPS的服务,但是需要自定义证书的服务,可以通过tls_config指定相关的证书信息:

http_custom_ca_example:prober: httphttp:method: GETtls_config:ca_file: "/certs/my_cert.crt"

8)自定义探针

在默认情况下HTTP探针只会对HTTP返回状态码进行校验,如果状态码为2XX(200 <= StatusCode < 300)则表示探测成功,并且探针返回的指标probe_success值为1。

如果用户需要指定HTTP返回状态码,或者对HTTP版本有特殊要求,如下所示,可以使用valid_http_versions和valid_status_codes进行定义:

http_2xx_example:prober: httptimeout: 5shttp:valid_http_versions: ["HTTP/1.1", "HTTP/2"]valid_status_codes: [200,301,302]

默认情况下,Blockbox返回的样本数据中也会包含指标probe_http_ssl,用于表明当前探针是否使用了SSL:

# HELP probe_http_ssl Indicates if SSL was used for the final redirect# TYPE probe_http_ssl gaugeprobe_http_ssl 0

而如果用户对于HTTP服务是否启用SSL有强制的标准。则可以使用fail_if_ssl和fail_if_not_ssl进行配置。fail_if_ssl为true时,表示如果站点启用了SSL则探针失败,反之成功。fail_if_not_ssl刚好相反。

http_2xx_example:prober: httptimeout: 5shttp:valid_status_codes: []method: GETno_follow_redirects: falsefail_if_ssl: falsefail_if_not_ssl: false

除了基于HTTP状态码,HTTP协议版本以及是否启用SSL作为控制探针探测行为成功与否的标准以外,还可以匹配HTTP服务的响应内容。使用fail_if_matches_regexp和fail_if_not_matches_regexp用户可以定义一组正则表达式,用于验证HTTP返回内容是否符合或者不符合正则表达式的内容。

http_2xx_example:prober: httptimeout: 5shttp:method: GETfail_if_matches_regexp:- "Could not connect to database"fail_if_not_matches_regexp:- "Download the latest version here"

4.2 TCP(监控主机端口存活状态)

4.2.1 检测端口是否存活,在blackbox.yml 配置文件中使用的配置是tcp_connect模块

# vim /usr/local/blackbox_exporter/blackbox.yml# 添加以下内容tcp_connect:timeout: 5sprober: tcp

4.2.2 添加Prometheus配置

# vim /usr/local/prometheus/prometheus.yml# 添加以下内容- job_name: 'blackbox_check_ports'metrics_path: /probeparams:module: [tcp_connect]static_configs:- targets:- 10.10.50.24:9115- 10.10.50.24:9090relabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__#blackbox exporter 所在节点replacement: 10.10.50.24:9115

4.2.3 Prometheus热加载配置文件

# curl -X POST ​ ​http://10.10.50.24:9090/-/reload

4.2.4 访问Prometheus的UI查看下targets

浏览器输入http://10.10.50.24:9090

查询数据

4.3 ICMP 测试(主机探活)

4.3.1 通过 ping(icmp) 检测服务器的存活,在blackbox.yml使用的配置是icmp模块

# vim /usr/local/blackbox_exporter/blackbox.yml# 添加以下内容icmp:prober: icmptimeout: 5sicmp:ttl: 5

4.3.2 添加Prometheus配置 

# vim /usr/local/prometheus/prometheus.yml# 添加以下内容- job_name: 'blackbox_check_hosts'metrics_path: /probeparams:module: [icmp]static_configs:- targets:- 10.10.50.24- 10.10.50.23relabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__#blackbox exporter 所在节点replacement: 10.10.50.24:9115

4.3.3 Prometheus热加载配置文件

# curl -X POST ​ ​http://10.10.50.24:9090/-/reload

4.3.4 访问Prometheus的UI查看下targets

浏览器输入http://10.10.50.24:9090

查询数据

4.4 检测SSL证书过期时间

4.4.1 检测SSL证书,在blackbox.yml配置文件中使用的配置是模块

# vim /usr/local/blackbox_exporter/blackbox.yml# 添加以下内容http_2xx:prober: httphttp:method: GETpreferred_ip_protocol: "ipv4"valid_http_versions: ["HTTP/1.1", "HTTP/2"]valid_status_codes: [200,301,302,303]

4.4.2 添加Prometheus配置

 vim /usr/local/prometheus/prometheus.yml# 添加以下内容- job_name: 'blackbox_check_ssl'metrics_path: /probeparams:module: [http_2xx]static_configs:- targets:- https://www.yangxingzhen.com- https://www.i7ti.cnrelabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__#blackbox exporter 所在节点replacement: 10.10.50.24:9115

4.4.3 Prometheus热加载配置文件

# curl -X POST ​ ​http://10.10.50.24:9090/-/reload

4.4.4 访问Prometheus的UI查看下targets

浏览器输入http://10.10.50.24:9090

查询数据

4.4.5 时间戳转换

这里以yangxingzhen.com的SSL证书为例,证书过期时间为2023年10月15日

# date -d "@1697327999"Sun Oct 15 07:59:59 CST 2023

4.5 相关告警规则

4.5.1 主机端口不通

probe_success{job="blackbox_check_ports"} == 0

4.5.2 主机ping不通

probe_success{job="blackbox_check_hosts"} == 0

4.5.3 非200HTTP状态码

probe_http_status_code{job="blackbox_http_2xx"} != 200

4.5.4 SSL证书还有30天过期

probe_ssl_earliest_cert_expiry{job="blackbox_check_ssl"} - time() < 86400 * 30
 

http://www.lryc.cn/news/41840.html

相关文章:

  • 【蓝桥杯集训·每日一题】AcWing 1051. 最大的和
  • 【Unity工具,简单应用】Photon + PUN 2,做一个简单多人在线聊天室
  • 程序员增加收入实战 让小伙伴们都加个鸡腿
  • GPIO四种输入和四种输出模式
  • ChatGPT能够改变时代吗?一点点思考
  • Markdown如何使用详细教程
  • HTML5庆祝生日蛋糕烟花特效
  • 算法套路四——反转链表
  • 多线程 (六) wait和notify
  • React--》状态管理工具—Mobx的讲解与使用
  • 有效的括号长按键入验证外星语词典字符的最短距离用栈实现队列
  • 《前端开发者的进阶之路》
  • 为什么说网络安全是风口行业?是IT行业最后的红利?
  • 使用shell 脚本,批量解压一批zip文件,解压后的文件放在以原zip文件名前10个字符的文件夹中的例子
  • 01 | Msyql系统架构
  • Linux命令---设备管理
  • 前端入门:HTML5+CSS3+JAAVASCRIPT
  • 【头歌实验】课外作业一:开通ECS及使用Linux命令
  • CMSIS-RTOS2 RTX5移植到GD32L233
  • [网络原理] 网络中的基本概念
  • BeanPostProcessor原理分析
  • 人工智能和网络安全,应该如何选择?
  • Flink预加载分区维表,实时更新配置信息
  • 大数据现在找工作难么
  • 【Linux】学会这些基本指令来上手Linux吧
  • 【沐风老师】3DMAX交通流插件TrafficFlow使用方法详解
  • c#实现视频的批量剪辑
  • 小白怎么系统的自学计算机科学和黑客技术?
  • scheduler 的使用实验对比和总结(PyTorch)
  • vue2 虚拟列表(优化版)