logstash一個實例運行多個配置文件,將所有配置文件放到以下目錄即可
/usr/share/logstash/pipeline
但是默認行為不是每個配置文件獨立運行,而是作為一個整體,每個input會匹配所有的filter,然后匹配所有的output,可能會導致數據被錯誤的處理以及發送到錯誤的地方;
解決方法一:
在input中設置一個變量,在filter和output中判斷該變量,實現每個配置文件獨立運行,不會相互影響,使用哪個變量呢?
input默認有很多通用參數,但是只有type可用,官方描述如下:
Add a
type
field to all events handled by this input.Types are used mainly for filter activation.
The type is stored as part of the event itself, so you can also use the type to search for it in Kibana.
input中的type參數會被添加到event中,所以后續在filter和output中都可以使用,其他參數就不行了,配置如下:
input { jdbc { ... type => "some_type" } } filter { if [type] == "some_type" { ... } } output { if [type] == "some_type" { ... } }
如果一切正常,恭喜你,如果還有問題,有可能是你的event里本來就有type字段,然后又賦值一個,會出現一個type數組(追加而不是覆蓋),這時有兩個問題:
1)filter和output中的if判斷失效,你的數據壓根就不會處理也不會發送出去;
2)你的數據中的type字段被改亂了;
解決方法二:
在event中手工添加一個變量,比如my_type,配置如下:
input { jdbc { ... statement => "select *, 'some_type' my_type from my_table where update_time > :sql_last_value" ... } } filter { if [my_type] == "some_type" { ... } } output { if [my_type] == "some_type" { ... } }
還有其他解決方法詳見下邊引用的官方文檔,包括各種if判斷以及@metadata變量的使用;
參考:
jdbc input
https://www.elastic.co/guide/en/logstash/6.3/plugins-inputs-jdbc.html
event dependent configuration
https://www.elastic.co/guide/en/logstash/6.7/event-dependent-configuration.html