概述
Filebeat是Beats家族的成员之一,是个轻量级的日志采集工具,通过收集日志信息,可以转发到 Elasticsearch 或者 Logstash进行索引存储。
工作原理
当启动Filebeat时,它会启动一个或多个输入来定位日志文件。对于每个日志文件,都将启动一个harvester(收割机),harvester读取单个日志文件以获取新的内容,并将新的日志数据发送到libbeat,libbeat聚合数据并发送到配置的output端,例如elasticsearch等等。
环境
CentOS 7.3
Filebeat 7.6.0
Elasticsearch 7.6.0
官网:https://www.elastic.co/cn/beats/filebeat
官方文档:https://www.elastic.co/guide/en/beats/filebeat/current/index.html
安装配置
#解压
tar -zxvf filebeat-7.6.0-linux-x86_64.tar.gz -C /usr/local
cd /usr/local/filebeat-7.6.0-linux-x86_64/
#创建个测试的配置文件(控制台输入输出)
touch test.yml
test.yml文件内容如下:
filebeat.inputs:
- type: stdin
enabled: true
setup.template.settings:
index.number_of_shards: 2
output.console:
pretty: true
enable: true
启动命令
./filebeat -e -c test.yml
- -e:输出到标准输出,默认输出到syslog和logs下
- -c:指定配置文件
- -d:输出debug信息
控制台测试
控制台输入test,会带出输出信息:
输出如下:
test
{
"@timestamp": "2020-08-27T03:33:53.893Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "7.6.0"
},
"ecs": {
"version": "1.4.0"
},
"host": {
"name": "cluster01"
},
"agent": {
"version": "7.6.0",
"type": "filebeat",
"ephemeral_id": "88c02c64-b16b-4e1e-9768-eb832433f43f",
"hostname": "cluster01",
"id": "7f16be81-9321-40e6-8867-124f0bee0da0"
},
"message": "test",
"log": {
"offset": 0,
"file": {
"path": ""
}
},
"input": {
"type": "stdin"
}
}
读取文件
mkdir logs
cd logs
#创建2个日志文件
touch 1.log 2.log
- 修改配置文件input部分,并重新启动
filebeat.inputs:
- type: log
enabled: true
paths: ["/usr/local/filebeat-7.6.0-linux-x86_64/logs/*.log"]
- 往log文件追加数据,查看filebeat控制台输出
#追加数据
echo "hello world" > 1.log
#输出如下
{
"@timestamp": "2020-08-27T04:14:03.103Z",
"@metadata": {
"beat": "filebeat",
"type": "_doc",
"version": "7.6.0"
},
"host": {
"name": "cluster01"
},
"agent": {
"id": "7f16be81-9321-40e6-8867-124f0bee0da0",
"version": "7.6.0",
"type": "filebeat",
"ephemeral_id": "b25e7ac5-5e98-4c4e-b53a-882a7c7bb419",
"hostname": "cluster01"
},
"log": {
"offset": 0,
"file": {
"path": "/usr/local/filebeat-7.6.0-linux-x86_64/logs/1.log"
}
},
"message": "hello world",
"input": {
"type": "log"
},
"ecs": {
"version": "1.4.0"
}
}
输出到elasticsearch
- 把output部分替换如下,重新启动filebeat
#elasticsearch主机地址
output.elasticsearch:
hosts: ["192.168.25.132:9200","192.168.25.133:9200","192.168.25.134:9200"]
- 继续往log文件追加数据
echo es-msg > 2.log
echo es-msg1 > 1.log
监听到信息,Filebeat连接ElasticSearch并创建索引
打开es-head,红框部分这2个索引,是Filebeat创建的,其中filebeat-7.6.0-2020.08.27-000001存放的是消息数据