云计算、AI、云原生、大数据等一站式技术学习平台

网站首页 > 教程文章 正文

Prometheus+Grafana+BlackBox黑盒监

jxf315 2025-09-28 02:05:26 教程文章 17 ℃

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

Blackbox_exporter 可以对一下场景进行监控:

o HTTP 测试定义 Request Header 信息判断 Http status / Http Respones Header / Http Body 内容

o TCP 测试业务组件端口状态监听应用层协议定义与监听

o ICMP 测试主机探活机制

o POST 测试接口联通性SSL 证书过期时间

在之前的运维中,因为忘了更换证书有些服务挂了一会,虽然很快恢复了但是也让我出一身冷汗。一开始我是用shell写了一个脚本定时执行来监听证书的时间,后来发现blackbox-exporter可以对证书等多中黑盒状态进行探测,所以也应用到了线上,这个场景比较适用免费证书的应用,因为现在你在购买证书的时候一般到期前都会给你收集发短信一类的通知,云场景就更不用说了,部署完了之后会有消息通知的策略设置。

1.安装blackbox_exporter

# 创建文件夹
root@ubuntu2204-98:~# cd /usr/local/prometheus_monitor/
root@ubuntu2204-98:/usr/local/prometheus_monitor# mkdir blackbox_exporter
root@ubuntu2204-98:/usr/local/prometheus_monitor# cd blackbox_exporter
# 创建docker-compose文件,启动
root@ubuntu2204-98:/usr/local/prometheus_monitor/blackbox_exporter# vi docker-compose.yml 
version: '3'
services:
    blackbox_exporter:
        image: prom/blackbox-exporter:master
        ports:
          - 9115:9115
        restart: unless-stopped
root@ubuntu2204-98:/usr/local/prometheus_monitor/blackbox_exporter# docker-compose up -d

2.创建blackbox监控Web列表

root@ubuntu2204-98:/usr/local/prometheus_monitor/blackbox_exporter# cd /usr/local/prometheus_monitor/prometheus
root@ubuntu2204-98:/usr/local/prometheus_monitor/prometheus# mkdir blackbox_monitor
root@ubuntu2204-98:/usr/local/prometheus_monitor/prometheus# cd blackbox_monitor
root@ubuntu2204-98:/usr/local/prometheus_monitor/prometheus/blackbox_monitor# vi blackbox-exporter.yaml
# 定义要监控的网站
- targets:
  - https://gitlab.ccc.top   # Target to probe with http.
  - https://jenkins.ccc.top/login?from=%2F  # Target to probe with https.
  - https://chatnext.ccc.top # Target to probe with http on port 8080.
  - https://drawio.ccc.top # Target to probe with http on path /healthz

3.Prometheus加入blackbox

root@ubuntu2204-98:/usr/local/prometheus_monitor/prometheus/blackbox_monitor# cd ..
root@ubuntu2204-98:/usr/local/prometheus_monitor/prometheus# vi prometheus.yml 

  - job_name: 'blackbox'
    metrics_path: /probe
    params:
      module: [http_2xx]
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.1.98:9115 # The blackbox exporter's real hostname:port.
    static_configs:
    file_sd_configs:
    - files:
      - /etc/prometheus/blackbox_monitor/blackbox-exporter.yaml

4.导入模板

模板id:9965

image-20250831095800345

最近发表
标签列表