Create classic metric alerts in Azure Monitor for Azure services - Cross-platform CLI
This article describes how to create older classic metric alerts. Azure Monitor now supports newer, better metric alerts. These alerts can monitor multiple metrics and allow for alerting on dimensional metrics. CLI support for newer metric alerts is coming soon.
This article shows you how to set up Azure classic metric alerts using the cross-platform Command Line Interface (CLI).
Azure Monitor is the new name for what was called "Azure Insights" until Sept 25th, 2016. However, the namespaces and thus the commands below still contain the "insights".
You can receive an alert based on monitoring metrics for, or events on, your Azure services.
- Metric values - The alert triggers when the value of a specified metric crosses a threshold you assign in either direction. That is, it triggers both when the condition is first met and then afterwards when that condition is no longer being met.
- Activity log events - An alert can trigger on every event, or, only when a certain event occurs. To learn more about activity log alerts click here
You can configure a classic metric alert to do the following when it triggers:
- send email notifications to the service administrator and co-administrators
- send email to additional emails that you specify.
- call a webhook
- start execution of an Azure runbook (only from the Azure portal at this time)
You can configure and get information about classic metric alert rules using
You can always receive help for commands by typing a command and putting -help at the end. For example:
```console azure insights alerts -help azure insights alerts actions email create -help ```
Create alert rules using the CLI
Perform the Prerequisites and login to Azure. See Azure Monitor CLI samples. In short, install the CLI and run these commands. They get you logged in, show what subscription you are using, and prepare you to run Azure Monitor commands.
azure login azure account show azure config mode arm
To list existing rules on a resource group, use the following form azure insights alerts rule list [options] <resourceGroup>
azure insights alerts rule list myresourcegroupname
To create a rule, you need to have several important pieces of information first.
- The Resource ID for the resource you want to set an alert for
The metric definitions available for that resource
One way to get the Resource ID is to use the Azure portal. Assuming the resource is already created, select it in the portal. Then in the next blade, select Properties under the Settings section. The RESOURCE ID is a field in the next blade. Another way is to use the Azure Resource Explorer.
An example resource id for a web app is
To get a list of the available metrics and units for those metrics for the previous resource example, use the following CLI command:
azure insights metrics list /subscriptions/dededede-7aa0-407d-a6fb-eb20c8bd1192/resourceGroups/myresourcegroupname/providers/Microsoft.Web/sites/mywebsitename PT1M
PT1M is the granularity of the available measurement (1-minute intervals). Using different granularities gives you different metric options.
To create a metric-based alert rule, use a command of the following form:
azure insights alerts rule metric set [options] <ruleName> <location> <resourceGroup> <windowSize> <operator> <threshold> <targetResourceId> <metricName> <timeAggregationOperator>
The following example sets up an alert on a web site resource. The alert triggers whenever it consistently receives any traffic for 5 minutes and again when it receives no traffic for 5 minutes.
azure insights alerts rule metric set myrule eastus myreasourcegroup PT5M GreaterThan 2 /subscriptions/dededede-7aa0-407d-a6fb-eb20c8bd1192/resourceGroups/myresourcegroupname/providers/Microsoft.Web/sites/mywebsitename BytesReceived Total
To create webhook or send email when a classic metric alert fires, first create the email and/or webhooks. Then create the rule immediately afterwards. You cannot associate webhook or emails with already created rules using the CLI.
azure insights alerts actions email create --customEmails firstname.lastname@example.org azure insights alerts actions webhook create https://www.contoso.com azure insights alerts rule metric set myrulewithwebhookandemail eastus myreasourcegroup PT5M GreaterThan 2 /subscriptions/dededede-7aa0-407d-a6fb-eb20c8bd1192/resourceGroups/myresourcegroupname/providers/Microsoft.Web/sites/mywebsitename BytesReceived Total
You can verify that your alerts have been created properly by looking at an individual rule.
azure insights alerts rule list myresourcegroup --ruleName myrule
To delete rules, use a command of the form:
insights alerts rule delete [options] <resourceGroup> <ruleName>
These commands delete the rules previously created in this article.
azure insights alerts rule delete myresourcegroup myrule azure insights alerts rule delete myresourcegroup myrulewithwebhookandemail azure insights alerts rule delete myresourcegroup myActivityLogRule
- Get an overview of Azure monitoring including the types of information you can collect and monitor.
- Learn more about configuring webhooks in alerts.
- Learn more about configuring alerts on Activity log events.
- Learn more about Azure Automation Runbooks.
- Get an overview of collecting diagnostic logs to collect detailed high-frequency metrics on your service.
- Get an overview of metrics collection to make sure your service is available and responsive.