0x00 概述

我们用Logsatsh写配置文件的时候,如果读取的文件太多,匹配的正则过多,会使配置文件动辄成百上千行代码,可能会造成阅读和修改困难。这时候,我们可以将配置文件的输入、过滤、输出分别放在不同的配置文件里,甚至把输入、过滤、输出再次分离,放在不同的文件里。
这时候,后期再需要增删改查内容的时候,就容易维护了。

0x01 logstash如何读取多个配置文件

我们知道在启动logstash的时候,只要加上-f /you_path_to_config_file就可以加载配置文件了,如果我们需要加载多个配置文件,只需要-f /you_path_to_config_directory就可以了。简单说,就是在-f后面加上目录就可以。
注意:目录后面不能加 * 号,否则只会读取一个文件,但是在读取日志文件时,*可以匹配所有,比如sys.log*可以匹配所有以sys.log开头的日志文件,如sys.log1,sys.log2等。

示例如下:

//比如 /home/husen/config/目录下有
//in1.conf、in2.conf、filter1.conf、filter2.conf、out.conf这5个文件 //我们使用 /logstash-5.5.1/bin/logstash -f /home/husen/config启动logtstash
//logstash会自动加载这个5个配置文件,并合并成1个整体的配置文件

0x02 logstash多个配置文件里的input、filter、output是否相互独立

答案是:NO!

比如:

## in1.conf内容如下:
input{
file{
path=>[
"/home/husen/log/sys.log"
]
}
} ## in2.conf内容如下:
input{
file{
path=>[
"/home/husen/log/error.log"
]
}
} ## out1.conf如下
elasticsearch {
action => "index"
hosts => "localhost:9200"
index => "from_sys_log"
codec => "json"
} ## out2.conf如下
elasticsearch {
action => "index"
hosts => "localhost:9200"
index => "from_error_log"
codec => "json"
}
//这几个配置文件的目的是:
//想把in1.conf读进来的sys.log的索引建立为from_sys_log
//把in.conf读进来的error.log的索引建立为femo_error_log //logstash-5.5.1/bin/logstash -f /home/husen/config //启动之后,会发现in1.conf的日志被输出了两次,in2.conf读进来的日志也被输出了两次 //结论:logstash读取多个配置文件只是简单的将所有配置文件整合到了一起!
//如果要彼此独立,需要自己加字段,然后判断一下
//比如读取来不同不同服务器的同样格式的日志,那么filter是可以共用的
//但是输出的索引需要分别建立,以提高辨识度

0x03 logstash读取多个配置文件建议的配置方法

如果要在配置文件中,独立一些部分,又要共用一些部分,比如我上门提高同样的日志来自不同的服务器,需要用同样的filter,但是建立不同的索引的问题,该怎么办?
建议使用tags或者type这两个特殊字段,即在读取文件的时候,添加标识符在tags中或者定义type变量。

示例如下:

## in1.conf内容如下:
input{
file{
path=>[
"/home/husen/log/sys.log"
]
type => "from_sys"
#tags => ["from_sys"]
}
} ## in2.conf内容如下:
input{
file{
path=>[
"/home/husen/log/error.log"
]
type => "from_error"
#tags => ["from_sys"]
}
} ## out1.conf如下
if [type] == "from_sys"{
#if "from_sys" in [tags]
elasticsearch {
action => "index"
hosts => "localhost:9200"
index => "from_sys_log"
codec => "json"
}
} ## out2.conf如下
if [type] == "from_error"{
#if "from_error" in [tags]
elasticsearch {
action => "index"
hosts => "localhost:9200"
index => "from_error_log"
codec => "json"
}
} #特别地,如果要针对不同的类型日志用不同filter来grok解析,
#也可以通过类似的方法判断

最新文章

  1. 二、获取微信用户openId
  2. CAD 快捷键Ctrl+2 Ctrl+3
  3. 将access数据库导入mysql
  4. Round Numbers
  5. Java中的继承性特性
  6. 【前端】jQuery移动端左滑删除
  7. threesum
  8. ERROR: invalid byte sequence for encoding "UTF8": 0x00
  9. Envoy 源码分析--network L4 filter manager
  10. 自己实现HashSet
  11. 【css】css 中文字体 unicode 对照表
  12. jmeter经验----java 读取文件中文乱码问题
  13. 半夜思考, Java 重载的实现
  14. 正规式->最小化DFA说明
  15. Ubuntu的常识使用了解4
  16. MYSQL题讲答案
  17. 使用Hive Rest API 连接HDInsight
  18. YII2集成GOAOP,实现面向方面编程!
  19. IE 内使用ActiveX,写注册表被重定向到如下注册表
  20. node 文件下载到本地 (支持中文文件名)

热门文章

  1. 配送城市地址联动选择JQuery
  2. shell 判断大小
  3. 上传一句话木马时<? php被过滤的解决办法
  4. python mysql数据库压力测试
  5. 华为云ARM64服务器试用
  6. HikariCP连接池配置
  7. tensorflow 笔记 15:如何使用 Supervisor
  8. c# 格式化时间
  9. fatal error: sys/videoio.h: No such file or directory
  10. MySQL 使用 ON UPDATE CURRENT_TIMESTAMP 自动更新 timestamp (转)