使用正则表达式和Grok

时间:2016-11-06 18:15:57

标签: regex logstash-grok grok graylog

我正在尝试为某些日志文件构建一个Grok模式。我在日志消息中有一个字段,它看起来像以下两个:

IP Address: (192.168.1.100),
IP Address: (192.168.1.100, 2001:0db8:85a3:0000:0000:8a2e:0370:7334),

处理第一个例子很简单我定义了一个名为IP2的新IP模式,以便从括号\((?:%{IP})\)中转义出来,并最终得到类似的结果:

Example Core RegEx Patterns:
IPV6 ((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?
IPV4 (?<![0-9])(?:(?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])[.](?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]))(?![0-9])
IP (?:%{IPV6}|%{IPV4})
IP2 \((?:%{IP})\)

Grok Pattern for Field:
IP Address: %{IP2:ipv4_address},

我正在尝试弄清楚当IPv4和IPv6地址都显示时如何创建正则表达式模式和Grok模式。我总是可以生成IPv6字段,如果它不存在则它就是空的。

1 个答案:

答案 0 :(得分:2)

您需要使用可选组:

\(%{IPV4:ipv4_address}(?:,\s*%{IPV6:ipv6_address})?\)
                      ^^^                        ^^ 

<强>击穿

  • \( - 开放(
  • %{IPV4:ipv4_address} - IPV4模式
  • (?: - 可以发生1或0次的可选组的开始
    • , - 逗号
    • \s* - 零个或多个空格
    • %{IPV6:ipv6_address} - IPV6模式
  • )? - 可选组的结尾(请注意,?是与量化子模式的1或0次匹配的量词)
  • \) - 关闭)