简体   繁体   English

如何使用Powershell在Azure中检索警报的当前状态

[英]How to retrieve the current state of an Alert in Azure using Powershell

I'm writing a Powershell runbook that will scale up a VM ScaleSet until an Application Insights alert is resolved. 我正在编写Powershell Runbook,它将扩展VM ScaleSet,直到解决Application Insights警报为止。

To do this, I need to query the status of the alert in my Powershell script, ie no if an alert has been triggered or resolved. 为此,我需要在Powershell脚本中查询警报的状态,即,是否已触发或解决警报,否。

I have tried to use Get-AzureRmAlertRule and Get-AzureRmAlertHistory , but this only gives me respectively the disabled/enabled state of the alert rule, or the actions that were perform on the rule itself (ie updating the rule, or deleting the alert, etc). 我曾尝试使用Get-AzureRmAlertRuleGet-AzureRmAlertHistory ,但这仅分别为我提供了警报规则的禁用/启用状态,或分别对规则本身执行的操作(即更新规则或删除警报,等等)。

Is there any way to simply know if an alert is currently being triggered or resolved? 有什么方法可以简单地知道当前是否正在触发或解决警报?

So I'm actively working through this issue too and thought I would share what I found. 因此,我也在积极地解决这个问题,并认为我会分享发现的内容。

The following was pulled from Microsoft documentation : 以下是从Microsoft文档中提取的:

The Get-AzureRmAlertHistory cmdlet gets the history of alerts as they are enabled, disabled, fired , resolved , and so on. Get-AzureRmAlertHistory cmdlet获取警报的历史记录,这些警报处于启用,禁用, 触发 ,已解决等状态。

While messing around with this command, I found that if you don't give it any parameters, it will only return history for the current day; 在弄乱该命令时,我发现如果您不给它任何参数,它将仅返回当天的历史记录。 however, when you use the -StartTime and -EndTime parameters you can obtain details of alerts from further in the past. 但是,当您使用-StartTime-EndTime参数时,您可以从过去获取警报的详细信息。

While this doesn't give you the current status of an alert in a single command, can throw together some logic that will grab the latest alert within a given time range and check the status there. 虽然这不能在一个命令中为您提供警报的当前状态,但是可以将一些逻辑组合在一起,这些逻辑将在给定的时间范围内获取最新警报并在那里检查状态。

For my purposes, this code with check the status of a sibling alert from within a runbook that was called from the alert webhook. 出于我的目的,此代码可从警报Webhook调用的运行本中检查同级警报的状态。 So I can gather the time ranges based on the data provided in the webhook. 因此,我可以根据Webhook中提供的数据收集时间范围。 I know this isn't a perfect solution for all cases, but at least it could be used as a starting point. 我知道这并不是适用于所有情况的完美解决方案,但至少可以用作起点。

Note: The version of the AzureRM.Insights module I'm working with is 3.2.1 behavior may differ depending on the version of this module you're using. 注意:我正在使用的AzureRM.Insights模块的版本为3.2.1,其行为可能会有所不同,具体取决于您所使用的该模块的版本。

Update: 更新:

As I continued to work on the code, I found that there are some issues with filtering with the -ResourceId parameter. 在继续处理代码时,我发现使用-ResourceId参数进行过滤存在一些问题。 When you provide the ResourceId for the alert that you want to find history on, it won't return any result. 当您提供要查找其历史记录的警报的ResourceId时,它将不返回任何结果。 From what I can tell, the ResourceId isn't populated when the alert objects are returned when using the Get-AzureRmAlertHistory cmdlet with just the -ResourceId parameter. 从我所知道的,在使用时将返回警报对象将不会填充RESOURCEID Get-AzureRmAlertHistory只有该cmdlet -ResourceId参数。 I did manage to find two ways to get this to work though. 不过,我确实设法找到了两种方法来使其工作。

  1. Pass the -DetailedOutput parameter in before the -ResourceId parameter. -DetailedOutput在之前参数-ResourceId参数。 It turns out that the ResourceId is populated in the DetailedOutput and can be matched there; 事实证明,ResourceId填充在DetailedOutput中,并且可以在此处进行匹配。 however, if you pass the -ResourceId in first, the cmdlet acts as though it evaluates that first prior to bringing back the detailed output. 但是,如果您首先传入-ResourceId ,则cmdlet的行为就像是先对其进行评估,然后再带回详细的输出。

    Get-AzureRmAlertHistory -StartTime 2018-01-16 -EndTime 2018-01-17 -DetailedOutput -ResourceId $AlertResourceID

  2. The property CorrelationId contains within it the ResourceId. 属性CorrelationId中包含ResourceId。 Using the Where-Object syntax, you can match on your ResourceId using Regex. 使用Where-Object语法,您可以使用Regex在ResourceId上进行匹配。

    Get-AzureRmAlertHistory -StartTime 2018-01-16 -EndTime 2018-01-17 | Where-Object {$_.CorrelationId -Match "$AlertResourceID/incidents/.*"}

Now that you have the records you want, you can use a simple Sort-Object on the -EventTimestamp property and assign the results to a variable. 现在您有了所需的记录,可以在-EventTimestamp属性上使用简单的Sort-Object并将结果分配给变量。 Then if you reference the -1 index of the variable you assigned your results to, it should give you the latest alert instance along with the alert Status. 然后,如果引用分配了结果的变量的-1索引,则它应为您提供最新的警报实例以及警报状态。

$AlertHistory = Get-AzureRmAlertHistory -StartTime 2018-01-16 -EndTime 2018-01-17 | Where-Object {$_.CorrelationId -Match "$AlertResourceID/incidents/.*"} | Sort-Object -Property EventTimestamp;

$AlertHistory[-1];

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

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