簡體   English   中英

SPLUNK 在第二次搜索中使用第一次搜索的結果

[英]SPLUNK use result from first search in second search

假設我有一個查詢,例如

index="example" source="example.log" host="example" "ERROR 1234" 
| stats distinct_count by id

這將為我提供每個 ID 帶有該錯誤代碼的所有事件。

然后我想結合這個查詢來搜索同一個日志文件中的另一個字符串,但僅限於第一次搜索返回的唯一 ID。 因為新字符串將出現在一個單獨的事件中,所以我不能只做一個“與”。

有幾種方法可以做到這一點,包括使用子搜索、 joinappend ,但這些方法需要多次遍歷數據。 這是一種通過索引進行單次傳遞的方法。

index=example source="example.log" ("ERROR 1234" OR "ERROR 5678")
``` Check for the presence of each string in the event ```
| eval string1=if(searchmatch("ERROR 1234"), 1, 0)
| eval string2=if(searchmatch("ERROR 5678"), 1, 0)
``` Count string occurrences by id ```
| stats sum(string1) as string1, sum(string2) as string2 by id
``` Keep only the ids that have both strings ```
| where (string1 > 0 AND string2 > 0)

您可以在子搜索中搜索“其他字符串”,然后joinidid

index="example" source="example.log" host="example" "ERROR 1234"  
| join id [search index="example" source="example.log" host="example" "some other string" ]
| stats distinct_count by id

假設您的id字段相同並且在兩個索引中都可用,則此表單應該有效:

(index=ndxA sourcetype=srctpA id=* source=example.log host=example "ERROR 1234") OR (index=ndxB sourcetype=srctpB id=* "some other string")
| rex field=_raw "(?<first_field>ERROR 1234)"
| rex field=_raw "(?<second_field>some other string)"
| fillnull value="-" first_field second_field
| stats count by id first_string second_string
| search NOT (first_string="-" OR second_string="-")

如果您的id字段在其他索引中有不同的名稱,請在stats行之前像這樣rename

| rename otherIdFieldName as id

這種格式的優點:

  • 您不受子搜索約束的限制(搜索必須在 60 秒內完成,不超過 50k 行)
  • 搜索節點(即索引器)將處理所有開銷,而不必等待啟動搜索的搜索頭進行大量后處理(所有 SH 所做的就是發送分布式搜索,然后發送一個后stats )過濾以確保first_stringfirst_string second_string具有您要查找的值)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM