概要
今天在群里一個關於在 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