平时在物理机上使用 Filebeat 收集日志时,会编写多个 filebeat 配置文件然后启动多个 filebeat 进程来收集不同路径下的日志并设置相对应的索引。那么如果将所有的日志路径都写到一个 filebeat 配置文件中,那么就需要根据不同的日志来设置索引了。

其实 logstash 也可以实现这个功能。但是此处只演示在 Filebeat 上实现。步骤和讲解如下:

例如现在有如下三个日志文件,需要输出到不同的索引:

access.log     ---->  索引:web-nginx-access-log
error.log ----> 索引:web-nginx-error-log
blacklist.log ----> 索引:web-nginx-blacklist-log

所需要的 filebeat 配置文件如下:

filebeat.idle_timeout: 2s
filebeat.name: filebeat-shiper
filebeat.spool_zie: 50000 filebeat.inputs: # 从这里开始定义每个日志的路径、类型、收集方式等信息
- type: log # 指定收集的类型为 log
paths:
- /usr/local/nginx/logs/access.log # 设置 access.log 的路径
fields: # 设置一个 fields,用于标记这个日志
type: access-log # 为 fields 设置一个关键字 type,值为 access-log
enabled: true
backoff: 1s
backoff_factor: 2
close_inactive: 1h
encoding: plain
harvester_buffer_size: 262144
max_backoff: 10s
max_bytes: 10485760
scan_frequency: 10s
tail_lines: true
- type: log
paths:
- /usr/local/nginx/logs/error.log # 设置 error.log 的路径
fields: # 设置一个 fields,用于标记这个日志
type: error-log # 为 fields 设置一个关键字 type,值为 error-log
enabled: true
backoff: 1s
backoff_factor: 2
close_inactive: 1h
encoding: plain
harvester_buffer_size: 262144
max_backoff: 10s
max_bytes: 10485760
scan_frequency: 10s
tail_lines: true
- type: log
paths:
- /usr/local/nginx/logs/blacklist.log # 设置 blacklist.log 的路径
fields: # 设置一个 fields,用于标记这个日志
type: blacklist-log # 为 fields 设置一个关键字 type,值为 blacklist-log
enabled: true
backoff: 1s
backoff_factor: 2
close_inactive: 1h
encoding: plain
harvester_buffer_size: 262144
max_backoff: 10s
max_bytes: 10485760
scan_frequency: 10s
tail_lines: true output.elasticsearch:
workers: 4
bulk_max_size: 8192
hosts: # 设置 elastic 的地址
- 10.16.12.206:30187
- 10.16.12.207:30187
- 10.16.12.208:30187
- 10.16.13.214:30187
- 10.16.13.215:30187
index: web-nginx-%{[fields.type]}-%{+yyyy.MM.dd} # 设置索引名称,后面引用的 fields.type 变量。此处的配置应该可以省略(不符合下面创建索引条件的日志,会使用该索引,后续会测试是否是这样)
indices: # 使用 indices 代表要创建多个索引
- index: web-nginx-access-log-%{+yyyy.MM.dd} # 设置 access.log 日志的索引,注意索引前面的 web-nginx 要与setup.template.pattern 的配置相匹配
when.equals: # 设置创建索引的条件:当 fields.type 的值等于 access-log 时才生效
fields.type: access-log
- index: web-nginx-error-log-%{+yyyy.MM.dd}
when.equals:
fields.type: error-log
- index: web-nginx-blacklist-log-%{+yyyy.MM.dd}
when.equals:
fields.type: blacklist-log processors:
- drop_fields:
fields:
- agent.ephemeral_id
- agent.hostname
- agent.id
- agent.type
- agent.version
- ecs.version
- input.type
- log.offset
- version
- decode_json_fields:
fields:
- message
max_depth: 1
overwrite_keys: true setup.ilm.enabled: false # 如果要创建多个索引,需要将此项设置为 false
setup.template.name: web-nginx-log # 设置模板的名称
setup.template.pattern: web-nginx-* # 设置模板的匹配方式,上面索引的前缀要和这里保持一致
setup.template.overwrite: true
setup.template.enabled: true

编辑完成后,启动 filebeat 进程。到 Kibana 中查看索引列表,可以发现已经有三个新创建的索引:

名称                                   运行状况    状态     主分片    副本分片    文档计数    存储大小
web-nginx-access-log-2020.10.28 green open 1 1 110 73.9kb
web-nginx-error-log-2020.10.28 green open 1 1 354 155kb
web-nginx-blacklist-log-2020.10.28 green open 1 1 460 219.5kb

针对这三个索引创建索引模式后,就可以在 Kibana 中对日志进行展示了。

最新文章

  1. Atitit.兼具兼容性和扩展性的配置方案attilax总结
  2. Python ZIP压缩
  3. Unhandled exception at 0x........ in XXXX.exe: 0xC0000005:错误
  4. LinkedList方法总结 ListIterator和Iterator的区别
  5. CenterOS中安装Redis及开机启动设置
  6. JAVA程序猿面试题汇总
  7. UITableView 的使用小点
  8. SEO优化:浅析关键词出现在网站哪些地方更有利?
  9. 常见的异步方式async 和 await
  10. 降维【PCA & SVD】
  11. 20. Web proxies (网页代理 4个)
  12. uCrop图片裁剪
  13. Linux记录屏幕输出log
  14. C# install-package:"xx"已拥有为“xxx”定义的依赖项
  15. ecplise自动提示失效,使用补全自动提示快捷键(Alt+/),但只显示“No Default Proposals”
  16. 谷歌浏览器安装POSTMAN
  17. 将SharePoint站点另存为模板并根据模板创建站点!
  18. mysql5.6 函数大全
  19. Python多态、鸭子类型
  20. 爬虫之Requests: 让 HTTP 服务人类

热门文章

  1. SpringMVC学习(二)
  2. Markdown 1.0.1
  3. Tomcat 8.5安装
  4. Redis 4.0.2分布式锁的Java实现
  5. 1-浅谈 python变量
  6. Kafka单机安装
  7. SpringBoot+单机redis
  8. IdentityServer 3.1.x 迁移到 4.x
  9. JVM系列【6】GC与调优2.md
  10. spring cloud:搭建基于consul的服务提供者集群(spring cloud hoxton sr8 / spring boot 2.3.4)