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 whisper-fetch.py --from 0000000000 mymetric.wsp |grep -vi None|awk '$2 < 0{print "whisper-update.py 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 whisper-fetch.py --from 0000000000 $file |grep -vi None|awk -v f="$file" '$2 < 0{print "whisper-update.py " f " "  $1 ":0"}'|sh; done

Leave a Reply

Your email address will not be published.