Azure Monitor CLI samples

This article shows you sample command-line interface (CLI) commands to help you access Azure Monitor features. 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

If you haven't already installed the Azure CLI, follow the instructions for Install the Azure CLI. See a full reference of all available commands in the Azure Monitor CLI reference.

Log in to Azure

The first step is to log in to your Azure account.

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.

List the details of your current subscription.

az account show

Change working context to a different subscription.

az account set -s <Subscription ID or name>

View a list of all supported Azure Monitor commands.

az monitor -h

View activity log

View a list of activity log events.

az monitor activity-log list

View all available options.

az monitor activity-log list -h

List logs by a resourceGroup.

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

List logs by caller.

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

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

Note

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>

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 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>