Khushboo Chandak
Khushboo Chandak

Reputation: 11

I need to fetch data like How many CPUs and RAM is being utilized in Virtual machines using resource graph explorer

resources| where type == 'microsoft.compute/virtualmachines'
| extend Size = tostring(properties.hardwareProfile.vmSize)
| extend CPUs = toint(extract(".+\[A-Z\](\[0-9\]+).+", 1, tostring(Size)))
| extend RamSize = tostring(properties.storageProfile.osDisk.diskSizeGB)
| extend RamSizeGB = toint(extract("(\[0-9\]+)", 1, RamSize))
| extend VMName = name
| where CPUs > 1
| project VMName, Size, CPUs, RamSizeGB

I am using his query, but this is not correct way as it regex from size type and RAM is also not coming, it is fetching disk size instead of RAM.

Upvotes: 0

Views: 162

Answers (1)

Venkat V
Venkat V

Reputation: 7715

I need to fetch data like How many CPUs and RAM is being utilized in Virtual machines using resource graph explorer.

AFAIK, since there is no way to monitor live metrics using Resource Graph Explorer, you can obtain the same information via DCR

Here are the detailed steps to fetch the same information using LAW.

  1. Create a Log analytics workspace

  2. Once you create the Log Analytics Workspace, you can then create a Data Collection Rule with your virtual machine resources.

enter image description here

  1. Configure the data source and destination as shown in the image below

enter image description here

  1. Once all configurations are complete, wait for some time (about 10 minutes) for the current metrics to sync to LAW.

  2. Run the query below in the Log Analytics Workspace by navigating to LAW > Logs.

Perf
| where ObjectName == "Processor Information" and CounterName == "% Processor Time" and InstanceName == "_Total"
| summarize 
    AVG_CPU_Used = avg(CounterValue), 
    MAX_CPU_Used = max(CounterValue) 
    by Computer, _ResourceId
| join kind=inner (
    Perf  
    | where ObjectName == "Memory" 
    | where CounterName == "Available Bytes" or CounterName == "% Committed Bytes In Use" or CounterName == "Committed Bytes"
    | summarize 
        AvailableRAM_In_GB = avgif(CounterValue, CounterName == "Available Bytes") / (1024 * 1024 * 1024),  // Convert Available Bytes to GB
        RAM_In_Use_Percent = avgif(CounterValue, CounterName == "% Committed Bytes In Use") 
    by Computer
    | extend TotalRAM_In_GB = AvailableRAM_In_GB / (1 - (RAM_In_Use_Percent / 100)) // Calculate Total RAM based on Available RAM and RAM usage percentage
) on Computer
| project Computer, AVG_CPU_Used, MAX_CPU_Used, AvailableRAM_In_GB, RAM_In_Use_Percent, TotalRAM_In_GB, _ResourceId
| order by Computer asc

Output:

enter image description here

Reference: Create and edit data collection rules (DCRs) and associations in Azure Monitor

Create a Log Analytics workspace

Upvotes: 0

Related Questions