【Beats03】企业级日志分析系统ELK之Filebeat 收集日志及案例二

利用 Filebeat 收集日志

Filebeat 是用于转发和集中日志数据的轻量级传送程序。作为服务器上的代理安装,Filebeat监视您指定 的日志文件或位置,收集日志事件,并将它们转发到Elasticsearch或Logstash进行索引。

Logstash 也可以直接收集日志,但需要安装JDK并且会占用至少500M 以上的内存

生产一般使用filebeat代替logstash, 基于go开发,部署方便,重要的是只需要10M多内存,比较节约资源.

filebeat 支持从日志文件,Syslog,Redis,Docker,TCP,UDP,标准输入等读取数据,对数据做简单处理,再输 出至Elasticsearch,logstash,Redis,Kafka等

Filebeat的工作方式如下:

  • 启动Filebeat时,它将启动一个或多个输入源,这些输入将在为日志数据指定的位置中查找。

  • 对于Filebeat所找到的每个日志,Filebeat都会启动收集器harvester进程。

  • 每个收集器harvester都读取一个日志以获取新内容,并将新日志数据发送到libbeat

  • libbeat会汇总事件并将汇总的数据发送到为Filebeat配置的输出。

        

Filebeat 官方说明

https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-overview.html
https://www.elastic.co/guide/en/beats/filebeat/current/configuring-howto-filebeat.html 

输入和输入官方说明

https://www.elastic.co/guide/en/beats/filebeat/current/configuration-filebeat-options.html
https://www.elastic.co/guide/en/beats/filebeat/current/configuring-output.html

注意: Filebeat 支持多个输入,但不支持同时有多个输出,如果多输出,会报错如下

Exiting: error unpacking config data: more than one namespace configured 
accessing 'output' (source:'/etc/filebeat/stdout_file.yml')

安装 Filebeat 和配置说明

下载链接

Download Filebeat • Lightweight Log Analysis | Elastic

安装 Filebeat
#新版下载
[root@elk-web1 ~]#wget 
https://mirrors.tuna.tsinghua.edu.cn/elasticstack/8.x/apt/pool/main/f/filebeat/filebeat-8.6.1-amd64.deb
#旧版下载
[root@elk-web1 ~]#wget 
https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/apt/pool/main/f/filebeat/filebeat-7.6.2-amd64.deb
#安装
[root@elk-web1 ~]#dpkg -i filebeat-7.6.2-amd64.deb
#默认没有启动
[root@elk-web1 ~]#systemctl enable --now filebeat
#filebeat以root身份启动
[root@elk-web1 ~]#systemctl start filebeatps aux|grep filebeat
#先停止服务,方便后续调试
[root@elk-web1 ~]#systemctl stop filebeat
Filebeat 配置

配置文件官方说明

https://www.elastic.co/guide/en/beats/filebeat/current/configuring-howto-filebeat.html
https://www.elastic.co/guide/en/beats/filebeat/8.3/configuration-general-options.html 

Filebeat的 默认配置文件是/etc/filebeat/filebeat.yml,遵循YAML语法。常见配置包括如下

  • Filebeat

  • Output

  • Shipper

  • Logging(可选)

  • Run Options(可选)

filebeat.yml的格式说明

input_type: log
#指定输入类型

paths
#支持基本的正则,所有golang glob都支持,支持/var/log/*/*.log

encoding
#plain, latin1, utf-8, utf-16be-bom, utf-16be, utf-16le, big5, gb18030, gbk, hz-gb-2312,euc-kr, euc-jp, iso-2022-jp, shift-jis, and so on 

exclude_lines
#支持正则 排除匹配的行,如果有多行,合并成一个单一行来进行过滤

include_lines
#支持正则 include_lines执行完毕之后会执行exclude_lines。

exclude_files
#支持正则 排除匹配的文件
exclude_files: ['.gz$']

tags
#列表中添加标签,用过过滤
filebeat.inputs:
- paths: ["/var/log/app/*.json"]
tags: ["json"]

fields
#可选字段,选择额外的字段进行输出
#可以是标量值,元组,字典等嵌套类型

ignore_older
#可以指定Filebeat忽略指定时间段以外修改的日志内容
#文件被忽略之前,确保文件不在被读取,必须设置ignore older时间范围大于close_inactive
#如果一个文件正在读取时候被设置忽略,它会取得到close_inactive后关闭文件,然后文件被忽略

close_*
#close_ *配置选项用于在特定标准或时间之后关闭harvester。 关闭harvester意味着关闭文件处理程序。 如果在harvester关闭后文件被更新,则在scan_frequency过后,文件将被重新拾取。 但是,如果在harvester关闭时移动或删除文件,Filebeat将无法再次接收文件,并且harvester未读取的任何数据都将