Pages

2/15/2012

VMWare io stats

VMWare 4 Statistics:  Get iops per vm on VMFS datastores


################################################################################
# GatherIOPS.ps1
#
# Thanks to:
# Curtis Salinas, # http://virtualcurtis.wordpress.com, October 2010 
################################################################################
# 
# Given a list of datastore names in file named store.txt & a vCenter Server FQDN,
# this script will return a table of IOPS by every virtual machine
# on those datastores over a 60 minute interval. This data
# is output to the PowerShell screen and to a csv file
#
#Run the following to create credentials file with password to username that is 
#defined by $username:
#read-host -prompt "enter password" -assecurestring | convertfrom-securestring | out-file cred.txt
#
#$username is a local account on each host
#
################################################################################


#param($datastores, $server, $numsamples)

$today = get-date
$day = $today.Day
$mth = $today.Month
$year = $today.Year
$hour = $today.Hour
$min = $today.Minute
$sec = $today.Second
$date = "$year-$mth-$day-$hour$min$sec"

$outfile = "IO-VM-$date.CSV"

$datastores = get-content .\stores.txt
$server = "vcs01.domain.com"
$numsamples = 180
 # number of samples = x time
 # 180 = 60min
 # 90 = 30min
 # 45 = 15min
 # 15 = 5min
 # 3 = 1min
 # 1 = 20sec (.33 min)

$username = "root"
$password = get-content cred.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password


# add VMware PS snapin
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
    Add-PSSnapin VMware.VimAutomation.Core
}

# connect vCenter server session
Connect-VIServer $server -NotDefault -WarningAction SilentlyContinue | Out-Null


# function to get iops for a vm on a particular host
function GetAvgStat($vmhost,$vm,$ds,$samples,$stat){

 # connect to host
 connect-viserver -server $vmhost -credential $credentials -NotDefault -WarningAction SilentlyContinue | Out-Null
 
 # identify device id for datastore
 $myDatastoreID = ((Get-Datastore $ds -server $vmhost) | Get-View).info.vmfs.extent[0].diskname
 
 # gather iops generated by vm
 $rawVMStats = get-stat -entity (get-vm $vm -server $vmhost) -stat $stat -maxSamples $samples
 $results = @()
 
 foreach ($stat in $rawVMStats) {
  if ($stat.instance.Equals($myDatastoreID)) {
   $results += $stat.Value
  }
 }
 
 $totalIOPS = 0
 foreach ($res in $results) {
  $totalIOPS += $res 
 }
 
 return [int] ($totalIOPS/$samples/20)
}

$IOPSReport = @()

$i = 0
$dstotal = $datastores.length + 1
foreach ($datastore in $datastores) {
  write-progress -id 1 -activity "Gathering IOPS Data" -status "% Datastores Complete" -percentcomplete (($i / $dstotal)*100)
  
  # Grab datastore and find VMs on that datastore
  $myDatastore = Get-Datastore -Name $datastore -server $server
  $myVMs = Get-VM -Datastore $myDatastore -server $server | Where {$_.PowerState -eq "PoweredOn"}
  
  if ($myVMs -eq $null) { continue } #go to next in foreach if no VM's listed
  
  # Gather current IO snapshot for each VM
  $dataArray = @()
  
  $j = 0
  $vmtotal = $myVMs.length + 1
  foreach ($vm in $myVMs) {
   write-progress -id 2 -parentID 1 -activity "Gathering IOPS Data" -status "% VMs in $datastore" -percentcomplete (($j / $vmtotal)*100)
   $data = ""| Select "Time", "VM", "Interval (minutes)", "Avg Write IOPS", "Avg Read IOPS", "Total IOPS"
   $data."Time" = $date
   $data."VM" = $vm.name
   $data."Interval (minutes)" = ($numsamples*20)/60
   $data."Avg Write IOPS" = GetAvgStat -vmhost $vm.host.name -vm $vm.name -ds $datastore -samples $numsamples -stat disk.numberWrite.summation
   $data."Avg Read IOPS" = GetAvgStat -vmhost $vm.host.name -vm $vm.name -ds $datastore -samples $numsamples -stat disk.numberRead.summation
   $data."Total IOPS" = $data."Avg Write IOPS" + $data."Avg Read IOPS"
   $dataArray += $data
   $j++
   }
  
  # Do something with the array of data
  $IOPSReport += $dataArray
  
  $i++

}

$IOPSReport
$IOPSReport | Export-CSV $outfile -NoType

No comments: