Forum Discussion

JoseO1335's avatar
JoseO1335
Copper Contributor
Oct 24, 2024

Azure Monitor Alert for low disk space percent and MB

Hello,

 

I am trying to create a KQL query that will alert me if the diskspace percentage exceeds a percentage threshold. But I also want the alert to show how much used space and free space is left in MB.  I have the below query that shows the percentage. How can I get it to show the used space and free space is left in MB?  Thank you.

 

InsightsMetrics
| where Origin == "vm.azm.ms"
| where Namespace == "LogicalDisk" and Name == "FreeSpacePercentage"
| summarize LogicalDiskSpacePercentageFreeAverage = avg(Val) by bin(TimeGenerated, 15m), Computer, _ResourceId

  • Clive_Watson's avatar
    Clive_Watson
    Bronze Contributor

    You can get close with 

    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)

    source: kql - How to find both disk size and disk space used in azure - Stack Overflow

    Recently the ability to use ARG in a Azure Monitor was added, so you can also use that data - by adding arg('') to the start of a query in the Logs blade.  This is a simple example of that, you'd have to find the VM by name and its associated disk to make this usable...

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

  • Try follow, make sure you are fully understood before apply:

     

    InsightsMetrics
    | where Origin == "vm.azm.ms"
    | where Namespace == "LogicalDisk" and (Name == "FreeSpacePercentage" or Name == "FreeSpaceMB" or Name == "CapacityMB")
    | summarize 
        LogicalDiskSpacePercentageFreeAverage = avg(iif(Name == "FreeSpacePercentage", Val, 0)),
        FreeSpaceMB = avg(iif(Name == "FreeSpaceMB", Val, 0)),
        CapacityMB = avg(iif(Name == "CapacityMB", Val, 0))
    by bin(TimeGenerated, 15m), Computer, _ResourceId
    | extend UsedSpaceMB = CapacityMB - FreeSpaceMB
    | where LogicalDiskSpacePercentageFreeAverage < 20 // Threshold example for disk space percentage
    | project TimeGenerated, Computer, _ResourceId, LogicalDiskSpacePercentageFreeAverage, FreeSpaceMB, UsedSpaceMB

Resources