参考
https://blog.51cto.com/shoufu/2469397
https://www.lmonkey.com/t/8ZBW0AxEP

一、Blackbox_exporter应用场景

blackbox_exporter是Prometheus 官方提供的 exporter 之一,可以提供 http、dns、tcp、icmp 的监控数据采集。

  • HTTP 测试
    定义 Request Header 信息
    判断 Http status / Http Respones Header / Http Body 内容
  • TCP 测试
    业务组件端口状态监听
    应用层协议定义与监听
  • ICMP 测试
    主机探活机制
  • POST 测试
    接口联通性
    SSL 证书过期时间

二、prometheus安装

https://prometheus.io/download/
1、下载安装包然后解压

cd /usr/local
wget https://github.com/prometheus/prometheus/releases/download/v2.8.1/prometheus-2.8.1.linux-amd64.tar.gz
tar -zxvf prometheus-2.8.1.linux-amd64.tar.gz
mv prometheus-2.8.1.linux-amd64/ prometheus
cd prometheus/
./prometheus --versionuseradd  -s /sbin/nologin -M prometheus
chown -R prometheus:prometheus /usr/local/prometheus
yum -y install ntp
ntpdate ntp1.aliyun.com

2、创建Systemd服务启动prometheus

vim /etc/systemd/system/prometheus.service
在service文件里面,定义了启动的命令,可以定义数据存储路径,否则默认会在prometheus二进制的目录的data下。

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl start prometheus
systemctl status prometheus
systemctl enable prometheus

3、打开prometheus的web页面
http://10.0.0.102:9090/graph

三、Blackbox_exporter 安装

https://github.com/prometheus/blackbox_exporter/releases
1、安装解压

wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.16.0/blackbox_exporter-0.16.0.linux-amd64.tar.gz
tar xf blackbox_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/
ln -s /usr/local/blackbox_exporter-0.16.0.linux-amd64/ /usr/local/blackbox_exporter

2、使用systemd进行管理blackbox_exporter服务
vim /etc/systemd/system/blackbox_exporter.service

[Unit]
Description=blackbox_exporter
After=network.target[Service]
Restart=on-failure
ExecStart=/usr/local/blackbox_exporter/blackbox_exporter --web.listen-address=0.0.0.0:9115 --config.file=/usr/local/blackbox_exporter/blackbox.yml[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start blackbox_exporter.service
systemctl enable blackbox_exporter.service

四、blackbox exporter 配置文件解读

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

1、默认配置

modules:http_2xx:prober: httphttp_post_2xx:prober: httphttp:method: POSTtcp_connect:prober: tcppop3s_banner:prober: tcptcp:query_response:- expect: "^+OK"tls: truetls_config:insecure_skip_verify: falsessh_banner:prober: tcptcp:query_response:- expect: "^SSH-2.0-"irc_banner:prober: tcptcp:query_response:- send: "NICK prober"- send: "USER prober prober prober :prober"- expect: "PING :([^ ]+)"send: "PONG ${1}"- expect: "^:[^ ]+ 001"icmp:prober: icmp

运行 blackbox exporter 时,需要用户提供探针的配置信息,这些配置信息可能是一些自定义的 HTTP 头信息,也可能是探测时需要的一些 TSL(秘钥证书) 配置,也可能是探针本身的验证行为.在 blackbox exporter 每一个探针配置称为一个 module,并且以 YAML 配置文件的形式提供给 blackbox exporter

每一个 module 主要包含以下配置内容:探针类型(prober),验证访问超时时间(timeout),以及当前探针的具体配置项

# 探针类型: http https tcp dns icmp
prober: <prober_string>   #必选# 超时时间:
[timeout: <duration>] #默认单位秒# 探针的详细配置,最多只能配置其中一个
[ http: <http_probe> ]
[ tcp: <tcp_probe> ]
[ dns: <dns_probe> ]
[ icmp: <icmp_probe> ]

2、<http_probe>可配置参数

# 此探针接受的状态代码。 默认为2xx。[ valid_status_codes: <int>, ... | default = 2xx ]# 此探针接受的 HTTP 版本.[ valid_http_versions: <string>, ... ]#探针将使用的HTTP方法。[ method: <string> | default = "GET" ]# 为探针设置的HTTP标头。headers:[ <string>: <string> ... ]# 探针是否将遵循任何重定向[ no_follow_redirects: <boolean> | default = false ]# 如果存在SSL,则探测失败。[ fail_if_ssl: <boolean> | default = false ]# 如果不存在SSL,则探测失败。[ fail_if_not_ssl: <boolean> | default = false ]# 如果响应主体与正则表达式匹配,则探测失败。fail_if_body_matches_regexp:[ - <regex>, ... ]# 如果响应主体与正则表达式不匹配,则探测失败。fail_if_body_not_matches_regexp:[ - <regex>, ... ]# 如果响应头与正则表达式匹配,则探测失败。 对于具有多个值的标头,如果*至少一个*匹配,则失败。fail_if_header_matches:[ - <http_header_match_spec>, ... ]# 如果响应头与正则表达式不匹配,则探测失败。 对于具有多个值的标头,如果* none *不匹配,则失败。fail_if_header_not_matches:[ - <http_header_match_spec>, ... ]# HTTP探针的TLS协议的配置。tls_config:[ <tls_config> ]# 目标的HTTP基本身份验证凭据。basic_auth:[ username: <string> ][ password: <secret> ]# 目标的承载令牌。[ bearer_token: <secret> ]# 目标的承载令牌文件[ bearer_token_file: <filename> ]# 用于连接到目标的HTTP代理服务器。[ proxy_url: <string> ]# HTTP探针的IP协议(ip4,ip6)[ preferred_ip_protocol: <string> | default = "ip6" ][ ip_protocol_fallback: <boolean> | default = true ]# 探针中使用的HTTP请求的主体。body: [ <string> ]###################################################################
<http_header_match_spec>header: <string>,regexp: <regex>,[ allow_missing: <boolean> | default = false ]

五、分别介绍几种应用场景

1、HTTP

(1) blackbox 配置

/usr/local/blackbox_exporter/blackbox.yml

modules:http_2xx:prober: httphttp:method: GEThttp_post_2xx:prober: httphttp:method: POST

(2) prometheus配置

/usr/local/prometheus/prometheus.yml

  - job_name: 'blackbox_http'scrape_interval: 30smetrics_path: /probeparams:module: [http_2xx]static_configs:- targets:- https://www.baidu.com- https://www.lkcoffee.comrelabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__#blackbox exporter 所在节点replacement: 10.0.0.103:9115

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

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

(3)访问prometheus页面

http://10.0.0.102:9090/targets

#查询数据
{job="blackbox_http"}

(4)自定义 HTTP 请求

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

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

http_post_2xx:prober: httptimeout: 5shttp:method: POSTheaders:Content-Type: application/jsonbody: '{}'

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

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

对于使用了Bear Token的服务也可以通过bearer_token配置项直接指定令牌字符串,或者通过bearer_token_file指定令牌文件。
对于一些启用了HTTPS的服务,但是需要自定义证书的服务,可以通过tls_config指定相关的证书信息:

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

(5)自定义探针行为

在默认情况下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 gauge
probe_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"

2、ping 检测

可以通过 ping(icmp)检测服务器的存活,在 prometheus 配置文件中配置使用 ping module:

  icmp:prober: icmp

与 prometheus 集成

  - job_name: 'blackbox-ping'metrics_path: /probeparams:modelus: [icmp]static_configs:- targets:- 223.5.5.5lables:instance: aliyun- targets:- 47.92.229.67lables:instance: zsfrelabel_configs:- source_labels: [__address__]target_label: __param_target- source_labels: [__param_target]target_label: instance- target_label: __address__replacement: 192.168.111.65:9115

Prometheus监控 Blackbox_exporter黑盒监测相关推荐

  1. Prometheus 监控之 Blackbox_exporter黑盒监测 [icmp、tcp、http(get\post)、dns、ssl证书过期时间]

    Blackbox_exporter 主动监测主机与服务状态 Prometheus 官方提供的 exporter 之一,可以提供 http.dns.tcp.icmp 的监控数据采集 官方github: ...

  2. Prometheus监控实战之Blackbox_exporter黑盒监测

    1 Blackbox_exporter应用场景 blackbox_exporter是Prometheus官方提供的exporter之一,可以提供HTTP.HTTPS.DNS.TCP以及ICMP的方式对 ...

  3. Blackbox_exporter黑盒监测

    一.概述 blackbox_exporter是Prometheus 官方提供的 exporter 之一,可以提供 http.dns.tcp.icmp 的监控数据采集. Blackbox_exporte ...

  4. Prometheus 监控服务端口、网站状态等(黑盒监测)

    1.blackbox_exporter介绍 blackbox_exporter是Prometheus 官方提供的 exporter 之一,可以提供 http.dns.tcp.icmp 的监控数据采集. ...

  5. Prometheus黑盒监测之blackbox_exporter

    1 什么是黑盒监控 我们监控主机的资源用量.容器的运行状态.数据库中间件的运行数据.这些都是支持业务和服务的基础设施,通过白盒能够了解其内部的实际运行状态,通过对监控指标的观察能够预判可能出现的问题, ...

  6. Prometheus Operator + blackbox_exporter 监控Web页面

    背景 目前生产环境使用Zabbix自带的web监控模块对所有子优鸟页面进行监控,由于目前Zabbix服务器为单节点,经常出现取不到web监控数据的情况.现将web监控迁移到Prometheus上. 但 ...

  7. 【Prometheus 】 Blackbox_exporter 指标 probe_http_duration_seconds

    Prometheus 之 Blackbox_exporter 黑盒测试 probe_http_duration_seconds phase 值的解析 - resolve:DNS解析持续时间 - con ...

  8. 云计算监控—Prometheus监控系统(文末赠书)

    陈金窗 刘政委 张其栋 郑少斌 读完需要 20 分钟 速读仅需 7 分钟 本文摘自于<Prometheus 监控技术与实战>一书,从云计算时代的业务特点出发,探讨了云计算监控的目标和挑战, ...

  9. prometheus监控-1

    1.prometheus安装 1.1 prometheus安装 tar -xzvf sotf/prometheus-2.35.0.linux-amd64.tar.gz -C ./app/ 1.2 pr ...

  10. Prometheus 监控详解

    文章目录 一.常用监控系统介绍 1. Cacti 2. Nagios 3. Zabbix 4. Prometheus 5. Open-falcon 二.运维监控平台设计思路 三.Prometheus ...

最新文章

  1. Python3.7实现socket多线程通信
  2. C语言编写2048小游戏
  3. 【maven install报错】Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war
  4. jquery获取java对象的属性_jQuery - 获得内容和属性
  5. 代码传奇丨美女黑客张婉桥的“爱丽丝奇遇记”
  6. [RHCSA学习笔记]Autofs实现自动挂载NFS共享
  7. 第五篇:你“ 看不见 ” 的隐式转换
  8. 【NOIP模拟】矩阵
  9. html简单页面实验报告原理,html网页设计实验报告
  10. ISOIEC27000标准族的介绍与进展
  11. python航空订票系统_航空订票系统
  12. IM即时通讯实现的原理
  13. Comsol学习——经典案例:插销支架受力分析
  14. 在树莓派上驱动CSI摄像头
  15. php 中 normdist,excel中的normdist
  16. pytorch学习第三天: 阿里云物联网平台使用
  17. oracle替换图片颜色,PS人物抠图、衣服颜色更换及相片增加清晰度方法
  18. ug12在win8计算机名错,Win8安装UG9.0时出错提示“UGII_TMP_DIR 被设为一个有无效(非ASCII)字符的目录”怎么办...
  19. 蓝桥杯——单片机设计与开发初学者必备资料
  20. 完全二叉树的节点个数

热门文章

  1. 信息搜集工具:Maltego
  2. 什么是 URL Schema
  3. Java 8 Nashorn 教程
  4. AUTOSAR E2E SecOC Comparison
  5. cortex a7 a53_镜头测试:蔡司红T28/2.8+索尼微单A7实拍北京景山公园
  6. ASIC 芯片 PVT
  7. 最受家长们欢迎的10本家教好书
  8. C++PrimerPlus 第七章 函数-C++的编程模块-7.3 函数和数组
  9. 普加项目管理甘特图使用--安装部署
  10. python 实现QQ邮箱发送邮件