【问题标题】:Splunk Cloud search query with variable does not return results带变量的 Splunk Cloud 搜索查询不返回结果
【发布时间】:2020-12-09 08:08:58
【问题描述】:

我有一个不返回结果且不显示错误的查询(与 where 和 search 命令相同):

"ExtendedProperties.PrCode"="myProductName" 
| eval myversion="12.916"|  where "ExtendedProperties.ProductVersion"=myversion

不带eval的查询返回结果:

"ExtendedProperties.PrCode"="myProductName" 
|  search "ExtendedProperties.ProductVersion"="12.916" 

产品版本的最后三位数字是月份(9 月)和日期(16),我的最终目标是使用 now() 函数从当前日期中提取它们。这将消除每天更新查询的需要。 不幸的是,这个查询也没有返回结果:

"ExtendedProperties.PrCode"="myProductName" 
| eval month = ltrim(tostring(strftime(now(),"%m")),"0") 
| eval day = strftime(now(),"%d") 
| eval version="12." + month + day 
| where "ExtendedProperties.ProductVersion"=version

这里是一些示例数据:

{"Timestamp":"2020-12-14T14:37:00.2662745Z","Categories":["someCategoryString"],"Metadata":["someMetadataString"],"ExtendedProperties":{"MachineId":"SomeMachineId","ProductVersion":"12.916","PrCode":"MyProductName","ProductType":"1","Type":"ProductUsed","Source":"SomeSourceString","SessionId":"SomeGuid","TimeStamp":"2020-12-14T14:36:56.7086819Z","Environment":"SomeEnvironment"}}

这会返回结果:

|makeresults | eval _raw = "{\"Timestamp\":\"2020-12-14T14:37:00.2662745Z\",\"Categories\":[\"someCategoryString\"],\"Metadata\":[\"someMetadataString\"],\"ExtendedProperties\":{\"MachineId\":\"SomeMachineId\",\"ProductVersion\":\"12.1219\",\"PrCode\":\"MyProductName\",\"ProductType\":\"1\",\"Type\":\"ProductUsed\",\"Source\":\"SomeSourceString\",\"SessionId\":\"SomeGuid\",\"TimeStamp\":\"2020-12-14T14:36:56.7086819Z\",\"Environment\":\"SomeEnvironment\"}}", month = ltrim(tostring(strftime(now(),"%m")),"0"), day = strftime(now(),"%d"),version="12."+month+day|spath | search "ExtendedProperties.ProductVersion"="12.1219"

但是,当我将字符串“12.1219”替换为具有相同值的版本变量时(在搜索结束时),没有找到结果:

|makeresults | eval _raw = "{\"Timestamp\":\"2020-12-14T14:37:00.2662745Z\",\"Categories\":[\"someCategoryString\"],\"Metadata\":[\"someMetadataString\"],\"ExtendedProperties\":{\"MachineId\":\"SomeMachineId\",\"ProductVersion\":\"12.1219\",\"PrCode\":\"MyProductName\",\"ProductType\":\"1\",\"Type\":\"ProductUsed\",\"Source\":\"SomeSourceString\",\"SessionId\":\"SomeGuid\",\"TimeStamp\":\"2020-12-14T14:36:56.7086819Z\",\"Environment\":\"SomeEnvironment\"}}", month = ltrim(tostring(strftime(now(),"%m")),"0"), day = strftime(now(),"%d"),version="12."+month+day|spath | search "ExtendedProperties.ProductVersion"=version

预期输出是包含预期版本(今天为 12.1219)的一条记录。

【问题讨论】:

    标签: splunk splunk-query


    【解决方案1】:

    不要使用evalwhereevalsearch

    把它放在初始搜索中:

    "ExtendedProperties.PrCode"="myProductName" "ExtendedProperties.ProductVersion"="12.916"
    

    让 Splunk 为您完成工作 - 并让它以尽可能最有效的方式完成工作 :)

    编辑反映问题更新:

    试试这样的:

    index=ndx "ExtendedProperties.PrCode"="myProductName" "ExtendedProperties.ProductVersion"="12.*"
    | eval monthday=strftime(now(),"%m%d")
    | where match("ExtendedProperties.ProductVersion",monthday)
    

    首先,不要使用两个evals :)

    其次,了解各种函数及其参数,例如strftimecommon time formats。或match

    【讨论】:

    • 感谢您的宝贵时间。我想实现更复杂的搜索,但我无法理解为什么这个更基本的搜索不起作用。也许我对变量声明做错了什么?如果使用变量,您能告诉我正确的语法是什么吗?
    • @K.B. - 你实际上想要完成什么?
    • 我在问题中添加了作为我最终目标的查询。我想从当前日期获取月份和日期,然后将其作为产品版本变量的一部分传递,这样我就不必每天更新查询。
    • 感谢您提供的信息,不幸的是,建议的查询也没有返回结果。
    • @K.B. - 如果这不起作用,并且您的搜索不起作用...那么我怀疑您错过了有关数据的某些内容...因为我只能关闭您共享的内容。请分享一些示例数据,以便我们更好地帮助您:)
    【解决方案2】:

    我发现如果没有正确提取该字段,则查询无法返回任何结果。因此,使用此查询,结果按预期显示:

    |makeresults | eval _raw = "{\"Timestamp\":\"2020-12-14T14:37:00.2662745Z\",\"Categories\":[\"someCategoryString\"],\"Metadata\":[\"someMetadataString\"],\"ExtendedProperties\":{\"MachineId\":\"SomeMachineId\",\"ProductVersion\":\"12.1219\",\"PrCode\":\"MyProductName\",\"Environment\":\"SomeEnvironment\"}}", month = ltrim(tostring(strftime(now(),"%m")),"0"), day = strftime(now(),"%d"),version="12."+month+day | rex "ProductVersion[\\\":]*(?<ExtractedProductVersion>[^\\\":]*)" | where ExtractedProductVersion=version
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-24
      • 2021-02-10
      • 1970-01-01
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      相关资源
      最近更新 更多