在Logstash中使用grok解析多行JSON

我有一个格式的JSON:

{

"SOURCE":"Source A",

"Model":"ModelABC",

"Qty":"3"

}

我正在尝试使用logstash解析此JSON。基本上,我希望Logstash输出是可以使用kibana进行分析的key:value对的列表。我认为可以开箱即用。从大量的阅读中,我了解到我必须使用grok插件(我仍然不确定json插件的用途)。但是我无法获得所有领域的事件。我收到多个事件(甚至对于JSON的每个属性都一个)。像这样:

{

"message" => " \"SOURCE\": \"Source A\",",

"@version" => "1",

"@timestamp" => "2014-08-31T01:26:23.432Z",

"type" => "my-json",

"tags" => [

[0] "tag-json"

],

"host" => "myserver.example.com",

"path" => "/opt/mount/ELK/json/mytestjson.json"

}

{

"message" => " \"Model\": \"ModelABC\",",

"@version" => "1",

"@timestamp" => "2014-08-31T01:26:23.438Z",

"type" => "my-json",

"tags" => [

[0] "tag-json"

],

"host" => "myserver.example.com",

"path" => "/opt/mount/ELK/json/mytestjson.json"

}

{

"message" => " \"Qty\": \"3\",",

"@version" => "1",

"@timestamp" => "2014-08-31T01:26:23.438Z",

"type" => "my-json",

"tags" => [

[0] "tag-json"

],

"host" => "myserver.example.com",

"path" => "/opt/mount/ELK/json/mytestjson.json"

}

我应该使用多行编解码器还是json_lines编解码器?如果是这样,我该怎么做?我是否需要编写自己的grok模式,或者是否有JSON通用的东西,可以为我在上述一个事件中获得的key:value对提供一个事件?我找不到任何可以说明这一点的文档。任何帮助,将不胜感激。我的配置文件如下所示:

input

{

file

{

type => "my-json"

path => ["/opt/mount/ELK/json/mytestjson.json"]

codec => json

tags => "tag-json"

}

}

filter

{

if [type] == "my-json"

{

date { locale => "en" match => [ "RECEIVE-TIMESTAMP", "yyyy-mm-dd HH:mm:ss" ] }

}

}

output

{

elasticsearch

{

host => localhost

}

stdout { codec => rubydebug }

}

回答:

我想我找到了解决我问题的可行方法。我不确定这是否是一种干净的解决方案,但它有助于解析上述类型的多行JSON。

input 

{

file

{

codec => multiline

{

pattern => '^\{'

negate => true

what => previous

}

path => ["/opt/mount/ELK/json/*.json"]

start_position => "beginning"

sincedb_path => "/dev/null"

exclude => "*.gz"

}

}

filter

{

mutate

{

replace => [ "message", "%{message}}" ]

gsub => [ 'message','\n','']

}

if [message] =~ /^{.*}$/

{

json { source => message }

}

}

output

{

stdout { codec => rubydebug }

}

我的多行编码解码器无法处理最后一个花括号,因此不会以JSON形式出现json { source => message }。因此,mutate过滤器:

replace => [ "message", "%{message}}" ]

那增加了缺少的括号。和

gsub => [ 'message','\n','']

删除\n引入的字符。最后,我有一个单行JSON,可以读取json { source => message }

如果有一种更干净/更简便的方法将原始的多行JSON转换为单行JSON,请执行POST,因为我觉得上述内容不太干净。

以上是 在Logstash中使用grok解析多行JSON 的全部内容, 来源链接: utcz.com/qa/423394.html

回到顶部