概要
今天在群里一个关于在 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 .conf
input {
file {
path => "/home/lcy/file_*"
start_position => "beginning"
ignore_older =>
stat_interval =>
discover_interval =>
}
}
filter {
mutate {
add_field => {
"add_from_1" => ""
}
}
}
output {
stdout {
codec => rubydebug
}
}
其中 2.conf 没有配置 input 。在 fileter 中加一个 filed ,名称叫 add_from_2。输出格式为 JSON。
// 文件2.conf的内容
lcy@lcy:/etc/logstash/conf.d$ cat .conf
filter {
mutate {
add_field => {
"add_from_2" => ""
}
}
}
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:
Logstash startup completed
可以执行:
对配置文件进行调试
/opt/logstash/bin/logstash agent -f /etc/logstash/conf.d –configtest
或从命令行以交互方式运行它.它会产生很多其他信息.
/opt/logstash/bin/logstash –debug -f /etc/logstash/conf.d
或运行以下只是测试并查看已编译的配置
/opt/logstash/bin/logstash –debug –debug-config -t -f /etc/logstash/conf.d/
2. 输出结果
即输出了 rubydebug 格式,又输出了 JSON 格式。
lcy@lcy:/etc/logstash/conf.d$ sudo /opt/logstash/bin/logstash -f /etc/logstash/conf.d/
Settings: Default pipeline workers:
Logstash startup completed
{
"message" => "{\"file_1\":{\"tag_1\":\"value_1\"}}",
"@version" => "",
"@timestamp" => "2016-04-28T06:42:43.050Z",
"path" => "/home/lcy/file_1",
"host" => "lcy",
"add_from_1" => "",
"add_from_2" => ""
}
{"message":"{\"file_1\":{\"tag_1\":\"value_1\"}}","@version":"","@timestamp":"2016-04-28T06:42:43.050Z","path":"/home/lcy/file_1","host":"lcy","add_from_1":"","add_from_2":""}
^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 两个 field
OUTPUT :输出了2种格式,rubydebug 和 JSON