Filebeat 日志收集工具配置实践 | 您所在的位置:网站首页 › java接入kafka › Filebeat 日志收集工具配置实践 |
文章目录Filebeat介绍Filebeat工作原理部署与运行解压安装包配置运行读取文件自定义字段输出到Elasticsearch读取Nginx日志文件Module使用Module启用Nginx Module配置Filebeat介绍 Filebeat是一个轻量型日志采集器,可以方便的同kibana集成,启动filebeat后,可以直接在kibana中观看对日志文件进行detail的过程。结合搜索功能可以查看某个程序、某个服务器 在某段时间的日志情况。 Filebeat工作原理Filebeat由两个主要组件组成:prospector 和 harvester。harvester 负责读取单个文件的内容。 如果文件在读取时被删除或重命名,Filebeat将继续读取文件。prospector prospector 负责管理harvester并找到所有要读取的文件来源。 如果输入类型为日志,则查找器将查找路径匹配的所有文件,并为每个文件启动一个harvester。 Filebeat目前支持两种prospector类型:log和stdin。 Filebeat如何保持文件的状态 Filebeat 保存每个文件的状态并经常将状态刷新到磁盘上的注册文件中。 该状态用于记住harvester正在读取的最后偏移量,并确保发送所有日志行。 如果输出(例如Elasticsearch或Logstash)无法访问,Filebeat会跟踪最后发送的行,并在输出再次可用时继续读取文件。 在Filebeat运行时,每个prospector内存中也会保存的文件状态信息,当重新启动Filebeat时,将使用注册文件的数据来重建文件状态,Filebeat将每个harvester在从保存的最后偏移量继续读取。 文件状态记录在data/registry文件中 部署与运行解压安装包解压命令: tar -zvxf filebeat-6.5.4-linux-x86_64.tar.gz 解压到 /opt/apps 下 并建立软连接 到 filebeat 目录 命令:ln -s filebeat-6.5.4-linux-x86_64 filebeat 目录结构如下: [root@localhost apps]# lselasticsearch-6.5.4 jdkelasticsearch-6.5.4.tar.gz jdk1.8.0_191elsearch jdk-8u191-linux-x64.tar.gzfilebeat nginxfilebeat-6.5.4-linux-x86_64 nginx-1.12.2filebeat-6.5.4-linux-x86_64.tar.gz nginx-1.12.2.tar.gz配置运行在filebeat目录下 创建配置文件 test.yml,内容如下: [root@localhost filebeat]# vi test.ymlfilebeat.inputs:- type: stdin enabled: truesetup.template.settings: index.number_of_shards: 3output.console: pretty: true enable: true该文件配置,监控控制台输入,将监控到的结果直接在终端输出。 运行如下命令启动filebeat ./filebeat -e -c test.yml可以看到 filebeat正常启动,输入hello运行结果如下: hello{ "@timestamp": "2020-06-28T05:56:55.077Z", "@metadata": { "beat": "filebeat", "type": "doc", "version": "6.5.4" }, "prospector": { "type": "stdin" }, "input": { "type": "stdin" }, "beat": { "version": "6.5.4", "name": "localhost.localdomain", "hostname": "localhost.localdomain" }, "host": { "name": "localhost.localdomain" }, "message": "hello", "source": "", "offset": 0}读取文件创建 /opt/apps/filebeat/logs/ 目录 ,并在该目录下创建 a.log 和b.log 文件 增加配置文件test-log.yml 配置文件内容如下: filebeat.inputs:- type: log enabled: true paths: - /opt/apps/filebeat/logs/*.logsetup.template.settings: index.number_of_shards: 3output.console: pretty: true enable: true该文件监控 目录下所有 .log 文件,监控到变化后输出到控制台 启动filebeat ./filebeat -e -c test-log.yml 通过如下命令写入内容到 日志文件 echo ddd>a.log echo dee>b.log filebeat 控制台输出结果如下: { "@timestamp": "2020-06-28T06:10:20.021Z", "@metadata": { "beat": "filebeat", "type": "doc", "version": "6.5.4" }, "prospector": { "type": "log" }, "input": { "type": "log" }, "beat": { "name": "localhost.localdomain", "hostname": "localhost.localdomain", "version": "6.5.4" }, "host": { "name": "localhost.localdomain" }, "source": "/opt/apps/filebeat/logs/a.log", "offset": 0, "message": "ddd"}{ "@timestamp": "2020-06-28T06:10:54.990Z", "@metadata": { "beat": "filebeat", "type": "doc", "version": "6.5.4" }, "source": "/opt/apps/filebeat/logs/b.log", "offset": 0, "message": "deee", "prospector": { "type": "log" }, "input": { "type": "log" }, "beat": { "name": "localhost.localdomain", "hostname": "localhost.localdomain", "version": "6.5.4" }, "host": { "name": "localhost.localdomain" }}可以看出,已经检测到日志文件有更新,立刻就会读取到更新的内容,并且输出到控制台 自定义字段修改 test-log.yml 配置文件内容如下: filebeat.inputs:- type: log enabled: true paths: - /opt/apps/filebeat/logs/*.log tags: ["logfile"] fields: from: log fields_under_root: true #true为添加到根节点,false为添加到子节点中setup.template.settings: index.number_of_shards: 3output.console: pretty: true enable: true启动服务 ./filebeat -e -c test-log.yml 输入dddd 到b.log echo dddd > b.log filebeat控制台输出: { "@timestamp": "2020-06-28T06:22:39.636Z", "@metadata": { "beat": "filebeat", "type": "doc", "version": "6.5.4" }, "message": "dddd", "input": { "type": "log" }, "prospector": { "type": "log" }, "from": "log", -- 根目录下增加了from字段 "beat": { "name": "localhost.localdomain", "hostname": "localhost.localdomain", "version": "6.5.4" }, "host": { "name": "localhost.localdomain" }, "source": "/opt/apps/filebeat/logs/b.log", "offset": 0, "tags": [ -- 根目录下增加了tags字段且 "logfile" ]}输出到Elasticsearch修改配置文件 test-log.yml内容如下: filebeat.inputs:- type: log enabled: true paths: - /opt/apps/filebeat/logs/*.log tags: ["logfile"] fields: from: log fields_under_root: truesetup.template.settings: index.number_of_shards: 3output.elasticsearch: hosts: ["192.168.12.10:9200","192.168.12.11:9200","192.168.12.12:9200"]启动服务 ./filebeat -e -c test-log.yml 同时确保 es服务正常,再次输入内容到b.log echo 123 > b.log 之后可以看到 es中自动创建了索引,并且保存了刚才监控到的数据如下: 修改配置文件test-log.yml如下: filebeat.inputs:- type: log enabled: true paths: - /usr/local/nginx/logs/*.log tags: ["nginx"] fields: from: nginx fields_under_root: truesetup.template.settings: index.number_of_shards: 3output.elasticsearch: hosts: ["192.168.12.10:9200","192.168.12.11:9200","192.168.12.12:9200"]启动服务后可以看到 filebeat 会将nginx日志都存入到 es中如下: 前面要想实现日志数据的读取以及处理都是自己手动配置的,其实,在Filebeat中,有大量的Module,可以简化我们的配置,直接就可以使用,如下 [root@localhost filebeat]# ./filebeat modules listEnabled:Disabled:apache2auditdelasticsearchhaproxyicingaiiskafkakibanalogstashmongodbmysqlnginxosquerypostgresqlredissuricatasystemtraefik 可以看到,内置了很多的module,但是都没有启用,如果需要启用需要进行enable操作(./filebeat modules disable nginx #禁用) [root@localhost filebeat]# ./filebeat modules enable nginxEnabled nginx[root@localhost filebeat]# ./filebeat modules listEnabled:nginxDisabled:apache2auditdelasticsearchhaproxyicingaiiskafkakibanalogstashmongodbmysqlosquerypostgresqlredissuricatasystemtraefik 同时可以看到 module.d下nginx.yml 文件可用 [root@localhost filebeat]# cd modules.d/[root@localhost modules.d]# ll总用量 72-rw-r--r--. 1 root root 371 12月 18 2018 apache2.yml.disabled-rw-r--r--. 1 root root 175 12月 18 2018 auditd.yml.disabled-rw-r--r--. 1 root root 845 12月 18 2018 elasticsearch.yml.disabled-rw-r--r--. 1 root root 269 12月 18 2018 haproxy.yml.disabled-rw-r--r--. 1 root root 546 12月 18 2018 icinga.yml.disabled-rw-r--r--. 1 root root 371 12月 18 2018 iis.yml.disabled-rw-r--r--. 1 root root 396 12月 18 2018 kafka.yml.disabled-rw-r--r--. 1 root root 188 12月 18 2018 kibana.yml.disabled-rw-r--r--. 1 root root 361 12月 18 2018 logstash.yml.disabled-rw-r--r--. 1 root root 189 12月 18 2018 mongodb.yml.disabled-rw-r--r--. 1 root root 368 12月 18 2018 mysql.yml.disabled-rw-r--r--. 1 root root 369 12月 18 2018 nginx.yml-rw-r--r--. 1 root root 388 12月 18 2018 osquery.yml.disabled-rw-r--r--. 1 root root 192 12月 18 2018 postgresql.yml.disabled-rw-r--r--. 1 root root 463 12月 18 2018 redis.yml.disabled-rw-r--r--. 1 root root 190 12月 18 2018 suricata.yml.disabled-rw-r--r--. 1 root root 574 12月 18 2018 system.yml.disabled-rw-r--r--. 1 root root 195 12月 18 2018 traefik.yml.disabledNginx Module配置修改 nginx.yml 内容如下: [root@localhost modules.d]# vi nginx.yml - module: nginx # Access logs access: enabled: true var.paths: ["/usr/local/nginx/logs/access.log*"] # Set custom paths for the log files. If left empty, # Filebeat will choose the paths depending on your OS. #var.paths:# Error logs error: enabled: true var.paths: ["/usr/local/nginx/logs/error.log*"] # Set custom paths for the log files. If left empty, # Filebeat will choose the paths depending on your OS. #var.paths: 修改 新建 test-nginx.yml 内容如下: filebeat.inputs:setup.template.settings: index.number_of_shards: 3output.elasticsearch: hosts: ["192.168.12.10:9200","192.168.12.11:9200","192.168.12.12:9200"]filebeat.config.modules: path: ${path.config}/modules.d/*.yml reload.enabled: false启动服务发现报错 ERROR pipeline/output.go:100 Failed to connect to backoff(elasticsearch(http://192.168.12.10:9200)): Connection marked as failed because the onConnect callback failed: Error loading pipeline for fileset nginx/access: This module requires the following Elasticsearch plugins: ingest-user-agent, ingest-geoip. You can install them by running the following commands on all the Elasticsearch nodes: sudo bin/elasticsearch-plugin install ingest-user-agent sudo bin/elasticsearch-plugin install ingest-geoip按照提示 到 es各个节点 执行 如下命令安装插件: bin/elasticsearch-plugin install ingest-user-agent bin/elasticsearch-plugin install ingest-geoip安装完成后重启es服务和filebeat服务 ,es中可以正常采集nginx日志信息且,日志格式更加友好,字段准确 其他的Module的用法参加官方文档: https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-modules.html
|
CopyRight 2018-2019 实验室设备网 版权所有 |