【问题标题】:Parse JSON message in Logstash在 Logstash 中解析 JSON 消息
【发布时间】:2015-12-18 12:31:38
【问题描述】:

我正在使用以下配置将我的 jenkins 日志发送到 logstash:

 redis {
    host => "localhost"
    key => "logstash"
    data_type => "list"
    codec => json
    }

这和预期一样顺利,现在我在 KIBANA 中看到以下消息:

{
  "_index": "logstash-2015.12.18",
  "_type": "logs",
  "_id": "AVG1BN5LXZBIbp7HE4xN",
  "_score": null,
  "_source": {
    "data": {
      "id": "965",
      "projectName": "NicePJ",
      "displayName": "#965",
      "fullDisplayName": "NicePJ",
      "url": "job/NIcePJ/965/",
      "buildHost": "Jenkins",
      "buildLabel": "master",
      "buildNum": 965,
      "buildDuration": 1,
      "rootProjectName": "NicePJ",
      "rootProjectDisplayName": "#965",
      "rootBuildNum": 965,
      "buildVariables": {
        "target_SUT": "0201",
        "report_warnings": "false",
        "product": "Ours",
        "testsuite": "Exciting_stuff5",
        "qft_version": "current",
        "target_task": "t324",
        "branch": "test",
        "testcase": "",
        "revision": "HEAD",
        "node": "hsqs960",
        "client": "Desktop",
        "run_specific_test": "false",
        "user": "xxxxx"
      }
    },
    "message": [
      "A         This is a message XYZ"
    ],
    "source": "jenkins",
    "source_host": "http://serverXL:8080/",
    "@timestamp": "2015-12-18T12:16:02.000Z",
    "@version": 1
  },
  "fields": {
    "@timestamp": [
      1450440962000
    ]
  },
  "sort": [
    1450440962000
  ]
}

现在我想过滤某些消息的消息字段,但我无法让它工作。如何过滤消息字段以及如何访问 buildHost 字段以在管道中的 if 语句中使用它?

在我尝试了很多例子之后:

 if[data][buildHost]== "jenkins"
  {
         grok
         {
           match => { "message[0]"  => "\[exec\]\s*\<%{GREEDYDATA:test}\s*\[%{GREEDYDATA:result}\]" }
         }
  }

但这根本不起作用,请帮帮我。

【问题讨论】:

    标签: json jenkins logstash


    【解决方案1】:

    有条件的

    == 比较简单的字符串和区分大小写,因此 "jenkins" 不会像您的数据显示的那样匹配 ("buildHost": "Jenkins",):

    if[data][buildHost]== "jenkins"
    

    但以下是:

    if[data][buildHost]== "Jenkins"
    

    如果您需要同时匹配两者,您可以使用|| 或正则表达式=~。

    Grok

    grok 是一个使用正则表达式模式解析消息的过滤器。您可以使用

    测试您的正则表达式模式
    • 在线 grok 调试器
    • Kibana 开发工具的 grok 调试器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多