共用方式為


包含 ADX 和 ARG 的範例記錄搜尋提醒查詢

記錄搜尋警示規則會使用Log Analytics查詢來評估設定頻率的記錄,來監視資源。 您可以在記錄搜尋警示規則查詢中包含來自 Azure 數據總管和 Azure Resource Graph 的數據。

本文提供使用 Azure 數據總管和 Azure Resource Graph 的記錄搜尋警示規則查詢範例。 如需建立記錄搜尋警示規則的詳細資訊,請參閱 建立記錄搜尋警示規則

檢查虛擬機健康情況的查詢

此查詢會尋找在過去 2 分鐘內標示為沒有活動訊號的虛擬機。

    arg("").Resources
    | where type == "microsoft.compute/virtualmachines"
    | summarize LastCall = max(case(isnull(TimeGenerated), make_datetime(1970, 1, 1), TimeGenerated)) by name, id
    | extend SystemDown = case(LastCall < ago(2m), 1, 0)
    | where SystemDown == 1

此查詢會尋找標示為「重要」的虛擬機,其活動訊號超過24小時前,但在過去2分鐘內沒有活動訊號。

{
    arg("").Resources
    | where type == "microsoft.compute/virtualmachines"
    | where tags.BusinessCriticality =~ 'critical' and subscriptionId == '123-456-123-456'
    | join kind=leftouter (
    Heartbeat
    | where TimeGenerated > ago(24h)
    )
    on $left.name == $right.Resource
    | summarize LastCall = max(case(isnull(TimeGenerated), make_datetime(1970, 1, 1), TimeGenerated)) by name, id
    | extend SystemDown = case(LastCall < ago(2m), 1, 0)
    | where SystemDown == 1
}

查詢,以篩選需要監視的虛擬機

   {
    let RuleGroupTags = dynamic(['Linux']);
    Perf | where ObjectName == 'Processor' and CounterName == '% Idle Time' and (InstanceName in ('_Total,'total'))
    | extend CpuUtilisation = (100 - CounterValue)   
    | join kind=inner hint.remote=left (arg("").Resources
        | where type =~ 'Microsoft.Compute/virtualMachines'
    | project _ResourceId=tolower(id), tags) on _ResourceId
    | project-away _ResourceId1
    | where  (tostring(tags.monitorRuleGroup) in (RuleGroupTags)) 
}

查詢,尋找憑證將在 30 天內到期的資源

{
    arg("").Resources
    | where type == "microsoft.web/certificates"
    | extend ExpirationDate = todatetime(properties.expirationDate)
    | project ExpirationDate, name, resourceGroup, properties.expirationDate
    | where ExpirationDate < now() + 30d
    | order by ExpirationDate asc
}

在訂用帳戶中建立新資源時警示的查詢

{
    arg("").resourcechanges
    | extend changeTime = todatetime(properties.changeAttributes.timestamp), 
    changeType = tostring(properties.changeType),targetResourceType = tostring(properties.targetResourceType),
    changedBy = tostring(properties.changeAttributes.changedBy),
    createdResource = tostring(properties.targetResourceId)
    | where changeType == "Create" and changeTime <ago(1h)
    | project changeTime,createdResource,changedBy

}

下一步