使用Add-AzMetricAlertRuleV2创建Azure数据库警报时遇NotFound错误求助
排查Azure SQL数据库指标警报创建失败(NotFound错误)
使用Add-AzMetricAlertRuleV2命令为Azure SQL数据库创建存储使用率指标警报时,返回NotFound错误,但该命令对App Service等其他资源类型可正常工作。
执行代码
$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
返回错误信息
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
排查步骤及解决方案
1. 验证目标资源ID有效性
- 先确认
$ResourceGroupNames变量已正确赋值为数据库所在的资源组名称。 - 手动执行以下命令,检查目标SQL数据库是否存在:
如果返回空结果,说明资源名称或资源组填写错误,需要修正后再执行。Get-AzResource -ResourceGroupName <你的资源组名> -Name "s0010asq/mfwt1"
2. 修正参数格式问题
- 时间参数:
WindowSize和Frequency必须指定时间单位,支持格式如PT5M(5分钟)、00:05:00,不能只传字符串"5"。 - 数值参数:
Threshold(阈值)需改为数值类型(去掉引号),Alerts_Severity(警报级别)需改为整数类型(去掉引号)。
3. 确认指标名称准确性
Azure SQL数据库的存储使用率指标正确名称为Storage(指标值为百分比),而非storage_percent。可以通过Azure门户查看该数据库的指标列表,确认可用指标的准确名称。
4. 验证操作组ID正确性
执行以下命令检查操作组是否存在:
Get-AzActionGroup -ResourceGroupName s00149rgp -Name "Infra Sev4"
如果返回空,说明操作组ID中的订阅ID、资源组或名称有误,需修正。
5. 权限检查
确保执行命令的账号拥有Microsoft.Insights/metricAlerts/write权限,以及目标SQL数据库的读取权限,权限不足可能导致此类错误。
修正后的示例代码
$ResourceName = "s0010asq/mfwt1" $ResourceGroupNames = "<替换为实际资源组名>" $MetricName = "Storage" $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 = "PT5M" $Frequency = "PT5M" $Alerts_Severity = 4 Set-AzContext -Subscription "s00yrjfjhjjhdopdnu74" | Out-Null # 先验证资源是否存在 $targetResource = Get-AzResource -ResourceGroupName $ResourceGroupNames -Name $ResourceName if (-not $targetResource) { Write-Error "目标资源 $ResourceName 在资源组 $ResourceGroupNames 中不存在" exit 1 } $scope = $targetResource.ResourceId $alertName = "$ResourceName $MetricName High" Write-Output "`n`n创建 $alertName 警报" $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
内容的提问来源于stack exchange,提问作者pks
相关产品推荐
相关产品推荐

