PowerCLI

 View Only
Expand all | Collapse all

average cpu and memory script

  • 1.  average cpu and memory script

    Posted Jul 16, 2014 05:25 PM

    Hi ,

    need to extract cpu and memory usage for 800+ vm's for a day.. the existing once which are there in the community are getting hung , due to which its not completing.

    could any one pls help.

    thanks



  • 2.  RE: average cpu and memory script

    Posted Jul 16, 2014 07:01 PM

    Do you mean the average CPU and Mem for each VM over a 1 day interval.

    Or all the metrics for all VMs for 1 day ?



  • 3.  RE: average cpu and memory script

    Posted Jul 16, 2014 07:53 PM

    for each vm 1 cpu usage value which is an average for the day and 1 mem usage value which is an averate for the day.



  • 4.  RE: average cpu and memory script

    Posted Jul 16, 2014 08:11 PM

    Try like this

    $stats = "cpu.usage.average","mem.usage.average"
    $start = (Get-Date).AddDays(-1)
    $vms = Get-VM

    Get-Stat -Entity $vms -Stat $stats -Start $start |
    Group-Object -Property {$_.Entity.Name} | %{
     
    New-Object PSObject -Property @{
       
    VM = $_.Group[0].Entity.Name
       
    Date = $start
       
    CPUAvg = $_.Group | where {$_.MetricId -eq "cpu.usage.average"} |
         
    Measure-Object -Property Value -Average |
         
    Select -ExpandProperty Average
       
    MemAvg = $_.Group | where {$_.MetricId -eq "mem.usage.average"} |
         
    Measure-Object -Property Value -Average |
         
    Select -ExpandProperty Average
      }
    }


  • 5.  RE: average cpu and memory script

    Posted Jul 17, 2014 06:22 AM

    Luc, I found your old script Discover Memory Overallocations the other day, which I thought was kind of sweet. I guess this is pretty much the same thing. In that function however you get the option to select the time frame.



  • 6.  RE: average cpu and memory script

    Posted Aug 13, 2014 07:08 AM

    Hi,

    I was looking for a script which will give me Average CPU and Average Memory statistics (with interval as 30 minutes) for last day. I have somewhere around 500 VMs.

    I tried with some of the Scripts from this Community, but those are taking a lot of time which is a main concern for me.

    Basically i wanted to schedule a job which will pull CPU and Memory values (with interval as small as 30 min) on a daily basis and store in my local database so that I can do some analysis on such historical data which is collected.

    If my jobs runs for a longer durations say (more than 2-3 hours), it will create problem for me.

    I want result set in a quick way. Please help.



  • 7.  RE: average cpu and memory script

    Posted Aug 13, 2014 08:24 AM

    Could you perhaps share the script you are using ?

    An alternative could be to use my Get-Stat2 function, that one tends to be faster than the regular Get-Stat cmdlet.



  • 8.  RE: average cpu and memory script

    Posted Aug 13, 2014 08:52 AM

    Below is the query I used

    ForEach ($VM in Get-VM )

    {

    $todaymidnight = (get-Date -hour 0 -minute 0 -second 0).addminutes(-1)

    Get-stat -entity $VM -stat cpu.usage.average -start $todaymidnight.adddays(-1) -finish $todaymidnight | Select Entity, TimeStamp, Value | Export-Csv "C:\vCenter\one.csv" -NoTypeInformation -UseCulture

    }



  • 9.  RE: average cpu and memory script

    Posted Aug 13, 2014 09:00 AM

    You seem to be calling the Get-Stat cmdlet for each VM, that causes a lot of unnecessary overhead.

    You can call Get-Stat for all VM and then use the Group-Object cmdlet to split out the results per VM (see the script I included previously).



  • 10.  RE: average cpu and memory script

    Posted Aug 13, 2014 10:26 AM

    Thanks for the help LucD. I am new to Power Cli and vCenter.

    I used below script, but still taking time (running for more than 90 minutes).

    In one of my vCenter site, I have 1000 servers, one get-stat command for pulling CPU and Memory for one day 30 min interval taking average 40 seconds, so I believe entire result set will take more than 11 hours, which is not quite acceptable.

    Can you suggest a way (is there any other way to pull data from vCenter (APIs or DB) - just guessing...

    $stats = "cpu.usage.average","mem.usage.average"

    $todaymidnight = (get-Date -hour 0 -minute 0 -second 0).addminutes(-1)

    start $todaymidnight.adddays(-1)

    get-date

    $vms = Get-VM

    Get-Stat -Entity $vms -Stat $stats -Start $start |

    Group-Object -Property EntityId | Foreach-Object{

      New-Object PSObject -Property @{

        VM = $_.Group[0].Entity.Name

        Date = $_.Group[0].Timestamp.Date

        CPU = $_.Group | Where-Object {$_.MetricId -eq $stats[0]} |

              Measure-Object -Average -Property Value |

              Select -ExpandProperty Average

        Memory = $_.Group | where {$_.MetricId -eq $stats[1]} |

              Measure-Object -Average -Property Value |

              Select -ExpandProperty Average

      }

    } | Select VM,Date,CPU,Memory |  Export-Csv "C:\vCenter\two.csv" -NoTypeInformation -UseCulture



  • 11.  RE: average cpu and memory script

    Posted Aug 13, 2014 11:28 AM

    I think there might be a typo in the script.

    Shouldn't it be like this ?

    $stats = "cpu.usage.average","mem.usage.average"

    $todaymidnight = (get-Date -hour 0 -minute 0 -second 0).addminutes(-1)

    $start = $todaymidnight.adddays(-1)

    $finish = $todaymidnight

    $vms = Get-VM

    Get-Stat -Entity $vms -Stat $stats -Start $start |

    Group-Object -Property EntityId | Foreach-Object{

      New-Object PSObject -Property @{

        VM = $_.Group[0].Entity.Name

        Date = $_.Group[0].Timestamp.Date

        CPU = $_.Group | Where-Object {$_.MetricId -eq $stats[0]} |

              Measure-Object -Average -Property Value |

              Select -ExpandProperty Average

        Memory = $_.Group | where {$_.MetricId -eq $stats[1]} |

              Measure-Object -Average -Property Value |

              Select -ExpandProperty Average

      }

    } | Select VM,Date,CPU,Memory |  Export-Csv "C:\vCenter\two.csv" -NoTypeInformation -UseCulture

    That script is calculating the averages of the complete previous day for CPU and memory for each VM.

    Is that what you are looking for ?

    You should time this with more than 1 VM on the Entity parameter.

    The advantage is that by passing multiple entities, you avoid a lot of the repetitive overhead that an individual Get-Stat call has.



  • 12.  RE: average cpu and memory script

    Posted Aug 13, 2014 11:35 AM

    Sorry I was looking for granular data (like 30 minutes interval) for the last day ?

    Can you provide correct script for that ?