Prometheus黑盒监控
本文原文介绍出处:运维老兵Alex
什么是黑盒探针?
- “黑盒监控”:它指在程序外部通过探针的方法模拟访问,获取程序的响应指标来监控程序状态,如请求处理时间、状态码等。在实际生产环境中, 往往会将两种监控方式混合使用,以实现对应用的全方位监控。
项目地址:
https://github.com/prometheus/blackbox_exporter/releases
本篇主要介绍Prometheus如何通过Blackbox_exporter 的探针检测功能,来实现对应用的外部监控。
一)、Blackbox_exporter 介绍:
- Blackbox exporter使用go语言开发,它支持通过HTTP、HTTPS、DNS、TCP和ICMP的方式来探测目标端点。它的使用方式与其他exporter不太一样,在Blackbox exporter的内部需要定义好检查的模块,如HTTP检测模块。Prometheus将目标和模块名做为URL的参数传递给Blackbox exporter,再由exporter 生成对应的探测请求到目标端点,根据返回的请求状态生成对应的时间序列指标,并传递给Prometheus。
-
HTTP 测试:
- 定义Request Header 信息(获取请求消息数据)
- 判断Http status(http状态) / Http Respones Header(http 头部响应) / Http Body 内容(二进制流)
-
TCP 测试:
-
ICMP 测试:
-
POST 测试:
- SSL 证书过期时间:
二)、部署Blackbox_exporter
Github下载地址
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.19.0/blackbox_exporter-0.19.0.linux-amd64.tar.gz
tar xf blackbox_exporter-0.19.0.linux-amd64.tar.gz -C /opt
ln -s /opt/blackbox_exporter-0.19.0.linux-amd64 /opt/blackbox_exporter
[root@ops blackbox_exporter]#vim /usr/lib/systemd/system/blackbox_exporter.service
//写入启动文件
[Unit]
Description=blackbox_exporter Exporter
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/opt/blackbox_exporter/blackbox_exporter --config.file=/opt/blackbox_exporter/blackbox.yml
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=blackbox_exporter
[Install]
WantedBy=default.target
[root@ops blackbox_exporter]#systemctl restart blackbox_exporter.service
[root@ops blackbox_exporter]#systemctl status blackbox_exporter.service
[root@ops blackbox_exporter]#systemctl enable blackbox_exporter.service
[root@ops blackbox_exporter]# ss -lnt //查看端口9115是否启动
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 16384 [::]:9115 [::]:*
LISTEN 0 16384 [::]:9090 [::]:*
LISTEN 0 16384 [::]:9093 [::]:*
LISTEN 0 16384 [::]:9094 [::]:*
LISTEN 0 80 [::]:3306 [::]:*
http://locakhost:9115
三)、配置Prometheus:
#http 配置
- job_name: 'blackbox-http' # 标签名
metrics_path: /probe
params:
module: [http_2xx]
static_configs: #静态组
- targets:
- https://www.163.com
- https://www.baidu.com
- http://192.168.117.128:3000
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target] #源字段
target_label: instance
- target_label: __address__ #新生成的键
replacement: 127.0.0.1:9115 #匹配的值
#icmp 配置
- job_name: 'blackbox-icmp'
metrics_path: /probe
params:
module: [icmp]
static_configs:
- targets:
- 192.168.117.128
- 192.168.168.129
labels:
instance: node_status
group: 'node'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
replacement: 192.168.117.128:9115
#端口状态
- job_name: 'blackbox-port'
metrics_path: /probe
params:
module: [tcp_connect]
static_configs:
- targets:
- 192.168.117.128:3000
labels:
instance: 'port_status'
group: 'tcp'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 192.168.117.128:9115
添加完成后重启prometheus
四)、浏览器进行验证:
http://locakhost:9090/targets
五)、Grafana里进行验证: