使用 blackbox_exporter、Prometheus 和 Grafana 进行服务器的 Ping 监视
首先
-
- ローカルPCとのPing疎通が途絶えたらSlackにアラートメッセージを飛ばすようにしました
- 実際の運用では、サーバの死活監視をするような場面を想定しています
基础设施构建

blackbox_exporter
ポートを開放して Prometheus から監視対象ノードに Ping (ICMP) 疎通確認ができるようにする
prometheus
メトリクス情報の収集
grafana
可視化 + アラートの Slack 通知
我认为在实际运营中,需要将监控节点和被监控节点分开配置。然而,为了简化,我们这次在本地的Macbook上构建了环境。
创建环境
黑匣子导出器的安装
为了使Prometheus能够对监控节点进行Ping监控,需要安装blackbox_exporter。
-
- 从 Prometheus 的下载页面下载 Blackbox Exporter 的二进制文件,并将其解压到适当的位置。
确保 blackbox.yml 中包含以下内容(默认已包含)。
modules:
icmp:
prober: icmp
# IPV6 が利用できない環境の場合は以下の設定を追加します。
icmp_ipv4:
prober: icmp
icmp:
preferred_ip_protocol: ip4
-
- 转到先前展开的目录并启动blackbox_exporter。
-
- sudo ./blackbox_exporter
-
- 由于需要访问Socket,因此需要root/sudo权限。
- 检查与本地主机的ping通信。
$ curl "http://localhost:9115/probe?module=icmp&target=localhost"
# HELP probe_dns_lookup_time_seconds Returns the time taken for probe dns lookup in seconds
# TYPE probe_dns_lookup_time_seconds gauge
probe_dns_lookup_time_seconds 0.000636406
# HELP probe_duration_seconds Returns how long the probe took to complete in seconds
# TYPE probe_duration_seconds gauge
probe_duration_seconds 0.001000538
# HELP probe_icmp_duration_seconds Duration of icmp request by phase
# TYPE probe_icmp_duration_seconds gauge
probe_icmp_duration_seconds{phase="resolve"} 0.000636406
probe_icmp_duration_seconds{phase="rtt"} 0.000144149
probe_icmp_duration_seconds{phase="setup"} 7.2599e-05
# HELP probe_ip_protocol Specifies whether probe ip protocol is IP4 or IP6
# TYPE probe_ip_protocol gauge
probe_ip_protocol 4
# HELP probe_success Displays whether or not the probe was a success
# TYPE probe_success gauge
probe_success 1
如果显示为 “probe_success 1″,则表示成功。
普罗米修斯的安装
-
- 从Prometheus的下载页面下载Prometheus的二进制文件,然后将其解压到适当的位置。
-
- 对于Mac,也可以使用brew进行安装。
-
- brew install prometheus
移动到解压的二进制文件位置,并按照以下方式编辑prometheus.yml。
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: 'blackbox'
metrics_path: /probe
params:
module: [icmp]
static_configs:
- targets:
- localhost
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 # blackbox_expoter のエンドポイントを指定
-
- 启动 Prometheus。
-
- 1. 运行./prometheus命令。
-
- (如果使用brew安装,请使用prometheus命令)
-
- 2. 通过浏览器访问 Prometheus 的管理页面。
-
- http://localhost:9090/graph
-
- 3. 输入以下查询,并点击执行。
- probe_success{instance=”localhost”}
会显示如下图所示。

安装Grafana。
-
- 按照安装手册进行安装。
-
- 启动服务。
在Mac OSX上: 运行命令brew services start grafana。
确认服务已经启动。
在Mac OSX上: 运行命令brew services list。
使用浏览器登录grafana的管理界面。
http://localhost:3000
ID和密码为root/root。
在Grafana中进行管理界面的设置。
-
- 根据 Prometheus 的官方手册,设置 Grafana 的数据源为 Prometheus。
URL:http://localhost:9090 (Prometheus 的端点)。
新增用于监控的Ping图表
-
- 使用Grafana的管理界面创建新的仪表板,并添加监控Ping连通性的图表。
查询: Prometheus
probe_success{instance=”localhost”}

设置警报
如果1分钟的平均值低于1,则发出警报(如果有一次通信失败,则发出警报)。

设置警报通道
-
- 根据Slack Incoming Webhook的设置,生成所需通知频道的Incoming Webhook URL。
按照Grafana官方文档的指引,添加Slack作为Alert Channel。
发布警报
重新创建节点通信被阻断的情况,以确认是否发出警报。
(例如:停止blackbox_exporter,并使用普通用户重新启动,以重现节点通信的断开)

参考的文章
- https://www.robustperception.io/icmp-pings-with-the-blackbox-exporter