To configure Azure Monitor Alerts with the Azure Portal, follow these steps:
- Navigate to the Azure portal and select your resource or resource group that you want to create an alert for.
- Click on “Alerts” under the “Monitoring” section in the left-hand menu.
- Click on “New Alert Rule” to create a new alert rule.
- Define the scope of the alert by selecting a resource, resource group, or subscription.
- Configure the conditions for the alert, including metrics, time range, aggregation, and threshold.
- Choose the action to be taken when the alert is triggered, such as sending an email, SMS message, or webhook notification.
- Review and confirm the alert rule settings, then save and enable the alert.
To configure Azure Monitor Alerts with PowerShell, you can use the New-AzMetricAlertRuleV2 cmdlet. Here’s an example script:
$resourceGroup = "myResourceGroup"
$resourceName = "myVM"
$ruleName = "myAlertRule"
$metricName = "Percentage CPU"
$operator = "GreaterThan"
$threshold = 90
$timeAggregation = "Average"
$timeWindow = "00:05:00"
$actionGroupId = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/microsoft.insights/actionGroups/{actionGroupId}"
New-AzMetricAlertRuleV2 `
-ResourceGroup $resourceGroup `
-TargetResourceId "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/microsoft.compute/virtualMachines/$resourceName" `
-Name $ruleName `
-MetricName $metricName `
-Operator $operator `
-Threshold $threshold `
-TimeAggregation $timeAggregation `
-WindowSize $timeWindow `
-Actions $actionGroupId
This script creates a new alert rule for a virtual machine resource, based on the percentage of CPU utilization. When the CPU utilization is greater than 90% over a 5-minute window, the action group specified by the $actionGroupId variable is notified.
