skip to Main Content

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?

2

Answers


  1. 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

    Login or Signup to reply.
  2. But is there a way to see both size of disk and how much space is left?

    You can use below Kusto Query Language that retrieves both the disk sizes and used disk space also free space for each disk.

    KQL Query:

    Perf | where ObjectName == "LogicalDisk" and CounterName == "Free Megabytes" 
    | summarize TotalDiskSpace_GB = (max(CounterValue) + sum(CounterValue)/1024), UsedDiskSpace_GB = (sum(CounterValue)/1024),Freespace_GB = ((max(CounterValue) + sum(CounterValue))-sum(CounterValue)/1024) by Computer, InstanceName
    

    Output:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search