Skip to content

Commit 858949c

Browse files
committed
Update VMPerf-To-Graphite.ps1
1 parent f7a0073 commit 858949c

1 file changed

Lines changed: 98 additions & 50 deletions

File tree

VMPerf-To-Graphite.ps1

Lines changed: 98 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
VMPerf-To-Graphite.ps1 -Verbose
1111
Use default values from within this script and display the status output on the screen.
1212
.EXAMPLE
13+
VMPerf-To-Graphite.ps1 -Verbose -Server myvcenter.vienna.acme.com -User ACME\StatsReader -Password mypass -Graphiteserver graphite1.it.acme.com -Iterations 1 -FromLastPoll Vienna_Poll.xml
14+
Run the cmdlet just once. Write the date and time of the Poll to Vienna_Poll.xml. The next time the script runs, it will read the file and gather the metrics from VCenter starting at the last poll.
15+
.EXAMPLE
16+
VMPerf-To-Graphite.ps1 -Verbose -Server myvcenter.vienna.acme.com -User ACME\StatsReader -Password mypass -Graphiteserver graphite1.it.acme.com,graphdev.it.acme.com:62033 -Iterations 1 -FromLastPoll Vienna_Poll.xml
17+
Same as above but send the metrics to two servers, graphite1.it.acme.com at (default) port 2003 and graphdev.it.acme.com on port 62033.
18+
.EXAMPLE
1319
VMPerf-To-Graphite.ps1 -Verbose -Server myvcenter.vienna.acme.com -User ACME\StatsReader -Password mypass -Sleepseconds 300 -Graphiteserver graphite1.it.acme.com -Group Vienna
1420
Read the counters from the VCenter server myvcenter.vienna.acme.com, send the metrics to graphite1.it.acme.com with a metrics path of "vmperf.Vienna." and then wait 5 minutes before the next iteration.
1521
.EXAMPLE
@@ -18,15 +24,13 @@ Read the counters from Cluster TESTDEV in the VCenter server myvcenter.vienna.ac
1824
.EXAMPLE
1925
VMPerf-To-Graphite.ps1 -Verbose -Iterations 1 -WhatIf | Out-GridView
2026
Run the cmdlet just once, but do not send the metrics to Graphite, instead open a window and display the results.
21-
.EXAMPLE
22-
VMPerf-To-Graphite.ps1 -Verbose -Server myvcenter.vienna.acme.com -User ACME\StatsReader -Password mypass -Graphiteserver graphite1.it.acme.com -Iterations 1 -FromLastPoll Vienna_Poll.xml
23-
Run the cmdlet just once. Write the date and time of the Poll to Vienna_Poll.xml. The next time the script runs, it will read the file and gather the metrics from VCenter starting at the last poll.
2427
.NOTES
2528
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
2629
It is free-of-charge and it comes without any warranty, to the extent permitted by applicable law.
2730
Matthias Rettl, 2016
28-
Script Version 1.4.0 (2016-May-20)
31+
Script Version 1.5.0 (2016-Jun-04)
2932
.LINK
33+
https://github.com/mothe-at/VMPerf-To-Graphite-PowerShell-Script
3034
http://rettl.org/scripts/
3135
http://creativecommons.org/licenses/by-nc-sa/4.0/
3236
#>
@@ -44,16 +48,14 @@ param(
4448
[string[]]$Datacenter = "*",
4549
# Specifies the VMWare Clusters you want to receive data from. Default is to read all Clusters managed by VCenter server or, if -Datacenter is specified, all Clusters in this Datacenter.
4650
[string[]]$Cluster = "*",
47-
# Specifies the IP address or the DNS name of the Graphite server to which you want to connect.
48-
[string]$Graphiteserver = "your_default_grafana_server",
51+
# Specifies one or more (separated by comma) IP addresses or the DNS names of the Graphite servers to which you want to connect.
52+
# You can also add the Portnumber to each Server like "grafana.acme.com:2003"
53+
[string[]]$Graphiteserver = "your_default_grafana_server",
4954
# Specifies the port on the Graphite server you want to use for the connection. Defaults to 2003.
55+
# You can also add the portnumber to the servers hostname or IP address in the -Graphiteserver parameter.
5056
[ValidateRange(1024,65536)][int]$Graphiteserverport = 2003,
5157
# Specifies the Group, an additional prefix for the metrics path in Graphite. The metrics path will be "vmperf.<Group>."
5258
[string]$Group = "Default",
53-
# Specifies the number of seconds to wait between iterations. The counter starts after the last statistics have been sent to Graphite.
54-
# Note that VCenter is collecting its performance statistics every 20 seconds and saves an average of the collected counters. It makes no sense to specify a value below 20 seconds here. The script reads the so called "Realtime" counters from VCenter which will be kept there for one hour. So do not use anything above 3600 seconds.
55-
# The script requests all statistics data from VCenter server since the last time they were requested, regardless of how long the Sleepseconds parameter was set. You wont miss any data.
56-
[ValidateRange(0,3600)][int]$Sleepseconds = 60,
5759
# Specifies the number of iterations. 0 = indefinitely.
5860
[ValidateRange(0,65536)][int]$Iterations = 0,
5961
# Optional path and name of an .xml file where the date and time of the last poll will be saved.
@@ -62,6 +64,10 @@ param(
6264
# This is useful if you want to schedule the script externally (with Task Scheduler, for instance) and you want to use the "-Iterations 1" parameter.
6365
# But be careful, VCenter stores the Real-Time statistics just for a limited number of time (1 day per default).
6466
[string]$FromLastPoll = "",
67+
# Specifies the number of seconds to wait between iterations. The counter starts after the last statistics have been sent to Graphite.
68+
# Note that VCenter is collecting its performance statistics every 20 seconds and saves an average of the collected counters. It makes no sense to specify a value below 20 seconds here. The script reads the so called "Realtime" counters from VCenter which will be kept there for one hour. So do not use anything above 3600 seconds.
69+
# The script requests all statistics data from VCenter server since the last time they were requested, regardless of how long the Sleepseconds parameter was set. You wont miss any data.
70+
[ValidateRange(0,3600)][int]$Sleepseconds = 60,
6571
# Indicate that the cmdlet will process but will NOT send any metrics to Graphite, instead display a list of metrics that would be sent to Graphite.
6672
[Switch]$Whatif,
6773
# Set the Log-Level for writing events to the Windows Aplication log. Valid values are Error, Warning, Information, and None. The default value is Warning.
@@ -85,10 +91,10 @@ if (($ell -ne 0) -AND ($sev -ge $ell)) {
8591
$myscriptfull = $MyInvocation.ScriptName
8692
$l = [Environment]::NewLine
8793

88-
New-EventLog –LogName Application –Source $myscriptname -ErrorAction SilentlyContinue
94+
New-EventLog –LogName Application –Source $myscriptname -ErrorAction SilentlyContinue
8995

90-
$msg = $message + $l+$l + "Called by:" + $l + "$myscriptfull -Server $server -User $user -Password [**HIDDEN**] -Protocol $protocol -Datacenter $datacenter -Cluster $cluster -Group $Group -Graphiteserver $Graphiteserver -Graphiteserverport $Graphiteserverport -Sleepseconds $Sleepseconds -Iterations $Iterations -WhatIf:`$$Whatif -EventLogLevel $EventLogLevel"
91-
Write-EventLog –LogName Application –Source $myscriptname –EntryType $severity –EventID $id –Message $msg -Category 0
96+
$msg = $message + $l+$l + "Called by:" + $l + "$myscriptfull -Server $server -User $user -Password [**HIDDEN**] -Protocol $protocol -Datacenter $datacenter -Cluster $cluster -Group $Group -Graphiteserver $Graphiteserver -Graphiteserverport $Graphiteserverport -Sleepseconds $Sleepseconds -Iterations $Iterations -FromLastPoll $FromLastPoll -WhatIf:`$$Whatif -EventLogLevel $EventLogLevel"
97+
Write-EventLog –LogName Application –Source $myscriptname –EntryType $severity –EventID $id –Message $msg -Category 0
9298

9399
}
94100

@@ -122,38 +128,71 @@ try {
122128
# ---------------------------------------
123129
function sendtographite ($metrics)
124130
{
125-
do {
126-
$iserr = $false
127-
Try
128-
{
129-
$socket = new-object system.net.sockets.tcpclient
130-
131-
$socket.connect($graphiteserver, $graphiteserverport)
132-
133-
$stream = $socket.getstream()
134-
$writer = new-object system.io.streamwriter($stream)
135-
136-
foreach($i in 0..($metrics.count-1)){
137-
$writer.writeline($metrics[$i])
138-
}
139-
140-
$writer.flush()
141-
$writer.close()
142-
$stream.close()
143-
$socket.close()
144-
}
145-
Catch
146-
{
147-
$IsErr = $true
148-
$ErrorMessage = $_.Exception.Message
149-
$FailedItem = $_.Exception.ItemName
150-
$msg = "Connection to $graphiteserver on port $graphiteserverport failed with $ErrorMessage! Will retry in 10 seconds"
151-
Write-Warning "$(Get-Date -format G) $msg"
152-
Write-To-Windows-EventLog "Warning" 2007 $msg
153-
Start-Sleep -s 10
154-
}
131+
$maxretries = 10 # Maximum number of retries to connect to a Graphite server. Only applicable if multiple servers are specified. With only 1 server it will try forever.
132+
$aservers = $graphiteserver -split ' '
155133

156-
} while ($IsErr)
134+
foreach($s in 0..($aservers.count-1)){
135+
136+
$cserver = $aservers[$s]
137+
if ($cserver.contains(":"))
138+
{
139+
$atmp = $cserver -split ':'
140+
$cserver = $atmp[0]
141+
$cport = $atmp[1]
142+
}
143+
else
144+
{
145+
$cport = $graphiteserverport
146+
}
147+
148+
$msg = "Sending $scount metrics for $vcount VMs of iteration # $iteration to Graphite server $cserver`:$cport"
149+
Write-Verbose "$(Get-Date -format G) $msg"
150+
Write-To-Windows-EventLog "Information" 1005 $msg
151+
$trycount = 1
152+
do {
153+
$iserr = $false
154+
Try
155+
{
156+
$socket = new-object system.net.sockets.tcpclient
157+
158+
$socket.connect($cserver, $cport)
159+
160+
$stream = $socket.getstream()
161+
$writer = new-object system.io.streamwriter($stream)
162+
163+
foreach($i in 0..($metrics.count-1)){
164+
$writer.writeline($metrics[$i])
165+
}
166+
167+
$writer.flush()
168+
$writer.close()
169+
$stream.close()
170+
$socket.close()
171+
}
172+
Catch
173+
{
174+
$IsErr = $true
175+
$ErrorMessage = $_.Exception.Message
176+
$FailedItem = $_.Exception.ItemName
177+
$msg = "Connection to $cserver`:$cport failed with $ErrorMessage! Will retry in 10 seconds"
178+
Write-Warning "$(Get-Date -format G) $msg"
179+
Write-To-Windows-EventLog "Warning" 2007 $msg
180+
Start-Sleep -s 10
181+
182+
if (($trycount -ge $maxretries) -and ($aservers.count -gt 1)) {
183+
# Giving up this Server, it does not work, lets try the other one(s)
184+
$msg = "Giving up connection to $cserver`:$cport after $trycount failed attempts"
185+
Write-Error "$(Get-Date -format G) $msg"
186+
Write-To-Windows-EventLog "Error" 2007 $msg
187+
$IsErr = $false
188+
}
189+
190+
$trycount++
191+
192+
}
193+
194+
} while ($IsErr)
195+
}
157196

158197
}
159198

@@ -188,6 +227,7 @@ return $vcc
188227
}
189228

190229
# -----------------------------------------------------------------------------------------------------------------------------------------
230+
# -- MAIN PROCEDURE -----------------------------------------------------------------------------------------------------------------------
191231
# -----------------------------------------------------------------------------------------------------------------------------------------
192232

193233
if (!$Group.EndsWith(".")) {$Group = $Group+"."}
@@ -232,9 +272,20 @@ if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue)
232272

233273
}
234274

235-
# Try to resolve VCenter and Graphite Hostname and Quit script if unsuccessful.
275+
# Try to resolve VCenter and Graphite Hostname(s) and Quit script if unsuccessful.
236276
checkiporhostname "VCenter server" $Server True
237-
checkiporhostname "Graphite" $Graphiteserver True
277+
278+
$aservers = $graphiteserver -split ' '
279+
foreach($s in 0..($aservers.count-1)){
280+
281+
$cserver = $aservers[$s]
282+
if ($cserver.contains(":"))
283+
{
284+
$atmp = $cserver -split ':'
285+
$cserver = $atmp[0]
286+
}
287+
checkiporhostname "Graphite" $cserver True
288+
}
238289

239290
# First attempt to connect to the VCenter server. Stop immediately if the connection fails.
240291
$vcc = connectviserver True
@@ -265,6 +316,7 @@ $timespan = New-TimeSpan -Seconds $sleepseconds
265316
$starttime = (Get-Date)-$timespan
266317

267318
if ($FromLastPoll -ne "") {
319+
$FromLastPoll = [System.IO.Path]::GetFullPath($FromLastPoll)
268320
if (Test-Path $FromLastPoll) {
269321
Write-Verbose "Reading last polling time from $FromLastPoll"
270322
$starttime = Import-Clixml $FromLastPoll
@@ -402,10 +454,6 @@ if ($FromLastPoll -ne "") {
402454

403455
$scount = $results.count
404456

405-
$msg = "Sending $scount metrics for $vcount VMs of iteration # $iteration to Graphite server $graphiteserver"
406-
Write-Verbose "$(Get-Date -format G) $msg"
407-
Write-To-Windows-EventLog "Information" 1005 $msg
408-
409457
if (!$Whatif)
410458
{
411459
# Sends the metrics to Graphite.

0 commit comments

Comments
 (0)