-
-
Save jmara/c40fb4ce51f60ea16c9d to your computer and use it in GitHub Desktop.
| #! /bin/sh | |
| ### BEGIN INIT INFO | |
| # Provides: varnishncsa | |
| # Required-Start: $local_fs $remote_fs $network | |
| # Required-Stop: $local_fs $remote_fs $network | |
| # Default-Start: 2 3 4 5 | |
| # Default-Stop: 0 1 6 | |
| # Short-Description: Start HTTP accelerator log daemon | |
| # Description: This script provides logging for varnish | |
| ### END INIT INFO | |
| # Source function library | |
| . /lib/lsb/init-functions | |
| NAME=varnishncsa | |
| DESC="HTTP accelerator log deamon" | |
| PATH=/sbin:/bin:/usr/sbin:/usr/bin | |
| DAEMON=/usr/bin/$NAME | |
| PIDFILE=/var/run/$NAME/$NAME.pid | |
| LOGFILE=/var/log/varnish/varnishncsa.log | |
| USER=varnishlog | |
| # Include defaults if available | |
| if [ -f /etc/default/$NAME ] ; then | |
| . /etc/default/$NAME | |
| fi | |
| DAEMON_OPTS="-a -w ${LOGFILE} -D -P ${PIDFILE} ${OPTS}" | |
| # If unset, or set to "0" or "no", exit | |
| if [ -z "${VARNISHNCSA_ENABLED}" ] || \ | |
| [ "${VARNISHNCSA_ENABLED}" = "0" ] || \ | |
| [ "${VARNISHNCSA_ENABLED}" = "no" ]; then | |
| exit 0; | |
| fi | |
| test -x $DAEMON || exit 0 | |
| start_varnishncsa() { | |
| output=$(/bin/tempfile -s.varnish) | |
| log_daemon_msg "Starting $DESC" "$NAME" | |
| create_pid_directory | |
| if start-stop-daemon --start --quiet --pidfile ${PIDFILE} \ | |
| --chuid $USER --exec ${DAEMON} -- ${DAEMON_OPTS} \ | |
| > ${output} 2>&1; then | |
| log_end_msg 0 | |
| else | |
| log_end_msg 1 | |
| cat $output | |
| exit 1 | |
| fi | |
| rm $output | |
| } | |
| stop_varnishncsa(){ | |
| log_daemon_msg "Stopping $DESC" "$NAME" | |
| if start-stop-daemon --stop --quiet --pidfile $PIDFILE \ | |
| --retry 10 --exec $DAEMON; then | |
| log_end_msg 0 | |
| else | |
| log_end_msg 1 | |
| fi | |
| } | |
| reload_varnishncsa(){ | |
| log_daemon_msg "Reloading $DESC" "$NAME" | |
| if kill -HUP $(cat $PIDFILE) >/dev/null 2>&1; then | |
| log_end_msg 0 | |
| else | |
| log_end_msg 1 | |
| exit 1 | |
| fi | |
| } | |
| status_varnishncsa(){ | |
| status_of_proc -p "${PIDFILE}" "${DAEMON}" "${NAME}" | |
| } | |
| create_pid_directory() { | |
| install -o $USER -g $USER -d $(dirname $PIDFILE) | |
| } | |
| case "$1" in | |
| start) | |
| start_varnishncsa | |
| ;; | |
| stop) | |
| stop_varnishncsa | |
| ;; | |
| reload) | |
| reload_varnishncsa | |
| ;; | |
| status) | |
| status_varnishncsa | |
| ;; | |
| restart|force-reload) | |
| $0 stop | |
| $0 start | |
| ;; | |
| *) | |
| log_success_msg "Usage: $0 {start|stop|restart|force-reload|reload}" | |
| exit 1 | |
| ;; | |
| esac | |
| exit 0 |
| ... | |
| + create_pid_directory | |
| ++ dirname /var/run/varnishncsa/varnishncsa.pid | |
| + install -o varnishlog -g varnishlog -d /var/run/varnishncsa | |
| + start-stop-daemon --start --quiet --pidfile /var/run/varnishncsa/varnishncsa.pid --chuid varnishlog --exec /usr/bin/varnishncsa -- -a -w /var/log/varnish/varnishncsa.log -D -P /var/run/varnishncsa/varnishncsa.pid -F ''\''%{X-Forwarded-For}i' %l %u %t '"%r"' %s %b '"%{Referer}i"' '"%{User-agent}i"'\''' | |
| + log_end_msg 1 | |
| + '[' -z 1 ']' | |
| + '[' 171 ']' | |
| ... |
| root@varnish:/etc/default# cat varnishncsa | |
| VARNISHNCSA_ENABLED=1 | |
| LOG_FORMAT="%{X-Forwarded-For}i %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\"" | |
| # Extra Options for the Init-Script | |
| OPTS="-F '${LOG_FORMAT}'" |
| --- varnishncsa.orig 2015-02-18 16:32:35.000000000 +0100 | |
| +++ varnishncsa 2015-06-24 08:42:25.219814047 +0200 | |
| @@ -21,6 +21,7 @@ | |
| LOGFILE=/var/log/varnish/varnishncsa.log | |
| USER=varnishlog | |
| DAEMON_OPTS="-a -w ${LOGFILE} -D -P ${PIDFILE}" | |
| +LOG_FORMAT="" | |
| # Include defaults if available | |
| if [ -f /etc/default/$NAME ] ; then | |
| @@ -40,14 +41,26 @@ | |
| output=$(/bin/tempfile -s.varnish) | |
| log_daemon_msg "Starting $DESC" "$NAME" | |
| create_pid_directory | |
| - if start-stop-daemon --start --quiet --pidfile ${PIDFILE} \ | |
| - --chuid $USER --exec ${DAEMON} -- ${DAEMON_OPTS} \ | |
| - > ${output} 2>&1; then | |
| - log_end_msg 0 | |
| + if [ -z "$LOG_FORMAT" ]; then | |
| + if start-stop-daemon --start --quiet --pidfile ${PIDFILE} \ | |
| + --chuid $USER --exec ${DAEMON} -- ${DAEMON_OPTS} \ | |
| + > ${output} 2>&1; then | |
| + log_end_msg 0 | |
| + else | |
| + log_end_msg 1 | |
| + cat $output | |
| + exit 1 | |
| + fi | |
| else | |
| - log_end_msg 1 | |
| - cat $output | |
| - exit 1 | |
| + if start-stop-daemon --start --quiet --pidfile ${PIDFILE} \ | |
| + --chuid $USER --exec ${DAEMON} -- ${DAEMON_OPTS} -F "${LOG_FORMAT}" \ | |
| + > ${output} 2>&1; then | |
| + log_end_msg 0 | |
| + else | |
| + log_end_msg 1 | |
| + cat $output | |
| + exit 1 | |
| + fi | |
| fi | |
| rm $output | |
| } |
So there are 2 problems. Single quotes arent supported in init.d. You cannot use double quotes since you use them within your parameter.
To fix, change line 45 in the init.d script to:
--chuid $USER --exec ${DAEMON} -- ${DAEMON_OPTS} -F "${FORMAT}" \
Then define FORMAT:
FORMAT="%{X-Forwarded-For}i %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\""
Hey,
thanks again for you comment :)
That makes the /etc/default/varnish kind of useless :-/ and I've to roll my own init-script through all the Varnish Updates, force upgrades and double check if something changed in the meantime.
For my use case its sad that the -f switch was removed in Varnish 4.0 which switched %h to %{X-Forwarded-For}i inside of varnishncsa. But nevetheless I'll stick to your recommendations and double check on new Varnish version ;)
Cheers,
Jan M.
The Problem is the string interpolation:
-F ''''%{X-Forwarded-For}i' %l %u %t '"%r"' %s %b '"%{Referer}i"' '"%{User-agent}i"''''
instead of
-F '%{X-Forwarded-For}i %l %u %t "%r" %s %b "%{Referer}i" "%{User-agent}i"'