Tài liệu hướng dẫn cài đặt, cấu hình Alloy để thu thập metrics, logs từ máy chủ Windows.

1. Chuẩn bị

  • Đã cài đặt alloy trên Windows Server
  • Chạy lệnh sau để kiểm tra xem alloy đã được cài trên Windows Server hay chưa:
sc query "alloy" | find "STATE" 
  • Nếu có kết quả như sau thì Windows Server đã được cài đặt alloy:

powershell -ExecutionPolicy Bypass -Command "iwr -UseBasicParsing https://s3hn.smartcloud.vn/agent/alloy/windows/install-windows.ps1 | iex"

2. Cấu hình Alloy

Chỉnh sửa Alloy để tích hợp giám sát Windows Server

2.1 Thu thập Metrics

Thay các trường trong 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
enabled_collectorsDanh sách các đối số muốn bật thu thập metrics"cpu", "cs", "logical_disk", "net", "os", "service", "system", "time"

 

Sửa cấu hình thu thập metrics:

logging {
  level  = "warn"
  format = "logfmt"
}
prometheus.remote_write "metrics_integrations_windows" {
  endpoint {
    url = "<endpoint dịch vụ APM>"
    headers = {
      "Authorization" = "<token dịch vụ APM>",
    }
  }
}
prometheus.exporter.windows "metrics_integrations_windows" {
  enabled_collectors = [<danh sách các đối số muốn bật>]
}
discovery.relabel "metrics_integrations_windows" {
  targets = prometheus.exporter.windows.metrics_integrations_windows.targets

  rule {
    target_label = "job"
    replacement  = "integrations/windows_exporter"
  }

  rule {
    target_label = "instance"
    replacement  = constants.hostname
  }
}
prometheus.scrape "metrics_integrations_windows" {
  targets    = discovery.relabel.metrics_integrations_windows.output
  forward_to = [prometheus.relabel.metrics_integrations_windows.receiver]
  job_name   = "integrations/windows_exporter"
  scrape_interval = "15s"
  scrape_timeout  = "10s"
}
prometheus.relabel "metrics_integrations_windows" {
  forward_to = [prometheus.remote_write.metrics_integrations_windows.receiver]

  rule {
    source_labels = ["volume"]
    regex         = "HarddiskVolume.*"
    action        = "drop"
  }
}

2.1 Thu thập Logs

Thay các trường trong 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

Sửa cấu hình thu thập logs:

logging {
  level  = "warn"
  format = "logfmt"
}

loki.write "logs_integrations_windows" {
  endpoint {
    url ="<endpoint dịch vụ APM>"
    headers = {
      "Authorization" = "<token dịch vụ APM>",
    }
  }
}

loki.process "logs_integrations_windows_exporter_application" {
  forward_to = [loki.write.logs_integrations_windows.receiver]

  stage.json {
    expressions = {
      level  = "levelText",
      source = "source",
    }
  }

  stage.labels {
    values = {
      level  = "",
      source = "",
    }
  }
}
loki.relabel "logs_integrations_windows_exporter_application" {
  forward_to = [loki.process.logs_integrations_windows_exporter_application.receiver]

  rule {
    source_labels = ["computer"]
    target_label  = "agent_hostname"
  }
}
loki.source.windowsevent "logs_integrations_windows_exporter_application" {
  locale                 = 1033
  eventlog_name          = "Application"
  bookmark_path          = "./bookmarks-app.xml"
  poll_interval          = "5s"
  use_incoming_timestamp = true
  forward_to             = [loki.relabel.logs_integrations_windows_exporter_application.receiver]
  labels                 = {
    instance = constants.hostname,
    job      = "integrations/windows_exporter",
  }
}
loki.process "logs_integrations_windows_exporter_system" {
  forward_to = [loki.write.logs_integrations_windows.receiver]

  stage.json {
    expressions = {
      level  = "levelText",
      source = "source",
    }
  }

  stage.labels {
    values = {
      level  = "",
      source = "",
    }
  }
}
loki.relabel "logs_integrations_windows_exporter_system" {
  forward_to = [loki.process.logs_integrations_windows_exporter_system.receiver]

  rule {
    source_labels = ["computer"]
    target_label  = "agent_hostname"
  }
}
loki.source.windowsevent "logs_integrations_windows_exporter_system" {
  locale                 = 1033
  eventlog_name          = "System"
  bookmark_path          = "./bookmarks-sys.xml"
  poll_interval          = "5s"
  use_incoming_timestamp = true
  forward_to             = [loki.relabel.logs_integrations_windows_exporter_system.receiver]
  labels                 = {
    instance = constants.hostname,
    job      = "integrations/windows_exporter",
  }
}

  Lưu ý

Khi cần bổ sung một Windows Event Log mới, hãy cấu hình block theo hướng dẫn sau:

loki.relabel "logs_integrations_windows_exporter_<log_name>" {
  forward_to = [loki.write.logs_integrations_windows.receiver]

  rule {
    source_labels = ["computer"]
    target_label  = "agent_hostname"
  }
}

loki.source.windowsevent "logs_integrations_windows_exporter_<log_name>" {
  locale                 = 1033
  eventlog_name          = "<EventLogName>"
  bookmark_path          = "./bookmarks-<log_name>.xml"
  poll_interval          = "5s"
  use_incoming_timestamp = true

  forward_to = [loki.relabel.logs_integrations_windows_exporter_<log_name>.receiver]

  labels = {
    instance = constants.hostname
    job      = "integrations/windows_exporter"
  }
}

2.3 Thu thập Metrics, Logs

Thay các trường trong 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
enabled_collectorsDanh sách các đối số muốn bật thu thập metrics"cpu", "cs", "logical_disk", "net", "os", "service", "system", "time"

Sửa cấu hình thu thập metrics, logs:

logging {
  level  = "warn"
  format = "logfmt"
}
prometheus.remote_write "metrics_integrations_windows" {
  endpoint {
    url = "<endpoint dịch vụ APM>"
    headers = {
      "Authorization" = "<token dịch vụ APM>",
    }
  }
}
loki.write "logs_integrations_windows" {
  endpoint {
    url ="<endpoint dịch vụ APM>"
    headers = {
      "Authorization" = "<token dịch vụ APM>",
    }
  }
}
prometheus.exporter.windows "metrics_integrations_windows" {
  enabled_collectors = [<danh sách các đối số muốn bật>]
}
discovery.relabel "metrics_integrations_windows" {
  targets = prometheus.exporter.windows.metrics_integrations_windows.targets

  rule {
    target_label = "job"
    replacement  = "integrations/windows_exporter"
  }

  rule {
    target_label = "instance"
    replacement  = constants.hostname
  }
}
prometheus.scrape "metrics_integrations_windows" {
  targets    = discovery.relabel.metrics_integrations_windows.output
  forward_to = [prometheus.relabel.metrics_integrations_windows.receiver]
  job_name   = "integrations/windows_exporter"
  scrape_interval = "15s"
  scrape_timeout  = "10s"
}
prometheus.relabel "metrics_integrations_windows" {
  forward_to = [prometheus.remote_write.metrics_integrations_windows.receiver]

  rule {
    source_labels = ["volume"]
    regex         = "HarddiskVolume.*"
    action        = "drop"
  }
}
loki.process "logs_integrations_windows_exporter_application" {
  forward_to = [loki.write.logs_integrations_windows.receiver]

  stage.json {
    expressions = {
      level  = "levelText",
      source = "source",
    }
  }

  stage.labels {
    values = {
      level  = "",
      source = "",
    }
  }
}
loki.relabel "logs_integrations_windows_exporter_application" {
  forward_to = [loki.process.logs_integrations_windows_exporter_application.receiver]

  rule {
    source_labels = ["computer"]
    target_label  = "agent_hostname"
  }
}
loki.source.windowsevent "logs_integrations_windows_exporter_application" {
  locale                 = 1033
  eventlog_name          = "Application"
  bookmark_path          = "./bookmarks-app.xml"
  poll_interval          = "0s"
  use_incoming_timestamp = true
  forward_to             = [loki.relabel.logs_integrations_windows_exporter_application.receiver]
  labels                 = {
    instance = constants.hostname,
    job      = "integrations/windows_exporter",
  }
}
loki.process "logs_integrations_windows_exporter_system" {
  forward_to = [loki.write.logs_integrations_windows.receiver]

  stage.json {
    expressions = {
      level  = "levelText",
      source = "source",
    }
  }

  stage.labels {
    values = {
      level  = "",
      source = "",
    }
  }
}
loki.relabel "logs_integrations_windows_exporter_system" {
  forward_to = [loki.process.logs_integrations_windows_exporter_system.receiver]

  rule {
    source_labels = ["computer"]
    target_label  = "agent_hostname"
  }
}
loki.source.windowsevent "logs_integrations_windows_exporter_system" {
  locale                 = 1033
  eventlog_name          = "System"
  bookmark_path          = "./bookmarks-sys.xml"
  poll_interval          = "0s"
  use_incoming_timestamp = true
  forward_to             = [loki.relabel.logs_integrations_windows_exporter_system.receiver]
  labels                 = {
    instance = constants.hostname,
    job      = "integrations/windows_exporter",
  }
}

3. Cài đặt

Mở Command Prompt chạy với quyền administrator:

sc start alloy

4. Kiểm tra

4.1 Kiểm tra dịch vụ

Kiểm tra hệ thống đã chạy thành công hay chưa:

sc query "alloy" | find "STATE" 

Trạng thái alloy thành công sẽ nhận được:

4.2 Kiểm tra tích hợp

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

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