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解析,
#也可以通过类似的方法判断

ELK学习笔记之logstash将配置写在多个文件的更多相关文章

  1. ELK学习笔记之Logstash详解

    0x00 Logstash概述 官方介绍:Logstash is an open source data collection engine with real-time pipelining cap ...

  2. ELK学习笔记之Logstash和Filebeat解析对java异常堆栈下多行日志配置支持

    0x00 概述 logstash官方最新文档.假设有几十台服务器,每台服务器要监控系统日志syslog.tomcat日志.nginx日志.mysql日志等等,监控OOM.内存低下进程被kill.ngi ...

  3. ELK 学习笔记之 Logstash之output配置

    Logstash之output配置: 输出到file 配置conf: input{ file{ path => "/usr/local/logstash-5.6.1/bin/spark ...

  4. ELK 学习笔记之 Logstash之inputs配置

    Logstash之inputs配置: input plugin doc: https://www.elastic.co/guide/en/logstash/current/index.html 插件很 ...

  5. ELK学习笔记之logstash配置多入多出并互相隔离

    0x00 概述 需求:需要利用同一logstash进程采集不同日志,输出到es的不同index,各输入输出隔离: 主要需要解决如下两个问题: 0x01 如何加载多个配置文件 普通启动方式: nohup ...

  6. ELK 学习笔记之 Logstash之filter配置

    Logstash之filter: json filter: input{ stdin{ } } filter{ json{ source => "message" } } o ...

  7. ELK 学习笔记之 Logstash之codec配置

    Logstash之codec: Logstash处理流程: input=>decode=>filter=>encode=>output 分类: Plain编码: input{ ...

  8. ELK 学习笔记之 Logstash安装

    Logstash安装: https://www.elastic.co/downloads/logstash 下载解压: tar –zxvf logstash-5.6.1.tar.gz 在/usr/lo ...

  9. ELK学习笔记之Kibana安装配置

    Kibana 是一个开源的分析和可视化平台,是ELK的重要部分.Kibana提供搜索.查看和与存储在 Elasticsearch 索引中的数据进行交互的功能.开发者或运维人员可以轻松地执行高级数据分析 ...

随机推荐

  1. mysql左连接查询结果不准确

    现有四张表 表(1)res_resource_catalog 表(2)res_catalog_classify 表(3)res_resource_classify 表(4)res_resource_m ...

  2. ASP.NET,C#后台调用前台javascript的五种方法

    C#后台调用前台javascript的五种方法 由于项目需要,用到其他项目组用VC开发的组件,在web后台代码无法访问这个组件,所以只好通过后台调用前台的javascript,从而操作这个组件.在网上 ...

  3. 11 JavaScript Utility Libraries you Should Know in 2019

    11 Useful Javascript utility libraries to speed your development.

  4. Javascript Date构造函数和比较 (二)

    JavaScript Date对象 构造函数实例 Date构造函数中没有参数,将返回当前日期 var currentDate = new Date(); writeLine(currentDate.t ...

  5. FTO Obesity Variant Circuitry and Adipocyte Browning in Humans

    好文献非常难得,提供了核心的研究思路. FTO Obesity Variant Circuitry and Adipocyte Browning in Humans - 这篇文章需要好好的解析 为深入 ...

  6. C# 序列化与反序列化之xml通过实现IXmlSerializable进行序列化的解决方案

    新建控制台console项目,添加XmlPersonIXmlSerializable类,和AddressIXmlSerializable类(实现IXmlSerializable)以及AddressIX ...

  7. 微信小程序:用 Promise 解决方案代替回调地狱。 修复 this._invokeMethod is not a function 的问题

    /** * 将回调地狱转换为 Promise 形式 * https://blog.csdn.net/SEAYEHIN/article/details/88663740 * raw: wx.downlo ...

  8. Redis禁用部分危险命令(keys/flushdb等)

    在redis.conf文件[SECURITY]区域配置: 1.禁用命令: rename-command KEYS "" rename-command FLUSHALL " ...

  9. response.redirect 正在中止线程

    问题描述:正在中止线程问题原因:Response.End 方法终止页的执行,并将此执行切换到应用程序的事件管线中的 Application_EndRequest 事件.不执行 Response.End ...

  10. C#-DllImport 路径问题

    原文:C# DllImport 相对路径无法找到dll DllImport DLL查找顺序:1.应用程序所在目录2.Windows目录和Windows\System32目录3.环境变量目录 只需要你把 ...