VMware Cloud Community
vlife201110141
Enthusiast
Enthusiast
Jump to solution

Powercli way of esxtop utility

Hello Luc,

I borrowed the following script from your web site but couldn't make it run. What am I missing ? Thank you,

PowerCLI Version

----------------

VMware vSphere PowerCLI 4.1 U1 build 332441

Connect-VIServer -Server 'esxhost' -User 'root' -Password 'password'
$esxName = "esxhost"
$tgtAdapters = "vmhba1", "vmhba2"
$metrics = "disk.deviceLatency.average", "disk.kernelLatency.average", "disk.queueLatency.average", "disk.commandsAborted.summation", "disk.busResets.summation"
$esxImpl = Get-VMHost -Name $esxName
$esx = $esxImpl | Get-View
$endDate = Get-Date
$startDate = $endDate.AddHours(-1) 
# # Find active instances per selected adapter 
$instances = @{} 
$instancesFilter = @() 
foreach($adapter in $tgtAdapters){ 
$adapterInstances = @() 
$adapterKey = ($esx.Config.StorageDevice.HostBusAdapter | where {$adapter -eq $_.Device}).Key 
foreach($lun in $esx.Config.StorageDevice.MultipathInfo.Lun){ 
foreach($path in $lun.Path){ 
if(($path.Adapter -eq $adapterKey) -and ($path.PathState -eq "active")){ 
foreach($scsiLun in $esx.Config.StorageDevice.ScsiLun){ 
if($scsiLun.Key -eq $lun.lun){ 
$adapterInstances += $scsiLun.CanonicalName 
$instancesFilter += $scsiLun.CanonicalName 





$instances[$adapter] = $adapterInstances

# Get statistics for instances 
$stats = Get-Stat -Entity $esxImpl -Stat $metrics -Start $startDate -Finish $endDate | ` 
where {$instancesFilter -contains $_.Instance} 
# Create the reporting array 
$hbaTab = @{} 
$stats | %{ 
# Determine HBA-name based on instance 
$instance = $_.Instance 
$instances.GetEnumerator() | %{ 
if($_.Value -contains $instance){$currentHba = $_.Name} 

# Gethash table for adapter 
if($hbaTab.ContainsKey($currentHba)){ 
$timeTab = $hbaTab[$currentHba] 

else{ 
$timeTab = @{} 

# Check if there is a row for this timestamp 
if($timeTab.ContainsKey($_.Timestamp)){ 
$row = $timeTab[$_.Timestamp] 

else{ 
$row = New-Object PSObject -Property @{ 
Time = $_.Timestamp 
Interval = 0 
GAVG = 0 
DAVG = 0 
KAVG = 0 
QUED = 0 
ABRT = 0 
RESET = 0 


$row.Interval = $_.IntervalSecs 
$value = $_.Value 
switch($_.MetricId){
"disk.deviceLatency.average"{$row.DAVG += $Value} 
"disk.kernelLatency.average"{$row.KAVG += $Value} 
"disk.queueLatency.average"{$row.QUED += $Value} 
"disk.commandsAborted.summation"{$row.ABRT += $Value} 
"disk.busResets.summation"{$row.RESET += $Value} 

$timeTab[$_.Timestamp] = $row
$hbaTab[$currentHba] = $timeTab

$hbaTab.GetEnumerator() | %{ 
$_.Value.GetEnumerator() | %{ 
$_.Value.GAVG = $_.Value.DAVG + $_.Value.KAVG 
$_.Value.ABRT /= $_.Value.Interval 
$_.Value.RESET /= $_.Value.Interval 

$_.Value.Values | Sort-Object -Property Time | ` 
Export-Csv ("C:\IO-stat-" + $esxName.Split(".")[0] + "-" + $_.Name + ".csv") -NoTypeInformation -UseCulture
}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I suspect you have a blank after the back-tick in this line. Can you check ?

$stats = Get-Stat -Entity $esxImpl -Stat $metrics -Start $startDate -Finish $endDate | `

The back-tick should be the last character on the line.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Any error messages ?

Also note that the statistics level has to be at least 2 to be able to use these metrics.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vlife201110141
Enthusiast
Enthusiast
Jump to solution

I changed statistics level from 1 to 2 a couple of minutes ago.

The term ' ' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the sp

elling of the name, or if a path was included, verify that the path is correct and try again.

At C:\esxtop.ps1:30 char:90

+ $stats = Get-Stat -Entity $esxImpl -Stat $metrics -Start $startDate -Finish $endDate | ` <<<<

+ CategoryInfo : ObjectNotFound: ( :String) [], CommandNotFoundException

+ FullyQualifiedErrorId : CommandNotFoundException

Exception calling "ContainsKey" with "1" argument(s): "Anahtar null olamaz.

Parametre adı: key"

At C:\esxtop.ps1:41 char:23

+ if($hbaTab.ContainsKey <<<< ($currentHba)){

+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException

+ FullyQualifiedErrorId : DotNetMethodException

You cannot call a method on a null-valued expression.

At C:\esxtop.ps1:48 char:24

+ if($timeTab.ContainsKey <<<< ($_.Timestamp)){

+ CategoryInfo : InvalidOperation: (ContainsKey:String) [], RuntimeException

+ FullyQualifiedErrorId : InvokeMethodOnNull

Property 'Interval' cannot be found on this object; make sure it exists and is settable.

At C:\esxtop.ps1:63 char:6

+ $row. <<<< Interval = $_.IntervalSecs

+ CategoryInfo : InvalidOperation: (Interval:String) [], RuntimeException

+ FullyQualifiedErrorId : PropertyNotFound

Cannot index into a null array.

At C:\esxtop.ps1:72 char:10

+ $timeTab[ <<<< $_.Timestamp] = $row

+ CategoryInfo : InvalidOperation: (:) [], RuntimeException

+ FullyQualifiedErrorId : NullArray

Index operation failed; the array index evaluated to null.

At C:\esxtop.ps1:73 char:9

+ $hbaTab[ <<<< $currentHba] = $timeTab

+ CategoryInfo : InvalidOperation: (System.Collections.Hashtable:Hashtable) [], RuntimeException

+ FullyQualifiedErrorId : NullArrayIndex

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect you have a blank after the back-tick in this line. Can you check ?

$stats = Get-Stat -Entity $esxImpl -Stat $metrics -Start $startDate -Finish $endDate | `

The back-tick should be the last character on the line.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vlife201110141
Enthusiast
Enthusiast
Jump to solution

thanks Luc.That was the issue.

0 Kudos