gpt4 book ai didi

Azure - 如何检查资源图资源管理器中所有应用程序的可用性百分比?

转载 作者:行者123 更新时间:2023-12-03 06:10:51 25 4
gpt4 key购买 nike

对于我所有订阅中的每个 Azure 资源,我需要获取指定时间范围内资源不可用的总时间,并据此计算给定时间范围内的可用性百分比。我不想使用 Azure Monitor 中的应用程序见解,因为它要求我单独检查每个应用程序,并且我希望立即获取所有资源。我在图形资源管理器中有以下 KQL 脚本,它返回每个可用性状态更改的时间以及它是否更改为可用或不可用的信息。

healthresourcechanges
| where id contains "/providers/Microsoft.ResourceHealth/availabilityStatuses/current"
| project id, resourceId = tostring(properties.targetResourceId), name, type, location, resourceGroup, subscriptionId, timeStamp = tostring(properties.changeAttributes.timestamp), avStatePrev = properties.changes['properties.availabilityState'].previousValue, avStateNew = properties.changes['properties.availabilityState'].newValue
| order by timeStamp desc

我想获取它给我的状态更改,按资源对它们进行分组,找到不可用的每个更改,计算从该时刻到下一个更改可用所需的时间,将该时间添加到总时间中资源已关闭,最后将每个资源的总停机时间除以我监控的总时间(例如 7 天)。如何修改脚本才能实现这一目标?可以在资源图浏览器中执行吗?如果不是,那么我可以使用哪些其他工具来一次为我的所有资源实现该结果?

我尝试使用以下行按资源对状态更改进行分组:

| summarize count=count() by resourceId, tostring(avStateNew)

但这样它只返回每个状态的状态更改数量,而不返回每个更改的时间戳,因此我无法使用它来计算它们之间的时间。

最佳答案

根据 healthresourcechanges 中可用性状态的变化计算每个资源的总停机时间和可用性百分比表,下面使用 KQL 查询。我已在 healthresourcechanges 中复制了示例数据表。

let healthresourcechanges = datatable(id:string, properties:dynamic)
[
"id1", dynamic({"targetResourceId": "/subscriptions/sub1/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/vm1", "changeAttributes": {"timestamp": "2022-01-01T00:00:00Z"}, "changes": {"properties.availabilityState": {"previousValue": "Unavailable", "newValue": "Available"}}}),
"id2", dynamic({"targetResourceId": "/subscriptions/sub1/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/vm1", "changeAttributes": {"timestamp": "2022-01-02T00:00:00Z"}, "changes": {"properties.availabilityState": {"previousValue": "Available", "newValue": "Unavailable"}}}),
"id3", dynamic({"targetResourceId": "/subscriptions/sub1/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/vm1", "changeAttributes": {"timestamp": "2022-01-03T00:00:00Z"}, "changes": {"properties.availabilityState": {"previousValue": "Unavailable", "newValue": "Available"}}}),
"id4", dynamic({"targetResourceId": "/subscriptions/sub1/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/vm1", "changeAttributes": {"timestamp": "2022-01-04T00:00:00Z"}, "changes": {"properties.availabilityState": {"previousValue": "Available", "newValue": "Unavailable"}}}),
"id5", dynamic({"targetResourceId": "/subscriptions/sub2/resourceGroups/rg2/providers/Microsoft.Storage/storageAccounts/sa1", "changeAttributes": {"timestamp": "2022-01-01T00:00:00Z"}, "changes": {"properties.availabilityState": {"previousValue": "Unavailable", "newValue": "Available"}}}),
"id6", dynamic({"targetResourceId": "/subscriptions/sub2/resourceGroups/rg2/providers/Microsoft.Storage/storageAccounts/sa1", "changeAttributes": {"timestamp": "2022-01-02T00:00:00Z"}, "changes": {"properties.availabilityState": {"previousValue": "Available", "newValue": "Unavailable"}}}),
"id7", dynamic({"targetResourceId": "/subscriptions/sub2/resourceGroups/rg2/providers/Microsoft.Storage/storageAccounts/sa1", "changeAttributes": {"timestamp": "2022-01-03T00:00:00Z"}, "changes": {"properties.availabilityState": {"previousValue": "Unavailable", "newValue": "Available"}}})
];
healthresourcechanges
| project resourceId = tostring(properties.targetResourceId), avStatePrev = properties.changes['properties.availabilityState'].previousValue, avStateNew = properties.changes['properties.availabilityState'].newValue, timestamp = todatetime(properties.changeAttributes.timestamp)
| order by resourceId, timestamp asc
| extend duration_downtime_in_days = iif(avStatePrev == "Unavailable" and avStateNew == "Available" and prev(resourceId)==resourceId, todouble((timestamp - prev(timestamp))/1d) , todouble(0))
| summarize total_downtime = sum(duration_downtime_in_days), total_time = todouble((max(timestamp)-min(timestamp))/1d) by resourceId
| extend down_time_percentage= total_downtime/total_time
| extend available_percentage=1-down_time_percentage

project运算符用于提取 resourceId , avStatePrev , avStateNew ,和timestamp来自 healthresourcechanges 的列 table 。然后结果按照resourceId排序和timestamp按升序排列。接下来,要计算每个资源的每个停机时间段的持续时间,extend用来。 duration_downtime_in_days的值设置为可用性状态从“不可用”更改为“可用”的更改时间戳之间的差值。对于其他情况,我们设置值duration_downtime_in_days为 0。则 summarize运算符用于计算每个资源的总停机时间和总时间。最后,down_time_percentage的值计算方式为总停机时间除以总时间,值为 available_percentage为 1 减去停机时间百分比。

fiddle

示例数据的输出

<表类=“s-表”><标题>资源ID总停机时间总时间down_time_percentage可用百分比 <正文>/subscriptions/sub2/resourceGroups/rg2/providers/Microsoft.Storage/storageAccounts/sa1120.50.5/subscriptions/sub1/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/vm1130.333333333333333310.66666666666666674

关于Azure - 如何检查资源图资源管理器中所有应用程序的可用性百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76826591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com