使用 kusto 查询语言验证 VM 是 运行 或停止了多长时间

Verify VM is running or stopped from how long using kusto query language

我是 kusto 查询语言的新手。我需要一些帮助,如何检查 vm 关闭了多长时间以及 运行 关闭了多长时间 运行。你能帮我解决这个问题吗,因为我刚刚开始学习 kusto 查询语言。

更新 1:


原回答:

当虚拟机停止时,名为 Deallocate Virtual Machine 的事件被发送到 AzureActivity table。当 vm 启动到 运行 时,有名为 Start Virtual Machine 的事件被发送到 AzureActivity table。

所以很容易找到 vm 运行 或通过下面的查询停止(在 azure monitor -> Logs 中):

AzureActivity 
| where OperationName in ("Deallocate Virtual Machine","Start Virtual Machine")
| project TimeGenerated,OperationName
| top 1 by TimeGenerated desc

如果查询结果包含Deallocate Virtual Machine,则表示虚拟机处于停止状态。否则,它处于 运行 状态。截图如下:

接下来,既然我们知道了虚拟机的状态,比如虚拟机处于停止状态,那么我们就可以编写查询来计算它停止了多长时间。为此,我们可以使用当前时间减去虚拟机停止的时间。如下查询:

let stop_time = AzureActivity 
| where OperationName == "Deallocate Virtual Machine" 
| project TimeGenerated
| top 1 by TimeGenerated desc;
AzureActivity
| extend the_time = now() - toscalar(stop_time)
| project the_time
| top 1 by the_time

测试结果如下:

如果虚拟机现在处于 运行 状态,您还可以修改上面的查询以计算 运行 时间。