前言
...........
工作原理
Filebeat由两个主要组件组成, prospectors和harvesters,他们一起协作tail文件并将事件发送给声明的输出。
harvester的职责是以行为单位读取文件,发送给输出,每个文件由不同的harvester读取。
prospector的职责是管理harvester并找到要读取的文件。
Filebeat当前支持log和stdin这两种prospector,每种prospector可以定义多次。
Filebeat在注册表(通过参数filebeat.registry_file声明,默认是${path.data}/registry)中记录了每个文件的状态,状态记录了上一次harvester的读取偏移量。prospector则记录了每个找到的文件的状态。Filebeat确保所有的事件都被发送至少一次。
配置文件
要使用Filebeat,我们需要在filebeat.yml配置文件的filebeat.prospectors下声明prospector,prospector不限定只有一个。例如:
filebeat.prospectors:
- type: log
paths:
- /var/log/*.log # 声明日志文件的绝对路径
fields:
type: syslog # 声明增加一个值为syslog的type字段到事件中
- type: log paths: - /var/log/messages - /var/log/*.log
output.logstash: hosts: ["localhost:5044"]
其他有用的选项还包括include_lines(仅读取匹配的行)、exclude_lines(不读取匹配的行)、exclude_files(排除某些文件)、tags、fields、fields_under_root、close_inactive(日志文件多久没有变化后自动关闭harvester,默认5分钟)、scan_frequency(prospector为harvester扫描新文件的频率,注意,因close_inactive自动关闭的也算新文件,默认为10s,不要低于1s)等,具体可见。
启动
默认情况下,filebeat运行在后台,要以前台方式启动,运行./filebeat -e。
解析多行消息
对于采用ELK作为应用日志来说,多行消息的友好展示是必不可少的,否则ELK的价值就大大打折了。要正确的处理多行消息,需要在filebeat.yml中设置multiline规则以声明哪些行属于一个事件。主要是由multiline.pattern、multiline.negate、multiline.match这三个参数决定。
比如,对于java日志而言,可以使用:
multiline.pattern: '^['
multiline.negate: true
multiline.match: after
或:
multiline.pattern: '^[[:space:]]+(at|.{3})|^Caused by:'
multiline.negate: false
multiline.match: after
这样,下面的日志就算一个事件了:
beat-logstash-some-name-832-2015.11.28] IndexNotFoundException[no such index]
at org.elasticsearch.cluster.metadata.IndexNameExpressionResolver$WildcardExpressionResolver.resolve(IndexNameExpressionResolver.java:566)
at org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.concreteIndices(IndexNameExpressionResolver.java:133)
at org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.concreteIndices(IndexNameExpressionResolver.java:77)
at org.elasticsearch.action.admin.indices.delete.TransportDeleteIndexAction.checkBlock(TransportDeleteIndexAction.java:75)
支持的输出
包括Elasticsearch、Logstash、Kafka、Redis、File、Console,都挺简单,可以参考https://www.elastic.co/guide/en/beats/filebeat/6.2/kafka-output.html
其它
Filebeat模块提供了一种更便捷的方式处理常见的日志格式,比如apache2、mysql等。从性质上来说,他就像spring boot,约定优于配置。具体可以参考https://www.elastic.co/guide/en/beats/filebeat/6.2/filebeat-modules-overview.html
Filebeat模块要求Elasticsearch 5.2以及之后的版本