【问题标题】:Splunk rex: extracting repeating keys and values to a tableSplunk rex:将重复的键和值提取到表中
【发布时间】:2020-02-10 20:50:55
【问题描述】:

我在 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
    -                    -          -

请注意,我在这里使用- 字符来表示我的表中缺少数据。其他每一行都完全空白,hostTmp 的 mvcountlen 值始终为空。

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

【问题讨论】:

    标签: regex splunk splunk-query


    【解决方案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
    

    【讨论】:

    • 这正是我一直在寻找的并且完美地工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    相关资源
    最近更新 更多