Azure Resource Graph sample queries for Azure Resource Health

This page is a collection of Azure Resource Graph sample queries for Azure Resource Health.

Note

After each query, you should see the updated results within 5 minutes in general.

Overview

This page helps you monitor and understand the health of your Azure services and resources using Kusto Query Language (KQL) through Azure Resource Graph.

It includes sample queries specifically for Azure Resource Health.

Resource Health sample queries

Count of virtual machines by state of availability and subscription ID

This query shows how many virtual machines (Microsoft.Compute/virtualMachines) are in each availability state, grouped by each of your subscriptions.

HealthResources
| where type =~ 'microsoft.resourcehealth/availabilitystatuses'
| summarize count() by subscriptionId, AvailabilityState = tostring(properties.availabilityState)
az graph query -q "HealthResources | where type =~ 'microsoft.resourcehealth/availabilitystatuses' | summarize count() by subscriptionId, AvailabilityState = tostring(properties.availabilityState)"

Lists of virtual machines and associated availability states by resource IDs

This query lists the most recent list of virtual machines (VM) (Microsoft.Compute/virtualMachines) grouped by their availability state. The query also includes each VM’s Resource ID (properties.targetResourceId) to help with debugging and troubleshooting.

Availability states can be one of four values: Available, Unavailable, Degraded, or Unknown.
For more information on what each state means, see Azure Resource Health overview.

HealthResources
| where type =~ 'microsoft.resourcehealth/availabilitystatuses'
| summarize by ResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState)
az graph query -q "HealthResources | where type =~ 'microsoft.resourcehealth/availabilitystatuses' | summarize by ResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState)"

Lists of virtual machines by availability and power states with resource IDs and resource groups

This query retrieves list of virtual machines (Microsoft.Compute/virtualMachines) and summarizes their health by aggregating both their power state and availability state.
The query also provides details on the resource group and resource ID associated with each entry for detailed visibility into your resources.

Resources
| where type =~ 'microsoft.compute/virtualmachines'
| project resourceGroup, Id = tolower(id), PowerState = tostring( properties.extended.instanceView.powerState.code)
| join kind=leftouter (
  HealthResources
  | where type =~ 'microsoft.resourcehealth/availabilitystatuses'
  | where tostring(properties.targetResourceType) =~ 'microsoft.compute/virtualmachines'
  | project targetResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState))
  on $left.Id == $right.targetResourceId
| project-away targetResourceId
| where PowerState != 'PowerState/deallocated'
az graph query -q "Resources | where type =~ 'microsoft.compute/virtualmachines' | project resourceGroup, Id = tolower(id), PowerState = tostring( properties.extended.instanceView.powerState.code) | join kind=leftouter ( HealthResources | where type =~ 'microsoft.resourcehealth/availabilitystatuses' | where tostring(properties.targetResourceType) =~ 'microsoft.compute/virtualmachines' | project targetResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState)) on \$left.Id == \$right.targetResourceId | project-away targetResourceId | where PowerState != 'PowerState/deallocated'"

Lists of virtual machines that aren't available by resource IDs

This query lists the most recent virtual machines (VM) (Microsoft.Compute/virtualMachines) that aren't in an Available state and grouped by their availability status.
It also includes the Resource ID (from properties.targetResourceId) of each VM to help with troubleshooting.

If all your virtual machines are in the Available state, the query returns no results.

HealthResources
| where type =~ 'microsoft.resourcehealth/availabilitystatuses'
| where tostring(properties.availabilityState) != 'Available'
| summarize by ResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState)
az graph query -q "HealthResources | where type =~ 'microsoft.resourcehealth/availabilitystatuses' | where tostring(properties.availabilityState) != 'Available' | summarize by ResourceId = tolower(tostring(properties.targetResourceId)), AvailabilityState = tostring(properties.availabilityState)"

Next steps