VarnishNCSA LogFormat hackery

Varnishncsa is the logging component of the Varnish web cache software (https://www.varnish-cache.org/). Sadly it doesn't care for reading a config file, so the only way to configure the format of the log lines is on the command line. The trouble with *that* is spaces and quotes. Sure, on Debian/Ubuntu you have /etc/default/varnishncsa, and you can set "$DAEMONOPTS" to add -F . But because this log format will typically contain spaces, the quoting needs to be just so and as far as I've been able to determine, by the time it's been set through $DAEMONOPTS, there's either no way to get the quoting right, or it's too complex for a simple sysadmin like me to figure out (and according to my web searching, many smarter people have tried that approach and failed). So up until now, we've been doing bad things and editing /etc/init.d/varnishnsca, changing the line that starts it to end with:

${DAEMON_OPTS} -F "%h ..."

i.e. adding the -F and then the log format quoted with double quotes. This ensures that the entire log format gets passed to varnishncsa as a single argument.

After fighting this again today hoping I could find a way of quoting it 'just so', I came up with an alternative approach. In /etc/default/varnishncsa:


LOG_FORMAT="%h ..."
#Only change DAEMON at start; the wrapper will finish leaving just the varnishncsa process, and DAEMON needs to be the
# original binary during stop for start-stop-daemon to find it.
if [ $1 = "start" ]; then
DAEMON=/usr/local/sbin/varnishncsa_wrapper.sh
fi

Then create /usr/local/sbin/varnishncsa_wrapper.sh:

#!/bin/bash
. /etc/default/varnishncsa
/usr/bin/varnishncsa $* -F"${LOG_FORMAT}"

It's a hit of a hack, but it does mean that we can leave /etc/init.d/varnishncsa the way God & the Debian packagers intended, and only mess with the files we should.