一、Blackbox Exporter介绍
1 什么是黑盒监控
我们监控主机的资源用量、容器的运行状态、数据库中间件的运行数据。这些都是支持业务和服务的基础设施,通过白盒能够了解其内部的实际运行状态,通过对监控指标的观察能够预判可能出现的问题,从而对潜在的不确定因素进行优化。
而从完整的监控逻辑的角度,除了大量的应用白盒监控以外,还应该添加适当的黑盒监控。黑盒监控即以用户的身份测试服务的外部可见性,常见的黑盒监控包括HTTP探针、TCP探针等用于检测站点或者服务的可访问性,以及访问效率等。
黑盒监控相较于白盒监控最大的不同在于黑盒监控是以故障为导向当故障发生时,黑盒监控能快速发现故障,而白盒监控则侧重于主动发现或者预测潜在的问题。一个完善的监控目标是要能够从白盒的角度发现潜在问题,能够在黑盒的角度快速发现已经发生的问题。
2 介绍
Blackbox Exporter是Prometheus社区提供的官方黑盒监控解决方案,其允许用户通过:HTTP、HTTPS、DNS、TCP以及ICMP的方式对网络进行探测。
应用场景
HTTP 测试
定义 Request Header 信息 判断 Http status Http Respones Header Http Body 内容
TCP 测试
业务组件端口状态监听 应用层协议定义与监听
ICMP 测试
主机探活机制
POST 测试
接口联通性
SSL 证书过期时间
#########################################
运行Blackbox Exporter时,需要用户提供探针的配置信息,这些配置信息可能是一些自定义的HTTP头信息
也可能是探测时需要的一些TSL配置,也可能是探针本身的验证行为。在Blackbox Exporter
每一个探针配置称为一个module,并且以YAML配置文件的形式提供给Blackbox Exporter。
每一个module主要包含以下配置内容,包括探针类型(prober)、验证访问超时时间(timeout)、
以及当前探针的具体配置项:
prober: <prober_string>
# 超时时间
[ timeout: <duration> ]
# 探针的详细配置,最多只能配置其中的一个
[ http: <http_probe> ]
[ tcp: <tcp_probe> ]
[ dns: <dns_probe> ]
[ icmp: <icmp_probe> ]
下面是一个简化的探针配置文件blockbox.yml,包含两个HTTP探针配置项:
modules:
http_2xx:
prober: http
http:
method: GET
http_post_2xx:
prober: http
http:
method: POST
二、安装blackbox_exporte
# wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.16.0/blackbox_exporter-0.16.0.linux-amd64.tar.gz
# tar xf blackbox_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/
# ln -s /usr/local/blackbox_exporter-0.16.0.linux-amd64/ /usr/local/blackbox_exporter
# 使用systemd管理blackbox_exporter服务
# vim /usr/lib/systemd/system/blackbox_exporter.service
[Unit]
Description=blackbox_exporter
After=network.target
[Service]
User=root
Type=simple
ExecStart=/usr/local/blackbox_exporter/blackbox_exporter --config.file=/usr/local/blackbox_exporter/blackbox.yml
Restart=on-failure
[Install]
WantedBy=multi-user.target
刷新
# systemctl daemon-reload
启动服务
# systemctl start blackbox_exporter
加入开机自启动
# systemctl enable blackbox_exporter
三、配置prometheus
按自己的需求更改(# 网站监控 # ping 检测 # 端口监控)
# cd /usr/local/prometheus
# vim prometheus.yml
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
# 网站监控
- job_name: 'http_status'
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets: ['http://www.baidu.com']
labels:
instance: http_status
group: web
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
replacement: 192.168.31.63:9115
# ping 检测
- job_name: 'ping_status'
metrics_path: /probe
params:
module: [icmp]
static_configs:
- targets: ['192.168.31.62']
labels:
instance: 'ping_status'
group: 'icmp'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
replacement: 192.168.31.63:9115
# 端口监控
- job_name: 'port_status'
metrics_path: /probe
params:
module: [tcp_connect]
static_configs:
- targets: ['192.168.31.62:80']
labels:
instance: 'port_status'
group: 'port'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
replacement: 192.168.31.63:9115
从启动服务
# systemctl restart prometheus
#################################################################################
一些说明解析
- job_name: 'http_status' # 配置job名
metrics_path: /probe # 定义metric获取的路径
params:
module: [http_2xx] # 这里就是我们在black_exporter中定义的模块名
file_sd_configs: # 因需要监控的地址很多,我们这里将所有地址独立出来,后面会介绍该文件
- files:
- '/etc/prometheus/etc.d/job_web.yaml'
refresh_interval: 30s # 30秒刷新一次,当有新的监控地址时,会自动加载进来不需要重启
relabel_configs:
- source_labels: [__address__] # 当前target的访问地址,比如监控百度则为 https://baidu.com
target_label: __param_target # __param是默认参数前缀,target为参数,这里可以理解为把__address__ 的值赋给__param_target,若监控百度,则target=https://baidu.com
- source_labels: [__param_target]
target_label: instance # 可以理解为把__param_target的值赋给instance标签
- target_label: __address__
replacement: 172.33.0.33:9115 # web监控原本的target为站点的地址,但Prometheus不是直接去请求该地址,而是去请求black_exporter,故需要把目标地址替换为black_exporter的地址
job_web.yaml 样例
vim /etc/prometheus/etc.d/job_web.yaml
如下
---
- targets:
- https://www.baidu.com/
labels:
env: pro
app: web
project: 百度
desc: 百度生产
- targets:
- https://blog.csdn.net/
labels:
env: test
app: web
project: CSDN
desc: 测试一下啦
not_200: yes # 这个自定义标签是为了标识某些地址在正常情况下不是返回200状态码
################################################################################
四、Grafana可视化
需要安装饼状图插件 安装后从启动Grafana
grafana-cli plugins install grafana-piechart-panel
systemctl restart grafana-server.service
添加模板
模板编号ID:9965
ID: 14603
五、告警规则
groups:
- name: web
rules:
- alert: Web访问异常
expr: probe_http_status_code{not_200 != "yes" } != 200
for: 30s
annotations:
summary: Web 访问异常{{ $labels.instance }}
labels:
Severity: '严重'
- alert: Web访问响应响应时间>3s
expr: probe_duration_seconds >= 3
for: 30s
annotations:
summary: Web 响应异常{{ $labels.instance }}
labels:
Severity: '警告'
- alert: 证书过期时间<30天
expr: probe_ssl_earliest_cert_expiry-time()< 3600*24*30
annotations:
summary: Web 证书将在30天后过期 {{ $labels.instance }}
labels:
Severity: '提醒'
- alert: 证书过期时间<7天
expr: probe_ssl_earliest_cert_expiry-time()< 3600*24*7
annotations:
summary: Web 证书将在30天后过期 {{ $labels.instance }}
labels:
Severity: '严重'
- alert: 证书过期时间<1天
expr: probe_ssl_earliest_cert_expiry-time()< 3600*24*1
annotations:
summary: Web 证书将在30天后过期 {{ $labels.instance }}
labels:
Severity: '灾难'