0

I am trying to create alerts for databases in Azure using Add-AzMetricAlertRuleV2 command , but approach is working for other resources types like ASP , app services. not working with databases . can some one help me to figure it out.

 $ResourceName = "s0010asq/mfwt1"
$jsonFiles = "sqlalert"
$MetricName = "storage_percent"
$MetricNamespace = "Microsoft.Sql/servers/databases"
$Operator = "GreaterThan"
$Threshold = "90.0"
$TimeAggregation = "Average"
$Actiongroup = "/subscriptions/35324-3245-3245-3425-32453452345/resourceGroups/s00149rgp/providers/microsoft.insights/actionGroups/Infra Sev4"
$WindowSize = "5"
$Frequency = "5"
$Alerts_Severity = "4"
Set-AzContext -Subscription "s00yrjfjhjjhdopdnu74" | Out-Null
$scope = (Get-AzResource -ResourceGroupName $ResourceGroupNames -Name $ResourceName).ResourceId
$alertName = "$ResourceName $MetricName High"

                                Write-Output "`n`n[34;1m**creating $alertName alert with template $($jsonFiles.FullName)**[0m"
                                $condition = New-AzMetricAlertRuleV2Criteria  -MetricName $MetricName `
                                    -MetricNamespace $MetricNamespace `
                                    -Operator $Operator `
                                    -Threshold $Threshold `
                                    -TimeAggregation $TimeAggregation `
                                     
                                Add-AzMetricAlertRuleV2 -Name $alertName -ResourceGroupName $ResourceGroupNames `
                                    -TargetResourceId $scope `
                                    -ActionGroupId $Actiongroup `
                                    -WindowSize $WindowSize `
                                    -Frequency $Frequency `
                                    -Condition $condition `
                                    -Severity $Alerts_Severity `
                                    -Description $alertName

Getting error like :

Add-AzMetricAlertRuleV2 : Exception type: ErrorResponseException, Message: Null/Empty, Code: Null, Status code:NotFound, Reason phrase: Not Found
At line:24 char:33
+ ...             Add-AzMetricAlertRuleV2 -Name $alertName -ResourceGroupNa ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Add-AzMetricAlertRuleV2], PSInvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Alerts.AddAzureRmMetricAlertRuleV2Command
> Blockquote
2
  • In your code, you have not specified the variable $ResourceGroupNames anywhere, yet you are using it for the -ResourceGroupName parameter. Commented Aug 9, 2024 at 4:42
  • You said it is only happening with databases. Use SQL Server Management Studio and get log files which is in the explorer under Management. There should be a login message when you connect in the PS cmdlet. There should be a User and Group that is used for the connection. There may be an error message that explains the issue. Commented Aug 9, 2024 at 6:31

1 Answer 1

0

Add-AzMetricAlertRuleV2 - Getting Not found error

In your code, the $ResourceGroupNames variable is not defined, and the values for Frequency and Alerts_Severity are provided as follows

$WindowSize = [System.TimeSpan]::Parse("00:05:00")
$Frequency = [System.TimeSpan]::Parse("00:05:00")

When I use your code, I also get the same error.

Message: Null/Empty, Code: Null, Status code:NotFound, Reason phrase: Not Found

enter image description here

Here is the updated code to create a alert for SQL Server

$SubscriptionId = "zzzzzznfnfnfnfnffnf"
$ResourceGroupNames = "SQLDB-RG"
$ResourceName= "venkatserverdemo/venkatDB"
$MetricName = "storage_percent"
$Threshold = 90
$TimeAggregation = "Average"
$Operator = "GreaterThan"
$WindowSize = [System.TimeSpan]::Parse("00:05:00")
$Frequency = [System.TimeSpan]::Parse("00:05:00")
$Severity = 4
$ActionGroupId = "/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupNames/providers/microsoft.insights/actiongroups/venkatacg"
$NewAlertName = "venkatserveralert"

Set-AzContext -Subscription $SubscriptionId | Out-Null

$scope = (Get-AzResource -ResourceGroupName $ResourceGroupNames -Name $ResourceName).ResourceId
$Condition = New-AzMetricAlertRuleV2Criteria -MetricName $MetricName `
    -MetricNamespace "Microsoft.Sql/servers/databases" `
    -Operator $Operator `
    -Threshold $Threshold `
    -TimeAggregation $TimeAggregation

Add-AzMetricAlertRuleV2 -Name $NewAlertName `
    -ResourceGroupName $ResourceGroupName `
    -TargetResourceId $Scope `
    -ActionGroupId $ActionGroupId `
    -WindowSize $WindowSize `
    -Frequency $Frequency `
    -Condition $Condition `
    -Severity $Severity `
    -Description "Alert for $MetricName with new name"

Output:

enter image description here

The alert was created successfully after running the PowerShell code.

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

$ResourceName what about this value
In my code i have hardcoded resource group I'd ,if you want you can fetch the I'd from code.
Updated code with $ResourceName.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.