获取grok过滤器转换为的正则表达式?

时间:2014-10-23 09:00:47

标签: logstash logstash-grok

我有一个复杂的grok过滤器表达式...是否可以获得此过滤器转换为的正则表达式?

1 个答案:

答案 0 :(得分:2)

您可以使用一个简单的Perl脚本来读取模式文件,并将%{PATTERN}内容替换为它所基于的实际正则表达式 - 您必须自定义这一点,但它显示了如何做到这一点:

#!/usr/bin/perl

# this is the path to your grok-patterns file
open(F,"patterns/grok-patterns");
while (<F>) {
  chomp;
  if (/^(\S+) (.*)/) {
    $pattern{$1} = $2;
  }
}
close(F);

# this is the grok pattern I want to expand
$pattern='%{IP:junk} %{COMBINEDAPACHELOG:junk2}';

while ($pattern =~ /(%\{([^:\}]+):?[^\}]*\})/) {
    $name = $2;
    substr($pattern,$-[0],$+[0]) = $pattern{$name};
}
print $pattern,"\n";