[英]Parsing JSON event in Logstash
我以以下格式登錄,它是帶有嵌套字段的純json。
{
"level": "info",
"message": {
"req": {
"headers": {
"host": "localhost:8080",
"connection": "keep-alive",
"x-forwarded-for": "192.168.1.1, 1.1.1.1",
"x-forwarded-proto": "http"
},
"url": "/products?userId=493d0aec-a9a7-42a3",
"method": "GET",
"originalUrl": "/products?userId=493d0aec-a9a7-42a3",
"params": {
"0": "/products"
},
"query": {
"userId": "493d0aec-a9a7-42a3"
},
"body": ""
},
"res": {
"headers": {
"traceid": "ac586e4e924048",
"x-correlation-id": "57d7920d-b623-48f8",
"content-type": "application/json;charset=UTF-8",
"content-length": "2",
"date": "Fri, 08 Mar 2019 09:55:45 GMT",
"connection": "close"
},
"statusCode": 200,
"body": "[]"
},
"gateway": "internal"
},
"correlationId": "57d7920d-b623-48f8",
"timestamp": "2019-03-08T09:55:45.833Z"
}
如何使用Filebeat和Logstash正確解析它,以將Kibana中的所有json字段視為單獨的(解析的)字段? 我有嵌套json字段的“ message”字段有問題。 我沒有問題可以解析一個在“消息”中包含字符串但不是json的事件。
我的嘗試:
1。 我試圖告訴Filebeat它是具有以下配置的json:
(並且在LS端不執行任何操作)
filebeat.inputs:
- type: stdin
json.keys_under_root: true
json.add_error_key: true
結果對我來說很奇怪,因為我在Kibana中以字符串形式獲取了“消息”,其中所有:
均替換為=>
{
"req" => {
"originalUrl" => "/offers", "params" => {
"0" => "/offers"
}, "query" => {}, "body" => "", "headers" => {
"accept-encoding" => "gzip", "user-agent" => "okhttp/3.8.1", "x-consumer-id" => "f2a6e4cd-2224-4535
正確解析“消息”之外的其他字段
2。 我沒有在Filebeat方面執行任何操作,並在LS中使用了過濾器:
json {
source => "message"
target => "message_json"
}
日志根本沒有出現在Kibana中,我在LS中遇到以下錯誤:
[2019-03-08T09:55:47,084] [WARN] [logstash.outputs.elasticsearch]無法將事件索引到Elasticsearch。 {:status => 400,:action => [“ index”,{:_id => nil,:_index =>“ filebeat-6.5.0-2019.03.08-sdx”,:_type =>“ doc”,: routing => nil},#],:response => {“ index” => {“ _ index” =>“ filebeat-6.5.0-2019.03.08-sdx”,“ _type” =>“ doc”,“ id “ =>” ERS6XGkBgE -US7A6Mvt“,”狀態“ => 400,”錯誤“ => {”類型“ =>” mapper_parsing_exception“,”原因“ =>”無法解析類型為[關鍵字]的字段[json.message] ]“,” caused_by“ => {” type“ =>” illegal_state_exception“,”原因“ =>”無法在1:461的START_OBJECT上獲取文本“”}}}} [2019-03-08T09:55 :47,085] [WARN] [logstash.outputs.elasticsearch]無法將事件編入Elasticsearch。 {:status => 400,:action => [“ index”,{:_id => nil,:_index =>“ filebeat-6.5.0-2019.03.08-sdx”,:_type =>“ doc”,: routing => nil},#],:response => {“ index” => {“ _ index” =>“ filebeat-6.5.0-2019.03.08-sdx”,“ _type” =>“ doc”,“ id “ =>” EhS6XGkBgE -US7A6Mvt“,” status“ => 400,” error“ => {” type“ =>” mapper_parsing_exception“,”原因“ =>”無法解析類型為[keyword]的字段[json.message] ]“,” caused_by“ => {” type“ =>” illegal_state_exception“,”原因“ =>”無法在1:461的START_OBJECT上獲取文字“”}}}}}
如果“消息”字段是字符串(而不是json),則此過濾器對我來說效果很好。
有什么想法如何解析“消息字段”中的嵌套json嗎?
我在通過logstash解析JSON時遇到問題。
我在這個問題上掙扎了一段時間。 並沒有解決成logstash。
但幸運的是,我們已經在彈性搜索本身中吸收了節點 。
我想為您的問題提出我的解決方案:
您制作管道(非常簡單的管道):
{
"description": "Parse JSON log",
"processors": [
{
"json": {
"field": "message",
"target_field": "message-json"
}
},
{
"remove": {
"field": "message"
}
},
{
"append": {
"field": "tags",
"value": [
"isjsonlog"
]
}
}
],
"on_failure": [
{
"append": {
"field": "tags",
"value": [
"nonjsonlog"
]
}
}
]
}
在輸出插件中,配置:
elasticsearch {
hosts => [ localhost ]
index => "filebeat-%{+YYYY.MM.dd}"
manage_template => false
pipeline => "your_pipeline_name"
}
而且您會忘記json解析的問題。
如果使用filebeat ,則可以通過配置filebeat 將json日志直接發送到管道 :
output.elasticsearch:
....
pipelines:
- pipeline: "your pipeline name"
when:
contains:
message: "{"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.