top of page
Writer's pictureRattandeep singh

Time To Upgrade: Optimizing Azure VM Performance - Create an Effective PowerShell Script for Memory Usage Monitoring



In today's cloud-driven world, managing costs effectively is crucial. For Azure users, understanding and optimizing virtual machine (VM) memory usage can be a significant step towards achieving FinOps (financial management for cloud services). This blog post will explore two PowerShell scripts that can help you gain valuable insights into your VM memory utilization and how this data can be leveraged for FinOps practices.


Script 1: Building the Inventory
$data = get-content ./Inputfile.txt
$output = @()
foreach($record in $data){$output += get-azvm -name $record | select` Name,ID,ResourceGroupName,@{name = "VmSize";
						expression ={($_.hardwareprofile.vmsize)}} 
}
$output | export-csv Inputfile.csv

The first script serves as a data preparation tool. It reads an input file containing a list of VM names and generates a CSV file with essential VM details like Name, ID, Resource Group Name, and VM Size. Here's a breakdown of the script:


  1. Data Input: The script retrieves VM names from a text file named Inputfile.txt.

  2. Creating the Output: An empty array named $output is initialized to store the processed VM data.

  3. Iterating Through VMs: The script loops through each VM name ($record) in the input file.

  4. Extracting VM Details: The Get-AzVM cmdlet retrieves information for the current VM and selects specific properties:

  • Name: Name of the virtual machine.

  • ID: Unique identifier for the VM resource.

  • ResourceGroupName: Resource group to which the VM belongs.

  • VmSize (Custom Property): This line uses a calculated property to extract the VM size from the hardwareprofile.vmsize property.

  1. Building the Output Array: The script adds the extracted details for each VM as a new object to the $output array.

  2. Exporting Data: Finally, the $output array containing the VM inventory is exported to a CSV file named Inputfile.csv using Export-Csv.

Note: Ensure you have the Azure Az module installed and configured with appropriate permissions to access your VMs.


Script 2: Monitoring Memory Usage
$startTime = (Get-Date).AddDays(-30)
$endTime = Get-Date
$data = @()
$azVM = Import-Csv ./Inputfile.csv
foreach($vm in $azVM)
{$vmsize = $vm.VmSize
 write-host "$($vm.HardwareProfile.VmSize)"
 $vmmem = Get-AzVMSize -ResourceGroupName $vm.ResourceGroupName -VMName   $vm.Name | where{$_.Name -like $vmsize}
  $vmmemGB = $vmmem.MemoryInMB/1024
  $resourceId = $vm.id
  $metrics = Get-AzMetric -ResourceId $resourceId -MetricName "Available Memory Bytes" -TimeGrain 01:00:00 -StartTime $startTime -EndTime $endTime -ErrorAction SilentlyContinue
  $metrics.Data |`
  ForEach-Object {
          $data += [PSCustomObject]@{VM_Name = $vm.Name
                        Timestamp = $($_.TimeStamp)
                        Used_Memory = ($vmmemGB - ($($_.Average)/1073741824))
                        Available_Memory_GB = ($($_.Average)/1073741824)
                        Total_Memory_GB = $vmmemGB
                        }
                 
                 }
}
$data |Export-Csv Mem_Usage.csv

The second script focuses on gathering and analyzing memory utilization data. It iterates through the VM list generated by the first script and calculates the used memory for each VM over a specified time period.


  1. Setting Time Frame: The script defines the start and end dates ($startTime and $endTime) for collecting memory usage data. You can adjust these values to fit your desired analysis window (e.g., past week, past month).

  2. Initializing Variables: An empty array $data is created to store the memory usage data for all VMs. The script then imports the VM inventory data from Inputfile.csv using Import-Csv.

  3. Iterating Through VMs: The script loops through each VM ($vm) in the imported data.

  4. Extracting VM Size: The VM size ($vmsize) is retrieved from the current VM object.

  5. Verifying VM Size: The script uses Write-Host to display the VM size for reference (optional for troubleshooting).

  6. Retrieving Memory Specs: The Get-AzVMSize cmdlet retrieves the memory specifications for the specific VM size, considering the VM's resource group and name. The script then calculates the total memory in gigabytes ($vmmemGB) by dividing the retrieved memory value (in MB) by 1024.

  7. Building Resource ID: The script constructs the unique resource ID ($resourceId) for the current VM.

  8. Collecting Memory Metrics: The Get-AzMetric cmdlet retrieves memory usage data for the VM within the specified time frame. It focuses on the "Available Memory Bytes" metric with a one-hour time grain (01:00:00). The -ErrorAction SilentlyContinue parameter ensures the script continues processing even if data retrieval fails for a specific VM.

  9. Calculating Memory Usage: The script iterates through each data point ($_) in the retrieved metrics ($metrics.Data). It calculates the used memory by subtracting the average available memory from the total memory and then dividing by the conversion factor (1073741824) to get used memory in gigabytes. The available memory is directly derived from the average metric value divided by the conversion factor.

  10. Building Custom Object: A new custom object is created for each data point, containing the following information:

  • VM_Name: Name of the virtual machine.

  • Timestamp: Date and time for the data point.

  • Used_Memory: Memory utilized by the VM in gigabytes.

  • Available_Memory_GB: Available memory on the VM in gigabytes.

  • Total_Memory_GB: Total memory allocated to the VM in gigabytes.

  1. Storing Data: The script adds the newly created custom object


Leveraging Memory Usage Data for FinOps

By utilizing these scripts, you can gain valuable insights into your Azure VM memory consumption. The collected data on used memory, available memory, and total memory provides a strong foundation for FinOps practices:

  • Rightsizing VMs: Analyze VM memory usage patterns. If a VM consistently uses only a fraction of its allocated memory, consider downsizing it to a smaller size instance, reducing your overall Azure costs.

  • Identifying Idle VMs:  If a VM has consistently high available memory, it might be underutilized. Consider stopping or de-provisioning such VMs to eliminate unnecessary charges.

  • Scheduling VM Operations: Schedule memory-intensive tasks for VMs with sufficient available memory during off-peak hours to optimize resource utilization and potentially avoid scaling costs.


Using the Data

The memory usage data is exported to a CSV file named Mem_Usage.csv. You can import this file into various data analysis tools like Excel, Power BI, or even integrate it with your existing cost management platform to visualize memory usage trends and identify opportunities for cost optimization.

By combining this data with your VM billing information, you can calculate the memory utilization cost for each VM and identify areas for potential cost savings. Remember, FinOps is an ongoing process. Regularly monitor your VM memory usage and adjust your resources as needed to achieve optimal performance and cost-effectiveness for your Azure environment.

31 views0 comments

Comments


bottom of page