Splunk rex:将重复的键和值提取到表中

问题描述 投票:0回答:1

我在Splunk中有一些日志,我正尝试为其提取一些值。我的日志条目如下所示:

host-03.company.local:9011[read 3617, write 120 bytes] host-05.company.local:9011[read 370658827, write 177471 bytes] host-07.company.local:9011[read 99, write 96 bytes] host-07.company.local:9011[read 96, write 96 bytes] host-05.company.local:9011[read 120, write 120 bytes] host-05.company.local:9011[read 120, write 120 bytes] host-03.company.local:9015[read 42955, write 120 bytes] host-05.company.local:9015[read 3048879, write 86677386 bytes] host-02.company.local:7035[read 120, write 120 bytes] host-03.company.local:9015[read 120, write 120 bytes] host-05.company.local:9015[read 809077, write 120 bytes] host-02.company.local:7035[read 120, write 120 bytes] host-03.company.local:9015[read 120, write 120 bytes] host-05.company.local:9015[read 120, write 120 bytes] host-02.company.local:7035[read 120, write 120 bytes]

这些日志条目遵循的模式是host:port[read xxx, write yyy bytes]

此日志行中可以有1到大约20个主机记录。

我希望在Splunk中将这些字段提取到表中,以使结果看起来像:

hostname                   readBytes WriteBytes
-----------------------------------------------
host-03.company.local:9011      3617        120
host-05.company.local:9011 370658827     177471
host-07.company.local:9011        99         96
host-05.company.local:9011       120        120

这里的逻辑是我为每个主机提取readwrite条目,以便每个主机都成为该表中的一行。

我在使用rex提取主机方面取得了一些进展:

index=myApplication <mySearch>
  | rex field=_raw "(?<hostsTmp>([a-zA-Z0-9\-\.]+:[0-9]+))"
  | table hostsTmp

但是,即使此结果似乎是错误的,某些结果也只是空白行。另外,hostsTemp字段似乎不是多变量字段。 mvcount(hostsTemp)对于每个条目均不返回任何内容。

mvcount(hostsTmp)   len(hostsTmp)   hostsTmp
--------------------------------------------
    -                    -          host-05.company.local:9011
    -                    -          -
    -                    -          host-05.company.local:9011
    -                    -          -
    -                    -          host-05.company.local:9011
    -                    -          -

[请注意,我在这里使用-字符表示表中缺少数据。每隔一行仅是完全空白,并且hostsTmp的mvcountlen值始终为空。

Splunk相对较新,不是正则表达式方面的专家,因此可以提供任何帮助。

regex splunk splunk-query
1个回答
1
投票

我建议将主机的每个结果拆分为一个单独的事件,然后对每个事件执行rex

这将执行您的完整事件,并为每个主机及其数据创建一个多值字段(名为ev)。>

| eval ev=split(raw,"]")
| mvexpand ev

然后,可以使用简单的rex提取数据。

| rex field=ev "^\s*(?<hostname>[^\[]+)\[read\s+(?<readBytes>\d+),\s+write\s+(?<writeBytes>\d+)\s+bytes"

并使用table对其进行适当的格式化。

| table hostname readBytes writeBytes

以下是显示其正常工作的示例。您可能需要更改split(raw以指向您自己的事件中的字段,或使用_raw

| makeresults | eval raw="host-03.company.local:9011[read 3617, write 120 bytes] host-05.company.local:9011[read 370658827, write 177471 bytes] host-07.company.local:9011[read 99, write 96 bytes] host-07.company.local:9011[read 96, write 96 bytes] host-05.company.local:9011[read 120, write 120 bytes] host-05.company.local:9011[read 120, write 120 bytes] host-03.company.local:9015[read 42955, write 120 bytes] host-05.company.local:9015[read 3048879, write 86677386 bytes] host-02.company.local:7035[read 120, write 120 bytes] host-03.company.local:9015[read 120, write 120 bytes] host-05.company.local:9015[read 809077, write 120 bytes] host-02.company.local:7035[read 120, write 120 bytes] host-03.company.local:9015[read 120, write 120 bytes] host-05.company.local:9015[read 120, write 120 bytes] host-02.company.local:7035[read 120, write 120 bytes]"
| eval ev=split(raw,"]")
| mvexpand ev
| rex field=ev "^\s*(?<hostname>[^\[]+)\[read\s+(?<readBytes>\d+),\s+write\s+(?<writeBytes>\d+)\s+bytes"
| table hostname readBytes writeBytes
© www.soinside.com 2019 - 2024. All rights reserved.