When you run the varnishstat binary, you’ll a continuously updated list of counters. That nice from a human readable point of view, but from a code/automation point of view, this is not really working for us.

The command below outputs the value only once and doesn’t return a continuously updated list of values:

varnishstat -f SMA.s0.c_bytes -1

So the “-1” parameter will make sure the output will only be listed once. The “-f SMA.S0.c_bytes” parameter will only list the amount of used bytes.

This is the output:

SMA.s0.c_bytes 66144000 53471.30 Bytes allocated

This is a step in the right direction, but JSON would be an ideal output format. Varnishstat supports JSON output and it’s just a matter of adding a “-j” parameter:

I wrote it in Go

You might have noticed that I mentioned the term “Go”. Yes, I did write it in Go. Why? Because Go is cool, Go is powerful, Go is a language on the rise. Go is a modern compiled language that was originally invented at Google. It’s open source, it’s statically typed, it’s very fast (thanks to built-in concurrency mechanisms) and surprisingly easy to use. Go is a true systems language.

GoJi

Have I mentioned I’m using GoJi. Probably not. GoJi is a web micro-framework written in Go and I’m using it to quickly write fast and effective RESTful APIs. You can compare GoJi to micro-frameworks in other languages like:

Running the code

Before we run it, we need to make sure all external dependencies are present. To achieve that we run the following command:

go get

If you run the script using the go runtime, you need to make sure the Go runtime is installed on your production server. The upside is that you don’t need to compile your code for the specific architecture. The downside is that you need to install yet another piece of software on the production machine. This is how you do it:

go run varnishusage.go

The other option is to compile the code (including all of its dependencies) as a single static binary. That way, you don’t need to install any extra software on the server where the script will be running. But be sure to compile it for the right architecture. This could actually mean cross-compiling for a different architecture.

Compiling it for your current architecture can be done with the following command:

If the architecture or operating system on which you develop your code differs from the production environment, you’re going to have to cross-compile your code. If you’re using Go 1.5, it’ll be pretty easy:

env GOOS=linux GOARCH=amd64 go build varnishusage.go

The video below shows you how to do it in more detail:

And finally it’s a matter of performing an HTTP call on port 8000 of your Varnish machine to retrieve the usage