prometheus学习笔记之黑盒探针blackbox_exporter
项目地址:https://github.com/prometheus/blackbox_exporter
一、安装blackbox_exporter
wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.25.0/blackbox_exporter-0.25.0.linux-amd64.tar.gz
tar xf blackbox_exporter-0.25.0.linux-amd64.tar.gz -C /usr/local/
cd /usr/local/
ln -sv blackbox_exporter-0.25.0.linux-amd64/ 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=/usr/local/blackbox_exporter/blackbox_exporter \
--config.file=/usr/local/blackbox_exporter/blackbox.yml \
--web.listen-address=:9115
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl restart blackbox-exporter.service
systemctl enable blackbox-exporter.service
netstat -tnlp|grep 9115
浏览器验证
二、blackbox_exporter使用简介
1.页面使用http探测及结果解析
访问blackbox probe接口并传递对应参数
# target 指定探测的域名或地址
# module 指定探测的模块
# debug 开启或关闭debug模式
http://192.168.100.133:9115/probe?target=https://baidu.com&module=http_2xx&debug=true
探测结果说明
2.blackbox_exporter默认支持的探测模块
#共有以下7种
http_2xx代表http get方法,返回code为2xx代表正常
http_post_2xx代表http post方法,返回code为2xx代表正常
icmp 代表icmp 协议
irc_banner代表irc协议,需要匹配发送的请求和响应
pop3s_banner代表邮局协议
ssh_banner代表ssh探活
tcp_connect代表tcp端口探活
三、配置prometheus抓取
blackbox_exporter比较特殊,它的监控对象需要由prometheus提供
示例1:blackbox exporter 实现 URL 监控
- job_name: 'http_status'
metrics_path: /probe
params:
module: [http_2xx] #2xx状态码检测
static_configs:
- targets: ['http://www.xiaomi.com', 'http://www.jd.com/'] #对小米 京东检测
labels:
group: web
relabel_configs:
- source_labels: [__address__] #relabel 通过将__address__(当前目标地址)写入__param_target 标签来创建一个 label。
target_label: __param_target #监控目标 www.xiaomi.com,作为__address__的 value
- source_labels: [__param_target] #监控目标
target_label: instance #将监控目标与 url 创建一个 label
- target_label: __address__
replacement: 192.168.100.134:9115
/usr/local/prometheus/promtool check config prometheus.yml
curl -X POST http://127.0.0.1:9090/-/reload
浏览器验证
示例2:blackbox exporter 实现ICMP 监控
- job_name: 'icmp_status'
metrics_path: /probe
params:
module: [icmp]
static_configs:
- targets: ['192.168.100.131', '192.168.100.132']
labels:
group: icmp
relabel_configs:
- source_labels: [__address__] #relabel 通过将__address__(当前目标地址)写入__param_target 标签来创建一个 label。
target_label: __param_target #监控目标 www.xiaomi.com,作为__address__的 value
- source_labels: [__param_target] #监控目标
target_label: instance #将监控目标与 url 创建一个 label
- target_label: __address__
replacement: 192.168.100.134:9115
/usr/local/prometheus/promtool check config prometheus.yml
curl -X POST http://127.0.0.1:9090/-/reload
浏览器验证
示例3:blackbox_exporter 实现端口监控
- job_name: 'port_status'
metrics_path: /probe
params:
module: [tcp_connect]
static_configs:
- targets: ['192.168.100.131:9100', '192.168.100.132:8080']
labels:
group: port
relabel_configs:
- source_labels: [__address__] #relabel 通过将__address__(当前目标地址)写入__param_target 标签来创建一个 label。
target_label: __param_target #监控目标 www.xiaomi.com,作为__address__的 value
- source_labels: [__param_target] #监控目标
target_label: instance #将监控目标与 url 创建一个 label
- target_label: __address__
replacement: 192.168.100.134:9115
/usr/local/prometheus/promtool check config prometheus.yml
curl -X POST http://127.0.0.1:9090/-/reload
浏览器验证
四、配置grafana
推荐模板: ID 9965
name字段不显示问题
常用查询语句
#所有模块都适用的 探测失败probe_success == 0 #1为成功 探测时间阈值probe_duration_seconds >5 dns解析时间超过5秒probe_dns_lookup_time_seconds > 5 #http模块 http接口返回状态码4xx/5xx错误probe_http_status_code >=400 https证书过期时间小于7天(probe_ssl_earliest_cert_expiry - time())/ 3600 / 24<7 http探测连接耗时大于5秒probe_http_duration_seconds{phase="connect"} >5
官方支持的exporter列表:https://prometheus.io/docs/instrumenting/exporters/
"一劳永逸" 的话,有是有的,而 "一劳永逸" 的事却极少