您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

使用 Azure Monitor 创建、查看和管理经典指标警报Create, view, and manage classic metric alerts using Azure Monitor

Azure Monitor 中的经典指标警报提供了一种在指标超出阈值时获得通知的方法。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 分钟”,且警报针对 CPU 高于 80% 的情况,则 CPU 持续高于 80% 达到 5 分钟时将触发警报。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. 如果希望在触发警报时调用有效的 URI,请将其放入“Webhook”**** 字段中。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 Monitor 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 监视器 (Insights) 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 和默认订阅 ID。Once you sign in your Account, TenantID, and default Subscription ID are displayed. 所有 Azure cmdlets 都在默认订阅的上下文中工作。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. 例如,虚拟机上的所有警报规则设置。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. 若要创建警报规则,需要使用新的 Add-AzMetricAlertRule 命令。To create an alert rule you need to use the new 'Add-AzMetricAlertRule' command.

后续步骤Next steps