Search code examples
azurekqlazure-resource-graph

How to find both disk size and disk space used in azure


Is there a way to get both disk sizes and disk space used in azure?

I can use a query like this and get the percentage or mb used

InsightsMetrics
| where Origin == "vm.azm.ms"
 and Namespace == "LogicalDisk" and Name == "FreeSpacePercentage"
| extend Disk=tostring(todynamic(Tags)["vm.azm.ms/mountId"])
| summarize Disk_Free_Space = avg(Val) by Computer, Disk, _ResourceId
| project Computer, Disk, Disk_Free_Space

And see disk sizes like this

resources
| where type == "microsoft.compute/disks"
| project properties['diskSizeGB']

But is there a way to see both size of disk and how much space is left?


Solution

  • This query would give you an approximation disk size, though it will never be 100% accurate, but very close

    InsightsMetrics
    | where Origin == "vm.azm.ms"
     and Namespace == "LogicalDisk" 
     | where Name in ("FreeSpaceMB", "FreeSpacePercentage")
    | extend Disk=tostring(todynamic(Tags)["vm.azm.ms/mountId"])
    | summarize arg_max(TimeGenerated, Val) by Computer, Name, Disk, _ResourceId
    | extend Packed = bag_pack(Name, Val) 
    | project TimeGenerated, Computer, Disk, _ResourceId, Packed
    | summarize Packed = make_bag(Packed) by TimeGenerated, Computer, Disk, _ResourceId
    | evaluate bag_unpack(Packed) : (TimeGenerated:datetime , Computer:string, Disk:string, _ResourceId:string, FreeSpaceMB:long, FreeSpacePercentage:decimal)
    | extend DiskSizeGB = ceiling((todecimal(FreeSpaceMB) / (FreeSpacePercentage / todecimal(100))) /1024)
    

    Another way you could calculate it is by taking your two original queries and using Azure Workbooks to query Log Analytics and Azure Resource Graph and then merge the results https://learn.microsoft.com/en-us/azure/azure-monitor/visualize/workbooks-data-sources#merge