喜欢使用 Grafana 进行服务器指标监控吗? 您也会喜欢用于网站监控的 Blackbox Exporter!
在本教程中,您将学习在 CentOS/RHEL 上安装 Blackbox Exporter,并了解如何将其与 Prometheus 和 Grafana 结合使用来监控网站性能和正常运行时间。
什么是黑盒导出器?
黑盒出口商 Prometheus 用于探测 HTTPS、HTTP、TCP、DNS 和 ICMP 等端点。 定义端点后,Blackbox 导出器会生成数百个指标,这些指标可以使用 Grafana 进行可视化。 测量响应时间是 Blackbox 导出器最重要的功能。
Blackbox Exporter 是一种自托管解决方案。 如果您正在寻找类似的东西,但作为 SaaS 或基于云的,那么您可以尝试 Grafana worldPing。
目录
先决条件
必须预先安装 Prometheus 和 Grafana。 按照本文安装 Prometheus 和 Grafana。
Blackbox 导出器安装
让我先向您展示如何安装 Blackbox exporter。
第 1 步:下载 Blackbox Exporter
前往 官方网站 下载最新的 Linux AMD Blackbox 导出程序包。 我正在下载 blackbox_exporter-0.16.0.linux-amd64.tar.gz 文件。
- 将下载的文件传输到服务器
- 从包中提取文件。
[[email protected] ~]$ tar -xzf blackbox_exporter-0.16.0.linux-amd64.tar.gz
检查 Blackbox exporter 中的所有文件。
[[email protected] ~]$ cd blackbox_exporter-0.16.0.linux-amd64/ [[email protected] blackbox_exporter-0.16.0.linux-amd64]$ ls -l total 16672 -rwxr-xr-x. 1 centos centos 17050332 Nov 11 2019 blackbox_exporter -rw-r--r--. 1 centos centos 629 Nov 11 2019 blackbox.yml -rw-r--r--. 1 centos centos 11357 Nov 11 2019 LICENSE -rw-r--r--. 1 centos centos 94 Nov 11 2019 NOTICE
这里有两个文件 blackbox_exporter 和 blackbox.yml 很重要。
- blackbox_exporter:这是一个运行服务的可执行文件。
- blackbox.yml:一个 YAML 文件,其中包含您定义要探测的所有端点和要运行的模块的所有 backbox 导出器配置。
下面的命令将为您提供可与 Blackbox 导出器一起使用的所有标志。
[[email protected] blackbox_exporter-0.16.0.linux-amd64]$ ./blackbox_exporter -h usage: blackbox_exporter [<flags>] Flags: -h, --help Show context-sensitive help (also try --help-long and --help-man). --config.file="blackbox.yml" Blackbox exporter configuration file. --web.listen-address=":9115" The address to listen on for HTTP requests. --timeout-offset=0.5 Offset to subtract from timeout in seconds. --config.check If true validate the config file and then exit. --history.limit=100 The maximum amount of items to keep in the history. --web.external-url=<url> The URL under which Blackbox exporter is externally reachable (for example, if Blackbox exporter is served via a reverse proxy). Used for generating relative and absolute links back to Blackbox exporter itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served by Blackbox exporter. If omitted, relevant URL components will be derived automatically. --web.route-prefix=<path> Prefix for the internal routes of web endpoints. Defaults to path of --web.external-url. --log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error] --log.format=logfmt Output format of log messages. One of: [logfmt, json] --version Show application version.
第 2 步:为 Blackbox 创建用户
这是可选的。
您可以创建一个新用户来运行 Blackbox 或使用现有的 Prometheus 用户来运行它。
提供权限并将其所有文件的所有权分配给新用户。
[[email protected] ~]$ sudo useradd -rs /bin/false blackbox [[email protected] ~]$ sudo chmod -R 777 /home/centos/blackbox_exporter-0.16.0.linux-amd64 [[email protected] ~]$ sudo chown -R blackbox:blackbox /home/centos/blackbox_exporter-0.16.0.linux-amd64
第 3 步:配置黑盒服务
是时候将 Blackbox Exporter 配置为 systemd 中的一项服务了。 创建文件 blackbox.service 并将下面提到的行放入文件中并保存。
[[email protected] ~]$ sudo gedit /etc/systemd/system/blackbox.service [Unit] Description=Blackbox Exporter Service Wants=network-online.target After=network-online.target [Service] Type=simple User=blackbox Group=blackbox ExecStart=/home/centos/blackbox_exporter-0.16.0.linux-amd64/blackbox_exporter --config.file==/home/centos/blackbox_exporter-0.16.0.linux-amd64/blackbox.yml [Install] WantedBy=multi-user.target
不要忘记将路径更改为您提取它的实际位置。
第 4 步:启动 Blackbox Exporter
运行以下命令以将 Blackbox 导出器作为服务启动。
[[email protected] ~]$ sudo systemctl daemon-reload [[email protected] ~]$ sudo systemctl start blackbox.service [[email protected] ~]$ sudo systemctl status blackbox.service ● blackbox.service - Blackbox Exporter Service Loaded: loaded (/etc/systemd/system/blackbox.service; enabled; vendor preset: disabled) Active: active (running) since Sat 2020-06-13 17:28:40 EDT; 1s ago Main PID: 30427 (blackbox_export) Tasks: 5 CGroup: /system.slice/blackbox.service └─30427 /usr/local/bin/blackbox_exporter --config.file=/etc/blackbox.yml Jun 13 17:28:40 techblik.com.linuxvmimages.com systemd[1]: Started Blackbox Exporter Service. Jun 13 17:28:40 techblik.com.linuxvmimages.com blackbox_exporter[30427]: level=info ts=2020-06-13T21:28:40.283Z caller=main.go:212 msg="Starting b...b9a9)" Jun 13 17:28:40 techblik.com.linuxvmimages.com blackbox_exporter[30427]: level=info ts=2020-06-13T21:28:40.283Z caller=main.go:213 msg="Build cont...SSING) Jun 13 17:28:40 techblik.com.linuxvmimages.com blackbox_exporter[30427]: level=info ts=2020-06-13T21:28:40.283Z caller=main.go:225 msg="Loaded config file" Jun 13 17:28:40 techblik.com.linuxvmimages.com blackbox_exporter[30
伟大的!
Blackbox 导出器已成功启动。 在 http://localhost:9115/ 你可以找到正在运行的 Blackbox Exporter。
将 Blackbox 与 Prometheus 集成
让我们将 Blackbox Exporter 与 Prometheus 集成以开始抓取指标。
- 在 prometheus.yml 文件中添加具有以下配置的黑盒作业。 我将监控 https://www.telegraph.co.uk 的性能。
[[email protected] ~]$ sudo gedit /etc/prometheus/prometheus.yml - job_name: 'blackbox' metrics_path: /probe params: module: [http_2xx] # Look for a HTTP 200 response. static_configs: - targets: - https://www.telegraph.co.uk 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 # The blackbox exporter's real hostname:port.
[[email protected] ~]$ sudo systemctl restart prometheus.service
- 当您再次检查 http://localhost:9090/targets 上的目标时,您会发现一个黑盒作业正在运行我定义的端点。
- 单击图表并运行查询 – probe_http_duration_seconds。
- 将在 Prometheus 监控的数据上创建可视化。
现在是时候在 Grafana 上可视化 Blackbox 作业了。
启动它的方法有很多,但最快的方法之一是从现成的仪表板开始 – 普罗米修斯黑盒出口商.
- 转到 Grafana 仪表板,单击 + 号并单击导入。
- 在通过 grafana.com 导入中,输入仪表板 ID 7587 并单击加载。
- 然后选择 Prometheus 数据源并单击导入。
- Prometheus Blackbox Exporter 仪表板将被导入。
- 您将能够可视化指标,例如全局探测持续时间、状态、HTTP 持续时间、平均探测持续时间、平均 DNS 查找、SSL 过期等等。
- 这些都是仪表板上的独立面板。 让我详细展示 HTTP 持续时间面板。 此面板显示连接、处理、解析、tls 和传输请求所用时间的可视化。
仪表板上有很多选项供您使用。 您可以更改希望显示可视化的时间范围。 您还可以更改数据刷新的时间间隔; 在这里,新数据每 5 秒进入仪表板。
- 当您单击 HTTP Duration 向下箭头时,您将获得如下所示的选项。 单击编辑。
- 在可视化面板下方,您将获得一个查询编辑器。 目前,下面快照中显示的查询正在可视化 – probe_http_duration_seconds。
- 有数百个查询,您可以轻松地将其可视化并根据需要将其添加到仪表板。
- 您还有很多选项可以更改可视化类型。 平均探测持续时间以文本格式显示在仪表板上。 在右侧的可视化面板中,单击平均探测持续时间的 Guage 选项。
- 这就是可视化将如何改变。
结论
以上只是让您了解使用 Prometheus Blackbox 和 Grafana 监控网站有多么容易。 有很多选择,我强烈建议您尝试使用它来体验它。 如果你对学习 Grafana 感到好奇,那么看看他的 在线课程.
喜欢阅读这篇文章吗? 与世界分享怎么样?