教程:监视 Azure 中的 Windows 虚拟机Tutorial: Monitor a Windows virtual machine in Azure

Azure 监视使用代理从 Azure VM 收集启动和性能数据,将此数据存储在 Azure 存储中,并使其可供通过门户、Azure PowerShell 模块和 Azure CLI 进行访问。Azure monitoring uses agents to collect boot and performance data from Azure VMs, store this data in Azure storage, and make it accessible through portal, the Azure PowerShell module, and Azure CLI. 用于 VM 的 Azure Monitor 通过收集性能指标、发现 VM 上安装的应用程序组件来提供高级监视,并包括性能图表和依赖关系图。Advanced monitoring is delivered with Azure Monitor for VMs by collecting performance metrics, discover application components installed on the VM, and includes performance charts and dependency map.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 在 VM 上启用启动诊断Enable boot diagnostics on a VM
  • 查看启动诊断View boot diagnostics
  • 查看 VM 主机指标View VM host metrics
  • 启用用于 VM 的 Azure MonitorEnable Azure Monitor for VMs
  • 查看 VM 性能指标View VM performance metrics
  • 创建警报Create an alert

启动 Azure 本地 ShellLaunch Azure local Shell

Note

本文进行了更新,以便使用新的 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.

创建虚拟机Create virtual machine

若要在本教程中配置 Azure 监视和更新管理,需要 Azure 中的 Windows VM。To configure Azure monitoring and update management in this tutorial, you need a Windows VM in Azure. 首先,使用 Get-Credential 设置 VM 的管理员用户名和密码:First, set an administrator username and password for the VM with Get-Credential:

$cred = Get-Credential

现在,使用 New-AzVM 创建 VM。Now create the VM with New-AzVM. 以下示例在“ChinaEast”位置 创建一个名为 myVM 的 VM。The following example creates a VM named myVM in the ChinaEast location. 如果资源组 myResourceGroupMonitorMonitor 和支持的网络资源不存在,则会创建它们:If they do not already exist, the resource group myResourceGroupMonitorMonitor and supporting network resources are created:

New-AzVm `
    -ResourceGroupName "myResourceGroupMonitor" `
    -Name "myVM" `
    -Location "China East" `
    -Credential $cred

创建资源和 VM 需要几分钟的时间。It takes a few minutes for the resources and VM to be created.

查看启动诊断View boot diagnostics

当 Windows 虚拟机启动时,启动诊断代理将捕获屏幕输出,可以使用该输出进行故障排除。As Windows virtual machines boot up, the boot diagnostic agent captures screen output that can be used for troubleshooting purpose. 此功能是默认启用的。This capability is enabled by default. 捕获的屏幕截图存储在一个 Azure 存储帐户中,该帐户也是默认创建的。The captured screenshots are stored in an Azure storage account, which is also created by default.

可以使用 Get-AzureRmVMBootDiagnosticsData 命令获取启动诊断数据。You can get the boot diagnostic data with the Get-AzureRmVMBootDiagnosticsData command. 在下面的示例中,启动诊断下载到了 *c:* 驱动器的根目录中。In the following example, boot diagnostics are downloaded to the root of the *c:* drive.

Get-AzVMBootDiagnosticsData -ResourceGroupName "myResourceGroupMonitor" -Name "myVM" -Windows -LocalPath "c:\"

查看主机指标View host metrics

Windows VM 在 Azure 中有一个与它交互的专用主机 VM。A Windows VM has a dedicated Host VM in Azure that it interacts with. 系统会自动收集该主机的指标,可以在 Azure 门户中查看这些指标。Metrics are automatically collected for the Host and can be viewed in the Azure portal.

  1. 在 Azure 门户中单击“资源组”,选择“myResourceGroupMonitor”,并在资源列表中选择“myVM”。 In the Azure portal, click Resource Groups, select myResourceGroupMonitor, and then select myVM in the resource list.

  2. 要查看主机 VM 的性能情况,请在 VM 边栏选项卡上单击“指标”,并选择“可用指标”下的任一主机指标。 Click Metrics on the VM blade, and then select any of the Host metrics under Available metrics to see how the Host VM is performing.

    查看主机指标

创建警报Create alerts

可以根据特定的性能指标创建警报。You can create alerts based on specific performance metrics. 例如,当平均 CPU 使用率超过特定的阈值或者可用磁盘空间低于特定的空间量时,警报可用于发出通知。Alerts can be used to notify you when average CPU usage exceeds a certain threshold or available free disk space drops below a certain amount, for example. 警报显示在 Azure 门户中,也可以通过电子邮件发送。Alerts are displayed in the Azure portal or can be sent via email. 还可以触发 Azure 自动化 Runbook 或 Azure 逻辑应用来响应生成的警报。You can also trigger Azure Automation runbooks or Azure Logic Apps in response to alerts being generated.

以下示例针对平均 CPU 使用率创建警报。The following example creates an alert for average CPU usage.

  1. 在 Azure 门户中单击“资源组”,选择“myResourceGroupMonitor”,并在资源列表中选择“myVM”。 In the Azure portal, click Resource Groups, select myResourceGroupMonitor, and then select myVM in the resource list.

  2. 在 VM 边栏选项卡上单击“警报规则”,然后单击警报边栏选项卡顶部的“添加指标警报”。 Click Alert rules on the VM blade, then click Add metric alert across the top of the alerts blade.

  3. 为警报提供名称,例如 myAlertRuleProvide a Name for your alert, such as myAlertRule

  4. 若要在 CPU 百分比持续 5 分钟超过 1.0 时触发警报,请选中其他所有默认值。To trigger an alert when CPU percentage exceeds 1.0 for five minutes, leave all the other defaults selected.

  5. (可选)选中“电子邮件所有者、参与者和读者”对应的框,以便向他们发送电子邮件通知。 Optionally, check the box for Email owners, contributors, and readers to send email notification. 默认操作是在门户中显示通知。The default action is to present a notification in the portal.

  6. 单击“确定” 按钮。Click the OK button.

后续步骤Next steps

在本教程中,你配置并查看了 VM 的性能。In this tutorial, you configured and viewed performance of your VM. 你已了解如何:You learned how to:

  • 创建资源组和 VMCreate a resource group and VM
  • 在 VM 上启用启动诊断Enable boot diagnostics on the VM
  • 查看启动诊断View boot diagnostics
  • 查看主机指标View host metrics
  • 创建警报Create an alert