PowerCLI

 View Only
Expand all | Collapse all

Script to find overutilized virtual machines

  • 1.  Script to find overutilized virtual machines

    Posted Jan 17, 2012 02:54 PM

    Hello:

    Is there a way to find all overutilized virtual machines on the VC via script? for example, I want to find all Vms that are using more than 80% memory and/or CPU 10 minutes in a row or longer.

    The same question I have for underutilized virtual machines, but it will be easy once I get the first part working.

    Thanks a lot,

    qwert



  • 2.  RE: Script to find overutilized virtual machines

    Posted Jan 17, 2012 04:39 PM

    I dont know how to do this via script but we use Capacity IQ to do it. You can dowload a 60 day free trial of CapIQ and install the appliance, give it some time to gather info and then run the reports to gather all those metrics. It will give you oversized VMs and undersized VMs so you can better tweek and balance your systems. It does a lot of other stuff too. Worth atleast getting the trial. The longer you run it the better the porojections on future capacity are, but for what you are looking for if it runs for a day you will see your oversized and undersized VMs.

    Mike P



  • 3.  RE: Script to find overutilized virtual machines

    Posted Jan 17, 2012 09:14 PM

    You can try something like this.

    The script will collect the statistical data for the last 24 hours and group the results in 10-minute intervals.

    If the CPU or the memory is used more than 80% in such an interval the script will generate an object.

    $vms = Get-VM 
    $metric
    = "cpu.usage.average","mem.usage.average"
    $start
    = (Get-Date).AddHours(-24) $stats = Get-Stat -Entity $vms -Stat $metric -Start $start
    $stats
    |
    Group-Object -Property {$_.Entity.Name},MetricId,{[int](($_.Timestamp.Hour * 60 + $_.Timestamp.Minute)/10)} | %{     if(($_.Group | Measure-Object -Property Value -Average).Average -gt 80){         New-Object PSObject -Property @{             VM = $_.Values[0]             Time = ($_.Group | Sort-Object -Property TimeStamp | Select -First 1).TimeStamp             Metric = $_.Values[1]             Average = "{0:N}" -f ($_.Group | Measure-Object -Property Value -Average).Average         }     } }


  • 4.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 02:21 PM

    Luc,

    Thanks a lot!

    Looks like it's what I am looking.

    However when I run your script I got error:

    A parameter cannot be found that matches argument '$null'.

    At :line:7 char:18

    + $stats = Get-Stat <<<< -Entity $vms -Stat $metric -Start $start $stats |

    I cannot figure out what is wrong here. Maybe I should select only VMs that are powered On? What is wrong here?

    Again, thanks a lot for your help.

    qwert



  • 5.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 02:25 PM

    Yes, try with a VM that is powered on for at least 24 hours (we are retrieving the last 24 hours of data).

    Change the first line to something like this

    $vms = Get-VM -Name MyVM


  • 6.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 04:30 PM

    Luc,

    No, it does not work.  I picked VM that been up for few days for sure, but it gave me the same error:

    A parameter cannot be found that matches argument '$null'.

    At :line:7 char:18

    + $stats = Get-Stat <<<< -Entity $vms -Stat $metric -Start $start $stats |

    Any ideas?

    Thank you,

    qwert



  • 7.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 04:55 PM

    Ok, are you using PowerCLI 5.0.1 with VMs running on ESX 4.1 servers ?



  • 8.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 05:17 PM

    No, I am using PowerCLI 4.1 U1 build 332441 and hosts are on ESX 4.1 servers



  • 9.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 05:23 PM

    I think I found it, there was a <CR><LF> gone in the code above.

    I corrected it, please try again.



  • 10.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 05:53 PM

    Luc,

    It's working better now. At least it did not give me any errors. It did not give me anything at the result and it's probably because I do not have any overutilized VMs; However, when I changed to "Average -le 50" in order to find underutilized VMs it gave me the following error:

    A parameter cannot be found that matches parameter name 'Property'.

    At :line:27 char:38

    + New-Object PSObject -Property <<<< @{

    How can I fix this (hope last error)?

    thank you,

    qwert



  • 11.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 06:03 PM

    The Property parameter was introduced in PowerShell v2.

    Are you still on PowerShell v1 perhaps ?

    Do a

    Get-Host

    Does it say Version 2 in the output ?



  • 12.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 06:15 PM

    Yes, it said Version 1.0.0.0

    Should I upgrade to v2?



  • 13.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 06:20 PM

    For now PowerCLI still supports PowerShell v1.

    But with PowerShell v3 around the corner, I think you should upgrade to v2.



  • 14.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 06:37 PM

    Luc,

    I will upgrade it tomorrow and letyou know if it fix it.

    Thank you



  • 15.  RE: Script to find overutilized virtual machines

    Posted Jan 19, 2012 07:34 PM

    Luc,

    I upgraded PowerShell to v2 and your script started to work just fine.

    However, it does not create list of the VMs, but gives statistics  for cpu usage every 30 minutes for VM with cpu average more (or less) than parameter in the script.

    Few more questions:

    How script should be changed in order just collect names of those VMs?

    What should be done in order to add "memory" parameter as well and select VMs that have both parameters over/under utilized?

    Thanks again,

    qwert



  • 16.  RE: Script to find overutilized virtual machines

    Posted Jan 19, 2012 08:09 PM

    Try something like this

    $limit = 80
    $vms = Get-VM
    $metric = "cpu.usage.average","mem.usage.average" $start = (Get-Date).AddHours(-24) $stats = Get-Stat -Entity $vms -Stat $metric -Start $start $stats | Group-Object -Property {$_.Entity.Name},{[int](($_.Timestamp.Hour * 60 + $_.Timestamp.Minute)/10)} | %{     $cpuAvg = ($_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Average).Average     $memAvg = ($_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property Value -Average).Average     if($cpuAvg -gt $limit -and $memAvg -gt $limit){             Select -InputObject $_ @{N="VM";E={$_.Values[0]}},@{N="Time";E={$_.Group[0].Timestamp}}     } }


  • 17.  RE: Script to find overutilized virtual machines

    Posted Feb 04, 2013 04:43 PM

    Hello Guys,

    I dont mean to Hijack some one's thread. I am looking for the same script to get the Oversized and undersized virtual machines list.

    I am getting the following error when i ran the above scipt??

    Get-Stat : 2/4/2013 10:36:35 AM    Get-Stat        The metric counter "cpu.usage.average" doesn't exist for entity "SDB-Win7".
    At line:1 char:18
    + $stats = Get-Stat <<<<  -Entity $vms -Stat $metric -Start $start
        + CategoryInfo          : ResourceUnavailable: (cpu.usage.average:String) [Get-Stat], VimException
        + FullyQualifiedErrorId : Client20_RuntimeDataServiceImpl_CheckUserMetrics_MetricDoesntExist,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetViStats

    Also help me with how can i export output in to an excel sheet.

    Thanks

    VK



  • 18.  RE: Script to find overutilized virtual machines

    Posted Feb 04, 2013 04:51 PM

    That's probably because that VM was not running at the start of the requested interval.

    Try adding '-ErrorAction SilentlyContinue' to the Get-Stat cmdlet.



  • 19.  RE: Script to find overutilized virtual machines

    Posted Feb 04, 2013 05:27 PM

    Hey LucD,

    I tried adding it and it did not recognize the parameter. Can you suggest how to add it and how to export the output to excel or html file ??

    $limit = 80
    $vms = Get-VM
    $metric = "cpu.usage.average","mem.usage.average"
    $start = (Get-Date).AddHours(-24)
    $stats = Get-Stat -ErrorAction SilentlyContinue
    $stats = Get-Stat -Entity $vms -Stat $metric -Start $start
    $stats |
    Group-Object -Property {$_.Entity.Name},{[int](($_.Timestamp.Hour * 60 + $_.Timestamp.Minute)/10)} | %{
        $cpuAvg = ($_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Average).Average
        $memAvg = ($_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property Value -Average).Average
        if($cpuAvg -gt $limit -and $memAvg -gt $limit){
                Select -InputObject $_ @{N="VM";E={$_.Values[0]}},@{N="Time";E={$_.Group[0].Timestamp}}

    Thanks

    VK



  • 20.  RE: Script to find overutilized virtual machines

    Posted Feb 04, 2013 05:31 PM

    That should be

    $limit = 80
    $vms = Get-VM
    $metric = "cpu.usage.average","mem.usage.average"
    $start = (Get-Date).AddHours(-24)
    $stats = Get-Stat -Entity $vms -Stat $metric -Start $start -ErrorAction SilentlyContinue
    $stats |
    Group-Object -Property {$_.Entity.Name},{[int](($_.Timestamp.Hour * 60 + $_.Timestamp.Minute)/10)} | %{
        $cpuAvg = ($_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Average).Average
        $memAvg = ($_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property Value -Average).Average
        if($cpuAvg -gt $limit -and $memAvg -gt $limit){
                Select -InputObject $_ @{N="VM";E={$_.Values[0]}},@{N="Time";E={$_.Group[0].Timestamp}}
    }}


  • 21.  RE: Script to find overutilized virtual machines

    Posted Feb 04, 2013 05:59 PM

    Thanks a lot LucD,

    It worked. In the Next steps the screen got hang up at this >> mark and it gives me no output.

    PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> $limit = 80
    PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> $vms = Get-VM
    PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> $metric = "cpu.usage.average","mem.usage.average"
    PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> $start = (Get-Date).AddHours(-24)
    PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> $stats = Get-Stat -Entity $vms -Stat $metric -Start $start -ErrorAction SilentlyContinue
    PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> $stats |
    >> Group-Object -Property {$_.Entity.Name},{[int](($_.Timestamp.Hour * 60 + $_.Timestamp.Minute)/10)} | %{
    >>     $cpuAvg = ($_.Group | where {$_.MetricId -eq "cpu.usage.average"} | Measure-Object -Property Value -Average).Average
    >>     $memAvg = ($_.Group | where {$_.MetricId -eq "mem.usage.average"} | Measure-Object -Property Value -Average).Average
    >>     if($cpuAvg -gt $limit -and $memAvg -gt $limit){
    >>             Select -InputObject $_ @{N="VM";E={$_.Values[0]}},@{N="Time";E={$_.Group[0].Timestamp}}
    >>
    >>
    >>

    if i keep on pressing enter it loads >> but there is no ouptut.

    Thanks

    VK



  • 22.  RE: Script to find overutilized virtual machines

    Posted Feb 04, 2013 06:17 PM

    The last line was missing.

    There should be 2 closing curly braces.

    Btw, isn't a lot easier when you save the code in a .ps1 file and then execute the script from the file ?

    PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> ./script.ps1


  • 23.  RE: Script to find overutilized virtual machines

    Posted Feb 04, 2013 07:58 PM

    Hey LucD,

    I ran the script in the powershell editor and also in power cli on two different vcenters. I did not get any errors and also no output. I guess i have to chage the limit. so I changed  the limit to 40.The limit you are reffering is to print the machine names which reached the average use of CPU and Memory to 40%. am i right??

    Also the output prints the same vm multiple times with the time stamp??

    here is the output

    VM                                                                               Time

    --                                                                               ----

    28                                                       2/4/2013 12:00:00 PM

    48                                                        2/4/2013 10:30:00 AM

    48                                                       2/4/2013 10:00:00 AM

    48                                                        2/4/2013 9:30:00 AM

    48                                                      2/4/2013 9:00:00 AM

    48                                                      2/4/2013 8:00:00 AM

    48                                                    2/4/2013 7:30:00 AM

    45                                   2/4/2013 9:30:00 AM

    45                                      2/4/2013 9:00:00 AM

    45                                      2/4/2013 8:00:00 AM

    45                                   2/4/2013 7:30:00 AM

    45                                       2/4/2013 3:00:00 AM

    56                                                  2/4/2013 12:30:00 PM

    56                                                 2/4/2013 12:00:00 PM

    56                                             2/4/2013 11:30:00 AM

    lab-vm                                                             2/4/2013 1:30:00 AM

    lab-vm                                                             2/4/2013 1:00:00 AM

    After the braces i added this export command and it started asking for Input object. I think powershell recognized export command as a new command line rather than part of that code?? How can i add the export command as the part of the code??

    >> export-csv C:\report.csv -UseCulture -NoTypeInformation
    >>
    cmdlet Export-Csv at command pipeline position 1
    Supply values for the following parameters:
    InputObject:

    Thanks

    VK



  • 24.  RE: Script to find overutilized virtual machines

    Posted Feb 04, 2013 08:01 PM

    You have to 'pipe' the objects to the Export-Csv cmdlet. Did you place the pipe symbol (vertical bar) after the last closing curly brace ?

    You will get instances if both CPU and memory are above the limit.

    And you see a line for each interval for each VM where this condition evaluated to $true.

    That explains why the multiple lines for the same VM, but each with a different timestamp.



  • 25.  RE: Script to find overutilized virtual machines

    Posted Feb 04, 2013 09:27 PM

    Hey LucD,

    That is working is fine. I have added the Pipe. If  i want to find out the overutilized vm's only w.r.t to memory not cpu for about 10 days is it possible?? Shall i increase the (Get-Date).AddHours(-24) to 240 (10days) and for memory what i have to do??

    Thanks

    VK



  • 26.  RE: Script to find overutilized virtual machines

    Posted Feb 04, 2013 09:57 PM

    That would be possible, but a DateTime object in PowerShell has other methods.

    You could do

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

    The test against the limit for the CPU and memory is done in this line

    if($cpuAvg -gt $limit -and $memAvg -gt $limit){
    

    If you only want to test the memory against the limit, you change that line like this

    if($memAvg -gt $limit){
    


  • 27.  RE: Script to find overutilized virtual machines

    Posted Jul 04, 2014 03:03 AM

    Hi

       LucD Guru  User Moderators vExpert  

     



  • 28.  RE: Script to find overutilized virtual machines

    Posted Jul 04, 2014 04:48 AM

    In my PowerCLI & vSphere statistics – Part 2 – Come together post I have an example on how to produce reports for specific time frames.

    Have a look.



  • 29.  RE: Script to find overutilized virtual machines

    Posted Jul 04, 2014 01:50 PM

    This is awesome.

    Thanks Luc !



  • 30.  RE: Script to find overutilized virtual machines

    Posted Jan 18, 2012 05:19 PM

    There is something in $vms I assume ?



  • 31.  RE: Script to find overutilized virtual machines

    Posted Jul 04, 2014 02:56 AM

    Well if u looking for VM which are using 80% CPU then u would call them undersize , don't u .. ie u need to add more memory .. the headline of the thread is otherwise tho ..