菜单
开源

使用 Grafana Alloy 监控 Microsoft Windows 服务器和桌面

Microsoft Windows 提供了性能监视器和事件查看器等工具来跟踪系统性能指标和事件日志。使用 Alloy,您可以收集性能指标和事件日志,将其转发到 Grafana 技术栈,并创建仪表盘来监控 Windows 的性能和事件。

alloy-scenarios 仓库包含完整的 Alloy 部署示例。克隆仓库并使用示例来了解 Alloy 如何收集、处理和导出遥测信号。

在此示例场景中,Alloy 收集 Windows 性能指标和事件日志,并将其转发到 Loki 目标。

开始之前

确保已安装以下软件

  • Docker
  • Git
  • 一台 Windows Server 或桌面。本示例监控一台运行 Windows 的计算机。
  • Windows 管理员权限。安装 Alloy 并配置其收集指标和日志需要管理员权限。

注意

运行 docker 命令需要管理员权限。

克隆并部署示例

按照以下步骤克隆示例仓库并部署监控示例

  1. 克隆 Alloy 示例仓库。

    shell
    git clone https://github.com/grafana/alloy-scenarios.git
  2. 启动 Docker 以部署 Grafana 技术栈。

    shell
    cd alloy-scenarios/windows
    docker compose up -d

    验证 Docker 容器的状态

    shell
    docker ps
  3. 在 Windows 上安装 Alloy

  4. 将默认的 config.alloy 文件替换为 alloy-scenarios/windows 目录中包含的预配置 config.alloy 文件。有关如何停止和启动 Alloy 服务的详细步骤,请参阅配置 Windows 上的 Alloy

    1. 停止 Alloy 服务。
    2. C:\Program Files\GrafanaLabs\Alloy 中的 config.alloy 文件替换为 alloy-scenarios/windows 目录中的 config.alloy 文件。
    3. 启动 Alloy 服务。
  5. (可选)要从远程计算机访问 Alloy UI,请将 --server.http.listen-addr=0.0.0.0:12345 添加到 Alloy 运行时参数。有关如何更新此命令行参数的详细步骤,请参阅将 UI 暴露给其他机器。此步骤会使 Alloy UI 在 http://<WINDOWS_IP_ADDRESS>:12345 可用。

  6. (可选)探索完本示例后,停止 Docker 以关闭 Grafana 技术栈。

    shell
    docker compose down

监控和可视化您的数据

使用 Grafana 监控您的部署健康状况并可视化您的数据。

监控 Alloy

要监控 Alloy 部署的健康状况,请打开浏览器并访问 https://:12345

有关 Alloy UI 的更多信息,请参阅调试 Grafana Alloy

可视化您的数据

要探索指标,请打开浏览器并访问 https://:3000/explore/metrics

要使用 Grafana Logs Drilldown,请打开浏览器并访问 https://:3000/a/grafana-lokiexplore-app

要创建仪表盘来可视化指标和日志,请打开浏览器并访问 https://:3000/dashboards

了解 Alloy 配置

本示例使用 config.alloy 文件配置 Alloy 组件以进行指标和日志记录。您可以在克隆的仓库中找到 config.alloy 文件,路径为 alloy-scenarios/windows/

配置中包含 livedebugging,用于将实时数据流式传输到 Alloy UI。

配置指标

本示例中的指标配置需要三个组件

  • prometheus.exporter.windows
  • prometheus.scrape
  • prometheus.remote_write

prometheus.exporter.windows

prometheus.exporter.windows 组件暴露基于 Windows 系统的硬件和操作系统指标。在此示例中,该组件需要以下参数

  • enabled_collectors: 要启用的收集器列表。
alloy
prometheus.exporter.windows "default" {
  enabled_collectors = ["cpu","cs","logical_disk","net","os","service","system", "memory", "scheduled_task", "tcp"]
}

prometheus.scrape

prometheus.scrape 组件抓取 Windows 指标并将其转发给接收器。在此示例中,该组件需要以下参数

  • targets: 抓取指标的目标。
  • forward_to: 转发指标的目标地址。
alloy
prometheus.scrape "example" {
  targets    = prometheus.exporter.windows.default.targets
  forward_to = [prometheus.remote_write.demo.receiver]
}

prometheus.remote_write

prometheus.remote_write 组件将指标发送到 Prometheus 服务器。在此示例中,该组件需要以下参数

  • url: 定义发送指标的完整 URL 端点。
alloy
prometheus.remote_write "demo" {
  endpoint {
    url = "https://:9090/api/v1/write"
  }
}

配置日志

本示例中的日志配置需要三个组件

  • loki.source.windowsevent
  • loki.process
  • loki.write

loki.source.windowsevent

loki.source.windowsevent 组件读取 Windows 事件日志中的事件并将其转发给其他 Loki 组件。在此示例中,该组件需要以下参数

  • eventlog_name: 要读取的事件日志。
  • use_incoming_timestamp: 为日志分配当前时间戳。
  • forward_to: 发送日志条目的接收器列表。
alloy
loki.source.windowsevent "application"  {
    eventlog_name = "Application"
    use_incoming_timestamp = true
    forward_to = [loki.process.endpoint.receiver]
}
alloy
loki.source.windowsevent "System"  {
    eventlog_name = "System"
    use_incoming_timestamp = true
    forward_to = [loki.process.endpoint.receiver]
}

loki.process

loki.process 组件从其他 Loki 组件接收日志条目,应用一个或多个处理阶段,并将结果转发给接收器列表。在此示例中,该组件需要以下参数

  • forward_to: 发送日志条目的接收器列表。
  • expressions: 定义提取数据名称及其值的键值对。
  • values: 定义要设置的标签以及如何查找它们的键值对。
  • source: 从提取的值映射中用于时间戳的名称。
  • overwrite_existing: 覆盖现有的提取数据字段。
alloy
loki.process "endpoint" {
  forward_to = [loki.write.endpoint.receiver]
  stage.json {
      expressions = {
          message = "",
          Overwritten = "",
          source = "",
          computer = "",
          eventRecordID = "",
          channel = "",
          component_id = "",
          execution_processId = "",
          execution_processName = "",
      }
  }

  stage.structured_metadata {
      values = {
          "eventRecordID" = "",
          "channel" = "",
          "component_id" = "",
          "execution_processId" = "",
          "execution_processName" = "",
      }
  }

  stage.eventlogmessage {
      source = "message"
      overwrite_existing = true
  }

  stage.labels {
      values = {
          "service_name" = "source",
      }
  }

  stage.output {
    source = "message"
  }

}

loki.write

loki.write 组件将日志写入到 Loki 目标。在此示例中,该组件需要以下参数

  • url: 定义 Loki 中发送日志的完整 URL 端点。
alloy
loki.write "endpoint" {
    endpoint {
        url ="https://:3100/loki/api/v1/push"
    }
}

配置 `livedebugging`

Livedebugging 将实时数据从组件直接流式传输到 Alloy UI。有关此功能的更多详细信息,请参阅故障排除文档

livedebugging

默认情况下,livedebugging 是禁用的。通过 livedebugging 配置块显式启用它,以便在 Alloy UI 中看到调试数据。

alloy
livedebugging {
  enabled = true
}