將記錄搜尋警示查詢最佳化

本文描述如何撰寫及轉換記錄搜尋警示,以達到最佳效能。 最佳化查詢可減少經常執行的警示延遲和負載。

開始撰寫警示記錄查詢

警示查詢會從查詢 Log Analytics 中的記錄資料開始,並指出問題。 若要了解您可以探索的內容,請參閱在 Azure 監視器 Log Analytics 中使用查詢。 您也可以開始撰寫自己的查詢

指出問題的查詢,而非警示

建置警示流程是為了將指出問題的結果轉換為警示。 例如,在類似下列查詢的情況下:

SecurityEvent
| where EventID == 4624

如果使用者的意圖是為了警示,當此事件類型發生時,警示邏輯會將 count 附加至查詢。 執行的查詢會是:

SecurityEvent
| where EventID == 4624
| count

您不需要將警示邏輯新增至查詢,這麼做甚至可能會造成問題。 在上述範例中,若您在查詢中包含 count,則一律會產生值 1,因為警示服務會執行 countcount

避免限制及採用運算子

在查詢中使用 limittake 可能會增加警示的延遲與負載,因為結果在一段時間內不一致。 只在需要時使用這些運算子。

記錄查詢限制式

Azure 監視器中的記錄查詢會從資料表 searchunion 運算子開始。

針對記錄搜尋警示規則進行的查詢應一律以資料表為開頭,以定義明確的範圍,藉以改善查詢效能與結果相關性。 警示規則中的查詢經常執行。 使用 searchunion 可能會導致過多的額外負荷,這會增加警示的延遲,因為其需要跨多個資料表進行掃描。 這些運算子也會減少警示服務最佳化查詢的能力。

除了跨資源查詢以外,我們不支援建立或修改使用 searchunion 運算子的記錄搜尋警示規則。

例如,下列警示查詢只限於 SecurityEvent 資料表,並搜尋特定事件識別碼。 這是查詢必須處理的唯一資料表。

SecurityEvent
| where EventID == 4624

使用跨資源查詢的記錄搜尋警示規則不受此變更所影響,因為跨資源查詢使用 union 類型,其會將查詢範圍限制於特定資源。 下列範例是有效的記錄搜尋警示查詢:

union
app('00000000-0000-0000-0000-000000000001').requests,
app('00000000-0000-0000-0000-000000000002').requests,
workspace('00000000-0000-0000-0000-000000000003').Perf 

注意

新的 scheduledQueryRules API 支援跨資源查詢。 若您仍然使用舊版 Log Analytics 警示 API 來建立記錄搜尋警示,請參閱將舊版規則管理升級至目前的 Azure 監視器排定查詢規則 API,以了解如何切換。

範例

下列範例包含使用 searchunion 的記錄查詢。 其會提供可用於修改這些查詢的步驟,以用於警示規則。

範例 1

您想要使用下列查詢建立記錄搜尋警示規則,以便使用 search 擷取效能資訊:

search *
| where Type == 'Perf' and CounterName == '% Free Space'
| where CounterValue < 30
  1. 若要修改此查詢,請先使用下列查詢識別屬性隸屬的資料表:

    search *
    | where CounterName == '% Free Space'
    | summarize by $table
    

    此查詢的結果會顯示 CounterName 屬性來自於 Perf 資料表。

  2. 使用此結果可建立下列查詢,其可用於警示規則:

    Perf
    | where CounterName == '% Free Space'
    | where CounterValue < 30
    

範例 2

您想要使用下列查詢建立記錄搜尋警示規則,以便使用 search 擷取效能資訊:

search ObjectName =="Memory" and CounterName=="% Committed Bytes In Use"
| summarize Avg_Memory_Usage =avg(CounterValue) by Computer
| where Avg_Memory_Usage between(90 .. 95)  
  1. 若要修改此查詢,請先使用下列查詢識別屬性隸屬的資料表:

    search ObjectName=="Memory" and CounterName=="% Committed Bytes In Use"
    | summarize by $table
    

    此查詢的結果會顯示 ObjectNameCounterName 屬性來自於 Perf 資料表。

  2. 使用此結果可建立下列查詢,其可用於警示規則:

    Perf
    | where ObjectName =="Memory" and CounterName=="% Committed Bytes In Use"
    | summarize Avg_Memory_Usage=avg(CounterValue) by Computer
    | where Avg_Memory_Usage between(90 .. 95)
    

範例 3

您想要使用下列查詢來建立記錄搜尋警示規則,該查詢會使用 searchunion 來擷取效能資訊:

search (ObjectName == "Processor" and CounterName == "% Idle Time" and InstanceName == "_Total")
| where Computer !in (
    union *
    | where CounterName == "% Processor Utility"
    | summarize by Computer)
| summarize Avg_Idle_Time = avg(CounterValue) by Computer
  1. 若要修改此查詢,請先使用下列查詢,在查詢的第一個部分中識別屬性隸屬的資料表:

    search (ObjectName == "Processor" and CounterName == "% Idle Time" and InstanceName == "_Total")
    | summarize by $table
    

    此查詢的結果會顯示所有這些屬性來自於 Perf 資料表。

  2. 使用 unionwithsource 命令,識別已提供每個資料列的來源資料表:

    union withsource=table *
    | where CounterName == "% Processor Utility"
    | summarize by table
    

    此查詢的結果會顯示這些屬性也來自於 Perf 資料表。

  3. 使用這些結果可建立下列查詢,其可用於警示規則:

    Perf
    | where ObjectName == "Processor" and CounterName == "% Idle Time" and InstanceName == "_Total"
    | where Computer !in (
        (Perf
        | where CounterName == "% Processor Utility"
        | summarize by Computer))
    | summarize Avg_Idle_Time = avg(CounterValue) by Computer
    

範例 4

您想要使用下列查詢建立記錄搜尋警示規則,以便聯結兩個 search 查詢的結果:

search Type == 'SecurityEvent' and EventID == '4625'
| summarize by Computer, Hour = bin(TimeGenerated, 1h)
| join kind = leftouter (
    search in (Heartbeat) OSType == 'Windows'
    | summarize arg_max(TimeGenerated, Computer) by Computer , Hour = bin(TimeGenerated, 1h)
    | project Hour , Computer
) on Hour
  1. 若要修改此查詢,請先使用下列查詢,識別聯結的左邊有這些屬性的資料表:

    search Type == 'SecurityEvent' and EventID == '4625'
    | summarize by $table
    

    結果指出聯結左側出現的屬性屬於 SecurityEvent 資料表。

  2. 使用下列查詢,找出聯結右側中包含這些屬性的資料表:

    search in (Heartbeat) OSType == 'Windows'
    | summarize by $table
    

    結果指出聯結右側的屬性屬於 Heartbeat 資料表。

  3. 使用這些結果可建立下列查詢,其可用於警示規則:

    SecurityEvent
    | where EventID == '4625'
    | summarize by Computer, Hour = bin(TimeGenerated, 1h)
    | join kind = leftouter (
        Heartbeat
        | where OSType == 'Windows'
        | summarize arg_max(TimeGenerated, Computer) by Computer , Hour = bin(TimeGenerated, 1h)
        | project Hour , Computer
    ) on Hour
    

下一步