# encoding: ascii
# api: powershell
# title: ESX Lun Latencies
# description: Given an ESX host, produce a report of read and write latencies for all attached LUNs.
# version: 0.1
# type: function
# author: Carter Shanklin
# license: CC0
# function: Get-VMHostLunLatency
# x-poshcode-id: 2515
# x-archived: 2017-03-25T18:32:20
# x-published: 2011-02-22T05:53:00
#
#
function Get-VMHostLunLatency {
  param([parameter(Mandatory=$true, ValueFromPipeline=$true)] $VMHost)

  process {
    $luns = Get-ScsiLun -VmHost $VMHost
    $DiskStats = $VMHost |
      Get-Stat -stat disk.totalreadlatency.average,disk.totalwritelatency.average -maxsamples 1 -realtime 
    foreach ($lun in $luns) {
      $Stats = $DiskStats  |
        Where { $_.Instance -eq $Lun.CanonicalName }
      if ($stats.length -ne $null) {
        $obj = New-Object PSObject
        $obj | Add-Member -MemberType NoteProperty -Name VMHost -Value $VMHost.Name
        $obj | Add-Member -MemberType NoteProperty -Name Lun -Value $lun.CanonicalName
        $obj | Add-Member -MemberType NoteProperty -Name ReadLatency -Value ($stats[0].Value)
        $obj | Add-Member -MemberType NoteProperty -Name WriteLatency -Value ($stats[1].Value)
        Write-Output $obj
      }
    }
  }
}
