Blackbox Exporter

Tài liệu này hướng dẫn cấu hình Blackbox Exporter để thu thập metrics giám sát tình trạng hoạt động của các endpoints qua các giao thức như HTTP, TCP, ICMP, DNS

1. Chuẩn bị

/bin/sh -c "$(curl -fsSL https://s3hn.smartcloud.vn/agent/alloy/linux/releases/v1.8.3/install-linux.sh)"

2. Cấu hình tích hợp

Tại đường dẫn /etc/alloy/config.alloy, chỉnh sửa Alloy để tích hợp Blackbox Exporter:

sudo vi /etc/alloy/config.alloy

2.1 Tạo file module.yml

File module.yml cho phép cấu hình chi tiết cách Blackbox Exporter thực hiện kiểm tra, gồm:

  • Giao thức sử dụng (HTTP, TCP, ICMP, DNS)
  • Phương thức (GET/POST)
  • Kiểu truy vấn DNS

Ví dụ

modules:
  <module_name_1>:
    prober: <protocol>
    timeout: <timeout>
    <các tùy chọn cụ thể cho từng loại prober>

  <module_name_2>:
    prober: <protocol>
    timeout: <timeout>
    <các tùy chọn cụ thể cho từng loại prober>

Ý nghĩa các trường trong file module.yml

TrườngÝ nghĩaGiá trị mẫu
module_nameTên module dùng trong file alloyicmp_ping
protocolGiao thức sử dụngicmp
timeoutThời gian tối đa cho một probe10s

Ví dụ một file cấu hình cụ thể trông như sau:

modules:
  icmp_ping:
    prober: icmp
    timeout: 5s

  tcp_connect:
    prober: tcp
    timeout: 5s
    tcp:
      preferred_ip_protocol: ip4

  dns_check:
    prober: dns
    timeout: 5s
    dns:
      query_name: "vnpt.vn"
      query_type: "A"

  http_get:
    prober: http
    timeout: 10s
    http:
      method: GET
      valid_status_codes: [200]
      preferred_ip_protocol: ip4

  http_post:
    prober: http
    timeout: 10s
    http:
      method: POST
      valid_status_codes: [200]
      preferred_ip_protocol: ip4

2.2 Cấu hình alloy để thu thập metrics

Thay các trường ở file cấu hình sau, trong đó:

TrườngÝ nghĩaGiá trị mẫu
urlEndpoint của dịch vụ APM trên giao diện Consolehttps://metrics-0qcs3dsvvva.apm.vnptplatform.vn/api/v1/push
AuthorizationToken của dịch vụ APM trên giao diện Consolea5130b91-ff9b-41ef-8722-9d923a574123
tenantTên dịch vụ APMapm-vnptcloud
module_fileĐường dẫn chứa định nghĩa giao thức/etc/alloy/module.yml
target_addressEndpoint cần giám sátfacebook.com
moduleTên module đã được cấu hình trong module_filetcp_connect
labelThêm label cho metric thu đượcpurpose = "http"
target_nameTên target muốn lấy metricsping_google
descriptionMô tả về target của blackboxping google.com
methodPhương thức sử dụngGET
domainTên miền cần phân giảivnpt.com
logging {
  level  = "warn"
  format = "logfmt"
}

prometheus.remote_write "remote_receiver" {
  endpoint {
    url = "<url>"
    headers = {
      Authorization = "<token>",
    }
  }
  external_labels = {
    tenant = "<tenant>",
  }
}

prometheus.exporter.blackbox "ping_monitor" {
  config_file = "/etc/alloy/blackexporter-module.yml"

  target {
    name = "<target_name_1>"
    address = "<target_address_1>"
    module  = "<module>"
    labels = { purpose = "ping", target = "<target_address>", description = "<description>" }
  }

  target {
    name = "<target_name_2>"
    address = "<target_address_2>"
    module  = "<module>"
    labels = { purpose = "ping", target = "<target_address>", description = "<description>" }
  }
}

prometheus.exporter.blackbox "tcp_ports" {
  config_file = "/etc/alloy/blackexporter-module.yml"

  target {
    name = "<target_name_3>"
    address = "<target_address_3>"
    module  = "<module>"
    labels = { purpose = "tcp",target="<target_address>",port = "443", description = "<description>" }
  }

  target {
    name = "<target_name_4>"
    address = "<target_address_4>"
    module  = "<module>"
    labels = { purpose = "tcp",target="<target_address>", port = "465", description = "<description>" }
  }
}

prometheus.exporter.blackbox "dns_monitor" {
  config_file = "/etc/alloy/blackexporter-module.yml"

  target {
    name = "<target_name_5>"
    address = "1.1.1.1"
    module  = "<module>"
    labels = { domain = "<domain_name>", purpose = "dns", description = "<description>" }
  }

  target {
    name = "<target_name_6>"
    address = "1.1.1.1"
    module  = "<module>"
    labels = { domain = "<domain_name>", purpose = "dns", description = "<description>" }
  }
}

prometheus.exporter.blackbox "http_monitor" {
  config_file = "/etc/alloy/blackexporter-module.yml"

  target {
    name = "<target_name_7>"
    address = "<target_address_7>"
    module  = "<module>"
    labels = { method = "<method>", target = "<target_address>", purpose = "http", description = "<description>" }
  }

  target {
    name = "<target_name_8>"
    address = "<target_address_8>"
    module  = "<module>"
    labels = { method = "<method>", target = "<target_address>", purpose = "http", description = "<description>" }
  }
}

prometheus.scrape "blackbox_all" {
  targets    =  concat(prometheus.exporter.blackbox.dns_monitor.targets, prometheus.exporter.blackbox.tcp_ports.targets, prometheus.exporter.blackbox.ping_monitor.targets, prometheus.exporter.blackbox.http_monitor.targets)
  scrape_interval = "5m"
  forward_to = [prometheus.remote_write.remote_receiver.receiver]
}

2.3 Cài đặt

Thực hiện lệnh sau:

sudo systemctl status alloy

Kết quả nếu thành công:

 alloy.service - Vendor-agnostic OpenTelemetry Collector distribution with programmable pipelines
    Loaded: loaded (/lib/systemd/system/alloy.service; enabled; vendor preset: enabled)
    Active: active (running) since Wed 2025-04-23 10:44:50 +07; 5s ago
      Docs: https://grafana.com/docs/alloy
  Main PID: 3119454 (alloy)
     Tasks: 13 (limit: 9385)
    Memory: 61.3M
       CPU: 848ms
    CGroup: /system.slice/alloy.service
            └─3119454 /usr/bin/alloy run --server.http.listen-addr=0.0.0.0:30000 --storage.path=/var/lib/alloy/data /etc/alloy/config.alloy

Nếu chưa thành công, thực hiện lệnh sau để xem log của Alloy:

sudo journalctl -u alloy

Sau đó, sửa lại giá trị của cấu hình và restart lại Alloy:

sudo systemctl restart alloy

2.5 Kiểm tra tích hợp

Tham khảo hướng dẫn tích hợp dashboard, kiểm tra metrics Blackbox Exporter trên giao diện Dashboard:

Một số dashboard mẫu khi tích hợp thành công: