Advertisement
Guest User

Powershell GatherIOPS

a guest
Jan 26th, 2015
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ################################################################################
  2. # GatherIOPS v1.2
  3. # by Curtis Salinas
  4. # http://virtualcurtis.wordpress.com
  5. # October 2010
  6. ################################################################################
  7. #
  8. # Given an array of datastore names, a vCenter Server FQDN,
  9. # and a number of samples, this script will return a table
  10. # of the read and write IOPS done by every virtual machine
  11. # on those datastores over the sample interval. This data
  12. # is output to the PowerShell screen and to a csv file
  13. # called "IOPSReport.csv".
  14. #
  15. # Example:
  16. # GatherIOPS.ps1 -server myvcenterserver.mydns.com -datastores @("DS01") -numsamples 90
  17. #
  18. # Returns:
  19. # VM     Interval (minutes)         Avg Write IOPS            Avg Read IOPS
  20. # --     ------------------         --------------            -------------
  21. # VM01                   30       9.97194444444444            0.466111111111111
  22. # VM01                   30       3.03222222222222            0.483888888888889
  23. # VM01                   30                 8.7625            0.104444444444444
  24. # VM01                   30       6.73638888888889            0.211111111111111
  25. # VM01                   30       15.5652777777778            0.303055555555556
  26. #
  27. ################################################################################
  28.  
  29.  
  30. param($datastores, $server, $numsamples)
  31.  
  32. $datastores = "server1", "server2", "server3"
  33. $Server = "vCenter"
  34. $numsamples = 90
  35.  
  36. # $username = read-host -prompt "Please enter local user account for host access"
  37. # read-host -prompt "Please enter password for host account" -assecurestring | convertfrom-securestring | out-file cred.txt
  38. # $password = get-content cred.txt | convertto-securestring
  39. # $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password
  40. $username = "root"
  41. $password = get-content c:\users\MyUser\desktop\iops\cred.txt | convertto-securestring
  42. $credentials = new-object system.management.automation.pscredential $username, $password
  43.  
  44. # add VMware PS snapin
  45. if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
  46.     Add-PSSnapin VMware.VimAutomation.Core
  47. }
  48.  
  49. # connect vCenter server session
  50. Connect-VIServer $server -NotDefault -WarningAction SilentlyContinue | Out-Null
  51.  
  52.  
  53. # function to get average iops for a vm on a particular host
  54. function GetAvgStat($vmhost,$vm,$ds,$samples,$stat){
  55.     # number of samples = x time
  56.     # 180 = 60min
  57.     # 90 = 30min
  58.     # 45 = 15min
  59.     # 15 = 5min
  60.     # 3 = 1min
  61.     # 1 = 20sec (.33 min)
  62.  
  63.     # connect to host
  64.     connect-viserver -server $vmhost -credential $credentials -NotDefault -WarningAction SilentlyContinue | Out-Null
  65.    
  66.     # identify device id for datastore
  67.     $myDatastoreID = ((Get-Datastore $ds -server $vmhost) | Get-View).info.vmfs.extent[0].diskname
  68.    
  69.     # gather iops generated by vm
  70.     $rawVMStats = get-stat -entity  (get-vm $vm -server $vmhost) -stat $stat -maxSamples $samples
  71.     $results = @()
  72.    
  73.     foreach ($stat in $rawVMStats) {
  74.         if ($stat.instance.Equals($myDatastoreID)) {
  75.             $results += $stat.Value
  76.         }
  77.     }
  78.    
  79.     $totalIOPS = 0
  80.     foreach ($res in $results) {
  81.         $totalIOPS += $res 
  82.     }
  83.    
  84.     return [int] ($totalIOPS/$samples/20)
  85. }
  86.  
  87. $IOPSReport = @()
  88.  
  89. foreach ($datastore in $datastores) {
  90.  
  91.   # Grab datastore and find VMs on that datastore
  92.   $myDatastore = Get-Datastore -Name $datastore -server $server
  93.   $myVMs = Get-VM -Datastore $myDatastore -server $server | Where {$_.PowerState -eq "PoweredOn"}
  94.  
  95.   # Gather current IO snapshot for each VM
  96.   $dataArray = @()
  97.   foreach ($vm in $myVMs) {
  98.   $data = "" | Select "VM", "Time", "Datastore", "Interval (minutes)", "Avg Write IOPS", "Avg Read IOPS"
  99.   $data."VM" = $vm.name
  100.   $data."Time" = Get-Date
  101.   $data."Datastore" = $myDatastore.name
  102.   $data."Interval (minutes)" = ($numsamples*20)/60
  103.   $data."Avg Write IOPS" = GetAvgStat -vmhost $vm.host.name -vm $vm.name -ds $datastore -samples $numsamples -stat disk.numberWrite.summation
  104.   $data."Avg Read IOPS" = GetAvgStat -vmhost $vm.host.name -vm $vm.name -ds $datastore -samples $numsamples -stat disk.numberRead.summation
  105.   $dataArray += $data  
  106.   }
  107.  
  108.   # Do something with the array of data
  109.   $IOPSReport += $dataArray
  110.  
  111. }
  112.  
  113. # set variable datetime to use for filename
  114.  
  115. $datetime = get-date -uformat "%m%d%Y-%H.%M"
  116.  
  117. # set variable IOPFilename that will combine date/time stamp to filename
  118.  
  119. #$IOPFilename = "IOPSReport"+$datetime+".csv"
  120. #$IOPFilename = "IOPSReport.csv"
  121.  
  122. $IOPSReport
  123. #$IOPSReport | Export-CSV -append $IOPFilename -NoType
  124. $IOPSReport | Export-CSV "c:\users\MyUser\desktop\iops\IOPSReport.csv" -NoType
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement