Search code examples
azureazure-virtual-networkazure-vm

How to monitor IP allocation in Azure subnet?


I need to find the current usage of IP addresses within subnets in Azure. I found free IPs in Virtual Networks->subnets but the number is the total available IP addresses within the subnet. How can I see how many of the available IPs within the subnet are actually in use (how many of them are free)? Is there a way to set the monitoring for free IP addresses (or used IP addresses) for Azure subnets? Thanks in advance.


Solution

  • You can call the listUsage API /subscriptions/****/resourceGroups/aoprod9574-stamp-canadacentral-rg/providers/Microsoft.Network/virtualNetworks/vnet-name/usages?api-version=2021-02-01 to get the current allocations per Subnet in a VNet

    {
      "value": [
        {
          "currentValue": 3,
          "id": "/subscriptions/****/resourceGroups/my-rg/providers/Microsoft.Network/virtualNetworks/aoprod9574-canadacen-vnet/subnets/cosmosdb-pe-snet",
          "limit": 27,
          "name": {
            "localizedValue": "Subnet size and usage",
            "value": "SubnetSpace"
          },
          "resourceGroup": "my-rg",
          "unit": "Count"
        },
        {
          "currentValue": 93,
          "id": "/subscriptions/****/resourceGroups/my-rg/providers/Microsoft.Network/virtualNetworks/aoprod9574-canadacen-vnet/subnets/kubernetes-snet",
          "limit": 1019,
          "name": {
            "localizedValue": "Subnet size and usage",
            "value": "SubnetSpace"
          },
          "resourceGroup": "my-rg",
          "unit": "Count"
        }
      ]
    }