教程:监视 Azure 中的 Linux 虚拟机Tutorial: Monitor a Linux 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
  • 创建警报Create an alert

启动 Azure 本地 ShellLaunch Azure local Shell


在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

如果选择在本地安装并使用 CLI,本教程要求运行 Azure CLI 2.0.30 或更高版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.30 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

创建 VMCreate VM

若要查看诊断和指标的状态,需要创建一个 VM。To see diagnostics and metrics in action, you need a VM. 首先,使用 az group create 创建资源组。First, create a resource group with az group create. 以下示例在“chinaeast” 位置创建名为“myResourceGroupMonitor” 的资源组。The following example creates a resource group named myResourceGroupMonitor in the chinaeast location.

az group create --name myResourceGroupMonitor --location chinaeast

现在,请使用 az vm create 创建 VM。Now create a VM with az vm create. 以下示例将创建名为 myVM 的 VM,并生成 SSH 密钥(如果它们尚不存在于 ~/.ssh/ 中):The following example creates a VM named myVM and generates SSH keys if they do not already exist in ~/.ssh/:

az vm create \
  --resource-group myResourceGroupMonitor \
  --name myVM \
  --image UbuntuLTS \
  --admin-username azureuser \

启用启动诊断Enable boot diagnostics

Linux VM 启动时,启动诊断扩展将捕获启动输出并将其存储在 Azure 存储中。As Linux VMs boot, the boot diagnostic extension captures boot output and stores it in Azure storage. 此数据可以用于排查 VM 启动问题。This data can be used to troubleshoot VM boot issues. 使用 Azure CLI 创建 Linux VM 时,不会自动启用启动诊断。Boot diagnostics are not automatically enabled when you create a Linux VM using the Azure CLI.

在启用启动诊断之前,需要创建一个存储帐户来存储启动日志。Before enabling boot diagnostics, a storage account needs to be created for storing boot logs. 存储帐户的名称必须全局唯一,介于 3 和 24 个字符之间,并且只能包含数字和小写字母。Storage accounts must have a globally unique name, be between 3 and 24 characters, and must contain only numbers and lowercase letters. 使用 az storage account create 命令创建存储帐户。Create a storage account with the az storage account create command. 本示例使用一个随机字符串来创建唯一的存储帐户名称。In this example, a random string is used to create a unique storage account name.


az storage account create \
  --resource-group myResourceGroupMonitor \
  --name $storageacct \
  --sku Standard_LRS \
  --location chinaeast

启用引导诊断时,需要 Blob 存储容器的 URI。When enabling boot diagnostics, the URI to the blob storage container is needed. 以下命令查询存储帐户以返回此 URI。The following command queries the storage account to return this URI. URI 值存储在名为 bloburi 的变量中,将在下一步骤中使用。The URI value is stored in a variable names bloburi, which is used in the next step.

bloburi=$(az storage account show --resource-group myResourceGroupMonitor --name $storageacct --query 'primaryEndpoints.blob' -o tsv)

现在,请使用 az vm boot-diagnostics enable 启用启动诊断。Now enable boot diagnostics with az vm boot-diagnostics enable. --storage 值是在上一步骤中收集的 Blob URI。The --storage value is the blob URI collected in the previous step.

az vm boot-diagnostics enable \
  --resource-group myResourceGroupMonitor \
  --name myVM \
  --storage $bloburi

查看启动诊断View boot diagnostics

启用引导诊断后,每当停止再启动 VM 时,会将有关启动过程的信息写入日志文件。When boot diagnostics are enabled, each time you stop and start the VM, information about the boot process is written to a log file. 本示例首先使用 az vm deallocate 命令解除分配 VM,如下所示:For this example, first deallocate the VM with the az vm deallocate command as follows:

az vm deallocate --resource-group myResourceGroupMonitor --name myVM

现在,请使用 az vm start 命令启动 VM,如下所示:Now start the VM with the az vm start command as follows:

az vm start --resource-group myResourceGroupMonitor --name myVM

可以使用 az vm boot-diagnostics get-boot-log 命令获取 myVM 的启动诊断数据,如下所示:You can get the boot diagnostic data for myVM with the az vm boot-diagnostics get-boot-log command as follows:

az vm boot-diagnostics get-boot-log --resource-group myResourceGroupMonitor --name myVM

查看主机指标View host metrics

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

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

  2. 若要查看主机 VM 的性能情况,请在 VM 窗口中选择“指标”,并选择“可用指标”下面的任一“[主机]”指标。 To see how the host VM is performing, select Metrics on the VM window, then choose any of the [Host] metrics under Available metrics.


创建警报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