OneTwo
OneTwo

Reputation: 2483

Is there any API to query an Azure VM for free disk/memory space?

Looking at the Metrics available through the azure Metrics API disk space, nor free memory, are available as metrics.

https://learn.microsoft.com/en-us/azure/azure-monitor/platform/metrics-supported#microsoftcomputevirtualmachines

Now I know I can view these metrics through the portal using this.

https://learn.microsoft.com/en-us/azure/cost-management/azure-vm-extended-metrics#enable-extended-metrics-in-the-azure-portal

But I'm specifically looking to query this data on a regular basis to alert me when any of my VMs disk space is nearly full (or memory is nearly full).

Is there any way of doing this?

Upvotes: 1

Views: 4150

Answers (3)

Andy Shen
Andy Shen

Reputation: 992

If you collect disk space / free memory as custom metrics in Azure Monitor, you will be able to query them via the standard Azure Monitor Metrics REST API. For example, you can use Windows Azure Diagnostics (WAD) to collect these performance counters on Windows VM and send them as custom metrics.

https://learn.microsoft.com/en-us/azure/azure-monitor/platform/metrics-custom-overview

https://learn.microsoft.com/en-us/azure/azure-monitor/platform/rest-api-walkthrough

Upvotes: 1

KrishnaG
KrishnaG

Reputation: 3484

If you ultimate goal is to

  1. 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.
  2. query an Azure VM's free disk / memory space metrics using API - then follow this API reference or this and this documents.
  3. 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

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

Upvotes: 5

Related Questions