【问题标题】:How to Build Splunk Search Query for below Scenario如何为以下场景构建 Splunk 搜索查询
【发布时间】:2020-09-30 10:26:49
【问题描述】:

我能够在 splunk 仪表板中获取多个事件(api 的日志),如下所示

事件 1:

{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }

事件 2:

{ "corrId":"69863", "traceId":"srh-2", "apiName":"api2" }

事件 3:

{ "corrId":"12345", "traceId":"srh-3", "apiName":"api3" }

我想通过提供 apiName 并基于检索到的 corrId 值构建 splunk 搜索查询,从一个事件(api 日志)动态检索 corrId(例如:-“corrId”:“12345”),这意味着它将拉所有包含相同corrId ("corrId":"12345") 的事件日志。

输出

在上述情况下,预期结果如下所示

事件 1:

{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }

事件 3:

{ "corrId":"12345", "traceId":"srh-3", "apiName":"api3" }

我是 splunk 的新手,请在这里帮助我,如何通过提供 apiName 等其他字段来动态获取 "corrId":"12345" 并基于此构建 Splunk 搜索查询。

我已经尝试如下,但没有运气。

index = "test_srh source=policy.log [ search index = "test_srh source=policy.log | rex field=_raw "apiName":|s+"(?[^"]+)" | search name="api1" | table corrId]

此查询仅提供 event-1 日志,但我们需要包含相同 corrId ("corrId":"12345") 的所有其他事件。在这里感谢快速帮助。

【问题讨论】:

    标签: splunk splunk-query


    【解决方案1】:

    鉴于您明确提取 apiName 字段,我假设 corrId 字段也不会自动提取。这意味着将corrId="12345" 放在基本查询中是行不通的。试试index=test_srh source=policy.log corrId="12345" 来验证。

    如果需要提取 corrId 字段,请尝试此查询。

    index=test_srh source=policy.log 
    | rex "corrId\\":\\"(?<corrId>[^\\"]+)"
    | where [ search index = "test_srh source=policy.log 
      | rex "apiName\":\"(?<name>[^\"]+)" 
      | search name="api1" 
      | rex "corrId\\":\\"(?<corrId>[^\\"]+)" 
      | fields corrId | format ]
    

    注意:我还更正了正则表达式以正确提取 apiName 字段。

    【讨论】:

    • 非常感谢您的回答,根据我的偶数日志字段名称是“x-xxxCorrId”而不是“corrId”。我曾尝试使用上述查询将“correId”替换为“x-xxxCorrId”,但出现以下语法错误,例如 - 'rex' 命令中的错误:在编译 rex 'x-xxxCorrId\“时遇到以下错误:\ "(?[^\"]+)" 正则表达式:子模式名称中的语法错误(缺少终止符)..我尝试了很多方法仍然没有解决
    • 嵌入的引号需要额外的转义。我已经修改了答案。
    • 这次出现了以下错误,例如 -- 'SearchParser' 中的错误:不匹配 ']'。下面是我使用的查询——index=test_srh source=policy.log | rex "x-xxxCorrId\\":\\"(?[^\\"]+)" | where [ search index = "test_srh source=policy.log | rex "apiName\":\"(?[^\"]+)" | 搜索名称="api1" | rex "x-xxxCorrId\\":\\"(? [^\\"]+)" |字段 x-xxxCorrId|格式]
    • 尝试为每个嵌入的引号添加另一个转义字符。另外,尽量避免在字段名称中使用连字符。
    • 现在再次出现第一次错误--'rex' 命令出错:在编译 rex 'x-xxxCorrId\":\"(?[ ^\"]+)" 正则表达式:子模式名称中的语法错误(缺少终止符)。我不确定它为什么会出现
    猜你喜欢
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多