- 运维监控系列文章入口:【运维监控】系列文章汇总索引
文章目录
-
本示例通过blackbox_exporter收集http/https(业务接口)、tcp、ping、dns的监控指标,然后将数据收集到prometheus中,配置预警规则,出现警告信息时通过邮件通知相关人员,同时也通过grafana的dashboard导入模板进行可视化。
-
本示例分为六个部分,即prometheus、grafana、blackbox_exporter部署,prometheus与blackbox_exporter集成,prometheus、blackbox_exporter与grafana集成,prometheus、blackbox_exporter与alertmanager集成。
-
说明:
1、本示例中的部署环境没有要求,即应用部署的机器根据实际情况而定,只要网络连通即可。
2、prometheus与alertmanager可直接集成,与blackbox_exporter无关,其只是在本示例中使用其进行一些信息的收集。
一、prometheus部署
参考:【运维监控】prometheus+node exporter+grafana 监控linux机器运行情况关于prometheus的部署。
二、grafana部署
参考:【运维监控】prometheus+node exporter+grafana 监控linux机器运行情况 关于grafana的部署。
三、blackbox_exporter部署
blackbox_exporter 是 Prometheus 官方提供的官方黑盒监控解决方案,其中 exporter 之一,可以提供 http(s)、dns、tcp、icmp 的方式对网络进行探测。
目前支持的应用场景:
- ICMP 测试,主机探活机制
- TCP 测试,端口状态监听和应用层协议定义与监听
- HTTP 测试,定义 Request Header 信息、判断 Http status / Http Respones Header / Http Body 内容
- POST 测试,接口联通性
- SSL 证书过期时间
- 自定义测试(扩展)
本服务部署在server2上。
1、下载
下载地址:https://github.com/prometheus/blackbox_exporter
下载版本:blackbox_exporter-0.25.0.linux-amd64.tar.gz
2、解压
[alanchan@server2 bigdata]$ tar xf blackbox_exporter-0.25.0.linux-amd64.tar.gz -C /usr/local/bigdata
[alanchan@server2 blackbox_exporter-0.25.0.linux-amd64]$ pwd
/usr/local/bigdata/blackbox_exporter-0.25.0.linux-amd64
[alanchan@server2 blackbox_exporter-0.25.0.linux-amd64]$ ll
total 21124
-rwxr-xr-x 1 alanchan root 21606608 Apr 9 12:59 blackbox_exporter
-rw-r--r-- 1 alanchan root 1404 Sep 11 06:46 blackbox.yml
-rw-r--r-- 1 alanchan root 11357 Apr 9 13:00 LICENSE
-rw-r--r-- 1 alanchan root 94 Apr 9 13:00 NOTICE
3、启动
blackbox.yml的默认配置文件内容如下。可以根据自己的需要进行修改或保持默认。
[alanchan@server2 blackbox_exporter-0.25.0.linux-amd64]$ cat blackbox.yml
modules:
http_2xx: # 模块名称,prometheus配置文件中要匹配
prober: http # 协议
http: # 模块的采集协议
preferred_ip_protocol: "ip4" # 使用的ipv4协议
http_post_2xx:
prober: http
http:
method: POST # http请求的方法
# headers: # 配置post请求的header头
# Content-Type: application/json
# body: ''{"username": "alanchanchn", "password": "123456"}'' # 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
启动命令如下。
[alanchan@server2 blackbox_exporter-0.25.0.linux-amd64]$ ll
total 21124
-rwxr-xr-x 1 alanchan root 21606608 Apr 9 12:59 blackbox_exporter
-rw-r--r-- 1 alanchan root 956 Apr 9 13:00 blackbox.yml
-rw-r--r-- 1 alanchan root 11357 Apr 9 13:00 LICENSE
-rw-r--r-- 1 alanchan root 94 Apr 9 13:00 NOTICE
[alanchan@server2 blackbox_exporter-0.25.0.linux-amd64]$ blackbox_exporter
ts=2024-09-11T06:45:43.194Z caller=main.go:87 level=info msg="Starting blackbox_exporter" version="(version=0.25.0, branch=HEAD, revision=ef3ff4fef195333fb8ee0039fb487b2f5007908f)"
ts=2024-09-11T06:45:43.194Z caller=main.go:88 level=info build_context="(go=go1.22.2, platform=linux/amd64, user=root@47d5b0d99f18, date=20240409-12:58:39, tags=unknown)"
ts=2024-09-11T06:45:43.194Z caller=main.go:91 level=error msg="Error loading config" err="error parsing config file: yaml: unmarshal errors:\n line 52: mapping key \"modules\" already defined at line 1"
[alanchan@server2 blackbox_exporter-0.25.0.linux-amd64]$ blackbox_exporter
ts=2024-09-11T06:46:08.036Z caller=main.go:87 level=info msg="Starting blackbox_exporter" version="(version=0.25.0, branch=HEAD, revision=ef3ff4fef195333fb8ee0039fb487b2f5007908f)"
ts=2024-09-11T06:46:08.036Z caller=main.go:88 level=info build_context="(go=go1.22.2, platform=linux/amd64, user=root@47d5b0d99f18, date=20240409-12:58:39, tags=unknown)"
ts=2024-09-11T06:46:08.036Z caller=main.go:100 level=info msg="Loaded config file"
ts=2024-09-11T06:46:08.037Z caller=tls_config.go:313 level=info msg="Listening on" address=[::]:9115
ts=2024-09-11T06:46:08.037Z caller=tls_config.go:316 level=info msg="TLS is disabled." http2=false address=[::]:9115
4、验证
正常启动后,可在浏览器中输入http://server2:9115/,查看信息,界面如下。
刚启动应该是没有数据的。至此,blackbox_exporter部署完成。
四、prometheus集成blackbox_exporter
1、添加prometheus监控规则
命名blackbox_exporter.yml,其内容如下
groups:
- name: Blackbox_alanchan
rules:
- alert:blackbox探测失败警告
expr: probe_success == 0
for: 1m
labels:
severity: critical
annotations:
summary: "blackbox探测失败{
{ $labels.instance }}"
description: "blackbox检测失败,当前值:{
{ $value }}"
- alert: 请求慢预警
expr: avg_over_time(probe_duration_seconds[1m]) > 3
for: 1m
labels:
severity: warning
annotations:
summary: "请求慢{
{ $labels.instance }}"
description: "请求时间超过3秒,值为:{
{ $value }}"
- alert: http状态码检测失败
expr: probe_http_status_code <= 199 OR probe_http_status_code >= 400
for: 1m
labels:
severity: critical
annotations:
summary: "http状态码检测失败{
{ $labels.instance }}"
description: "HTTP状态码不在 200-399 区间,当前状态码是{
{ $value }}"
Prometheus alert 预警状态有三种状态:Inactive、Pending、Firing。
- Inactive:非活动状态,表示正在监控,但是还未有任何警报触发。
- Pending:已触发阈值,但未满足告警持续时间(即rule中的for字段)。由于警报可以被分组、抑制或静默,所以等待验证,一旦所有的验证都通过,则将转到 Firing 状态。
- Firing:将警报发送到 AlertManager,它将按照配置将警报的发送给所有接收者。一旦警报解除,则将状态转到 Inactive,如此循环。
Prometheus支持两种类型的规则,即记录规则和警报规则。它们可以进行配置,然后定期进行评估。 要将规则包含在Prometheus中,需要先创建一个包含必要规则的文件,并让Prometheus通过Prometheus配置中的rule_fies字段加载该文件。 默认情况下,prometheus的规则文件使用YAML。
规则的使用流程如下:
- 创建一个满足规则标准的规则
- 发送SIGHUP给Prometheus进程,prometheus在运行时重新加载规则文件,从而让规则在prometheus运行环境中生效。
规则文件的语法
groups:
[ - <rule_group> ]
规则文件属性
name 规则组名,必须是唯一的
interval 定制规则执行的间隔时间
rules 设定规则具体信息
record 定制指标的名称
expr 执行成功的PromQL
labels 为该规则设定标签
示例
groups:
- name: Blackbox_alanchan
rules:
- alert: blackbox探测失败警告 #自定义预警的动作名称
expr: probe_success == 0
for: 1m # expr动作触发后,持续的时间,达到该条件就会预警
labels:
severity: critical # 预警级别
annotations: # 注释信息,注释信息中的变量需要从模板中或者系统中读取
summary: "blackbox探测失败{
{ $labels.instance }}"
description: "blackbox检测失败,当前值:{
{ $value }}"
规则语法检测命令
promtool check rules /usr/local/bigdata/prometheus-2.54.0.linux-amd64/rules/blackbox_exporter.yml
2、添加prometheus的预警规则
去掉prometheus.yml中关于预警规则的注释,修改内容为如下。具体视环境而定,下文是作者的规则文件存放目录。
rule_files:
- "/usr/local/bigdata/prometheus-2.54.0.linux-amd64/rules/blackbox_exporter.yml"
添加好的预警规则可以在http://server2:9090/rules中查看,示例如下。
3、添加prometheus的监控信息收集
在prometheus.yml中添加如下信息。
scrape_configs:
# blackbox
#http配置
- job_name: "blackbox_http"
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
- https://www.baidu.com
- http://192.168.3.105:8080/test/example/test.json
- http://192.168.3.105:8080/test/example
- https://github.com/prometheus/blackbox_exporter
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: server2:9115 # blackbox安装在哪台机器
#tcp检查配置
- job_name: "blackbox_tcp"
metrics_path: /probe
params:
module: [tcp_connect]
static_configs:
- targets:
- server8:22
- server2:9090
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: server2:9115
#icmp检查配置 ping
- job_name: "blackbox_icmp"
metrics_path: /probe
params:
module: [icmp]
static_configs:
- targets:
- 192.168.10.71
- 192.168.10.118
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: server2:9115
4、重启prometheus
[alanchan@server2 prometheus-2.54.0.linux-amd64]$ ./prometheus
ts=2024-09-12T05:50:17.005Z caller=main.go:601 level=info msg="No time or size retention was set so using the default time retention" duration=15d
ts=2024-09-12T05:50:17.005Z caller=main.go:645 level=info msg="Starting Prometheus Server" mode=server version="(version=2.54.0, branch=HEAD, revision=5354e87a70d3eb26b81b601b286d66ff983990f6)"
ts=2024-09-12T05:50:17.005Z caller=main.go:650 level=info build_context="(go=go1.22.6, platform=linux/amd64, user=root@68a9e2472a68, date=20240809-11:36:32, tags=netgo,builtinassets,stringlabels)"
ts=2024-09-12T05:50:17.005Z caller=main.go:651 level=info host_details="(Linux 2.6.32-754.35.1.el6.x86_64 #1 SMP Sat Nov 7 12:42:14 UTC 2020 x86_64 server2 (none))"
ts=2024-09-12T05:50:17.005Z caller=main.go:652 level=info fd_limits="(soft=131072, hard=131072)"
ts=2024-09-12T05:50:17.005Z caller=main.go:653 level=info vm_limits="(soft=unlimited, hard=unlimited)"
ts=2024-09-12T05:50:17.009Z caller=web.go:571 level=info component=web msg="Start listening for connections" address=0.0.0.0:9090
ts=2024-09-12T05:50:17.009Z caller=main.go:1160 level=info msg="Starting TSDB ..."
ts=2024-09-12T05:50:17.012Z caller=tls_config.go:313 level=info component=web msg="Listening on" address=[::]:9090
ts=2024-09-12T05:50:17.012Z caller=tls_config.go:316 level=info component=web msg="TLS is disabled." http2=false address=[::]:9090
ts=2024-09-12T05:50:17.027Z caller=head.go:626 level=info component=tsdb msg="Replaying on-disk memory mappable chunks if any"
ts=2024-09-12T05:50:17.036Z caller=head.go:713 level=info component=tsdb msg="On-disk memory mappable chunks replay completed" duration=9.269999ms
ts=2024-09-12T05:50:17.036Z caller=head.go:721 level=info component=tsdb msg="Replaying WAL, this may take a while"
ts=2024-09-12T05:50:17.048Z caller=head.go:758 level=info component=tsdb msg="WAL checkpoint loaded"
ts=2024-09-12T05:50:17.483Z caller=head.go:830 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=11.904575ms wal_replay_duration=435.015809ms wbl_replay_duration=158ns chunk_snapshot_load_duration=0s mmap_chunk_replay_duration=9.269999ms total_replay_duration=456.234198ms
ts=2024-09-12T05:50:17.487Z caller=main.go:1181 level=info fs_type=EXT4_SUPER_MAGIC
ts=2024-09-12T05:50:17.487Z caller=main.go:1184 level=info msg="TSDB started"
ts=2024-09-12T05:50:17.488Z caller=main.go:1367 level=info msg="Loading configuration file" filename=prometheus.yml
ts=2024-09-12T05:50:17.490Z caller=main.go:1404 level=info msg="updated GOGC" old=100 new=75
ts=2024-09-12T05:50:17.490Z caller=main.go:1415 level=info msg="Completed loading of configuration file" filename=prometheus.yml totalDuration=2.770824ms db_storage=7.513µs remote_storage=4.818µs web_handler=941ns query_engine=3.672µs scrape=412.441µs scrape_sd=261.046µs notify=44.541µs notify_sd=10.73µs rules=1.118939ms tracing=13.933µs
ts=2024-09-12T05:50:17.490Z caller=main.go:1145 level=info msg="Server is ready to receive web requests."
ts=2024-09-12T05:50:17.491Z caller=manager.go:164 level=info component="rule manager" msg="Starting rule manager..."
启动成功后,可以通过下面验证部分进行验证。
5、验证
验证分为2个部分,即添加的预警规则是否正常显示以及监控的信息是否收集到。添加的监控信息如下图可以看到已经加载进来。
可以通过prometheus的预警按钮查看,如下图。由于监控的服务没有启动,所以会出现预警。
点击详细进去可以看看,如下图。
启动我们提前创建好的接口服务,再观察监控的情况。
接口服务为简单的2个spring boot创建的restful接口,其中一个接口设置延迟时间600毫秒。
另外,我们监控的192.168.10.71机器不存在,49机器不能访问。
以上就完成了prometheus和blackbox_expoerter监控http协议的接口、tcp的连接等操作了,并可进行预警。
该文章分为2个部分,即: