2017-04-07 96 views
0

我複製如何篩選{「foo」:「bar」,「bar」:「foo」} grok只獲取foo字段?

{"name":"myapp","hostname":"banana.local","pid":40161,"level":30,"msg":"hi","time":"2013-01-04T18:46:23.851Z","v":0} 

https://github.com/trentm/node-bunyan並將其保存爲我的logs.json。我試圖通過LogStash將兩個字段(名稱和味精)導入ElasticSearch。問題是我依賴於一種我無法完成的過濾器。那麼我已經成功地將這一行作爲一條消息導入,但在我的真實情況下肯定不值得。

這就是說,我如何才能將名稱和味精導入ElasticSearch?我使用http://grokdebug.herokuapp.com/測試了幾種替代方案,以達到一個有用的過濾器,但根本沒有成功。

例如,%{GREEDYDATA:message}會將整行作爲唯一消息,但如何拆分它並忽略名稱和消息字段以外的所有內容?

最後,我刨用在這裏:

input { 
    file { 
     type => "my_type" 
     path => [ "/home/logs/logs.log" ] 
     codec => "json" 
    } 
} 

filter {  

    grok { 
      match => { "message" => "data=%{GREEDYDATA:request}"}   
     } 
#### some extra lines here probably 
} 

output 
{ 
    elasticsearch { 
    codec => json 
    hosts => "http://127.0.0.1:9200" 
    index => "indextest" 
    } 

    stdout { codec => rubydebug } 
} 

回答

0

我剛剛通過的available Logstash filters列表中消失了。 prune filter應該符合您的需求。

假定您已經安裝了prune filter,你的配置文件應該是這樣:

input { 
    file { 
    type => "my_type" 
    path => [ "/home/logs/logs.log" ] 
    codec => "json" 
    } 
} 

filter { 
    prune { 
    whitelist_names => [ 
     "@timestamp", 
     "type", 
     "name", 
     "msg" 
    ] 
    } 
} 

output { 
    elasticsearch { 
    codec => json 
    hosts => "http://127.0.0.1:9200" 
    index => "indextest" 
    } 

    stdout { codec => rubydebug } 
} 

請注意,你會希望保持type爲Elasticsearch索引它變成一個正確的類型。如果您要查看Kibana上的數據,則需要@timestamp

相關問題