1

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?

OneTwo
  • 2,291
  • 6
  • 33
  • 55
  • 1
    Instead of metrics, I think you want logs. Take a look at https://learn.microsoft.com/en-us/azure/azure-monitor/insights/vminsights-performance – Svend Oct 22 '19 at 09:42
  • Thanks, do you know if there is a way to query these logs using an api? – OneTwo Oct 22 '19 at 09:48
  • 1
    I've not messed much with Monitor (which has a REST API of course). But it's something about a Log Analytics workspace, which monitor works against (I think). You can also try the "Diagnostics settings" in the AzureVM blade which exports something to plain storage accounts as JSON blobs. The portals a bit faulty right now, but I believe you can find performance counters in there as well. – Svend Oct 22 '19 at 09:54
  • It's all very confusing to me. I have enabled "Diagnostics settings", but the logs are saved to a storage account in a table, not exactly easy to query with an API. – OneTwo Oct 22 '19 at 10:17
  • You would think something like this would be much easier. – OneTwo Oct 22 '19 at 10:17
  • 1
    According to your question and discussion in the comment, only related to Azure. It should not related to Azure DevOps which Collaborate on software development through source control, work tracking, and continuous integration and delivery, both on-premises and in the cloud. By any chance, could you remove that unrelated `azure-devops` tag? – PatrickLu-MSFT Oct 22 '19 at 10:32
  • Will do, sorry wasn't aware. – OneTwo Oct 22 '19 at 11:08
  • Aside from the question, I would suggest not to rely on the Azure's built-in monitoring features. Just install any monitoring agent on your VMs and use the your own monitoring system. – ei-grad Oct 24 '19 at 20:55
  • is your VM running on Linux or Windows? – Rav Oct 31 '19 at 06:16

3 Answers3

5

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!

KrishnaG
  • 3,340
  • 2
  • 6
  • 16
1

You can use powershell and execute scripts remotely:

https://learn.microsoft.com/en-us/azure/virtual-machines/windows/run-command

and/or

https://techcommunity.microsoft.com/t5/ITOps-Talk-Blog/PowerShell-Basics-Connecting-to-VMs-with-Azure-PSRemoting/ba-p/428403

To get the metrics you want:

How to get free physical memory of remote computer using PowerShell

How to get disk capacity and free space of remote computer

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • Thanks for this, although I'm specifically looking for a way to query it from an API, ultimately to setup alerts. – OneTwo Oct 22 '19 at 16:37
1

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

Andy Shen
  • 962
  • 6
  • 7