平时在物理机上使用 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 中对日志进行展示了。

Filebeat 根据不同的日志设置不同的索引的更多相关文章

  1. ElasticSearch+Logstash+Filebeat+Kibana集群日志管理分析平台搭建

    一.ELK搜索引擎原理介绍 在使用搜索引擎是你可能会觉得很简单方便,只需要在搜索栏输入想要的关键字就能显示出想要的结果.但在这简单的操作背后是搜索引擎复杂的逻辑和许多组件协同工作的结果. 搜索引擎的组 ...

  2. ELK之filebeat替代logstash收集日志

    filebeat->redis->logstash->elasticsearch 官网下载地址:https://www.elastic.co/downloads/beats/file ...

  3. ELK快速入门(四)filebeat替代logstash收集日志

    ELK快速入门四-filebeat替代logstash收集日志 filebeat简介 Filebeat是轻量级单用途的日志收集工具,用于在没有安装java的服务器上专门收集日志,可以将日志转发到log ...

  4. 使用Filebeat传送多行日志

    文章转载自:https://blog.csdn.net/UbuntuTouch/article/details/106272704 在解决应用程序问题时,多行日志为开发人员提供了宝贵的信息. 堆栈跟踪 ...

  5. tomcat7 日志设置为log4j

    tomcat的日志设置用log4j的官方文档:http://tomcat.apache.org/tomcat-7.0-doc/logging.html 1. 下载tomcat-juli.jar, to ...

  6. LR中日志设置和日志函数

    LR中日志参数的设置与使用 1.Run-Time Setting日志参数的设置 在loadrunner的vuser菜单下的Run-Time Setting的General的LOG选项中可以对在执行脚本 ...

  7. Jmeter 日志设置---如何设置java协议中被测jar的日志?

    先转载一下Jmeter的日志设置: Jmeter运行出现问题可以通过调整jmeter的日志级别定位问题,但运行测试时建议关闭jmeter日志,jmeter打印日志耗费系统性能. Jmeter日志默认存 ...

  8. 【kafka学习之五】kafka运维:kafka操作日志设置和主题删除

    一.操作日志 首先附上kafka 操作日志配置文件:log4j.properties 根据相应的需要设置日志. #日志级别覆盖规则 优先级:ALL < DEBUG < INFO <W ...

  9. 转 -Filebeat + Redis 管理 LOG日志实践

    Filebeat + Redis 管理 LOG日志实践 小赵营 关注 2019.01.06 17:52* 字数 1648 阅读 24评论 0喜欢 2 引用 转载 请注明出处 某早上,领导怒吼声远远传来 ...

随机推荐

  1. java获取一天前的时间

    获取一天前的时间 Date date = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); ...

  2. RabbitMQ 3.6.12延迟队列简单示例

    简介 延迟队列存储的消息是不希望被消费者立刻拿到的,而是等待特定时间后,消费者才能拿到这个消息进行消费.使用场景比较多,例如订单限时30分钟内支付,否则取消,再如分布式环境中每隔一段时间重复执行某操作 ...

  3. 06 C语言变量

    C语言变量 变量的本质 变量的本质其实是程序可操作的存储区的名称. C 中每个变量都有特定的类型,类型决定了变量存储的大小的范围,在范围内的值都可以存储在内存中,运算符可应用于变量上. 变量的名称可以 ...

  4. matlab receive License Manager Error -103?

    参考:https://www.mathworks.com/matlabcentral/answers/91874-why-do-i-receive-license-manager-error-103 ...

  5. NET Standard中配置TargetFrameworks输出多版本类库

    系列目录     [已更新最新开发文章,点击查看详细] 在.NET Standard/.NET Core技术出现之前,编写一个类库项目(暂且称为基础通用类库PA)且需要支持不同 .NET Framew ...

  6. trie树——【吴传之火烧连营】

    突然发现好像没有讲过一种叫做tire树的神奇东西. 问题描述: 题目描述 [题目背景] 蜀汉章武元年(221年),刘备为报吴夺荆州.关羽被杀之仇,率大军攻吴.吴将陆逊为避其锋,坚守不战,双方成对峙之势 ...

  7. Go strconv包

    strconv包 该包主要实现基本数据类型与其字符串表示的转换. 常用函数为Atoi().Itia().parse系列.format系列.append系列. 更多函数请查看官方文档. string与i ...

  8. Cesium资料

    CesiumLab论坛:https://github.com/cesiumlab/cesium-lab-forum/issues简书上的Cesium实验室文集:https://www.jianshu. ...

  9. hashCode()方法源码分析

    执行代码 public class Demo06 { public static void main(String[] args) { String s="hello"; Syst ...

  10. Prometheus入门教程(二):Prometheus + Grafana实现可视化、告警

    文章首发于[陈树义]公众号,点击跳转到原文:https://mp.weixin.qq.com/s/56S290p4j9KROB5uGRcGkQ Prometheus UI 提供了快速验证 PromQL ...