要使用普罗米修斯监控你的网站主页 http://gyq.com/,可以通过以下步骤实现。普罗米修斯本身并不直接支持 HTTP 状态码的监控,但可以通过 Blackbox Exporter 来完成这项任务。

方案概述
  1. Blackbox Exporter 是一个普罗米修斯官方提供的工具,用于探测网络服务的可用性(如 HTTP、HTTPS、TCP、ICMP 等)。
  2. 通过配置 Blackbox Exporter,可以定期检查你的网站是否能正常访问(HTTP 状态码是否为 200)。
  3. 普罗米修斯会从 Blackbox Exporter 中拉取监控数据,并根据配置设置告警规则。
具体步骤
1. 安装和配置 Blackbox Exporter
  • 下载 Blackbox Exporter

2.解压

tar -xvzf blackbox_exporter-*.tar.gz
cd blackbox_exporter-*
  • 1.
  • 2.
  • 编辑 blackbox.yml 文件,添加对 HTTP 的探测模块:
modules:
  http_2xx:
    prober: http
    timeout: 5s
    http:
      preferred_ip_protocol: "ip4"
      valid_http_versions: ["HTTP/1.1", "HTTP/2"]  # 允许的 HTTP 版本
      valid_status_codes: []  # 默认只允许 2xx 状态码
      fail_if_ssl: false  # 不强制要求 SSL
      fail_if_not_ssl: false  # 不强制要求非 SSL
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 这个配置表示:如果返回的状态码是 2xx,则认为探测成功。
  • 启动 Blackbox Exporter
nohup ./blackbox_exporter --config.file=blackbox.yml &
  • 1.
2. 配置 Prometheus
  • 修改 Prometheus 配置文件 编辑 prometheus.yml 文件,添加 Blackbox Exporter 的监控任务:
- job_name: 'gyq.com_monitor'
    metrics_path: /probe
    params:
      module: [http_2xx]  # 使用 Blackbox Exporter 中定义的模块
    static_configs:
      - targets:
          - http://gyq.com/  # 要监控的目标网址
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.158.183:9115  # Blackbox Exporter 的地址和端口
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

使用Prometheus监控网站是否正常打开_钉钉告警

  • 说明:
  • job_name: 'website_monitor':为这个监控任务命名。
  • metrics_path: /probe:指定 Blackbox Exporter 的探测路径。
  • params.module: [http_2xx]:指定使用的探测模块(即 http_2xx)。
  • targets:填写你要监控的目标 URL(如 http://gyq.com/)。
  • relabel_configs:将目标 URL 转换为 Blackbox Exporter 的参数。

重启 Prometheus

重新加载 Prometheus 配置:

curl -X POST http://localhost:9090/-/reload
  • 1.

或者直接重启 Prometheus 服务。

3. 配置告警规则
  • 编辑告警规则文件 创建或编辑 Prometheus 的告警规则文件
[root@prometheus rules]# pwd
/usr/local/prometheus/rules
[root@prometheus rules]# cat web_rules.yml
groups:
  - name: web_gyq_com
    rules:
      - alert: gyq_com_Down
        expr: probe_success == 0
        for: 5s
        labels:
          severity: critical
        annotations:
          summary: "Website {{ $labels.instance }} is down"
          description: "这个网站 {{ $labels.instance }} 目前打不开了."
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

使用Prometheus监控网站是否正常打开_linux_02

注意:以上规则文件需要在 Prometheus配置文件中定义好规则路径,修改yml配置后都需要重启或重新加载才能生效curl -X POST http://localhost:9090/-/reload

使用Prometheus监控网站是否正常打开_监控_03

验证监控
  1. 访问 Prometheus Web UI 打开浏览器访问 http://<prometheus-ip>:9090,查看以下指标:
  • probe_success:是否为 1(表示探测成功)。
  • probe_duration_seconds:探测耗时。
  1. 模拟网站故障 暂时关闭 http://gyq.com/,观察 Prometheus 和 Alertmanager 是否发出告警。

也可以使用以下命令验证 Blackbox Exporter 是否正常工作默认端口是9115

curl "http://localhost:9115/probe?module=http_2xx&target=http://gyq.com/"
  • 1.

使用Prometheus监控网站是否正常打开_prometheus_04

使用Prometheus监控网站是否正常打开_钉钉告警_05

以上都是正常状态

接下来关闭网站模拟故障报警

使用Prometheus监控网站是否正常打开_普罗米修斯_06

查看界面Prometheus Web UI 发现probe_success值变成了1,状态为失败。

使用Prometheus监控网站是否正常打开_linux_07

再看看钉钉收到了监控告警

使用Prometheus监控网站是否正常打开_监控_08

最后启动Nginx

使用Prometheus监控网站是否正常打开_钉钉告警_09

这个时候查看界面Prometheus Web UI 发现恢复了正常

使用Prometheus监控网站是否正常打开_钉钉告警_10

查看钉钉也收到了恢复消息

使用Prometheus监控网站是否正常打开_普罗米修斯_11