Prometheus黑盒测试【blackbox-exporter】
官方下载地址
blackbox-exporter是Prometheus官方提供的一个黑盒测试的解决方案,可用于以下使用场景:
- TCP:端口存活检测
- HTTP/HTTPS:可用性检测
- ICMP:主机存活检测
- TCP:端口存活检测
- DNS:域名解析
1、安装blackbox-exporter
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.22.0/blackbox_exporter-0.22.0.linux-arm64.tar.gz
tar zxf blackbox_exporter-0.22.0.linux-amd64.tar.gz -C /usr/local/src/
cd /usr/local/src/
mv blackbox_exporter-0.22.0.linux-amd64 blackbox_exporter
2、添加system管理
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/src/blackbox_exporter/blackbox_exporter --config.file=/usr/local/src/blackbox_exporter/blackbox.yml --web.listen-address=:9115
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
3、启动验证
systemctl restart blackbox-exporter.service
netstat -anput |grep 9115
4、prometheus.yml中配置监听TCP端口
- job_name: "Nginx端口"
metrics_path: /probe # 定义指标接口
params: # 指定查询参数,在Prometheus向target发送Get请求获取指标数据时,会传递到url上
module: [tcp_connect]
static_configs:
- targets: ["nginx.huayunworld.com:80","nginx.huayunworld.com:443"]
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: 10.9.11.14:9115 # 此处修改地址为blackbox-exporter所部署服务器地址
5、prometheus.yml中配置检测服务接口
- job_name: "全媒体"
scrape_interval: 15s
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets: ['https://IP/api']
labels:
blackbox: '短信接口'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 10.9.11.14:9115 # The blackbox exporter's real hostname:port.
6、验证
prometheus中输入probe_success
状态1
为UP 0
为DOWN 如下图:
7、最后分析几款Grafana模板
模板ID:9965
模板ID:13659
模板ID:12275