使用Azure CLI列出活动的Azure监视器警报

5lhxktic  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(108)

我正在尝试找出是否有方法使用Azure CLI列出活动(已触发)警报?我发现了此命令:az monitor metrics alert list将列出所有警报。然而,似乎没有任何信息告诉我警报的当前状态。是否存在?
最接近的是Azure Resource Graph Explorer查询,它看起来像这样,其中列出了活动警报:

alertsmanagementresources
| where type == 'microsoft.alertsmanagement/alerts'
| extend severity = tostring(properties["essentials"]["severity"])
| where properties["essentials"]["monitorCondition"] in~ ('Fired')
| where properties["essentials"]["startDateTime"] >= datetime(Tue, 07 Mar 2023 05:49:47 GMT) and properties["essentials"]["startDateTime"] <= datetime(Thu, 06 Apr 2023 05:49:47 GMT)
| project id,severity,name,essentials = properties["essentials"],subscriptionId
| order by todatetime(essentials["startDateTime"]) desc

我可以从Azure CLI运行它吗?

goqiplq2

goqiplq21#

我可以从Azure CLI运行它吗?
是的,你可以在Azure cli中这样做。我已经在我的环境中复制了,并得到了如下预期结果:
你可以使用下面的cli命令,我已经遵循了Microsoft-Document:
我修改了你的代码:

az graph query -q "
alertsmanagementresources
| where type == 'microsoft.alertsmanagement/alerts'
| extend severity = tostring(properties['essentials']['severity'])
| where properties['essentials']['monitorCondition'] in~ ('Fired')
| where properties['essentials']['startDateTime'] >= datetime(Tue, 07 Mar 2023 05:49:47 GMT) and properties['essentials']['startDateTime'] <= datetime(Thu, 06 Apr 2023 05:49:47 GMT)
| project id,severity,name,essentials = properties['essentials'],subscriptionId
| order by todatetime(essentials['startDateTime']) desc "

输出:

Portal中的输出:

在资源图资源管理器中:

如果你想要表格形式的数据,在CLI中使用下面的代码:

$x=az graph query -q "
    alertsmanagementresources
    | where type == 'microsoft.alertsmanagement/alerts'
    | extend severity = tostring(properties['essentials']['severity'])
    | where properties['essentials']['monitorCondition'] in~ ('Fired')
    | where properties['essentials']['startDateTime'] >= datetime(Tue, 07 Mar 2023 05:49:47 GMT) and properties['essentials']['startDateTime'] <= datetime(Thu, 06 Apr 2023 05:49:47 GMT)
    | project id,severity,name,essentials = properties['essentials'],subscriptionId
    | order by todatetime(essentials['startDateTime']) desc" |ConvertFrom-Josn
$x.data

输出:

相关问题