This repository was archived by the owner on May 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathGraphite-PowerShell.ps1
1001 lines (788 loc) · 33.4 KB
/
Graphite-PowerShell.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Determine The Path Of The XML Config File
$configPath = [string](Split-Path -Parent $MyInvocation.MyCommand.Definition) + '\StatsToGraphiteConfig.xml'
Function Start-StatsToGraphite
{
<#
.Synopsis
Starts the loop which sends Windows Performance Counters to Graphite.
.Description
Starts the loop which sends Windows Performance Counters to Graphite. Configuration is all done from the StatsToGraphiteConfig.xml file.
.Parameter Verbose
Provides Verbose output which is useful for troubleshooting
.Parameter TestMode
Metrics that would be sent to Graphite is shown, without sending the metric on to Graphite.
.Parameter ExcludePerfCounters
Excludes Performance counters defined in XML config
.Parameter SqlMetrics
Includes SQL Metrics defined in XML config
.Example
PS> Start-StatsToGraphite
Will start the endless loop to send stats to Graphite
.Example
PS> Start-StatsToGraphite -Verbose
Will start the endless loop to send stats to Graphite and provide Verbose output.
.Example
PS> Start-StatsToGraphite -SqlMetrics
Sends perf counters & sql metrics
.Example
PS> Start-StatsToGraphite -SqlMetrics -ExcludePerfCounters
Sends only sql metrics
.Notes
NAME: Start-StatsToGraphite
AUTHOR: Matthew Hodgkins
WEBSITE: http://www.hodgkins.net.au
#>
[CmdletBinding()]
Param
(
# Enable Test Mode. Metrics will not be sent to Graphite
[Parameter(Mandatory = $false)]
[switch]$TestMode,
[switch]$ExcludePerfCounters = $false,
[switch]$SqlMetrics = $false
)
# Run The Load XML Config Function
$Config = Import-XMLConfig -ConfigPath $configPath
# Get Last Run Time
$sleep = 0
$configFileLastWrite = (Get-Item -Path $configPath).LastWriteTime
if($ExcludePerfCounters -and -not $SqlMetrics) {
throw "Parameter combination provided will prevent any metrics from being collected"
}
if($SqlMetrics) {
if ($Config.MSSQLServers.Length -gt 0)
{
# Check for SQLPS Module
if (($listofSQLModules = Get-Module -List SQLPS).Length -eq 1)
{
# Load The SQL Module
Import-Module SQLPS -DisableNameChecking
}
# Check for the PS SQL SnapIn
elseif ((Test-Path ($env:ProgramFiles + '\Microsoft SQL Server\100\Tools\Binn\Microsoft.SqlServer.Management.PSProvider.dll')) `
-or (Test-Path ($env:ProgramFiles + ' (x86)' + '\Microsoft SQL Server\100\Tools\Binn\Microsoft.SqlServer.Management.PSProvider.dll')))
{
# Load The SQL SnapIns
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
}
# If No Snapin's Found end the function
else
{
throw "Unable to find any SQL CmdLets. Please install them and try again."
}
}
else
{
Write-Warning "There are no SQL Servers in your configuration file. No SQL metrics will be collected."
}
}
# Start Endless Loop
while ($true)
{
# Loop until enough time has passed to run the process again.
if($sleep -gt 0) {
Start-Sleep -Milliseconds $sleep
}
# Used to track execution time
$iterationStopWatch = [System.Diagnostics.Stopwatch]::StartNew()
# Convert To The TimeZone of the Graphite Server
$convertedTime = Convert-TimeZone -DateTime (Get-Date -Format s) -ToTimeZone $Config.TimeZoneOfGraphiteServer
# Get the TargetTime Part of the Script
$convertedTime = $convertedTime.TargetTime
# Round Time to Nearest Time Period
$convertedTime = $convertedTime.AddSeconds(- ($convertedTime.Second % $Config.MetricSendIntervalSeconds))
$metricsToSend = @{}
if(-not $ExcludePerfCounters)
{
# Take the Sample of the Counter
$collections = Get-Counter -Counter $Config.Counters -SampleInterval 1 -MaxSamples 1
# Filter the Output of the Counters
$samples = $collections.CounterSamples
# Verbose
Write-Verbose "All Samples Collected"
# Loop Through All The Counters
foreach ($sample in $samples)
{
if ($Config.ShowOutput)
{
$samplePath = $sample.Path
Write-Verbose "Sample Name: $samplePath"
}
# Create Stopwatch for Filter Time Period
$filterStopWatch = [System.Diagnostics.Stopwatch]::StartNew()
# Check if there are filters or not
if ([string]::IsNullOrWhiteSpace($Config.Filters) -or $sample.Path -notmatch [regex]$Config.Filters)
{
# Run the sample path through the ConvertTo-GraphiteMetric function
$cleanNameOfSample = ConvertTo-GraphiteMetric -MetricToClean $sample.Path -RemoveUnderscores -NicePhysicalDisks
# Build the full metric path
$metricPath = $Config.MetricPath + '.' + $cleanNameOfSample
$metricsToSend[$metricPath] = $sample.Cookedvalue
}
else
{
Write-Verbose "Filtering out Sample Name: $($samplePath) as it matches something in the filters."
}
$filterStopWatch.Stop()
Write-Verbose "Job Execution Time To Get to Clean Metrics: $($filterStopWatch.Elapsed.TotalSeconds) seconds."
}# End for each sample loop
}# end if ExcludePerfCounters
if($SqlMetrics) {
# Loop through each SQL Server
foreach ($sqlServer in $Config.MSSQLServers)
{
Write-Verbose "Running through SQLServer $($sqlServer.ServerInstance)"
# Loop through each query for the SQL server
foreach ($query in $sqlServer.Queries)
{
Write-Verbose "Current Query $($query.TSQL)"
$sqlCmdParams = @{
'ServerInstance' = $sqlServer.ServerInstance;
'Database' = $query.Database;
'Query' = $query.TSQL;
'ConnectionTimeout' = $Config.MSSQLConnectTimeout;
'QueryTimeout' = $Config.MSSQLQueryTimeout
}
# Run the Invoke-SqlCmd Cmdlet with a username and password only if they are present in the config file
if (-not [string]::IsNullOrWhitespace($sqlServer.Username) `
-and -not [string]::IsNullOrWhitespace($sqlServer.Password))
{
$sqlCmdParams['Username'] = $sqlServer.Username
$sqlCmdParams['Password'] = $sqlServer.Password
}
# Run the SQL Command
try
{
$commandMeasurement = Measure-Command -Expression {
$sqlresult = Invoke-SQLCmd @sqlCmdParams
# Build the MetricPath that will be used to send the metric to Graphite
$metricPath = $Config.MSSQLMetricPath + '.' + $query.MetricName
$metricsToSend[$metricPath] = $sqlresult[0]
}
Write-Verbose ('SQL Metric Collection Execution Time: ' + $commandMeasurement.TotalSeconds + ' seconds')
}
catch
{
$exceptionText = GetPrettyProblem $_
throw "An error occurred with processing the SQL Query. $exceptionText"
}
} #end foreach Query
} #end foreach SQL Server
}#endif SqlMetrics
# Send To Graphite Server
$sendBulkGraphiteMetricsParams = @{
"CarbonServer" = $Config.CarbonServer
"CarbonServerPort" = $Config.CarbonServerPort
"Metrics" = $metricsToSend
"DateTime" = $convertedTime
"UDP" = $Config.SendUsingUDP
"TestMode" = $TestMode
}
Send-BulkGraphiteMetrics @sendBulkGraphiteMetricsParams
# Reloads The Configuration File After the Loop so new counters can be added on the fly
if((Get-Item $configPath).LastWriteTime -gt (Get-Date -Date $configFileLastWrite)) {
$Config = Import-XMLConfig -ConfigPath $configPath
}
$iterationStopWatch.Stop()
$collectionTime = $iterationStopWatch.Elapsed
$sleep = $Config.MetricTimeSpan.TotalMilliseconds - $collectionTime.TotalMilliseconds
if ($Config.ShowOutput)
{
# Write To Console How Long Execution Took
$VerboseOutPut = 'PerfMon Job Execution Time: ' + $collectionTime.TotalSeconds + ' seconds'
Write-Output $VerboseOutPut
}
}
}
Function Start-SQLStatsToGraphite
{
<#
.Synopsis
Starts a loop which sends the result of SQL Server queries to Graphite.
.Description
Starts a loop which sends the result of SQL queries defined in the configuration file to Graphite. Configuration is all done from the StatsToGraphiteConfig.xml file. The specified SQL commands must return a single row containing a number.
This function requires the Microsoft SQL PowerShell Modules/SnapIns. The easiest way to get these on your server is to download them from the SQL 2012 R2 Feature Pack. You will need to grab the following:
- Microsoft® SQL Server® 2012 Shared Management Object
- Microsoft® System CLR Types for Microsoft® SQL Server® 2012
- Microsoft® Windows PowerShell Extensions for Microsoft® SQL Server® 2012
.Parameter Verbose
Provides Verbose output which is useful for troubleshooting
.Parameter TestMode
Result will be collected from SQL and the metric that would be sent to Graphite is shown, without sending the metric on to Graphite.
.Example
PS> Start-SQLStatsToGraphite
Will start the endless loop to send the result of the SQL queries to Graphite.
.Example
PS> Start-SQLStatsToGraphite -Verbose
Will start the endless loop to send the result of the SQL queries to Graphite and provide Verbose output.
.Example
PS> Start-SQLStatsToGraphite -TestMode
Results will be collected from SQL but they will not be sent over to Graphite.
.Notes
NAME: Start-SQLStatsToGraphite
AUTHOR: Matthew Hodgkins
WEBSITE: http://www.hodgkins.net.au
#>
[CmdletBinding()]
Param
(
# Enable Test Mode. Metrics will not be sent to Graphite
[Parameter(Mandatory = $false)]
[switch]$TestMode
)
$PSBoundParameters['ExcludePerfCounters'] = $true
$PSBoundParameters['SqlMetrics'] = $true
Start-StatsToGraphite @PSBoundParameters
}
Function ConvertTo-GraphiteMetric
{
<#
.Synopsis
Converts Windows PerfMon metrics into a metric name that is suitable for Graphite.
.Description
Converts Windows PerfMon metrics into a metric name that is suitable for a Graphite metric path.
.Parameter MetricToClean
The metric to be cleaned.
.Parameter RemoveUnderscores
Removes Underscores from the metric name
.Parameter NicePhysicalDisks
Makes the physical disk perf counters prettier
.Example
PS> ConvertTo-GraphiteMetric -MetricToClean "\Processor(_Total)\% Processor Time"
.Processor._Total.ProcessorTime
.Example
PS> ConvertTo-GraphiteMetric -MetricToClean "\Processor(_Total)\% Processor Time" -RemoveUnderscores
.Processor.Total.ProcessorTime
.Notes
NAME: ConvertTo-GraphiteMetric
AUTHOR: Matthew Hodgkins
WEBSITE: http://www.hodgkins.net.au
#>
param
(
[CmdletBinding()]
[parameter(Mandatory = $true)]
[string]$MetricToClean,
[parameter(Mandatory = $false)]
[switch]$RemoveUnderscores,
[parameter(Mandatory = $false)]
[switch]$NicePhysicalDisks
)
# Removing Beginning Backslashes"
$cleanNameOfSample = $MetricToClean -replace '^\\\\', ''
# Replacing Backslashes After ServerName With dot"
$cleanNameOfSample = $cleanNameOfSample -replace '\\\\', '.'
# Removing Replacing Colon with Dot"
$cleanNameOfSample = $cleanNameOfSample -replace ':', '.'
# Changing Fwd Slash To Dash"
$cleanNameOfSample = $cleanNameOfSample -replace '\/', '-'
# Changing BackSlash To Dot"
$cleanNameOfSample = $cleanNameOfSample -replace '\\', '.'
# Changing Opening Round Bracket to Dot"
$cleanNameOfSample = $cleanNameOfSample -replace '\(', '.'
# Removing Closing Round Bracket to Dot"
$cleanNameOfSample = $cleanNameOfSample -replace '\)', ''
# Removing Square Bracket"
$cleanNameOfSample = $cleanNameOfSample -replace '\]', ''
# Removing Square Bracket"
$cleanNameOfSample = $cleanNameOfSample -replace '\[', ''
# Removing Percentage Sign"
$cleanNameOfSample = $cleanNameOfSample -replace '\%', ''
# Removing Spaces"
$cleanNameOfSample = $cleanNameOfSample -replace '\s+', ''
# Removing Double Dots"
$cleanNameOfSample = $cleanNameOfSample -replace '\.\.', '.'
if ($RemoveUnderscores)
{
Write-Verbose "Removing Underscores as the switch is enabled"
$cleanNameOfSample = $cleanNameOfSample -replace '_', ''
}
if ($NicePhysicalDisks)
{
Write-Verbose "NicePhyiscalDisks switch is enabled"
# Get Drive Letter
$driveLetter = ([regex]'physicaldisk\.\d([a-zA-Z])').match($cleanNameOfSample).groups[1].value
# Add -drive to the drive letter
$cleanNameOfSample = $cleanNameOfSample -replace 'physicaldisk\.\d([a-zA-Z])', ('physicaldisk.' + $driveLetter + '-drive')
# Get the new cleaned drive letter
$niceDriveLetter = ([regex]'physicaldisk\.(.*)\.avg\.').match($cleanNameOfSample).groups[1].value
# Remvoe the .avg. section
$cleanNameOfSample = $cleanNameOfSample -replace 'physicaldisk\.(.*)\.avg\.', ('physicaldisk.' + $niceDriveLetter + '.')
}
Write-Output $cleanNameOfSample
}
function Send-GraphiteMetric
{
<#
.Synopsis
Sends Graphite Metrics to a Carbon server.
.Description
This function takes a metric, value and Unix timestamp and sends it to a Graphite server.
.Parameter CarbonServer
The Carbon server IP or address.
.Parameter CarbonServerPort
The Carbon server port. Default is 2003.
.Parameter MetricPath
The Graphite formatted metric path. (Must contain no spaces).
.Parameter MetricValue
The the value of the metric path you are sending.
.Parameter UnixTime
The the unix time stamp of the metric being sent the Graphite Server.
.Parameter DateTime
The DateTime object of the metric being sent the Graphite Server. This does a direct conversion to Unix time without accounting for Time Zones. If your PC time zone does not match your Graphite servers time zone the metric will appear on the incorrect time.
.Example
Send-GraphiteMetric -CarbonServer myserver.local -CarbonServerPort 2003 -MetricPath houston.servers.webserver01.cpu.processortime -MetricValue 54 -UnixTime 1391141202
This sends the houston.servers.webserver01.cpu.processortime metric to the specified carbon server.
.Example
Send-GraphiteMetric -CarbonServer myserver.local -CarbonServerPort 2003 -MetricPath houston.servers.webserver01.cpu.processortime -MetricValue 54 -DateTime (Get-Date)
This sends the houston.servers.webserver01.cpu.processortime metric to the specified carbon server.
.Notes
NAME: Send-GraphiteMetric
AUTHOR: Matthew Hodgkins
WEBSITE: http://www.hodgkins.net.au
#>
param
(
[CmdletBinding(DefaultParametersetName = 'Date Object')]
[parameter(Mandatory = $true)]
[string]$CarbonServer,
[parameter(Mandatory = $false)]
[ValidateRange(1, 65535)]
[int]$CarbonServerPort = 2003,
[parameter(Mandatory = $true)]
[string]$MetricPath,
[parameter(Mandatory = $true)]
[string]$MetricValue,
[Parameter(Mandatory = $true,
ParameterSetName = 'Epoch / Unix Time')]
[ValidateRange(1, 99999999999999)]
[string]$UnixTime,
[Parameter(Mandatory = $true,
ParameterSetName = 'Date Object')]
[datetime]$DateTime,
# Will Display what will be sent to Graphite but not actually send it
[Parameter(Mandatory = $false)]
[switch]$TestMode,
# Sends the metrics over UDP instead of TCP
[Parameter(Mandatory = $false)]
[switch]$UDP
)
# If Received A DateTime Object - Convert To UnixTime
if ($DateTime)
{
# Convert to a Unix time without any rounding
$UnixTime = (Get-Date $DateTime -UFormat %s) -Replace ("[,\.]\d*", "")
}
# Create Send-To-Graphite Metric
$metric = $MetricPath + " " + $MetricValue + " " + $UnixTime
Write-Verbose "Metric Received: $metric"
$sendMetricsParams = @{
"CarbonServer" = $CarbonServer
"CarbonServerPort" = $CarbonServerPort
"Metrics" = $metric
"IsUdp" = $UDP
"TestMode" = $TestMode
}
SendMetrics @sendMetricsParams
}
function Send-BulkGraphiteMetrics
{
<#
.Synopsis
Sends several Graphite Metrics to a Carbon server with one request. Bulk requests save a lot of resources for Graphite server.
.Description
This function takes hashtable (MetricPath => MetricValue) and Unix timestamp and sends them to a Graphite server.
.Parameter CarbonServer
The Carbon server IP or address.
.Parameter CarbonServerPort
The Carbon server port. Default is 2003.
.Parameter Metrics
Hashtable (MetricPath => MetricValue).
.Parameter UnixTime
The the unix time stamp of the metrics being sent the Graphite Server.
.Parameter DateTime
The DateTime object of the metrics being sent the Graphite Server. This does a direct conversion to Unix time without accounting for Time Zones. If your PC time zone does not match your Graphite servers time zone the metric will appear on the incorrect time.
.Example
Send-BulkGraphiteMetrics -CarbonServer myserver.local -CarbonServerPort 2003 -Metrics @{"houston.servers.webserver01.cpu.processortime" = 54; "houston.servers.webserver02.cpu.processortime" = 43} -UnixTime 1391141202
This sends the houston.servers.webserver0*.cpu.processortime metrics to the specified carbon server.
.Example
Send-BulkGraphiteMetrics -CarbonServer myserver.local -CarbonServerPort 2003 -Metrics @{"houston.servers.webserver01.cpu.processortime" = 54; "houston.servers.webserver02.cpu.processortime" = 43} -DateTime (Get-Date)
This sends the houston.servers.webserver0*.cpu.processortime metrics to the specified carbon server.
.Notes
NAME: Send-BulkGraphiteMetrics
AUTHOR: Alexey Kirpichnikov
#>
param
(
[CmdletBinding(DefaultParametersetName = 'Date Object')]
[parameter(Mandatory = $true)]
[string]$CarbonServer,
[parameter(Mandatory = $false)]
[ValidateRange(1, 65535)]
[int]$CarbonServerPort = 2003,
[parameter(Mandatory = $true)]
[hashtable]$Metrics,
[Parameter(Mandatory = $true,
ParameterSetName = 'Epoch / Unix Time')]
[ValidateRange(1, 99999999999999)]
[string]$UnixTime,
[Parameter(Mandatory = $true,
ParameterSetName = 'Date Object')]
[datetime]$DateTime,
# Will Display what will be sent to Graphite but not actually send it
[Parameter(Mandatory = $false)]
[switch]$TestMode,
# Sends the metrics over UDP instead of TCP
[Parameter(Mandatory = $false)]
[switch]$UDP
)
# If Received A DateTime Object - Convert To UnixTime
if ($DateTime)
{
# Convert to a Unix time without any rounding
$UnixTime = (Get-Date $DateTime -UFormat %s) -Replace ("[,\.]\d*", "")
}
# Create Send-To-Graphite Metric
[string[]]$metricStrings = @()
foreach ($key in $Metrics.Keys)
{
$metricStrings += $key + " " + $Metrics[$key] + " " + $UnixTime
Write-Verbose ("Metric Received: " + $metricStrings[-1])
}
$sendMetricsParams = @{
"CarbonServer" = $CarbonServer
"CarbonServerPort" = $CarbonServerPort
"Metrics" = $metricStrings
"IsUdp" = $UDP
"TestMode" = $TestMode
}
SendMetrics @sendMetricsParams
}
function Send-GraphiteEvent
{
<#
.Synopsis
Sends an event to Graphite.
.Description
Sends an event to a Graphite server. Examples of events that are appropriate for this metric type include releases, commits, application exceptions or anything that represents a state change where you might wish to track the affected data. More information is available here: http://obfuscurity.com/2014/01/Graphite-Tip-A-Better-Way-to-Store-Events
.Example
PS> Send-GraphiteEvent -GraphiteURL "http://10.4.48.113:81/" -What "Windows Patch Deploy"
Sends an event to Graphite.
.Example
PS> Send-GraphiteEvent -GraphiteURL "http://10.4.48.113:81/" -What "Website Deploy" -Tags "webdeploy, patches"
Sends a web deploy event to Graphite with multiple tags.
.Example
PS> Send-GraphiteEvent -GraphiteURL "http://10.4.48.113:81/" -What "Website Deploy" -Tags "webdeploy" -Data "Deployed patch #4123 to the Web Server"
Sends a web deploy event to Graphite with Tags and Data.
.Notes
NAME: Send-GraphiteEvent
AUTHOR: Matthew Hodgkins
WEBSITE: http://www.hodgkins.net.au
#>
[CmdletBinding()]
param
(
[CmdletBinding()]
# The URL of the Graphite Servers Web Interface. For example http://10.4.48.113:8080 or https://myGraphiteServer.local
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ $_ -match '^(http|https)\:\/\/.*' })]
[string]$GraphiteURL,
# The "What" or Topic for the Event
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[Alias("Topic", "Title", "Subject")]
[string]$What,
# A tag or multiple tags for the event. If you are using multiple tags, separated then with commas
[Parameter(Mandatory = $false)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$Tags,
# The body of the event
[Parameter(Mandatory = $false)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[Alias("Body")]
[string]$Data
)
# Check for trailing slash
if (!($GraphiteURL.Substring($GraphiteURL.Length - 1) -eq '/'))
{
$GraphiteURL = $GraphiteURL + '/'
}
# Construct Full URL to Events API
$GraphiteURL = $GraphiteURL + '/events/'
# Build an Object to hold the data from the Function
$EventObject = New-Object PSObject -Property @{
what = $What
}
# If there are tags
if ($Tags)
{
Add-Member -NotePropertyName tags -NotePropertyValue $Tags -InputObject $EventObject
}
# If there is data
if ($Data)
{
Add-Member -NotePropertyName data -NotePropertyValue $Data -InputObject $EventObject
}
$EventObject = $EventObject | ConvertTo-Json
Write-Verbose "Json Object:"
Write-Verbose $EventObject
try
{
$result = Invoke-WebRequest -Uri $GraphiteURL -Body $EventObject -method Post -ContentType "application/json"
Write-Verbose "Returned StatusCode: $($result.StatusCode)"
Write-Verbose "Returned StatusDescription: $($result.StatusDescription)"
}
catch
{
$exceptionText = GetPrettyProblem $_
throw "An error occurred trying to post data to Graphite. $exceptionText"
}
}
function Convert-TimeZone
{
<#
.Synopsis
Coverts from a given time zone to the specified time zone.
.Description
Coverts from a given time zone to the specified time zone.
.Parameter DateTime
A DateTime object will be converted to the new time zone.
.Parameter ToTimeZone
The name of the target time zone you wish to convert to. You can get the names by using the -ListTimeZones parameter.
.Parameter ListTimeZones
Lists all the time zones that can be used.
.Example
Convert-TimeZone -ListTimeZones
Id : Dateline Standard Time
DisplayName : (UTC-12:00) International Date Line West
StandardName : Dateline Standard Time
DaylightName : Dateline Daylight Time
BaseUtcOffset : -12:00:00
SupportsDaylightSavingTime : False
Id : UTC-11
DisplayName : (UTC-11:00) Coordinated Universal Time-11
StandardName : UTC-11
DaylightName : UTC-11
BaseUtcOffset : -11:00:00
SupportsDaylightSavingTime : False
Lists available time zones to convert to.
.Example
Convert-TimeZone -DateTime (Get-Date) -ToTimeZone UTC
Converts current time to UTC time.
.Notes
NAME: Convert-TimeZone
AUTHOR: Matthew Hodgkins
WEBSITE: http://www.hodgkins.net.au
#>
param
(
[CmdletBinding(DefaultParametersetName = 'Convert Time Zone')]
[Parameter(Mandatory = $true,
ParameterSetName = 'Convert Time Zone')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[datetime]$DateTime,
[Parameter(Mandatory = $true,
ParameterSetName = 'Convert Time Zone')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$ToTimeZone,
[Parameter(Mandatory = $false,
ParameterSetName = 'List Time Zones')]
[switch]$ListTimeZones
)
# Loading dll for Windows 2003 R2
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Core')
# List TimeZones for the user
if ($ListTimeZones)
{
[System.TimeZoneInfo]::GetSystemTimeZones()
return
}
# Run the Function
else
{
$TimeZoneObject = [System.TimeZoneInfo]::FindSystemTimeZoneById($ToTimeZone)
$TargetZoneTime = [System.TimeZoneInfo]::ConvertTime($DateTime, $TimeZoneObject)
$OutObject = New-Object -TypeName PSObject -Property @{
LocalTime = $DateTime
LocalTimeZone = $(([System.TimeZoneInfo]::LOCAL).id)
TargetTime = $TargetZoneTime
TargetTimeZone = $($TimeZoneObject.id)
}
Write-Output $OutObject
}
}
Function Import-XMLConfig
{
<#
.Synopsis
Loads the XML Config File for Send-StatsToGraphite.
.Description
Loads the XML Config File for Send-StatsToGraphite.
.Parameter ConfigPath
Full path to the configuration XML file.
.Example
Import-XMLConfig -ConfigPath C:\Stats\Send-PowerShellGraphite.ps1
.Notes
NAME: Convert-TimeZone
AUTHOR: Matthew Hodgkins
WEBSITE: http://www.hodgkins.net.au
#>
[CmdletBinding()]
Param
(
# Configuration File Path
[Parameter(Mandatory = $true)]
$ConfigPath
)
[hashtable]$Config = @{ }
# Load Configuration File
$xmlfile = [xml]([System.IO.File]::ReadAllText($configPath))
#Set the Graphite carbon server location and port number
$Config.CarbonServer = $xmlfile.Configuration.Graphite.CarbonServer
$Config.CarbonServerPort = $xmlfile.Configuration.Graphite.CarbonServerPort
#Get Metric Send Interval From Config
[int]$Config.MetricSendIntervalSeconds = $xmlfile.Configuration.Graphite.MetricSendIntervalSeconds
# Get the Timezone Of the Graphite Server
$Config.TimeZoneOfGraphiteServer = $xmlfile.Configuration.Graphite.TimeZoneOfGraphiteServer
# Convert Value in Configuration File to Bool for Sending via UDP
[bool]$Config.SendUsingUDP = [System.Convert]::ToBoolean($xmlfile.Configuration.Graphite.SendUsingUDP)
# Convert Interval into TimeSpan
$Config.MetricTimeSpan = [timespan]::FromSeconds($Config.MetricSendIntervalSeconds)
# What is the metric path
$Config.MetricPath = $xmlfile.Configuration.Graphite.MetricPath
# Convert Value in Configuration File to Bool for showing Verbose Output
[bool]$Config.ShowOutput = [System.Convert]::ToBoolean($xmlfile.Configuration.Logging.VerboseOutput)
# Create the Performance Counters Array
$Config.Counters = @()
# Load each row from the configuration file into the counter array
foreach ($counter in $xmlfile.Configuration.PerformanceCounters.Counter)
{
$Config.Counters += $counter.Name
}
# Load each row from the configuration file into the counter array
foreach ($MetricFilter in $xmlfile.Configuration.Filtering.MetricFilter)
{
$Config.Filters += $MetricFilter.Name + '|'
}
if($Config.Filters.Length -gt 0) {
# Trim trailing and leading white spaces
$Config.Filters = $Config.Filters.Trim()
# Strip the Last Pipe From the filters string so regex can work against the string.
$Config.Filters = $Config.Filters.TrimEnd("|")
}
else
{
$Config.Filters = $null
}
# Below is for SQL Metrics
$Config.MSSQLMetricPath = $xmlfile.Configuration.MSSQLMetics.MetricPath
$Config.MSSQLMetricSendIntervalSeconds = $xmlfile.Configuration.MSSQLMetics.MetricSendIntervalSeconds
$Config.MSSQLMetricTimeSpan = [timespan]::FromSeconds($Config.MSSQLMetricSendIntervalSeconds)
[int]$Config.MSSQLConnectTimeout = $xmlfile.Configuration.MSSQLMetics.SQLConnectionTimeoutSeconds
[int]$Config.MSSQLQueryTimeout = $xmlfile.Configuration.MSSQLMetics.SQLQueryTimeoutSeconds
# Create the Performance Counters Array
$Config.MSSQLServers = @()
foreach ($sqlServer in $xmlfile.Configuration.MSSQLMetics.SQLServers.SQLServer)
{
# Load each SQL Server into an array
$Config.MSSQLServers += [pscustomobject]@{
ServerInstance = $sqlServer.ServerInstance;
Username = $sqlServer.Username;
Password = $sqlServer.Password;
Queries = $sqlServer.Query
}
}
Return $Config
}
# http://support-hq.blogspot.com/2011/07/using-clause-for-powershell.html
function PSUsing
{
param (
[System.IDisposable] $inputObject = $(throw "The parameter -inputObject is required."),
[ScriptBlock] $scriptBlock = $(throw "The parameter -scriptBlock is required.")
)
Try
{
&$scriptBlock
}
Finally
{
if ($inputObject -ne $null)
{
if ($inputObject.psbase -eq $null)
{
$inputObject.Dispose()
}
else
{
$inputObject.psbase.Dispose()
}
}
}
}
function SendMetrics
{
param (
[string]$CarbonServer,
[int]$CarbonServerPort,
[string[]]$Metrics,
[switch]$IsUdp = $false,
[switch]$TestMode = $false
)
if (!($TestMode))
{
try
{
if ($isUdp)
{
PSUsing ($udpobject = new-Object system.Net.Sockets.Udpclient($CarbonServer, $CarbonServerPort)) -ScriptBlock {
$enc = new-object system.text.asciiencoding
$Message = new-object System.Text.StringBuilder
foreach($metric in $metrics) {
$Message.Append($metric)
$Message.Append("`n")
}
$byte = $enc.GetBytes($Message.ToString())
$Sent = $udpobject.Send($byte,$byte.Length)
}
Write-Verbose "Sent via UDP to $($CarbonServer) on port $($CarbonServerPort)."
}
else
{
PSUsing ($socket = New-Object System.Net.Sockets.TCPClient) -ScriptBlock {
$socket.connect($CarbonServer, $CarbonServerPort)
PSUsing ($stream = $socket.GetStream()) {
PSUSing($writer = new-object System.IO.StreamWriter($stream)) {
foreach ($metricString in $Metrics)
{
$writer.WriteLine($metricString)
}
$writer.Flush()
Write-Verbose "Sent via TCP to $($CarbonServer) on port $($CarbonServerPort)."
}
}
}
}
}
catch
{
$exceptionText = GetPrettyProblem $_
throw "Error sending metrics to the Graphite Server. Please check your configuration file. `n$exceptionText"
}
}
}
function GetPrettyProblem {
param (
$Problem
)
$prettyString = (Out-String -InputObject (format-list -inputobject $Problem -Property * -force)).Trim()
return $prettyString