1 domain_exporter

domain_exporter 主要⽤来监控⽹站域名的过期时间。这对于企业和个⼈都是⽐较重要的,因为域名过期可能会导致⽹站⽆法访问,进⽽影响业务的正常运⾏。因此监控“域名的过期时间”就显得⽐较重要了。

domain_exporter 的⼯作逻辑有如下⼏步:

  1. 收集域名信息:通过WHOIS 协议收集“target参数定义的域名”信息。
  2. 解析域名信息:从收集到的数据中解析出域名的过期时间。
  3. 导出指标:将解析出的域名过期时间等信息格式化为Prometheus兼容的格式,⽽后通过 /probe 接⼝输出指标。

1.1 安装domain_exporter

1.1.1 下载domain_exporter

domain_exporter的github地址https://github.com/caarlos0/domain_exporter/releases
下载domain_exporter
#加速地址 
wget https://mirror.ghproxy.com/https://github.com/caarlos0/domain_exporter/releases/download/v1.23.0/domain_exporter_1.23.0_linux_amd64.tar.gz

1.1.2 解压domain_exporter

mkdir /app/module/domain_exporter
tar -xf domain_exporter_1.23.0_linux_amd64.tar.gz -C /app/module/domain_exporter/

1.1.3 配置domain_exporter启动⽂件

vim /usr/lib/systemd/system/domain_exporter.service
[Unit]
Description=domain_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
ExecStart=/app/module/domain_exporter/domain_exporter --bind=:9222
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=20s
Restart=always
[Install]
WantedBy=multi-user.target

1.1.4 启动domain_exporter

systemctl daemon-reload
systemctl start domain_exporter.service

1.1.5 通过 domain_exporter 获取域名过期时间

访问URL http://localhost:9222/probe?target=www.baidu.com 来获取特定域名(如www.baidu.com)的过期时间。

Prometheus监控之domain_exporter域名_github

1.2 配置Prometheus

1、修改Prometheus配置,使⽤relabel_configs做标签替换
  - job_name: "domain_exporter"
    metrics_path: /probe
    static_configs:
    - targets:
      - baidu.com
      - jd.com
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.137.131:9222 #domain_exporter address

2、重新加载Prometheus配置⽂件 
curl -X POST http://192.168.137.131:9090/-/reload

3、检查Prometheus的Status->Targets⻚⾯,验证 domain_exporter 是否已经成功纳⼊监控

Prometheus监控之domain_exporter域名_linux_02

1.3 domain常⽤指标与示例

1.3.1 domain_exporter相关指标

指标名称

指标类型

指标含义

domain_expiry_days

gauge

显示了域名距离过期还剩余的天数。

domain_probe_success

gauge

对域名检测是否成功。1 表示成功,0 表示失败。

domain_probe_duration_seconds


gauge

完成⼀次WHOIS 查询所需要的时间,单位为秒。如果延迟过⾼,说明连接WHOIS服务器的响应⽐较慢。

案例1:查询域名检测失败的站点 
sum(domain_probe_success) by (domain,instance,job) == 0

案例2:查询域名还剩100天过期的站点 
domain_expiry_days < 100

1.4 domain告警规则⽂件

1.4.1 告警规则⽂件

vim /app/module/prometheus/rules/domain_rules.yml
groups:
- name: domain告警规则文件
  rules:
  - alert: 域名即将过期 <100
    expr: domain_expiry_days < 100
    for: 5m
    labels:
      severity: warning
    annotations:
      summary: "域名即将过期 (实例 {{ $labels.instance }})"
      description: "域名 {{ $labels.domain }} 还有少于100天即将过期。当前剩余天数:{{ $value }}。"
  - alert: 域名即将过期<30
    expr: domain_expiry_days < 30
    for: 5m
    labels:
      severity: critical
    annotations:
      summary: "域名即将过期 (实例 {{ $labels.instance }})"
      description: "域名 {{ $labels.domain }} 还有少于30天即将过期。当前剩余天数:{{ $value }}。"  
  - alert: 域名检测失败
    expr: sum(domain_probe_success) by (domain, instance, job) == 0
    for: 5m
    labels:
      severity: critical
    annotations:
      summary: "域名检测失败 (实例 {{ $labels.instance }})"
      description: "域名 {{ $labels.domain }} 在 {{ $labels.instance }} 上的检测失败。当前值:{{ $value }}。"

1.4.2 检查rules语法

/app/module/prometheus/promtool check rules /app/module/prometheus/rules/domain_rules.yml

1.4.3 重新加载Prometheus

curl -X POST http://192.168.137.131:9090/-/reload

1.4.4 验证告警规则

Prometheus监控之domain_exporter域名_vim_03

1.5 导⼊domain_exporter图形

查看域名的状态,以及连通性和过期时间,导⼊ID 13924

Prometheus监控之domain_exporter域名_github_04