如何为下面的场景构建Splunk搜索查询

如何为下面的场景构建Splunk搜索查询,splunk,splunk-query,Splunk,Splunk Query,我能够在splunk仪表板中获得多个事件(api日志),如下所示 事件1: { "corrId":"12345", "traceId":"srh-1", "apiName":"api1" } { "corrId":"69863", "traceId":"srh-2", "apiName&

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

事件1:

{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }
{ "corrId":"69863", "traceId":"srh-2", "apiName":"api2" }
{ "corrId":"12345", "traceId":"srh-3", "apiName":"api3" }
{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }
{ "corrId":"12345", "traceId":"srh-3", "apiName":"api3" }
事件2:

{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }
{ "corrId":"69863", "traceId":"srh-2", "apiName":"api2" }
{ "corrId":"12345", "traceId":"srh-3", "apiName":"api3" }
{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }
{ "corrId":"12345", "traceId":"srh-3", "apiName":"api3" }
事件3:

{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }
{ "corrId":"69863", "traceId":"srh-2", "apiName":"api2" }
{ "corrId":"12345", "traceId":"srh-3", "apiName":"api3" }
{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }
{ "corrId":"12345", "traceId":"srh-3", "apiName":"api3" }
我想从一个事件(api日志)中动态检索
corrId
(例如:-“corrId”:“12345”),方法是提供apiName并基于检索到的
corrId
值构建splunk搜索查询,这意味着它将提取包含相同
corrId
“corrId”:“12345”
)的所有事件日志

输出

在上述场景中,预期结果如下所示

事件1:

{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }
{ "corrId":"69863", "traceId":"srh-2", "apiName":"api2" }
{ "corrId":"12345", "traceId":"srh-3", "apiName":"api3" }
{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }
{ "corrId":"12345", "traceId":"srh-3", "apiName":"api3" }
事件3:

{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }
{ "corrId":"69863", "traceId":"srh-2", "apiName":"api2" }
{ "corrId":"12345", "traceId":"srh-3", "apiName":"api3" }
{ "corrId":"12345", "traceId":"srh-1", "apiName":"api1" }
{ "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]

此查询仅提供事件1日志,但我们需要包含相同
corrId
“corrId”:“12345”
)的所有其他事件。感谢您的快速帮助。

鉴于您正在显式提取
apiName
字段,我假定
corrId
字段也不会自动提取。这意味着将
corrId=“12345”
放入基本查询将不起作用。请尝试
index=test\u 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 ]
index=test\u srh source=policy.log
|rex“corrId\\”:\\“(?[^\\”]+)”
|其中[search index=“test\u srh source=policy.log
|rex“apiName\”:“(?[^\”]+)”
|搜索名称=“api1”
|rex“corrId\\”:\\“(?[^\\”]+)”
|字段更正|格式]

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

如果您显式提取
apiName
字段,我将假定
corrId
字段也不会自动提取。这意味着将
corrId=“12345”
放入基本查询将不起作用。请尝试
index=test\u 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 ]
index=test\u srh source=policy.log
|rex“corrId\\”:\\“(?[^\\”]+)”
|其中[search index=“test\u srh source=policy.log
|rex“apiName\”:“(?[^\”]+)”
|搜索名称=“api1”
|rex“corrId\\”:\\“(?[^\\”]+)”
|字段更正|格式]

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

非常感谢您的回答,因为根据我的偶数日志,字段名是“x-xxxCorrId”,而不是“corrId”。我曾尝试使用上面的查询将“correId”替换为“x-xxxCorrId”,但我得到了以下语法错误,如--“rex”命令中的错误:在编译rex“x-xxxCorrId\”时遇到以下错误:“(?[^\”]+)”Regex:子模式名称中的语法错误(缺少终止符)。。我已经尝试了很多方法仍然没有解决嵌入引号需要额外转义的问题。我已经修改了答案。这次出现了如下错误,如--“SearchParser”中的错误:不匹配“]”。下面一个是我使用的查询--index=test\u srh source=policy.log | rex“x-xxxCorrId\\”:\“(?[^\\”]+)”;其中[search index=“test\u srh source=policy.log | rex“apiName\”:\”(?[^\”]+)“| search name=“api1”| rex“x-xxxCorrId\\”:\”(?[^\\)+)”)”:\“+”(?[^\\”)”)”字段x-xxorrid格式]尝试为每个嵌入的引号添加另一个转义字符。另外,尽量避免在字段名中使用连字符。现在再次出现第一次错误--“rex”命令中的错误:在编译rex'x-xxxCorrId\“:\”(?[^\“]+)”Regex时遇到以下错误:子模式名中的语法错误(缺少终止符)。我不知道为什么会出现,非常感谢你的回答,因为根据我的偶数日志,字段名是“x-xxxCorrId”,而不是“corrId”。我曾尝试使用上面的查询将“correId”替换为“x-xxxCorrId”,但我得到了以下语法错误,如--“rex”命令中的错误:在编译rex“x-xxxCorrId\”时遇到以下错误:“(?[^\”]+)”Regex:子模式名称中的语法错误(缺少终止符)。。我已经尝试了很多方法仍然没有解决嵌入引号需要额外转义的问题。我已经修改了答案。这次出现了如下错误,如--“SearchParser”中的错误:不匹配“]”。下面一个是我使用的查询--index=test\u srh source=policy.log | rex“x-xxxCorrId\\”:\“(?[^\\”]+)”;其中[search index=“test\u srh source=policy.log | rex“apiName\”:\”(?[^\”]+)“| search name=“api1”| rex“x-xxxCorrId\\”:\”(?[^\\)+)”)”:\“+”(?[^\\”)”)”字段x-xxorrid格式]尝试为每个嵌入的引号添加另一个转义字符。另外,尽量避免在字段名中使用连字符。现在再次出现第一次错误--“rex”命令中的错误:在编译rex'x-xxxCorrId\“:\”(?[^\“]+)”Regex时遇到以下错误:子模式名中的语法错误(缺少终止符)。我不知道它为什么会出现