简体   繁体   English

Azure 监控告警多个订阅

[英]Azure monitor alert multiple subscriptions

I need to create multiple azure monitor alarms.我需要创建多个 azure 监视器警报。 I'm trying to do this following this website: https://www.azureblue.io/how-to-create-an-alert-rule-using-powershell-and-azure-cli/我正在尝试按照以下网站执行此操作: https://www.azureblue.io/how-to-create-an-alert-rule-using-powershell-and-azure-cli/

This solution works for me if I have action group and target resources in the same subscription but generally I don't.如果我在同一个订阅中有操作组和目标资源,那么这个解决方案对我有用,但通常我没有。 I have one actiongroup and hundreds targets in almost 100 subscriptions.我有一个行动组和近 100 个订阅中的数百个目标。

I have:我有:

#jump to subscription with actiongroup
$context = Get-AzSubscription -SubscriptionId xxx
Set-AzContext $context

$actionGroup = Get-AzActionGroup -name "xxx" -ResourceGroupName "xxx"
$actionGroupId = New-AzActionGroup -ActionGroupId $actionGroup.Id

#jump to subscription with target without this I can't ask for target.id
$context = Get-AzSubscription -SubscriptionId xx
Set-AzContext $context

# Creates a local criteria object that can be used to create a new metric alert
$condition = New-AzMetricAlertRuleV2Criteria `
    -MetricName "Data space used percent (Platform)" `
    -TimeAggregation Maximum `
    -Operator GreaterThan `
    -Threshold 0.8

$windowSize = New-TimeSpan -Minutes 30
$frequency = New-TimeSpan -Minutes 5 
$targetResourceId = (Get-AzResource -Name xxx).Id

#I was thinking that this jump to subscription will solve my issue but doesn't
$context = Get-AzSubscription -SubscriptionId cf653672-c304-49a2-b01b-171f6236bad6
Set-AzContext $context

# Adds or updates a V2 (non-classic) metric-based alert rule.
Add-AzMetricAlertRuleV2 `
    -Name "test" `
    -ResourceGroupName "xxx" `
    -WindowSize $windowSize `
    -Frequency $frequency `
    -TargetResourceId $targetResourceId `
    -Condition $condition `
    -ActionGroup $actionGroupId `
    -Severity 3

I got this error:我收到此错误:

Add-AzMetricAlertRuleV2 : Exception type: ErrorResponseException, Message: Null/Empty, Code: Null, Status code:NotFound, Reason phrase: Not Found
At line:26 char:1
+ Add-AzMetricAlertRuleV2 `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Add-AzMetricAlertRuleV2], PSInvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Alerts.AddAzureRmMetricAlertRuleV2Command

From GUI there is no a problem to chose actiongroup from different subscription but you can't chose targets from multiple subscription.从 GUI 中,从不同的订阅中选择操作组没有问题,但您不能从多个订阅中选择目标。

If I will find solution for this one example I will try to read all my resources and run the code in a loop.如果我能找到这个例子的解决方案,我会尝试读取所有资源并循环运行代码。

I had an error like that recently.. It was due to an invalid input.我最近遇到了这样的错误。这是由于输入无效造成的。 In my particular case it was due to invalid characters in the alertName.在我的特殊情况下,这是由于 alertName 中的无效字符造成的。

As for the main issue of multiple subscriptions...至于多订阅的主要问题……

Here are a couple of ideas:这里有几个想法:

  1. To apply an single metric alert to multiple resources of the same type as the same time.将单个指标警报同时应用于同一类型的多个资源。 You can specify scope, region etc with Add-AzMetricAlertRuleV2 see: https://learn.microsoft.com/en-us/powershell/module/az.monitor/add-azmetricalertrulev2?view=azps-7.2.0 .您可以使用 Add-AzMetricAlertRuleV2 指定 scope、区域等,请参阅: https://learn.microsoft.com/en-us/powershell/module/az.monitor/add-azmetricalertrulev2?view=azps-7.2.0 This is limited to only a few resources https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-metric-overview#monitoring-at-scale-using-metric-alerts-in-azure-monitor .这仅限于少数资源https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-metric-overview#monitoring-at-scale-using-metric-alerts-in-天蓝色监视器

Note that TargetResourceScope is a array of strings.请注意,TargetResourceScope 是一个字符串数组。 So it seems possible in theory to apply the alert to multiple subscriptions with this.因此,理论上似乎可以将警报应用于多个订阅。

  1. When other option are not possible: use Get-AzSubscription, and deploy the code to each subscription or to get the list of subscriptions you need to run the code with.当其他选项不可行时:使用 Get-AzSubscription,并将代码部署到每个订阅或获取运行代码所需的订阅列表。

Your single action group.您的单一行动小组。

I can validate that, you can have a single action group for any alerts inside a subscription.我可以验证,您可以为订阅内的任何警报设置一个单独的操作组。 I don't see any reason why the the action group could not be in a separate subscription provided the actiongroup.id is correct and there are no RBAC/firewall issues.如果 actiongroup.id 正确并且没有 RBAC/防火墙问题,我看不出为什么操作组不能在单独的订阅中。 But I don't have a way to test this.但是我没有办法测试这个。

Lastly, here is a relevant conversation in an MS forum.最后,这是 MS 论坛中的相关对话。 https://techcommunity.microsoft.com/t5/azure-monitor/azure-monitor-multiple-subscriptions/mp/1348362 https://techcommunity.microsoft.com/t5/azure-monitor/azure-monitor-multiple-subscriptions/mp/1348362

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM