使用logstash解析动态ruby哈希

时间:2017-10-20 10:51:39

标签: ruby hash logstash

我使用logstash,我的日志中有Ruby哈希值。日志看起来像:

 id: 20171023080217469299836 time: 2017-10-23 08:02:17 +0500 login: 123 params: {:service_id=>21164, :user_id=>"771713"}
 id: 20171022185107064615881 time: 2017-10-22 18:51:07 +0500 login: 321 params: {:unc=>"521130929", :id=>"107005094"}

我将其解析为字段“params”

   id: 20171023080217469299836 time: 2017-10-23 08:02:17 +0500 login: 123 params: {:service_id=>21164, :user_id=>"771713"}                         
   {                                                                                                                                               
       "@timestamp" => 2017-10-23T03:02:17.000Z,                                                                                                   
         "@version" => "1",                                                                                                                        
             "host" => "elk",                                                                                                                
               "id" => "20171023080217469299836",                                                                                                 
            "login" => "123",                                                                                                                     
           "params" => "{:service_id=>21164, :user_id=>\"771713\"}",                                                                                                                                                                                                                     
   }                                                                                                                                               
   id: 20171022185107064615881 time: 2017-10-22 18:51:07 +0500 login: 321 params: {:unc=>"521130929", :id=>"107005094"}                            
   {                                                                                                                                               
       "@timestamp" => 2017-10-22T13:51:07.000Z,    
         "@version" => "1",                                                                                                                        
             "host" => "elk",                                                                                                                
               "id" => "20171022185107064615881",                                                                                                 
            "login" => "321",                                                                                                                     
           "params" => "{:unc=>\"521130929\", :id=>\"107005094\"}",                                                                                                                                                                                                                         
   }                                                                                                                                               

参数名称各种各样。也可以添加或删除(哈希中有时有2或4-5个参数)。我想将它解析到不同的领域(如xml-filter parses)

{
"parse.service_id" : 21164, 
"parse.user_id" : 771713
}

{
"parse.unc" : 521130929 
"parse.id" : 107005094
}

但是找不到怎么做。 有Logstash ruby​​解析器吗?

1 个答案:

答案 0 :(得分:0)

您可以同时使用kv过滤器和grok过滤器。

在filter.conf logtash文件中,添加以下内容:

grok {
  match => { "message" => "id: %{INT:id} time: %{GREEDYDATA:time} login: %{INT:login} params: %{GREEDYDATA:params}" }
}
date {
    match => ["time", "yyyy-MM-dd HH:mm:ss Z"]
  }
kv {
  source => "params"
  remove_char_key => ":"
  remove_char_value => "\""
  field_split => ","
  value_split => "="
  trim_key => "\{"
  trim_value => "\}>\""
  prefix => "parse."

}

PS:您需要使用"转义特殊的正则表达式字符,例如{\

这将为您提供以下可视化:

Kibana Visualization