黑盒监控blackbox_exporter

前边介绍有很多exporter可以直接将metrics暴露给Prometheus进行监控,这些称为“白盒监控”,那些exporter无法监控到的指标呢?或者未暴露Metrics给Prometheus的一些服务怎么办?这时就要用到 blackbox_exporte “黑盒监控”。
blackbox_exporte支持用户通过:HTTP、HTTPS、DNS、TCP和ICMP的方式对网络进行探测,还可以探测SSL证书过期时间。

部署及使用blackbox_exporter

部署blackbox_exporter

这里以linux二进制部署为例:
  1. 下载安装包
curl -LO https://github.com/prometheus/blackbox_exporter/releases/download/v0.22.0/blackbox_exporter-0.22.0.linux-amd64.tar.gz
  1. 展开程序包:
tar xf blackbox_exporter-0.22.0.linux-amd64.tar.gz -C /usr/local/
ln -sv /usr/local/blackbox_exporter-0.22.0.linux-amd64 /usr/local/blackbox_exporter
  1. 创建用户,或prometheus用户已经存在,可略过该步骤:
useradd -r prometheus
  1. 创建Systemd Unitfile,保存于/usr/lib/systemd/system/blackbox_exporter.service文件中:
[Unit]
Description=blackbox_exporter
After=network.target [Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/blackbox_exporter/blackbox_exporter \
--config.file=/usr/local/blackbox_exporter/blackbox.yml \
--web.listen-address=:9115
Restart=on-failure [Install]
WantedBy=multi-user.target
  1. 启动服务:
systemctl daemon-reload
systemctl start blackbox_exporter.service
systemctl enable blackbox_exporter.service
  1. 验证监听的端口,并测试访问其暴露的指标
ss -tnlp | grep '9115'
curl localhost:9115/metrics
随后即可访问Blackbox Exporter的Web UI,其使用的URL如下,其中的<host_ip>要替换为节点的实际地址:http://<host_ip>:9115/

icmp监控,监控主机存活状态

prometheus 添加相关监控,Blackbox 使用默认配置启动即可
vi /usr/local/prometheus/prometheus.yml
  - job_name: "icmp_ping"
metrics_path: /probe
params:
module: [icmp] # 使用icmp模块
file_sd_configs:
- refresh_interval: 10s #检测时间间隔
files:
- "ping/ping_status*.yml" #具体的配置文件路径
relabel_configs:
- source_labels: [__address__]
regex: (.*)(:80)?
target_label: __param_target
replacement: ${1}
- source_labels: [__param_target]
target_label: instance
- source_labels: [__param_target]
regex: (.*)
target_label: ping
replacement: ${1}
- source_labels: []
regex: .*
target_label: __address__
replacement: 127.0.0.1:9115

这里有很多relabel的操作,下篇博客会详细讲解

创建对应的ping目录
cd /usr/local/prometheus/
mkdir ping
cd ping
vi ping_status.yml
- targets: ['monitor.example.com']
labels:
group: '跳板机'
- targets: ['10.xx.xx.xx','10.xx.xx.xx','10.xx.xx.xx']
labels:
group: 'k8s cluster'
- targets: ['www.baidu.com']
labels:
group: '百度'
配置完成后,可以检查配置文件语法,并让Prometheus重载配置。
./promtool check config prometheus.yml
curl -XPOST monitor.example.com:9090/-/reload
打开Prometheus web UI,可以看到,已经监控到了主机icmp情况:

http监控

编辑prometheus的主配置文件prometheus.yml,添加类似如下内容,即可用户对目标站点的探测。
  # Blackbox Exporter
- job_name: 'http_get_status'
metrics_path: /probe
params:
module: [http_2xx] # Look for a HTTP 200 response.
file_sd_configs:
- refresh_interval: 2m
files:
- "httpget/http_get*.yml" #具体的配置文件
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: "monitor.example.com:9115" # 指向实际的Blackbox exporter.
- target_label: region
replacement: "local"
vi httpget/http_get.yml
static_configs:
- targets:
- "https://monitor.example.com"
- "http://monitor.example.com:8080"
- "www.google.com"
refresh_interval: 2m
重新加载Prometheus
curl -XPOST monitor.example.com:9090/-/reload
展示:
这里要注意,blackbox下并不是prometheus中State状态为UP就认为是正常状态,其实并不然,这里我们随便写一个不存在的域名,例如:http://www.buzhida2222o.com,这里看也是UP状态:
但实际看其的metrics指标并不正常,探活的指标是失败,这里不确定是BUG还是怎样。

tcp端口监控

大体的步骤都是一致的,这里就直接上配置:
- job_name: 'tcp_port_status'
metrics_path: /probe
params:
module: [tcp_connect]
static_configs:
- targets: ['monitor.example.com:80','monitor.example.com:8080','monitor.example.com:443']
labels:
instance: 'port_status'
group: 'tcp'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: monitor.example.com:9115
成功监控:
至此,黑盒监控中常用的一些功能介绍完毕,监控项配置完毕后,可以通过导入dashboard到grafana来更直观的查看监控数据。

自定义blackbox.yml

blackbox的默认监控配置也可以进行自定义修改,例如http GET添加一些headers,设置boby_size_limit值或判断一些response body是否符合预期,还有一些TLS的设置等等,我们可以参考官网文档中给出的example来进行自定义:
# github地址
https://github.com/prometheus/blackbox_exporter # github中blackbox.yml各配置项解析
blackbox_exporter/CONFIGURATION.md at master · prometheus/blackbox_exporter · GitHub # github中example文件
blackbox_exporter/example.yml at master · prometheus/blackbox_exporter · GitHub
这里我们做一个演示https及私有TLS证书的演示,监控证书过期时间。
首先需要更改默认的vim blackbox.yml
modules:
http_2xx:
prober: http
http:
preferred_ip_protocol: "ip4"
valid_http_versions: ["HTTP/1.1", "HTTP/2"]
valid_status_codes: [200,301,302,303]
tls_config:
insecure_skip_verify: true
http_ca_example:
prober: http
http:
method: GET
preferred_ip_protocol: "ip4"
valid_http_versions: ["HTTP/1.1", "HTTP/2"]
fail_if_ssl: false
fail_if_not_ssl: true
tls_config:
insecure_skip_verify: false
ca_file: /usr/local/blackbox_exporter/certs/ca.crt
cert_file: /usr/local/blackbox_exporter/certs/server.crt
key_file: /usr/local/blackbox_exporter/certs/server.key
http_2xx:
在此模块的基础上添加了请求状态码的校验,并设置了tls校验直接跳过,这种就比较省事,可以直接跳过证书的校验。
http_ca_example:
此模块为新增,主要配置了tls证书的一些配置,添加ca、证书和私钥的文件位置,使blackbox请求时带着证书。
配置完成后,重启blackbox服务:
systemctl restart blackbox_exporter.service
然后配置prometheus.yml添加对应模块的使用:
  - job_name: 'http_get_status'
metrics_path: /probe
params:
module: [http_2xx] # Look for a HTTP 200 response.
file_sd_configs:
- refresh_interval: 2m
files:
- "httpget/http_get*.yml" #具体的配置文件
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: "monitor.example.com:9115" # 指向实际的Blackbox exporter.
- target_label: region
replacement: "local" - job_name: 'http_get_ca_status'
metrics_path: /probe
params:
module: [http_ca_example]
file_sd_configs:
- refresh_interval: 2m
files:
- "httpget/http_ca.yml"
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: "monitor.example.com:9115" # 指向实际的Blackbox exporter.
- target_label: region
replacement: "beijing"
在对应的发现文件中,配置主机并重新加载prometheus配置,并查看监控状态:
curl -XPOST monitor.example.com:9090/-/reload
搜索指标
probe_http_duration_seconds{phase="tls"}

证书过期时间已经获取成功,这个时候可以导入ID为13230的Dashboard到Grafana,然后再设置一个告警rule,就可以完成TLS证书过期监控啦。
最终效果:
 
 

Prometheus-2:blackbox_exporter黑盒监控的更多相关文章

  1. Prometheus 监控之 Blackbox_exporter黑盒监测

    Prometheus 监控之 Blackbox_exporter黑盒监测 1.blackbox_exporter概述 1.1 Blackbox_exporter 应用场景 2.blackbox_exp ...

  2. prometheus 配置容器 cadvisor监控节点

    安装cadvisor docker run \ --volume=/:/roofs:ro \ --volume=/var/run:/var/run:rw \ --volume=/sys:/sys:ro ...

  3. 理解OpenShift(7):基于 Prometheus 的集群监控

    理解OpenShift(1):网络之 Router 和 Route 理解OpenShift(2):网络之 DNS(域名服务) 理解OpenShift(3):网络之 SDN 理解OpenShift(4) ...

  4. prometheus + grafana部署RabbitMQ监控

    prometheus + grafana部署RabbitMQ监控 1.grafana导入dashboards https://grafana.com/dashboards/2121   2.expor ...

  5. 基于Prometheus和Grafana的监控平台 - 运维告警

    通过前面几篇文章我们搭建好了监控环境并且监控了服务器.数据库.应用,运维人员可以实时了解当前被监控对象的运行情况,但是他们不可能时时坐在电脑边上盯着DashBoard,这就需要一个告警功能,当服务器或 ...

  6. Centos7.X 搭建Prometheus+node_exporter+Grafana实时监控平台

    Prometheus简介 什么是 Prometheus Prometheus是一个开源监控报警系统和时序列数据库 主要功能 多维数据模型(时序由 metric 名字和 k/v 的 labels 构成) ...

  7. Prometheus+Grafana通过kafka_exporter监控kafka

    Prometheus+Grafana通过kafka_exporter监控kafka 一.暴露 kafka-metric 方式 二.jmx_exporter方式 2.1 下载jmx_prometheus ...

  8. Docker监控平台prometheus和grafana,监控redis,mysql,docker,服务器信息

    Docker监控平台prometheus和grafana,监控redis,mysql,docker,服务器信息 一.通过redis_exporter监控redis 1.1 下载镜像 1.2 运行服务 ...

  9. Grafana+Prometheus 搭建 JuiceFS 可视化监控系统

    作为承载海量数据存储的分布式文件系统,用户通常需要直观地了解整个系统的容量.文件数量.CPU 负载.磁盘 IO.缓存等指标的变化. JuiceFS 没有重复造轮子,而是通过 Prometheus 兼容 ...

  10. 【Prometheus+Grafana系列】监控MySQL服务

    前言 前面的一篇文章已经介绍了 docker-compose 搭建 Prometheus + Grafana 服务.当时实现了监控服务器指标数据,是通过 node_exporter.Prometheu ...

随机推荐

  1. LeeCode 942 增减字符串匹配

    LeeCode 942 题目描述: 由范围 [0,n] 内所有整数组成的 n+1 个整数的排列序列可以表示为长度为 n 的字符串 s ,其中: 如果 perm[i] < perm[i + 1]  ...

  2. LeeCode数组问题(二)

    LeeCode 977:有序数组的平方 题目描述: 给你一个按非递减顺序排列的整数数组nums,返回每个数字的平方组成的新数组,要求也按非递减顺序排序. 标签:数组,首尾指针,最大值优先 时间复杂度: ...

  3. MySQL(八)哈希索引、AVL树、B树与B+树的比较

    Hash索引 简介 ​ 这部分略了 Hash索引效率高,为什么还要设计索引结构为树形结构? Hash索引仅能满足 =.<>和IN查询,如果进行范围查询,哈希的索引会退化成O(n):而树型的 ...

  4. Hooks与事件绑定

    Hooks与事件绑定 在React中,我们经常需要为组件添加事件处理函数,例如处理表单提交.处理点击事件等.通常情况下,我们需要在类组件中使用this关键字来绑定事件处理函数的上下文,以便在函数中使用 ...

  5. 图计算引擎分析--GridGraph

    作者:京东科技 李永萍 GridGraph:Large-Scale Graph Processing on a Single Machine Using 2-Level Hierarchical Pa ...

  6. 3520. 【NOIP2013模拟11.7B组】原根(math)

    题目: 考试想法: 考试的时候觉得这些数学公式太恶心了,所以就直接跳过了. 正解: 直接暴力模拟就可以了. 代码: #include<bits/stdc++.h> using namesp ...

  7. AspectCore和MSDI 实现Name注册以及解析对象

    AspectCore 在注册服务这块比较简单,默认是无法根据Name去注册和解析对象,这边做一下这块的扩展 大致原理是根据自定义Name去生成对应的动态类型,然后使用委托或者对象的方式,进行注册 ti ...

  8. 【C#】图片上传并根据长宽大小进行正方形、长方形及等比缩放。

    #region 正方型裁剪并缩放 /// <summary> /// 正方型裁剪 /// 以图片中心为轴心,截取正方型,然后等比缩放 /// 用于头像处理 /// </summary ...

  9. ChatGPT4通道开放接入基于OPEN AI 平台你的任何APP 可一键接入AI 智能

    你一定很好奇什么是 OPEN AI快速开发平台 顾名思义,开放的OPEN AI平台. 基于这个平台你的上层应用,如何 APP,小程序,H5,WEB, 公众号,任何一切终端都可以轻松接入,AI智能应用. ...

  10. kafka生产者你不得不知的那些事儿

    前言 kafka生产者作为消息发送中很重要的一环,这里面可是大有文章,你知道生产者消息发送的流程吗?知道消息是如何发往哪个分区的吗?如何保证生产者消息的可靠性吗?如何保证消息发送的顺序吗?如果对于这些 ...