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 | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 860 | "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%s %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 | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 862 | tm.tm_hour, tm.tm_min, tm.tm_sec, localtimezone, |
| 863 | global.log_send_hostname ? global.log_send_hostname : hostname); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 864 | /* WARNING: depending upon implementations, snprintf may return |
| 865 | * either -1 or the number of bytes that would be needed to store |
| 866 | * the total message. In both cases, we must adjust it. |
| 867 | */ |
| 868 | if (hdr_len < 0 || hdr_len > global.max_syslog_len) |
| 869 | hdr_len = global.max_syslog_len; |
| 870 | |
| 871 | dataptr = logheader_rfc5424 + hdr_len; |
| 872 | } |
| 873 | |
| 874 | dataptr[0] = 0; // ensure we get rid of any previous attempt |
| 875 | |
| 876 | return dataptr; |
| 877 | } |
| 878 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 879 | /* |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 880 | * This function sends the syslog message using a printf format string. It |
| 881 | * expects an LF-terminated message. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 882 | */ |
| 883 | void send_log(struct proxy *p, int level, const char *format, ...) |
| 884 | { |
| 885 | va_list argp; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 886 | int data_len; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 887 | |
Willy Tarreau | 8c97ab5 | 2015-01-15 16:29:53 +0100 | [diff] [blame] | 888 | if (level < 0 || format == NULL || logline == NULL) |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 889 | return; |
| 890 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 891 | va_start(argp, format); |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 892 | data_len = vsnprintf(logline, global.max_syslog_len, format, argp); |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 893 | if (data_len < 0 || data_len > global.max_syslog_len) |
| 894 | data_len = global.max_syslog_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 895 | va_end(argp); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 896 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 897 | __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] | 898 | } |
| 899 | |
| 900 | /* |
| 901 | * This function sends a syslog message. |
| 902 | * It doesn't care about errors nor does it report them. |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 903 | * It overrides the last byte of the message vector with an LF character. |
| 904 | * The arguments <sd> and <sd_size> are used for the structured-data part |
| 905 | * in RFC5424 formatted syslog messages. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 906 | */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 907 | 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] | 908 | { |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 909 | static struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { }; |
| 910 | static struct msghdr msghdr = { |
| 911 | .msg_iov = iovec, |
| 912 | .msg_iovlen = NB_MSG_IOVEC_ELEMENTS |
| 913 | }; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 914 | static int logfdunix = -1; /* syslog to AF_UNIX socket */ |
| 915 | static int logfdinet = -1; /* syslog to AF_INET socket */ |
| 916 | static char *dataptr = NULL; |
| 917 | int fac_level; |
| 918 | struct list *logsrvs = NULL; |
| 919 | struct logsrv *tmp = NULL; |
| 920 | int nblogger; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 921 | char *hdr, *hdr_ptr; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 922 | size_t hdr_size; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 923 | time_t time = date.tv_sec; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 924 | struct chunk *tag = &global.log_tag; |
| 925 | static int curr_pid; |
| 926 | static char pidstr[100]; |
| 927 | static struct chunk pid; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 928 | |
| 929 | dataptr = message; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 930 | |
| 931 | if (p == NULL) { |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 932 | if (!LIST_ISEMPTY(&global.logsrvs)) { |
| 933 | logsrvs = &global.logsrvs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 934 | } |
| 935 | } else { |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 936 | if (!LIST_ISEMPTY(&p->logsrvs)) { |
| 937 | logsrvs = &p->logsrvs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 938 | } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 939 | if (p->log_tag.str) { |
| 940 | tag = &p->log_tag; |
| 941 | } |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 942 | } |
| 943 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 944 | if (!logsrvs) |
| 945 | return; |
| 946 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 947 | if (unlikely(curr_pid != getpid())) { |
| 948 | curr_pid = getpid(); |
| 949 | ltoa_o(curr_pid, pidstr, sizeof(pidstr)); |
| 950 | chunk_initstr(&pid, pidstr); |
| 951 | } |
| 952 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 953 | /* Send log messages to syslog server. */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 954 | nblogger = 0; |
| 955 | list_for_each_entry(tmp, logsrvs, list) { |
| 956 | const struct logsrv *logsrv = tmp; |
David du Colombier | 11bcb6c | 2011-03-24 12:23:00 +0100 | [diff] [blame] | 957 | int *plogfd = logsrv->addr.ss_family == AF_UNIX ? |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 958 | &logfdunix : &logfdinet; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 959 | char *pid_sep1 = NULL, *pid_sep2 = NULL; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 960 | int sent; |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 961 | int maxlen; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 962 | int hdr_max = 0; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 963 | int tag_max = 0; |
| 964 | int pid_sep1_max = 0; |
| 965 | int pid_max = 0; |
| 966 | int pid_sep2_max = 0; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 967 | int sd_max = 0; |
Dragan Dosen | 5b78d9b | 2015-09-28 16:01:03 +0200 | [diff] [blame] | 968 | int max = 0; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 969 | |
Willy Tarreau | c7c7be2 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 970 | nblogger++; |
| 971 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 972 | /* 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] | 973 | if (level > logsrv->level) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 974 | continue; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 975 | |
Willy Tarreau | c7c7be2 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 976 | if (unlikely(*plogfd < 0)) { |
| 977 | /* socket not successfully initialized yet */ |
| 978 | int proto = logsrv->addr.ss_family == AF_UNIX ? 0 : IPPROTO_UDP; |
| 979 | |
| 980 | if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, proto)) < 0) { |
| 981 | Alert("socket for logger #%d failed: %s (errno=%d)\n", |
| 982 | nblogger, strerror(errno), errno); |
| 983 | continue; |
| 984 | } |
| 985 | /* we don't want to receive anything on this socket */ |
| 986 | setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero)); |
| 987 | /* does nothing under Linux, maybe needed for others */ |
| 988 | shutdown(*plogfd, SHUT_RD); |
| 989 | } |
| 990 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 991 | switch (logsrv->format) { |
| 992 | case LOG_FORMAT_RFC3164: |
| 993 | hdr = logheader; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 994 | hdr_ptr = update_log_hdr(time); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 995 | break; |
| 996 | |
| 997 | case LOG_FORMAT_RFC5424: |
| 998 | hdr = logheader_rfc5424; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 999 | hdr_ptr = update_log_hdr_rfc5424(time); |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1000 | sd_max = sd_size; /* the SD part allowed only in RFC5424 */ |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1001 | break; |
| 1002 | |
| 1003 | default: |
| 1004 | continue; /* must never happen */ |
| 1005 | } |
| 1006 | |
| 1007 | hdr_size = hdr_ptr - hdr; |
| 1008 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1009 | /* For each target, we may have a different facility. |
| 1010 | * We can also have a different log level for each message. |
| 1011 | * This induces variations in the message header length. |
| 1012 | * Since we don't want to recompute it each time, nor copy it every |
| 1013 | * time, we only change the facility in the pre-computed header, |
| 1014 | * and we change the pointer to the header accordingly. |
| 1015 | */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1016 | fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1017 | hdr_ptr = hdr + 3; /* last digit of the log level */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1018 | do { |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1019 | *hdr_ptr = '0' + fac_level % 10; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1020 | fac_level /= 10; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1021 | hdr_ptr--; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1022 | } while (fac_level && hdr_ptr > hdr); |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1023 | *hdr_ptr = '<'; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1024 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1025 | hdr_max = hdr_size - (hdr_ptr - hdr); |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1026 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1027 | /* time-based header */ |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1028 | if (unlikely(hdr_size >= logsrv->maxlen)) { |
| 1029 | hdr_max = MIN(hdr_max, logsrv->maxlen) - 1; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1030 | sd_max = 0; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1031 | goto send; |
| 1032 | } |
| 1033 | |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1034 | maxlen = logsrv->maxlen - hdr_max; |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1035 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1036 | /* tag */ |
| 1037 | tag_max = tag->len; |
| 1038 | if (unlikely(tag_max >= maxlen)) { |
| 1039 | tag_max = maxlen - 1; |
| 1040 | sd_max = 0; |
| 1041 | goto send; |
| 1042 | } |
| 1043 | |
| 1044 | maxlen -= tag_max; |
| 1045 | |
| 1046 | /* first pid separator */ |
| 1047 | pid_sep1_max = log_formats[logsrv->format].pid.sep1.len; |
| 1048 | if (unlikely(pid_sep1_max >= maxlen)) { |
| 1049 | pid_sep1_max = maxlen - 1; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1050 | sd_max = 0; |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1051 | goto send; |
| 1052 | } |
| 1053 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1054 | pid_sep1 = log_formats[logsrv->format].pid.sep1.str; |
| 1055 | maxlen -= pid_sep1_max; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1056 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1057 | /* pid */ |
| 1058 | pid_max = pid.len; |
| 1059 | if (unlikely(pid_max >= maxlen)) { |
| 1060 | pid_max = maxlen - 1; |
| 1061 | sd_max = 0; |
| 1062 | goto send; |
| 1063 | } |
| 1064 | |
| 1065 | maxlen -= pid_max; |
| 1066 | |
| 1067 | /* second pid separator */ |
| 1068 | pid_sep2_max = log_formats[logsrv->format].pid.sep2.len; |
| 1069 | if (unlikely(pid_sep2_max >= maxlen)) { |
| 1070 | pid_sep2_max = maxlen - 1; |
| 1071 | sd_max = 0; |
| 1072 | goto send; |
| 1073 | } |
| 1074 | |
| 1075 | pid_sep2 = log_formats[logsrv->format].pid.sep2.str; |
| 1076 | maxlen -= pid_sep2_max; |
| 1077 | |
| 1078 | /* structured-data */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1079 | if (sd_max >= maxlen) { |
| 1080 | sd_max = maxlen - 1; |
| 1081 | goto send; |
| 1082 | } |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1083 | |
Dragan Dosen | 5b78d9b | 2015-09-28 16:01:03 +0200 | [diff] [blame] | 1084 | max = MIN(size, maxlen - sd_max) - 1; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1085 | send: |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1086 | iovec[0].iov_base = hdr_ptr; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1087 | iovec[0].iov_len = hdr_max; |
| 1088 | iovec[1].iov_base = tag->str; |
| 1089 | iovec[1].iov_len = tag_max; |
| 1090 | iovec[2].iov_base = pid_sep1; |
| 1091 | iovec[2].iov_len = pid_sep1_max; |
| 1092 | iovec[3].iov_base = pid.str; |
| 1093 | iovec[3].iov_len = pid_max; |
| 1094 | iovec[4].iov_base = pid_sep2; |
| 1095 | iovec[4].iov_len = pid_sep2_max; |
| 1096 | iovec[5].iov_base = sd; |
| 1097 | iovec[5].iov_len = sd_max; |
| 1098 | iovec[6].iov_base = dataptr; |
| 1099 | iovec[6].iov_len = max; |
| 1100 | iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */ |
| 1101 | iovec[7].iov_len = 1; |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1102 | |
| 1103 | msghdr.msg_name = (struct sockaddr *)&logsrv->addr; |
| 1104 | msghdr.msg_namelen = get_addr_len(&logsrv->addr); |
| 1105 | |
| 1106 | sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL); |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 1107 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1108 | if (sent < 0) { |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1109 | Alert("sendmsg logger #%d failed: %s (errno=%d)\n", |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1110 | nblogger, strerror(errno), errno); |
| 1111 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1115 | extern fd_set hdr_encode_map[]; |
| 1116 | extern fd_set url_encode_map[]; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1117 | extern fd_set http_encode_map[]; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1118 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1119 | |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 1120 | 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] | 1121 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 1122 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 1123 | Set-cookie Updated, unknown, unknown */ |
| 1124 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1125 | /* |
| 1126 | * try to write a character if there is enough space, or goto out |
| 1127 | */ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1128 | #define LOGCHAR(x) do { \ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1129 | if (tmplog < dst + maxsize - 1) { \ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1130 | *(tmplog++) = (x); \ |
| 1131 | } else { \ |
| 1132 | goto out; \ |
| 1133 | } \ |
| 1134 | } while(0) |
| 1135 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1136 | |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 1137 | /* Builds a log line in <dst> based on <list_format>, and stops before reaching |
| 1138 | * <maxsize> characters. Returns the size of the output string in characters, |
| 1139 | * not counting the trailing zero which is always added if the resulting size |
| 1140 | * is not zero. |
| 1141 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1142 | 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] | 1143 | { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1144 | struct session *sess = strm_sess(s); |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1145 | struct proxy *fe = sess->fe; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 1146 | struct proxy *be = s->be; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 1147 | struct http_txn *txn = s->txn; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1148 | struct chunk chunk; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1149 | char *uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1150 | char *spc; |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 1151 | char *qmark; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1152 | char *end; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1153 | struct tm tm; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1154 | int t_request; |
| 1155 | int hdr; |
| 1156 | int last_isspace = 1; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1157 | int nspaces = 0; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1158 | char *tmplog; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1159 | char *ret; |
| 1160 | int iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1161 | struct logformat_node *tmp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1162 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1163 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1164 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1165 | t_request = -1; |
| 1166 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1167 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1168 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1169 | tmplog = dst; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1170 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1171 | /* fill logbuffer */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1172 | if (LIST_ISEMPTY(list_format)) |
| 1173 | return 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1174 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1175 | list_for_each_entry(tmp, list_format, list) { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1176 | struct connection *conn; |
Willy Tarreau | 4f65356 | 2012-10-12 19:48:16 +0200 | [diff] [blame] | 1177 | const char *src = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1178 | struct sample *key; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1179 | const struct chunk empty = { NULL, 0, 0 }; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1180 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1181 | switch (tmp->type) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1182 | case LOG_FMT_SEPARATOR: |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1183 | if (!last_isspace) { |
| 1184 | LOGCHAR(' '); |
| 1185 | last_isspace = 1; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1186 | } |
| 1187 | break; |
| 1188 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1189 | case LOG_FMT_TEXT: // text |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1190 | src = tmp->arg; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1191 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1192 | if (iret == 0) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1193 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1194 | tmplog += iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1195 | last_isspace = 0; |
| 1196 | break; |
| 1197 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1198 | case LOG_FMT_EXPR: // sample expression, may be request or response |
| 1199 | key = NULL; |
| 1200 | if (tmp->options & LOG_OPT_REQ_CAP) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1201 | 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] | 1202 | if (!key && (tmp->options & LOG_OPT_RES_CAP)) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1203 | 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] | 1204 | if (tmp->options & LOG_OPT_HTTP) |
| 1205 | ret = encode_chunk(tmplog, dst + maxsize, |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1206 | '%', http_encode_map, key ? &key->data.u.str : &empty); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1207 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1208 | 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] | 1209 | if (ret == 0) |
| 1210 | goto out; |
| 1211 | tmplog = ret; |
| 1212 | last_isspace = 0; |
| 1213 | break; |
| 1214 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1215 | case LOG_FMT_CLIENTIP: // %ci |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1216 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1217 | if (conn) |
| 1218 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1219 | else |
| 1220 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1221 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1222 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1223 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1224 | last_isspace = 0; |
| 1225 | break; |
| 1226 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1227 | case LOG_FMT_CLIENTPORT: // %cp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1228 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1229 | if (conn) { |
| 1230 | if (conn->addr.from.ss_family == AF_UNIX) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1231 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1232 | } else { |
| 1233 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, |
| 1234 | dst + maxsize - tmplog, tmp); |
| 1235 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1236 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1237 | else |
| 1238 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1239 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1240 | if (ret == NULL) |
| 1241 | goto out; |
| 1242 | tmplog = ret; |
| 1243 | last_isspace = 0; |
| 1244 | break; |
| 1245 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1246 | case LOG_FMT_FRONTENDIP: // %fi |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1247 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1248 | if (conn) { |
| 1249 | conn_get_to_addr(conn); |
| 1250 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1251 | } |
| 1252 | else |
| 1253 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1254 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1255 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1256 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1257 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1258 | last_isspace = 0; |
| 1259 | break; |
| 1260 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1261 | case LOG_FMT_FRONTENDPORT: // %fp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1262 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1263 | if (conn) { |
| 1264 | conn_get_to_addr(conn); |
| 1265 | if (conn->addr.to.ss_family == AF_UNIX) |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1266 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1267 | else |
| 1268 | 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] | 1269 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1270 | else |
| 1271 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1272 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1273 | if (ret == NULL) |
| 1274 | goto out; |
| 1275 | tmplog = ret; |
| 1276 | last_isspace = 0; |
| 1277 | break; |
| 1278 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1279 | case LOG_FMT_BACKENDIP: // %bi |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1280 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1281 | if (conn) |
| 1282 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1283 | else |
| 1284 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1285 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1286 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1287 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1288 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1289 | last_isspace = 0; |
| 1290 | break; |
| 1291 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1292 | case LOG_FMT_BACKENDPORT: // %bp |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1293 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1294 | if (conn) |
| 1295 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1296 | else |
| 1297 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1298 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1299 | if (ret == NULL) |
| 1300 | goto out; |
| 1301 | tmplog = ret; |
| 1302 | last_isspace = 0; |
| 1303 | break; |
| 1304 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1305 | case LOG_FMT_SERVERIP: // %si |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1306 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1307 | if (conn) |
| 1308 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1309 | else |
| 1310 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1311 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1312 | if (ret == NULL) |
| 1313 | goto out; |
| 1314 | tmplog = ret; |
| 1315 | last_isspace = 0; |
| 1316 | break; |
| 1317 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1318 | case LOG_FMT_SERVERPORT: // %sp |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1319 | conn = objt_conn(s->si[1].end); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1320 | if (conn) |
| 1321 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1322 | else |
| 1323 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1324 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1325 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1326 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1327 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1328 | last_isspace = 0; |
| 1329 | break; |
| 1330 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1331 | case LOG_FMT_DATE: // %t |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1332 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1333 | ret = date2str_log(tmplog, &tm, &(s->logs.accept_date), |
| 1334 | dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1335 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1336 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1337 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1338 | last_isspace = 0; |
| 1339 | break; |
| 1340 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1341 | case LOG_FMT_DATEGMT: // %T |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1342 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1343 | ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1344 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1345 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1346 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1347 | last_isspace = 0; |
| 1348 | break; |
| 1349 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 1350 | case LOG_FMT_DATELOCAL: // %Tl |
| 1351 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
| 1352 | ret = localdate2str_log(tmplog, &tm, dst + maxsize - tmplog); |
| 1353 | if (ret == NULL) |
| 1354 | goto out; |
| 1355 | tmplog = ret; |
| 1356 | last_isspace = 0; |
| 1357 | break; |
| 1358 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1359 | case LOG_FMT_TS: // %Ts |
| 1360 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 1361 | if (tmp->options & LOG_OPT_HEXA) { |
| 1362 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)s->logs.accept_date.tv_sec); |
| 1363 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1364 | goto out; |
| 1365 | last_isspace = 0; |
| 1366 | tmplog += iret; |
| 1367 | } else { |
| 1368 | ret = ltoa_o(s->logs.accept_date.tv_sec, tmplog, dst + maxsize - tmplog); |
| 1369 | if (ret == NULL) |
| 1370 | goto out; |
| 1371 | tmplog = ret; |
| 1372 | last_isspace = 0; |
| 1373 | } |
| 1374 | break; |
| 1375 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1376 | case LOG_FMT_MS: // %ms |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1377 | if (tmp->options & LOG_OPT_HEXA) { |
| 1378 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)s->logs.accept_date.tv_usec/1000); |
| 1379 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1380 | goto out; |
| 1381 | last_isspace = 0; |
| 1382 | tmplog += iret; |
| 1383 | } else { |
| 1384 | if ((dst + maxsize - tmplog) < 4) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1385 | goto out; |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 1386 | ret = utoa_pad((unsigned int)s->logs.accept_date.tv_usec/1000, |
| 1387 | tmplog, 4); |
| 1388 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1389 | goto out; |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 1390 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1391 | last_isspace = 0; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1392 | } |
| 1393 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1394 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1395 | case LOG_FMT_FRONTEND: // %f |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1396 | src = fe->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1397 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1398 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1399 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1400 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1401 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1402 | break; |
| 1403 | |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 1404 | case LOG_FMT_FRONTEND_XPRT: // %ft |
| 1405 | src = fe->id; |
| 1406 | if (tmp->options & LOG_OPT_QUOTE) |
| 1407 | LOGCHAR('"'); |
| 1408 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
| 1409 | if (iret == 0) |
| 1410 | goto out; |
| 1411 | tmplog += iret; |
| 1412 | #ifdef USE_OPENSSL |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1413 | if (sess->listener->xprt == &ssl_sock) |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 1414 | LOGCHAR('~'); |
| 1415 | #endif |
| 1416 | if (tmp->options & LOG_OPT_QUOTE) |
| 1417 | LOGCHAR('"'); |
| 1418 | last_isspace = 0; |
| 1419 | break; |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1420 | #ifdef USE_OPENSSL |
| 1421 | case LOG_FMT_SSL_CIPHER: // %sslc |
| 1422 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1423 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1424 | if (conn) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1425 | if (sess->listener->xprt == &ssl_sock) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1426 | src = ssl_sock_get_cipher_name(conn); |
| 1427 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1428 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 1429 | if (ret == NULL) |
| 1430 | goto out; |
| 1431 | tmplog = ret; |
| 1432 | last_isspace = 0; |
| 1433 | break; |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 1434 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1435 | case LOG_FMT_SSL_VERSION: // %sslv |
| 1436 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1437 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1438 | if (conn) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1439 | if (sess->listener->xprt == &ssl_sock) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1440 | src = ssl_sock_get_proto_version(conn); |
| 1441 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 1442 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 1443 | if (ret == NULL) |
| 1444 | goto out; |
| 1445 | tmplog = ret; |
| 1446 | last_isspace = 0; |
| 1447 | break; |
| 1448 | #endif |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1449 | case LOG_FMT_BACKEND: // %b |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1450 | src = be->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1451 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1452 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1453 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1454 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1455 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1456 | break; |
| 1457 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1458 | case LOG_FMT_SERVER: // %s |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1459 | switch (obj_type(s->target)) { |
| 1460 | case OBJ_TYPE_SERVER: |
| 1461 | src = objt_server(s->target)->id; |
| 1462 | break; |
| 1463 | case OBJ_TYPE_APPLET: |
| 1464 | src = objt_applet(s->target)->name; |
| 1465 | break; |
| 1466 | default: |
| 1467 | src = "<NOSRV>"; |
| 1468 | break; |
| 1469 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1470 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1471 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1472 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1473 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1474 | last_isspace = 0; |
| 1475 | break; |
| 1476 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1477 | case LOG_FMT_TQ: // %Tq |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1478 | ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1479 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1480 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1481 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1482 | last_isspace = 0; |
| 1483 | break; |
| 1484 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1485 | case LOG_FMT_TW: // %Tw |
| 1486 | 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] | 1487 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1488 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1489 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1490 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1491 | last_isspace = 0; |
| 1492 | break; |
| 1493 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1494 | case LOG_FMT_TC: // %Tc |
| 1495 | 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] | 1496 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1497 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1498 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1499 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1500 | last_isspace = 0; |
| 1501 | break; |
| 1502 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1503 | case LOG_FMT_TR: // %Tr |
| 1504 | 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] | 1505 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1506 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1507 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1508 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1509 | last_isspace = 0; |
| 1510 | break; |
| 1511 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1512 | case LOG_FMT_TT: // %Tt |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1513 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1514 | LOGCHAR('+'); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1515 | ret = ltoa_o(s->logs.t_close, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1516 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1517 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1518 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1519 | last_isspace = 0; |
| 1520 | break; |
| 1521 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1522 | case LOG_FMT_STATUS: // %ST |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1523 | ret = ltoa_o(txn->status, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1524 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1525 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1526 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1527 | last_isspace = 0; |
| 1528 | break; |
| 1529 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1530 | case LOG_FMT_BYTES: // %B |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 1531 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1532 | LOGCHAR('+'); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1533 | ret = lltoa(s->logs.bytes_out, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1534 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1535 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1536 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1537 | last_isspace = 0; |
| 1538 | break; |
| 1539 | |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 1540 | case LOG_FMT_BYTES_UP: // %U |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 1541 | ret = lltoa(s->logs.bytes_in, tmplog, dst + maxsize - tmplog); |
| 1542 | if (ret == NULL) |
| 1543 | goto out; |
| 1544 | tmplog = ret; |
| 1545 | last_isspace = 0; |
| 1546 | break; |
| 1547 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1548 | case LOG_FMT_CCLIENT: // %CC |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1549 | src = txn->cli_cookie; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1550 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1551 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1552 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1553 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1554 | last_isspace = 0; |
| 1555 | break; |
| 1556 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1557 | case LOG_FMT_CSERVER: // %CS |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1558 | src = txn->srv_cookie; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1559 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1560 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1561 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1562 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1563 | last_isspace = 0; |
| 1564 | break; |
| 1565 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1566 | case LOG_FMT_TERMSTATE: // %ts |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1567 | LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 1568 | LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 6580c06 | 2012-03-12 15:09:42 +0100 | [diff] [blame] | 1569 | *tmplog = '\0'; |
| 1570 | last_isspace = 0; |
| 1571 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1572 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1573 | 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] | 1574 | LOGCHAR(sess_term_cond[(s->flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 1575 | LOGCHAR(sess_fin_state[(s->flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 1576 | LOGCHAR((be->ck_opts & PR_CK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-'); |
| 1577 | 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] | 1578 | last_isspace = 0; |
| 1579 | break; |
| 1580 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1581 | case LOG_FMT_ACTCONN: // %ac |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1582 | ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1583 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1584 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1585 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1586 | last_isspace = 0; |
| 1587 | break; |
| 1588 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1589 | case LOG_FMT_FECONN: // %fc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1590 | ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1591 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1592 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1593 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1594 | last_isspace = 0; |
| 1595 | break; |
| 1596 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1597 | case LOG_FMT_BECONN: // %bc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1598 | ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1599 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1600 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1601 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1602 | last_isspace = 0; |
| 1603 | break; |
| 1604 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1605 | case LOG_FMT_SRVCONN: // %sc |
Willy Tarreau | 54a08d3 | 2012-11-12 01:14:56 +0100 | [diff] [blame] | 1606 | ret = ultoa_o(objt_server(s->target) ? |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 1607 | objt_server(s->target)->cur_sess : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1608 | 0, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1609 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1610 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1611 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1612 | last_isspace = 0; |
| 1613 | break; |
| 1614 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1615 | case LOG_FMT_RETRIES: // %rq |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1616 | if (s->flags & SF_REDISP) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1617 | LOGCHAR('+'); |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1618 | ret = ltoa_o((s->si[1].conn_retries>0) ? |
| 1619 | (be->conn_retries - s->si[1].conn_retries) : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1620 | be->conn_retries, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1621 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1622 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1623 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1624 | last_isspace = 0; |
| 1625 | break; |
| 1626 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1627 | case LOG_FMT_SRVQUEUE: // %sq |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1628 | ret = ltoa_o(s->logs.srv_queue_size, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1629 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1630 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1631 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1632 | last_isspace = 0; |
| 1633 | break; |
| 1634 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1635 | case LOG_FMT_BCKQUEUE: // %bq |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1636 | ret = ltoa_o(s->logs.prx_queue_size, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1637 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1638 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1639 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1640 | last_isspace = 0; |
| 1641 | break; |
| 1642 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1643 | case LOG_FMT_HDRREQUEST: // %hr |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1644 | /* request header */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1645 | if (fe->nb_req_cap && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1646 | if (tmp->options & LOG_OPT_QUOTE) |
| 1647 | LOGCHAR('"'); |
| 1648 | LOGCHAR('{'); |
| 1649 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1650 | if (hdr) |
| 1651 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1652 | if (s->req_cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1653 | ret = encode_string(tmplog, dst + maxsize, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1654 | '#', hdr_encode_map, s->req_cap[hdr]); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1655 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1656 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1657 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1658 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1659 | } |
| 1660 | LOGCHAR('}'); |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1661 | if (tmp->options & LOG_OPT_QUOTE) |
| 1662 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1663 | last_isspace = 0; |
| 1664 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1665 | break; |
| 1666 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1667 | case LOG_FMT_HDRREQUESTLIST: // %hrl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1668 | /* request header list */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1669 | if (fe->nb_req_cap && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1670 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1671 | if (hdr > 0) |
| 1672 | LOGCHAR(' '); |
| 1673 | if (tmp->options & LOG_OPT_QUOTE) |
| 1674 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1675 | if (s->req_cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1676 | ret = encode_string(tmplog, dst + maxsize, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1677 | '#', hdr_encode_map, s->req_cap[hdr]); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1678 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1679 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1680 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1681 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 1682 | LOGCHAR('-'); |
| 1683 | if (tmp->options & LOG_OPT_QUOTE) |
| 1684 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1685 | last_isspace = 0; |
| 1686 | } |
| 1687 | } |
| 1688 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1689 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1690 | |
| 1691 | case LOG_FMT_HDRRESPONS: // %hs |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1692 | /* response header */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1693 | if (fe->nb_rsp_cap && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1694 | if (tmp->options & LOG_OPT_QUOTE) |
| 1695 | LOGCHAR('"'); |
| 1696 | LOGCHAR('{'); |
| 1697 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1698 | if (hdr) |
| 1699 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1700 | if (s->res_cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1701 | ret = encode_string(tmplog, dst + maxsize, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1702 | '#', hdr_encode_map, s->res_cap[hdr]); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1703 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1704 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1705 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1706 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1707 | } |
| 1708 | LOGCHAR('}'); |
| 1709 | last_isspace = 0; |
| 1710 | if (tmp->options & LOG_OPT_QUOTE) |
| 1711 | LOGCHAR('"'); |
| 1712 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1713 | break; |
| 1714 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1715 | case LOG_FMT_HDRRESPONSLIST: // %hsl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1716 | /* response header list */ |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1717 | if (fe->nb_rsp_cap && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1718 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1719 | if (hdr > 0) |
| 1720 | LOGCHAR(' '); |
| 1721 | if (tmp->options & LOG_OPT_QUOTE) |
| 1722 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1723 | if (s->res_cap[hdr] != NULL) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1724 | ret = encode_string(tmplog, dst + maxsize, |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 1725 | '#', hdr_encode_map, s->res_cap[hdr]); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1726 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1727 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1728 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1729 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 1730 | LOGCHAR('-'); |
| 1731 | if (tmp->options & LOG_OPT_QUOTE) |
| 1732 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1733 | last_isspace = 0; |
| 1734 | } |
| 1735 | } |
| 1736 | break; |
| 1737 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1738 | case LOG_FMT_REQ: // %r |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1739 | /* Request */ |
| 1740 | if (tmp->options & LOG_OPT_QUOTE) |
| 1741 | LOGCHAR('"'); |
| 1742 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1743 | ret = encode_string(tmplog, dst + maxsize, |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1744 | '#', url_encode_map, uri); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1745 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 1746 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1747 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1748 | if (tmp->options & LOG_OPT_QUOTE) |
| 1749 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1750 | last_isspace = 0; |
| 1751 | break; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1752 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1753 | case LOG_FMT_HTTP_PATH: // %HP |
| 1754 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1755 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1756 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1757 | LOGCHAR('"'); |
| 1758 | |
| 1759 | end = uri + strlen(uri); |
| 1760 | // look for the first whitespace character |
| 1761 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 1762 | uri++; |
| 1763 | |
| 1764 | // keep advancing past multiple spaces |
| 1765 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 1766 | uri++; nspaces++; |
| 1767 | } |
| 1768 | |
| 1769 | // look for first space or question mark after url |
| 1770 | spc = uri; |
| 1771 | while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc)) |
| 1772 | spc++; |
| 1773 | |
| 1774 | if (!txn->uri || nspaces == 0) { |
| 1775 | chunk.str = "<BADREQ>"; |
| 1776 | chunk.len = strlen("<BADREQ>"); |
| 1777 | } else { |
| 1778 | chunk.str = uri; |
| 1779 | chunk.len = spc - uri; |
| 1780 | } |
| 1781 | |
| 1782 | ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk); |
| 1783 | if (ret == NULL || *ret != '\0') |
| 1784 | goto out; |
| 1785 | |
| 1786 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1787 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1788 | LOGCHAR('"'); |
| 1789 | |
| 1790 | last_isspace = 0; |
| 1791 | break; |
| 1792 | |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 1793 | case LOG_FMT_HTTP_QUERY: // %HQ |
| 1794 | if (tmp->options & LOG_OPT_QUOTE) |
| 1795 | LOGCHAR('"'); |
| 1796 | |
| 1797 | if (!txn->uri) { |
| 1798 | chunk.str = "<BADREQ>"; |
| 1799 | chunk.len = strlen("<BADREQ>"); |
| 1800 | } else { |
| 1801 | uri = txn->uri; |
| 1802 | end = uri + strlen(uri); |
| 1803 | // look for the first question mark |
| 1804 | while (uri < end && *uri != '?') |
| 1805 | uri++; |
| 1806 | |
| 1807 | qmark = uri; |
| 1808 | // look for first space or question mark after url |
| 1809 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 1810 | uri++; |
| 1811 | |
| 1812 | chunk.str = qmark; |
| 1813 | chunk.len = uri - qmark; |
| 1814 | } |
| 1815 | |
| 1816 | ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk); |
| 1817 | if (ret == NULL || *ret != '\0') |
| 1818 | goto out; |
| 1819 | |
| 1820 | tmplog = ret; |
| 1821 | if (tmp->options & LOG_OPT_QUOTE) |
| 1822 | LOGCHAR('"'); |
| 1823 | |
| 1824 | last_isspace = 0; |
| 1825 | break; |
| 1826 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1827 | case LOG_FMT_HTTP_URI: // %HU |
| 1828 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1829 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1830 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1831 | LOGCHAR('"'); |
| 1832 | |
| 1833 | end = uri + strlen(uri); |
| 1834 | // look for the first whitespace character |
| 1835 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 1836 | uri++; |
| 1837 | |
| 1838 | // keep advancing past multiple spaces |
| 1839 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 1840 | uri++; nspaces++; |
| 1841 | } |
| 1842 | |
| 1843 | // look for first space after url |
| 1844 | spc = uri; |
| 1845 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 1846 | spc++; |
| 1847 | |
| 1848 | if (!txn->uri || nspaces == 0) { |
| 1849 | chunk.str = "<BADREQ>"; |
| 1850 | chunk.len = strlen("<BADREQ>"); |
| 1851 | } else { |
| 1852 | chunk.str = uri; |
| 1853 | chunk.len = spc - uri; |
| 1854 | } |
| 1855 | |
| 1856 | ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk); |
| 1857 | if (ret == NULL || *ret != '\0') |
| 1858 | goto out; |
| 1859 | |
| 1860 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1861 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1862 | LOGCHAR('"'); |
| 1863 | |
| 1864 | last_isspace = 0; |
| 1865 | break; |
| 1866 | |
| 1867 | case LOG_FMT_HTTP_METHOD: // %HM |
| 1868 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1869 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1870 | LOGCHAR('"'); |
| 1871 | |
| 1872 | end = uri + strlen(uri); |
| 1873 | // look for the first whitespace character |
| 1874 | spc = uri; |
| 1875 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 1876 | spc++; |
| 1877 | |
| 1878 | if (spc == end) { // odd case, we have txn->uri, but we only got a verb |
| 1879 | chunk.str = "<BADREQ>"; |
| 1880 | chunk.len = strlen("<BADREQ>"); |
| 1881 | } else { |
| 1882 | chunk.str = uri; |
| 1883 | chunk.len = spc - uri; |
| 1884 | } |
| 1885 | |
| 1886 | ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk); |
| 1887 | if (ret == NULL || *ret != '\0') |
| 1888 | goto out; |
| 1889 | |
| 1890 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1891 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1892 | LOGCHAR('"'); |
| 1893 | |
| 1894 | last_isspace = 0; |
| 1895 | break; |
| 1896 | |
| 1897 | case LOG_FMT_HTTP_VERSION: // %HV |
| 1898 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1899 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1900 | LOGCHAR('"'); |
| 1901 | |
| 1902 | end = uri + strlen(uri); |
| 1903 | // look for the first whitespace character |
| 1904 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 1905 | uri++; |
| 1906 | |
| 1907 | // keep advancing past multiple spaces |
| 1908 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 1909 | uri++; nspaces++; |
| 1910 | } |
| 1911 | |
| 1912 | // look for the next whitespace character |
| 1913 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 1914 | uri++; |
| 1915 | |
| 1916 | // keep advancing past multiple spaces |
| 1917 | while (uri < end && HTTP_IS_SPHT(*uri)) |
| 1918 | uri++; |
| 1919 | |
| 1920 | if (!txn->uri || nspaces == 0) { |
| 1921 | chunk.str = "<BADREQ>"; |
| 1922 | chunk.len = strlen("<BADREQ>"); |
| 1923 | } else if (uri == end) { |
| 1924 | chunk.str = "HTTP/0.9"; |
| 1925 | chunk.len = strlen("HTTP/0.9"); |
| 1926 | } else { |
| 1927 | chunk.str = uri; |
| 1928 | chunk.len = end - uri; |
| 1929 | } |
| 1930 | |
| 1931 | ret = encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk); |
| 1932 | if (ret == NULL || *ret != '\0') |
| 1933 | goto out; |
| 1934 | |
| 1935 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 1936 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1937 | LOGCHAR('"'); |
| 1938 | |
| 1939 | last_isspace = 0; |
| 1940 | break; |
| 1941 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1942 | case LOG_FMT_COUNTER: // %rt |
| 1943 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 1944 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", s->uniq_id); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1945 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1946 | goto out; |
| 1947 | last_isspace = 0; |
| 1948 | tmplog += iret; |
| 1949 | } else { |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 1950 | ret = ltoa_o(s->uniq_id, tmplog, dst + maxsize - tmplog); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1951 | if (ret == NULL) |
| 1952 | goto out; |
| 1953 | tmplog = ret; |
| 1954 | last_isspace = 0; |
| 1955 | } |
| 1956 | break; |
| 1957 | |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 1958 | case LOG_FMT_LOGCNT: // %lc |
| 1959 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1960 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 1961 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1962 | goto out; |
| 1963 | last_isspace = 0; |
| 1964 | tmplog += iret; |
| 1965 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1966 | ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 1967 | if (ret == NULL) |
| 1968 | goto out; |
| 1969 | tmplog = ret; |
| 1970 | last_isspace = 0; |
| 1971 | } |
| 1972 | break; |
| 1973 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1974 | case LOG_FMT_HOSTNAME: // %H |
| 1975 | src = hostname; |
| 1976 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 1977 | if (ret == NULL) |
| 1978 | goto out; |
| 1979 | tmplog = ret; |
| 1980 | last_isspace = 0; |
| 1981 | break; |
| 1982 | |
| 1983 | case LOG_FMT_PID: // %pid |
| 1984 | if (tmp->options & LOG_OPT_HEXA) { |
| 1985 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid); |
| 1986 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 1987 | goto out; |
| 1988 | last_isspace = 0; |
| 1989 | tmplog += iret; |
| 1990 | } else { |
| 1991 | ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog); |
| 1992 | if (ret == NULL) |
| 1993 | goto out; |
| 1994 | tmplog = ret; |
| 1995 | last_isspace = 0; |
| 1996 | } |
| 1997 | break; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 1998 | |
| 1999 | case LOG_FMT_UNIQUEID: // %ID |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2000 | ret = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2001 | src = s->unique_id; |
Thierry FOURNIER | 1be6910 | 2014-04-15 01:38:48 +0200 | [diff] [blame] | 2002 | ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2003 | if (ret == NULL) |
| 2004 | goto out; |
| 2005 | tmplog = ret; |
| 2006 | last_isspace = 0; |
| 2007 | break; |
| 2008 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | out: |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2013 | /* *tmplog is a unused character */ |
| 2014 | *tmplog = '\0'; |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 2015 | return tmplog - dst; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2016 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2017 | } |
| 2018 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2019 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2020 | * 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] | 2021 | * Will not log if the frontend has no log defined. |
| 2022 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2023 | void strm_log(struct stream *s) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2024 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2025 | struct session *sess = s->sess; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2026 | int size, err, level; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2027 | int sd_size = 0; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2028 | |
| 2029 | /* if we don't want to log normal traffic, return now */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2030 | err = (s->flags & SF_REDISP) || |
| 2031 | ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) || |
| 2032 | (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) && |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2033 | (s->si[1].conn_retries != s->be->conn_retries)) || |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2034 | ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2035 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2036 | if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2037 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2038 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2039 | if (LIST_ISEMPTY(&sess->fe->logsrvs)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2040 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2041 | |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2042 | if (s->logs.level) { /* loglevel was overridden */ |
| 2043 | if (s->logs.level == -1) { |
| 2044 | s->logs.logwait = 0; /* logs disabled */ |
| 2045 | return; |
| 2046 | } |
| 2047 | level = s->logs.level - 1; |
| 2048 | } |
| 2049 | else { |
| 2050 | level = LOG_INFO; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2051 | if (err && (sess->fe->options2 & PR_O2_LOGERRORS)) |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2052 | level = LOG_ERR; |
| 2053 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2054 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2055 | /* if unique-id was not generated */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2056 | if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2057 | if ((s->unique_id = pool_alloc2(pool2_uniqueid)) != NULL) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2058 | 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] | 2059 | } |
| 2060 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2061 | if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) { |
| 2062 | sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len, |
| 2063 | &sess->fe->logformat_sd); |
| 2064 | } |
| 2065 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 2066 | size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2067 | if (size > 0) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2068 | sess->fe->log_count++; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2069 | __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2070 | s->logs.logwait = 0; |
| 2071 | } |
| 2072 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2073 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2074 | /* |
| 2075 | * Local variables: |
| 2076 | * c-indent-level: 8 |
| 2077 | * c-basic-offset: 8 |
| 2078 | * End: |
| 2079 | */ |