简体   繁体   English

用logstash解析动态红宝石哈希

[英]Parse dynamic ruby hash with logstash

I use logstash and I have Ruby hashes in my log. 我使用logstash,并且日志中有Ruby哈希。 Logs looks like: 日志看起来像:

 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"}

And I parse it into field "params" 我将其解析为字段“ 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\"}",                                                                                                                                                                                                                         
   }                                                                                                                                               

Parameter names various. 参数名称多种多样。 and can also be added or deleted (there are sometimes 2 or 4-5 parameters in hash). 并且还可以添加或删除(哈希中有时有2或4-5个参数)。 I'd like to parse this into different fields (like xml-filter parses) 我想将其解析为不同的字段(例如xml-filter解析)

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

and

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

But can't find how to do this. 但是找不到如何执行此操作。 Has Logstash ruby parser? 有Logstash红宝石解析器吗?

You can use the kv filter and the grok filter together. 您可以同时使用kv过滤器和grok过滤器。

In your filter.conf logtash file, add the following: 在您的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: you need to escape special regex characters like " and { using \\ . PS:您需要使用\\来转义特殊的正则表达式字符,例如"{

This will give you the following visualization: 这将为您提供以下可视化效果:

Kibana可视化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM