前言
Blackbox_exporter 是 Prometheus 官方提供的一个 exporter,可以监控 HTTP,HTTPS,DNS,TCP ,ICMP和gRPC等目标实例,从而实现对被监控节点进行监控和数据采集。
部署Blackbox_exporter
下载安装二进制文件:
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.22.0/blackbox_exporter-0.22.0.linux-amd64.tar.gz
tar zxvf blackbox_exporter-0.22.0.linux-amd64.tar.gz
cp -r blackbox_exporter-0.22.0.linux-amd64 /apps/blackbox_exporter
创建blackbox_exporter服务:
vim /etc/systemd/system/blackbox-exporter.service
[Unit]
Description=Prometheus Blackbox Exporter
After=network.target
[Service]
Type=simple
User=root
Group=root
ExecStart=/apps/blackbox_exporter/blackbox_exporter \
--config.file=/apps/blackbox_exporter/blackbox.yml \
--web.listen-address=:9115
Restart=on-failure
[Install]
WantedBy=multi-user.target
开机自启动:
systemctl daemon-reload
systemctl enable --now blackbox-exporter.service
systemctl status blackbox-exporter.service
访问blackbox_exporter服务,在服务定义了端口9115:
配置Prometheus
blackbox_exporter的监控,包括URL,ICMP,TCP等。都是先在prometheus设置监控内容,再转发到blackbox_exporter的地址,最后得出监控结果。
设置完prometheus配置文件后,可以使用检查工具:
# 检查工具路径,命令,配置文件
sudo /apps/prometheus/promtool check config /apps/prometheus/prometheus.yml
# 正确提示
Checking /apps/prometheus/prometheus.yml
SUCCESS: /apps/prometheus/prometheus.yml is valid prometheus config file syntax
# 错误提示
Checking /apps/prometheus/prometheus.yml
FAILED: parsing YAML file /apps/prometheus/prometheus.yml: yaml: line 71: did not find expected '-' indicator
URL监控
在prometheus服务器添加url监控工作,末尾指定blackbox_exporter地址:
vim /apps/prometheus/prometheus.yml
- job_name: 'blackbox_url'
metrics_path: /probe # Probe is the path
params:
module: [http_2xx] # Look for a HTTP 200 response.
static_configs:
- targets:
- http://www.baidu.com # Target to probe with http.
- https://www.baidu.com # Target to probe with https.
- http://example.com:8080 # Target to probe with http on port 8080.
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.100.185:9115 # The blackbox exporter's real hostname:port.
在blackbox_exporter服务器查看最近URL探测:
在prometheus查看URL工作采集状态:
ICMP监控
ICMP就是Ping使用的协议,可以探测IP是否在线:
vim /apps/prometheus/prometheus.yml
- job_name: 'ping_status'
metrics_path: /probe
params:
module: [icmp]
static_configs:
- targets:
- 192.168.100.191
- 192.168.100.196
labels:
instance: 'ping_status'
group: 'icmp'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: ip
- target_label: __address__
replacement: 192.168.100.185:9115
在prometheus查看ICMP工作采集状态:
在blackbox_exporter服务器查看最近ICMP探测:
Port监控
Port端口用于检测服务器端口是否开启,服务是否可用:
vim /apps/prometheus/prometheus.yml
- job_name: 'port_status'
metrics_path: /probe
params:
module: [tcp_connect]
static_configs:
- targets: ['192.168.100.194:22', '192.168.100.195:3306','192.168.100.196:9100']
labels:
instance: 'port_status'
group: 'port'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: ip
- target_label: __address__
replacement: 192.168.100.185:9115
在prometheus查看Port工作采集状态:
在blackbox_exporter服务器查看最近Port探测:
Grafana模板
可在官网查找blackbox_exporter相关的模板:
https://grafana.com/grafana/dashboards/
推荐模板ID:9965, 13587
TLS证书过期监控
在总览面板中,有一项证书(天)选项记录了网站的Https证书剩余天数,可为延期做准备。
下列例子中,https://www.baidu.com的证书还剩282天到期: