使用 Grafana Alloy 监控 Linux 服务器
Linux 操作系统生成各种指标和日志,您可以使用它们来监控硬件和操作系统的运行状况和性能。使用 Alloy,您可以收集指标和日志,将其转发到 Grafana 堆栈,并创建仪表盘来监控 Linux 服务器。
alloy-scenarios
仓库包含 Alloy 部署的完整示例。克隆仓库并使用示例来了解 Alloy 如何收集、处理和导出遥测信号。
在此示例场景中,Alloy 收集 Linux 指标并将其转发到 Loki 目标。
开始之前
确保您已具备以下条件
注意
您需要管理员权限才能运行
docker
命令。
克隆并部署示例
按照以下步骤克隆仓库并部署监控示例
克隆 Alloy 场景仓库
git clone https://github.com/grafana/alloy-scenarios.git
启动 Docker 部署 Grafana 堆栈
cd alloy-scenarios/linux docker compose up -d
验证 Docker 容器的状态
docker ps
(可选)探索完此示例后,停止 Docker 以关闭 Grafana 堆栈
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。
- 下载预配置的Linux 节点仪表盘的 JSON 文件。
- 转到 Dashboards > Import
- 上传 JSON 文件。
- 选择 Prometheus 数据源并点击导入
这个社区仪表盘提供了全面的系统指标,包括 CPU、内存、磁盘和网络使用情况。
理解 Alloy 配置
此示例使用 config.alloy
文件来配置 Alloy 组件以进行指标和日志记录。您可以在克隆的仓库中找到此文件,路径为 alloy-scenarios/linux/
。
配置指标
此示例中的指标配置需要四个组件
prometheus.exporter.unix
discovery.relabel
prometheus.scrape
prometheus.remote_write
prometheus.exporter.unix
prometheus.exporter.unix
组件暴露硬件和 Linux 内核指标。这是您配置用于收集 Linux 系统指标的主要组件。
在此示例中,此组件需要以下参数
disable_collectors
:禁用特定收集器以减少不必要的开销。enable_collectors
:启用meminfo
收集器。fs_types_exclude
:要忽略的文件系统类型的正则表达式。mount_points_exclude
:要忽略的挂载点类型的正则表达式。mount_timeout
:等待挂载响应的时间,超过此时间则将其标记为过期。ignored_devices
:要忽略的虚拟和容器网络接口的正则表达式。device_exclude
:要排除的虚拟和容器网络接口的正则表达式。
prometheus.exporter.unix "integrations_node_exporter" {
disable_collectors = ["ipvs", "btrfs", "infiniband", "xfs", "zfs"]
enable_collectors = ["meminfo"]
filesystem {
fs_types_exclude = "^(autofs|binfmt_misc|bpf|cgroup2?|configfs|debugfs|devpts|devtmpfs|tmpfs|fusectl|hugetlbfs|iso9660|mqueue|nsfs|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|selinuxfs|squashfs|sysfs|tracefs)$"
mount_points_exclude = "^/(dev|proc|run/credentials/.+|sys|var/lib/docker/.+)($|/)"
mount_timeout = "5s"
}
netclass {
ignored_devices = "^(veth.*|cali.*|[a-f0-9]{15})$"
}
netdev {
device_exclude = "^(veth.*|cali.*|[a-f0-9]{15})$"
}
}
此组件为 prometheus.scrape
提供 prometheus.exporter.unix.integrations_node_exporter.output
目标。
discovery.relabel
实例和作业标签
此配置中有两个 discovery.relabel
组件。此 discovery.relabel
组件会将从 node_exporter
获取的实例和作业标签替换为机器主机名以及所有指标的标准作业名称。
在此示例中,此组件需要以下参数
targets
:要重新标记的目标。source_labels
:选择用于重新标记的标签列表。规则提取实例和作业标签。replacement
:替换源标签的值。规则将目标标签设置为constants.hostname
和integrations/node_exporter
。
discovery.relabel "integrations_node_exporter" {
targets = prometheus.exporter.unix.integrations_node_exporter.targets
rule {
target_label = "instance"
replacement = constants.hostname
}
rule {
target_label = "job"
replacement = "integrations/node_exporter"
}
}
discovery.relabel
用于 systemd journal 日志
此 discovery.relabel
组件定义 systemd journal 日志的重新标记规则。在此示例中,此组件需要以下参数
targets
:要重新标记的目标。不修改任何目标,因此targets
参数是一个空数组。source_labels
:选择用于重新标记的标签列表。规则提取 systemd 单元、ID、传输类型和日志优先级。target_label
:写入目标的标签。规则将目标标签设置为unit
、boot_id
、transport
和level
。
discovery.relabel "logs_integrations_integrations_node_exporter_journal_scrape" {
targets = []
rule {
source_labels = ["__journal__systemd_unit"]
target_label = "unit"
}
rule {
source_labels = ["__journal__boot_id"]
target_label = "boot_id"
}
rule {
source_labels = ["__journal__transport"]
target_label = "transport"
}
rule {
source_labels = ["__journal_priority_keyword"]
target_label = "level"
}
}
prometheus.scrape
prometheus.scrape
组件抓取 node_exporter
指标并将其转发到接收器。在此示例中,此组件需要以下参数
targets
:从中抓取指标的目标。使用带有discovery.relabel
组件标签的目标。forward_to
:转发指标的目标。将抓取的指标发送到重新标记组件。scrape_interval
:抓取目标的频率。
prometheus.scrape "integrations_node_exporter" {
scrape_interval = "15s"
targets = discovery.relabel.integrations_node_exporter.output
forward_to = [prometheus.remote_write.local.receiver]
}
prometheus.remote_write
prometheus.remote_write
组件将指标发送到 Prometheus 服务器。在此示例中,此组件需要以下参数
url
:定义发送指标的完整 URL 端点。
此组件为 prometheus.scrape
提供 prometheus.remote_write.local.receiver
目标。
prometheus.remote_write "local" {
endpoint {
url = "http://prometheus:9090/api/v1/write"
}
}
此组件为 prometheus.scrape
提供 prometheus.remote_write.local.receiver
目标。
配置日志记录
此示例中的日志配置需要四个组件
loki.source.journal
local.file_match
loki.source.file
loki.write
loki.source.journal
loki.source.journal
组件从 systemd journal 收集日志并将其转发到 Loki 接收器。在此示例中,此组件需要以下参数
max_age
:仅收集过去 24 小时的日志。relabel_rules
:应用于日志条目的重新标记规则。forward_to
:将日志发送到本地 Loki 实例。
loki.source.journal "logs_integrations_integrations_node_exporter_journal_scrape" {
max_age = "24h0m0s"
relabel_rules = discovery.relabel.logs_integrations_integrations_node_exporter_journal_scrape.rules
forward_to = [loki.write.local.receiver]
}
local.file_match
local.file_match
组件使用 glob 模式发现本地文件系统上的文件。在此示例中,此组件需要以下参数
path_targets
:要展开的目标__address__
:以本地主机为目标进行日志收集。__path__
:收集标准系统日志。instance
:添加一个包含主机名的实例标签。job
:为日志添加一个作业标签。
local.file_match "logs_integrations_integrations_node_exporter_direct_scrape" {
path_targets = [{
__address__ = "localhost",
__path__ = "/var/log/{syslog,messages,*.log}",
instance = constants.hostname,
job = "integrations/node_exporter",
}]
}
loki.source.file
loki.source.file
组件从文件中读取日志条目并将其转发到其他 Loki 组件。在此示例中,此组件需要以下参数
targets
:要从中读取日志的文件列表。forward_to
:发送日志条目的接收器列表。
loki.source.file "logs_integrations_integrations_node_exporter_direct_scrape" {
targets = local.file_match.logs_integrations_integrations_node_exporter_direct_scrape.targets
forward_to = [loki.write.local.receiver]
}
loki.write
loki.write
组件将日志写入 Loki 目标。在此示例中,此组件需要以下参数
url
:定义发送日志到 Loki 的完整 URL 端点。
loki.write "local" {
endpoint {
url = "http://loki:3100/loki/api/v1/push"
}
}
配置 livedebugging
livedebugging
将组件的实时数据直接流式传输到 Alloy UI。有关此功能的更多详细信息,请参考故障排除文档。
livedebugging
默认情况下,livedebugging
是禁用的。通过 livedebugging
配置块显式启用它,以使调试数据在 Alloy UI 中可见。您可以使用此块的空配置,Alloy 将使用默认值。
livedebugging{}