viernes, 5 de marzo de 2021

Script to add an alert to an Azure App Service using Powershell

With this script, you can add an alert about the number of HTTP errors to an Azure App Service.

The same script could be used to add any kind of alert to any kind of Azure Resource.

---------------------------------------------------

Login-AzAccount -Subscription d9e************4d612 -Tenant b4773745-***********a79a457c

$act = Get-AzActionGroup -Name AG-Alert -ResourceGroupName the_resource_group

$condition = New-AzMetricAlertRuleV2Criteria -MetricName "Http5xx" -MetricNameSpace "microsoft.web/sites" -TimeAggregation "Total" -Operator GreaterThan -Threshold 10

$alertName = "HTTP_Errors"

$RGroupName = "the_resource_group"

$website = "the_app_service"

$alertDescription = "Alert HTTP Errors for $website"

$TResourceId = "/subscriptions/d9ecfd*********74d612/resourceGroups/$RGroupName/providers/Microsoft.Web/sites/$website"

Add-AzMetricAlertRuleV2 -Condition $condition -Frequency 0:5 -Name $alertName -ResourceGroupName $RGroupName -Severity 3 -TargetResourceId $TResourceId -WindowSize 0:5 -ActionGroupId $act.Id -description $alertDescription

---------------------------------------------------


Before executing the script, we have to declare the variables needed to create the alert, according to our needs and environment:

$act is the action we want to be executed when the alert is triggered. The most basic is to send an email. The action should be create before create the alert. This same action can be used in several alerts

$condition is the criteria that will trigger the alert. In this case will evaluate if there are more than 10 HTTP errors

$alertName will be a descriptive name for the alert

$RGroupName is the name of the Resource Group where the App Service is located

$website is the name of the App Service we are going to add the alert.

$alertDescription will be a description for the alert.

In the Add-AzMetricAlertRuleV2 we will specify the frequency, the severity and the period (WindowSize) that will be evaluated.

No hay comentarios:

Publicar un comentario