Breyta

Deila með


Create a new alert rule using the CLI, PowerShell, or an ARM template

You can create a new alert rule using the the CLI, PowerShell, or an Azure Resource Manager template.

Prerequisites

To create or edit an alert rule, you must have the following permissions:

  • Read permission on the target resource of the alert rule.
  • Write permission on the resource group in which the alert rule is created. If you're creating the alert rule from the Azure portal, the alert rule is created by default in the same resource group in which the target resource resides.
  • Read permission on any action group associated to the alert rule, if applicable.

Create a new alert rule using the CLI

You can create a new alert rule using the Azure CLI. The following code examples use Azure Cloud Shell. You can see the full list of the Azure CLI commands for Azure Monitor.

  1. In the portal, select Cloud Shell. At the prompt, use these.

    For example, to create a metric alert rule that monitors if average Percentage CPU on a VM is greater than 90:

     az monitor metrics alert create -n {nameofthealert} -g {ResourceGroup} --scopes {VirtualMachineResourceID} --condition "avg Percentage CPU > 90" --description {descriptionofthealert}
    

Create a new alert rule using PowerShell

  • To create a metric alert rule using PowerShell, use the Add-AzMetricAlertRuleV2 cmdlet.

    Note

    When you create a metric alert on a single resource, the syntax uses the TargetResourceId. When you create a metric alert on multiple resources, the syntax contains the TargetResourceScope, TargetResourceType, and TargetResourceRegion.

  • To create a log search alert rule using PowerShell, use the New-AzScheduledQueryRule cmdlet.
  • To create an activity log alert rule using PowerShell, use the New-AzActivityLogAlert cmdlet.

Create a new alert rule using an ARM template

You can use an Azure Resource Manager template (ARM template) to configure alert rules consistently in all of your environments.

  1. Create a new resource, using the following resource types:

    • For metric alerts: Microsoft.Insights/metricAlerts

      Note

      • We recommend that you create the metric alert using the same resource group as your target resource.
      • Metric alerts for an Azure Log Analytics workspace resource type (Microsoft.OperationalInsights/workspaces) are configured differently than other metric alerts. For more information, see Resource Template for Metric Alerts for Logs.
      • If you are creating a metric alert for a single resource, the template uses the ResourceId of the target resource. If you are creating a metric alert for multiple resources, the template uses the scope, TargetResourceType, and TargetResourceRegion for the target resources.
    • For log search alerts: Microsoft.Insights/scheduledQueryRules
    • For activity log, service health, and resource health alerts: microsoft.Insights/activityLogAlerts
  2. Copy one of the templates from these sample ARM templates.

  3. Edit the template file to contain appropriate information for your alert, and save the file as <your-alert-template-file>.json.

  4. Edit the corresponding parameters file to customize the alert, and save as <your-alert-template-file>.parameters.json.

  5. Set the metricName parameter, using one of the values in Azure Monitor supported metrics.

  6. Deploy the template using PowerShell or the CLI.

Next steps