forked from vmkdaily/vFlux-Stats-Kit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvFlux-IOPS.ps1
228 lines (187 loc) · 10.6 KB
/
vFlux-IOPS.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<#
.NOTES
=========================================================================================================
Filename: vFlux-IOPS.ps1
Version: 0.1c
Created: 12/21/2015
Updated: 27March2016
Requires: curl.exe for Windows (https://curl.haxx.se/download.html)
Requires: InfluxDB 0.9.4 or later. The latest 0.10.x is preferred.
Requires: Grafana 2.5 or later. The latest 2.6.x is preferred.
Prior Art: Based on the get-stat technique often illustrated by Luc Dekens
Prior Art: Uses MattHodge's InfluxDB write protocol syntax
Author: Mike Nisk (a.k.a. 'grasshopper')
Twitter: @vmkdaily
=========================================================================================================
.SYNOPSIS
Gathers VMware vSphere virtual machine disk stats and writes them to InfluxDB. This script understands
NFS and VMFS and gathers the appropriate stat type accordingly.
.DESCRIPTION
This PowerCLI script supports InfluxDB 0.9.4 and later (including the latest 0.10.x).
The InfluxDB write syntax is based on naf_perfmon_to_influxdb.ps1 by D'Haese Willem,
which itself is based on MattHodge's Graphite-PowerShell-Functions.
Please note that we use curl.exe for InfluxDB line protocol writes. This means you must
download curl.exe for Windows in order for Powershell to write to InfluxDB.
.PARAMETER vCenter
The name or IP address of the vCenter Server to connect to
.PARAMETER ShowStats
Optionally show some debug info on the writes to InfluxDB
.EXAMPLE
vFlux-IOPS.ps1 -vCenter <VC Name or IP>
.EXAMPLE
vFlux-IOPS.ps1 -vCenter <VC Name or IP> -ShowStats
#>
[cmdletbinding()]
param (
[Parameter(Mandatory = $True)]
[String]$vCenter,
[Parameter(Mandatory = $False)]
[switch]$ShowStats
)
Begin {
## User-Defined Influx Setup
$InfluxStruct = New-Object -TypeName PSObject -Property @{
CurlPath = 'C:\Windows\System32\curl.exe';
InfluxDbServer = '1.2.3.4'; #IP Address
InfluxDbPort = 8086;
InfluxDbName = 'iops';
InfluxDbUser = 'esx';
InfluxDbPassword = 'esx';
MetricsString = '' #emtpy string that we populate later.
}
## User-Defined Logging Preferences
$Logging = 'off'
$LogDir = 'C:\bin\logs'
$LogName = 'vFlux-IOPS.log'
## User-defined Datastores to Ignore
## The default is to report stats for all VMs, including those running on local and ISO datastores.
## To continue reporting on all such VMs, leave the following variables null ("")
$dasd = ""
$iso = ""
## Exclusive of the above option, to hide your local and ISO datastores from reporting, customize and uncomment the following.
## In this example, stats are not collected for VMs running on datastores with 'local' or 'utils' in the name.
#$dasd = "*local*"
#$iso = "*utils*"
}
#####################################
## No need to edit beyond this point
#####################################
Process {
## Start Logging
$dt = Get-Date -Format 'ddMMMyyyy_HHmm'
If (Test-Path -Path C:\temp) { $TempDir = 'C:\temp' } Else { $TempDir = Get-Path -Path $Env:TEMP }
If (!(Test-Path -Path $LogDir)) { $LogDir = $TempDir }
If ($Logging -eq 'On') { Start-Transcript -Append -Path $LogDir\$LogName }
## Get the PowerCLI snapin if needed
if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null) { Add-PSSnapin -Name VMware.VimAutomation.Core }
## Connect to vCenter
Connect-VIServer $vCenter | Out-Null
If (!$Global:DefaultVIServer -or ($Global:DefaultVIServer -and !$Global:DefaultVIServer.IsConnected)) { Throw "vCenter Connection Required!" }
Get-Datacenter | Out-Null # clear first slow API access
Write-Output -InputObject "Connected to $Global:DefaultVIServer"
## Start script execution timer
$vCenterStartDTM = (Get-Date)
#Region Datastore Enumeration
$ds = Get-Datastore
$dsLocal = $ds | Where-Object { $_.Type -eq "VMFS" -and $_.Name -like $dasd }
$dsVMFS = $ds | Where-Object { $_.Type -eq "VMFS" -and $_.Name -notlike $dasd -and $_.Name -notlike $iso }
$dsNFS = $ds | Where-Object { $_.Type -eq "NFS" -and $_.Name -notlike $iso }
$dsISO = $ds | Where-Object { $_.Name -like $iso }
## Running VMs by storage type
$BlockVMs = $dsVMFS | Get-VM | Where-Object { $_.PowerState -eq "PoweredOn" } | Sort-Object -Property $_.VMHost
$NfsVMs = $dsNFS | Get-VM | Where-Object { $_.PowerState -eq "PoweredOn" } | Sort-Object -Property $_.VMHost
## Datastore and VM counts by type
$localDsCount = ($dsLocal).Count
$VmfsDsCount = ($dsVMFS).Count
$NfsDsCount = ($dsNFS).Count
$VmfsVMsCount = ($BlockVMs).Count
$NfsVMsCount = ($NfsVMs).Count
If($ShowStats){
## debug console output
Write-Output -InputObject "`n$DefaultVIServer Overview"
Write-Output -InputObject "Local Datastores:$localDsCount"
Write-Output -InputObject "VMFS Datastores: $VmfsDsCount"
Write-Output -InputObject "NFS Datastores: $NfsDsCount"
Write-Output -InputObject "Block VMs: $VmfsVMsCount"
Write-Output -InputObject "NFS VMs: $NfsVMsCount"
Write-Output -InputObject "`nBeginning stat collection."
}
## VMFS Block VM Section
If($BlockVMs) {
## Desired vSphere metrics for block-based virtual machine performance reporting
$BlockStatTypes = 'disk.numberwrite.summation','disk.numberread.summation','disk.maxTotalLatency.latest'
## Iterate through VMFS Block VM list
foreach ($vm in $BlockVMs) {
## Gather desired stats
$stats = Get-Stat -Entity $vm -Stat $BlockStatTypes -Realtime -MaxSamples 1
foreach ($stat in $stats) {
## Create and populate variables for the purpose of writing to InfluxDB Line Protocol
$measurement = $stat.MetricId
$value = $stat.Value
$name = $vm.Name
$type = 'VM'
$DiskType = 'Block'
If($stat.Instance) {$instance = $stat.Instance} Else {$instance -eq $null}
if($stat.Unit) {$unit = $stat.Unit} Else {$unit -eq $null}
$vc = ($global:DefaultVIServer).Name
$cluster = $vm.VMHost.Parent
[int64]$timestamp = (([datetime]::UtcNow)-(Get-Date -Date "1/1/1970")).TotalMilliseconds * 1000000 #nanoseconds since Unix epoch
## Write to InfluxDB for this VMFS Block VM iteration
$InfluxStruct.MetricsString = ''
$InfluxStruct.MetricsString += "$measurement,host=$name,type=$type,vc=$vc,cluster=$cluster,disktype=$DiskType,instance=$instance,unit=$Unit value=$value $timestamp"
$InfluxStruct.MetricsString += "`n"
$CurlCommand = "$($InfluxStruct.CurlPath) -u $($InfluxStruct.InfluxDbUser):$($InfluxStruct.InfluxDbPassword) -i -XPOST `"http://$($InfluxStruct.InfluxDbServer):$($InfluxStruct.InfluxDbPort)/write?db=$($InfluxStruct.InfluxDbName)`" --data-binary `'$($InfluxStruct.MetricsString)`'"
Invoke-Expression -Command $CurlCommand 2>&1
## debug output
If($ShowStats){
Write-Output -InputObject "Measurement: $measurement"
Write-Output -InputObject "Value: $value"
Write-Output -InputObject "Name: $Name"
Write-Output -InputObject "Unix Timestamp: $timestamp`n"
Write-Output -InputObject ''
}
}
}
}
## NFS VM Section
If($NfsVMs) {
## Desired vSphere metrics for NFS-based virtual machine performance reporting
$NfsStatTypes = 'virtualdisk.numberwriteaveraged.average','virtualdisk.numberreadaveraged.average','virtualDisk.readLatencyUS.latest','virtualDisk.writeLatencyUS.latest'
## Iterate through NFS VM list
foreach ($vm in $NfsVMs) {
## Gather desired stats
$stats = Get-Stat -Entity $vm -Stat $NfsStatTypes -Realtime -MaxSamples 1
foreach ($stat in $stats) {
## Create and populate variables for the purpose of writing to InfluxDB Line Protocol
$measurement = $stat.MetricId
$value = $stat.Value
$name = $vm.Name
$type = 'VM'
$DiskType = 'NFS'
If($stat.Instance) {$instance = $stat.Instance} Else {$instance -eq $null}
if($stat.Unit) {$unit = $stat.Unit} Else {$unit -eq $null}
$vc = ($global:DefaultVIServer).Name
$cluster = $vm.VMHost.Parent
[int64]$timestamp = (([datetime]::UtcNow)-(Get-Date -Date "1/1/1970")).TotalMilliseconds * 1000000 #nanoseconds since Unix epoch
## Write to InfluxDB for this NFS VM iteration
$InfluxStruct.MetricsString = ''
$InfluxStruct.MetricsString += "$measurement,host=$name,type=$type,vc=$vc,cluster=$cluster,disktype=$DiskType,instance=$instance,unit=$Unit value=$value $timestamp"
$InfluxStruct.MetricsString += "`n"
$CurlCommand = "$($InfluxStruct.CurlPath) -u $($InfluxStruct.InfluxDbUser):$($InfluxStruct.InfluxDbPassword) -i -XPOST `"http://$($InfluxStruct.InfluxDbServer):$($InfluxStruct.InfluxDbPort)/write?db=$($InfluxStruct.InfluxDbName)`" --data-binary `'$($InfluxStruct.MetricsString)`'"
Invoke-Expression -Command $CurlCommand 2>&1
## debug console output
If($ShowStats){
Write-Output -InputObject "Measurement: $measurement"
Write-Output -InputObject "Value: $value"
Write-Output -InputObject "Name: $Name"
Write-Output -InputObject "Unix Timestamp: $timestamp`n"
Write-Output -InputObject ''
}
}
}
}
Disconnect-VIServer '*' -Confirm:$false
Write-Output -InputObject "Script complete.`n"
If ($Logging -eq 'On') { Stop-Transcript }
}