作者:北干吴 | 来源:互联网 | 2023-06-04 21:08
文章目录配置详情安装配置black_expoter配置Prometheus支持black_expoter配置告警规则配置grafana 本文所指的web监控是指对某些访问地址或者说是接口进行监控。我们
文章目录
- 配置详情
- 安装配置black_expoter
- 配置Prometheus支持black_expoter
- 配置告警规则
- 配置grafana
本文所指的web监控是指对某些访问地址或者说是接口进行监控。我们将通过一些实例,来介绍如何配置Prometheus 、black_exporter、grafana来监控站点的以下几个方面:
- 状态码
- 响应时间
- 证书过期时间
最终的效果图如下(图片建议在电脑端看):
单个站点监控详情图
企业微信告警样图
Prometheus的web监控需要借助 blackbox_exporter
当然black_exporter 的功能远不止于监控web站点,它还能监控端口(TCP),DNS、UDP等,以下是官方给出的介绍
The blackbox exporter allows blackbox probing of endpoints over HTTP, HTTPS, DNS, TCP and ICMP.
我们这里只专注于web监控,其他监控不在本文介绍范围。
配置详情
配置大概分为以下几步:
- 安装black_exporter
- 配置监控目标地址
- 配置告警规则
- 配置grafana面板
安装配置black_expoter
我这里使用docker-compose进行部署(我的Prometheus、grafana、alertmanager等都定义在一个compose文件中,这里只列出black_exporter)
version: '3.7'
services:
blackbox_exporter:
container_name: blackbox_exporter
image: prom/blackbox-exporter:master
volumes:
- /data/monitor/blackbox_exporter/config/config.yml:/etc/blackbox_exporter/config.yml
ports:
- 9115:9115
配置文件/data/monitor/blackbox_exporter/config/config.yml 内容如下:
modules:
http_2xx:
prober: http
timeout: 5s
http:
valid_status_codes: []
method: GET
fail_if_body_not_matches_regexp: []
tls_config:
insecure_skip_verify: true
配置Prometheus支持black_expoter
在prometheus.yml配置文件中加入以下内容:
- job_name: 'http_status'
metrics_path: /probe
params:
module: [http_2xx]
file_sd_configs:
- files:
- '/etc/prometheus/etc.d/job_web.yaml'
refresh_interval: 30s
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 172.33.0.33:9115
job_web.yaml 样例
---
- targets:
- https://www.baidu.com/
labels:
env: pro
app: web
project: 百度
desc: 百度生产
- targets:
- https://blog.csdn.net/
labels:
env: test
app: web
project: CSDN
desc: 测试一下啦
not_200: yes
配置完成后,将在你的Prometheus看到类似以下的目标
我们找一个点击进去,可以看到最终跳转的地址类似如下:
http://172.33.0.33:9115/probe?module=http_2xx&target=https%3A%2F%2Fwww.baidu.com
指标信息截图
配置告警规则
告警规则文件
groups:
- name: web
rules:
- alert: Web访问异常
expr: probe_http_status_code{not_200 != "yes" } != 200
for: 30s
annotations:
summary: Web 访问异常{{ $labels.instance }}
labels:
Severity: '严重'
- alert: Web访问响应响应时间>3s
expr: probe_duration_seconds >= 3
for: 30s
annotations:
summary: Web 响应异常{{ $labels.instance }}
labels:
Severity: '警告'
- alert: 证书过期时间<30天
expr: probe_ssl_earliest_cert_expiry-time()<3600*24*30
annotations:
summary: Web 证书将在30天后过期 {{ $labels.instance }}
labels:
Severity: '提醒'
- alert: 证书过期时间<7天
expr: probe_ssl_earliest_cert_expiry-time()<3600*24*7
annotations:
summary: Web 证书将在30天后过期 {{ $labels.instance }}
labels:
Severity: '严重'
- alert: 证书过期时间<1天
expr: probe_ssl_earliest_cert_expiry-time()<3600*24*1
annotations:
summary: Web 证书将在30天后过期 {{ $labels.instance }}
labels:
Severity: '灾难'
配置grafana
grafana web监控我制作了2个面板,如上文中的截图。
一个是所有站点的概览,另一个是单个站点的详情。
通过概览点击可以跳转到详情页。
grafana的配置较为繁琐,文中的面板已经开源,可以在grafana官网下载该面板 Web Monitor Center dashboard for Grafana | Grafana Labs
注意: 在笔者实际使用过程中,发现grafana的兼容性不是很好,从grafana下载下来的面板有很多不兼容,故此也可能出现兼容性问题,目前笔者grafana的版本为: v7.5.3
如果你觉得有收获,可以关注公众号。