With the release of PowerCLI v4 the Get-Stat cmdlet has been much improved.
In October 2008, as a temporary solution for the problems with the Get-Stat cmdlet in older PowerCLI or VITK releases, I wrote the Get-Stat2 script.
The Get-Stat2 script allows lightly different parameters than the regular Get-Stat cmdlet.
For that reason I will make the Get-Stat2 script available again (the original document was removed some time ago).
But please only use this script when you couldn't get the result you needed with the current Get-Stat cmdlet!
An extract from the original document:
The script accepts the following parameters:
-Entity <VIObject> : the object for which the statiscal data will be collected. Only 1 object allowed ! -Start <DateTime> : start of the interval -Finish <DateTime> : end of the interval -Stat <string[]> : array of statistic identifiers -Instance <string> : some statistic identifiers allow instances. ("" : aggregate (default) / "*" : all instances / array of specific instances) -Interval <string> : desired sampling interval, allowed values: "RT","HI1","HI2","HI3","HI4" RT : real time HI : historical interval (results in these defaults 20, 300, 1800, 7200, 86400 seconds) -Maxsamples <int32> : only for "Real time" interval; number of intervals -QueryMetrics : return all available metrics for the Entity -QueryInstances : return all available instances for the Entity and Stat
Update 01/11/08
When you add a new resource to an entity, the statistical data for historical intervals will not be available immediately.
You will have to wait at least till the sampling period of the historical interval has expired.
The QueryPerf method will return sampling periods (date/time stamps) but there will be no statistical data (yet).
Example:
you add a vCPU to a virtual machine
data for HI2 for that CPU instance will only be available after +/- 30 minutes
The minimum wait time to see data for a new resource:
RT 20 secs
HI1 5 mins
HI2 30 mins
HI3 2 hours
HI4 1 day
To avoid errors from the Get-Stat2 function a test was added to verify if any data was returned by the QueryPerf method.
If there is no data, the Get-Stat2 function returns $null.
The attached script has been updated.
Update 15/02/09
The attached Get-Stat2.ps1 script has been updated.
The changes:
corrected MaxSamples parameter
corrected Cluster entity clastype
corrected missing Start and Finish parameters test
added -QueryMetrics option
added -QueryInstances option
With the addition of these last two parameters, the script can now be used to
query all the valid metrics for a specific entity
query all the valid instances for one or more metrics
Example -QueryMetrics
$esx = Get-VMHost <ESX-hostname> | Get-View ./Get-Stat2.ps1 -entity $esx -querymetrics |ft
produces an array of custom objects like this
Group Name Rollup Id Level Type Unit ----- ---- ------ -- ----- ---- ---- clusterServices cpufairness latest 105 1 absolute number clusterServices memfairness latest 106 1 absolute number cpu idle summation 112 3 delta millisecond cpu reservedCapacity average 110 2 absolute megaHertz cpu usage average 2 1 rate percent cpu usage maximum 4 4 rate percent cpu usage minimum 3 4 rate percent cpu usagemhz average 6 1 rate megaHertz cpu usagemhz maximum 8 4 rate megaHertz cpu usagemhz minimum 7 4 rate megaHertz cpu used summation 111 3 delta millisecond disk busResets summation 115 3 delta number disk commands summation 113 3 delta number disk commandsAborted summation 114 3 delta number disk deviceLatency average 156 2 absolute millisecond disk deviceReadLatency average 148 2 absolute millisecond disk deviceWriteLatency average 152 2 absolute millisecond disk kernelLatency average 157 2 absolute millisecond disk kernelReadLatency average 149 2 absolute millisecond disk kernelWriteLatency average 153 2 absolute millisecond disk numberRead summation 91 3 delta number disk numberWrite summation 92 3 delta number disk queueLatency average 159 2 absolute millisecond disk queueReadLatency average 151 2 absolute millisecond ...
Example QueryInstances
$esx = Get-VMHost <ESX-hostname> | Get-View ./Get-Stat2-v1.1.ps1 -entity $esx -stat "disk.deviceLatency.average","net.received.average" -queryinstances | ft
produces an array of custom objects like this
Stat Instance ---- -------- disk.deviceLatency.average vmhba0:0:0 disk.deviceLatency.average vmhba0:1:0 disk.deviceLatency.average vmhba32:0:0 disk.deviceLatency.average vmhba32:0:1 disk.deviceLatency.average vmhba32:1:0 disk.deviceLatency.average vmhba32:11:0 disk.deviceLatency.average vmhba32:2:0 disk.deviceLatency.average vmhba32:2:1 disk.deviceLatency.average vmhba32:6:0 disk.deviceLatency.average vmhba32:6:1 disk.deviceLatency.average vmhba32:7:0 disk.deviceLatency.average vmhba32:7:1 disk.deviceLatency.average vmhba32:8:0 net.received.average vmnic0 net.received.average vmnic1 net.received.average vmnic2 net.received.average vmnic3
Note that an instance can have a length zero for some metrics.
This ("") is the instance that returns the aggregated statistics value.
$esx = Get-VMHost <ESX-hostname> | Get-View ./Get-Stat2-v1.1.ps1 -entity $esx -stat "cpu.usage.average" -queryinstances | ft
returns
Stat Instance ---- -------- cpu.usage.average cpu.usage.average 0 cpu.usage.average 1
The first object in the array is the "aggregate" instance.
Update 02/03/09
The attached Get-Stat2.ps1 script has been updated.
The changes:
corrected issue with missing -Maxsamples parameter
corrected issue with incorrect instances returned for -QueryInstances
introduced default value ("RT") for -Interval parameter
Awesome script LucD, i needed to get the swapused value and you save my life (again !) !
luc,
I'm on powershell window. I source the script by . .\get-stat2.ps1
Then, I am trying to call get-stat2 function as follows and get an error. I ran this against an existing host, cluster, still the same error, any ideas?
PS C:\scripts> get-stat2 -Entity <cluster> -Stat "mem.usage.average"
-Entity parameters should be of type HostSystem, VirtualMachine, ClusterComputeResource, Datastore or ResourcePool
At C:\scripts\Get-Stat2.ps1:56 char:10
+ Throw <<<< "-Entity parameters should be of type HostSystem, VirtualMachine, ClusterComputeResource, Datastore or ResourcePool"
+ CategoryInfo : OperationStopped: (-Entity paramet...or ResourcePool:String) [], RuntimeException
+ FullyQualifiedErrorId : -Entity parameters should be of type HostSystem, VirtualMachine, ClusterComputeResource, Datastore or ResourcePool
Hi Mike,
The Get-Stat2 function requires a vSphere object, not a .Net object that is returned by a PowerCLI cmdlet.
You can do
$cluster = Get-Cluster -Name MyCluster
Get-Stat2 -Entity $cluster.ExtensionData -Stat "mem.usage.average"
Luc,
I am trying to connect to vCenter, and trying to collect statistics on clusters. I dont know the names of the clusters. Would you recommend creating an array of clusters, then go though each one or is there already a function that does that already?
Once connected to the vCenter, you can do
Get-Cluster
This should return all the clusters.
With my Get-Stat2 function you can only pass 1 entity per cll, with the Get-Stat cmdlet you can pass multiple entities in 1 call.
The latter could be faster in the end.
In that case you could use a Group-Object cmdlet to split out the results per cluster.
You can find an example in Dutch VMUG: The Statistics Reporting Session