source s_haproxy_hue { unix-dgram("/var/lib/adh-haproxy/hue/dev/log"); };

destination d_haproxy_hue {
     file("/var/log/adh-haproxy/haproxy-hue.$YEAR-$MONTH-$DAY.log"
          template("$DATE $FULLHOST $PROGRAM: ${HAPROXY.CLIENT_IPPORT} \[${HAPROXY.DATE}\] ${HAPROXY.FRONTEND} ${HAPROXY.BACKEND} ${HAPROXY.TIME} ${HAPROXY.STATUS_CODE} ${HAPROXY.BYTES_READ} ${HAPROXY.COOKIE_REQ} ${HAPROXY.COOKIE_RESP} ${HAPROXY.TERM_STATE} ${HAPROXY.RUN_STATE} ${HAPROXY.QUEUE_STATE} \{${HAPROXY.HEADERS_REQ}\} \"${HAPROXY.METHOD} ${HAPROXY.FRONTEND_PROTOCOL}://${HAPROXY.HOST}${HAPROXY.URL} ${HAPROXY.HTTP_VERSION}\"\n")
          group(adm) perm(0640) dir_perm(0750) template_escape(no)
         );
};

filter f_haproxy_hue { program("haproxy_hue"); };
filter f_haproxy_hue_ssl { match("~ "); };

rewrite r_haproxy_hue_set_frontend_protocol {
  set("http", value("HAPROXY.FRONTEND_PROTOCOL") condition(filter(f_haproxy_hue)));
  set("https", value("HAPROXY.FRONTEND_PROTOCOL") condition(filter(f_haproxy_hue_ssl)));
};

parser p_haproxy_hue {
  csv-parser(columns("HAPROXY.CLIENT_IPPORT", "HAPROXY.DATE",
                     "HAPROXY.FRONTEND", "HAPROXY.BACKEND",
                     "HAPROXY.TIME", "HAPROXY.STATUS_CODE",
                     "HAPROXY.BYTES_READ", "HAPROXY.COOKIE_REQ",
                     "HAPROXY.COOKIE_RESP", "HAPROXY.TERM_STATE",
                     "HAPROXY.RUN_STATE", "HAPROXY.QUEUE_STATE",
                     "HAPROXY.HEADERS_REQ", "HAPROXY.REQUEST")
             flags(escape-double-char,strip-whitespace)
             delimiters(" ")
             quote-pairs('""[]{}'));
};

parser p_haproxy_hue_request {
  csv-parser(columns("HAPROXY.METHOD", "HAPROXY.URL",
                     "HAPROXY.HTTP_VERSION")
             delimiters(" ")
             flags(escape-none)
             template("${HAPROXY.REQUEST}"));
};

parser p_haproxy_hue_headers_req {
  csv-parser(columns("HAPROXY.HOST", "HAPROXY.USER_AGENT")
             delimiters("|")
             flags(escape-none)
             template("${HAPROXY.HEADERS_REQ}"));
};

log {
  source(s_haproxy_hue);
  filter(f_haproxy_hue);
  parser(p_haproxy_hue);
  parser(p_haproxy_hue_request);
  parser(p_haproxy_hue_headers_req);
  rewrite(r_haproxy_hue_set_frontend_protocol);
  destination(d_haproxy_hue);
};
