使用 Azure 監視器建立、檢視及管理傳統計量警示Create, view, and manage classic metric alerts using Azure Monitor

想在其中一個計量超過閾值時收到通知,可以利用 Azure 監視器中的傳統計量警示。Classic metric alerts in Azure Monitor provide a way to get notified when one of your metrics cross a threshold. 傳統計量警示是較舊的功能,允許只針對無維度計量發出警示。Classic metric alerts is an older functionality that allows for alerting only on non-dimensional metrics. 現有的較新功能稱為計量警示,和傳統計量警示相比,功能更強。There is an existing newer functionality called Metric alerts which has improved functionality over classic metric alerts. 您可以在計量警示概觀深入了解新的計量警示功能。You can learn more about the new metric alerts functionality in metric alerts overview. 在此文章中,我們將會說明如何透過 Azure 入口網站、Azure CLI 與 Powershell 建立、檢視及管理傳統計量警示規則。In this article, we will describe how to create, view and manage classic metric alert rules through Azure portal, Azure CLI and Powershell.

使用 Azure 入口網站With Azure portal

  1. 入口網站中,找出您想要監視的資源,然後選取它。In the portal, locate the resource that you want to monitor, and then select it.

  2. 選取 [監視] 區段底下的 [警示 (傳統)] 。In the MONITORING section, select Alerts (Classic). 不同資源的文字和圖示會有些許不同。The text and icon might vary slightly for different resources. 如果在此處找不到 [警示 (傳統)] ,可能會位在 [警示] 或 [警示規則] 中。If you don't find Alerts (Classic) here, you might find it in Alerts or Alert Rules.

    監視

  3. 選取 [新增計量警示 (傳統)] 命令,然後填寫各欄位。Select the Add metric alert (classic) command, and then fill in the fields.

    新增警示

  4. 命名警示規則。Name your alert rule. 然後選擇也會顯示在電子郵件通知中的 [描述] 。Then choose a Description, which also appears in notification emails.

  5. 選取您想要監視的計量Select the Metric that you want to monitor. 然後選擇計量的條件閾值Then choose a Condition and Threshold value for the metric. 同時選擇警示觸發程序之前,計量規則必須滿足的 [期間] 。Also choose the Period of time that the metric rule must be satisfied before the alert triggers. 例如,如果您使用「超過最後 5 分鐘」期間,且您的警示會尋找高於 80% 的 CPU,當 CPU 已持續 5 分鐘高於 80%,警示就會觸發。For example, if you use the period "Over the last 5 minutes" and your alert looks for a CPU above 80%, the alert triggers when the CPU has been consistently above 80% for 5 minutes. 發生第一次觸發之後,它會在 CPU 持續 5 分鐘低於 80 % 時再次觸發。After the first trigger occurs, it triggers again when the CPU stays below 80% for 5 minutes. 系統每分鐘會測量一次 CPU 計量。The CPU metric measurement happens every minute.

  6. 如果您想要讓系統管理員和共同管理員在警示引發時收到電子郵件通知,請選取 [電子郵件的擁有者...] 。Select Email owners... if you want administrators and co-administrators to receive email notifications when the alert fires.

  7. 如果您想要在警示引發時傳送通知給其他電子郵件地址,請在 [其他管理員的電子郵件] 欄位中新增。If you want to send notifications to additional email addresses when the alert fires, add them in the Additional Administrator email(s) field. 使用以下列格式的分號分隔多個電子郵件:電子郵件@contoso.com;email2@contoso.comSeparate multiple emails with semicolons, in the following format: email@contoso.com;email2@contoso.com

  8. 如果您想在警示引發時呼叫 Webhook,在 [Webhook] 欄位中放入有效的 URI。Put in a valid URI in the Webhook field if you want it to be called when the alert fires.

  9. 如果您使用 Azure 自動化,可以選取警示引發時執行的 Runbook。If you use Azure Automation, you can select a runbook to be run when the alert fires.

  10. 選取 [確定] 可建立警示。Select OK to create the alert.

在幾分鐘之內,警示會開始作用,且先前所述觸發。Within a few minutes, the alert is active and triggers as previously described.

建立警示之後,您可以選取此警示,然後進行下列其中一個工作:After you create an alert, you can select it and do one of the following tasks:

  • 檢視圖表,其中顯示計量閾值與前一天的實際值。View a graph that shows the metric threshold and the actual values from the previous day.
  • 編輯或刪除警示。Edit or delete it.
  • 如果您想要暫時停止或恢復接收警示的通知,可以停用啟用警示。Disable or Enable it if you want to temporarily stop or resume receiving notifications for that alert.

透過 Azure CLIWith Azure CLI

先前幾節說明如何使用 Azure 入口網站建立、檢視及管理計量警示規則。The previous sections described how to create, view and manage metric alert rules using Azure portal. 此節說明如何使用跨平台 Azure CLI 執行相同的作業。This section will describe how to do the same using cross-platform Azure CLI. 開始使用 Azure CLI 最快的方式就是透過 Azure Cloud ShellQuickest way to start using Azure CLI is through Azure Cloud Shell.

取得資源群組中的所有傳統警示規則Get all classic metric alert rules in a resource group

az monitor alert list --resource-group <group name>

查看特定傳統計量警示規則的詳細資訊See details of a particular classic metric alert rule

az monitor alert show --resource-group <group name> --name <alert name>

建立傳統計量警示規則Create a classic metric alert rule

az monitor alert create --name <alert name> --resource-group <group name> \
    --action email <email1 email2 ...> \
    --action webhook <URI> \
    --target <target object ID> \
    --condition "<METRIC> {>,>=,<,<=} <THRESHOLD> {avg,min,max,total,last} ##h##m##s"

刪除傳統計量警示規則Delete a classic metric alert rule

az monitor alert delete --name <alert name> --resource-group <group name>

透過 PowerShellWith PowerShell

注意

本文已更新為使用新的 Azure PowerShell Az 模組。This article has been updated to use the new Azure PowerShell Az module. AzureRM 模組在至少 2020 年 12 月之前都還會持續收到錯誤 (Bug) 修正,因此您仍然可以持續使用。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要深入了解新的 Az 模組和 AzureRM 的相容性,請參閱新的 Azure PowerShell Az 模組簡介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 如需 Az 模組安裝指示,請參閱安裝 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

此節示範如何使用 PowerShell 命令建立、檢視及管理傳統計量警示。文章中的範例說明針對傳統計量警示使用 Azure 監視器 Cmdlet 的方式。This sections shows how to use PowerShell commands create, view and manage classic metric alerts.The examples in the article illustrate how you can use Azure Monitor cmdlets for classic metric alerts.

  1. 設定要在電腦上執行的 PowerShell (如果您還未設定)。If you haven't already, set up PowerShell to run on your computer. 如需詳細資訊,請參閱如何安裝及設定 PowerShellFor more information, see How to Install and Configure PowerShell. 您也可以在 Azure 監視器 Cmdlet 檢閱整個 Azure 監視器 PowerShell Cmdlet 清單。You can also review the entire list of Azure Monitor PowerShell cmdlets at Azure Monitor (Insights) Cmdlets.

  2. 首先,登入您的 Azure 訂用帳戶。First, log in to your Azure subscription.

    Connect-AzAccount
    
  3. 您會看到登入畫面。You'll see a sign in screen. 一旦登入之後,就會顯示您的帳戶、TenantID 和預設的訂用帳戶識別碼。Once you sign in your Account, TenantID, and default Subscription ID are displayed. 所有 Azure Cmdlet 都會在您的預設訂用帳戶內容中運作。All the Azure cmdlets work in the context of your default subscription. 若要檢視您具有存取權的訂用帳戶的清單,請使用下列命令:To view the list of subscriptions you have access to, use the following command:

    Get-AzSubscription
    
  4. 若要將使用中的內容變更為不同的訂用帳戶,請使用下列命令:To change your working context to a different subscription, use the following command:

    Set-AzContext -SubscriptionId <subscriptionid>
    
  5. 您可以擷取資源群組中的所有傳統計量警示規則:You can retrieve all classic metric alert rules on a resource group:

    Get-AzAlertRule -ResourceGroup montest
    
  6. 您可以檢視傳統計量警示規則的詳細資料You can view details of a classic metric alert rule

    Get-AzAlertRule -Name simpletestCPU -ResourceGroup montest -DetailedOutput
    
  7. 您可以擷取針對目標資源設定的所有警示規則。You can retrieve all alert rules set for a target resource. 例如,在 VM 上設定的所有警示規則。For example, all alert rules set on a VM.

    Get-AzAlertRule -ResourceGroup montest -TargetResourceId /subscriptions/s1/resourceGroups/montest/providers/Microsoft.Compute/virtualMachines/testconfig
    
  8. 無法再透過 PowerShell 建立傳統的警示規則。Classic alert rules can no longer be created via PowerShell. 若要建立您要使用新的警示規則' 新增 AzMetricAlertRule'命令。To create an alert rule you need to use the new 'Add-AzMetricAlertRule' command.

後續步驟Next steps