Filebeat配置详解
Filebeat配置详解
Filebeat的配置主要可以反划分成三个部分:输入源、输出源、设置
配置格式
filebeat.inputs
- type: log
...
- type: filestream
...
output.elasticsearch:
...
orther_settings
一个简单的开始
Filebeat最主要的配置可以分为两个部分,input和output部分。如下所示
filebeat.inputs:
- type: log
#日志文件位置
paths:
- /var/log/*.log
output.elasticsearch:
#es连接信息
hosts: ["localhost:9200"]
protocol: "http"
username: "elastic"
password: "elastic"
elasticsearch会自动创建一个名字格式为"filebeat-%{[agent.version]}-%{+yyyy.MM.dd}-%{index_num}"的索引。比如:“filebeat-7.17.16-2024.07.01-000001”
默认使用es的索引声明周期策略配置(ILM配置)
#auto、false和true
setup.ilm.enabled: auto
#索引别名
setup.ilm.rollover_alias: "filebeat"
#索引增加策略
setup.ilm.pattern: "{now/d}-000001"
如果setup.ilm.enabled为false则需要配置模板设置:
setup.ilm.enabled: false
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.overwrite: false
filebeat.inputs配置
配置项参数
| 配置项 | 说明 |
|---|---|
| type | 定义日志数据收集类型 |
| enabled | 是否开启 |
| paths | 配置日志数据的位置 |
| fields | 自定义元数据字段,就是定义变量 |
日志收集基础配置
可以由一个和多个配置组成,配置如下:
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/*.log
- /var/log/apache/*.log
fields:
app: nginx
type: web
- type: log
enabled: true
paths:
- /var/log/message
fields:
app: message
type: system
日志多行合并
在默认的情况下收集日志一行为一条记录,有些情况下需要一条完整的日志包含多行数据,这个时候就可以配置多行匹配,在filebeat.inputs里面配置
filebeat.inputs:
- type: log
...
multiline.pattern: '^#\['
multiline.negate: true
multiline.match: after
multiline.pattern指定日志匹配的正则,可以根据自己需求定义相对应的正则。
output输出源配置
输出到Console
output.console:
enabled: true
pretty: true
codec.format:
string: '%{[message]}'
输出到文件
output.file:
enabled: true
path: "/var/log/filebeat"
filename: filebeat
rotate_every_kb: 1000
number_of_files: 7
输出到Elasticsearch
output.elasticsearch:
hosts: ["http://elasticsearch:9200"]
protocol: "http"
username: "username"
password: "password"
index: "filebeat-%{[agent.version]}-%{+yyyy.MM.dd}"
输出到Kafka
output.kafka:
hosts: ["kafka1:9092", "kafka2:9092", "kafka3:9092"]
topic: "filebeat"
partition.round_robin:
reachable_only: false
required_acks: 1
compression: gzip
输出到Logstash
output.logstash:
hosts: ["127.0.0.1:5044"]
根据变量写入不同索引
我们可以在定义多个输出的时候定义相同的变量但是不同的值来写入不同的索引。如下所示:
filebeat.inputs:
- type: log
...
fields:
app: nginx
type: web
- type: log
...
fields:
app: message
type: system
output.elasticsearch:
...
index: "%{[fields.app]}-%{[fields.type]}-%{[agent.version]}-%{+yyyy.MM.dd}"
根据条件写入不同索引
我们还可以通过message的内容判断是否包含特定内容来写入不同索引,如下所示:
output.elasticsearch:
hosts: ["http://localhost:9200"]
indices:
- index: "warning-%{[agent.version]}-%{+yyyy.MM.dd}"
when.contains:
message: "WARN"
- index: "error-%{[agent.version]}-%{+yyyy.MM.dd}"
when.contains:
message: "ERR"
一个完整的配置示例
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
fields:
app: nginx
type: access
multiline.pattern: '^#\['
multiline.negate: true
multiline.match: after
- type: log
enabled: true
paths:
- /var/log/nginx/error.log
fields:
app: nginx
type: error
multiline.pattern: '^#\['
multiline.negate: true
multiline.match: after
- type: filestream
enabled: true
paths:
- /var/log/*
scan_frequency: 10s
ignore_older: 24h
fields:
app: system
type: info
output.elasticsearch:
hosts: ["localhost:9200"]
protocol: "http"
username: "elastic"
password: "elastic"
index: "%{[fields.app]}-%{[fields.type]}-%{[agent.version]}-%{+yyyy.MM.dd}"
setup.ilm.enabled: false
setup.template.name: "nginx"
setup.template.pattern: "nginx-*"
setup.template.overwrite: false
processors:
- add_host_metadata:
when.not.contains.tags: forwarded