13 Replies Latest reply on Jul 16, 2019 9:17 AM by wfrancoti

    Performance Script to get VM Maximum CPU and Memory

    wfrancoti Novice

      Hello Team,

       

      I am working in a project to consolidate some servers in a new Cluster and to right size this cluster I want to collect Average cpu peak of my VMs, I am not a script guy someone could help me here with that?

       

      My powercli version is 6.5 Release 1.

       

      Thanks in advanced.

        • 1. Re: Performance Script to get VM Maximum CPU and Memory
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          Is there a specific reason why you are using this old PowerCLI version?
          If not, I would suggest to first upgrade.
          See Welcome PowerCLI to the PowerShell Gallery – Install Process Updates for instructions.

           

          Once done, you could do something like this

           

          $clusterName = 'cluster'

          $start = (Get-Date).AddDays(-1)

          $stat = 'cpu.usage.average'

          $vms = Get-cluster -Name $clusterName | Get-VM


          Get-Stat -Entity $vms -Stat $stat -Start $start -Instance '*' -ErrorAction SilentlyContinue |

          Group-Object -Property {$_.Entity.Name} |

          ForEach-Object -Process {

             New-Object PSObject -Property ([ordered]@{

             VM = $_.Name

             Start = $start

             PeakCpuPerc = [math]::Round(($_.Group | Measure-Object -Property Value -Maximum).Maximum,1)

             })

          }

          • 2. Re: Performance Script to get VM Maximum CPU and Memory
            wfrancoti Novice

            Thanks for the quick reply LucD, I really appreciate your help! I just updated my PowerCli using the article you mentioned below. It worked

             

            Thanks

            • 3. Re: Performance Script to get VM Maximum CPU and Memory
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              What size environment are we talking about.

              Depending on the number of VMs and the length of the period you are looking at, the script might run for a longer time.

               

              I would suggest to first try it out with a few VMs and only for a short period, for example 1 day.

              • 4. Re: Performance Script to get VM Maximum CPU and Memory
                wfrancoti Novice

                The cluster I am running has 237 VMs and I am running it remotely. It takes sometimes but worked. Before I run the script I changed the 2nd line to $start = (Get-Date).AddDays(-30) it gets average Max CPU from the last 30 days? How to get it from the latest 3 months but instead of % in Mhz? and also I forgot to ask if is possible to export the output to a CSV file.

                 

                Thanks again

                • 5. Re: Performance Script to get VM Maximum CPU and Memory
                  LucD Guru
                  Community WarriorsUser ModeratorsvExpert

                  No, that data is the start of the interval the script looked at.

                  You definitely got the highest CPU% over the period in $start till now.

                   

                  The following version of the snippet provides the 3 timestamp, start and finish of the interval and the time when the highest percentage occurred.
                  And the results are exported to a CSV file.

                   

                  $clusterName = 'cluster'

                   

                  $now = Get-Date

                  $start = $now.AddDays(-1)

                  $stat = 'cpu.usage.average'


                  $vms = Get-cluster -Name $clusterName | Get-VM


                  Get-Stat -Entity $vms -Stat $stat -Start $start -Instance '*' -ErrorAction SilentlyContinue |

                  Group-Object -Property { $_.Entity.Name } |

                  ForEach-Object -Process {

                     $max = $_.Group | Sort-Object -Property value -Descending | select -first 1

                     New-Object PSObject -Property ([ordered]@{

                     VM = $_.Name

                     StartInterval = $start

                     EndInterval = $now

                     PeakCpuPerc = [math]::Round($max.Value, 1)

                     MaxTimestamp = $max.Timestamp

                     })

                  } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

                  • 6. Re: Performance Script to get VM Maximum CPU and Memory
                    wfrancoti Novice

                    Amazing you are rock!!!

                     

                    The last question, which line or parameter I need to change to get Mhz values instead of %?

                     

                    Thanks

                    • 7. Re: Performance Script to get VM Maximum CPU and Memory
                      LucD Guru
                      vExpertUser ModeratorsCommunity Warriors

                      Replace 'cpu.usage.average' with 'cpu.usagemhz.average'.

                      • 8. Re: Performance Script to get VM Maximum CPU and Memory
                        wfrancoti Novice

                        LucD, Excellent script I have no words to thank you. I’m indebted to you!

                        • 9. Re: Performance Script to get VM Maximum CPU and Memory
                          wfrancoti Novice

                          Ops, I forgot to request how to add ,'mem.usage.average' on this script.

                           

                          Thanks

                          • 10. Re: Performance Script to get VM Maximum CPU and Memory
                            LucD Guru
                            Community WarriorsvExpertUser Moderators

                            You could do something lie this

                             

                            $clusterName = 'cluster'

                            $now = Get-Date

                            $start = $now.AddDays(-1)

                            $stat = 'cpu.usagemhz.average', 'mem.usage.average'


                            $vms = Get-cluster -Name $clusterName | Get-VM


                            Get-Stat -Entity $vms -Stat $stat -Start $start -Instance '*' -ErrorAction SilentlyContinue |

                            Group-Object -Property { $_.Entity.Name } |

                            ForEach-Object -Process {

                               $maxCpu = $_.Group | where { $_.MetricId -eq 'cpu.usagemhz.average' } |

                               Sort-Object -Property value -Descending | select -first 1

                               $maxMem = $_.Group | where { $_.MetricId -eq 'mem.usage.average' } |

                               Sort-Object -Property value -Descending | select -first 1

                               New-Object PSObject -Property ([ordered]@{

                               VM = $_.Name

                               StartInterval = $start

                               EndInterval = $now

                               MaxCpuTimestamp = $maxCpu.Timestamp

                               PeakCpuMhz = [math]::Round($maxCpu.Value, 1)

                               MaxMemTimestamp = $maxMem.Timestamp

                               PeakMemPerc = [math]::Round($maxMem.Value, 1)

                               })

                            }

                            • 11. Re: Performance Script to get VM Maximum CPU and Memory
                              wfrancoti Novice

                              Hello LucD, good morning.

                               

                              Is there any chance to replace the line about memory average to get it in MB instead of %? I am running this script to right sizing a new cluster in my environment.

                               

                              Thanks a lot again.

                              • 12. Re: Performance Script to get VM Maximum CPU and Memory
                                LucD Guru
                                Community WarriorsvExpertUser Moderators

                                Not directly through a metric, but since we know the memorysize of the VM, we can calculate the value.

                                Something like this

                                 

                                $clusterName = 'cluster'

                                $now = Get-Date

                                $start = $now.AddDays(-1)

                                $stat = 'cpu.usagemhz.average', 'mem.usage.average'


                                $vms = Get-cluster -Name $clusterName | Get-VM


                                Get-Stat -Entity $vms -Stat $stat -Start $start -Instance '*' -ErrorAction SilentlyContinue |

                                Group-Object -Property { $_.Entity.Name } |

                                ForEach-Object -Process {

                                   $maxCpu = $_.Group | where { $_.MetricId -eq 'cpu.usagemhz.average' } |

                                   Sort-Object -Property value -Descending | select -first 1

                                   $maxMem = $_.Group | where { $_.MetricId -eq 'mem.usage.average' } |

                                   Sort-Object -Property value -Descending | select -first 1

                                   New-Object PSObject -Property ([ordered]@{

                                   VM = $_.Name

                                   StartInterval = $start

                                   EndInterval = $now

                                   MaxCpuTimestamp = $maxCpu.Timestamp

                                   PeakCpuMhz = [math]::Round($maxCpu.Value, 1)

                                   MaxMemTimestamp = $maxMem.Timestamp

                                   PeakMemGB = [math]::Round($maxMem.Entity.MemoryGB * $maxMem.Value / 100, 1)

                                   })

                                }

                                • 13. Re: Performance Script to get VM Maximum CPU and Memory
                                  wfrancoti Novice

                                  LucD, Thanks a lot for your help, your scripts worked like a charm, I really appreciate your availability and help with that.