Windows + Powershell + Graphite = Awesomeness

Anyone that knows or works with me is very much aware of my love and excitement for Graphite.  At work we made the decision to use Graphite as our primary metrics solution moving away from a few paid and open-source solutions that weren’t scaling well.   Our environment consists of both Windows and Linux based OS’s.  Unfortunately most people end up sending metrics to Graphite from Linux based machines using tools like Collectd (another great tool!) and Windows is more or less forgotten.  So I set out to figure out how we could collect system metrics in Windows and then send them over to Graphite.  The solution: Powershell.  Powershell which is Microsoft’s premier scripting language can leverage almost any .net object to accomplish almost any task.

Here is a very simple example that can be leveraged to send almost anything from your windows box to a Graphite instance in your enviornment:

#Set the Graphite carbon server location and port number
$carbonServer = "graphitebox.mydomain.com"
$carbonServerPort = 2003
#Get Unix epoch Time
$epochTime=[int](Get-Date -UFormat "%s")

#Putting some value here that I want to send
$value = 1234

#Build our metric string in the format required by Graphite's carbon-cache service.
$metric = ("servers.mymachine.somemetric " + $value + " " + $epochTime)

#Stream results to the Carbon server
$socket = New-Object System.Net.Sockets.TCPClient 
$socket.connect($carbonServer, $carbonServerPort) 
$stream = $socket.GetStream() 
$writer = new-object System.IO.StreamWriter($stream)
#Write out metric to the stream.
$writer.WriteLine($metric)
$writer.Flush() #Flush and write our metrics.
$writer.Close() 
$stream.Close()

 

Using this code as a starting point you can build scripts to scrape any system metrics and send them to Graphite.

5 comments

Skip to comment form

    • Sam on May 23, 2013 at 2:05 am
    • Reply

    Extremely useful stuff, just what I was looking for to try and hook the Windows boxen into the rest of the stats.

    Is that a Gravis Ultrasound I see in that header image by any chance?

    • Arno on October 30, 2013 at 2:30 am
    • Reply

    Thanks, it works!

    • Nader on July 12, 2014 at 8:51 pm
    • Reply

    I have the most difficult time loading Graphite on Windows. I was able to get Whisper loaded but Carbon and Web-app are different stories.
    I am new to this and we are going to use this system to collect and monitor Solar Energy data.

    So far I have done the following:

    (Installed Python2.7, Easy_install, Pip, Django, and created a Django project) on my Windows7
    Toward installing Graphite (Whisper, Carbon, Graphite-Web) I was able to install:
    PythonGTK+ (for Cairo graphics), Py2cairo
    “pygtk-all-in-one-2.24.0.win32-py2.7(2).msi
    GitHub
    twisted using:

    pip install Twisted

    Installed zope.interface using:

    pip install zope.interface

    I need to spend more time figuring out the
    http://graphite.wikidot.com/installation instructions,

    I was able to install Whisper this morning (Saturday) and I am working on Carbon and Graphite-web.

    I have downloaded:
    whisper-0.9.10.tar – TAR archive, unpacked size 39,703 bytes
    carbon-0.9.10.tar.gz.tar – GZIP archive, unpacked size 215,040 bytes
    graphite-web-0.9.10.tar – TAR archive, unpacked size 8,551,594 bytes

    I think these are the correct files to be extracted and loaded (?).

    Josh, please help me to install this great system.

    Thanks

    Nader

  1. For those interested, I created a full PowerShell function library which sends Windows performance counters to Graphite: https://github.com/MattHodge/Graphite-PowerShell-Functions

    It can be run as a service, and has a simple XML configuration file.

    Y’all might find it helpful 🙂

    • Tyler on May 23, 2017 at 7:08 am
    • Reply

    Hi
    I tried this example, but I’ve got empty values in graphite. What I else should I know to get it work? I’m using windows10
    Thanks.

Leave a Reply to Nader Cancel reply

Your email address will not be published.