Splunk rex:将重复键和值提取到 table
Splunk rex: extracting repeating keys and values to a table
我在 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 中将这些字段提取到 table,结果如下所示:
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
这里的逻辑是我正在为每个主机提取 read
和 write
条目,这样每个条目都成为此 table.
中的一行
我在使用 rex 提取主机方面取得了一些进展:
index=myApplication <mySearch>
| rex field=_raw "(?<hostsTmp>([a-zA-Z0-9\-\.]+:[0-9]+))"
| table hostsTmp
然而,即使这个结果似乎是错误的,有些结果只是空行。此外,hostsTemp
字段似乎不是多变量字段。 mvcount(hostsTemp) returns 每个条目都没有。
mvcount(hostsTmp) len(hostsTmp) hostsTmp
--------------------------------------------
- - host-05.company.local:9011
- - -
- - host-05.company.local:9011
- - -
- - host-05.company.local:9011
- - -
请注意,我在这里使用 -
字符表示 table 中缺少数据。每隔一行完全空白,hostsTmp 的 mvcount
和 len
值始终为空。
Splunk 相对较新,并非正则表达式专家,因此不胜感激。
我的建议是将主持人的每个结果分成一个单独的事件,然后对每个事件执行 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
我在 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 中将这些字段提取到 table,结果如下所示:
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
这里的逻辑是我正在为每个主机提取 read
和 write
条目,这样每个条目都成为此 table.
我在使用 rex 提取主机方面取得了一些进展:
index=myApplication <mySearch>
| rex field=_raw "(?<hostsTmp>([a-zA-Z0-9\-\.]+:[0-9]+))"
| table hostsTmp
然而,即使这个结果似乎是错误的,有些结果只是空行。此外,hostsTemp
字段似乎不是多变量字段。 mvcount(hostsTemp) returns 每个条目都没有。
mvcount(hostsTmp) len(hostsTmp) hostsTmp
--------------------------------------------
- - host-05.company.local:9011
- - -
- - host-05.company.local:9011
- - -
- - host-05.company.local:9011
- - -
请注意,我在这里使用 -
字符表示 table 中缺少数据。每隔一行完全空白,hostsTmp 的 mvcount
和 len
值始终为空。
Splunk 相对较新,并非正则表达式专家,因此不胜感激。
我的建议是将主持人的每个结果分成一个单独的事件,然后对每个事件执行 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