Sep 21

Remove all negative numbers from your graphite graph

I’ve written a few bash and powershell script that  scrape various processes or interfaces in an effort to capture a value and calculate a rate, then store than in whisper (graphite’s DB).  I found though that due to some corner cases my script logic sometimes calculates negative values and sends them to graphite.  If your have more than one negative value in your whisper file it can be quiet a job trying to zero out these data points.  So I came up with a simple bash command I wanted to share which zeros out all negative numbers:

sudo --from 0000000000 mymetric.wsp |grep -vi None|awk '$2 < 0{print " mymetric.wsp " $1 ":0"} '|sh

In the above line we simply run whisper-fetch to pull all data points from a whisper db called mymetric.wsp.  Then grep out all entries that have no value aka “None”. Then use awk to test each value to see if it’s less then zero.  Any values that ARE less then zero we print  the syntax needed to update the file using whisper-update and then pipe it to shell so it’s executed.

For fun, we can take the above statement one step further and wrap it in a for loop and use a find statement to remove negative numbers from a bunch of files that meet your search criteria:

sudo for file in $(find . -name somemetric.wsp); do --from 0000000000 $file |grep -vi None|awk -v f="$file" '$2 < 0{print " " f " "  $1 ":0"}'|sh; done

Aug 06

True windows process cpu utilization percentage using powershell

Recently I had to write a powershell script that grabbed the CPU percentage of a process running on one of our servers so we could trend it.  I figured this would be super simple task.  To my surprise this was quiet a challenge.  I tried several different approaches including get-process and get-wmiobject.  Most of the usual commands will return some value that’s either a process time, or a cpu percentage that’s not accurate unless you have a single core box.  After a little digging I figured it out, and since no one else out there has any really good example code I’m going to share with you my code:


#Define the process name
$ProcessName = "msiexec"
#Get the number of CPU cores
foreach ($proc in (Get-WmiObject  Win32_Processor)){if ($proc.numberofcores -eq $null){$cores++}else{$cores = $cores + $proc.numberofcores}}
#Get the process's perfmon counter for %Processor Time, divide by number of cores, and round to two decimal places 
[Math]::round(((((Get-Counter "\Process($ProcessName)\% Processor Time").Countersamples)[0].CookedValue)/$cores),2)


Jul 26

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 = ""
$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.Flush() #Flush and write our metrics.


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

May 05

First Post!

This is my first attempt at writing down my thoughts and ideas to share with others.   Hopefully people find information posted here useful.  So bare with me as I get the site setup and get some stuff posted.