Norrin Rad
Norrin Rad

Reputation: 991

KQL Query to get number of instances in VMSS

I'm trying to create a query that gives me the number of instances running in Azure from scale sets.

I need to configure an alert for monitoring when the number of instances drops below 2 VMs. Unless anyone knows how to monitor VM Scale sets, how do you track heartbeat when you have VMs scaling up and down.

I have tried in Resource Graph Explorer and they're available under :

Resources
    | where type =~ 'Microsoft.Compute/virtualMachineScaleSets'
    | where name contains "-conto"
    | join kind=inner (ResourceContainers | where type=='microsoft.resources/subscriptions' | project SubName=name, subscriptionId) on subscriptionId
    | project VMMSName = name, RGName = resourceGroup, VMMSSKU = sku.name, VMMSCount = sku.capacity, SubName, SubID = subscriptionId

But I can't use the same query in Log analytics. When I do a query in log anaytics, which has the option to create an alert I don't get numbe rof instances, and I need to have a way to monitor the VMs.

Heartbeat
| where ResourceType contains "virtualmachinescaleset"

Any help would be greatly appreciated. Thanks in advance.

Upvotes: 0

Views: 951

Answers (1)

Delliganesh Sevanesan
Delliganesh Sevanesan

Reputation: 4778

As of now as per the MSDOC we don't have any metrics to get the no. of VM Scale set instances down/up.

We can get the VM Scale Sets Performance metrics like (CPU/Disk/Memory/etc.,)

Refer here for more information

Thanks @hey for the workaround which you have provided.

Workaround:

Install the Azure Monitor Agent in your all VM Scale Set.

Azure Monitor Agent will send the heartbeat by default. It will be added in an Azure Monitor.

Query to get no of VM Scale Set instances from heartbeats.

InsightsMetrics 
| where Namespace == "Computer" 
| where Origin =="vm.azm.ms/map" 
| where Name == "Heartbeat" 
| summarize Hb_count = count() by bin(TimeGenerated, 10m), Computer 
| extend alive=iff(Hb_count > 2, 1.0, 0.0) //computer considered "down" if it has 2 or fewer heartbeats in 10 min interval 
| where alive == true 
| project TimeGenerated, alive, Computer 
| summarize Hb_alive_count = count() by bin(TimeGenerated, 10m), alive 
| project TimeGenerated, Hb_alive_count 
| render timechart 

Upvotes: 1

Related Questions