前言
blackbox_exporter是Prometheus 官方提供的 exporter 之一,可以提供 http、dns、tcp、icmp 的监控数据采集。
使用场景
HTTP 测试
定义 Request Header 信息
判断 Http status / Http Respones Header / Http Body 内容
TCP 测试
业务组件端口状态监听
应用层协议定义与监听
ICMP 测试
主机探活机制
POST 测试
接口联通性
SSL 证书过期时间
一、安装
1、解压安装包
tar zxf blackbox_exporter-0.16.0.linux-amd64.tar.gz -C /data/
2、创建systemd服务
vim /usr/lib/systemd/system/blackbox_exporter.service
[Unit]
Description=blackbox_exporter
After=network.target
[Service]
User=root
Type=simple
ExecStart=/data/blackbox_exporter-0.16.0.linux-amd64/blackbox_exporter --config.file=/data/blackbox_exporter-0.16.0.linux-amd64/blackbox.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
3、启动
#配置重载
systemctl daemon-reload
#启动
systemctl start blackbox_exporter.service
#开机自启
systemctl enable blackbox_exporter.service
二、使用
1、修改配置文件添加监控项
cd /data/prometheus-2.19.1.linux-amd64/
vim prometheus.yml
#网站监控
- job_name: 'web_status'
metrics_path: /probe
params:
module: [http_2xx] # Look for a HTTP 200 response.
static_configs:
- targets:
- http://192.168.84.128:3000 # Target to probe with http.
- https://baidu.com
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.84.128:9115
#端口监控
- job_name: 'port_status'
metrics_path: /probe
params:
module: [tcp_connect]
static_configs:
- targets:
- 192.168.84.128:80
- 192.168.84.129:80
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.84.128:9115
#主机存活监控
- job_name: 'ping_status'
metrics_path: /probe
params:
module: [icmp]
static_configs:
- targets:
- 192.168.84.128
- 192.168.84.129
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.84.128:9115
2、检查配置文件并重启
./promtool check config prometheus.yml
nohup ./prometheus --config.file=prometheus.yml &
3、检查是否生效
4、配置Grafana展示数据
导入已经画好的dashboard(可以去grafana官网去下载),数据源选择prometheus数据源即可。