S. Space
S. Space

Reputation: 315

Best way to retrieve CPU utilization for all VMs in a Azure subscription

Trying to get the CPU utilization (and preferably network as well. I would love to get RAM too, but as i understand that requires having the guest module installed to get those metrics. so at this point i just need the metrics at the 'host' level).

Idea is to run this against all VMs in a subscription, to get VM name, VM resource group, CPU utilization over the last x days, network in over the last x days, and network out over the last x days.

The first thing I tried though, using the "Get-AzureRMMetric", starts giving errors.

I type "get-azurermmetric", and am prompted for a resource ID. I enter the resource ID of the VM, and the response i get is a long string of warnings, and exception types, that an invalid status code 'notfound' was returned.

Any ideas?

Upvotes: 1

Views: 5534

Answers (2)

Space

Is that we can able to fetch maximum usage on CPU with date using same script, i have used to did below changes like call Maximum ...no luck..i hope some condition need to do you have solution for that

foreach($c in $cpu.Data.Maximum)
 {
  #this is a average value for 12 hours, so total = $c*12 (or should be $c*12*60*60)
  $cpu_total += $c*12
  #($c|measure -maximum).maximum
 }

Upvotes: 0

Ivan Glasenberg
Ivan Glasenberg

Reputation: 30015

First, you need to determine which metrics are supported for vm, use the following code:

(Get-AzureRmMetricDefinition -ResourceId "vm resource id").name

Then you can see the supported metrics(Just ignore the warning message):

enter image description here

As per your question, I think you need "Percentage CPU" / "Network In" / "Network Out".

Then you can use the sample code below for your purpose(you can make some changes if it does not meet your need):

#get all vms in a resource group, but you can remove -ResourceGroupName "xxx" to get all the vms in a subscription
$vms = Get-AzureRmVM -ResourceGroupName "xxx"

 #get the last 3 days data
 #end date
 $et=Get-Date

 #start date
 $st=$et.AddDays(-3)

 #define an array to store the infomation like vm name / resource group / cpu usage / network in / networkout
 $arr =@()

 foreach($vm in $vms)
 {
 #define a string to store related infomation like vm name etc. then add the string to an array
 $s = ""


 #percentage cpu usage
$cpu = Get-AzureRmMetric -ResourceId $vm.Id -MetricName "Percentage CPU" -DetailedOutput -StartTime $st `
 -EndTime $et -TimeGrain 12:00:00  -WarningAction SilentlyContinue


 #network in
 $in = Get-AzureRmMetric -ResourceId $vm.Id -MetricName "Network In" -DetailedOutput -StartTime $st `
 -EndTime $et  -TimeGrain 12:00:00 -WarningAction SilentlyContinue


 #network out 
$out = Get-AzureRmMetric -ResourceId $vm.Id -MetricName "Network Out" -DetailedOutput -StartTime $st `
 -EndTime $et -TimeGrain 12:00:00  -WarningAction SilentlyContinue


 # 3 days == 72hours == 12*6hours

 $cpu_total=0.0
 $networkIn_total = 0.0
 $networkOut_total = 0.0

 foreach($c in $cpu.Data.Average)
 {
  #this is a average value for 12 hours, so total = $c*12 (or should be $c*12*60*60)
  $cpu_total += $c*12
 }

 foreach($i in $in.Data.total)
 {
 $networkIn_total += $i 
 }

 foreach($t in $out.Data.total)
 {
 $networkOut_total += $t
 }

 # add all the related info to the string
 $s = "VM Name: " + $vm.name + "; Resource Group: " + $vm.ResourceGroupName + "; CPU: " +$cpu_total +"; Network In: " + $networkIn_total + "; Network Out: " + $networkOut_total

 # add the above string to an array
 $arr += $s
 }

 #check the values in the array
 $arr

The test result:

enter image description here

Upvotes: 2

Related Questions