If you ultimate goal is to
- setup alerts and get notified when a threshold is met - then you can just accomplish by creating a log alert rule. For more information w.r.t it, please refer this document.
- query an Azure VM's free disk / memory space metrics using API - then follow this API reference or this and this documents.
- query an Azure VM's free disk / memory space metrics using PowerShell - then follow this cmdlet.
For all the above mentioned ways to work, you would have to first
- create a Log Analytics workspace and install Log Analytics agent in your Azure VM (or in other words, enable the Log Analytics VM extension). To accomplish this using Azure portal, follow this document. To accomplish the same using Azure PowerShell / CLI, follow this or this document based on OS of your Azure VM.
- once your Azure Log Analytics workspace starts collecting log data then go to Azure Portal -> Log Analytics workspaces -> Your Log Analytics workspace -> Logs and then type your kusto query to find free disk / memory space details of your VM. The queries will be something like shown below.
If your Azure VM is of Windows OS then query to find disk Total free space is:
Perf
| where ( ObjectName == "LogicalDisk" )
| where ( CounterName == "% Free Space" )
| where ( InstanceName == "_Total" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
If your Azure VM is of Windows OS then query to find disk C drive free space is:
Perf
| where ( ObjectName == "LogicalDisk" )
| where ( CounterName == "% Free Space" )
| where ( InstanceName == "C:" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
If your Azure VM is of Linux OS then query to find disk Total free space is:
Perf
| where ( ObjectName == "Logical Disk" )
| where ( CounterName == "% Free Space" )
| where ( InstanceName == "_Total" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
If your Azure VM is of Linux OS then query to find disk mounted on Root free space is:
Perf
| where ( ObjectName == "Logical Disk" )
| where ( CounterName == "% Free Space" )
| where ( InstanceName == "/" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
If your Azure VM is of Linux OS then query to find Available MBytes memory is:
Perf
| where ( ObjectName == "Memory" )
| where ( CounterName == "Available MBytes Memory" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
If your Azure VM is of Windows OS then query to Available MBytes is:
Perf
| where ( ObjectName == "Memory" )
| where ( CounterName == "Available MBytes" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
If your Azure VM is of Windows OS then query to find Committed Bytes In Use is:
Perf
| where ( ObjectName == "Memory" )
| where ( CounterName == "% Committed Bytes In Use" )
| summarize AggregatedValue= avg(CounterValue) by Computer, bin(TimeGenerated, 30s)
Note1: For all the above mentioned queries to work, make sure the respective performance counters are added in Azure Portal -> Log Analytics workspaces -> Your Log Analytics workspace -> Advanced settings -> Data -> Windows Performance Counters / Linux Performance Counters.
Note2: Using other performance counters, you can also fetch much more data like Disk read time, Disk write time, Idle time, current disk queue length, cache bytes, commited bytes, page faults, page reads, page writes, free inodes, etc.
Hope this helps! Cheers!