在 KQL 查询中将磁盘大小从兆字节转换为千兆字节
Convert disk size from megabytes to gigabytes in KQL query
我有以下查询可以帮助我从虚拟机磁盘获取数据
InsightsMetrics
| where Namespace == "LogicalDisk"
| extend Tags = todynamic(Tags)
| extend Drive=tostring(todynamic(Tags)["vm.azm.ms/mountId"])
| extend DiskSize=tostring(todynamic(Tags)["vm.azm.ms/diskSizeMB"])
| summarize
Free_space_percentage = avgif(Val, Name == 'FreeSpacePercentage'),
Free_Gigabytes = avgif(Val, Name == 'FreeSpaceMB') /1024
by Computer, Drive
| join (
InsightsMetrics
| where Namespace == "LogicalDisk"
| extend Tags = todynamic(Tags)
| extend DiskSize=tostring(todynamic(Tags)["vm.azm.ms/diskSizeMB"])
| extend Drive=tostring(todynamic(Tags)["vm.azm.ms/mountId"])
) on Computer, Drive
| where DiskSize has "."
| summarize by Computer,Drive , Free_space_percentage, Free_Gigabytes, DiskSize
现在的问题是 DiskSize 以兆字节显示,而其他所有内容均以千兆字节为单位。我现在已经尝试了几个小时,尝试将它转换为 gigas,但没有成功。有人可以帮助我在查询中在哪里以及如何进行转换吗?
您的问题似乎不在于将 MB 转换为 GB,而是构造一个查询,该查询将为您提供平均值和磁盘大小。
假设磁盘的大小在查询期间没有改变,take_any() 就可以了。
InsightsMetrics
// | where TimeGenerated between(datetime(2022-04-01) .. datetime(2022-04-01 00:00:10))
| where Namespace == "LogicalDisk"
| extend Tags = todynamic(Tags)
| extend Drive = tostring(Tags["vm.azm.ms/mountId"])
| extend diskSizeGB = Tags["vm.azm.ms/diskSizeMB"]/1024.0
| summarize
avg_FreeSpacePercentage = avgif(Val, Name == 'FreeSpacePercentage')
,avg_FreeSpaceGB = avgif(Val, Name == 'FreeSpaceMB') /1024
,take_any(diskSizeGB)
by Computer, Drive
Computer
Drive
avg_FreeSpacePercentage
avg_FreeSpaceGB
diskSizeGB
DC00.na.contosohotels.com
C:
74.9538803100586
94.8232421875
126.50976181030273
DC00.na.contosohotels.com
D:
91.4168853759766
14.6240234375
15.998043060302734
SQL12.na.contosohotels.com
C:
57.7019577026367
72.998046875
126.50976181030273
SQL12.na.contosohotels.com
D:
92.02197265625
29.4443359375
31.998043060302734
SQL12.na.contosohotels.com
F:
99.9144668579102
127.7626953125
127.87304306030273
AppBE01.na.contosohotels.com
C:
73.2973098754883
92.7275390625
126.50976181030273
AppBE01.na.contosohotels.com
D:
91.3375244140625
14.611328125
15.998043060302734
我有以下查询可以帮助我从虚拟机磁盘获取数据
InsightsMetrics
| where Namespace == "LogicalDisk"
| extend Tags = todynamic(Tags)
| extend Drive=tostring(todynamic(Tags)["vm.azm.ms/mountId"])
| extend DiskSize=tostring(todynamic(Tags)["vm.azm.ms/diskSizeMB"])
| summarize
Free_space_percentage = avgif(Val, Name == 'FreeSpacePercentage'),
Free_Gigabytes = avgif(Val, Name == 'FreeSpaceMB') /1024
by Computer, Drive
| join (
InsightsMetrics
| where Namespace == "LogicalDisk"
| extend Tags = todynamic(Tags)
| extend DiskSize=tostring(todynamic(Tags)["vm.azm.ms/diskSizeMB"])
| extend Drive=tostring(todynamic(Tags)["vm.azm.ms/mountId"])
) on Computer, Drive
| where DiskSize has "."
| summarize by Computer,Drive , Free_space_percentage, Free_Gigabytes, DiskSize
现在的问题是 DiskSize 以兆字节显示,而其他所有内容均以千兆字节为单位。我现在已经尝试了几个小时,尝试将它转换为 gigas,但没有成功。有人可以帮助我在查询中在哪里以及如何进行转换吗?
您的问题似乎不在于将 MB 转换为 GB,而是构造一个查询,该查询将为您提供平均值和磁盘大小。
假设磁盘的大小在查询期间没有改变,take_any() 就可以了。
InsightsMetrics
// | where TimeGenerated between(datetime(2022-04-01) .. datetime(2022-04-01 00:00:10))
| where Namespace == "LogicalDisk"
| extend Tags = todynamic(Tags)
| extend Drive = tostring(Tags["vm.azm.ms/mountId"])
| extend diskSizeGB = Tags["vm.azm.ms/diskSizeMB"]/1024.0
| summarize
avg_FreeSpacePercentage = avgif(Val, Name == 'FreeSpacePercentage')
,avg_FreeSpaceGB = avgif(Val, Name == 'FreeSpaceMB') /1024
,take_any(diskSizeGB)
by Computer, Drive
Computer | Drive | avg_FreeSpacePercentage | avg_FreeSpaceGB | diskSizeGB |
---|---|---|---|---|
DC00.na.contosohotels.com | C: | 74.9538803100586 | 94.8232421875 | 126.50976181030273 |
DC00.na.contosohotels.com | D: | 91.4168853759766 | 14.6240234375 | 15.998043060302734 |
SQL12.na.contosohotels.com | C: | 57.7019577026367 | 72.998046875 | 126.50976181030273 |
SQL12.na.contosohotels.com | D: | 92.02197265625 | 29.4443359375 | 31.998043060302734 |
SQL12.na.contosohotels.com | F: | 99.9144668579102 | 127.7626953125 | 127.87304306030273 |
AppBE01.na.contosohotels.com | C: | 73.2973098754883 | 92.7275390625 | 126.50976181030273 |
AppBE01.na.contosohotels.com | D: | 91.3375244140625 | 14.611328125 | 15.998043060302734 |