官方下载地址
blackbox-exporter是Prometheus官方提供的一个黑盒测试的解决方案,可用于以下使用场景:
1、安装blackbox-exporter
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.22.0/blackbox_exporter-0.22.0.linux-amd64.tar.gz
tar zxf blackbox_exporter-0.22.0.linux-amd64.tar.gz -C /usr/local
cd /usr/local
mv blackbox_exporter-0.22.0.linux-amd64 blackbox_exporter
2、添加systemd管理
cat >> /lib/systemd/system/blackbox-exporter.service << EOF
[Unit]
Description=Prometheus 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
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
3、启动验证
systemctl restart blackbox-exporter.service
systemctl status blackbox-exporter.service
netstat -anput |grep 9115
1、编辑prometheus配置文件
编辑 prometheus.yml 配置文件中添加需要监控的IP+TCP端口,我这里以监控80端口为例:
- job_name: "Nginx端口"
metrics_path: /probe # 定义指标接口
params: # 指定查询参数,在Prometheus向target发送Get请求获取指标数据时,会传递到url上
module: [tcp_connect]
static_configs:
- targets: ["127.0.0.1:80"]
labels: # 自定义标签,附加在target上
blackbox: 'Nginx端口'
relabel_configs:
- source_labels: [__address__] # 将标签__address__的值赋值给__param_target标签,以__param开头的标签也会作为查询参数传递Prometheus的Get请求,作用和上面的params配置类似
target_label: __param_target
- source_labels: [__param_target]
target_label: instance # 将标签__param_target的值赋值给instance标签
- target_label: __address__
replacement: 16.32.15.115:9115 # 此处修改地址为blackbox-exporter所部署服务器地址
2、添加完成配置文件后使用命令测试格式上是否有问题
cd /usr/local/prometheus/
./promtool check config prometheus.yml
3、重启Prometheus
systemctl restart prometheus.service
4、Prometheus WEB端验证是否收集到数据
Prometheus WEB端中输入probe_success
状态1
为UP 0
为DOWN 如下图:
1、编辑prometheus配置文件
编辑 prometheus.yml 配置文件中添加需要监控URL,我这里以https://www.baidu.com/index.html
为例
- job_name: "百度"
scrape_interval: 15s
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets: ['https://www.baidu.com/index.html']
labels:
blackbox: '百度主页'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 16.32.15.115:9115
2、添加完成配置文件后使用命令测试格式上是否有问题
cd /usr/local/prometheus/
./promtool check config prometheus.yml
3、重启Prometheus
systemctl restart prometheus.service
4、Prometheus WEB端验证是否收集到数据
Prometheus WEB端中输入probe_success
状态1
为UP 0
为DOWN 如下图:
1、模板ID:9965
2、模板ID:13659