Logstash conf.d 多个配置文件
概要
今天在群里一个关于在 logstash 的配置目录存在多个配置文件时候如何处理的问题?
我说是加载所有配置文件并合并为一个。
lcy@lcy:~/ELK/logstash$ sudo /opt/logstash/bin/logstash --help [sudo] password for lcy: Usage: /bin/logstash agent [OPTIONS] Options: -f, --config CONFIG_PATH Load the logstash config from a specific file or directory. If a directory is given, all files in that directory will be concatenated in lexicographical order and then parsed as a single config file. You can also specify wildcards (globs) and any matched files will be loaded in the order described above.
下面,做个小实验用以说明。
文件说明
1. conf.d 目录说明
存在2个配置文件 1.conf 和 2.conf
// 存在2个配置文件 lcy@lcy:/etc/logstash/conf.d$ ls 1.conf 2.conf
其中 1.conf 配置了 input ,使用 file 插件来导入文件 file_1(input中参数配置为 file_*) 的内容。并且在 fileter 中加一个 filed ,名称叫 add_from_1。输出格式为 rubydebug。
// 文件1.conf的内容 lcy@lcy:/etc/logstash/conf.d$ cat 1.conf input { file { path => "/home/lcy/file_*" start_position => "beginning" ignore_older => 11111111 stat_interval => 1 discover_interval => 1 } } filter { mutate { add_field => { "add_from_1" => "1111111" } } } output { stdout { codec => rubydebug } }
其中 2.conf 没有配置 input 。在 fileter 中加一个 filed ,名称叫 add_from_2。输出格式为 JSON。
// 文件2.conf的内容 lcy@lcy:/etc/logstash/conf.d$ cat 2.conf filter { mutate { add_field => { "add_from_2" => "2222222" } } } output { stdout { codec => json } }
2. 输入源文件
file_1 很简单,就一个单行 JSON 文件。
// file_1 的文件内容 lcy@lcy:~$ cat file_1 {"file_1":{"tag_1":"value_1"}}
执行结果
1. 启动 logstash
lcy@lcy:/etc/logstash/conf.d$ sudo /opt/logstash/bin/logstash -f /etc/logstash/conf.d/ Settings: Default pipeline workers: 2 Logstash startup completed
2. 输出结果
即输出了 rubydebug 格式,又输出了 JSON 格式。
lcy@lcy:/etc/logstash/conf.d$ sudo /opt/logstash/bin/logstash -f /etc/logstash/conf.d/ Settings: Default pipeline workers: 2 Logstash startup completed { "message" => "{\"file_1\":{\"tag_1\":\"value_1\"}}", "@version" => "1", "@timestamp" => "2016-04-28T06:42:43.050Z", "path" => "/home/lcy/file_1", "host" => "lcy", "add_from_1" => "1111111", "add_from_2" => "2222222" } {"message":"{\"file_1\":{\"tag_1\":\"value_1\"}}","@version":"1","@timestamp":"2016-04-28T06:42:43.050Z","path":"/home/lcy/file_1","host":"lcy","add_from_1":"1111111","add_from_2":"2222222"}
^CSIGINT received. Shutting down the pipeline. {:level=>:warn} ^CSIGINT received. Terminating immediately.. {:level=>:fatal} ^CSIGINT received. Terminating immediately.. {:level=>:fatal}
结论
可以看出,实际执行中,把 1.conf 和 2.conf 文件的内容完全合并为了一个配置文件
INPUT :2.conf 没有配置 input 不会报错,因为 1.conf 中有(input 为必须)
FILETER :输出内容中即添加了 add_from_1 也添加了 add_from_2 两个 filed
OUTPUT :输出了2中格式,rubydebug 和 JSON