Azure Monitor CLI 快速入门示例Azure Monitor CLI quick start samples

本文演示有助于访问 Azure 监视器功能的示例命令行接口 (CLI) 命令。This article shows you sample command-line interface (CLI) commands to help you access Azure Monitor features. Azure Monitor 允许基于配置的遥测数据值自动缩放云服务、虚拟机和 Web 应用,以及发送警报通知或调用 Web URL。Azure Monitor allows you to AutoScale Cloud Services, Virtual Machines, and Web Apps and to send alert notifications or call web URLs based on values of configured telemetry data.

先决条件Prerequisites

如果尚未安装 Azure CLI,请按照安装 Azure CLI 的说明进行操作。If you haven't already installed the Azure CLI, follow the instructions for Install the Azure CLI. 请参阅 Azure Monitor CLI 参考中的所有可用命令的完整参考。See a full reference of all available commands in the Azure Monitor CLI reference.

登录 AzureLog in to Azure

第一步是登录 Azure 帐户。The first step is to login to your Azure account.

az cloud set --name AzureChinaCloud
az login

运行此命令后,必须按照屏幕上的说明进行登录。After running this command, you have to sign in via the instructions on the screen. 所有命令都在默认订阅的上下文中工作。All commands work in the context of your default subscription.

若要列出当前订阅的详细信息,请使用以下命令。To list the details of your current subscription, use the following command.

az account show

要将工作上下文更改为其他订阅,请使用以下命令。To change working context to a different subscription, use the following command.

az account set -s <Subscription ID or name>

若要查看所有支持的 Azure Monitor 命令的列表,请执行以下操作。To view a list of all supported Azure Monitor commands, perform the following.

az monitor -h

查看订阅的活动日志View activity log for a subscription

若要查看活动日志事件的列表,请执行以下操作。To view a list of activity log events, perform the following.

az monitor activity-log list

尝试以下操作以查看所有可用选项。Try the following to view all available options.

az monitor activity-log list -h

下面是一个按 resourceGroup 列出日志的示例Here is an example to list logs by a resourceGroup

az monitor activity-log list --resource-group <group name>

按调用方列出日志的示例Example to list logs by caller

az monitor activity-log list --caller myname@company.com

在日期范围内,按调用方对资源类型列出日志的示例Example to list logs by caller on a resource type, within a date range

az monitor activity-log list --resource-provider Microsoft.Web \
    --caller myname@company.com \
    --start-time 2016-03-08T00:00:00Z \
    --end-time 2016-03-16T00:00:00Z

使用警报Work with alerts

备注

目前,仅在 CLI 中支持警报(经典)。Only alerts (classic) is supported in CLI at this time.

在资源组中获取警报(经典)规则Get alert (classic) rules in a resource group

az monitor activity-log alert list --resource-group <group name>
az monitor activity-log alert show --resource-group <group name> --name <alert name>

创建指标警报(经典)规则Create a metric alert (classic) 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 an alert (classic) rule

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

日志配置文件Log profiles

可以按照此部分中的信息使用日志配置文件。Use the information in this section to work with log profiles.

获取日志配置文件Get a log profile

az monitor log-profiles list
az monitor log-profiles show --name <profile name>

添加具有保留期的日志配置文件Add a log profile with retention

az monitor log-profiles create --name <profile name> --location <location of profile> \
    --locations <locations to monitor activity in: location1 location2 ...> \
    --categories <categoryName1 categoryName2 ...> \
    --days <# days to retain> \
    --enabled true \
    --storage-account-id <storage account ID to store the logs in>

添加具有保留期和 EventHub 的日志配置文件Add a log profile with retention and EventHub

az monitor log-profiles create --name <profile name> --location <location of profile> \
    --locations <locations to monitor activity in: location1 location2 ...> \
    --categories <categoryName1 categoryName2 ...> \
    --days <# days to retain> \
    --enabled true
    --storage-account-id <storage account ID to store the logs in>
    --service-bus-rule-id <service bus rule ID to stream to>

删除日志配置文件Remove a log profile

az monitor log-profiles delete --name <profile name>

诊断Diagnostics

可以按照此部分中的信息使用诊断设置。Use the information in this section to work with diagnostic settings.

获取诊断设置Get a diagnostic setting

az monitor diagnostic-settings list --resource <target resource ID>

创建诊断日志设置Create a diagnostic log setting

az monitor diagnostic-settings create --name <diagnostic name> \
    --storage-account <storage account ID> \
    --resource <target resource object ID> \
    --logs '[
    {
        "category": <category name>,
        "enabled": true,
        "retentionPolicy": {
            "days": <# days to retain>,
            "enabled": true
        }
    }]'

删除诊断设置Delete a diagnostic setting

az monitor diagnostic-settings delete --name <diagnostic name> \
    --resource <target resource ID>

自动缩放Autoscale

按照此部分中的信息使用自动缩放设置。Use the information in this section to work with autoscale settings. 需要修改这些示例。You need to modify these examples.

获取资源组的自动缩放设置Get autoscale settings for a resource group

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

按名称获取资源组的自动缩放设置Get autoscale settings by name in a resource group

az monitor autoscale show --name <settings name> --resource-group <group name>

设置自动缩放设置Set autoscale settings

az monitor autoscale create --name <settings name> --resource-group <group name> \
    --count <# instances> \
    --resource <target resource ID>