云原生监控系统Prometheus——黑盒监控(blackbox_exporter)
黑盒监控(blackbox_exporter)
之前几个 exporter 的使用,我们可以称为 "白盒监控",即需要把对应的 expoter 程序安装到被监控的目标主机上,从而实现对主机各种资源及其状态的数据采集工作。但是由于某些清下操作技术或其他原因,不是所有的 expoter 都能部署到被监控的主机环境中,最典型的例子是监控全国网络质量的稳定性,通常的方法是使用 ping 操作,对选取的节点进行 ICMP 测试,此时不可能在他人的环境中部署相关的 expoter 程序。
通过 HTTP、HTTPS、DNS、TCP、ICMP、GRPC 等方式对网络进行探测监控,还可以探测 SSL 证书过期时间。
在安装之前,首先在官方下载页面 https://github.com/prometheus/blackbox_exporter/releases 下找到 blackbox_exporter,下载最新版本中特定平台的二进制文件。如下图所示:
一、部署 blackbox_exporter
这里以官方 docker 安装方式为例(https://github.com/prometheus/blackbox_exporter):
1 | docker run -- rm -d -p 9115:9115 --name blackbox_exporter - v ` pwd `: /config prom /blackbox-exporter :master --config. file = /config/blackbox .yml |
Note: You may want to enable ipv6 in your docker configuration:
-
- Edit /etc/docker/daemon.json, set the ipv6 key to true and the fixed-cidr-v6 key to your IPv6 subnet. In this example we are setting it to 2001:db8:1::/64.
1234
{
"ipv6"
:
true
,
"fixed-cidr-v6"
:
"2001:db8:1::/64"
}
- Reload the Docker configuration file.
1
systemctl reload docker
- Edit /etc/docker/daemon.json, set the ipv6 key to true and the fixed-cidr-v6 key to your IPv6 subnet. In this example we are setting it to 2001:db8:1::/64.
You can now create networks with the --ipv6 flag and assign containers IPv6 addresses using the --ip6 flag.
二、Checking the results
访问 http://localhost:9115/probe?target=google.com&module=http_2xx 将返回针对 google.com 的 HTTP 探测的指标。该 probe_success
指标指示探测是否成功。添加 debug=true
参数将返回该探测器的调试信息。
三、TLS and basic authentication
Blackbox Exporter 支持 TLS 和基本身份验证。这可以更好地控制各种 HTTP 端点。
要使用 TLS 和/或基本身份验证,您需要使用--web.config.file
参数传递配置文件。文件的格式 在 exporter-toolkit 存储库中描述。
请注意,TLS 和基本身份验证设置会影响所有 HTTP 端点:用于抓取的 /metrics、用于探测的 /probe 和 Web UI。
四、配置 blackbox_exporter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | modules: http_2xx: prober: http http_post_2xx: prober: http http: method: POST tcp_connect: prober: tcp pop3s_banner: prober: tcp tcp: query_response: - expect: "^+OK" tls: true tls_config: insecure_skip_verify: false grpc: prober: grpc grpc: tls: true preferred_ip_protocol: "ip4" grpc_plain: prober: grpc grpc: tls: false service: "service1" ssh_banner: prober: tcp tcp: query_response: - expect: "^SSH-2.0-" - send: "SSH-2.0-blackbox-ssh-check" irc_banner: prober: tcp tcp: query_response: - send: "NICK prober" - send: "USER prober prober prober :prober" - expect: "PING :([^ ]+)" send: "PONG ${1}" - expect: "^:[^ ]+ 001" icmp: prober: icmp icmp_ttl5: prober: icmp timeout: 5s icmp: ttl: 5 |
五、与 Prometheus 集成
- 5.1、ping 测试
12345678910111213141516171819
- job_name:
'ping_all'
scrape_interval: 1m
metrics_path:
/probe
params:
module: [
ping
]
static_configs:
- targets:
- 192.168.1.2
labels:
instance: node2
- targets:
- 192.168.1.3
labels:
instance: node3
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
replacement: 127.0.0.1:9115
下面是一个http://127.0.0.1:9115/probe?module=ping&target=192.168.1.2返回的是192.168.1.2这个target的metrics。
123456789101112131415161718192021222324252627282930313233#DNS解析时间,单位 s
probe_dns_lookup_time_seconds 0.039431355
#探测从开始到结束的时间,单位 s,请求这个页面响应时间
probe_duration_seconds 0.651619323
probe_failed_due_to_regex 0
#HTTP 内容响应的长度
probe_http_content_length -1
#按照阶段统计每阶段的时间
probe_http_duration_seconds{phase=
"connect"
} 0.050388884
#连接时间
probe_http_duration_seconds{phase=
"processing"
} 0.45868667
#处理请求的时间
probe_http_duration_seconds{phase=
"resolve"
} 0.040037612
#响应时间
probe_http_duration_seconds{phase=
"tls"
} 0.145433254
#校验证书的时间
probe_http_duration_seconds{phase=
"transfer"
} 0.000566269
#重定向的次数
probe_http_redirects 1
#ssl 指示是否将 SSL 用于最终重定向
probe_http_ssl 1
#返回的状态码
probe_http_status_code 200
#未压缩的响应主体长度
probe_http_uncompressed_body_length 40339
#http 协议的版本
probe_http_version 1.1
#使用的 ip 协议的版本号
probe_ip_protocol 4
probe_ssl_earliest_cert_expiry 1.59732e+09
#是否探测成功
probe_success 1
#TLS 的版本号
probe_tls_version_info{version=
"TLS 1.2"
} 1
- 5.2、http 监测
123456789101112131415
- job_name:
'http_get_all'
# blackbox_export module
scrape_interval: 30s
metrics_path:
/probe
params:
module: [http_2xx]
static_configs:
- targets:
- https:
//frognew
.com
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
http检测除了可以探测http服务的存活外,还可以根据指标probe_ssl_earliest_cert_expiry进行ssl证书有效期预警。
123# HELP probe_ssl_earliest_cert_expiry Returns earliest SSL cert expiry in unixtime
# TYPE probe_ssl_earliest_cert_expiry gauge
probe_ssl_earliest_cert_expiry 1.581897599e+09
- 5.3、监控主机存活状态
123456789101112131415
- job_name: node_status
metrics_path:
/probe
params:
module: [icmp]
static_configs:
- targets: [
'10.165.94.31'
]
labels:
instance: node_status
group:
'node'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
replacement: 172.19.155.133:9115
10.165.94.31是被监控端ip,172.19.155.133是Blackbox_exporter
- 5.4、监控主机端口存活状态
12345678910111213141516
- job_name:
'prometheus_port_status'
metrics_path:
/probe
params:
module: [tcp_connect]
static_configs:
- targets: [
'172.19.155.133:8765'
]
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: 172.19.155.133:9115
- 5.5、监控网站状态
1234567891011121314
- job_name: web_status
metrics_path:
/probe
params:
module: [http_2xx]
static_configs:
- targets: [
'http://www.baidu.com'
]
labels:
instance: user_status
group:
'web'
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- target_label: __address__
replacement: 172.19.155.133:9115
六、告警
1 2 3 4 5 6 7 8 9 10 11 | groups : - name: example rules: - alert: curlHttpStatus expr : probe_http_status_code{job= "blackbox-http" }>=400 and probe_success{job= "blackbox-http" }==0 #for: 1m labels: docker: number annotations: summary: '业务报警: 网站不可访问' description: '{{$labels.instance}} 不可访问,请及时查看,当前状态码为{{$value}}' |
七、Grafana
grafana模板号:9965。下载地址 https://grafana.com/grafana/plugins/grafana-piechart-panel
安装插件,重启grafana生效。
1 | grafana-cli plugins install grafana-piechart-panel |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析