Can I get Azure DNS Metrix via the API?

Anthony Robinson 0 Reputation points
2024-05-30T01:01:54.7533333+00:00

Hi, is it possible to use Azure DNS Management Client API to get the monthly amount of queries for a given zone?

Thanks

Azure DNS
Azure DNS
An Azure service that enables hosting Domain Name System (DNS) domains in Azure.
624 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 16,420 Reputation points MVP
    2024-05-30T01:51:02.27+00:00

    AFAIK, Azure DNS Management Client API itself does not provide a direct method to retrieve the monthly amount of DNS queries for a given zone. The API primarily focuses on managing DNS zones and records, such as creating, updating, and deleting DNS records, rather than providing usage statistics.

    However, you can achieve this by utilizing Azure Monitor and Azure Metrics, which can track and provide metrics for DNS query usage. Here's a general approach to get the monthly amount of DNS queries for a given zone using Azure Monitor:

    1. Enable Azure DNS Analytics: Ensure that Azure DNS Analytics is enabled in your Azure subscription. This allows Azure Monitor to collect DNS query metrics.
    2. Use Azure Monitor Metrics: Use the Azure Monitor Metrics API to retrieve DNS query metrics. This API can provide various metrics, including the number of DNS queries.
    # Define variables
    $subscriptionId = "your-subscription-id"
    $resourceGroupName = "your-resource-group-name"
    $dnsZoneName = "your-dns-zone-name"
    $metricName = "TotalQueries"
    # Set the time range (e.g., last 30 days)
    $endTime = (Get-Date).ToString("o")
    $startTime = (Get-Date).AddDays(-30).ToString("o")
    # Get the access token (make sure you have Azure CLI installed and you are logged in)
    $accessToken = (az account get-access-token --query 'accessToken' -o tsv)
    # Define the REST API endpoint
    $uri = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Network/dnsZones/$dnsZoneName/providers/Microsoft.Insights/metrics?api-version=2018-01-01&metricnames=$metricName&timespan=$startTime/$endTime"
    # Send the HTTP request
    $response = Invoke-RestMethod -Method Get -Uri $uri -Headers @{
        Authorization = "Bearer $accessToken"
    }
    # Process the response
    $metrics = $response.value
    # Output the total queries
    $totalQueries = $metrics.timeseries.data | Measure-Object -Property total -Sum
    "Total DNS queries in the last 30 days: $($totalQueries.Sum)"
    
    

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    0 comments No comments