Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * General logging functions. |
| 3 | * |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 14 | #include <fcntl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 15 | #include <stdarg.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
| 20 | #include <time.h> |
| 21 | #include <unistd.h> |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 22 | #include <errno.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 23 | |
| 24 | #include <sys/time.h> |
| 25 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 26 | #include <common/config.h> |
Willy Tarreau | d6d0690 | 2009-08-19 11:22:33 +0200 | [diff] [blame] | 27 | #include <common/compat.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/standard.h> |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 29 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <types/global.h> |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 32 | #include <types/log.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 33 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 34 | #include <proto/frontend.h> |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 35 | #include <proto/proto_http.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 36 | #include <proto/log.h> |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 37 | #include <proto/sample.h> |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 38 | #include <proto/stream.h> |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 39 | #include <proto/stream_interface.h> |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 40 | #ifdef USE_OPENSSL |
| 41 | #include <proto/ssl_sock.h> |
| 42 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 44 | struct log_fmt { |
| 45 | char *name; |
| 46 | struct { |
| 47 | struct chunk sep1; /* first pid separator */ |
| 48 | struct chunk sep2; /* second pid separator */ |
| 49 | } pid; |
| 50 | }; |
| 51 | |
| 52 | static const struct log_fmt log_formats[LOG_FORMATS] = { |
| 53 | [LOG_FORMAT_RFC3164] = { |
| 54 | .name = "rfc3164", |
| 55 | .pid = { |
| 56 | .sep1 = { .str = "[", .len = 1 }, |
| 57 | .sep2 = { .str = "]: ", .len = 3 } |
| 58 | } |
| 59 | }, |
| 60 | [LOG_FORMAT_RFC5424] = { |
| 61 | .name = "rfc5424", |
| 62 | .pid = { |
| 63 | .sep1 = { .str = " ", .len = 1 }, |
| 64 | .sep2 = { .str = " - ", .len = 3 } |
| 65 | } |
| 66 | } |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 70 | "kern", "user", "mail", "daemon", |
| 71 | "auth", "syslog", "lpr", "news", |
| 72 | "uucp", "cron", "auth2", "ftp", |
| 73 | "ntp", "audit", "alert", "cron2", |
| 74 | "local0", "local1", "local2", "local3", |
| 75 | "local4", "local5", "local6", "local7" |
| 76 | }; |
| 77 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | const char *log_levels[NB_LOG_LEVELS] = { |
| 79 | "emerg", "alert", "crit", "err", |
| 80 | "warning", "notice", "info", "debug" |
| 81 | }; |
| 82 | |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 83 | const char sess_term_cond[16] = "-LcCsSPRIDKUIIII"; /* normal, Local, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed, Up, -- */ |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 84 | const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 85 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 86 | |
| 87 | /* log_format */ |
| 88 | struct logformat_type { |
| 89 | char *name; |
| 90 | int type; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 91 | int mode; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 92 | int lw; /* logwait bitsfield */ |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 93 | int (*config_callback)(struct logformat_node *node, struct proxy *curproxy); |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 94 | const char *replace_by; /* new option to use instead of old one */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 97 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy); |
| 98 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 99 | /* log_format variable names */ |
| 100 | static const struct logformat_type logformat_keywords[] = { |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 101 | { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 102 | |
| 103 | /* please keep these lines sorted ! */ |
| 104 | { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */ |
| 105 | { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */ |
| 106 | { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */ |
| 107 | { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */ |
| 108 | { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */ |
Willy Tarreau | 4bf9963 | 2014-06-13 12:21:40 +0200 | [diff] [blame] | 109 | { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 110 | { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 111 | { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */ |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 112 | { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 113 | { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 114 | { "Tr", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 115 | { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 116 | { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 117 | { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */ |
| 118 | { "U", LOG_FMT_BYTES_UP, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from client to server */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 119 | { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 120 | { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 121 | { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 122 | { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */ |
| 123 | { "bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source port */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 124 | { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 125 | { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL }, /* client ip */ |
| 126 | { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL }, /* client port */ |
| 127 | { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */ |
| 128 | { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */ |
| 129 | { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend ip */ |
| 130 | { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL }, /* frontend port */ |
| 131 | { "ft", LOG_FMT_FRONTEND_XPRT, PR_MODE_TCP, LW_INIT, NULL }, /* frontend with transport mode */ |
Willy Tarreau | d9ed3d2 | 2014-06-13 12:23:06 +0200 | [diff] [blame] | 132 | { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */ |
| 133 | { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */ |
| 134 | { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */ |
| 135 | { "hsl", LOG_FMT_HDRRESPONSLIST, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response list */ |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 136 | { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */ |
| 137 | { "HP", LOG_FMT_HTTP_PATH, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP path */ |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 138 | { "HQ", LOG_FMT_HTTP_QUERY, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP query */ |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 139 | { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */ |
| 140 | { "HV", LOG_FMT_HTTP_VERSION, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP version */ |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 141 | { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 142 | { "ms", LOG_FMT_MS, PR_MODE_TCP, LW_INIT, NULL }, /* accept date millisecond */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 143 | { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 144 | { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */ |
| 145 | { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */ |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 146 | { "rt", LOG_FMT_COUNTER, PR_MODE_TCP, LW_REQ, NULL }, /* request counter (HTTP or TCP session) */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 147 | { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */ |
| 148 | { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */ |
| 149 | { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */ |
| 150 | { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */ |
| 151 | { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 152 | { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */ |
| 153 | { "sslv", LOG_FMT_SSL_VERSION, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL protocol version */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 154 | { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */ |
| 155 | { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */ |
| 156 | { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */ |
| 157 | |
| 158 | /* The following tags are deprecated and will be removed soon */ |
| 159 | { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */ |
| 160 | { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */ |
| 161 | { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP, NULL, "ci" }, /* client ip */ |
| 162 | { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP, NULL, "cp" }, /* client port */ |
| 163 | { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP, NULL, "fi" }, /* frontend ip */ |
| 164 | { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP, NULL, "fp" }, /* frontend port */ |
| 165 | { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */ |
| 166 | { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */ |
| 167 | { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */ |
| 168 | { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */ |
| 169 | { "st", LOG_FMT_STATUS, PR_MODE_HTTP, LW_RESP, NULL, "ST" }, /* status code */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 170 | { 0, 0, 0, 0, NULL } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 171 | }; |
| 172 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 173 | char default_http_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tq/%Tw/%Tc/%Tr/%Tt %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format |
| 174 | char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%T] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %Tq %Tw %Tc %Tr %Tt %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl"; |
| 175 | char default_tcp_log_format[] = "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq"; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 176 | char *log_format = NULL; |
| 177 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 178 | /* Default string used for structured-data part in RFC5424 formatted |
| 179 | * syslog messages. |
| 180 | */ |
| 181 | char default_rfc5424_sd_log_format[] = "- "; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 182 | |
| 183 | /* This is a global syslog header, common to all outgoing messages in |
| 184 | * RFC3164 format. It begins with time-based part and is updated by |
| 185 | * update_log_hdr(). |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 186 | */ |
| 187 | char *logheader = NULL; |
| 188 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 189 | /* This is a global syslog header for messages in RFC5424 format. It is |
| 190 | * updated by update_log_hdr_rfc5424(). |
| 191 | */ |
| 192 | char *logheader_rfc5424 = NULL; |
| 193 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 194 | /* This is a global syslog message buffer, common to all outgoing |
| 195 | * messages. It contains only the data part. |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 196 | */ |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 197 | char *logline = NULL; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 198 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 199 | /* A global syslog message buffer, common to all RFC5424 syslog messages. |
| 200 | * Currently, it is used for generating the structured-data part. |
| 201 | */ |
| 202 | char *logline_rfc5424 = NULL; |
| 203 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 204 | struct logformat_var_args { |
| 205 | char *name; |
| 206 | int mask; |
| 207 | }; |
| 208 | |
| 209 | struct logformat_var_args var_args_list[] = { |
| 210 | // global |
| 211 | { "M", LOG_OPT_MANDATORY }, |
| 212 | { "Q", LOG_OPT_QUOTE }, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 213 | { "X", LOG_OPT_HEXA }, |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 214 | { 0, 0 } |
| 215 | }; |
| 216 | |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 217 | /* return the name of the directive used in the current proxy for which we're |
| 218 | * currently parsing a header, when it is known. |
| 219 | */ |
| 220 | static inline const char *fmt_directive(const struct proxy *curproxy) |
| 221 | { |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 222 | switch (curproxy->conf.args.ctx) { |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 223 | case ARGC_ACL: |
| 224 | return "acl"; |
| 225 | case ARGC_STK: |
| 226 | return "stick"; |
| 227 | case ARGC_TRK: |
| 228 | return "track-sc"; |
| 229 | case ARGC_LOG: |
| 230 | return "log-format"; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 231 | case ARGC_LOGSD: |
| 232 | return "log-format-sd"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 233 | case ARGC_HRQ: |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 234 | return "http-request"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 235 | case ARGC_HRS: |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 236 | return "http-response"; |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 237 | case ARGC_UIF: |
| 238 | return "unique-id-format"; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 239 | case ARGC_RDR: |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 240 | return "redirect"; |
| 241 | case ARGC_CAP: |
| 242 | return "capture"; |
Willy Tarreau | 28d976d | 2015-07-09 11:39:33 +0200 | [diff] [blame] | 243 | case ARGC_SRV: |
| 244 | return "server"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 245 | default: |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 246 | return "undefined(please report this bug)"; /* must never happen */ |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 247 | } |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 248 | } |
| 249 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 250 | /* |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 251 | * callback used to configure addr source retrieval |
| 252 | */ |
| 253 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy) |
| 254 | { |
| 255 | curproxy->options2 |= PR_O2_SRC_ADDR; |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | /* |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 262 | * Parse args in a logformat_var |
| 263 | */ |
| 264 | int parse_logformat_var_args(char *args, struct logformat_node *node) |
| 265 | { |
| 266 | int i = 0; |
| 267 | int end = 0; |
| 268 | int flags = 0; // 1 = + 2 = - |
| 269 | char *sp = NULL; // start pointer |
| 270 | |
| 271 | if (args == NULL) |
| 272 | return 1; |
| 273 | |
| 274 | while (1) { |
| 275 | if (*args == '\0') |
| 276 | end = 1; |
| 277 | |
| 278 | if (*args == '+') { |
| 279 | // add flag |
| 280 | sp = args + 1; |
| 281 | flags = 1; |
| 282 | } |
| 283 | if (*args == '-') { |
| 284 | // delete flag |
| 285 | sp = args + 1; |
| 286 | flags = 2; |
| 287 | } |
| 288 | |
| 289 | if (*args == '\0' || *args == ',') { |
| 290 | *args = '\0'; |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 291 | for (i = 0; sp && var_args_list[i].name; i++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 292 | if (strcmp(sp, var_args_list[i].name) == 0) { |
| 293 | if (flags == 1) { |
| 294 | node->options |= var_args_list[i].mask; |
| 295 | break; |
| 296 | } else if (flags == 2) { |
| 297 | node->options &= ~var_args_list[i].mask; |
| 298 | break; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | sp = NULL; |
| 303 | if (end) |
| 304 | break; |
| 305 | } |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 306 | args++; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | /* |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 312 | * Parse a variable '%varname' or '%{args}varname' in log-format. The caller |
| 313 | * must pass the args part in the <arg> pointer with its length in <arg_len>, |
| 314 | * and varname with its length in <var> and <var_len> respectively. <arg> is |
| 315 | * ignored when arg_len is 0. Neither <var> nor <var_len> may be null. |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 316 | */ |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 317 | int parse_logformat_var(char *arg, int arg_len, char *var, int var_len, struct proxy *curproxy, struct list *list_format, int *defoptions) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 318 | { |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 319 | int j; |
| 320 | struct logformat_node *node; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 321 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 322 | for (j = 0; logformat_keywords[j].name; j++) { // search a log type |
| 323 | if (strlen(logformat_keywords[j].name) == var_len && |
| 324 | strncmp(var, logformat_keywords[j].name, var_len) == 0) { |
| 325 | if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) { |
| 326 | node = calloc(1, sizeof(struct logformat_node)); |
| 327 | node->type = logformat_keywords[j].type; |
| 328 | node->options = *defoptions; |
| 329 | if (arg_len) { |
| 330 | node->arg = my_strndup(arg, arg_len); |
| 331 | parse_logformat_var_args(node->arg, node); |
| 332 | } |
| 333 | if (node->type == LOG_FMT_GLOBAL) { |
| 334 | *defoptions = node->options; |
| 335 | free(node->arg); |
| 336 | free(node); |
| 337 | } else { |
| 338 | if (logformat_keywords[j].config_callback && |
| 339 | logformat_keywords[j].config_callback(node, curproxy) != 0) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 340 | return -1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 341 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 342 | curproxy->to_log |= logformat_keywords[j].lw; |
| 343 | LIST_ADDQ(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 344 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 345 | if (logformat_keywords[j].replace_by) |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 346 | Warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n", |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 347 | curproxy->conf.args.file, curproxy->conf.args.line, |
| 348 | logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 349 | return 0; |
| 350 | } else { |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 351 | Warning("parsing [%s:%d] : '%s' : format variable '%s' is reserved for HTTP mode.\n", |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 352 | curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), |
| 353 | logformat_keywords[j].name); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 354 | return -1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 355 | } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 356 | } |
| 357 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 358 | |
| 359 | j = var[var_len]; |
| 360 | var[var_len] = 0; |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 361 | Warning("parsing [%s:%d] : no such format variable '%s' in '%s'. If you wanted to emit the '%%' character verbatim, you need to use '%%%%' in log-format expressions.\n", |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 362 | curproxy->conf.args.file, curproxy->conf.args.line, var, fmt_directive(curproxy)); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 363 | var[var_len] = j; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * push to the logformat linked list |
| 369 | * |
| 370 | * start: start pointer |
| 371 | * end: end text pointer |
| 372 | * type: string type |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 373 | * list_format: destination list |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 374 | * |
| 375 | * LOG_TEXT: copy chars from start to end excluding end. |
| 376 | * |
| 377 | */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 378 | void add_to_logformat_list(char *start, char *end, int type, struct list *list_format) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 379 | { |
| 380 | char *str; |
| 381 | |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 382 | if (type == LF_TEXT) { /* type text */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 383 | struct logformat_node *node = calloc(1, sizeof(struct logformat_node)); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 384 | str = calloc(end - start + 1, 1); |
| 385 | strncpy(str, start, end - start); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 386 | str[end - start] = '\0'; |
| 387 | node->arg = str; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 388 | node->type = LOG_FMT_TEXT; // type string |
| 389 | LIST_ADDQ(list_format, &node->list); |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 390 | } else if (type == LF_SEPARATOR) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 391 | struct logformat_node *node = calloc(1, sizeof(struct logformat_node)); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 392 | node->type = LOG_FMT_SEPARATOR; |
| 393 | LIST_ADDQ(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | /* |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 398 | * Parse the sample fetch expression <text> and add a node to <list_format> upon |
| 399 | * success. At the moment, sample converters are not yet supported but fetch arguments |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 400 | * should work. The curpx->conf.args.ctx must be set by the caller. |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 401 | */ |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 402 | void add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap, const char *file, int line) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 403 | { |
| 404 | char *cmd[2]; |
| 405 | struct sample_expr *expr; |
| 406 | struct logformat_node *node; |
| 407 | int cmd_arg; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 408 | char *errmsg = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 409 | |
| 410 | cmd[0] = text; |
| 411 | cmd[1] = ""; |
| 412 | cmd_arg = 0; |
| 413 | |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 414 | expr = sample_parse_expr(cmd, &cmd_arg, file, line, &errmsg, &curpx->conf.args); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 415 | if (!expr) { |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 416 | Warning("parsing [%s:%d] : '%s' : sample fetch <%s> failed with : %s\n", |
| 417 | curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx), |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 418 | text, errmsg); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
| 422 | node = calloc(1, sizeof(struct logformat_node)); |
| 423 | node->type = LOG_FMT_EXPR; |
| 424 | node->expr = expr; |
| 425 | node->options = options; |
| 426 | |
| 427 | if (arg_len) { |
| 428 | node->arg = my_strndup(arg, arg_len); |
| 429 | parse_logformat_var_args(node->arg, node); |
| 430 | } |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 431 | if (expr->fetch->val & cap & SMP_VAL_REQUEST) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 432 | node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */ |
| 433 | |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 434 | if (expr->fetch->val & cap & SMP_VAL_RESPONSE) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 435 | node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */ |
| 436 | |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 437 | if (!(expr->fetch->val & cap)) |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 438 | Warning("parsing [%s:%d] : '%s' : sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here.\n", |
| 439 | curpx->conf.args.file, curpx->conf.args.line, fmt_directive(curpx), |
| 440 | text, sample_src_names(expr->fetch->use)); |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 441 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 442 | /* check if we need to allocate an hdr_idx struct for HTTP parsing */ |
| 443 | /* Note, we may also need to set curpx->to_log with certain fetches */ |
Willy Tarreau | 25320b2 | 2013-03-24 07:22:08 +0100 | [diff] [blame] | 444 | curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 445 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 446 | /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags |
| 447 | * needed with some sample fetches (eg: ssl*). We always set it for |
| 448 | * now on, but this will leave with sample capabilities soon. |
Willy Tarreau | 1f31c73 | 2013-01-10 16:22:27 +0100 | [diff] [blame] | 449 | */ |
| 450 | curpx->to_log |= LW_XPRT; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 451 | curpx->to_log |= LW_REQ; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 452 | LIST_ADDQ(list_format, &node->list); |
| 453 | } |
| 454 | |
| 455 | /* |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 456 | * Parse the log_format string and fill a linked list. |
| 457 | * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 458 | * You can set arguments using { } : %{many arguments}varname. |
| 459 | * The curproxy->conf.args.ctx must be set by the caller. |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 460 | * |
| 461 | * str: the string to parse |
| 462 | * curproxy: the proxy affected |
| 463 | * list_format: the destination list |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 464 | * options: LOG_OPT_* to force on every node |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 465 | * cap: all SMP_VAL_* flags supported by the consumer |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 466 | */ |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 467 | void parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, const char *file, int line) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 468 | { |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 469 | char *sp, *str, *backfmt; /* start pointer for text parts */ |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 470 | char *arg = NULL; /* start pointer for args */ |
| 471 | char *var = NULL; /* start pointer for vars */ |
| 472 | int arg_len = 0; |
| 473 | int var_len = 0; |
| 474 | int cformat; /* current token format */ |
| 475 | int pformat; /* previous token format */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 476 | struct logformat_node *tmplf, *back; |
| 477 | |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 478 | sp = str = backfmt = strdup(fmt); |
William Lallemand | 1dc00ef | 2012-08-09 16:41:35 +0200 | [diff] [blame] | 479 | curproxy->to_log |= LW_INIT; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 480 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 481 | /* flush the list first. */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 482 | list_for_each_entry_safe(tmplf, back, list_format, list) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 483 | LIST_DEL(&tmplf->list); |
| 484 | free(tmplf); |
| 485 | } |
| 486 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 487 | for (cformat = LF_INIT; cformat != LF_END; str++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 488 | pformat = cformat; |
| 489 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 490 | if (!*str) |
| 491 | cformat = LF_END; // preset it to save all states from doing this |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 492 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 493 | /* The prinicple of the two-step state machine below is to first detect a change, and |
| 494 | * second have all common paths processed at one place. The common paths are the ones |
| 495 | * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END). |
| 496 | * We use the common LF_INIT state to dispatch to the different final states. |
| 497 | */ |
| 498 | switch (pformat) { |
| 499 | case LF_STARTVAR: // text immediately following a '%' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 500 | arg = NULL; var = NULL; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 501 | arg_len = var_len = 0; |
| 502 | if (*str == '{') { // optional argument |
| 503 | cformat = LF_STARG; |
| 504 | arg = str + 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 505 | } |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 506 | else if (*str == '[') { |
| 507 | cformat = LF_STEXPR; |
| 508 | var = str + 1; // store expr in variable name |
| 509 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 510 | else if (isalpha((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 511 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 512 | var = str; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 513 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 514 | else if (*str == '%') |
| 515 | cformat = LF_TEXT; // convert this character to a litteral (useful for '%') |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 516 | else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') { |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 517 | /* single '%' followed by blank or digit, send them both */ |
| 518 | cformat = LF_TEXT; |
| 519 | pformat = LF_TEXT; /* finally we include the previous char as well */ |
| 520 | sp = str - 1; /* send both the '%' and the current char */ |
| 521 | Warning("parsing [%s:%d] : Fixed missing '%%' before '%c' at position %d in %s line : '%s'. Please use '%%%%' when you need the '%%' character in a log-format expression.\n", |
| 522 | curproxy->conf.args.file, curproxy->conf.args.line, *str, (int)(str - backfmt), fmt_directive(curproxy), fmt); |
| 523 | |
| 524 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 525 | else |
| 526 | cformat = LF_INIT; // handle other cases of litterals |
| 527 | break; |
| 528 | |
| 529 | case LF_STARG: // text immediately following '%{' |
| 530 | if (*str == '}') { // end of arg |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 531 | cformat = LF_EDARG; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 532 | arg_len = str - arg; |
| 533 | *str = 0; // used for reporting errors |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 534 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 535 | break; |
| 536 | |
| 537 | case LF_EDARG: // text immediately following '%{arg}' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 538 | if (*str == '[') { |
| 539 | cformat = LF_STEXPR; |
| 540 | var = str + 1; // store expr in variable name |
| 541 | break; |
| 542 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 543 | else if (isalnum((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 544 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 545 | var = str; |
| 546 | break; |
| 547 | } |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 548 | Warning("parsing [%s:%d] : Skipping isolated argument in '%s' line : '%%{%s}'\n", |
| 549 | curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), arg); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 550 | cformat = LF_INIT; |
| 551 | break; |
| 552 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 553 | case LF_STEXPR: // text immediately following '%[' |
| 554 | if (*str == ']') { // end of arg |
| 555 | cformat = LF_EDEXPR; |
| 556 | var_len = str - var; |
| 557 | *str = 0; // needed for parsing the expression |
| 558 | } |
| 559 | break; |
| 560 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 561 | case LF_VAR: // text part of a variable name |
| 562 | var_len = str - var; |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 563 | if (!isalnum((unsigned char)*str)) |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 564 | cformat = LF_INIT; // not variable name anymore |
| 565 | break; |
| 566 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 567 | default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 568 | cformat = LF_INIT; |
| 569 | } |
| 570 | |
| 571 | if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */ |
| 572 | switch (*str) { |
| 573 | case '%': cformat = LF_STARTVAR; break; |
| 574 | case ' ': cformat = LF_SEPARATOR; break; |
| 575 | case 0 : cformat = LF_END; break; |
| 576 | default : cformat = LF_TEXT; break; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 580 | if (cformat != pformat || pformat == LF_SEPARATOR) { |
| 581 | switch (pformat) { |
| 582 | case LF_VAR: |
| 583 | parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options); |
| 584 | break; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 585 | case LF_STEXPR: |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 586 | add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, file, line); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 587 | break; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 588 | case LF_TEXT: |
| 589 | case LF_SEPARATOR: |
| 590 | add_to_logformat_list(sp, str, pformat, list_format); |
| 591 | break; |
| 592 | } |
| 593 | sp = str; /* new start of text at every state switch and at every separator */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 594 | } |
| 595 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 596 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 597 | if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 598 | Warning("parsing [%s:%d] : Ignoring end of truncated '%s' line after '%s'\n", |
| 599 | curproxy->conf.args.file, curproxy->conf.args.line, fmt_directive(curproxy), |
| 600 | var ? var : arg ? arg : "%"); |
Willy Tarreau | 3ed22a4 | 2012-12-23 17:29:07 +0100 | [diff] [blame] | 601 | |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 602 | free(backfmt); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 603 | } |
| 604 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 605 | /* |
| 606 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 607 | * mode during startup. |
| 608 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 609 | void Alert(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 610 | { |
| 611 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 612 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 613 | |
| 614 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 615 | va_start(argp, fmt); |
| 616 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 617 | get_localtime(date.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 618 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 619 | tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid()); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 620 | vfprintf(stderr, fmt, argp); |
| 621 | fflush(stderr); |
| 622 | va_end(argp); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | |
| 627 | /* |
| 628 | * Displays the message on stderr with the date and pid. |
| 629 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 630 | void Warning(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 631 | { |
| 632 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 633 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 634 | |
| 635 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 636 | va_start(argp, fmt); |
| 637 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 638 | get_localtime(date.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 639 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 640 | tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid()); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 641 | vfprintf(stderr, fmt, argp); |
| 642 | fflush(stderr); |
| 643 | va_end(argp); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * Displays the message on <out> only if quiet mode is not set. |
| 649 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 650 | void qfprintf(FILE *out, const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 651 | { |
| 652 | va_list argp; |
| 653 | |
| 654 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 655 | va_start(argp, fmt); |
| 656 | vfprintf(out, fmt, argp); |
| 657 | fflush(out); |
| 658 | va_end(argp); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /* |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 663 | * returns log format for <fmt> or -1 if not found. |
| 664 | */ |
| 665 | int get_log_format(const char *fmt) |
| 666 | { |
| 667 | int format; |
| 668 | |
| 669 | format = LOG_FORMATS - 1; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 670 | while (format >= 0 && strcmp(log_formats[format].name, fmt)) |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 671 | format--; |
| 672 | |
| 673 | return format; |
| 674 | } |
| 675 | |
| 676 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 677 | * returns log level for <lev> or -1 if not found. |
| 678 | */ |
| 679 | int get_log_level(const char *lev) |
| 680 | { |
| 681 | int level; |
| 682 | |
| 683 | level = NB_LOG_LEVELS - 1; |
| 684 | while (level >= 0 && strcmp(log_levels[level], lev)) |
| 685 | level--; |
| 686 | |
| 687 | return level; |
| 688 | } |
| 689 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 690 | /* |
| 691 | * returns log facility for <fac> or -1 if not found. |
| 692 | */ |
| 693 | int get_log_facility(const char *fac) |
| 694 | { |
| 695 | int facility; |
| 696 | |
| 697 | facility = NB_LOG_FACILITIES - 1; |
| 698 | while (facility >= 0 && strcmp(log_facilities[facility], fac)) |
| 699 | facility--; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 700 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 701 | return facility; |
| 702 | } |
| 703 | |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 704 | /* |
| 705 | * Write a string in the log string |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 706 | * Take cares of quote options |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 707 | * |
| 708 | * Return the adress of the \0 character, or NULL on error |
| 709 | */ |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 710 | char *lf_text_len(char *dst, const char *src, size_t len, size_t size, struct logformat_node *node) |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 711 | { |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 712 | if (size < 2) |
| 713 | return NULL; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 714 | |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 715 | if (node->options & LOG_OPT_QUOTE) { |
| 716 | *(dst++) = '"'; |
| 717 | size--; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 718 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 719 | |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 720 | if (src && len) { |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 721 | if (++len > size) |
| 722 | len = size; |
| 723 | len = strlcpy2(dst, src, len); |
| 724 | |
| 725 | size -= len; |
| 726 | dst += len; |
| 727 | } |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 728 | else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) { |
| 729 | if (size < 2) |
| 730 | return NULL; |
| 731 | *(dst++) = '-'; |
| 732 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 733 | |
| 734 | if (node->options & LOG_OPT_QUOTE) { |
| 735 | if (size < 2) |
| 736 | return NULL; |
| 737 | *(dst++) = '"'; |
| 738 | } |
| 739 | |
| 740 | *dst = '\0'; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 741 | return dst; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 744 | static inline char *lf_text(char *dst, const char *src, size_t size, struct logformat_node *node) |
| 745 | { |
| 746 | return lf_text_len(dst, src, size, size, node); |
| 747 | } |
| 748 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 749 | /* |
| 750 | * Write a IP adress to the log string |
| 751 | * +X option write in hexadecimal notation, most signifant byte on the left |
| 752 | */ |
| 753 | char *lf_ip(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node) |
| 754 | { |
| 755 | char *ret = dst; |
| 756 | int iret; |
| 757 | char pn[INET6_ADDRSTRLEN]; |
| 758 | |
| 759 | if (node->options & LOG_OPT_HEXA) { |
| 760 | const unsigned char *addr = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr; |
| 761 | iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]); |
| 762 | if (iret < 0 || iret > size) |
| 763 | return NULL; |
| 764 | ret += iret; |
| 765 | } else { |
| 766 | addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn)); |
| 767 | ret = lf_text(dst, pn, size, node); |
| 768 | if (ret == NULL) |
| 769 | return NULL; |
| 770 | } |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Write a port to the log |
| 776 | * +X option write in hexadecimal notation, most signifant byte on the left |
| 777 | */ |
| 778 | char *lf_port(char *dst, struct sockaddr *sockaddr, size_t size, struct logformat_node *node) |
| 779 | { |
| 780 | char *ret = dst; |
| 781 | int iret; |
| 782 | |
| 783 | if (node->options & LOG_OPT_HEXA) { |
| 784 | const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port; |
| 785 | iret = snprintf(dst, size, "%02X%02X", port[0], port[1]); |
| 786 | if (iret < 0 || iret > size) |
| 787 | return NULL; |
| 788 | ret += iret; |
| 789 | } else { |
| 790 | ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size); |
| 791 | if (ret == NULL) |
| 792 | return NULL; |
| 793 | } |
| 794 | return ret; |
| 795 | } |
| 796 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 797 | /* Re-generate time-based part of the syslog header in RFC3164 format at |
| 798 | * the beginning of logheader once a second and return the pointer to the |
| 799 | * first character after it. |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 800 | */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 801 | static char *update_log_hdr(const time_t time) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 802 | { |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 803 | static long tvsec; |
| 804 | static char *dataptr = NULL; /* backup of last end of header, NULL first time */ |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 805 | static struct chunk host = { NULL, 0, 0 }; |
| 806 | static int sep = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 807 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 808 | if (unlikely(time != tvsec || dataptr == NULL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 809 | /* this string is rebuild only once a second */ |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 810 | struct tm tm; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 811 | int hdr_len; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 812 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 813 | tvsec = time; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 814 | get_localtime(tvsec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 815 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 816 | if (unlikely(global.log_send_hostname != host.str)) { |
| 817 | host.str = global.log_send_hostname; |
| 818 | host.len = host.str ? strlen(host.str) : 0; |
| 819 | sep = host.len ? 1 : 0; |
| 820 | } |
| 821 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 822 | hdr_len = snprintf(logheader, global.max_syslog_len, |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 823 | "<<<<>%s %2d %02d:%02d:%02d %.*s%*s", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 824 | monthname[tm.tm_mon], |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 825 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 826 | host.len, host.str, sep, ""); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 827 | /* WARNING: depending upon implementations, snprintf may return |
| 828 | * either -1 or the number of bytes that would be needed to store |
| 829 | * the total message. In both cases, we must adjust it. |
| 830 | */ |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 831 | if (hdr_len < 0 || hdr_len > global.max_syslog_len) |
| 832 | hdr_len = global.max_syslog_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 833 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 834 | dataptr = logheader + hdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 835 | } |
| 836 | |
Willy Tarreau | 094af4e | 2015-01-07 15:03:42 +0100 | [diff] [blame] | 837 | dataptr[0] = 0; // ensure we get rid of any previous attempt |
| 838 | |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 839 | return dataptr; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 840 | } |
| 841 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 842 | /* Re-generate time-based part of the syslog header in RFC5424 format at |
| 843 | * the beginning of logheader_rfc5424 once a second and return the pointer |
| 844 | * to the first character after it. |
| 845 | */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 846 | static char *update_log_hdr_rfc5424(const time_t time) |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 847 | { |
| 848 | static long tvsec; |
| 849 | static char *dataptr = NULL; /* backup of last end of header, NULL first time */ |
| 850 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 851 | if (unlikely(time != tvsec || dataptr == NULL)) { |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 852 | /* this string is rebuild only once a second */ |
| 853 | struct tm tm; |
| 854 | int hdr_len; |
| 855 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 856 | tvsec = time; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 857 | get_localtime(tvsec, &tm); |
| 858 | |
| 859 | hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len, |
Dragan Dosen | 17def46 | 2015-10-09 21:31:43 +0200 | [diff] [blame] | 860 | "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ", |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 861 | tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, |
Dragan Dosen | 17def46 | 2015-10-09 21:31:43 +0200 | [diff] [blame] | 862 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 863 | localtimezone, localtimezone+3, |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 864 | global.log_send_hostname ? global.log_send_hostname : hostname); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 865 | /* WARNING: depending upon implementations, snprintf may return |
| 866 | * either -1 or the number of bytes that would be needed to store |
| 867 | * the total message. In both cases, we must adjust it. |
| 868 | */ |
| 869 | if (hdr_len < 0 || hdr_len > global.max_syslog_len) |
| 870 | hdr_len = global.max_syslog_len; |
| 871 | |
| 872 | dataptr = logheader_rfc5424 + hdr_len; |
| 873 | } |
| 874 | |
| 875 | dataptr[0] = 0; // ensure we get rid of any previous attempt |
| 876 | |
| 877 | return dataptr; |
| 878 | } |
| 879 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 880 | /* |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 881 | * This function sends the syslog message using a printf format string. It |
| 882 | * expects an LF-terminated message. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 883 | */ |
| 884 | void send_log(struct proxy *p, int level, const char *format, ...) |
| 885 | { |
| 886 | va_list argp; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 887 | int data_len; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 888 | |
Willy Tarreau | 8c97ab5 | 2015-01-15 16:29:53 +0100 | [diff] [blame] | 889 | if (level < 0 || format == NULL || logline == NULL) |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 890 | return; |
| 891 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 892 | va_start(argp, format); |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 893 | data_len = vsnprintf(logline, global.max_syslog_len, format, argp); |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 894 | if (data_len < 0 || data_len > global.max_syslog_len) |
| 895 | data_len = global.max_syslog_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 896 | va_end(argp); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 897 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 898 | __send_log(p, level, logline, data_len, default_rfc5424_sd_log_format, 2); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /* |
| 902 | * This function sends a syslog message. |
| 903 | * It doesn't care about errors nor does it report them. |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 904 | * It overrides the last byte of the message vector with an LF character. |
| 905 | * The arguments <sd> and <sd_size> are used for the structured-data part |
| 906 | * in RFC5424 formatted syslog messages. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 907 | */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 908 | void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size) |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 909 | { |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 910 | static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { }; |
| 911 | static struct msghdr msghdr = { |
| 912 | .msg_iov = iovec, |
| 913 | .msg_iovlen = NB_MSG_IOVEC_ELEMENTS |
| 914 | }; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 915 | static int logfdunix = -1; /* syslog to AF_UNIX socket */ |
| 916 | static int logfdinet = -1; /* syslog to AF_INET socket */ |
| 917 | static char *dataptr = NULL; |
| 918 | int fac_level; |
| 919 | struct list *logsrvs = NULL; |
| 920 | struct logsrv *tmp = NULL; |
| 921 | int nblogger; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 922 | char *hdr, *hdr_ptr; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 923 | size_t hdr_size; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 924 | time_t time = date.tv_sec; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 925 | struct chunk *tag = &global.log_tag; |
| 926 | static int curr_pid; |
| 927 | static char pidstr[100]; |
| 928 | static struct chunk pid; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 929 | |
| 930 | dataptr = message; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 931 | |
| 932 | if (p == NULL) { |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 933 | if (!LIST_ISEMPTY(&global.logsrvs)) { |
| 934 | logsrvs = &global.logsrvs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 935 | } |
| 936 | } else { |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 937 | if (!LIST_ISEMPTY(&p->logsrvs)) { |
| 938 | logsrvs = &p->logsrvs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 939 | } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 940 | if (p->log_tag.str) { |
| 941 | tag = &p->log_tag; |
| 942 | } |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 943 | } |
| 944 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 945 | if (!logsrvs) |
| 946 | return; |
| 947 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 948 | if (unlikely(curr_pid != getpid())) { |
| 949 | curr_pid = getpid(); |
| 950 | ltoa_o(curr_pid, pidstr, sizeof(pidstr)); |
| 951 | chunk_initstr(&pid, pidstr); |
| 952 | } |
| 953 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 954 | /* Send log messages to syslog server. */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 955 | nblogger = 0; |
| 956 | list_for_each_entry(tmp, logsrvs, list) { |
| 957 | const struct logsrv *logsrv = tmp; |
David du Colombier | 11bcb6c | 2011-03-24 12:23:00 +0100 | [diff] [blame] | 958 | int *plogfd = logsrv->addr.ss_family == AF_UNIX ? |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 959 | &logfdunix : &logfdinet; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 960 | char *pid_sep1 = NULL, *pid_sep2 = NULL; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 961 | int sent; |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 962 | int maxlen; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 963 | int hdr_max = 0; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 964 | int tag_max = 0; |
| 965 | int pid_sep1_max = 0; |
| 966 | int pid_max = 0; |
| 967 | int pid_sep2_max = 0; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 968 | int sd_max = 0; |
Dragan Dosen | 5b78d9b | 2015-09-28 16:01:03 +0200 | [diff] [blame] | 969 | int max = 0; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 970 | |
Willy Tarreau | c7c7be2 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 971 | nblogger++; |
| 972 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 973 | /* we can filter the level of the messages that are sent to each logger */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 974 | if (level > logsrv->level) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 975 | continue; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 976 | |
Willy Tarreau | c7c7be2 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 977 | if (unlikely(*plogfd < 0)) { |
| 978 | /* socket not successfully initialized yet */ |
| 979 | int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP; |
| 980 | |
| 981 | if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) { |
| 982 | Alert("socket for logger #%d failed: %s (errno=%d)\n", |
| 983 | nblogger, strerror(errno), errno); |
| 984 | continue; |
| 985 | } |
| 986 | /* we don't want to receive anything on this socket */ |
| 987 | setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero)); |
| 988 | /* does nothing under Linux, maybe needed for others */ |
| 989 | shutdown(*plogfd, SHUT_RD); |
| 990 | } |
| 991 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 992 | switch (logsrv->format) { |
| 993 | case LOG_FORMAT_RFC3164: |
| 994 | hdr = logheader; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 995 | hdr_ptr = update_log_hdr(time); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 996 | break; |
| 997 | |
| 998 | case LOG_FORMAT_RFC5424: |
| 999 | hdr = logheader_rfc5424; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1000 | hdr_ptr = update_log_hdr_rfc5424(time); |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1001 | sd_max = sd_size; /* the SD part allowed only in RFC5424 */ |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1002 | break; |
| 1003 | |
| 1004 | default: |
| 1005 | continue; /* must never happen */ |
| 1006 | } |
| 1007 | |
| 1008 | hdr_size = hdr_ptr - hdr; |
| 1009 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1010 | /* For each target, we may have a different facility. |
| 1011 | * We can also have a different log level for each message. |
| 1012 | * This induces variations in the message header length. |
| 1013 | * Since we don't want to recompute it each time, nor copy it every |
| 1014 | * time, we only change the facility in the pre-computed header, |
| 1015 | * and we change the pointer to the header accordingly. |
| 1016 | */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1017 | fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1018 | hdr_ptr = hdr + 3; /* last digit of the log level */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1019 | do { |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1020 | *hdr_ptr = '0' + fac_level % 10; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1021 | fac_level /= 10; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1022 | hdr_ptr--; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1023 | } while (fac_level && hdr_ptr > hdr); |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1024 | *hdr_ptr = '<'; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1025 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1026 | hdr_max = hdr_size - (hdr_ptr - hdr); |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1027 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1028 | /* time-based header */ |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1029 | if (unlikely(hdr_size >= logsrv->maxlen)) { |
| 1030 | hdr_max = MIN(hdr_max, logsrv->maxlen) - 1; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1031 | sd_max = 0; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1032 | goto send; |
| 1033 | } |
| 1034 | |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1035 | maxlen = logsrv->maxlen - hdr_max; |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1036 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1037 | /* tag */ |
| 1038 | tag_max = tag->len; |
| 1039 | if (unlikely(tag_max >= maxlen)) { |
| 1040 | tag_max = maxlen - 1; |
| 1041 | sd_max = 0; |
| 1042 | goto send; |
| 1043 | } |
| 1044 | |
| 1045 | maxlen -= tag_max; |
| 1046 | |
| 1047 | /* first pid separator */ |
| 1048 | pid_sep1_max = log_formats[logsrv->format].pid.sep1.len; |
| 1049 | if (unlikely(pid_sep1_max >= maxlen)) { |
| 1050 | pid_sep1_max = maxlen - 1; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1051 | sd_max = 0; |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1052 | goto send; |
| 1053 | } |
| 1054 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1055 | pid_sep1 = log_formats[logsrv->format].pid.sep1.str; |
| 1056 | maxlen -= pid_sep1_max; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1057 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1058 | /* pid */ |
| 1059 | pid_max = pid.len; |
| 1060 | if (unlikely(pid_max >= maxlen)) { |
| 1061 | pid_max = maxlen - 1; |
| 1062 | sd_max = 0; |
| 1063 | goto send; |
| 1064 | } |
| 1065 | |
| 1066 | maxlen -= pid_max; |
| 1067 | |
| 1068 | /* second pid separator */ |
| 1069 | pid_sep2_max = log_formats[logsrv->format].pid.sep2.len; |
| 1070 | if (unlikely(pid_sep2_max >= maxlen)) { |
| 1071 | pid_sep2_max = maxlen - 1; |
| 1072 | sd_max = 0; |
| 1073 | goto send; |
| 1074 | } |
| 1075 | |
| 1076 | pid_sep2 = log_formats[logsrv->format].pid.sep2.str; |
| 1077 | maxlen -= pid_sep2_max; |
| 1078 | |
| 1079 | /* structured-data */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1080 | if (sd_max >= maxlen) { |
| 1081 | sd_max = maxlen - 1; |
| 1082 | goto send; |
| 1083 | } |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1084 | |
Dragan Dosen | 5b78d9b | 2015-09-28 16:01:03 +0200 | [diff] [blame] | 1085 | max = MIN(size, maxlen - sd_max) - 1; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1086 | send: |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1087 | iovec[0].iov_base = hdr_ptr; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1088 | iovec[0].iov_len = hdr_max; |
| 1089 | iovec[1].iov_base = tag->str; |
| 1090 | iovec[1].iov_len = tag_max; |
| 1091 | iovec[2].iov_base = pid_sep1; |
| 1092 | iovec[2].iov_len = pid_sep1_max; |
| 1093 | iovec[3].iov_base = pid.str; |
| 1094 | iovec[3].iov_len = pid_max; |
| 1095 | iovec[4].iov_base = pid_sep2; |
| 1096 | iovec[4].iov_len = pid_sep2_max; |
| 1097 | iovec[5].iov_base = sd; |
| 1098 | iovec[5].iov_len = sd_max; |
| 1099 | iovec[6].iov_base = dataptr; |
| 1100 | iovec[6].iov_len = max; |
| 1101 | iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */ |
| 1102 | iovec[7].iov_len = 1; |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1103 | |
| 1104 | msghdr.msg_name = (struct sockaddr *)&logsrv->addr; |
| 1105 | msghdr.msg_namelen = get_addr_len(&logsrv->addr); |
| 1106 | |
| 1107 | sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL); |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 1108 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1109 | if (sent < 0) { |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1110 | Alert("sendmsg logger #%d failed: %s (errno=%d)\n", |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1111 | nblogger, strerror(errno), errno); |
| 1112 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1116 | extern fd_set hdr_encode_map[]; |
| 1117 | extern fd_set url_encode_map[]; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1118 | extern fd_set http_encode_map[]; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1119 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1120 | |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 1121 | const char sess_cookie[8] = "NIDVEOU7"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, Unused, unknown */ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1122 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 1123 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 1124 | Set-cookie Updated, unknown, unknown */ |
| 1125 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1126 | /* |
| 1127 | * try to write a character if there is enough space, or goto out |
| 1128 | */ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1129 | #define LOGCHAR(x) do { \ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1130 | if (tmplog < dst + maxsize - 1) { \ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1131 | *(tmplog++) = (x); \ |
| 1132 | } else { \ |
| 1133 | goto out; \ |
| 1134 | } \ |
| 1135 | } while(0) |
| 1136 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1137 | |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 1138 | /* Builds a log line in <dst> based on <list_format>, and stops before reaching |
| 1139 | * <maxsize> characters. Returns the size of the output string in characters, |
| 1140 | * not counting the trailing zero which is always added if the resulting size |
| 1141 | * is not zero. |
| 1142 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1143 | int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1144 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1145 | struct session *sess = strm_sess(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1146 | struct proxy *fe = sess->fe; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 1147 | struct proxy *be = s->be; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1148 | struct http_txn *txn = s->txn; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1149 | struct chunk chunk; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1150 | char *uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1151 | char *spc; |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 1152 | char *qmark; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1153 | char *end; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1154 | struct tm tm; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1155 | int t_request; |
| 1156 | int hdr; |
| 1157 | int last_isspace = 1; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1158 | int nspaces = 0; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1159 | char *tmplog; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1160 | char *ret; |
| 1161 | int iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1162 | struct logformat_node *tmp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1163 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1164 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1165 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1166 | t_request = -1; |
| 1167 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1168 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1169 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1170 | tmplog = dst; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1171 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1172 | /* fill logbuffer */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1173 | if (LIST_ISEMPTY(list_format)) |
| 1174 | return 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1175 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1176 | list_for_each_entry(tmp, list_format, list) { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1177 | struct connection *conn; |
Willy Tarreau | 4f65356 | 2012-10-12 19:48:16 +0200 | [diff] [blame] | 1178 | const char *src = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1179 | struct sample *key; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1180 | const struct chunk empty = { NULL, 0, 0 }; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1181 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1182 | switch (tmp->type) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1183 | case LOG_FMT_SEPARATOR: |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1184 | if (!last_isspace) { |
| 1185 | LOGCHAR(' '); |
| 1186 | last_isspace = 1; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1187 | } |
| 1188 | break; |
| 1189 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1190 | case LOG_FMT_TEXT: // text |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1191 | src = tmp->arg; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1192 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1193 | if (iret == 0) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1194 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1195 | tmplog += iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1196 | last_isspace = 0; |
| 1197 | break; |
| 1198 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1199 | case LOG_FMT_EXPR: // sample expression, may be request or response |
| 1200 | key = NULL; |
| 1201 | if (tmp->options & LOG_OPT_REQ_CAP) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1202 | key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1203 | if (!key && (tmp->options & LOG_OPT_RES_CAP)) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1204 | key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, tmp->expr, SMP_T_STR); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1205 | if (tmp->options & LOG_OPT_HTTP) |
| 1206 | ret = encode_chunk(tmplog, dst + maxsize, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1207 | '%', http_encode_map, key ? &key->data.u.str : &empty); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1208 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1209 | ret = lf_text_len(tmplog, key ? key->data.u.str.str : NULL, key ? key->data.u.str.len : 0, dst + maxsize - tmplog, tmp); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1210 | if (ret == 0) |
| 1211 | goto out; |
| 1212 | tmplog = ret; |
| 1213 | last_isspace = 0; |
| 1214 | break; |
| 1215 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1216 | case LOG_FMT_CLIENTIP: // %ci |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1217 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1218 | if (conn) |
| 1219 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1220 | else |
| 1221 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1222 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1223 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1224 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1225 | last_isspace = 0; |
| 1226 | break; |
| 1227 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1228 | case LOG_FMT_CLIENTPORT: // %cp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1229 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1230 | if (conn) { |
| 1231 | if (conn->addr.from.ss_family == AF_UNIX) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1232 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1233 | } else { |
| 1234 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, |
| 1235 | dst + maxsize - tmplog, tmp); |
| 1236 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1237 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1238 | else |
| 1239 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1240 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1241 | if (ret == NULL) |
| 1242 | goto out; |
| 1243 | tmplog = ret; |
| 1244 | last_isspace = 0; |
| 1245 | break; |
| 1246 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1247 | case LOG_FMT_FRONTENDIP: // %fi |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1248 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1249 | if (conn) { |
| 1250 | conn_get_to_addr(conn); |
| 1251 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1252 | } |
| 1253 | else |
| 1254 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1255 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1256 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1257 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1258 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1259 | last_isspace = 0; |
| 1260 | break; |
| 1261 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1262 | case LOG_FMT_FRONTENDPORT: // %fp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1263 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1264 | if (conn) { |
| 1265 | conn_get_to_addr(conn); |
| 1266 | if (conn->addr.to.ss_family == AF_UNIX) |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1267 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1268 | else |
| 1269 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1270 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1271 | else |
| 1272 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1273 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1274 | if (ret == NULL) |
| 1275 | goto out; |
| 1276 | tmplog = ret; |
| 1277 | last_isspace = 0; |
| 1278 | break; |
| 1279 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1280 | case LOG_FMT_BACKENDIP: // %bi |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1281 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1282 | if (conn) |
| 1283 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1284 | else |
| 1285 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1286 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1287 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1288 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1289 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1290 | last_isspace = 0; |
| 1291 | break; |
| 1292 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1293 | case LOG_FMT_BACKENDPORT: // %bp |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1294 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1295 | if (conn) |
| 1296 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1297 | else |
| 1298 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1299 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1300 | if (ret == NULL) |
| 1301 | goto out; |
| 1302 | tmplog = ret; |
| 1303 | last_isspace = 0; |
| 1304 | break; |
| 1305 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1306 | case LOG_FMT_SERVERIP: // %si |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1307 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1308 | if (conn) |
| 1309 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1310 | else |
| 1311 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1312 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1313 | if (ret == NULL) |
| 1314 | goto out; |
| 1315 | tmplog = ret; |
| 1316 | last_isspace = 0; |
| 1317 | break; |
| 1318 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1319 | case LOG_FMT_SERVERPORT: // %sp |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1320 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1321 | if (conn) |
| 1322 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1323 | else |
| 1324 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1325 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1326 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1327 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1328 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1329 | last_isspace = 0; |
| 1330 | break; |
| 1331 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1332 | case LOG_FMT_DATE: // %t |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1333 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1334 | ret = date2str_log(tmplog, &tm, &(s->logs.accept_date), |
| 1335 | dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1336 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1337 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1338 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1339 | last_isspace = 0; |
| 1340 | break; |
| 1341 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1342 | case LOG_FMT_DATEGMT: // %T |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1343 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1344 | ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1345 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1346 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1347 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1348 | last_isspace = 0; |
| 1349 | break; |
| 1350 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 1351 | case LOG_FMT_DATELOCAL: // %Tl |
| 1352 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
| 1353 | ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog); |
| 1354 | if (ret == NULL) |
| 1355 | goto out; |
| 1356 | tmplog = ret; |
| 1357 | last_isspace = 0; |
| 1358 | break; |
| 1359 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1360 | case LOG_FMT_TS: // %Ts |
| 1361 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 1362 | if (tmp->options & LOG_OPT_HEXA) { |
| 1363 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec); |
| 1364 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1365 | goto out; |
| 1366 | last_isspace = 0; |
| 1367 | tmplog += iret; |
| 1368 | } else { |
| 1369 | ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog); |
| 1370 | if (ret == NULL) |
| 1371 | goto out; |
| 1372 | tmplog = ret; |
| 1373 | last_isspace = 0; |
| 1374 | } |
| 1375 | break; |
| 1376 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1377 | case LOG_FMT_MS: // %ms |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1378 | if (tmp->options & LOG_OPT_HEXA) { |
| 1379 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000); |
| 1380 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1381 | goto out; |
| 1382 | last_isspace = 0; |
| 1383 | tmplog += iret; |
| 1384 | } else { |
| 1385 | if ((dst + maxsize - tmplog) < 4) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1386 | goto out; |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 1387 | ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000, |
| 1388 | tmplog, 4); |
| 1389 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1390 | goto out; |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 1391 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1392 | last_isspace = 0; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1393 | } |
| 1394 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1395 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1396 | case LOG_FMT_FRONTEND: // %f |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1397 | src = fe->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1398 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1399 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1400 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1401 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1402 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1403 | break; |
| 1404 | |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 1405 | case LOG_FMT_FRONTEND_XPRT: // %ft |
| 1406 | src = fe->id; |
| 1407 | if (tmp->options & LOG_OPT_QUOTE) |
| 1408 | LOGCHAR('"'); |
| 1409 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
| 1410 | if (iret == 0) |
| 1411 | goto out; |
| 1412 | tmplog += iret; |
| 1413 | #ifdef USE_OPENSSL |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1414 | if (sess->listener->xprt == &ssl_sock) |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 1415 | LOGCHAR('~'); |
| 1416 | #endif |
| 1417 | if (tmp->options & LOG_OPT_QUOTE) |
| 1418 | LOGCHAR('"'); |
| 1419 | last_isspace = 0; |
| 1420 | break; |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1421 | #ifdef USE_OPENSSL |
| 1422 | case LOG_FMT_SSL_CIPHER: // %sslc |
| 1423 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1424 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1425 | if (conn) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1426 | if (sess->listener->xprt == &ssl_sock) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1427 | src = ssl_sock_get_cipher_name(conn); |
| 1428 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1429 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 1430 | if (ret == NULL) |
| 1431 | goto out; |
| 1432 | tmplog = ret; |
| 1433 | last_isspace = 0; |
| 1434 | break; |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 1435 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1436 | case LOG_FMT_SSL_VERSION: // %sslv |
| 1437 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1438 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1439 | if (conn) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1440 | if (sess->listener->xprt == &ssl_sock) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1441 | src = ssl_sock_get_proto_version(conn); |
| 1442 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1443 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 1444 | if (ret == NULL) |
| 1445 | goto out; |
| 1446 | tmplog = ret; |
| 1447 | last_isspace = 0; |
| 1448 | break; |
| 1449 | #endif |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1450 | case LOG_FMT_BACKEND: // %b |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1451 | src = be->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1452 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1453 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1454 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1455 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1456 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1457 | break; |
| 1458 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1459 | case LOG_FMT_SERVER: // %s |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1460 | switch (obj_type(s->target)) { |
| 1461 | case OBJ_TYPE_SERVER: |
| 1462 | src = objt_server(s->target)->id; |
| 1463 | break; |
| 1464 | case OBJ_TYPE_APPLET: |
| 1465 | src = objt_applet(s->target)->name; |
| 1466 | break; |
| 1467 | default: |
| 1468 | src = "<NOSRV>"; |
| 1469 | break; |
| 1470 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1471 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1472 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1473 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1474 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1475 | last_isspace = 0; |
| 1476 | break; |
| 1477 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1478 | case LOG_FMT_TQ: // %Tq |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1479 | ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1480 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1481 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1482 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1483 | last_isspace = 0; |
| 1484 | break; |
| 1485 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1486 | case LOG_FMT_TW: // %Tw |
| 1487 | ret = ltoa_o((s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1488 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1489 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1490 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1491 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1492 | last_isspace = 0; |
| 1493 | break; |
| 1494 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1495 | case LOG_FMT_TC: // %Tc |
| 1496 | ret = ltoa_o((s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1497 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1498 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1499 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1500 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1501 | last_isspace = 0; |
| 1502 | break; |
| 1503 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1504 | case LOG_FMT_TR: // %Tr |
| 1505 | ret = ltoa_o((s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1506 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1507 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1508 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1509 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1510 | last_isspace = 0; |
| 1511 | break; |
| 1512 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1513 | case LOG_FMT_TT: // %Tt |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1514 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1515 | LOGCHAR('+'); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1516 | ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1517 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1518 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1519 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1520 | last_isspace = 0; |
| 1521 | break; |
| 1522 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1523 | case LOG_FMT_STATUS: // %ST |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1524 | ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1525 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1526 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1527 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1528 | last_isspace = 0; |
| 1529 | break; |
| 1530 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1531 | case LOG_FMT_BYTES: // %B |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1532 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1533 | LOGCHAR('+'); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1534 | ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1535 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1536 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1537 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1538 | last_isspace = 0; |
| 1539 | break; |
| 1540 | |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 1541 | case LOG_FMT_BYTES_UP: // %U |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 1542 | ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog); |
| 1543 | if (ret == NULL) |
| 1544 | goto out; |
| 1545 | tmplog = ret; |
| 1546 | last_isspace = 0; |
| 1547 | break; |
| 1548 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1549 | case LOG_FMT_CCLIENT: // %CC |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1550 | src = txn->cli_cookie; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1551 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1552 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1553 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1554 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1555 | last_isspace = 0; |
| 1556 | break; |
| 1557 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1558 | case LOG_FMT_CSERVER: // %CS |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1559 | src = txn->srv_cookie; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1560 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1561 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1562 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1563 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1564 | last_isspace = 0; |
| 1565 | break; |
| 1566 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1567 | case LOG_FMT_TERMSTATE: // %ts |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1568 | LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 1569 | LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 6580c06 | 2012-03-12 15:09:42 +0100 | [diff] [blame] | 1570 | *tmplog = '\0'; |
| 1571 | last_isspace = 0; |
| 1572 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1573 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1574 | case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP) |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1575 | LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 1576 | LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 1577 | LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-'); |
| 1578 | LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1579 | last_isspace = 0; |
| 1580 | break; |
| 1581 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1582 | case LOG_FMT_ACTCONN: // %ac |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1583 | ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1584 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1585 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1586 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1587 | last_isspace = 0; |
| 1588 | break; |
| 1589 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1590 | case LOG_FMT_FECONN: // %fc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1591 | ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1592 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1593 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1594 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1595 | last_isspace = 0; |
| 1596 | break; |
| 1597 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1598 | case LOG_FMT_BECONN: // %bc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1599 | ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1600 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1601 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1602 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1603 | last_isspace = 0; |
| 1604 | break; |
| 1605 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1606 | case LOG_FMT_SRVCONN: // %sc |
Willy Tarreau | 54a08d3 | 2012-11-12 01:14:56 +0100 | [diff] [blame] | 1607 | ret = ultoa_o(objt_server(s->target) ? |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1608 | objt_server(s->target)->cur_sess : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1609 | 0, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1610 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1611 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1612 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1613 | last_isspace = 0; |
| 1614 | break; |
| 1615 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1616 | case LOG_FMT_RETRIES: // %rq |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1617 | if (s->flags & SF_REDISP) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1618 | LOGCHAR('+'); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1619 | ret = ltoa_o((s->si[1].conn_retries>0) ? |
| 1620 | (be->conn_retries - s->si[1].conn_retries) : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1621 | be->conn_retries, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1622 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1623 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1624 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1625 | last_isspace = 0; |
| 1626 | break; |
| 1627 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1628 | case LOG_FMT_SRVQUEUE: // %sq |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1629 | ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1630 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1631 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1632 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1633 | last_isspace = 0; |
| 1634 | break; |
| 1635 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1636 | case LOG_FMT_BCKQUEUE: // %bq |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1637 | ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1638 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1639 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1640 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1641 | last_isspace = 0; |
| 1642 | break; |
| 1643 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1644 | case LOG_FMT_HDRREQUEST: // %hr |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1645 | /* request header */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1646 | if (fe->nb_req_cap && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1647 | if (tmp->options & LOG_OPT_QUOTE) |
| 1648 | LOGCHAR('"'); |
| 1649 | LOGCHAR('{'); |
| 1650 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1651 | if (hdr) |
| 1652 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1653 | if (s->req_cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1654 | ret = encode_string(tmplog, dst + maxsize, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1655 | '#', hdr_encode_map, s->req_cap[hdr]); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1656 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1657 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1658 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1659 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1660 | } |
| 1661 | LOGCHAR('}'); |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1662 | if (tmp->options & LOG_OPT_QUOTE) |
| 1663 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1664 | last_isspace = 0; |
| 1665 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1666 | break; |
| 1667 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1668 | case LOG_FMT_HDRREQUESTLIST: // %hrl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1669 | /* request header list */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1670 | if (fe->nb_req_cap && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1671 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1672 | if (hdr > 0) |
| 1673 | LOGCHAR(' '); |
| 1674 | if (tmp->options & LOG_OPT_QUOTE) |
| 1675 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1676 | if (s->req_cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1677 | ret = encode_string(tmplog, dst + maxsize, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1678 | '#', hdr_encode_map, s->req_cap[hdr]); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1679 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1680 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1681 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1682 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 1683 | LOGCHAR('-'); |
| 1684 | if (tmp->options & LOG_OPT_QUOTE) |
| 1685 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1686 | last_isspace = 0; |
| 1687 | } |
| 1688 | } |
| 1689 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1690 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1691 | |
| 1692 | case LOG_FMT_HDRRESPONS: // %hs |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1693 | /* response header */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1694 | if (fe->nb_rsp_cap && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1695 | if (tmp->options & LOG_OPT_QUOTE) |
| 1696 | LOGCHAR('"'); |
| 1697 | LOGCHAR('{'); |
| 1698 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1699 | if (hdr) |
| 1700 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1701 | if (s->res_cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1702 | ret = encode_string(tmplog, dst + maxsize, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1703 | '#', hdr_encode_map, s->res_cap[hdr]); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1704 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1705 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1706 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1707 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1708 | } |
| 1709 | LOGCHAR('}'); |
| 1710 | last_isspace = 0; |
| 1711 | if (tmp->options & LOG_OPT_QUOTE) |
| 1712 | LOGCHAR('"'); |
| 1713 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1714 | break; |
| 1715 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1716 | case LOG_FMT_HDRRESPONSLIST: // %hsl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1717 | /* response header list */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1718 | if (fe->nb_rsp_cap && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1719 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1720 | if (hdr > 0) |
| 1721 | LOGCHAR(' '); |
| 1722 | if (tmp->options & LOG_OPT_QUOTE) |
| 1723 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1724 | if (s->res_cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1725 | ret = encode_string(tmplog, dst + maxsize, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1726 | '#', hdr_encode_map, s->res_cap[hdr]); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1727 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1728 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1729 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1730 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 1731 | LOGCHAR('-'); |
| 1732 | if (tmp->options & LOG_OPT_QUOTE) |
| 1733 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1734 | last_isspace = 0; |
| 1735 | } |
| 1736 | } |
| 1737 | break; |
| 1738 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1739 | case LOG_FMT_REQ: // %r |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1740 | /* Request */ |
| 1741 | if (tmp->options & LOG_OPT_QUOTE) |
| 1742 | LOGCHAR('"'); |
| 1743 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1744 | ret = encode_string(tmplog, dst + maxsize, |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1745 | '#', url_encode_map, uri); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1746 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1747 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1748 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1749 | if (tmp->options & LOG_OPT_QUOTE) |
| 1750 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1751 | last_isspace = 0; |
| 1752 | break; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1753 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1754 | case LOG_FMT_HTTP_PATH: // %HP |
| 1755 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1756 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1757 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1758 | LOGCHAR('"'); |
| 1759 | |
| 1760 | end = uri + strlen(uri); |
| 1761 | // look for the first whitespace character |
| 1762 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 1763 | uri++; |
| 1764 | |
| 1765 | // keep advancing past multiple spaces |
| 1766 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 1767 | uri++; nspaces++; |
| 1768 | } |
| 1769 | |
| 1770 | // look for first space or question mark after url |
| 1771 | spc = uri; |
| 1772 | while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc)) |
| 1773 | spc++; |
| 1774 | |
| 1775 | if (!txn->uri || nspaces == 0) { |
| 1776 | chunk.str = "<BADREQ>"; |
| 1777 | chunk.len = strlen("<BADREQ>"); |
| 1778 | } else { |
| 1779 | chunk.str = uri; |
| 1780 | chunk.len = spc - uri; |
| 1781 | } |
| 1782 | |
| 1783 | ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk); |
| 1784 | if (ret == NULL || *ret != '\0') |
| 1785 | goto out; |
| 1786 | |
| 1787 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1788 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1789 | LOGCHAR('"'); |
| 1790 | |
| 1791 | last_isspace = 0; |
| 1792 | break; |
| 1793 | |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 1794 | case LOG_FMT_HTTP_QUERY: // %HQ |
| 1795 | if (tmp->options & LOG_OPT_QUOTE) |
| 1796 | LOGCHAR('"'); |
| 1797 | |
| 1798 | if (!txn->uri) { |
| 1799 | chunk.str = "<BADREQ>"; |
| 1800 | chunk.len = strlen("<BADREQ>"); |
| 1801 | } else { |
| 1802 | uri = txn->uri; |
| 1803 | end = uri + strlen(uri); |
| 1804 | // look for the first question mark |
| 1805 | while (uri < end && *uri != '?') |
| 1806 | uri++; |
| 1807 | |
| 1808 | qmark = uri; |
| 1809 | // look for first space or question mark after url |
| 1810 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 1811 | uri++; |
| 1812 | |
| 1813 | chunk.str = qmark; |
| 1814 | chunk.len = uri - qmark; |
| 1815 | } |
| 1816 | |
| 1817 | ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk); |
| 1818 | if (ret == NULL || *ret != '\0') |
| 1819 | goto out; |
| 1820 | |
| 1821 | tmplog = ret; |
| 1822 | if (tmp->options & LOG_OPT_QUOTE) |
| 1823 | LOGCHAR('"'); |
| 1824 | |
| 1825 | last_isspace = 0; |
| 1826 | break; |
| 1827 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1828 | case LOG_FMT_HTTP_URI: // %HU |
| 1829 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1830 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1831 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1832 | LOGCHAR('"'); |
| 1833 | |
| 1834 | end = uri + strlen(uri); |
| 1835 | // look for the first whitespace character |
| 1836 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 1837 | uri++; |
| 1838 | |
| 1839 | // keep advancing past multiple spaces |
| 1840 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 1841 | uri++; nspaces++; |
| 1842 | } |
| 1843 | |
| 1844 | // look for first space after url |
| 1845 | spc = uri; |
| 1846 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 1847 | spc++; |
| 1848 | |
| 1849 | if (!txn->uri || nspaces == 0) { |
| 1850 | chunk.str = "<BADREQ>"; |
| 1851 | chunk.len = strlen("<BADREQ>"); |
| 1852 | } else { |
| 1853 | chunk.str = uri; |
| 1854 | chunk.len = spc - uri; |
| 1855 | } |
| 1856 | |
| 1857 | ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk); |
| 1858 | if (ret == NULL || *ret != '\0') |
| 1859 | goto out; |
| 1860 | |
| 1861 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1862 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1863 | LOGCHAR('"'); |
| 1864 | |
| 1865 | last_isspace = 0; |
| 1866 | break; |
| 1867 | |
| 1868 | case LOG_FMT_HTTP_METHOD: // %HM |
| 1869 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1870 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1871 | LOGCHAR('"'); |
| 1872 | |
| 1873 | end = uri + strlen(uri); |
| 1874 | // look for the first whitespace character |
| 1875 | spc = uri; |
| 1876 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 1877 | spc++; |
| 1878 | |
| 1879 | if (spc == end) { // odd case, we have txn->uri, but we only got a verb |
| 1880 | chunk.str = "<BADREQ>"; |
| 1881 | chunk.len = strlen("<BADREQ>"); |
| 1882 | } else { |
| 1883 | chunk.str = uri; |
| 1884 | chunk.len = spc - uri; |
| 1885 | } |
| 1886 | |
| 1887 | ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk); |
| 1888 | if (ret == NULL || *ret != '\0') |
| 1889 | goto out; |
| 1890 | |
| 1891 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1892 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1893 | LOGCHAR('"'); |
| 1894 | |
| 1895 | last_isspace = 0; |
| 1896 | break; |
| 1897 | |
| 1898 | case LOG_FMT_HTTP_VERSION: // %HV |
| 1899 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1900 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1901 | LOGCHAR('"'); |
| 1902 | |
| 1903 | end = uri + strlen(uri); |
| 1904 | // look for the first whitespace character |
| 1905 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 1906 | uri++; |
| 1907 | |
| 1908 | // keep advancing past multiple spaces |
| 1909 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 1910 | uri++; nspaces++; |
| 1911 | } |
| 1912 | |
| 1913 | // look for the next whitespace character |
| 1914 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 1915 | uri++; |
| 1916 | |
| 1917 | // keep advancing past multiple spaces |
| 1918 | while (uri < end && HTTP_IS_SPHT(*uri)) |
| 1919 | uri++; |
| 1920 | |
| 1921 | if (!txn->uri || nspaces == 0) { |
| 1922 | chunk.str = "<BADREQ>"; |
| 1923 | chunk.len = strlen("<BADREQ>"); |
| 1924 | } else if (uri == end) { |
| 1925 | chunk.str = "HTTP/0.9"; |
| 1926 | chunk.len = strlen("HTTP/0.9"); |
| 1927 | } else { |
| 1928 | chunk.str = uri; |
| 1929 | chunk.len = end - uri; |
| 1930 | } |
| 1931 | |
| 1932 | ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk); |
| 1933 | if (ret == NULL || *ret != '\0') |
| 1934 | goto out; |
| 1935 | |
| 1936 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1937 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1938 | LOGCHAR('"'); |
| 1939 | |
| 1940 | last_isspace = 0; |
| 1941 | break; |
| 1942 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1943 | case LOG_FMT_COUNTER: // %rt |
| 1944 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 1945 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1946 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1947 | goto out; |
| 1948 | last_isspace = 0; |
| 1949 | tmplog += iret; |
| 1950 | } else { |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 1951 | ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1952 | if (ret == NULL) |
| 1953 | goto out; |
| 1954 | tmplog = ret; |
| 1955 | last_isspace = 0; |
| 1956 | } |
| 1957 | break; |
| 1958 | |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 1959 | case LOG_FMT_LOGCNT: // %lc |
| 1960 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1961 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 1962 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1963 | goto out; |
| 1964 | last_isspace = 0; |
| 1965 | tmplog += iret; |
| 1966 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1967 | ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 1968 | if (ret == NULL) |
| 1969 | goto out; |
| 1970 | tmplog = ret; |
| 1971 | last_isspace = 0; |
| 1972 | } |
| 1973 | break; |
| 1974 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1975 | case LOG_FMT_HOSTNAME: // %H |
| 1976 | src = hostname; |
| 1977 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 1978 | if (ret == NULL) |
| 1979 | goto out; |
| 1980 | tmplog = ret; |
| 1981 | last_isspace = 0; |
| 1982 | break; |
| 1983 | |
| 1984 | case LOG_FMT_PID: // %pid |
| 1985 | if (tmp->options & LOG_OPT_HEXA) { |
| 1986 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid); |
| 1987 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1988 | goto out; |
| 1989 | last_isspace = 0; |
| 1990 | tmplog += iret; |
| 1991 | } else { |
| 1992 | ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog); |
| 1993 | if (ret == NULL) |
| 1994 | goto out; |
| 1995 | tmplog = ret; |
| 1996 | last_isspace = 0; |
| 1997 | } |
| 1998 | break; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1999 | |
| 2000 | case LOG_FMT_UNIQUEID: // %ID |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2001 | ret = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2002 | src = s->unique_id; |
Thierry FOURNIER | 1be6910 | 2014-04-15 01:38:48 +0200 | [diff] [blame] | 2003 | ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2004 | if (ret == NULL) |
| 2005 | goto out; |
| 2006 | tmplog = ret; |
| 2007 | last_isspace = 0; |
| 2008 | break; |
| 2009 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | out: |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2014 | /* *tmplog is a unused character */ |
| 2015 | *tmplog = '\0'; |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 2016 | return tmplog - dst; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2017 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2018 | } |
| 2019 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2020 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2021 | * send a log for the stream when we have enough info about it. |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2022 | * Will not log if the frontend has no log defined. |
| 2023 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2024 | void strm_log(struct stream *s) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2025 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2026 | struct session *sess = s->sess; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2027 | int size, err, level; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2028 | int sd_size = 0; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2029 | |
| 2030 | /* if we don't want to log normal traffic, return now */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2031 | err = (s->flags & SF_REDISP) || |
| 2032 | ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) || |
| 2033 | (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) && |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2034 | (s->si[1].conn_retries != s->be->conn_retries)) || |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2035 | ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2036 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2037 | if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2038 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2039 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2040 | if (LIST_ISEMPTY(&sess->fe->logsrvs)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2041 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2042 | |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2043 | if (s->logs.level) { /* loglevel was overridden */ |
| 2044 | if (s->logs.level == -1) { |
| 2045 | s->logs.logwait = 0; /* logs disabled */ |
| 2046 | return; |
| 2047 | } |
| 2048 | level = s->logs.level - 1; |
| 2049 | } |
| 2050 | else { |
| 2051 | level = LOG_INFO; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2052 | if (err && (sess->fe->options2 & PR_O2_LOGERRORS)) |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2053 | level = LOG_ERR; |
| 2054 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2055 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2056 | /* if unique-id was not generated */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2057 | if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2058 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2059 | build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id); |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2060 | } |
| 2061 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2062 | if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) { |
| 2063 | sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len, |
| 2064 | &sess->fe->logformat_sd); |
| 2065 | } |
| 2066 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 2067 | size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2068 | if (size > 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2069 | sess->fe->log_count++; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2070 | __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2071 | s->logs.logwait = 0; |
| 2072 | } |
| 2073 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2074 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2075 | /* |
| 2076 | * Local variables: |
| 2077 | * c-indent-level: 8 |
| 2078 | * c-basic-offset: 8 |
| 2079 | * End: |
| 2080 | */ |