PowerCLI

 View Only
Expand all | Collapse all

Get-stat Script for VM Perfomance - Questions

  • 1.  Get-stat Script for VM Perfomance - Questions

    Posted Jan 03, 2010 11:29 AM

    I am writing a script to get performance data on our VM's regarding in depth measures for CPU and Memory. Its a big project task I have to do for our mgt and so I am writing a hugely important and vital script for our company. I am using the get-stat command and from feedback from posts I posted regarding get-stat in particular from lucd and lebouf which have been mighty useful. But I have gathered their replies and written the script below. It currently has to be used with an argument but I can change that later. It also uses many counters for cpu and memory thats why it is a long script. But I have many questions regarding the script before I run it, as I need to be clear with the data I am getting back. The script is below:

    $vm = $args

    $vmguest = @{ N = "operating System" ; E = { ( get-vmguest -VM $_ ).osfullname}}

    Get-VM $args | where {$_.PowerState -eq "PoweredOn"} | `

    Select name,NumCpu, MemoryMB,

    @{N="Mem.Usage.Average";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat mem.usage.average | Measure-Object -Property Value -Average).Average}},

    @{N="Maxiumn Memory Usage Percentage";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat mem.usage.maximum | Measure-Object -Property Value -Average).Average}},

    @{N="Memory Usage KBytes";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat mem.consumed.average | Measure-Object -Property Value -Average).Average}},

    @{N="Memory State";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat mem.state.latest | Measure-Object -Property Value -Average).Average}},

    @{N="Memory Swapin Average KBytes";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat mem.swapin.average | Measure-Object -Property Value -Average).Average}},

    @{N="Memory Swapout Average KBytes";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat mem.swapout.average | Measure-Object -Property Value -Average).Average}},

    @{N="Memory Reserved Capacity MBytes";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat mem.reservedCapacity.average | Measure-Object -Property Value -Average).Average}},

    @{N="Average CPU Usage Percentage";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat cpu.usage.average | Measure-Object -Property Value -Average).Average}},

    @{N="CPU REady";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat cpu.ready.summation | Measure-Object -Property Value -Average).Average}},

    @{N="Maximum CPU Usage Percentage";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat cpu.usage.maximum | Measure-Object -Property Value -Average).Average}},

    @{N="Average CPU Usage MHZ";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat cpu.usagemhz.average | Measure-Object -Property Value -Average).Average}},

    @{N="Maximum CPU Usage MHZ";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat cpu.usagemhz.maximum| Measure-Object -Property Value -Average).Average}},

    @{N="Reserved CPU MHZ";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat cpu.reservedCapacity.average| Measure-Object -Property Value -Average).Average}},| `

    Export-Csv c:\stats5.csv

    The questions I have are:

    The first question is that I want the stats to be based on the last 7 days, so I have used the .adddays (-7). Would this give me the last 7 day stats?

    In my virtual centre statistics settings I have all the interval durations checked. So if I want the last 7 days stats, I would be using the samples for the interval duration 30 mins. Also I have not used any interval parameters in the get-stat, so my samples will bases on the 30 min interval. Is this correct ?

    Also I have not used the maxsample interval, so by default I will be getting to use all the sample values, and the measure-object will give me the average of all the samples values (30 min duration) in the last 7 days. Is this correct.?

    Another key question is that is it possible to get sample readings for only Mon- Fri, from 6am - 6pm, as thats when the VM's are used. I dont know how this can be done and how it can be incorporated in the script above. Is this possible to do ?

    Also regarding the measure object cmdlet, I have used for all the counters, such as for the average cpu and memory usage values. I am also using counters for max cpu and memory usage. Would I need to use the measure-object average value for this, as this counter will give me the max value for the cpu/memory for all the 7 days, which will be only one value, as I want to find out the max value it reaches, so I dont know if I should use measure-object with these counters, do I need to use it ?

    Also regarding the cpu ready counter I am using, gives the value in millseconds. Is it possible to get the cpu ready value in percentage like you see in ESXTop as this is a more easier to understand for cpuready. I cannot see any counter for cpu ready in percentages. Is there a way to get a value for this.



  • 2.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 03, 2010 01:01 PM

    Wow, lots of questions :smileywink:

    I'll try to answer.

    1) Yes, with .adddays (-7) you will get the last seven days of statistics.

    If you want this to start at midnight instead of the hour when you run the script have a look at PowerCLI & vSphere statistics – Part 1 – The basics. In the Intervals and values section I show how you can let your intervals start at midnight.

    2) Yes, the interval for the last seven days is normally the 30-minute interval. See also my blog entry I mentioned above. The section on Intervals

    And you will get all the samples (provided your are using at least PowerCLI 4).

    3) To get reports for a restricted period (Mon-Fri, 06:00 AM - 06:00 PM) you have several possibilities.

    You could use the Where-Object cmdlet (after the Get-Stat cmdlet) to let only the values for the period you're interested in reach the Measure-Object cmdlet

    Something like this:

    $businessDays = "Monday","Tuesday","Wednesday","Thursday","Friday"
    Get-Stat ... | Where {$businessDays -contains $_.Timestamp.DayOfWeek} | Measure-Object ...
    

    You can combine multiple conditions with the -and/-or operator in the where clause.

    Btw, my next blog will handle several filtering techniques.

    4) If you want to report the maximum CPU and Memory value over the 7-day period you should not go for the average but for the maximum.

    You can also do this with the Measure-Object cmdlet.

    ... | Measure-Object -Maximum
    

    Also consider what metric you're asking through the Get-Stat cmdlet. If you want the maximums you should for example use the cpu.usage.maximum metric and not the cpu.usage.average !

    5) Afaik there is no CPU Ready % metric. But its easy to calculate.

    Divide the ready value and divide it by the interval (30 minutes in your case) time. And multiply by 100 to get a percentage.



  • 3.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 06, 2010 06:04 PM

    Thanks for the feedback

    For the moment I ran the script as displayed on my original post, but I only get values for only few counters. I get no errors when I run the script.

    I get values for mem.usage.average, cpu.usage.average and cpu.usagemhz.average. But the other counters I get no values, these counters are:

    mem.usage.maximum

    mem.consumed.average

    mem.state.latest

    mem.swapin.average

    mem.swapout.average

    mem.reservedCapacity.average

    cpu.ready.summation

    cpu.usage.maximum

    cpu.reservedCapacity.average

    I need to find out why I dont get values for the above counters. Is there anything I need to setup or change. In the VC, all interval duration settings are selected in the statistics setting. Could it be anything to do with the measure-object which these counters do not like. I tried some of the counters without measure-object or use maximum instead of average but still I did not get any values.

    I am running ESX 3.5 and VC 2.5 not vsphere yet, do i need vpshere for these counters, I doubt it.

    Or is it anything to do with the 7 days.

    Just trying to find out why I get no values.

    Can anyone please assist. Thanks



  • 4.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 06, 2010 06:24 PM

    I'm afraid that script has a few flaws.

    I suspect you pass guestnames in the $Args variable ?

    *) the value you store in $vmguest is not exported to the CSV file

    *) the Get-Vm construction should, in my opinion, be something like this

    Get-VM $Args | where {$_.PowerState -eq "PoweredOn"} | %{
      Select name,NumCpu, MemoryMB,
        @{N="Mem.Usage.Average";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat mem.usage.average | Measure-Object -Property Value -Average).Average}},
        ...
       @{N="Reserved CPU MHZ";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat cpu.reservedCapacity.average| Measure-Object -Property Value -Average).Average}}} | Export-Csv ...
    

    The Get-Vm cmdlet can return zero, one or more guests.

    So you have to use a ForEach-Object loop (alias %) to run through all the returned guests.

    *) You have to be sure that all the metrics you specified are available in that time range. Check the statistical level for the period.

    See my PowerCLI & vSphere statistics – Part 1 – The basics post for more info on this.



  • 5.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 06, 2010 06:51 PM

    I tried the script against only one virtual machine and I only got values for the 3 counters I mentioned in my previous post. I will try the for-each and see if that makes a difference.

    In the virtual centre, in my statistics settings for each interval duration, I have statistical level 1 chosen which is the basic metrics for CPU and memory, so could it be the counters I have no values for are not included in this level. Is there a link which shows which metrics are included in which statistical level.

    I could choose statistical level 2 for the 30 minute duration and then wait a couple of days to run the script again. The only thing it says for level 2 is that all metrics are included for cpu and memory but the maximun and minimum roll types are excluded. Does that means the metrics for maximum cpu usage would not be included in this level 2 as there are maximum values included.

    Also changing a statistical level, does that require a restart of the VC service or does the change have an affect immediately.



  • 6.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 06, 2010 07:00 PM

    In my post there is a link to the vCenter Performance Counters document. In that document you can see which metric is included in which statistical level.

    Changing the statistical level does not require a restart of the VC service.



  • 7.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 06, 2010 09:19 PM

    Thanks for the reply and hood link to performance counters

    It seems that the counters that I am not getting values for are indeed in statistical level 2,3, or 4. But there are two counters mem.consumed.average and cpu.ready.summation that are in statistical level 1 but still show you as no values. Could this be that you still need to use a higher level or need to be in vpshere. Any feedback on this on why no values.

    I will change my statistical level in my VC to level 4 and then run the script again in a couple of days, maybe after 3 days.

    For now I will use the counters that give me values which are the mem and cpu average values. I have added the where statement which was lasted in the previous post about getting values for Mon - Fri as seen below:

    $vm = $args

    $vmguest = @{ N = "operating System" ; E = { ( get-vmguest -VM $_ ).osfullname}}

    $businessDays = "Monday","Tuesday","Wednesday","Thursday","Friday"

    Get-VM $args | where {$_.PowerState -eq "PoweredOn"} | `

    Select name, Host,$vmguest,NumCpu, MemoryMB,

    @{N="Mem.Usage.Average";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat mem.usage.average | Where {$businessDays -contains $_.Timestamp.DayOfWeek} | Measure-Object -Property Value -Average).Average}},

    @{N="Cpu.Usage.Average";E={(get-stat -entity $_ -Start ((Get-Date).AddDays(-7)) -Finish (Get-Date) -stat cpu.usage.average | Where {$businessDays -contains $_.Timestamp.DayOfWeek} | Measure-Object -Property Value -Average).Average}}

    So now the script above now gives me values for only monday to friday. Is this correct ?

    The next step now is that I want the values for 7am-6pm for Mon-Fri. Is there anyway of doing this. Is there a property in timestamp which has time values. Any assistance on this. Thanks



  • 8.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 06, 2010 10:11 PM


  • 9.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 07, 2010 12:41 AM

    Hi lucd, I must congrats you on your part 1 and 2 articles they are fantastic explanation on using get-stat.

    Just a quick question when I run the get-stat command without the -intervalmins and -maxsample parameneter, the default interval sample should then be 30 minutes, is this correct. When I run the below command:

    get-stat -entity (get-vm "name") -stat cpu.usage.average -start (get-date).adddays (-1) -finish (get-date)

    I get values which are 5 minutes apart, in other words 5 minutes samples. Is this how it should be, as I thought the default is 30 minutes. Or does it mean if I dont specify a start and end time. Just want to confirm this.



  • 10.  RE: Get-stat Script for VM Perfomance - Questions
    Best Answer

    Posted Jan 07, 2010 06:27 AM

    With the -Start parameter you specify that you want the values starting from 1 day back.

    That means you will get the values from Historical Interval 1 and there the interval period is 5 minutes.

    This is the HI1 interval in the schematic in Part 1.

    When you don't specify a -Start and -Finish parameter you will in fact get values from Historical Interval 4, which has a 1 day interval period.

    In a next blog post I will talk about the defaults in the Get-Stat cmdlet.



  • 11.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 08, 2010 10:17 PM

    thanks again lucd



  • 12.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 09, 2010 09:56 PM

    where would I begin if I wanted a similar script for the ESX host and cluster info in terms of mem, cpu, network, disk based on sample perood and umber of samples and such?



  • 13.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 09, 2010 10:20 PM

    read the links listed by Lucd in the previous post. They are excellent and a great start in terms of explaining get-stat and all its paramters like intervalmins.



  • 14.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 09, 2010 10:32 PM

    Would it be something similar to what I have put together below from other scripts i this fourm:

    Connect-VIServer virtualcenter

    Get-VMHost | Sort Name| ForEach-Object {

    Select ????????????????????????????????????????????????,

    @{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}},

    @{N="Mem.Usage.Average";E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date).AddHours(-72) -IntervalMins 5 -MaxSamples (12) | Measure-Object Value -Average).Average),2)}}

    except for the time intervals and such, I gues my main question is how to start off the script as I understand the body of the script gathering data for the counters time intervals and such but how do I connect to virtual center sort host by name and get the info I need.

    thanks in advacne for your assistance



  • 15.  RE: Get-stat Script for VM Perfomance - Questions

    Posted Jan 09, 2010 10:48 PM

    how about this will the below script connect to Virtual center sort by host name and provide nu,ber of virtual machines per host, cpu and memory averages over 30 minte interval for the prevous day

    Connect-VIServer "virtualcenter"

    Get-VMHost | where ( $_.PowerState -eq "poweredOn") | Sort Name | Foreach-object {

    Select Name, Location,

    @(N="NumVM":E={@(($_ | Get-VM)).Count}},

    @{N="Cpu.UsageMhz.Average";E={[Math]::Round((($_ | Get-Stat -Stat cpu.usagemhz.average -Start (Get-Date) -IntervalMins 30 -MaxSamples (60) | Measure-Object Value -Average).Average),2)}},

    @{N="Mem.Usage.Average";E={[Math]::Round((($_ | Get-Stat -Stat mem.usage.average -Start (Get-Date) -IntervalMins 30 -MaxSamples (60) | Measure-Object Value -Average).Average),2)}} | `

    Export-Csv "C:\VM-stats.csv" -NoTypeInformation -UseCulture