Sending metrics to StatsD with a Shell One-liner

Etsy’s great post about tracking every release has some good tips about tracking releases with statsd and graphite (including some essential graphite config tweaks). I was wondering how to do this from within a shell script, and I had to dig through lots of StatsD code and examples to find this snippet. I forget where I eventually found it, and thought it’d make it easier to find.

Deploy scripts are just one place where a concise and safe way to record a metric/event in important. From Etsy’s blog, using vertical lines to represent distinct events (code deployments) to give more context to the login trends:

Sending a metric from the command line, with netcat or curl, is just one bit of ‘glue’ that is essential for pulling together a complete metrics solution. This is essentially the base-case, the lowest-common-denominator that all metrics collection can be built upon.

While short-and-sweet, this snippit contains two measures of protection. First, it limits the run duration to a single second so it don’t have to worry about it hanging, and secondly, it uses UDP, so you don’t need to worry about the remote server being performant or even up. True fire-and-forget, so you can recklessly drop this in scripts left and right.

And the bash one-liner:

What metrics can you collect with a one-liner? Add some in the comments.

5 thoughts on “Sending metrics to StatsD with a Shell One-liner

  1. Cherian,

    Graphite supports plotting multiple graphs out-of-the-box, simply click on multiple different metrics and they will be graphed. The graphite docs do need lots of work, but start playing around and see what you can get going. The #graphite IRC room is a good place to ask questions.

  2. Monitor the size of your postfix mail queues:

    for i in maildrop hold incoming active deferred; do echo “postfix.queues.${i} `find /var/spool/postfix/${i} -type f | wc -l` `date +%s`”; done | nc -w 1 -u graphite.example.com 8125

  3. To avoid statsd complaining about an extra newline (Bad line: 1 in msg “”), add the -n option to your echo command.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>