Azure – Collecting performance metrics for Azure virtual machines

Azure Monitor provides several ways to interact with metrics, including charting them in the portal, accessing them through the REST API, or querying them using PowerShell or CLI.

In this blog, we shall learn how to fetch the metrics for our Azure Virtual Machines using PowerShell. The script that I provide can be used as a utility to generate quick reports.

Below is the script:

<#
AUTHOR:
Manjunath Rao
DATE:
February 21, 2018
DESCRIPTION:
The script will generate performance metrics (as recorded by Azure agent) from Azure virtual machines and then populate into an excel sheet.
REFERENCE:
https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitoring-supported-metrics
#>
$ErrorActionPreference = “SilentlyContinue”
# Login to Azure Account
try
{
Login-AzureRmAccount -ErrorAction Stop
}
catch
{
# The exception lands in [Microsoft.Azure.Commands.Common.Authentication.AadAuthenticationCanceledException]
Write-Host “User Cancelled The Authentication” -ForegroundColor Yellow
exit
}
# Prompting the user to select the subscription
Get-AzureRmSubscription | Out-GridView -OutputMode Single -Title “Please select a subscription” | ForEach-Object {$selectedSubscriptionID = $PSItem.SubscriptionId}
Write-Host “You have selected the subscription: $selectedSubscriptionID. Proceeding with fetching the inventory. `n” -ForegroundColor green
# Setting the selected subscription
Select-AzureRmSubscription -SubscriptionId $selectedSubscriptionID
# Get the list of resource groups
$resourcegroup_list = (get-azurermresourcegroup).resourcegroupname
try{
# Create an Excel COM Object
$excel = New-Object -ComObject excel.application
}catch{
Write-Host “Something went wrong in creating excel. Make sure you have MSOffice installed to access MSExcel. Please try running the script again. `n” -ForegroundColor Yellow
}
# Create a Workbook
$workbook = $excel.Workbooks.Add()
# Creating a directory overrides if any directory exists with the same name
Write-Host “Creating a directory: C:\AzurePerformanceMetrics. This operation will override if you have a directory with the same name. `n” -ForegroundColor Yellow
New-Item C:\AzurePerformanceMetrics -Type Directory -Force
Write-Host “Creating the Performance Metrics worksheet…” -ForegroundColor Green
# Adding worksheet
$workbook.Worksheets.Add()
# Creating the “Virtual Machine” worksheet and naming it
$VirtualMachineWorksheet = $workbook.Worksheets.Item(1)
$VirtualMachineWorksheet.Name = ‘Virtual Machine perf metrics’
# Headers for the worksheet
$VirtualMachineWorksheet.Cells.Item(1,1) = ‘Resource Group Name’
$VirtualMachineWorksheet.Cells.Item(1,2) = ‘VM Name’
$VirtualMachineWorksheet.Cells.Item(1,3) = ‘Location’
$VirtualMachineWorksheet.Cells.Item(1,4) = ‘Percentage CPU’
$VirtualMachineWorksheet.Cells.Item(1,5) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,6) = ‘Network IN’
$VirtualMachineWorksheet.Cells.Item(1,7) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,8) = ‘Network Out’
$VirtualMachineWorksheet.Cells.Item(1,9) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,10) = ‘Disk Read Bytes’
$VirtualMachineWorksheet.Cells.Item(1,11) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,12) = ‘Disk Write Bytes’
$VirtualMachineWorksheet.Cells.Item(1,13) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,14) = ‘Disk Read Operations/Sec’
$VirtualMachineWorksheet.Cells.Item(1,15) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,16) = ‘Disk Write Operations/Sec’
$VirtualMachineWorksheet.Cells.Item(1,17) = ‘Units’
# Cell Counter
$row_counter = 3
$column_counter = 1
foreach($resourcegroup_list_iterator in $resourcegroup_list){
#write-output “RG: ” $resourcegroup_list_iterator
$vm_list = get-azurermvm -ResourceGroupName $resourcegroup_list_iterator
foreach($vm_list_iterator in $vm_list){
write-host “Fetching performance metrics for the virtual machine: ” $vm_list_iterator.Name -ForegroundColor cyan
$percentage_cpu_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Percentage CPU” # Percentage
$network_in_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Network IN” # Bytes
$network_out_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Network Out” # Bytes
$disk_read_bytes_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Disk Read Bytes” # Bytes Per Second
$disk_write_bytes_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Disk Write Bytes” # Bytes Per Second
$disk_read_operations_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Disk Read Operations/Sec” # Count Per Second
$disk_write_operations_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Disk Write Operations/Sec” # Count Per Second
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $vm_list_iterator.ResourceGroupName.ToString()
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $vm_list_iterator.Name.ToString()
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $vm_list_iterator.Location.ToString()
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $percentage_cpu_data.Data[-2].Average
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Percentage”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $network_in_data.Data[-2].Total
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Bytes”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $network_out_data.Data[-2].Total
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Bytes”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $disk_read_bytes_data.Data[-2].Average
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Bytes Per Second”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $disk_write_bytes_data.Data[-2].Average
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Bytes Per Second”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $disk_read_operations_data.Data[-2].Average
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Count Per Second”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $disk_write_operations_data.Data[-2].Average
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Count Per Second”
$row_counter = $row_counter + 1
$column_counter = 1
}
Write-Output ” ”
}
# Checking if the Inventory.xlsx already exists
if(Test-Path C:\AzurePerformanceMetrics\Performance_metrics.xlsx){
Write-Host “C:\AzurePerformanceMetrics\Performance_metrics.xlsx already exitst. Deleting the current file and creating a new one. `n” -ForegroundColor Yellow
Remove-Item C:\AzurePerformanceMetrics\Performance_metrics.xlsx
# Saving the workbook/excel file
$workbook.SaveAs(“C:\AzurePerformanceMetrics\Performance_metrics.xlsx”)
}else {
# Saving the workbook/excel file
$workbook.SaveAs(“C:\AzurePerformanceMetrics\Performance_metrics.xlsx”)
}
$excel.Quit()

Click here to download my PowerShell scripts for Free !!

8 comments

  1. Hello, congratulations for Script, but after execute, returned this error.

    WARNING: API deprecation: The use of the legacy metrics API will be discontinued in the next release. This implies a change in the call and the output of this cmdlet.
    WARNING: API deprecation: The use of the legacy metrics API will be discontinued in the next release. This implies a change in the call and the output of this cmdlet.
    WARNING: API deprecation: The use of the legacy metrics API will be discontinued in the next release. This implies a change in the call and the output of this cmdlet.
    WARNING: API deprecation: The use of the legacy metrics API will be discontinued in the next release. This implies a change in the call and the output of this cmdlet.
    WARNING: API deprecation: The use of the legacy metrics API will be discontinued in the next release. This implies a change in the call and the output of this cmdlet.
    WARNING: API deprecation: The use of the legacy metrics API will be discontinued in the next release. This implies a change in the call and the output of this cmdlet.
    WARNING: API deprecation: The use of the legacy metrics API will be discontinued in the next release. This implies a change in the call and the output of this cmdlet.

    Have one idea bout api?

    Like

  2. am getting this error :

    ‘Virtual : The term ‘‘Virtual’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Users\skarrita\Downloads\NotePadPlus\vmdetails.ps1:36 char:33
    + $VirtualMachineWorksheet.Name = ‘Virtual Machine perf metrics’
    + ~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (‘Virtual:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    ‘Resource : The term ‘‘Resource’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Users\skarrita\Downloads\NotePadPlus\vmdetails.ps1:38 char:44
    + $VirtualMachineWorksheet.Cells.Item(1,1) = ‘Resource Group Name’
    + ~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (‘Resource:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    ‘VM : The term ‘‘VM’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Users\skarrita\Downloads\NotePadPlus\vmdetails.ps1:39 char:44
    + $VirtualMachineWorksheet.Cells.Item(1,2) = ‘VM Name’
    + ~~~~~
    + CategoryInfo : ObjectNotFound: (‘VM:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    ‘Location’ : The term ‘‘Location’’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Users\skarrita\Downloads\NotePadPlus\vmdetails.ps1:40 char:44
    + $VirtualMachineWorksheet.Cells.Item(1,3) = ‘Location’
    + ~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (‘Location’:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    ‘Percentage : The term ‘‘Percentage’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Users\skarrita\Downloads\NotePadPlus\vmdetails.ps1:41 char:44
    + $VirtualMachineWorksheet.Cells.Item(1,4) = ‘Percentage CPU’
    + ~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (‘Percentage:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    ‘Units’ : The term ‘‘Units’’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Users\skarrita\Downloads\NotePadPlus\vmdetails.ps1:42 char:44
    + $VirtualMachineWorksheet.Cells.Item(1,5) = ‘Units’
    + ~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (‘Units’:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Like

Leave a comment