Logstash自定义匹配

时间:2016-09-19 12:00:10

标签: regex logstash logstash-grok logstash-configuration

  AppliedProperty_PurchaseStatus = (string) <SOLD>
  AppliedProperty_UrlText = (string) <http://www.dummyurl.com>
  MA_Number= (decimal) [123456789]

我试图在导入应用程序日志时弄清楚如何与grok进行一些匹配。但坦率地说,我相当失落,我将如何进行上述匹配,以便我可以搜索&#34; MA_Number&#34;得到&#34; 123456789&#34;因为弹性

2 个答案:

答案 0 :(得分:1)

该数字的正则表达式,假设每行都是用grok逐个处理的,将是:

MA_Number= \(decimal\) \[%{NUMBER}\]

NUMBER是由grok定义的模式,以及许多可以帮助您的模式:

https://github.com/elastic/logstash/blob/v1.4.2/patterns/grok-patterns

关于您上面提供的消息(根本不匹配您的grok(??))

这是你可以快速测试/部署它的方法:

我的测试配置:

input {
stdin{}
}


filter {

    grok {
       match => ["message", "MA_Number= \(decimal\) \[%{NUMBER:num}\]" ]
    }

}


output {
  stdout { codec => rubydebug }
}

测试:

artur@pandaadb:~/dev/logstash$ ./logstash-2.3.2/bin/logstash -f conf3/
Settings: Default pipeline workers: 8
Pipeline main started
MA_Number= (decimal) [123456789]
{
       "message" => "MA_Number= (decimal) [123456789]",
      "@version" => "1",
    "@timestamp" => "2016-09-19T13:30:56.837Z",
          "host" => "pandaadb",
           "num" => "123456789"
}

查看如何将消息的数量提​​取到变量num中。

答案 1 :(得分:0)

您可以使用\[([0-9]+)\],它将匹配:

  • [litteral
  • 1个或更多数字
  • ]和结束括号

https://regex101.com/r/sP3gA7/2

示范

&#13;
&#13;
var re = /\[([0-9]+)\]/; 
var str = '  AppliedProperty_PurchaseStatus = (string) <SOLD>\n  AppliedProperty_UrlText = (string) <http://www.dummyurl.com>\n  MA_Number= (decimal) [123456789]';
var m;
 
if ((m = re.exec(str)) !== null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
    console.log(m[1]);
}
&#13;
&#13;
&#13;

https://regexper.com/#%5C%5B(%5B0-9%5D%2B)%5C%5D