CPU usage report of the virtual machines

Vinoth Kaliaperumal 386 Reputation points
2023-03-27T04:50:58.43+00:00

How to pull the CPU usage report of the virtual machines present in the subscription.
VM insights are disabled do we have any other way to pull the usage report.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
{count} votes

Answer accepted by question author
  1. vipullag-MSFT 26,522 Reputation points Moderator
    2023-03-28T08:57:01.0733333+00:00

    Hello Vinoth Kaliaperumal

    Welcome to Microsoft Q&A Platform, thanks for posting your query here.

    For your ask "I need to pull CPU usage report of all the virtual machines present in a subscription."

    As mentioned in previous response, there is not straight way from Azure Portal.

    You can do this using scripting. I just tried this using a powershell script and it works. Please note that this is a sample script and you may need to add/modify based on your need.

    #get all vms in a subscription
    $vms = Get-AzVM
    
     #get the last 3 days data
     #end date
     $et=Get-Date
    
     #start date
     $st=$et.AddDays(-3)
    
      $arr =@()
    
     foreach($vm in $vms)
     {
     
     $s = ""
     
     #percentage cpu usage
    $cpu = Get-AzMetric -ResourceId $vm.Id -MetricName "Percentage CPU" -DetailedOutput -StartTime $st `
     -EndTime $et -TimeGrain 12:00:00  -WarningAction SilentlyContinue
    
    
     
     # 3 days == 72hours == 12*6hours
    
     $cpu_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
     }
    
    
     # add all the related info to the string
     $s = "VM Name: " + $vm.name + "; Resource Group: " + $vm.ResourceGroupName + "; CPU: " +$cpu_total
    
     # add the above string to an array
     $arr += $s
     }
    
     #check the values in the array
     $arr
    

    Output sample for your reference:

    User's image

    Hope this helps.

    If the suggested response helped you resolve your issue, please 'Accept as answer', so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Manu Philip 20,491 Reputation points MVP Volunteer Moderator
    2023-03-27T05:13:54.42+00:00

    Here is the way in which you can monitor the cpu usage of the virtual machine. It uses Azure VM Metrics

    1. Go to the VM resource that you are interested in
    2. Go to Monitoring > Metrics
    3. Select the scope you are interested in
    4. Configure the filters to show the data you would like to see
    5. Click on Save to dashboard and then select Pin to dashboard
    6. Either use an existing dashboard or create a new dashboard
    7. Pin additional metrics from other resources/scopes as needed

    User's image


    --please don't forget to upvote and Accept as answer if the reply is helpful--


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.