Skip to content
This repository was archived by the owner on May 30, 2019. It is now read-only.

Commit 3fcdfbd

Browse files
committed
Adding feature to allow the hostname of the metrics to be overwritten in the XML configuration file by modifying the ConvertTo-GraphiteMetric function.
Modified Import-XMLConfig function to import the new configuration value, and updated Pester Tests Added Pester Tests for the the ConvertTo-GraphiteMetric function. Updated readme.md with new configuration value.
1 parent 843e516 commit 3fcdfbd

7 files changed

+110
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
2+
$toplevel = Split-Path -Parent $here
3+
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace(".Tests.", ".")
4+
5+
. "$here\$sut"
6+
7+
Describe "ConvertTo-GraphiteMetric" {
8+
Context "Metric Transformation - Base Function" {
9+
$TestMetric = ConvertTo-GraphiteMetric -MetricToClean "\\myServer\network interface(realtek pcie gbe family controller)\bytes received/sec"
10+
11+
It "Should Return Something" {
12+
$TestMetric | Should Not BeNullOrEmpty
13+
}
14+
It "Should Provide myServer.networkinterface.realtekpciegbefamilycontroller.bytesreceived-sec as Output" {
15+
$TestMetric | Should MatchExactly "myServer.networkinterface.realtekpciegbefamilycontroller.bytesreceived-sec"
16+
}
17+
It "Should Not Contain Left Parentheses" {
18+
$TestMetric | Should Not Match "\("
19+
}
20+
It "Should Not Contain Right Parentheses" {
21+
$TestMetric | Should Not Match "\)"
22+
}
23+
It "Should Not Contain Forward Slash" {
24+
$TestMetric | Should Not Match "\/"
25+
}
26+
It "Should Not Contain Back Slash" {
27+
$TestMetric | Should Not Match "\\"
28+
}
29+
It "Should Contain a Period" {
30+
$TestMetric | Should Match "\."
31+
}
32+
}
33+
Context "Metric Transformation - Remove Underscores" {
34+
$TestMetric = ConvertTo-GraphiteMetric -MetricToClean "\\myServer\Processor(_Total)\% Processor Time" -RemoveUnderscores
35+
36+
It "Should Return Something" {
37+
$TestMetric | Should Not BeNullOrEmpty
38+
}
39+
It "Should Return myServer.production.net.Processor.Total.ProcessorTime as Output" {
40+
$TestMetric | Should MatchExactly "myServer.Processor.Total.ProcessorTime"
41+
}
42+
It "Should Not Contain Underscores" {
43+
$TestMetric | Should Not Match "_"
44+
}
45+
}
46+
Context "Metric Transformation - Provide Nice Output for Physical Disks" {
47+
$TestMetric = ConvertTo-GraphiteMetric -MetricToClean "\\myServer\physicaldisk(1 e:)\avg. disk write queue length" -RemoveUnderscores -NicePhysicalDisks
48+
49+
It "Should Return Something" {
50+
$TestMetric | Should Not BeNullOrEmpty
51+
}
52+
It "Should Return myServer.physicaldisk.e-drive.diskwritequeuelength as Output" {
53+
$TestMetric | Should MatchExactly "myServer.physicaldisk.e-drive.diskwritequeuelength"
54+
}
55+
}
56+
Context "Metric Transformation - Replace HostName" {
57+
$TestMetric = ConvertTo-GraphiteMetric -MetricToClean "\\$($env:COMPUTERNAME)\physicaldisk(1 e:)\avg. disk write queue length" -RemoveUnderscores -NicePhysicalDisks -HostName "my.new.hostname"
58+
59+
It "Should Return Something" {
60+
$TestMetric | Should Not BeNullOrEmpty
61+
}
62+
It "Should Return my.new.hostname.physicaldisk.e-drive.diskwritequeuelength as Output" {
63+
$TestMetric | Should MatchExactly "my.new.hostname.physicaldisk.e-drive.diskwritequeuelength"
64+
}
65+
}
66+
}

Functions/ConvertTo-GraphiteMetric.ps1

+27-5
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,26 @@ Function ConvertTo-GraphiteMetric
1616
.Parameter NicePhysicalDisks
1717
Makes the physical disk perf counters prettier
1818
19+
.Parameter HostName
20+
Allows you to override the hostname of the metrics before sending. Default is not to override and use what the Windows Performance Counters provide.
21+
22+
.Example
23+
PS> ConvertTo-GraphiteMetric -MetricToClean "\\myServer\network interface(realtek pcie gbe family controller)\bytes received/sec"
24+
myServer.networkinterface.realtekpciegbefamilycontroller.bytesreceived-sec
25+
26+
Cleaning a Windows Performance Counter so its ready for Graphite.
27+
1928
.Example
20-
PS> ConvertTo-GraphiteMetric -MetricToClean "\Processor(_Total)\% Processor Time"
21-
.Processor._Total.ProcessorTime
29+
PS> ConvertTo-GraphiteMetric -MetricToClean "\\myServer\Processor(_Total)\% Processor Time" -RemoveUnderscores
30+
myServer.Processor.Total.ProcessorTime
31+
32+
Cleaning a Windows Performance Counter so its ready for Graphite and removing underscores.
2233
2334
.Example
24-
PS> ConvertTo-GraphiteMetric -MetricToClean "\Processor(_Total)\% Processor Time" -RemoveUnderscores
25-
.Processor.Total.ProcessorTime
35+
PS> ConvertTo-GraphiteMetric -MetricToClean "\\myServer\Processor(_Total)\% Processor Time" -RemoveUnderscores -HostName myserver.production.net
36+
myserver.production.net.Processor.Total.ProcessorTime
37+
38+
Cleaning a Windows Performance Counter so its ready for Graphite and removing underscores. Replacing HostName with custom name.
2639
2740
.Notes
2841
NAME: ConvertTo-GraphiteMetric
@@ -39,9 +52,18 @@ Function ConvertTo-GraphiteMetric
3952
[switch]$RemoveUnderscores,
4053

4154
[parameter(Mandatory = $false)]
42-
[switch]$NicePhysicalDisks
55+
[switch]$NicePhysicalDisks,
56+
57+
[parameter(Mandatory = $false)]
58+
[string]$HostName=$env:COMPUTERNAME
4359
)
4460

61+
# If HostName is being overwritten
62+
if ($HostName -ne $env:COMPUTERNAME)
63+
{
64+
$MetricToClean = $MetricToClean -replace "\\\\$($env:COMPUTERNAME)\\","\\$($HostName)\"
65+
}
66+
4567
# Removing Beginning Backslashes"
4668
$cleanNameOfSample = $MetricToClean -replace '^\\\\', ''
4769

Functions/Internal.Tests.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Describe "Importing of XML Configuration File" {
1111
It "Loaded Configuration File Should Not Be Empty" {
1212
$Config | Should Not BeNullOrEmpty
1313
}
14-
It "Should Have 15 Properties" {
15-
$Config.Count | Should Be 15
14+
It "Should Have 16 Properties" {
15+
$Config.Count | Should Be 16
1616
}
1717
It "SendUsingUDP should be Boolean" {
1818
$Config.SendUsingUDP -is [Boolean] | Should Be $true

Functions/Internal.ps1

+11-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@ Function Import-XMLConfig
3232
# Load Configuration File
3333
$xmlfile = [xml]([System.IO.File]::ReadAllText($configPath))
3434

35-
#Set the Graphite carbon server location and port number
35+
# Set the Graphite carbon server location and port number
3636
$Config.CarbonServer = $xmlfile.Configuration.Graphite.CarbonServer
3737
$Config.CarbonServerPort = $xmlfile.Configuration.Graphite.CarbonServerPort
3838

39-
#Get Metric Send Interval From Config
39+
# Get the HostName to use for the metrics from the config file
40+
$Config.NodeHostName = $xmlfile.Configuration.Graphite.NodeHostName
41+
42+
# Set the NodeHostName to ComputerName
43+
if($Config.NodeHostName -eq '$env:COMPUTERNAME')
44+
{
45+
$Config.NodeHostName = $env:COMPUTERNAME
46+
}
47+
48+
# Get Metric Send Interval From Config
4049
[int]$Config.MetricSendIntervalSeconds = $xmlfile.Configuration.Graphite.MetricSendIntervalSeconds
4150

4251
# Convert Value in Configuration File to Bool for Sending via UDP

Functions/Start-StatsToGraphite.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Function Start-StatsToGraphite
140140
if ([string]::IsNullOrWhiteSpace($Config.Filters) -or $sample.Path -notmatch [regex]$Config.Filters)
141141
{
142142
# Run the sample path through the ConvertTo-GraphiteMetric function
143-
$cleanNameOfSample = ConvertTo-GraphiteMetric -MetricToClean $sample.Path -RemoveUnderscores -NicePhysicalDisks
143+
$cleanNameOfSample = ConvertTo-GraphiteMetric -MetricToClean $sample.Path -RemoveUnderscores -NicePhysicalDisks -HostName $Config.NodeHostName
144144

145145
# Build the full metric path
146146
$metricPath = $Config.MetricPath + '.' + $cleanNameOfSample

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ More details at [http://www.hodgkins.net.au/mswindows/using-powershell-to-send-m
1111
* Can collect values by using T-SQL queries against MS SQL databases
1212
* Converts time to UTC on sending
1313
* All configuration can be done from a simple XML file
14+
* Allows you to override the hostname in Windows Performance Counters before sending on to Graphite
1415
* Reloads the XML configuration file automatically. For example, if more counters are added to the configuration file, the script will notice and start sending metrics for them to Graphite in the next send interval
1516
* Additional functions are exposed that allow you to send data to Graphite from PowerShell easily. [Here](#functions) is the list of included functions
1617
* Script can be installed to run as a service
@@ -38,6 +39,7 @@ Configuration Name | Description
3839
CarbonServer | The server name where Carbon is running. The Carbon daemon is usually running on the Graphite server.
3940
CarbonServerPort | The port number for Carbon. Its default port number is 2003.
4041
MetricPath | The path of the metric you want to be sent to the server. If you are using HostedGraphite, put your API key before the rest of the metric path, for example `YOUR-API-KEY.datacenter1.servers`.
42+
NodeHostName | This allows you to override the hostname of the server before sending the metrics on to Graphite. Default is use `$env:COMPUTERNAME`, which will use the local computer name.
4143
MetricSendIntervalSeconds | The interval to send metrics to Carbon; I recommend 5 seconds or greater. The more metrics you are collecting the longer it takes to send them to the Graphite server. You can see how long it takes to send the metrics each time the loop runs by using running the `Start-StatsToGraphite` function and having *VerboseOutput* set to *True*.
4244
SendUsingUDP | Sends metrics via UDP instead of TCP.
4345

StatsToGraphiteConfig.xml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<CarbonServer>yourCarbonServerNameOrIP</CarbonServer>
55
<CarbonServerPort>2003</CarbonServerPort>
66
<MetricPath>datacenter1.servers</MetricPath>
7+
<NodeHostName>$env:COMPUTERNAME</NodeHostName>
78
<MetricSendIntervalSeconds>5</MetricSendIntervalSeconds>
89
<SendUsingUDP>False</SendUsingUDP>
910
</Graphite>

0 commit comments

Comments
 (0)