- Filebeat 是 Elastic Stack 中的一个轻量级日志转发器,主要用于收集和转发日志数据。
- Filebeat 作为代理安装在您的服务器上,可以监控您指定的日志文件或位置,收集日志事件,并将其转发到 Elasticsearch 或 Logstash 进行索引。
一、原理
How Filebeat works | Filebeat Reference [8.15] | Elastic
Filebeat 的工作原理如下:启动 Filebeat 时,它会启动一个或多个输入,这些输入会查找您为日志数据指定的位置。对于 Filebeat 找到的每个日志,Filebeat 都会启动一个采集器。每个采集器都会读取新内容的单个日志,并将新的日志数据发送到 libbeat,libbeat 会聚合事件并将聚合数据发送到您为 Filebeat 配置的输出。
Filebeat 的两个主要组件
- Harvester 负责读取单个文件的内容。Harvester 逐行读取每个文件,并将内容发送到输出。为每个文件启动一个 harvester。Harvester 负责打开和关闭文件,这意味着当 Harvester 运行时,文件描述符保持打开状态。
- input 负责管理采集器并查找所有要读取的源。
Filebeat 如何保持文件的状态
Filebeat 会保留每个文件的状态,并经常将状态刷新到注册表文件中的磁盘。该状态用于记住 Harvester 读取的最后一个偏移量,并确保发送所有 log 行。如果无法访问输出(例如 Elasticsearch 或 Logstash),Filebeat 会跟踪发送的最后一行,并在输出再次可用时继续读取文件。当 Filebeat 运行时,每个输入的状态信息也会保留在内存中。当 Filebeat 重新启动时,注册表文件中的数据用于重建状态,并且 Filebeat 在最后一个已知位置继续每个采集器。
二、安装及配置
下载安装
Filebeat quick start: installation and configuration | Filebeat Reference [8.15] | Elastic
下载解压即可。
windows 下启动命令:
.\filebeat.exe
## 这个命令启动无任何输出,添加 -e 可输出相关日志
配置
Configure Filebeat | Filebeat Reference [8.15] | Elastic
默认配置模版(仅保留部分)
###################### Filebeat Configuration Example #########################
# ============================== Filebeat inputs ===============================
filebeat.inputs:
# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input-specific configurations.
# filestream is an input for collecting log messages from files.
- type: filestream
# Unique ID among all inputs, an ID is required.
id: my-filestream-id
# Change to true to enable this input configuration.
enabled: false
# Paths that should be crawled and fetched. Glob based paths.
paths:
- /var/log/*.log
#- c:\programdata\elasticsearch\logs\*
# Exclude lines. A list of regular expressions to match. It drops the lines that are
# matching any regular expression from the list.
# Line filtering happens after the parsers pipeline. If you would like to filter lines
# before parsers, use include_message parser.
#exclude_lines: ['^DBG']
# Include lines. A list of regular expressions to match. It exports the lines that are