• This repository has been archived on 30/May/2019
  • Stars
    star
    218
  • Rank 181,805 (Top 4 %)
  • Language
    PowerShell
  • License
    GNU General Publi...
  • Created almost 11 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A group of PowerShell functions that allow you to send Windows Performance counters to a Graphite Server, all configurable from a simple XML file.

Notice (30th July, 2016)

I am no longer maintaining this project as there is a far better tool to do this now called Telagraf, which supports sending to InfluxDB and Graphite.

You can a blog post on how to set it up here: https://hodgkins.io/windows-metric-dashboards-with-influxdb-and-grafana

Graphite PowerShell Functions

A group of PowerShell functions that allow you to send Windows Performance counters to a Graphite Server, all configurable from a simple XML file.

GitHub Version

More details at http://www.hodgkins.net.au/mswindows/using-powershell-to-send-metrics-graphite/

Features

  • Sends Metrics to Graphite's Carbon daemon using TCP or UDP
  • Can collect Windows Performance Counters
  • Can collect values by using T-SQL queries against MS SQL databases
  • Converts time to UTC on sending
  • All configuration can be done from a simple XML file
  • Allows you to override the hostname in Windows Performance Counters before sending on to Graphite
  • Allows renaming of metric names using regex via the configuration file
  • 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
  • Additional functions are exposed that allow you to send data to Graphite from PowerShell easily. Here is the list of included functions
  • Script can be installed to run as a service
  • Installable by Chef Cookbook which is available here
  • Installable by Puppet which is available here
  • Supports Hosted Graphite (https://www.hostedgraphite.com)

Installation

  1. Download the repository and place into a PowerShell Modules directory called Graphite-Powershell. The module directories can be found by running $env:PSModulePath in PowerShell. For example, C:\Program Files\WindowsPowerShell\Modules\Graphite-PowerShell
  2. Verify your folder structure looks like this, with the .psd1 and .psm1 files inside the Graphite-Powershell folder:

alt text 3. Make sure the files are un-blocked by right clicking on them and going to properties. 4. Modify the StatsToGraphiteConfig.xml configuration file. Instructions here. 5. Open PowerShell and ensure you set your Execution Policy to allow scripts be run. For example Set-ExecutionPolicy RemoteSigned.

Modifying the Configuration File

The configuration file is fairly self-explanatory, but here is a description for each of the values.

Graphite Configuration Section

Configuration Name Description
CarbonServer The server name where Carbon is running. The Carbon daemon is usually running on the Graphite server.
CarbonServerPort The port number for Carbon. Its default port number is 2003.
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.
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.
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.
SendUsingUDP Sends metrics via UDP instead of TCP.

Performance Counters Configuration Section

This section lists the performance counters you want the machine to send to Graphite. You can get these from Performance Monitor (perfmon.exe) or by using the command typeperf -qx in a command prompt.

I have included some basic performance counters in the configuration file. Asterisks can be used as a wildcard.

Here are some other examples:

  • <Counter Name="\Web Service(YourIISWebSite)\Total Bytes Received"/>
  • <Counter Name="\Web Service(YourIISWebSite)\Total Bytes Sent"/>
  • <Counter Name="\ASP.NET Apps v4.0.30319(_lm_w3svc_1_Root_YourIISApp)\Request Wait Time"/>
  • <Counter Name="\PhysicalDisk(*)\Avg. Disk Write Queue Length"/>

MetricReplace Configuration Section

This section lists regex find and replace rules used to clean up the Windows performance counters before sending them on to Graphite. Most people will be able to leave this section alone as it configured using sensible defaults.

The script processes these in the order they appear in the configuration file. Order is important!

There are two methods for replacing metrics with custom names:

  1. Replace with custom value. This does a regex search on This, and replaces it with the value in With. For example: <MetricReplace This="\#" With="num"/> This would find the # symbol and replace it with the text num
  2. Replace with RegEx capture group. This does a regex search on This, and then replaces it with the value in With. It then uses the RegEx capture that was found and substities it for #{CAPTUREGROUP}. This this will only work for the first capture. For example: <MetricReplace This="physicaldisk\(.* (.*)\:\)" With="physicaldisk.#{CAPTUREGROUP}-drive"/> This would capture the drive letter of the phyiscal disk, and replace the whole string with physicaldisk.DriverLetter-drive.

If you want to use the regex capture replace, I recommend doing this first, as it's easier to capture these sections before the metrics get cleaned.

Filtering Configuration Section

This section lists names you want to filter out of the Performance Counter list. Filtering is useful when you want to use a wildcard in the performance counter, but want to exclude some of the returned counters. I have included isatap and teredo tunneling by default to filter out IPv6 interfaces. Remove all tags if you want no filtering.

MSSQLMetics Configuration Section

This section allows you to configure the additional settings that will be used when running the Start-SQLStatsToGraphite command.

Configuration Name Description
MetricPath The path of the SQL metric you want to be sent to the server
MetricSendIntervalSeconds The interval to send SQL metrics to Carbon. I recommend 5 seconds or greater. The more queries you are running 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-SQLStatsToGraphite -Verbose -TestMode.
SQLConnectionTimeoutSeconds The time out period when attempting to connect to the SQL Server.
SQLQueryTimeoutSeconds The time out period when waiting for a SQL query to return.

The next section allows you to configure a list of SQL servers and the queries that will be run against those servers. You can add as many queries or servers as required. The only constraint is that they all need to be able to run within the time given by the MetricSendIntervalSeconds configuration value.

<SQLServer> Configuration Values Description
ServerInstance The hostname or Server Instance of the SQL server you want to connect to. SQL servers with instances can also be used.
Username The username to connect to SQL with using SQL Authentication. Leaving this and the Password option blank will make the script use Windows Authentication against the SQL Server. The current credentials that the PowerShell window are running under will be used.
Password The password to connect to SQL with using SQL Authentication. Leaving this and the Username option blank will make the script use Windows Authentication against the SQL Server. The current credentials that the PowerShell window are running under will be used.

The next part of the configuration allows you to add a list of the T-SQL queries that will be run against the SQL server.

<Query> Configuration Values Description
Database The database that the SQL query will be run against.
MetricName The Graphite metric name to use for this SQL query.
TSQL The T-SQL query to run against the SQL Server. If you need to use characters such as < or > in your query, you will need to replace them with the appropriate XML entity reference. For example, > would be replaced with &gt;. A full list of these can be found on MSDN.

There are a few important things to keep in mind when using this feature.

  • If you provide the SQL Username and Password options, they is stored in plain text in the configuration file. If you do not provide a username and password, the windows account that the PowerShell window is running under will be used against the SQL Server. This is a good way to protect the credentials.
  • There is no verification that the SQL command in the configuration file is not destructive. Be sure to use a low privilege account to authenticate against SQL so that any malicious T-SQL queries don't destroy your data.
  • If your T-SQL query returns multiple results, only the first one will be sent over to Graphite.

Logging Configuration Section

This section allows you to turn on or off Verbose output. This is useful when testing but is better left off when running as a service as you won't be able to see the output.

Configuration Name Description
VerboseOutput Will provide each of the metrics that were sent over to Carbon and the total execution time of the loop.

Usage - Windows Performance Counters

The following shows how to use the Start-StatsToGraphite, which will collect Windows performance counters and send them to Graphite.

  1. Open PowerShell
  2. Import the Module by running Import-Module -Name Graphite-PowerShell
  3. Start the script by using the function Start-StatsToGraphite. If you want Verbose details, use Start-StatsToGraphite -Verbose.

You may need to run the PowerShell instance with Administrative rights depending on the performance counters you want to access. This is due to the scripts use of the Get-Counter CmdLet.

From the Get-Counter help page on TechNet:

Performance counters are often protected by access control lists (ACLs). To get all available performance counters, open Windows PowerShell with the "Run as administrator" option.

The below image is what Start-StatsToGraphite like with VerboseOutput turned on in the XML configuration file looks like.

alt text

That is all there is to getting your Windows performance counters into Graphite.

Usage - SQL Query Results

The following shows how to use the Start-SQLStatsToGraphite, which will execute any SQL queries listed in the configuration file and send the result (which needs to be an integer) to Graphite.

  1. Open PowerShell
  2. Import the Module by running Import-Module -Name Graphite-PowerShell
  3. Start the script by using the function Start-SQLStatsToGraphite. If you want Verbose detailed use Start-SQLStatsToGraphite -Verbose. If you want to see what would be sent to Graphite, without actually sending the metrics, use Start-SQLStatsToGraphite -Verbose -TestMode

The below image is what Start-SQLStatsToGraphite like with VerboseOutput turned on in the XML configuration file looks like.

alt text

This function requires the Microsoft SQL PowerShell Modules/SnapIns. The easiest way to get these is to download them from the SQL 2012 R2 SP1 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

Installing as a Service

Once you have edited the configuration file and verified everything is functioning correctly by running either Start-StatsToGraphite or Start-SQLStatsToGraphite in an interactive PowerShell session, you might want to install one or both of these scripts as a service.

The easiest way to achieve this is using NSSM - the Non-Sucking Service Manager.

  1. Download nssm from nssm.cc
  2. Open up an Administrative command prompt and run nssm install GraphitePowerShell. (You can call the service whatever you want).
  3. A dialog will pop up allowing you to enter in settings for the new service. The following two tables below contains the settings to use.

alt text

  1. Click Install Service
  2. Make sure the service is started and it is set to Automatic
  3. Check your Graphite server and make sure the metrics are coming in

The below configurations will show how to run either Start-StatsToGraphite or Start-SQLStatsToGraphite as a service. If you want to run both on the same server, you will need to create two seperate services, one for each script.

Running Start-StatsToGraphite as a Service

The following configuration can be used to run Start-StatsToGraphite as a service.

Setting Name Value
Path C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Startup Directory Leave Blank
Options -command "& { Import-Module -Name Graphite-PowerShell ; Start-StatsToGraphite }"

Running Start-SQLStatsToGraphite as a Service

The following configuration can be used to run Start-SQLStatsToGraphite as a service.

Setting Name Value
Path C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Startup Directory Leave Blank
Options -command "& { Import-Module -Name Graphite-PowerShell ; Start-SQLStatsToGraphite }"

If you want to remove a service, read the NSSM documentation http://nssm.cc/commands for instructions.

Installing as a Service Using PowerShell

  1. Download nssm from nssm.cc and save it into a directory
  2. Open an Administrative PowerShell consolen and browse to the directory you saved NSSM
  3. Run Start-Process -FilePath .\nssm.exe -ArgumentList 'install GraphitePowerShell "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-command "& { Import-Module -Name Graphite-PowerShell ; Start-StatsToGraphite }"" ' -NoNewWindow -Wait
  4. Check the service installed successfully Get-Service -Name GraphitePowerShell
  5. Start the service Start-Service -Name GraphitePowerShell

Included Functions

There are several functions that are exposed by the module which are available to use in an ad-hoc manner.

For a list of functions in the module, run Get-Command -Module Graphite-PowerShell. For full help for these functions run Get-Help | <Function Name>

Function Name Description
ConvertTo-GraphiteMetric Takes the Windows Performance counter name and coverts it to something that Graphite can use.
Send-BulkGraphiteMetrics Sends several Graphite Metrics to a Carbon server with one request. Bulk requests save a lot of resources for Graphite server.
Send-GraphiteEvent Sends an event to Graphite using the Graphite Event API. More information about the events API can be found in this blog post.
Send-GraphiteMetric Allows you to send metrics to Graphite in an ad-hoc manner.
Start-SQLStatsToGraphite The function to query SQL. This is an endless loop which will send metrics to Graphite.
Start-StatsToGraphite The function to collect Windows Performance Counters. This is an endless loop which will send metrics to Graphite.

More Repositories

1

PackerTemplates

Packer Templates for building Windows Operating Systems
PowerShell
161
star
2

PoshHubot

PowerShell Module for Hubot
PowerShell
42
star
3

MattHodgePowerShell

Various PowerShell functions and scripts
PowerShell
28
star
4

terraform-provider-octopusdeploy

An Octopus Deploy Terraform Provider
Go
24
star
5

Hubot-DSC-Resource

PowerShell DSC Resource to install Hubot on Windows
PowerShell
21
star
6

ansible-testkitchen-windows

Running Ansible playbooks against Windows and testing with Pester
PowerShell
19
star
7

WindowsErrbot

Quick start for Errbot on Windows with PowerShell Integration
Python
17
star
8

PoshSensu

PoshSensu is a PowerShell module to handle running PowerShell scripts as Sensu checks. The results are submitted to a Sensu client via the client socket input feature that the Sensu client provides.
PowerShell
16
star
9

PagerDuty-PowerShell-CmdLets

PowerShell CmdLets for using the PagerDuty API.
PowerShell
16
star
10

go-teedy

A Go client library and CLI for accessing the API of Teedy.
Go
10
star
11

go-octopusdeploy

A Go wrapper for the Octopus Deploy REST API
Go
9
star
12

VagrantBoxes

Random VagrantBoxes
PowerShell
8
star
13

datadog-terraform-example

Example of DataDog Terraform Repo
HCL
8
star
14

HubotWindowsContainer

PowerShell
5
star
15

HubotWindows

A Hubot setup especially for Windows
CoffeeScript
5
star
16

splunk

Splunk Demos and Datafiles
Python
4
star
17

books

4
star
18

PSErrbot

PowerShell Errbot
Python
3
star
19

ansible-docker

Docker Container for running Ansible with support for WinRM
Dockerfile
3
star
20

awesome-datadog

A curated list of awesome DataDog libraries, open source repos, guides, blogs, and other resources.
2
star
21

AnsibleDemo

2
star
22

PSHubotDocker

JavaScript
2
star
23

go-fiber-templ-goth-aad-example

Go
2
star
24

docker_elk_with_redis

1
star
25

OctopusDeployAutomation

Cake Scripts to Automate Creation of Octopus Deploy Projects via YAML
C#
1
star
26

presentation_chatops_powershell

PowerShell
1
star
27

matthodge.github.io

Matthew Hodgkins Blog
CSS
1
star
28

ansible-role-mdt

Ruby
1
star
29

alexa

Alexa Projects
JavaScript
1
star
30

test-monorepo-auto

A monorepo to test intuit / auto
Shell
1
star