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> |
Willy Tarreau | 077edcb | 2016-08-10 18:30:56 +0200 | [diff] [blame] | 25 | #include <sys/uio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 27 | #include <common/config.h> |
Willy Tarreau | d6d0690 | 2009-08-19 11:22:33 +0200 | [diff] [blame] | 28 | #include <common/compat.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 29 | #include <common/initcall.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/standard.h> |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 31 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | |
Christopher Faulet | c1b730a | 2017-10-24 12:00:51 +0200 | [diff] [blame] | 33 | #include <types/cli.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | #include <types/global.h> |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 35 | #include <types/log.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 36 | |
Christopher Faulet | c1b730a | 2017-10-24 12:00:51 +0200 | [diff] [blame] | 37 | #include <proto/applet.h> |
| 38 | #include <proto/cli.h> |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 39 | #include <proto/frontend.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 40 | #include <proto/log.h> |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 41 | #include <proto/sample.h> |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 42 | #include <proto/stream.h> |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 43 | #include <proto/stream_interface.h> |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 44 | #ifdef USE_OPENSSL |
| 45 | #include <proto/ssl_sock.h> |
| 46 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 48 | struct log_fmt { |
| 49 | char *name; |
| 50 | struct { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 51 | struct buffer sep1; /* first pid separator */ |
| 52 | struct buffer sep2; /* second pid separator */ |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 53 | } pid; |
| 54 | }; |
| 55 | |
| 56 | static const struct log_fmt log_formats[LOG_FORMATS] = { |
| 57 | [LOG_FORMAT_RFC3164] = { |
| 58 | .name = "rfc3164", |
| 59 | .pid = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 60 | .sep1 = { .area = "[", .data = 1 }, |
| 61 | .sep2 = { .area = "]: ", .data = 3 } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 62 | } |
| 63 | }, |
| 64 | [LOG_FORMAT_RFC5424] = { |
| 65 | .name = "rfc5424", |
| 66 | .pid = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 67 | .sep1 = { .area = " ", .data = 1 }, |
| 68 | .sep2 = { .area = " - ", .data = 3 } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 69 | } |
Willy Tarreau | e8746a0 | 2018-11-12 08:45:00 +0100 | [diff] [blame] | 70 | }, |
| 71 | [LOG_FORMAT_SHORT] = { |
| 72 | .name = "short", |
| 73 | .pid = { |
| 74 | .sep1 = { .area = "", .data = 0 }, |
| 75 | .sep2 = { .area = " ", .data = 1 }, |
| 76 | } |
| 77 | }, |
Willy Tarreau | c1b0645 | 2018-11-12 11:57:56 +0100 | [diff] [blame] | 78 | [LOG_FORMAT_RAW] = { |
| 79 | .name = "raw", |
| 80 | .pid = { |
| 81 | .sep1 = { .area = "", .data = 0 }, |
| 82 | .sep2 = { .area = "", .data = 0 }, |
| 83 | } |
| 84 | }, |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 87 | #define FD_SETS_ARE_BITFIELDS |
| 88 | #ifdef FD_SETS_ARE_BITFIELDS |
| 89 | /* |
| 90 | * This map is used with all the FD_* macros to check whether a particular bit |
| 91 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 92 | * which should be escaped. When FD_ISSET() returns non-zero, it means that the |
| 93 | * byte should be escaped. Be careful to always pass bytes from 0 to 255 |
| 94 | * exclusively to the macros. |
| 95 | */ |
| 96 | fd_set rfc5424_escape_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 97 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 98 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 99 | fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 100 | |
| 101 | #else |
| 102 | #error "Check if your OS uses bitfields for fd_sets" |
| 103 | #endif |
| 104 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 106 | "kern", "user", "mail", "daemon", |
| 107 | "auth", "syslog", "lpr", "news", |
| 108 | "uucp", "cron", "auth2", "ftp", |
| 109 | "ntp", "audit", "alert", "cron2", |
| 110 | "local0", "local1", "local2", "local3", |
| 111 | "local4", "local5", "local6", "local7" |
| 112 | }; |
| 113 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | const char *log_levels[NB_LOG_LEVELS] = { |
| 115 | "emerg", "alert", "crit", "err", |
| 116 | "warning", "notice", "info", "debug" |
| 117 | }; |
| 118 | |
Willy Tarreau | 570f221 | 2013-06-10 16:42:09 +0200 | [diff] [blame] | 119 | 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] | 120 | 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] | 121 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 122 | |
| 123 | /* log_format */ |
| 124 | struct logformat_type { |
| 125 | char *name; |
| 126 | int type; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 127 | int mode; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 128 | int lw; /* logwait bitsfield */ |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 129 | int (*config_callback)(struct logformat_node *node, struct proxy *curproxy); |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 130 | const char *replace_by; /* new option to use instead of old one */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 133 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy); |
| 134 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 135 | /* log_format variable names */ |
| 136 | static const struct logformat_type logformat_keywords[] = { |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 137 | { "o", LOG_FMT_GLOBAL, PR_MODE_TCP, 0, NULL }, /* global option */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 138 | |
| 139 | /* please keep these lines sorted ! */ |
| 140 | { "B", LOG_FMT_BYTES, PR_MODE_TCP, LW_BYTES, NULL }, /* bytes from server to client */ |
| 141 | { "CC", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL }, /* client cookie */ |
| 142 | { "CS", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL }, /* server cookie */ |
| 143 | { "H", LOG_FMT_HOSTNAME, PR_MODE_TCP, LW_INIT, NULL }, /* Hostname */ |
| 144 | { "ID", LOG_FMT_UNIQUEID, PR_MODE_HTTP, LW_BYTES, NULL }, /* Unique ID */ |
Willy Tarreau | 4bf9963 | 2014-06-13 12:21:40 +0200 | [diff] [blame] | 145 | { "ST", LOG_FMT_STATUS, PR_MODE_TCP, LW_RESP, NULL }, /* status code */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 146 | { "T", LOG_FMT_DATEGMT, PR_MODE_TCP, LW_INIT, NULL }, /* date GMT */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 147 | { "Ta", LOG_FMT_Ta, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time active (tr to end) */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 148 | { "Tc", LOG_FMT_TC, PR_MODE_TCP, LW_BYTES, NULL }, /* Tc */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 149 | { "Th", LOG_FMT_Th, PR_MODE_TCP, LW_BYTES, NULL }, /* Time handshake */ |
| 150 | { "Ti", LOG_FMT_Ti, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time idle */ |
| 151 | { "Tl", LOG_FMT_DATELOCAL, PR_MODE_TCP, LW_INIT, NULL }, /* date local timezone */ |
| 152 | { "Tq", LOG_FMT_TQ, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tq=Th+Ti+TR */ |
| 153 | { "Tr", LOG_FMT_Tr, PR_MODE_HTTP, LW_BYTES, NULL }, /* Tr */ |
| 154 | { "TR", LOG_FMT_TR, PR_MODE_HTTP, LW_BYTES, NULL }, /* Time to receive a valid request */ |
Willy Tarreau | 27b639d | 2016-05-17 17:55:27 +0200 | [diff] [blame] | 155 | { "Td", LOG_FMT_TD, PR_MODE_TCP, LW_BYTES, NULL }, /* Td = Tt - (Tq + Tw + Tc + Tr) */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 156 | { "Ts", LOG_FMT_TS, PR_MODE_TCP, LW_INIT, NULL }, /* timestamp GMT */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 157 | { "Tt", LOG_FMT_TT, PR_MODE_TCP, LW_BYTES, NULL }, /* Tt */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 158 | { "Tw", LOG_FMT_TW, PR_MODE_TCP, LW_BYTES, NULL }, /* Tw */ |
| 159 | { "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] | 160 | { "ac", LOG_FMT_ACTCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* actconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 161 | { "b", LOG_FMT_BACKEND, PR_MODE_TCP, LW_INIT, NULL }, /* backend */ |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 162 | { "bc", LOG_FMT_BECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* beconn */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 163 | { "bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource }, /* backend source ip */ |
| 164 | { "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] | 165 | { "bq", LOG_FMT_BCKQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* backend_queue */ |
Willy Tarreau | d02286d | 2017-06-23 11:23:43 +0200 | [diff] [blame] | 166 | { "ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client ip */ |
| 167 | { "cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL }, /* client port */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 168 | { "f", LOG_FMT_FRONTEND, PR_MODE_TCP, LW_INIT, NULL }, /* frontend */ |
| 169 | { "fc", LOG_FMT_FECONN, PR_MODE_TCP, LW_BYTES, NULL }, /* feconn */ |
Willy Tarreau | d02286d | 2017-06-23 11:23:43 +0200 | [diff] [blame] | 170 | { "fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend ip */ |
| 171 | { "fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL }, /* frontend port */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 172 | { "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] | 173 | { "hr", LOG_FMT_HDRREQUEST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request */ |
| 174 | { "hrl", LOG_FMT_HDRREQUESTLIST, PR_MODE_TCP, LW_REQHDR, NULL }, /* header request list */ |
| 175 | { "hs", LOG_FMT_HDRRESPONS, PR_MODE_TCP, LW_RSPHDR, NULL }, /* header response */ |
| 176 | { "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] | 177 | { "HM", LOG_FMT_HTTP_METHOD, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP method */ |
| 178 | { "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] | 179 | { "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] | 180 | { "HU", LOG_FMT_HTTP_URI, PR_MODE_HTTP, LW_REQ, NULL }, /* HTTP full URI */ |
| 181 | { "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] | 182 | { "lc", LOG_FMT_LOGCNT, PR_MODE_TCP, LW_INIT, NULL }, /* log counter */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 183 | { "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] | 184 | { "pid", LOG_FMT_PID, PR_MODE_TCP, LW_INIT, NULL }, /* log pid */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 185 | { "r", LOG_FMT_REQ, PR_MODE_HTTP, LW_REQ, NULL }, /* request */ |
| 186 | { "rc", LOG_FMT_RETRIES, PR_MODE_TCP, LW_BYTES, NULL }, /* retries */ |
Willy Tarreau | 1f0da24 | 2014-01-25 11:01:50 +0100 | [diff] [blame] | 187 | { "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] | 188 | { "s", LOG_FMT_SERVER, PR_MODE_TCP, LW_SVID, NULL }, /* server */ |
| 189 | { "sc", LOG_FMT_SRVCONN, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_conn */ |
| 190 | { "si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination ip */ |
| 191 | { "sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL }, /* server destination port */ |
| 192 | { "sq", LOG_FMT_SRVQUEUE, PR_MODE_TCP, LW_BYTES, NULL }, /* srv_queue */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 193 | { "sslc", LOG_FMT_SSL_CIPHER, PR_MODE_TCP, LW_XPRT, NULL }, /* client-side SSL ciphers */ |
| 194 | { "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] | 195 | { "t", LOG_FMT_DATE, PR_MODE_TCP, LW_INIT, NULL }, /* date */ |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 196 | { "tr", LOG_FMT_tr, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request */ |
| 197 | { "trg",LOG_FMT_trg, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, GMT */ |
| 198 | { "trl",LOG_FMT_trl, PR_MODE_HTTP, LW_INIT, NULL }, /* date of start of request, local */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 199 | { "ts", LOG_FMT_TERMSTATE, PR_MODE_TCP, LW_BYTES, NULL },/* termination state */ |
| 200 | { "tsc", LOG_FMT_TERMSTATE_CK, PR_MODE_TCP, LW_INIT, NULL },/* termination state */ |
| 201 | |
| 202 | /* The following tags are deprecated and will be removed soon */ |
| 203 | { "Bi", LOG_FMT_BACKENDIP, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bi" }, /* backend source ip */ |
| 204 | { "Bp", LOG_FMT_BACKENDPORT, PR_MODE_TCP, LW_BCKIP, prepare_addrsource, "bp" }, /* backend source port */ |
Willy Tarreau | d02286d | 2017-06-23 11:23:43 +0200 | [diff] [blame] | 205 | { "Ci", LOG_FMT_CLIENTIP, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "ci" }, /* client ip */ |
| 206 | { "Cp", LOG_FMT_CLIENTPORT, PR_MODE_TCP, LW_CLIP | LW_XPRT, NULL, "cp" }, /* client port */ |
| 207 | { "Fi", LOG_FMT_FRONTENDIP, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fi" }, /* frontend ip */ |
| 208 | { "Fp", LOG_FMT_FRONTENDPORT, PR_MODE_TCP, LW_FRTIP | LW_XPRT, NULL, "fp" }, /* frontend port */ |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 209 | { "Si", LOG_FMT_SERVERIP, PR_MODE_TCP, LW_SVIP, NULL, "si" }, /* server destination ip */ |
| 210 | { "Sp", LOG_FMT_SERVERPORT, PR_MODE_TCP, LW_SVIP, NULL, "sp" }, /* server destination port */ |
| 211 | { "cc", LOG_FMT_CCLIENT, PR_MODE_HTTP, LW_REQHDR, NULL, "CC" }, /* client cookie */ |
| 212 | { "cs", LOG_FMT_CSERVER, PR_MODE_HTTP, LW_RSPHDR, NULL, "CS" }, /* server cookie */ |
| 213 | { "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] | 214 | { 0, 0, 0, 0, NULL } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 215 | }; |
| 216 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 217 | char default_http_log_format[] = "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format |
| 218 | char clf_http_log_format[] = "%{+Q}o %{-Q}ci - - [%trg] %r %ST %B \"\" \"\" %cp %ms %ft %b %s %TR %Tw %Tc %Tr %Ta %tsc %ac %fc %bc %sc %rc %sq %bq %CC %CS %hrl %hsl"; |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 219 | 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] | 220 | char *log_format = NULL; |
| 221 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 222 | /* Default string used for structured-data part in RFC5424 formatted |
| 223 | * syslog messages. |
| 224 | */ |
| 225 | char default_rfc5424_sd_log_format[] = "- "; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 226 | |
Willy Tarreau | 13ef773 | 2018-11-12 07:25:28 +0100 | [diff] [blame] | 227 | /* total number of dropped logs */ |
| 228 | unsigned int dropped_logs = 0; |
| 229 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 230 | /* This is a global syslog header, common to all outgoing messages in |
| 231 | * RFC3164 format. It begins with time-based part and is updated by |
| 232 | * update_log_hdr(). |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 233 | */ |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 234 | THREAD_LOCAL char *logheader = NULL; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 235 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 236 | /* This is a global syslog header for messages in RFC5424 format. It is |
| 237 | * updated by update_log_hdr_rfc5424(). |
| 238 | */ |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 239 | THREAD_LOCAL char *logheader_rfc5424 = NULL; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 240 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 241 | /* This is a global syslog message buffer, common to all outgoing |
| 242 | * messages. It contains only the data part. |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 243 | */ |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 244 | THREAD_LOCAL char *logline = NULL; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 245 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 246 | /* A global syslog message buffer, common to all RFC5424 syslog messages. |
| 247 | * Currently, it is used for generating the structured-data part. |
| 248 | */ |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 249 | THREAD_LOCAL char *logline_rfc5424 = NULL; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 250 | |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 251 | /* A global buffer used to store all startup alerts/warnings. It will then be |
| 252 | * retrieve on the CLI. */ |
Willy Tarreau | a648399 | 2018-12-15 16:55:36 +0100 | [diff] [blame] | 253 | static char *startup_logs = NULL; |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 254 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 255 | struct logformat_var_args { |
| 256 | char *name; |
| 257 | int mask; |
| 258 | }; |
| 259 | |
| 260 | struct logformat_var_args var_args_list[] = { |
| 261 | // global |
| 262 | { "M", LOG_OPT_MANDATORY }, |
| 263 | { "Q", LOG_OPT_QUOTE }, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 264 | { "X", LOG_OPT_HEXA }, |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 265 | { "E", LOG_OPT_ESC }, |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 266 | { 0, 0 } |
| 267 | }; |
| 268 | |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 269 | /* return the name of the directive used in the current proxy for which we're |
| 270 | * currently parsing a header, when it is known. |
| 271 | */ |
| 272 | static inline const char *fmt_directive(const struct proxy *curproxy) |
| 273 | { |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 274 | switch (curproxy->conf.args.ctx) { |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 275 | case ARGC_ACL: |
| 276 | return "acl"; |
| 277 | case ARGC_STK: |
| 278 | return "stick"; |
| 279 | case ARGC_TRK: |
| 280 | return "track-sc"; |
| 281 | case ARGC_LOG: |
| 282 | return "log-format"; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 283 | case ARGC_LOGSD: |
| 284 | return "log-format-sd"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 285 | case ARGC_HRQ: |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 286 | return "http-request"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 287 | case ARGC_HRS: |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 288 | return "http-response"; |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 289 | case ARGC_UIF: |
| 290 | return "unique-id-format"; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 291 | case ARGC_RDR: |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 292 | return "redirect"; |
| 293 | case ARGC_CAP: |
| 294 | return "capture"; |
Willy Tarreau | 28d976d | 2015-07-09 11:39:33 +0200 | [diff] [blame] | 295 | case ARGC_SRV: |
| 296 | return "server"; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 297 | case ARGC_SPOE: |
| 298 | return "spoe-message"; |
Thierry FOURNIER / OZON.IO | 4ed1c95 | 2016-11-24 23:57:54 +0100 | [diff] [blame] | 299 | case ARGC_UBK: |
| 300 | return "use_backend"; |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 301 | default: |
Willy Tarreau | 53e1a6d | 2015-07-09 11:20:00 +0200 | [diff] [blame] | 302 | return "undefined(please report this bug)"; /* must never happen */ |
Willy Tarreau | bf0addb | 2013-12-02 12:24:54 +0100 | [diff] [blame] | 303 | } |
Willy Tarreau | b1f3af2 | 2013-04-12 18:30:32 +0200 | [diff] [blame] | 304 | } |
| 305 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 306 | /* |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 307 | * callback used to configure addr source retrieval |
| 308 | */ |
| 309 | int prepare_addrsource(struct logformat_node *node, struct proxy *curproxy) |
| 310 | { |
| 311 | curproxy->options2 |= PR_O2_SRC_ADDR; |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /* |
Thierry FOURNIER / OZON.IO | bca46f0 | 2016-11-22 23:13:04 +0100 | [diff] [blame] | 318 | * Parse args in a logformat_var. Returns 0 in error |
| 319 | * case, otherwise, it returns 1. |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 320 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 321 | int parse_logformat_var_args(char *args, struct logformat_node *node, char **err) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 322 | { |
| 323 | int i = 0; |
| 324 | int end = 0; |
| 325 | int flags = 0; // 1 = + 2 = - |
| 326 | char *sp = NULL; // start pointer |
| 327 | |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 328 | if (args == NULL) { |
| 329 | memprintf(err, "internal error: parse_logformat_var_args() expects non null 'args'"); |
Thierry FOURNIER / OZON.IO | bca46f0 | 2016-11-22 23:13:04 +0100 | [diff] [blame] | 330 | return 0; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 331 | } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 332 | |
| 333 | while (1) { |
| 334 | if (*args == '\0') |
| 335 | end = 1; |
| 336 | |
| 337 | if (*args == '+') { |
| 338 | // add flag |
| 339 | sp = args + 1; |
| 340 | flags = 1; |
| 341 | } |
| 342 | if (*args == '-') { |
| 343 | // delete flag |
| 344 | sp = args + 1; |
| 345 | flags = 2; |
| 346 | } |
| 347 | |
| 348 | if (*args == '\0' || *args == ',') { |
| 349 | *args = '\0'; |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 350 | for (i = 0; sp && var_args_list[i].name; i++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 351 | if (strcmp(sp, var_args_list[i].name) == 0) { |
| 352 | if (flags == 1) { |
| 353 | node->options |= var_args_list[i].mask; |
| 354 | break; |
| 355 | } else if (flags == 2) { |
| 356 | node->options &= ~var_args_list[i].mask; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | sp = NULL; |
| 362 | if (end) |
| 363 | break; |
| 364 | } |
Willy Tarreau | 254d44c | 2012-12-20 18:19:26 +0100 | [diff] [blame] | 365 | args++; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 366 | } |
Thierry FOURNIER / OZON.IO | bca46f0 | 2016-11-22 23:13:04 +0100 | [diff] [blame] | 367 | return 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 371 | * Parse a variable '%varname' or '%{args}varname' in log-format. The caller |
| 372 | * must pass the args part in the <arg> pointer with its length in <arg_len>, |
| 373 | * and varname with its length in <var> and <var_len> respectively. <arg> is |
| 374 | * ignored when arg_len is 0. Neither <var> nor <var_len> may be null. |
Thierry FOURNIER / OZON.IO | eca4d95 | 2016-11-22 22:06:04 +0100 | [diff] [blame] | 375 | * Returns false in error case and err is filled, otherwise returns true. |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 376 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 377 | int parse_logformat_var(char *arg, int arg_len, char *var, int var_len, struct proxy *curproxy, struct list *list_format, int *defoptions, char **err) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 378 | { |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 379 | int j; |
| 380 | struct logformat_node *node; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 381 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 382 | for (j = 0; logformat_keywords[j].name; j++) { // search a log type |
| 383 | if (strlen(logformat_keywords[j].name) == var_len && |
| 384 | strncmp(var, logformat_keywords[j].name, var_len) == 0) { |
| 385 | if (logformat_keywords[j].mode != PR_MODE_HTTP || curproxy->mode == PR_MODE_HTTP) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 386 | node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 387 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 388 | memprintf(err, "out of memory error"); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 389 | return 0; |
| 390 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 391 | node->type = logformat_keywords[j].type; |
| 392 | node->options = *defoptions; |
| 393 | if (arg_len) { |
| 394 | node->arg = my_strndup(arg, arg_len); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 395 | if (!parse_logformat_var_args(node->arg, node, err)) |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 396 | return 0; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 397 | } |
| 398 | if (node->type == LOG_FMT_GLOBAL) { |
| 399 | *defoptions = node->options; |
| 400 | free(node->arg); |
| 401 | free(node); |
| 402 | } else { |
| 403 | if (logformat_keywords[j].config_callback && |
| 404 | logformat_keywords[j].config_callback(node, curproxy) != 0) { |
Thierry FOURNIER / OZON.IO | eca4d95 | 2016-11-22 22:06:04 +0100 | [diff] [blame] | 405 | return 0; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 406 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 407 | curproxy->to_log |= logformat_keywords[j].lw; |
| 408 | LIST_ADDQ(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 409 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 410 | if (logformat_keywords[j].replace_by) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 411 | ha_warning("parsing [%s:%d] : deprecated variable '%s' in '%s', please replace it with '%s'.\n", |
| 412 | curproxy->conf.args.file, curproxy->conf.args.line, |
| 413 | logformat_keywords[j].name, fmt_directive(curproxy), logformat_keywords[j].replace_by); |
Thierry FOURNIER / OZON.IO | eca4d95 | 2016-11-22 22:06:04 +0100 | [diff] [blame] | 414 | return 1; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 415 | } else { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 416 | memprintf(err, "format variable '%s' is reserved for HTTP mode", |
| 417 | logformat_keywords[j].name); |
Thierry FOURNIER / OZON.IO | eca4d95 | 2016-11-22 22:06:04 +0100 | [diff] [blame] | 418 | return 0; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 419 | } |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 420 | } |
| 421 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 422 | |
| 423 | j = var[var_len]; |
| 424 | var[var_len] = 0; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 425 | memprintf(err, "no such format variable '%s'. If you wanted to emit the '%%' character verbatim, you need to use '%%%%'", var); |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 426 | var[var_len] = j; |
Thierry FOURNIER / OZON.IO | eca4d95 | 2016-11-22 22:06:04 +0100 | [diff] [blame] | 427 | return 0; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* |
| 431 | * push to the logformat linked list |
| 432 | * |
| 433 | * start: start pointer |
| 434 | * end: end text pointer |
| 435 | * type: string type |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 436 | * list_format: destination list |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 437 | * |
| 438 | * LOG_TEXT: copy chars from start to end excluding end. |
| 439 | * |
| 440 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 441 | int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 442 | { |
| 443 | char *str; |
| 444 | |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 445 | if (type == LF_TEXT) { /* type text */ |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 446 | struct logformat_node *node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 447 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 448 | memprintf(err, "out of memory error"); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 449 | return 0; |
| 450 | } |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 451 | str = calloc(1, end - start + 1); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 452 | strncpy(str, start, end - start); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 453 | str[end - start] = '\0'; |
| 454 | node->arg = str; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 455 | node->type = LOG_FMT_TEXT; // type string |
| 456 | LIST_ADDQ(list_format, &node->list); |
Willy Tarreau | a357166 | 2012-12-20 21:59:12 +0100 | [diff] [blame] | 457 | } else if (type == LF_SEPARATOR) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 458 | struct logformat_node *node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 459 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 460 | memprintf(err, "out of memory error"); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 461 | return 0; |
| 462 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 463 | node->type = LOG_FMT_SEPARATOR; |
| 464 | LIST_ADDQ(list_format, &node->list); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 465 | } |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 466 | return 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 470 | * Parse the sample fetch expression <text> and add a node to <list_format> upon |
| 471 | * 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] | 472 | * should work. The curpx->conf.args.ctx must be set by the caller. |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 473 | * |
| 474 | * In error case, the function returns 0, otherwise it returns 1. |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 475 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 476 | int add_sample_to_logformat_list(char *text, char *arg, int arg_len, struct proxy *curpx, struct list *list_format, int options, int cap, char **err) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 477 | { |
| 478 | char *cmd[2]; |
| 479 | struct sample_expr *expr; |
| 480 | struct logformat_node *node; |
| 481 | int cmd_arg; |
| 482 | |
| 483 | cmd[0] = text; |
| 484 | cmd[1] = ""; |
| 485 | cmd_arg = 0; |
| 486 | |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 487 | expr = sample_parse_expr(cmd, &cmd_arg, curpx->conf.args.file, curpx->conf.args.line, err, &curpx->conf.args); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 488 | if (!expr) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 489 | memprintf(err, "failed to parse sample expression <%s> : %s", text, *err); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 490 | return 0; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 491 | } |
| 492 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 493 | node = calloc(1, sizeof(*node)); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 494 | if (!node) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 495 | memprintf(err, "out of memory error"); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 496 | return 0; |
| 497 | } |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 498 | node->type = LOG_FMT_EXPR; |
| 499 | node->expr = expr; |
| 500 | node->options = options; |
| 501 | |
| 502 | if (arg_len) { |
| 503 | node->arg = my_strndup(arg, arg_len); |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 504 | if (!parse_logformat_var_args(node->arg, node, err)) |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 505 | return 0; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 506 | } |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 507 | if (expr->fetch->val & cap & SMP_VAL_REQUEST) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 508 | node->options |= LOG_OPT_REQ_CAP; /* fetch method is request-compatible */ |
| 509 | |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 510 | if (expr->fetch->val & cap & SMP_VAL_RESPONSE) |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 511 | node->options |= LOG_OPT_RES_CAP; /* fetch method is response-compatible */ |
| 512 | |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 513 | if (!(expr->fetch->val & cap)) { |
David Carlier | 93e8b88 | 2017-09-21 14:36:43 +0000 | [diff] [blame] | 514 | free(node); |
| 515 | node = NULL; |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 516 | memprintf(err, "sample fetch <%s> may not be reliably used here because it needs '%s' which is not available here", |
| 517 | text, sample_src_names(expr->fetch->use)); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 518 | return 0; |
| 519 | } |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 520 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 521 | /* check if we need to allocate an hdr_idx struct for HTTP parsing */ |
| 522 | /* 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] | 523 | curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 524 | |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 525 | /* FIXME: temporary workaround for missing LW_XPRT and LW_REQ flags |
| 526 | * needed with some sample fetches (eg: ssl*). We always set it for |
| 527 | * now on, but this will leave with sample capabilities soon. |
Willy Tarreau | 1f31c73 | 2013-01-10 16:22:27 +0100 | [diff] [blame] | 528 | */ |
| 529 | curpx->to_log |= LW_XPRT; |
William Lallemand | 65ad6e1 | 2014-01-31 15:08:02 +0100 | [diff] [blame] | 530 | curpx->to_log |= LW_REQ; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 531 | LIST_ADDQ(list_format, &node->list); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 532 | return 1; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /* |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 536 | * Parse the log_format string and fill a linked list. |
| 537 | * 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] | 538 | * You can set arguments using { } : %{many arguments}varname. |
| 539 | * The curproxy->conf.args.ctx must be set by the caller. |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 540 | * |
| 541 | * str: the string to parse |
| 542 | * curproxy: the proxy affected |
| 543 | * list_format: the destination list |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 544 | * options: LOG_OPT_* to force on every node |
Willy Tarreau | 434c57c | 2013-01-08 01:10:24 +0100 | [diff] [blame] | 545 | * cap: all SMP_VAL_* flags supported by the consumer |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 546 | * |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 547 | * The function returns 1 in success case, otherwise, it returns 0 and err is filled. |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 548 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 549 | int parse_logformat_string(const char *fmt, struct proxy *curproxy, struct list *list_format, int options, int cap, char **err) |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 550 | { |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 551 | char *sp, *str, *backfmt; /* start pointer for text parts */ |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 552 | char *arg = NULL; /* start pointer for args */ |
| 553 | char *var = NULL; /* start pointer for vars */ |
| 554 | int arg_len = 0; |
| 555 | int var_len = 0; |
| 556 | int cformat; /* current token format */ |
| 557 | int pformat; /* previous token format */ |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 558 | struct logformat_node *tmplf, *back; |
| 559 | |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 560 | sp = str = backfmt = strdup(fmt); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 561 | if (!str) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 562 | memprintf(err, "out of memory error"); |
Thierry FOURNIER / OZON.IO | 9cbfef2 | 2016-11-22 23:24:10 +0100 | [diff] [blame] | 563 | return 0; |
| 564 | } |
William Lallemand | 1dc00ef | 2012-08-09 16:41:35 +0200 | [diff] [blame] | 565 | curproxy->to_log |= LW_INIT; |
William Lallemand | 5e19a28 | 2012-04-02 16:22:10 +0200 | [diff] [blame] | 566 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 567 | /* flush the list first. */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 568 | list_for_each_entry_safe(tmplf, back, list_format, list) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 569 | LIST_DEL(&tmplf->list); |
| 570 | free(tmplf); |
| 571 | } |
| 572 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 573 | for (cformat = LF_INIT; cformat != LF_END; str++) { |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 574 | pformat = cformat; |
| 575 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 576 | if (!*str) |
| 577 | cformat = LF_END; // preset it to save all states from doing this |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 578 | |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 579 | /* The principle of the two-step state machine below is to first detect a change, and |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 580 | * second have all common paths processed at one place. The common paths are the ones |
| 581 | * encountered in text areas (LF_INIT, LF_TEXT, LF_SEPARATOR) and at the end (LF_END). |
| 582 | * We use the common LF_INIT state to dispatch to the different final states. |
| 583 | */ |
| 584 | switch (pformat) { |
| 585 | case LF_STARTVAR: // text immediately following a '%' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 586 | arg = NULL; var = NULL; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 587 | arg_len = var_len = 0; |
| 588 | if (*str == '{') { // optional argument |
| 589 | cformat = LF_STARG; |
| 590 | arg = str + 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 591 | } |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 592 | else if (*str == '[') { |
| 593 | cformat = LF_STEXPR; |
| 594 | var = str + 1; // store expr in variable name |
| 595 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 596 | else if (isalpha((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 597 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 598 | var = str; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 599 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 600 | else if (*str == '%') |
| 601 | cformat = LF_TEXT; // convert this character to a litteral (useful for '%') |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 602 | else if (isdigit((unsigned char)*str) || *str == ' ' || *str == '\t') { |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 603 | /* single '%' followed by blank or digit, send them both */ |
| 604 | cformat = LF_TEXT; |
| 605 | pformat = LF_TEXT; /* finally we include the previous char as well */ |
| 606 | sp = str - 1; /* send both the '%' and the current char */ |
Jim Freeman | a2278c8 | 2017-04-15 08:01:59 -0600 | [diff] [blame] | 607 | memprintf(err, "unexpected variable name near '%c' at position %d line : '%s'. Maybe you want to write a single '%%', use the syntax '%%%%'", |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 608 | *str, (int)(str - backfmt), fmt); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 609 | return 0; |
Willy Tarreau | 06d97f9 | 2013-12-02 17:45:48 +0100 | [diff] [blame] | 610 | |
| 611 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 612 | else |
| 613 | cformat = LF_INIT; // handle other cases of litterals |
| 614 | break; |
| 615 | |
| 616 | case LF_STARG: // text immediately following '%{' |
| 617 | if (*str == '}') { // end of arg |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 618 | cformat = LF_EDARG; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 619 | arg_len = str - arg; |
| 620 | *str = 0; // used for reporting errors |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 621 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 622 | break; |
| 623 | |
| 624 | case LF_EDARG: // text immediately following '%{arg}' |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 625 | if (*str == '[') { |
| 626 | cformat = LF_STEXPR; |
| 627 | var = str + 1; // store expr in variable name |
| 628 | break; |
| 629 | } |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 630 | else if (isalnum((unsigned char)*str)) { // variable name |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 631 | cformat = LF_VAR; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 632 | var = str; |
| 633 | break; |
| 634 | } |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 635 | memprintf(err, "parse argument modifier without variable name near '%%{%s}'", arg); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 636 | return 0; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 637 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 638 | case LF_STEXPR: // text immediately following '%[' |
| 639 | if (*str == ']') { // end of arg |
| 640 | cformat = LF_EDEXPR; |
| 641 | var_len = str - var; |
| 642 | *str = 0; // needed for parsing the expression |
| 643 | } |
| 644 | break; |
| 645 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 646 | case LF_VAR: // text part of a variable name |
| 647 | var_len = str - var; |
Willy Tarreau | 0f28f82 | 2013-12-16 01:38:33 +0100 | [diff] [blame] | 648 | if (!isalnum((unsigned char)*str)) |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 649 | cformat = LF_INIT; // not variable name anymore |
| 650 | break; |
| 651 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 652 | default: // LF_INIT, LF_TEXT, LF_SEPARATOR, LF_END, LF_EDEXPR |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 653 | cformat = LF_INIT; |
| 654 | } |
| 655 | |
| 656 | if (cformat == LF_INIT) { /* resynchronize state to text/sep/startvar */ |
| 657 | switch (*str) { |
| 658 | case '%': cformat = LF_STARTVAR; break; |
| 659 | case ' ': cformat = LF_SEPARATOR; break; |
| 660 | case 0 : cformat = LF_END; break; |
| 661 | default : cformat = LF_TEXT; break; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 665 | if (cformat != pformat || pformat == LF_SEPARATOR) { |
| 666 | switch (pformat) { |
| 667 | case LF_VAR: |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 668 | if (!parse_logformat_var(arg, arg_len, var, var_len, curproxy, list_format, &options, err)) |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 669 | return 0; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 670 | break; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 671 | case LF_STEXPR: |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 672 | if (!add_sample_to_logformat_list(var, arg, arg_len, curproxy, list_format, options, cap, err)) |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 673 | return 0; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 674 | break; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 675 | case LF_TEXT: |
| 676 | case LF_SEPARATOR: |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 677 | if (!add_to_logformat_list(sp, str, pformat, list_format, err)) |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 678 | return 0; |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 679 | break; |
| 680 | } |
| 681 | 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] | 682 | } |
| 683 | } |
Willy Tarreau | 8a3f52f | 2012-12-20 21:23:42 +0100 | [diff] [blame] | 684 | |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 685 | if (pformat == LF_STARTVAR || pformat == LF_STARG || pformat == LF_STEXPR) { |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 686 | memprintf(err, "truncated line after '%s'", var ? var : arg ? arg : "%"); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 687 | return 0; |
| 688 | } |
Willy Tarreau | b83bc1e | 2012-12-24 12:36:33 +0100 | [diff] [blame] | 689 | free(backfmt); |
Thierry FOURNIER / OZON.IO | a2c38d7 | 2016-11-22 23:11:21 +0100 | [diff] [blame] | 690 | |
| 691 | return 1; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 692 | } |
| 693 | |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 694 | /* |
| 695 | * Parse "log" keyword and update <logsrvs> list accordingly. |
| 696 | * |
| 697 | * When <do_del> is set, it means the "no log" line was parsed, so all log |
| 698 | * servers in <logsrvs> are released. |
| 699 | * |
| 700 | * Otherwise, we try to parse the "log" line. First of all, when the list is not |
| 701 | * the global one, we look for the parameter "global". If we find it, |
| 702 | * global.logsrvs is copied. Else we parse each arguments. |
| 703 | * |
| 704 | * The function returns 1 in success case, otherwise, it returns 0 and err is |
| 705 | * filled. |
| 706 | */ |
| 707 | int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err) |
| 708 | { |
| 709 | struct sockaddr_storage *sk; |
| 710 | struct logsrv *logsrv = NULL; |
| 711 | int port1, port2; |
| 712 | int cur_arg; |
| 713 | |
| 714 | /* |
| 715 | * "no log": delete previous herited or defined syslog |
| 716 | * servers. |
| 717 | */ |
| 718 | if (do_del) { |
| 719 | struct logsrv *back; |
| 720 | |
| 721 | if (*(args[1]) != 0) { |
| 722 | memprintf(err, "'no log' does not expect arguments"); |
| 723 | goto error; |
| 724 | } |
| 725 | |
| 726 | list_for_each_entry_safe(logsrv, back, logsrvs, list) { |
| 727 | LIST_DEL(&logsrv->list); |
| 728 | free(logsrv); |
| 729 | } |
| 730 | return 1; |
| 731 | } |
| 732 | |
| 733 | /* |
| 734 | * "log global": copy global.logrsvs linked list to the end of logsrvs |
| 735 | * list. But first, we check (logsrvs != global.logsrvs). |
| 736 | */ |
| 737 | if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) { |
| 738 | if (logsrvs == &global.logsrvs) { |
| 739 | memprintf(err, "'global' is not supported for a global syslog server"); |
| 740 | goto error; |
| 741 | } |
| 742 | list_for_each_entry(logsrv, &global.logsrvs, list) { |
Christopher Faulet | 28ac099 | 2018-03-26 16:09:19 +0200 | [diff] [blame] | 743 | struct logsrv *node; |
| 744 | |
| 745 | list_for_each_entry(node, logsrvs, list) { |
| 746 | if (node->ref == logsrv) |
| 747 | goto skip_logsrv; |
| 748 | } |
| 749 | |
| 750 | node = malloc(sizeof(*node)); |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 751 | memcpy(node, logsrv, sizeof(struct logsrv)); |
Christopher Faulet | 28ac099 | 2018-03-26 16:09:19 +0200 | [diff] [blame] | 752 | node->ref = logsrv; |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 753 | LIST_INIT(&node->list); |
| 754 | LIST_ADDQ(logsrvs, &node->list); |
Christopher Faulet | 28ac099 | 2018-03-26 16:09:19 +0200 | [diff] [blame] | 755 | |
| 756 | skip_logsrv: |
| 757 | continue; |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 758 | } |
| 759 | return 1; |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * "log <address> ...: parse a syslog server line |
| 764 | */ |
| 765 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 766 | memprintf(err, "expects <address> and <facility> %s as arguments", |
| 767 | ((logsrvs == &global.logsrvs) ? "" : "or global")); |
| 768 | goto error; |
| 769 | } |
| 770 | |
Willy Tarreau | 5a32ecc | 2018-11-12 07:34:59 +0100 | [diff] [blame] | 771 | /* take care of "stdout" and "stderr" as regular aliases for fd@1 / fd@2 */ |
| 772 | if (strcmp(args[1], "stdout") == 0) |
| 773 | args[1] = "fd@1"; |
| 774 | else if (strcmp(args[1], "stderr") == 0) |
| 775 | args[1] = "fd@2"; |
| 776 | |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 777 | logsrv = calloc(1, sizeof(*logsrv)); |
| 778 | if (!logsrv) { |
| 779 | memprintf(err, "out of memory"); |
| 780 | goto error; |
| 781 | } |
| 782 | |
| 783 | /* skip address for now, it will be parsed at the end */ |
| 784 | cur_arg = 2; |
| 785 | |
| 786 | /* just after the address, a length may be specified */ |
| 787 | logsrv->maxlen = MAX_SYSLOG_LEN; |
| 788 | if (strcmp(args[cur_arg], "len") == 0) { |
| 789 | int len = atoi(args[cur_arg+1]); |
| 790 | if (len < 80 || len > 65535) { |
| 791 | memprintf(err, "invalid log length '%s', must be between 80 and 65535", |
| 792 | args[cur_arg+1]); |
| 793 | goto error; |
| 794 | } |
| 795 | logsrv->maxlen = len; |
| 796 | cur_arg += 2; |
| 797 | } |
| 798 | if (logsrv->maxlen > global.max_syslog_len) |
| 799 | global.max_syslog_len = logsrv->maxlen; |
| 800 | |
| 801 | /* after the length, a format may be specified */ |
| 802 | if (strcmp(args[cur_arg], "format") == 0) { |
| 803 | logsrv->format = get_log_format(args[cur_arg+1]); |
| 804 | if (logsrv->format < 0) { |
| 805 | memprintf(err, "unknown log format '%s'", args[cur_arg+1]); |
| 806 | goto error; |
| 807 | } |
| 808 | cur_arg += 2; |
| 809 | } |
| 810 | |
| 811 | /* parse the facility */ |
| 812 | logsrv->facility = get_log_facility(args[cur_arg]); |
| 813 | if (logsrv->facility < 0) { |
| 814 | memprintf(err, "unknown log facility '%s'", args[cur_arg]); |
| 815 | goto error; |
| 816 | } |
| 817 | cur_arg++; |
| 818 | |
| 819 | /* parse the max syslog level (default: debug) */ |
| 820 | logsrv->level = 7; |
| 821 | if (*(args[cur_arg])) { |
| 822 | logsrv->level = get_log_level(args[cur_arg]); |
| 823 | if (logsrv->level < 0) { |
| 824 | memprintf(err, "unknown optional log level '%s'", args[cur_arg]); |
| 825 | goto error; |
| 826 | } |
| 827 | cur_arg++; |
| 828 | } |
| 829 | |
| 830 | /* parse the limit syslog level (default: emerg) */ |
| 831 | logsrv->minlvl = 0; |
| 832 | if (*(args[cur_arg])) { |
| 833 | logsrv->minlvl = get_log_level(args[cur_arg]); |
| 834 | if (logsrv->minlvl < 0) { |
| 835 | memprintf(err, "unknown optional minimum log level '%s'", args[cur_arg]); |
| 836 | goto error; |
| 837 | } |
| 838 | cur_arg++; |
| 839 | } |
| 840 | |
| 841 | /* Too many args */ |
| 842 | if (*(args[cur_arg])) { |
| 843 | memprintf(err, "cannot handle unexpected argument '%s'", args[cur_arg]); |
| 844 | goto error; |
| 845 | } |
| 846 | |
| 847 | /* now, back to the address */ |
| 848 | sk = str2sa_range(args[1], NULL, &port1, &port2, err, NULL, NULL, 1); |
| 849 | if (!sk) |
| 850 | goto error; |
| 851 | logsrv->addr = *sk; |
| 852 | |
| 853 | if (sk->ss_family == AF_INET || sk->ss_family == AF_INET6) { |
| 854 | if (port1 != port2) { |
| 855 | memprintf(err, "port ranges and offsets are not allowed in '%s'", args[1]); |
| 856 | goto error; |
| 857 | } |
| 858 | logsrv->addr = *sk; |
| 859 | if (!port1) |
| 860 | set_host_port(&logsrv->addr, SYSLOG_PORT); |
| 861 | } |
| 862 | LIST_ADDQ(logsrvs, &logsrv->list); |
| 863 | return 1; |
| 864 | |
| 865 | error: |
| 866 | free(logsrv); |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 871 | /* Generic function to display messages prefixed by a label */ |
| 872 | static void print_message(const char *label, const char *fmt, va_list argp) |
| 873 | { |
| 874 | struct tm tm; |
| 875 | char *head, *msg; |
| 876 | |
| 877 | head = msg = NULL; |
| 878 | |
| 879 | get_localtime(date.tv_sec, &tm); |
| 880 | memprintf(&head, "[%s] %03d/%02d%02d%02d (%d) : ", |
| 881 | label, tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid()); |
| 882 | memvprintf(&msg, fmt, argp); |
| 883 | |
| 884 | if (global.mode & MODE_STARTING) |
| 885 | memprintf(&startup_logs, "%s%s%s", (startup_logs ? startup_logs : ""), head, msg); |
| 886 | |
| 887 | fprintf(stderr, "%s%s", head, msg); |
| 888 | fflush(stderr); |
| 889 | |
| 890 | free(head); |
| 891 | free(msg); |
| 892 | } |
| 893 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 894 | /* |
| 895 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 896 | * mode during startup. |
| 897 | */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 898 | void ha_alert(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 899 | { |
| 900 | va_list argp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 901 | |
| 902 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 903 | va_start(argp, fmt); |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 904 | print_message("ALERT", fmt, argp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 905 | va_end(argp); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | |
| 910 | /* |
| 911 | * Displays the message on stderr with the date and pid. |
| 912 | */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 913 | void ha_warning(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 914 | { |
| 915 | va_list argp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 916 | |
| 917 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 918 | va_start(argp, fmt); |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 919 | print_message("WARNING", fmt, argp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 920 | va_end(argp); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | /* |
William Lallemand | 9c56a22 | 2018-11-21 18:04:52 +0100 | [diff] [blame] | 925 | * Displays the message on stderr with the date and pid. |
| 926 | */ |
| 927 | void ha_notice(const char *fmt, ...) |
| 928 | { |
| 929 | va_list argp; |
| 930 | |
| 931 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 932 | va_start(argp, fmt); |
| 933 | print_message("NOTICE", fmt, argp); |
| 934 | va_end(argp); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 939 | * Displays the message on <out> only if quiet mode is not set. |
| 940 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 941 | void qfprintf(FILE *out, const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 942 | { |
| 943 | va_list argp; |
| 944 | |
| 945 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 946 | va_start(argp, fmt); |
| 947 | vfprintf(out, fmt, argp); |
| 948 | fflush(out); |
| 949 | va_end(argp); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /* |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 954 | * returns log format for <fmt> or -1 if not found. |
| 955 | */ |
| 956 | int get_log_format(const char *fmt) |
| 957 | { |
| 958 | int format; |
| 959 | |
| 960 | format = LOG_FORMATS - 1; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 961 | while (format >= 0 && strcmp(log_formats[format].name, fmt)) |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 962 | format--; |
| 963 | |
| 964 | return format; |
| 965 | } |
| 966 | |
| 967 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 968 | * returns log level for <lev> or -1 if not found. |
| 969 | */ |
| 970 | int get_log_level(const char *lev) |
| 971 | { |
| 972 | int level; |
| 973 | |
| 974 | level = NB_LOG_LEVELS - 1; |
| 975 | while (level >= 0 && strcmp(log_levels[level], lev)) |
| 976 | level--; |
| 977 | |
| 978 | return level; |
| 979 | } |
| 980 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 981 | /* |
| 982 | * returns log facility for <fac> or -1 if not found. |
| 983 | */ |
| 984 | int get_log_facility(const char *fac) |
| 985 | { |
| 986 | int facility; |
| 987 | |
| 988 | facility = NB_LOG_FACILITIES - 1; |
| 989 | while (facility >= 0 && strcmp(log_facilities[facility], fac)) |
| 990 | facility--; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 991 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 992 | return facility; |
| 993 | } |
| 994 | |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 995 | /* |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 996 | * Encode the string. |
| 997 | * |
| 998 | * When using the +E log format option, it will try to escape '"\]' |
| 999 | * characters with '\' as prefix. The same prefix should not be used as |
| 1000 | * <escape>. |
| 1001 | */ |
| 1002 | static char *lf_encode_string(char *start, char *stop, |
| 1003 | const char escape, const fd_set *map, |
| 1004 | const char *string, |
| 1005 | struct logformat_node *node) |
| 1006 | { |
| 1007 | if (node->options & LOG_OPT_ESC) { |
| 1008 | if (start < stop) { |
| 1009 | stop--; /* reserve one byte for the final '\0' */ |
| 1010 | while (start < stop && *string != '\0') { |
| 1011 | if (!FD_ISSET((unsigned char)(*string), map)) { |
| 1012 | if (!FD_ISSET((unsigned char)(*string), rfc5424_escape_map)) |
| 1013 | *start++ = *string; |
| 1014 | else { |
| 1015 | if (start + 2 >= stop) |
| 1016 | break; |
| 1017 | *start++ = '\\'; |
| 1018 | *start++ = *string; |
| 1019 | } |
| 1020 | } |
| 1021 | else { |
| 1022 | if (start + 3 >= stop) |
| 1023 | break; |
| 1024 | *start++ = escape; |
| 1025 | *start++ = hextab[(*string >> 4) & 15]; |
| 1026 | *start++ = hextab[*string & 15]; |
| 1027 | } |
| 1028 | string++; |
| 1029 | } |
| 1030 | *start = '\0'; |
| 1031 | } |
| 1032 | } |
| 1033 | else { |
| 1034 | return encode_string(start, stop, escape, map, string); |
| 1035 | } |
| 1036 | |
| 1037 | return start; |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * Encode the chunk. |
| 1042 | * |
| 1043 | * When using the +E log format option, it will try to escape '"\]' |
| 1044 | * characters with '\' as prefix. The same prefix should not be used as |
| 1045 | * <escape>. |
| 1046 | */ |
| 1047 | static char *lf_encode_chunk(char *start, char *stop, |
| 1048 | const char escape, const fd_set *map, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1049 | const struct buffer *chunk, |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1050 | struct logformat_node *node) |
| 1051 | { |
| 1052 | char *str, *end; |
| 1053 | |
| 1054 | if (node->options & LOG_OPT_ESC) { |
| 1055 | if (start < stop) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1056 | str = chunk->area; |
| 1057 | end = chunk->area + chunk->data; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1058 | |
| 1059 | stop--; /* reserve one byte for the final '\0' */ |
| 1060 | while (start < stop && str < end) { |
| 1061 | if (!FD_ISSET((unsigned char)(*str), map)) { |
| 1062 | if (!FD_ISSET((unsigned char)(*str), rfc5424_escape_map)) |
| 1063 | *start++ = *str; |
| 1064 | else { |
| 1065 | if (start + 2 >= stop) |
| 1066 | break; |
| 1067 | *start++ = '\\'; |
| 1068 | *start++ = *str; |
| 1069 | } |
| 1070 | } |
| 1071 | else { |
| 1072 | if (start + 3 >= stop) |
| 1073 | break; |
| 1074 | *start++ = escape; |
| 1075 | *start++ = hextab[(*str >> 4) & 15]; |
| 1076 | *start++ = hextab[*str & 15]; |
| 1077 | } |
| 1078 | str++; |
| 1079 | } |
| 1080 | *start = '\0'; |
| 1081 | } |
| 1082 | } |
| 1083 | else { |
| 1084 | return encode_chunk(start, stop, escape, map, chunk); |
| 1085 | } |
| 1086 | |
| 1087 | return start; |
| 1088 | } |
| 1089 | |
| 1090 | /* |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1091 | * Write a string in the log string |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1092 | * Take cares of quote and escape options |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1093 | * |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 1094 | * Return the address of the \0 character, or NULL on error |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1095 | */ |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1096 | char *lf_text_len(char *dst, const char *src, size_t len, size_t size, const struct logformat_node *node) |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1097 | { |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1098 | if (size < 2) |
| 1099 | return NULL; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1100 | |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1101 | if (node->options & LOG_OPT_QUOTE) { |
| 1102 | *(dst++) = '"'; |
| 1103 | size--; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1104 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1105 | |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 1106 | if (src && len) { |
Dragan Dosen | db1b6f9 | 2016-07-25 11:35:02 +0200 | [diff] [blame] | 1107 | if (++len > size) |
| 1108 | len = size; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1109 | if (node->options & LOG_OPT_ESC) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1110 | char *ret; |
| 1111 | |
Dragan Dosen | db1b6f9 | 2016-07-25 11:35:02 +0200 | [diff] [blame] | 1112 | ret = escape_string(dst, dst + len, '\\', rfc5424_escape_map, src); |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1113 | if (ret == NULL || *ret != '\0') |
| 1114 | return NULL; |
| 1115 | len = ret - dst; |
| 1116 | } |
| 1117 | else { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1118 | len = strlcpy2(dst, src, len); |
| 1119 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1120 | |
| 1121 | size -= len; |
| 1122 | dst += len; |
| 1123 | } |
Willy Tarreau | 6cbbdbf | 2013-02-05 18:52:25 +0100 | [diff] [blame] | 1124 | else if ((node->options & (LOG_OPT_QUOTE|LOG_OPT_MANDATORY)) == LOG_OPT_MANDATORY) { |
| 1125 | if (size < 2) |
| 1126 | return NULL; |
| 1127 | *(dst++) = '-'; |
| 1128 | } |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1129 | |
| 1130 | if (node->options & LOG_OPT_QUOTE) { |
| 1131 | if (size < 2) |
| 1132 | return NULL; |
| 1133 | *(dst++) = '"'; |
| 1134 | } |
| 1135 | |
| 1136 | *dst = '\0'; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1137 | return dst; |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 1138 | } |
| 1139 | |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1140 | static inline char *lf_text(char *dst, const char *src, size_t size, const struct logformat_node *node) |
Willy Tarreau | 2b0108a | 2012-12-21 19:23:44 +0100 | [diff] [blame] | 1141 | { |
| 1142 | return lf_text_len(dst, src, size, size, node); |
| 1143 | } |
| 1144 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1145 | /* |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 1146 | * Write a IP address to the log string |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1147 | * +X option write in hexadecimal notation, most signifant byte on the left |
| 1148 | */ |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1149 | char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node) |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1150 | { |
| 1151 | char *ret = dst; |
| 1152 | int iret; |
| 1153 | char pn[INET6_ADDRSTRLEN]; |
| 1154 | |
| 1155 | if (node->options & LOG_OPT_HEXA) { |
Radek Zajic | 594c456 | 2019-03-22 10:21:54 +0000 | [diff] [blame] | 1156 | unsigned char *addr = NULL; |
| 1157 | switch (sockaddr->sa_family) { |
| 1158 | case AF_INET: |
| 1159 | addr = (unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_addr.s_addr; |
| 1160 | iret = snprintf(dst, size, "%02X%02X%02X%02X", addr[0], addr[1], addr[2], addr[3]); |
| 1161 | break; |
| 1162 | case AF_INET6: |
| 1163 | addr = (unsigned char *)&((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr; |
| 1164 | iret = snprintf(dst, size, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", |
| 1165 | addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7], |
| 1166 | addr[8], addr[9], addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]); |
| 1167 | break; |
| 1168 | default: |
| 1169 | return NULL; |
| 1170 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1171 | if (iret < 0 || iret > size) |
| 1172 | return NULL; |
| 1173 | ret += iret; |
| 1174 | } else { |
| 1175 | addr_to_str((struct sockaddr_storage *)sockaddr, pn, sizeof(pn)); |
| 1176 | ret = lf_text(dst, pn, size, node); |
| 1177 | if (ret == NULL) |
| 1178 | return NULL; |
| 1179 | } |
| 1180 | return ret; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | * Write a port to the log |
| 1185 | * +X option write in hexadecimal notation, most signifant byte on the left |
| 1186 | */ |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 1187 | char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node) |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1188 | { |
| 1189 | char *ret = dst; |
| 1190 | int iret; |
| 1191 | |
| 1192 | if (node->options & LOG_OPT_HEXA) { |
| 1193 | const unsigned char *port = (const unsigned char *)&((struct sockaddr_in *)sockaddr)->sin_port; |
| 1194 | iret = snprintf(dst, size, "%02X%02X", port[0], port[1]); |
| 1195 | if (iret < 0 || iret > size) |
| 1196 | return NULL; |
| 1197 | ret += iret; |
| 1198 | } else { |
| 1199 | ret = ltoa_o(get_host_port((struct sockaddr_storage *)sockaddr), dst, size); |
| 1200 | if (ret == NULL) |
| 1201 | return NULL; |
| 1202 | } |
| 1203 | return ret; |
| 1204 | } |
| 1205 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1206 | /* Re-generate time-based part of the syslog header in RFC3164 format at |
| 1207 | * the beginning of logheader once a second and return the pointer to the |
| 1208 | * first character after it. |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1209 | */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1210 | static char *update_log_hdr(const time_t time) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1211 | { |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1212 | static THREAD_LOCAL long tvsec; |
| 1213 | static THREAD_LOCAL char *dataptr = NULL; /* backup of last end of header, NULL first time */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1214 | static THREAD_LOCAL struct buffer host = { }; |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1215 | static THREAD_LOCAL int sep = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1216 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1217 | if (unlikely(time != tvsec || dataptr == NULL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1218 | /* this string is rebuild only once a second */ |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1219 | struct tm tm; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1220 | int hdr_len; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1221 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1222 | tvsec = time; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1223 | get_localtime(tvsec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1224 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1225 | if (unlikely(global.log_send_hostname != host.area)) { |
| 1226 | host.area = global.log_send_hostname; |
| 1227 | host.data = host.area ? strlen(host.area) : 0; |
| 1228 | sep = host.data ? 1 : 0; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1229 | } |
| 1230 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1231 | hdr_len = snprintf(logheader, global.max_syslog_len, |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1232 | "<<<<>%s %2d %02d:%02d:%02d %.*s%*s", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1233 | monthname[tm.tm_mon], |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1234 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1235 | (int)host.data, host.area, sep, ""); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1236 | /* WARNING: depending upon implementations, snprintf may return |
| 1237 | * either -1 or the number of bytes that would be needed to store |
| 1238 | * the total message. In both cases, we must adjust it. |
| 1239 | */ |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 1240 | if (hdr_len < 0 || hdr_len > global.max_syslog_len) |
| 1241 | hdr_len = global.max_syslog_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1242 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1243 | dataptr = logheader + hdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
Willy Tarreau | 094af4e | 2015-01-07 15:03:42 +0100 | [diff] [blame] | 1246 | dataptr[0] = 0; // ensure we get rid of any previous attempt |
| 1247 | |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1248 | return dataptr; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1249 | } |
| 1250 | |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1251 | /* Re-generate time-based part of the syslog header in RFC5424 format at |
| 1252 | * the beginning of logheader_rfc5424 once a second and return the pointer |
| 1253 | * to the first character after it. |
| 1254 | */ |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1255 | static char *update_log_hdr_rfc5424(const time_t time) |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1256 | { |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1257 | static THREAD_LOCAL long tvsec; |
| 1258 | static THREAD_LOCAL char *dataptr = NULL; /* backup of last end of header, NULL first time */ |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 1259 | const char *gmt_offset; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1260 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1261 | if (unlikely(time != tvsec || dataptr == NULL)) { |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1262 | /* this string is rebuild only once a second */ |
| 1263 | struct tm tm; |
| 1264 | int hdr_len; |
| 1265 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1266 | tvsec = time; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1267 | get_localtime(tvsec, &tm); |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 1268 | gmt_offset = get_gmt_offset(time, &tm); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1269 | |
| 1270 | hdr_len = snprintf(logheader_rfc5424, global.max_syslog_len, |
Dragan Dosen | 17def46 | 2015-10-09 21:31:43 +0200 | [diff] [blame] | 1271 | "<<<<>1 %4d-%02d-%02dT%02d:%02d:%02d%.3s:%.2s %s ", |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1272 | tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, |
Dragan Dosen | 17def46 | 2015-10-09 21:31:43 +0200 | [diff] [blame] | 1273 | tm.tm_hour, tm.tm_min, tm.tm_sec, |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 1274 | gmt_offset, gmt_offset+3, |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1275 | global.log_send_hostname ? global.log_send_hostname : hostname); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1276 | /* WARNING: depending upon implementations, snprintf may return |
| 1277 | * either -1 or the number of bytes that would be needed to store |
| 1278 | * the total message. In both cases, we must adjust it. |
| 1279 | */ |
| 1280 | if (hdr_len < 0 || hdr_len > global.max_syslog_len) |
| 1281 | hdr_len = global.max_syslog_len; |
| 1282 | |
| 1283 | dataptr = logheader_rfc5424 + hdr_len; |
| 1284 | } |
| 1285 | |
| 1286 | dataptr[0] = 0; // ensure we get rid of any previous attempt |
| 1287 | |
| 1288 | return dataptr; |
| 1289 | } |
| 1290 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1291 | /* |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1292 | * This function sends the syslog message using a printf format string. It |
| 1293 | * expects an LF-terminated message. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1294 | */ |
| 1295 | void send_log(struct proxy *p, int level, const char *format, ...) |
| 1296 | { |
| 1297 | va_list argp; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1298 | int data_len; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1299 | |
Willy Tarreau | 8c97ab5 | 2015-01-15 16:29:53 +0100 | [diff] [blame] | 1300 | if (level < 0 || format == NULL || logline == NULL) |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1301 | return; |
| 1302 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1303 | va_start(argp, format); |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1304 | data_len = vsnprintf(logline, global.max_syslog_len, format, argp); |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 1305 | if (data_len < 0 || data_len > global.max_syslog_len) |
| 1306 | data_len = global.max_syslog_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1307 | va_end(argp); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1308 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1309 | __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] | 1310 | } |
| 1311 | |
| 1312 | /* |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1313 | * This function sends a syslog message to <logsrv>. |
| 1314 | * <pid_str> is the string to be used for the PID of the caller, <pid_size> is length. |
| 1315 | * Same thing for <sd> and <sd_size> which are used for the structured-data part |
| 1316 | * in RFC5424 formatted syslog messages, and <tag_str> and <tag_size> the syslog tag. |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1317 | * It overrides the last byte of the message vector with an LF character. |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1318 | * Does not return any error, |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1319 | */ |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1320 | static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_str, size_t pid_size, |
| 1321 | int level, char *message, size_t size, char *sd, size_t sd_size, |
| 1322 | char *tag_str, size_t tag_size) |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1323 | { |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1324 | static THREAD_LOCAL struct iovec iovec[NB_MSG_IOVEC_ELEMENTS] = { }; |
| 1325 | static THREAD_LOCAL struct msghdr msghdr = { |
| 1326 | //.msg_iov = iovec, |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1327 | .msg_iovlen = NB_MSG_IOVEC_ELEMENTS |
| 1328 | }; |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1329 | static THREAD_LOCAL int logfdunix = -1; /* syslog to AF_UNIX socket */ |
| 1330 | static THREAD_LOCAL int logfdinet = -1; /* syslog to AF_INET socket */ |
| 1331 | static THREAD_LOCAL char *dataptr = NULL; |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1332 | time_t time = date.tv_sec; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1333 | char *hdr, *hdr_ptr; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1334 | size_t hdr_size; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1335 | struct buffer *tag = &global.log_tag; |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1336 | int fac_level; |
| 1337 | int *plogfd; |
| 1338 | char *pid_sep1 = "", *pid_sep2 = ""; |
| 1339 | char logheader_short[3]; |
| 1340 | int sent; |
| 1341 | int maxlen; |
| 1342 | int hdr_max = 0; |
| 1343 | int tag_max = 0; |
| 1344 | int pid_sep1_max = 0; |
| 1345 | int pid_sep2_max = 0; |
| 1346 | int sd_max = 0; |
| 1347 | int max = 0; |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 1348 | |
| 1349 | msghdr.msg_iov = iovec; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1350 | |
| 1351 | dataptr = message; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1352 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1353 | if (logsrv->addr.ss_family == AF_UNSPEC) { |
| 1354 | /* the socket's address is a file descriptor */ |
| 1355 | plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr; |
| 1356 | if (unlikely(!((struct sockaddr_in *)&logsrv->addr)->sin_port)) { |
| 1357 | /* FD not yet initialized to non-blocking mode. |
| 1358 | * DON'T DO IT ON A TERMINAL! |
| 1359 | */ |
| 1360 | if (!isatty(*plogfd)) |
| 1361 | fcntl(*plogfd, F_SETFL, O_NONBLOCK); |
| 1362 | ((struct sockaddr_in *)&logsrv->addr)->sin_port = 1; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1363 | } |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1364 | } |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1365 | else if (logsrv->addr.ss_family == AF_UNIX) |
| 1366 | plogfd = &logfdunix; |
| 1367 | else |
| 1368 | plogfd = &logfdinet; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 1369 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1370 | if (unlikely(*plogfd < 0)) { |
| 1371 | /* socket not successfully initialized yet */ |
| 1372 | if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, |
| 1373 | (logsrv->addr.ss_family == AF_UNIX) ? 0 : IPPROTO_UDP)) < 0) { |
| 1374 | static char once; |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 1375 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1376 | if (!once) { |
| 1377 | once = 1; /* note: no need for atomic ops here */ |
| 1378 | ha_alert("socket() failed in logger #%d: %s (errno=%d)\n", |
| 1379 | nblogger, strerror(errno), errno); |
| 1380 | } |
| 1381 | return; |
| 1382 | } else { |
| 1383 | /* we don't want to receive anything on this socket */ |
| 1384 | setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero)); |
| 1385 | /* does nothing under Linux, maybe needed for others */ |
| 1386 | shutdown(*plogfd, SHUT_RD); |
| 1387 | fcntl(*plogfd, F_SETFD, fcntl(*plogfd, F_GETFD, FD_CLOEXEC) | FD_CLOEXEC); |
| 1388 | } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1391 | switch (logsrv->format) { |
| 1392 | case LOG_FORMAT_RFC3164: |
| 1393 | hdr = logheader; |
| 1394 | hdr_ptr = update_log_hdr(time); |
| 1395 | break; |
Willy Tarreau | c7c7be2 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 1396 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1397 | case LOG_FORMAT_RFC5424: |
| 1398 | hdr = logheader_rfc5424; |
| 1399 | hdr_ptr = update_log_hdr_rfc5424(time); |
| 1400 | sd_max = sd_size; /* the SD part allowed only in RFC5424 */ |
| 1401 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1402 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1403 | case LOG_FORMAT_SHORT: |
| 1404 | /* all fields are known, skip the header generation */ |
| 1405 | hdr = logheader_short; |
| 1406 | hdr[0] = '<'; |
| 1407 | hdr[1] = '0' + MAX(level, logsrv->minlvl); |
| 1408 | hdr[2] = '>'; |
| 1409 | hdr_ptr = hdr; |
| 1410 | hdr_max = 3; |
| 1411 | maxlen = logsrv->maxlen - hdr_max; |
| 1412 | max = MIN(size, maxlen) - 1; |
| 1413 | goto send; |
Willy Tarreau | 204e3f1 | 2018-12-15 15:48:48 +0100 | [diff] [blame] | 1414 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1415 | case LOG_FORMAT_RAW: |
| 1416 | /* all fields are known, skip the header generation */ |
| 1417 | hdr_ptr = hdr = ""; |
| 1418 | hdr_max = 0; |
| 1419 | maxlen = logsrv->maxlen; |
| 1420 | max = MIN(size, maxlen) - 1; |
| 1421 | goto send; |
Willy Tarreau | c98aebc | 2018-03-20 11:17:29 +0100 | [diff] [blame] | 1422 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1423 | default: |
| 1424 | return; /* must never happen */ |
| 1425 | } |
Willy Tarreau | c7c7be2 | 2014-06-23 18:07:15 +0200 | [diff] [blame] | 1426 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1427 | hdr_size = hdr_ptr - hdr; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1428 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1429 | /* For each target, we may have a different facility. |
| 1430 | * We can also have a different log level for each message. |
| 1431 | * This induces variations in the message header length. |
| 1432 | * Since we don't want to recompute it each time, nor copy it every |
| 1433 | * time, we only change the facility in the pre-computed header, |
| 1434 | * and we change the pointer to the header accordingly. |
| 1435 | */ |
| 1436 | fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl); |
| 1437 | hdr_ptr = hdr + 3; /* last digit of the log level */ |
| 1438 | do { |
| 1439 | *hdr_ptr = '0' + fac_level % 10; |
| 1440 | fac_level /= 10; |
| 1441 | hdr_ptr--; |
| 1442 | } while (fac_level && hdr_ptr > hdr); |
| 1443 | *hdr_ptr = '<'; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1444 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1445 | hdr_max = hdr_size - (hdr_ptr - hdr); |
Willy Tarreau | e8746a0 | 2018-11-12 08:45:00 +0100 | [diff] [blame] | 1446 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1447 | /* time-based header */ |
| 1448 | if (unlikely(hdr_size >= logsrv->maxlen)) { |
| 1449 | hdr_max = MIN(hdr_max, logsrv->maxlen) - 1; |
| 1450 | sd_max = 0; |
| 1451 | goto send; |
| 1452 | } |
Willy Tarreau | c1b0645 | 2018-11-12 11:57:56 +0100 | [diff] [blame] | 1453 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1454 | maxlen = logsrv->maxlen - hdr_max; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1455 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1456 | /* tag */ |
| 1457 | tag_max = tag->data; |
| 1458 | if (unlikely(tag_max >= maxlen)) { |
| 1459 | tag_max = maxlen - 1; |
| 1460 | sd_max = 0; |
| 1461 | goto send; |
| 1462 | } |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 1463 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1464 | maxlen -= tag_max; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 1465 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1466 | /* first pid separator */ |
| 1467 | pid_sep1_max = log_formats[logsrv->format].pid.sep1.data; |
| 1468 | if (unlikely(pid_sep1_max >= maxlen)) { |
| 1469 | pid_sep1_max = maxlen - 1; |
| 1470 | sd_max = 0; |
| 1471 | goto send; |
| 1472 | } |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1473 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1474 | pid_sep1 = log_formats[logsrv->format].pid.sep1.area; |
| 1475 | maxlen -= pid_sep1_max; |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1476 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1477 | /* pid */ |
| 1478 | if (unlikely(pid_size >= maxlen)) { |
| 1479 | pid_size = maxlen - 1; |
| 1480 | sd_max = 0; |
| 1481 | goto send; |
| 1482 | } |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1483 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1484 | maxlen -= pid_size; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1485 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1486 | /* second pid separator */ |
| 1487 | pid_sep2_max = log_formats[logsrv->format].pid.sep2.data; |
| 1488 | if (unlikely(pid_sep2_max >= maxlen)) { |
| 1489 | pid_sep2_max = maxlen - 1; |
| 1490 | sd_max = 0; |
| 1491 | goto send; |
| 1492 | } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1493 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1494 | pid_sep2 = log_formats[logsrv->format].pid.sep2.area; |
| 1495 | maxlen -= pid_sep2_max; |
Dragan Dosen | 68d2e3a | 2015-09-19 22:35:44 +0200 | [diff] [blame] | 1496 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1497 | /* structured-data */ |
| 1498 | if (sd_max >= maxlen) { |
| 1499 | sd_max = maxlen - 1; |
| 1500 | goto send; |
| 1501 | } |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1502 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1503 | max = MIN(size, maxlen - sd_max) - 1; |
| 1504 | send: |
| 1505 | iovec[0].iov_base = hdr_ptr; |
| 1506 | iovec[0].iov_len = hdr_max; |
| 1507 | iovec[1].iov_base = tag_str; |
| 1508 | iovec[1].iov_len = tag_size; |
| 1509 | iovec[2].iov_base = pid_sep1; |
| 1510 | iovec[2].iov_len = pid_sep1_max; |
| 1511 | iovec[3].iov_base = pid_str; |
| 1512 | iovec[3].iov_len = pid_size; |
| 1513 | iovec[4].iov_base = pid_sep2; |
| 1514 | iovec[4].iov_len = pid_sep2_max; |
| 1515 | iovec[5].iov_base = sd; |
| 1516 | iovec[5].iov_len = sd_max; |
| 1517 | iovec[6].iov_base = dataptr; |
| 1518 | iovec[6].iov_len = max; |
| 1519 | iovec[7].iov_base = "\n"; /* insert a \n at the end of the message */ |
| 1520 | iovec[7].iov_len = 1; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1521 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1522 | if (logsrv->addr.ss_family == AF_UNSPEC) { |
| 1523 | /* the target is a direct file descriptor */ |
| 1524 | sent = writev(*plogfd, iovec, 8); |
| 1525 | } |
| 1526 | else { |
| 1527 | msghdr.msg_name = (struct sockaddr *)&logsrv->addr; |
| 1528 | msghdr.msg_namelen = get_addr_len(&logsrv->addr); |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1529 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1530 | sent = sendmsg(*plogfd, &msghdr, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 1531 | } |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1532 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1533 | if (sent < 0) { |
| 1534 | static char once; |
Dragan Dosen | 43885c7 | 2015-10-01 13:18:13 +0200 | [diff] [blame] | 1535 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1536 | if (errno == EAGAIN) |
| 1537 | _HA_ATOMIC_ADD(&dropped_logs, 1); |
| 1538 | else if (!once) { |
| 1539 | once = 1; /* note: no need for atomic ops here */ |
| 1540 | ha_alert("sendmsg()/writev() failed in logger #%d: %s (errno=%d)\n", |
| 1541 | nblogger, strerror(errno), errno); |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1542 | } |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1543 | } |
| 1544 | } |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 1545 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1546 | /* |
| 1547 | * This function sends a syslog message. |
| 1548 | * It doesn't care about errors nor does it report them. |
| 1549 | * The arguments <sd> and <sd_size> are used for the structured-data part |
| 1550 | * in RFC5424 formatted syslog messages. |
| 1551 | */ |
| 1552 | void __send_log(struct proxy *p, int level, char *message, size_t size, char *sd, size_t sd_size) |
| 1553 | { |
| 1554 | struct list *logsrvs = NULL; |
| 1555 | struct logsrv *logsrv; |
| 1556 | int nblogger; |
| 1557 | static THREAD_LOCAL int curr_pid; |
| 1558 | static THREAD_LOCAL char pidstr[100]; |
| 1559 | static THREAD_LOCAL struct buffer pid; |
| 1560 | struct buffer *tag = &global.log_tag; |
Dragan Dosen | 609ac2a | 2015-09-16 18:25:42 +0200 | [diff] [blame] | 1561 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1562 | if (p == NULL) { |
| 1563 | if (!LIST_ISEMPTY(&global.logsrvs)) { |
| 1564 | logsrvs = &global.logsrvs; |
Willy Tarreau | 5a32ecc | 2018-11-12 07:34:59 +0100 | [diff] [blame] | 1565 | } |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1566 | } else { |
| 1567 | if (!LIST_ISEMPTY(&p->logsrvs)) { |
| 1568 | logsrvs = &p->logsrvs; |
Willy Tarreau | 5a32ecc | 2018-11-12 07:34:59 +0100 | [diff] [blame] | 1569 | } |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1570 | if (p->log_tag.area) { |
| 1571 | tag = &p->log_tag; |
| 1572 | } |
| 1573 | } |
Willy Tarreau | 18324f5 | 2014-06-27 18:10:07 +0200 | [diff] [blame] | 1574 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1575 | if (!logsrvs) |
| 1576 | return; |
Willy Tarreau | c98aebc | 2018-03-20 11:17:29 +0100 | [diff] [blame] | 1577 | |
Frédéric Lécaille | 0bad840 | 2019-04-10 08:22:17 +0200 | [diff] [blame] | 1578 | if (unlikely(curr_pid != getpid())) { |
| 1579 | curr_pid = getpid(); |
| 1580 | ltoa_o(curr_pid, pidstr, sizeof(pidstr)); |
| 1581 | chunk_initstr(&pid, pidstr); |
| 1582 | } |
| 1583 | |
| 1584 | /* Send log messages to syslog server. */ |
| 1585 | nblogger = 0; |
| 1586 | list_for_each_entry(logsrv, logsrvs, list) { |
| 1587 | /* we can filter the level of the messages that are sent to each logger */ |
| 1588 | if (level > logsrv->level) |
| 1589 | continue; |
| 1590 | |
| 1591 | __do_send_log(logsrv, ++nblogger, pid.area, pid.data, level, |
| 1592 | message, size, sd, sd_size, tag->area, tag->data); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1596 | extern fd_set hdr_encode_map[]; |
| 1597 | extern fd_set url_encode_map[]; |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1598 | extern fd_set http_encode_map[]; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1599 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1600 | |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 1601 | 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] | 1602 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 1603 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 1604 | Set-cookie Updated, unknown, unknown */ |
| 1605 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1606 | /* |
| 1607 | * try to write a character if there is enough space, or goto out |
| 1608 | */ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1609 | #define LOGCHAR(x) do { \ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1610 | if (tmplog < dst + maxsize - 1) { \ |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1611 | *(tmplog++) = (x); \ |
| 1612 | } else { \ |
| 1613 | goto out; \ |
| 1614 | } \ |
| 1615 | } while(0) |
| 1616 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1617 | |
Willy Tarreau | b6b3df3 | 2018-11-26 16:31:20 +0100 | [diff] [blame] | 1618 | /* Initializes some log data at boot */ |
| 1619 | static void init_log() |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1620 | { |
| 1621 | char *tmp; |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1622 | int i; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1623 | |
| 1624 | /* Initialize the escape map for the RFC5424 structured-data : '"\]' |
| 1625 | * inside PARAM-VALUE should be escaped with '\' as prefix. |
| 1626 | * See https://tools.ietf.org/html/rfc5424#section-6.3.3 for more |
| 1627 | * details. |
| 1628 | */ |
| 1629 | memset(rfc5424_escape_map, 0, sizeof(rfc5424_escape_map)); |
| 1630 | |
| 1631 | tmp = "\"\\]"; |
| 1632 | while (*tmp) { |
| 1633 | FD_SET(*tmp, rfc5424_escape_map); |
| 1634 | tmp++; |
| 1635 | } |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1636 | |
| 1637 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 1638 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 1639 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 1640 | * printable characters above. |
| 1641 | */ |
| 1642 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 1643 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 1644 | for (i = 0; i < 32; i++) { |
| 1645 | FD_SET(i, hdr_encode_map); |
| 1646 | FD_SET(i, url_encode_map); |
| 1647 | } |
| 1648 | for (i = 127; i < 256; i++) { |
| 1649 | FD_SET(i, hdr_encode_map); |
| 1650 | FD_SET(i, url_encode_map); |
| 1651 | } |
| 1652 | |
| 1653 | tmp = "\"#{|}"; |
| 1654 | while (*tmp) { |
| 1655 | FD_SET(*tmp, hdr_encode_map); |
| 1656 | tmp++; |
| 1657 | } |
| 1658 | |
| 1659 | tmp = "\"#"; |
| 1660 | while (*tmp) { |
| 1661 | FD_SET(*tmp, url_encode_map); |
| 1662 | tmp++; |
| 1663 | } |
| 1664 | |
| 1665 | /* initialize the http header encoding map. The draft httpbis define the |
| 1666 | * header content as: |
| 1667 | * |
| 1668 | * HTTP-message = start-line |
| 1669 | * *( header-field CRLF ) |
| 1670 | * CRLF |
| 1671 | * [ message-body ] |
| 1672 | * header-field = field-name ":" OWS field-value OWS |
| 1673 | * field-value = *( field-content / obs-fold ) |
| 1674 | * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] |
| 1675 | * obs-fold = CRLF 1*( SP / HTAB ) |
| 1676 | * field-vchar = VCHAR / obs-text |
| 1677 | * VCHAR = %x21-7E |
| 1678 | * obs-text = %x80-FF |
| 1679 | * |
| 1680 | * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB. |
| 1681 | * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 1682 | * "obs-fold" is voluntarily forgotten because haproxy remove this. |
Willy Tarreau | e10cd48 | 2018-09-10 18:16:53 +0200 | [diff] [blame] | 1683 | */ |
| 1684 | memset(http_encode_map, 0, sizeof(http_encode_map)); |
| 1685 | for (i = 0x00; i <= 0x08; i++) |
| 1686 | FD_SET(i, http_encode_map); |
| 1687 | for (i = 0x0a; i <= 0x1f; i++) |
| 1688 | FD_SET(i, http_encode_map); |
| 1689 | FD_SET(0x7f, http_encode_map); |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1690 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1691 | |
Willy Tarreau | b6b3df3 | 2018-11-26 16:31:20 +0100 | [diff] [blame] | 1692 | INITCALL0(STG_PREPARE, init_log); |
| 1693 | |
Christopher Faulet | cd7879a | 2017-10-27 13:53:47 +0200 | [diff] [blame] | 1694 | static int init_log_buffers_per_thread() |
| 1695 | { |
| 1696 | return init_log_buffers(); |
| 1697 | } |
| 1698 | |
| 1699 | static void deinit_log_buffers_per_thread() |
| 1700 | { |
| 1701 | deinit_log_buffers(); |
| 1702 | } |
| 1703 | |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1704 | /* Initialize log buffers used for syslog messages */ |
| 1705 | int init_log_buffers() |
| 1706 | { |
| 1707 | logheader = my_realloc2(logheader, global.max_syslog_len + 1); |
| 1708 | logheader_rfc5424 = my_realloc2(logheader_rfc5424, global.max_syslog_len + 1); |
| 1709 | logline = my_realloc2(logline, global.max_syslog_len + 1); |
| 1710 | logline_rfc5424 = my_realloc2(logline_rfc5424, global.max_syslog_len + 1); |
| 1711 | if (!logheader || !logline_rfc5424 || !logline || !logline_rfc5424) |
| 1712 | return 0; |
| 1713 | return 1; |
| 1714 | } |
| 1715 | |
| 1716 | /* Deinitialize log buffers used for syslog messages */ |
| 1717 | void deinit_log_buffers() |
| 1718 | { |
Olivier Houchard | 7c49711 | 2019-03-07 14:19:24 +0100 | [diff] [blame] | 1719 | void *tmp_startup_logs; |
| 1720 | |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1721 | free(logheader); |
| 1722 | free(logheader_rfc5424); |
| 1723 | free(logline); |
| 1724 | free(logline_rfc5424); |
Olivier Houchard | d2ee3e7 | 2019-03-08 18:53:21 +0100 | [diff] [blame] | 1725 | tmp_startup_logs = _HA_ATOMIC_XCHG(&startup_logs, NULL); |
Olivier Houchard | 7c49711 | 2019-03-07 14:19:24 +0100 | [diff] [blame] | 1726 | free(tmp_startup_logs); |
| 1727 | |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1728 | logheader = NULL; |
| 1729 | logheader_rfc5424 = NULL; |
| 1730 | logline = NULL; |
| 1731 | logline_rfc5424 = NULL; |
Christopher Faulet | d469638 | 2017-10-24 11:44:05 +0200 | [diff] [blame] | 1732 | startup_logs = NULL; |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 1733 | } |
| 1734 | |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 1735 | /* Builds a log line in <dst> based on <list_format>, and stops before reaching |
| 1736 | * <maxsize> characters. Returns the size of the output string in characters, |
| 1737 | * not counting the trailing zero which is always added if the resulting size |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1738 | * is not zero. It requires a valid session and optionally a stream. If the |
| 1739 | * stream is NULL, default values will be assumed for the stream part. |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 1740 | */ |
Willy Tarreau | 43c538e | 2018-09-05 14:58:15 +0200 | [diff] [blame] | 1741 | int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t maxsize, struct list *list_format) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1742 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 1743 | struct proxy *fe = sess->fe; |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1744 | struct proxy *be; |
| 1745 | struct http_txn *txn; |
| 1746 | const struct strm_logs *logs; |
| 1747 | const struct connection *be_conn; |
| 1748 | unsigned int s_flags; |
| 1749 | unsigned int uniq_id; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1750 | struct buffer chunk; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1751 | char *uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1752 | char *spc; |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 1753 | char *qmark; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1754 | char *end; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1755 | struct tm tm; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1756 | int t_request; |
| 1757 | int hdr; |
| 1758 | int last_isspace = 1; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 1759 | int nspaces = 0; |
Willy Tarreau | b1a2faf | 2012-03-19 16:51:53 +0100 | [diff] [blame] | 1760 | char *tmplog; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1761 | char *ret; |
| 1762 | int iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1763 | struct logformat_node *tmp; |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1764 | struct timeval tv; |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1765 | struct strm_logs tmp_strm_log; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1766 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1767 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1768 | |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1769 | if (likely(s)) { |
| 1770 | be = s->be; |
| 1771 | txn = s->txn; |
| 1772 | be_conn = cs_conn(objt_cs(s->si[1].end)); |
| 1773 | s_flags = s->flags; |
| 1774 | uniq_id = s->uniq_id; |
| 1775 | logs = &s->logs; |
| 1776 | } else { |
| 1777 | /* we have no stream so we first need to initialize a few |
| 1778 | * things that are needed later. We do increment the request |
| 1779 | * ID so that it's uniquely assigned to this request just as |
| 1780 | * if the request had reached the point of being processed. |
| 1781 | * A request error is reported as it's the only element we have |
| 1782 | * here and which justifies emitting such a log. |
| 1783 | */ |
| 1784 | be = fe; |
| 1785 | txn = NULL; |
| 1786 | be_conn = NULL; |
| 1787 | s_flags = SF_ERR_PRXCOND | SF_FINST_R; |
Olivier Houchard | d2ee3e7 | 2019-03-08 18:53:21 +0100 | [diff] [blame] | 1788 | uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1); |
Willy Tarreau | 09bb27c | 2018-09-05 16:55:15 +0200 | [diff] [blame] | 1789 | |
| 1790 | /* prepare a valid log structure */ |
| 1791 | tmp_strm_log.tv_accept = sess->tv_accept; |
| 1792 | tmp_strm_log.accept_date = sess->accept_date; |
| 1793 | tmp_strm_log.t_handshake = sess->t_handshake; |
| 1794 | tmp_strm_log.t_idle = tv_ms_elapsed(&sess->tv_accept, &now) - sess->t_handshake; |
| 1795 | tv_zero(&tmp_strm_log.tv_request); |
| 1796 | tmp_strm_log.t_queue = -1; |
| 1797 | tmp_strm_log.t_connect = -1; |
| 1798 | tmp_strm_log.t_data = -1; |
| 1799 | tmp_strm_log.t_close = tv_ms_elapsed(&sess->tv_accept, &now); |
| 1800 | tmp_strm_log.bytes_in = 0; |
| 1801 | tmp_strm_log.bytes_out = 0; |
| 1802 | tmp_strm_log.prx_queue_pos = 0; |
| 1803 | tmp_strm_log.srv_queue_pos = 0; |
| 1804 | |
| 1805 | logs = &tmp_strm_log; |
| 1806 | } |
| 1807 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1808 | t_request = -1; |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 1809 | if (tv_isge(&logs->tv_request, &logs->tv_accept)) |
| 1810 | t_request = tv_ms_elapsed(&logs->tv_accept, &logs->tv_request); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1811 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1812 | tmplog = dst; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1813 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1814 | /* fill logbuffer */ |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1815 | if (LIST_ISEMPTY(list_format)) |
| 1816 | return 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1817 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1818 | list_for_each_entry(tmp, list_format, list) { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1819 | struct connection *conn; |
Willy Tarreau | 4f65356 | 2012-10-12 19:48:16 +0200 | [diff] [blame] | 1820 | const char *src = NULL; |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1821 | struct sample *key; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1822 | const struct buffer empty = { }; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1823 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1824 | switch (tmp->type) { |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1825 | case LOG_FMT_SEPARATOR: |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1826 | if (!last_isspace) { |
| 1827 | LOGCHAR(' '); |
| 1828 | last_isspace = 1; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1829 | } |
| 1830 | break; |
| 1831 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1832 | case LOG_FMT_TEXT: // text |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1833 | src = tmp->arg; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1834 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1835 | if (iret == 0) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1836 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1837 | tmplog += iret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1838 | last_isspace = 0; |
| 1839 | break; |
| 1840 | |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1841 | case LOG_FMT_EXPR: // sample expression, may be request or response |
| 1842 | key = NULL; |
Olivier Houchard | f90db44 | 2018-12-15 14:00:06 +0100 | [diff] [blame] | 1843 | if (tmp->options & LOG_OPT_REQ_CAP && s) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1844 | key = sample_fetch_as_type(be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, tmp->expr, SMP_T_STR); |
Olivier Houchard | f90db44 | 2018-12-15 14:00:06 +0100 | [diff] [blame] | 1845 | if (!key && (tmp->options & LOG_OPT_RES_CAP) && s) |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1846 | 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] | 1847 | if (tmp->options & LOG_OPT_HTTP) |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 1848 | ret = lf_encode_chunk(tmplog, dst + maxsize, |
| 1849 | '%', http_encode_map, key ? &key->data.u.str : &empty, tmp); |
Thierry FOURNIER | d048d8b | 2014-03-13 16:46:18 +0100 | [diff] [blame] | 1850 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1851 | ret = lf_text_len(tmplog, |
| 1852 | key ? key->data.u.str.area : NULL, |
| 1853 | key ? key->data.u.str.data : 0, |
| 1854 | dst + maxsize - tmplog, |
| 1855 | tmp); |
Willy Tarreau | c836845 | 2012-12-21 00:09:23 +0100 | [diff] [blame] | 1856 | if (ret == 0) |
| 1857 | goto out; |
| 1858 | tmplog = ret; |
| 1859 | last_isspace = 0; |
| 1860 | break; |
| 1861 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1862 | case LOG_FMT_CLIENTIP: // %ci |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1863 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1864 | if (conn) |
| 1865 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.from, dst + maxsize - tmplog, tmp); |
| 1866 | else |
| 1867 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1868 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1869 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1870 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1871 | last_isspace = 0; |
| 1872 | break; |
| 1873 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1874 | case LOG_FMT_CLIENTPORT: // %cp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1875 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1876 | if (conn) { |
| 1877 | if (conn->addr.from.ss_family == AF_UNIX) { |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1878 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1879 | } else { |
| 1880 | ret = lf_port(tmplog, (struct sockaddr *)&conn->addr.from, |
| 1881 | dst + maxsize - tmplog, tmp); |
| 1882 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1883 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1884 | else |
| 1885 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1886 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1887 | if (ret == NULL) |
| 1888 | goto out; |
| 1889 | tmplog = ret; |
| 1890 | last_isspace = 0; |
| 1891 | break; |
| 1892 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1893 | case LOG_FMT_FRONTENDIP: // %fi |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1894 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1895 | if (conn) { |
| 1896 | conn_get_to_addr(conn); |
| 1897 | ret = lf_ip(tmplog, (struct sockaddr *)&conn->addr.to, dst + maxsize - tmplog, tmp); |
| 1898 | } |
| 1899 | else |
| 1900 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1901 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1902 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1903 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1904 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1905 | last_isspace = 0; |
| 1906 | break; |
| 1907 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1908 | case LOG_FMT_FRONTENDPORT: // %fp |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 1909 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1910 | if (conn) { |
| 1911 | conn_get_to_addr(conn); |
| 1912 | if (conn->addr.to.ss_family == AF_UNIX) |
Willy Tarreau | fb0afa7 | 2015-04-03 14:46:27 +0200 | [diff] [blame] | 1913 | ret = ltoa_o(sess->listener->luid, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1914 | else |
| 1915 | 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] | 1916 | } |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1917 | else |
| 1918 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1919 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1920 | if (ret == NULL) |
| 1921 | goto out; |
| 1922 | tmplog = ret; |
| 1923 | last_isspace = 0; |
| 1924 | break; |
| 1925 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1926 | case LOG_FMT_BACKENDIP: // %bi |
Willy Tarreau | 2393c5b | 2018-09-05 15:24:56 +0200 | [diff] [blame] | 1927 | if (be_conn) |
| 1928 | ret = lf_ip(tmplog, (const struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1929 | else |
| 1930 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1931 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1932 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1933 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1934 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1935 | last_isspace = 0; |
| 1936 | break; |
| 1937 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1938 | case LOG_FMT_BACKENDPORT: // %bp |
Willy Tarreau | 2393c5b | 2018-09-05 15:24:56 +0200 | [diff] [blame] | 1939 | if (be_conn) |
| 1940 | ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.from, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1941 | else |
| 1942 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1943 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1944 | if (ret == NULL) |
| 1945 | goto out; |
| 1946 | tmplog = ret; |
| 1947 | last_isspace = 0; |
| 1948 | break; |
| 1949 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1950 | case LOG_FMT_SERVERIP: // %si |
Willy Tarreau | 2393c5b | 2018-09-05 15:24:56 +0200 | [diff] [blame] | 1951 | if (be_conn) |
| 1952 | ret = lf_ip(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1953 | else |
| 1954 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1955 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1956 | if (ret == NULL) |
| 1957 | goto out; |
| 1958 | tmplog = ret; |
| 1959 | last_isspace = 0; |
| 1960 | break; |
| 1961 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 1962 | case LOG_FMT_SERVERPORT: // %sp |
Willy Tarreau | 2393c5b | 2018-09-05 15:24:56 +0200 | [diff] [blame] | 1963 | if (be_conn) |
| 1964 | ret = lf_port(tmplog, (struct sockaddr *)&be_conn->addr.to, dst + maxsize - tmplog, tmp); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 1965 | else |
| 1966 | ret = lf_text_len(tmplog, NULL, 0, dst + maxsize - tmplog, tmp); |
| 1967 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1968 | if (ret == NULL) |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1969 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1970 | tmplog = ret; |
William Lallemand | b7ff6a3 | 2012-03-02 14:35:21 +0100 | [diff] [blame] | 1971 | last_isspace = 0; |
| 1972 | break; |
| 1973 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1974 | case LOG_FMT_DATE: // %t = accept date |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 1975 | get_localtime(logs->accept_date.tv_sec, &tm); |
| 1976 | ret = date2str_log(tmplog, &tm, &logs->accept_date, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1977 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1978 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1979 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1980 | last_isspace = 0; |
| 1981 | break; |
| 1982 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1983 | case LOG_FMT_tr: // %tr = start of request date |
| 1984 | /* Note that the timers are valid if we get here */ |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 1985 | tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 1986 | get_localtime(tv.tv_sec, &tm); |
| 1987 | ret = date2str_log(tmplog, &tm, &tv, dst + maxsize - tmplog); |
| 1988 | if (ret == NULL) |
| 1989 | goto out; |
| 1990 | tmplog = ret; |
| 1991 | last_isspace = 0; |
| 1992 | break; |
| 1993 | |
| 1994 | case LOG_FMT_DATEGMT: // %T = accept date, GMT |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 1995 | get_gmtime(logs->accept_date.tv_sec, &tm); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 1996 | ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1997 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 1998 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 1999 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2000 | last_isspace = 0; |
| 2001 | break; |
| 2002 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2003 | case LOG_FMT_trg: // %trg = start of request date, GMT |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2004 | tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2005 | get_gmtime(tv.tv_sec, &tm); |
| 2006 | ret = gmt2str_log(tmplog, &tm, dst + maxsize - tmplog); |
| 2007 | if (ret == NULL) |
| 2008 | goto out; |
| 2009 | tmplog = ret; |
| 2010 | last_isspace = 0; |
| 2011 | break; |
| 2012 | |
| 2013 | case LOG_FMT_DATELOCAL: // %Tl = accept date, local |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2014 | get_localtime(logs->accept_date.tv_sec, &tm); |
| 2015 | ret = localdate2str_log(tmplog, logs->accept_date.tv_sec, &tm, dst + maxsize - tmplog); |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 2016 | if (ret == NULL) |
| 2017 | goto out; |
| 2018 | tmplog = ret; |
| 2019 | last_isspace = 0; |
| 2020 | break; |
| 2021 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2022 | case LOG_FMT_trl: // %trl = start of request date, local |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2023 | tv_ms_add(&tv, &logs->accept_date, logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2024 | get_localtime(tv.tv_sec, &tm); |
| 2025 | ret = localdate2str_log(tmplog, tv.tv_sec, &tm, dst + maxsize - tmplog); |
| 2026 | if (ret == NULL) |
| 2027 | goto out; |
| 2028 | tmplog = ret; |
| 2029 | last_isspace = 0; |
| 2030 | break; |
| 2031 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2032 | case LOG_FMT_TS: // %Ts |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2033 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2034 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", (unsigned int)logs->accept_date.tv_sec); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2035 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2036 | goto out; |
| 2037 | last_isspace = 0; |
| 2038 | tmplog += iret; |
| 2039 | } else { |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2040 | ret = ltoa_o(logs->accept_date.tv_sec, tmplog, dst + maxsize - tmplog); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2041 | if (ret == NULL) |
| 2042 | goto out; |
| 2043 | tmplog = ret; |
| 2044 | last_isspace = 0; |
| 2045 | } |
| 2046 | break; |
| 2047 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2048 | case LOG_FMT_MS: // %ms |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2049 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2050 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%02X",(unsigned int)logs->accept_date.tv_usec/1000); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2051 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2052 | goto out; |
| 2053 | last_isspace = 0; |
| 2054 | tmplog += iret; |
| 2055 | } else { |
| 2056 | if ((dst + maxsize - tmplog) < 4) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2057 | goto out; |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2058 | ret = utoa_pad((unsigned int)logs->accept_date.tv_usec/1000, |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 2059 | tmplog, 4); |
| 2060 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2061 | goto out; |
Willy Tarreau | 9e60cd8 | 2013-01-24 01:18:16 +0100 | [diff] [blame] | 2062 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2063 | last_isspace = 0; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2064 | } |
| 2065 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2066 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2067 | case LOG_FMT_FRONTEND: // %f |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2068 | src = fe->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2069 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2070 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2071 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2072 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2073 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2074 | break; |
| 2075 | |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2076 | case LOG_FMT_FRONTEND_XPRT: // %ft |
| 2077 | src = fe->id; |
| 2078 | if (tmp->options & LOG_OPT_QUOTE) |
| 2079 | LOGCHAR('"'); |
| 2080 | iret = strlcpy2(tmplog, src, dst + maxsize - tmplog); |
| 2081 | if (iret == 0) |
| 2082 | goto out; |
| 2083 | tmplog += iret; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 2084 | if (sess->listener->bind_conf->xprt == xprt_get(XPRT_SSL)) |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2085 | LOGCHAR('~'); |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2086 | if (tmp->options & LOG_OPT_QUOTE) |
| 2087 | LOGCHAR('"'); |
| 2088 | last_isspace = 0; |
| 2089 | break; |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2090 | #ifdef USE_OPENSSL |
| 2091 | case LOG_FMT_SSL_CIPHER: // %sslc |
| 2092 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2093 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2094 | if (conn) { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 2095 | src = ssl_sock_get_cipher_name(conn); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2096 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2097 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 2098 | if (ret == NULL) |
| 2099 | goto out; |
| 2100 | tmplog = ret; |
| 2101 | last_isspace = 0; |
| 2102 | break; |
Willy Tarreau | 773d65f | 2012-10-12 14:56:11 +0200 | [diff] [blame] | 2103 | |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2104 | case LOG_FMT_SSL_VERSION: // %sslv |
| 2105 | src = NULL; |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 2106 | conn = objt_conn(sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2107 | if (conn) { |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 2108 | src = ssl_sock_get_proto_version(conn); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 2109 | } |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 2110 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 2111 | if (ret == NULL) |
| 2112 | goto out; |
| 2113 | tmplog = ret; |
| 2114 | last_isspace = 0; |
| 2115 | break; |
| 2116 | #endif |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2117 | case LOG_FMT_BACKEND: // %b |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2118 | src = be->id; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2119 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2120 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2121 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2122 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2123 | last_isspace = 0; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2124 | break; |
| 2125 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2126 | case LOG_FMT_SERVER: // %s |
Willy Tarreau | e1809df | 2018-09-05 15:30:16 +0200 | [diff] [blame] | 2127 | switch (obj_type(s ? s->target : NULL)) { |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2128 | case OBJ_TYPE_SERVER: |
Willy Tarreau | 1aaf324 | 2018-09-20 11:13:58 +0200 | [diff] [blame] | 2129 | src = __objt_server(s->target)->id; |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2130 | break; |
| 2131 | case OBJ_TYPE_APPLET: |
Willy Tarreau | 1aaf324 | 2018-09-20 11:13:58 +0200 | [diff] [blame] | 2132 | src = __objt_applet(s->target)->name; |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2133 | break; |
| 2134 | default: |
| 2135 | src = "<NOSRV>"; |
| 2136 | break; |
| 2137 | } |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2138 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2139 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2140 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2141 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2142 | last_isspace = 0; |
| 2143 | break; |
| 2144 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2145 | case LOG_FMT_Th: // %Th = handshake time |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2146 | ret = ltoa_o(logs->t_handshake, tmplog, dst + maxsize - tmplog); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2147 | if (ret == NULL) |
| 2148 | goto out; |
| 2149 | tmplog = ret; |
| 2150 | last_isspace = 0; |
| 2151 | break; |
| 2152 | |
| 2153 | case LOG_FMT_Ti: // %Ti = HTTP idle time |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2154 | ret = ltoa_o(logs->t_idle, tmplog, dst + maxsize - tmplog); |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2155 | if (ret == NULL) |
| 2156 | goto out; |
| 2157 | tmplog = ret; |
| 2158 | last_isspace = 0; |
| 2159 | break; |
| 2160 | |
| 2161 | case LOG_FMT_TR: // %TR = HTTP request time |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2162 | ret = ltoa_o((t_request >= 0) ? t_request - logs->t_idle - logs->t_handshake : -1, |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2163 | tmplog, dst + maxsize - tmplog); |
| 2164 | if (ret == NULL) |
| 2165 | goto out; |
| 2166 | tmplog = ret; |
| 2167 | last_isspace = 0; |
| 2168 | break; |
| 2169 | |
| 2170 | case LOG_FMT_TQ: // %Tq = Th + Ti + TR |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2171 | ret = ltoa_o(t_request, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2172 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2173 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2174 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2175 | last_isspace = 0; |
| 2176 | break; |
| 2177 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2178 | case LOG_FMT_TW: // %Tw |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2179 | ret = ltoa_o((logs->t_queue >= 0) ? logs->t_queue - t_request : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2180 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2181 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2182 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2183 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2184 | last_isspace = 0; |
| 2185 | break; |
| 2186 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2187 | case LOG_FMT_TC: // %Tc |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2188 | ret = ltoa_o((logs->t_connect >= 0) ? logs->t_connect - logs->t_queue : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2189 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2190 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2191 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2192 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2193 | last_isspace = 0; |
| 2194 | break; |
| 2195 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2196 | case LOG_FMT_Tr: // %Tr |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2197 | ret = ltoa_o((logs->t_data >= 0) ? logs->t_data - logs->t_connect : -1, |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2198 | tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2199 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2200 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2201 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2202 | last_isspace = 0; |
| 2203 | break; |
| 2204 | |
Willy Tarreau | 27b639d | 2016-05-17 17:55:27 +0200 | [diff] [blame] | 2205 | case LOG_FMT_TD: // %Td |
Willy Tarreau | a21c0e6 | 2018-09-05 15:07:15 +0200 | [diff] [blame] | 2206 | if (be->mode == PR_MODE_HTTP) |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2207 | ret = ltoa_o((logs->t_data >= 0) ? logs->t_close - logs->t_data : -1, |
Willy Tarreau | 27b639d | 2016-05-17 17:55:27 +0200 | [diff] [blame] | 2208 | tmplog, dst + maxsize - tmplog); |
| 2209 | else |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2210 | ret = ltoa_o((logs->t_connect >= 0) ? logs->t_close - logs->t_connect : -1, |
Willy Tarreau | 27b639d | 2016-05-17 17:55:27 +0200 | [diff] [blame] | 2211 | tmplog, dst + maxsize - tmplog); |
| 2212 | if (ret == NULL) |
| 2213 | goto out; |
| 2214 | tmplog = ret; |
| 2215 | last_isspace = 0; |
| 2216 | break; |
| 2217 | |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2218 | case LOG_FMT_Ta: // %Ta = active time = Tt - Th - Ti |
| 2219 | if (!(fe->to_log & LW_BYTES)) |
| 2220 | LOGCHAR('+'); |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2221 | ret = ltoa_o(logs->t_close - (logs->t_idle >= 0 ? logs->t_idle + logs->t_handshake : 0), |
Thierry FOURNIER / OZON.IO | 4cac359 | 2016-07-28 17:19:45 +0200 | [diff] [blame] | 2222 | tmplog, dst + maxsize - tmplog); |
| 2223 | if (ret == NULL) |
| 2224 | goto out; |
| 2225 | tmplog = ret; |
| 2226 | last_isspace = 0; |
| 2227 | break; |
| 2228 | |
| 2229 | case LOG_FMT_TT: // %Tt = total time |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2230 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2231 | LOGCHAR('+'); |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2232 | ret = ltoa_o(logs->t_close, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2233 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2234 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2235 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2236 | last_isspace = 0; |
| 2237 | break; |
| 2238 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2239 | case LOG_FMT_STATUS: // %ST |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2240 | ret = ltoa_o(txn ? txn->status : 0, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2241 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2242 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2243 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2244 | last_isspace = 0; |
| 2245 | break; |
| 2246 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2247 | case LOG_FMT_BYTES: // %B |
Willy Tarreau | d79a3b2 | 2012-12-28 09:40:16 +0100 | [diff] [blame] | 2248 | if (!(fe->to_log & LW_BYTES)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2249 | LOGCHAR('+'); |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2250 | ret = lltoa(logs->bytes_out, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2251 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2252 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2253 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2254 | last_isspace = 0; |
| 2255 | break; |
| 2256 | |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 2257 | case LOG_FMT_BYTES_UP: // %U |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2258 | ret = lltoa(logs->bytes_in, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | c5259fd | 2012-12-20 15:38:04 +0100 | [diff] [blame] | 2259 | if (ret == NULL) |
| 2260 | goto out; |
| 2261 | tmplog = ret; |
| 2262 | last_isspace = 0; |
| 2263 | break; |
| 2264 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2265 | case LOG_FMT_CCLIENT: // %CC |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2266 | src = txn ? txn->cli_cookie : NULL; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2267 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2268 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2269 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2270 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2271 | last_isspace = 0; |
| 2272 | break; |
| 2273 | |
Willy Tarreau | 2beef58 | 2012-12-20 17:22:52 +0100 | [diff] [blame] | 2274 | case LOG_FMT_CSERVER: // %CS |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2275 | src = txn ? txn->srv_cookie : NULL; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2276 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2277 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2278 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2279 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2280 | last_isspace = 0; |
| 2281 | break; |
| 2282 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2283 | case LOG_FMT_TERMSTATE: // %ts |
Willy Tarreau | b8bc525 | 2018-09-05 15:51:28 +0200 | [diff] [blame] | 2284 | LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 2285 | LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 6580c06 | 2012-03-12 15:09:42 +0100 | [diff] [blame] | 2286 | *tmplog = '\0'; |
| 2287 | last_isspace = 0; |
| 2288 | break; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2289 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2290 | case LOG_FMT_TERMSTATE_CK: // %tsc, same as TS with cookie state (for mode HTTP) |
Willy Tarreau | b8bc525 | 2018-09-05 15:51:28 +0200 | [diff] [blame] | 2291 | LOGCHAR(sess_term_cond[(s_flags & SF_ERR_MASK) >> SF_ERR_SHIFT]); |
| 2292 | LOGCHAR(sess_fin_state[(s_flags & SF_FINST_MASK) >> SF_FINST_SHIFT]); |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2293 | LOGCHAR((txn && (be->ck_opts & PR_CK_ANY)) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-'); |
| 2294 | LOGCHAR((txn && (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] | 2295 | last_isspace = 0; |
| 2296 | break; |
| 2297 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2298 | case LOG_FMT_ACTCONN: // %ac |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2299 | ret = ltoa_o(actconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2300 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2301 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2302 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2303 | last_isspace = 0; |
| 2304 | break; |
| 2305 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2306 | case LOG_FMT_FECONN: // %fc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2307 | ret = ltoa_o(fe->feconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2308 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2309 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2310 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2311 | last_isspace = 0; |
| 2312 | break; |
| 2313 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2314 | case LOG_FMT_BECONN: // %bc |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2315 | ret = ltoa_o(be->beconn, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2316 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2317 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2318 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2319 | last_isspace = 0; |
| 2320 | break; |
| 2321 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2322 | case LOG_FMT_SRVCONN: // %sc |
Willy Tarreau | e1809df | 2018-09-05 15:30:16 +0200 | [diff] [blame] | 2323 | ret = ultoa_o(objt_server(s ? s->target : NULL) ? |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 2324 | objt_server(s->target)->cur_sess : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2325 | 0, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2326 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2327 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2328 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2329 | last_isspace = 0; |
| 2330 | break; |
| 2331 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2332 | case LOG_FMT_RETRIES: // %rq |
Willy Tarreau | b8bc525 | 2018-09-05 15:51:28 +0200 | [diff] [blame] | 2333 | if (s_flags & SF_REDISP) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2334 | LOGCHAR('+'); |
Willy Tarreau | abd71a5 | 2018-09-04 19:21:44 +0200 | [diff] [blame] | 2335 | ret = ltoa_o((s && s->si[1].conn_retries > 0) ? |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2336 | (be->conn_retries - s->si[1].conn_retries) : |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2337 | be->conn_retries, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2338 | if (ret == NULL) |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2339 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2340 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2341 | last_isspace = 0; |
| 2342 | break; |
| 2343 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2344 | case LOG_FMT_SRVQUEUE: // %sq |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2345 | ret = ltoa_o(logs->srv_queue_pos, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2346 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2347 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2348 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2349 | last_isspace = 0; |
| 2350 | break; |
| 2351 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2352 | case LOG_FMT_BCKQUEUE: // %bq |
Willy Tarreau | 372ac5a | 2018-09-05 15:16:23 +0200 | [diff] [blame] | 2353 | ret = ltoa_o(logs->prx_queue_pos, tmplog, dst + maxsize - tmplog); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2354 | if (ret == NULL) |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2355 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2356 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2357 | last_isspace = 0; |
| 2358 | break; |
| 2359 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2360 | case LOG_FMT_HDRREQUEST: // %hr |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2361 | /* request header */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2362 | if (fe->nb_req_cap && s && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2363 | if (tmp->options & LOG_OPT_QUOTE) |
| 2364 | LOGCHAR('"'); |
| 2365 | LOGCHAR('{'); |
| 2366 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 2367 | if (hdr) |
| 2368 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2369 | if (s->req_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2370 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2371 | '#', hdr_encode_map, s->req_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2372 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2373 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2374 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2375 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2376 | } |
| 2377 | LOGCHAR('}'); |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2378 | if (tmp->options & LOG_OPT_QUOTE) |
| 2379 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2380 | last_isspace = 0; |
| 2381 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2382 | break; |
| 2383 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2384 | case LOG_FMT_HDRREQUESTLIST: // %hrl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2385 | /* request header list */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2386 | if (fe->nb_req_cap && s && s->req_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2387 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 2388 | if (hdr > 0) |
| 2389 | LOGCHAR(' '); |
| 2390 | if (tmp->options & LOG_OPT_QUOTE) |
| 2391 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2392 | if (s->req_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2393 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2394 | '#', hdr_encode_map, s->req_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2395 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2396 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2397 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2398 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 2399 | LOGCHAR('-'); |
| 2400 | if (tmp->options & LOG_OPT_QUOTE) |
| 2401 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2402 | last_isspace = 0; |
| 2403 | } |
| 2404 | } |
| 2405 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2406 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2407 | |
| 2408 | case LOG_FMT_HDRRESPONS: // %hs |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2409 | /* response header */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2410 | if (fe->nb_rsp_cap && s && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2411 | if (tmp->options & LOG_OPT_QUOTE) |
| 2412 | LOGCHAR('"'); |
| 2413 | LOGCHAR('{'); |
| 2414 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 2415 | if (hdr) |
| 2416 | LOGCHAR('|'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2417 | if (s->res_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2418 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2419 | '#', hdr_encode_map, s->res_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2420 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2421 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2422 | tmplog = ret; |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2423 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2424 | } |
| 2425 | LOGCHAR('}'); |
| 2426 | last_isspace = 0; |
| 2427 | if (tmp->options & LOG_OPT_QUOTE) |
| 2428 | LOGCHAR('"'); |
| 2429 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2430 | break; |
| 2431 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2432 | case LOG_FMT_HDRRESPONSLIST: // %hsl |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2433 | /* response header list */ |
Willy Tarreau | d4f9166 | 2018-09-05 15:28:07 +0200 | [diff] [blame] | 2434 | if (fe->nb_rsp_cap && s && s->res_cap) { |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2435 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 2436 | if (hdr > 0) |
| 2437 | LOGCHAR(' '); |
| 2438 | if (tmp->options & LOG_OPT_QUOTE) |
| 2439 | LOGCHAR('"'); |
Willy Tarreau | cb7dd01 | 2015-04-03 22:16:32 +0200 | [diff] [blame] | 2440 | if (s->res_cap[hdr] != NULL) { |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2441 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2442 | '#', hdr_encode_map, s->res_cap[hdr], tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2443 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2444 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2445 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2446 | } else if (!(tmp->options & LOG_OPT_QUOTE)) |
| 2447 | LOGCHAR('-'); |
| 2448 | if (tmp->options & LOG_OPT_QUOTE) |
| 2449 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2450 | last_isspace = 0; |
| 2451 | } |
| 2452 | } |
| 2453 | break; |
| 2454 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2455 | case LOG_FMT_REQ: // %r |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2456 | /* Request */ |
| 2457 | if (tmp->options & LOG_OPT_QUOTE) |
| 2458 | LOGCHAR('"'); |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2459 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2460 | ret = lf_encode_string(tmplog, dst + maxsize, |
| 2461 | '#', url_encode_map, uri, tmp); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2462 | if (ret == NULL || *ret != '\0') |
William Lallemand | 51b5dca | 2012-03-26 17:52:55 +0200 | [diff] [blame] | 2463 | goto out; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2464 | tmplog = ret; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2465 | if (tmp->options & LOG_OPT_QUOTE) |
| 2466 | LOGCHAR('"'); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2467 | last_isspace = 0; |
| 2468 | break; |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2469 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2470 | case LOG_FMT_HTTP_PATH: // %HP |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2471 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2472 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2473 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2474 | LOGCHAR('"'); |
| 2475 | |
| 2476 | end = uri + strlen(uri); |
| 2477 | // look for the first whitespace character |
| 2478 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2479 | uri++; |
| 2480 | |
| 2481 | // keep advancing past multiple spaces |
| 2482 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2483 | uri++; nspaces++; |
| 2484 | } |
| 2485 | |
| 2486 | // look for first space or question mark after url |
| 2487 | spc = uri; |
| 2488 | while (spc < end && *spc != '?' && !HTTP_IS_SPHT(*spc)) |
| 2489 | spc++; |
| 2490 | |
Nenad Merdanovic | 54e439f | 2016-04-26 01:39:02 +0200 | [diff] [blame] | 2491 | if (!txn || !txn->uri || nspaces == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2492 | chunk.area = "<BADREQ>"; |
| 2493 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2494 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2495 | chunk.area = uri; |
| 2496 | chunk.data = spc - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2499 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2500 | if (ret == NULL || *ret != '\0') |
| 2501 | goto out; |
| 2502 | |
| 2503 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2504 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2505 | LOGCHAR('"'); |
| 2506 | |
| 2507 | last_isspace = 0; |
| 2508 | break; |
| 2509 | |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2510 | case LOG_FMT_HTTP_QUERY: // %HQ |
| 2511 | if (tmp->options & LOG_OPT_QUOTE) |
| 2512 | LOGCHAR('"'); |
| 2513 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2514 | if (!txn || !txn->uri) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2515 | chunk.area = "<BADREQ>"; |
| 2516 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2517 | } else { |
| 2518 | uri = txn->uri; |
| 2519 | end = uri + strlen(uri); |
| 2520 | // look for the first question mark |
| 2521 | while (uri < end && *uri != '?') |
| 2522 | uri++; |
| 2523 | |
| 2524 | qmark = uri; |
| 2525 | // look for first space or question mark after url |
| 2526 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2527 | uri++; |
| 2528 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2529 | chunk.area = qmark; |
| 2530 | chunk.data = uri - qmark; |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2533 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | e63ac87 | 2015-07-31 16:14:16 +0000 | [diff] [blame] | 2534 | if (ret == NULL || *ret != '\0') |
| 2535 | goto out; |
| 2536 | |
| 2537 | tmplog = ret; |
| 2538 | if (tmp->options & LOG_OPT_QUOTE) |
| 2539 | LOGCHAR('"'); |
| 2540 | |
| 2541 | last_isspace = 0; |
| 2542 | break; |
| 2543 | |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2544 | case LOG_FMT_HTTP_URI: // %HU |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2545 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2546 | |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2547 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2548 | LOGCHAR('"'); |
| 2549 | |
| 2550 | end = uri + strlen(uri); |
| 2551 | // look for the first whitespace character |
| 2552 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2553 | uri++; |
| 2554 | |
| 2555 | // keep advancing past multiple spaces |
| 2556 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2557 | uri++; nspaces++; |
| 2558 | } |
| 2559 | |
| 2560 | // look for first space after url |
| 2561 | spc = uri; |
| 2562 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 2563 | spc++; |
| 2564 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2565 | if (!txn || !txn->uri || nspaces == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2566 | chunk.area = "<BADREQ>"; |
| 2567 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2568 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2569 | chunk.area = uri; |
| 2570 | chunk.data = spc - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2573 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2574 | if (ret == NULL || *ret != '\0') |
| 2575 | goto out; |
| 2576 | |
| 2577 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2578 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2579 | LOGCHAR('"'); |
| 2580 | |
| 2581 | last_isspace = 0; |
| 2582 | break; |
| 2583 | |
| 2584 | case LOG_FMT_HTTP_METHOD: // %HM |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2585 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2586 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2587 | LOGCHAR('"'); |
| 2588 | |
| 2589 | end = uri + strlen(uri); |
| 2590 | // look for the first whitespace character |
| 2591 | spc = uri; |
| 2592 | while (spc < end && !HTTP_IS_SPHT(*spc)) |
| 2593 | spc++; |
| 2594 | |
| 2595 | if (spc == end) { // odd case, we have txn->uri, but we only got a verb |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2596 | chunk.area = "<BADREQ>"; |
| 2597 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2598 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2599 | chunk.area = uri; |
| 2600 | chunk.data = spc - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2603 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2604 | if (ret == NULL || *ret != '\0') |
| 2605 | goto out; |
| 2606 | |
| 2607 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2608 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2609 | LOGCHAR('"'); |
| 2610 | |
| 2611 | last_isspace = 0; |
| 2612 | break; |
| 2613 | |
| 2614 | case LOG_FMT_HTTP_VERSION: // %HV |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2615 | uri = txn && txn->uri ? txn->uri : "<BADREQ>"; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2616 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2617 | LOGCHAR('"'); |
| 2618 | |
| 2619 | end = uri + strlen(uri); |
| 2620 | // look for the first whitespace character |
| 2621 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2622 | uri++; |
| 2623 | |
| 2624 | // keep advancing past multiple spaces |
| 2625 | while (uri < end && HTTP_IS_SPHT(*uri)) { |
| 2626 | uri++; nspaces++; |
| 2627 | } |
| 2628 | |
| 2629 | // look for the next whitespace character |
| 2630 | while (uri < end && !HTTP_IS_SPHT(*uri)) |
| 2631 | uri++; |
| 2632 | |
| 2633 | // keep advancing past multiple spaces |
| 2634 | while (uri < end && HTTP_IS_SPHT(*uri)) |
| 2635 | uri++; |
| 2636 | |
Willy Tarreau | 57bc891 | 2016-04-25 17:09:40 +0200 | [diff] [blame] | 2637 | if (!txn || !txn->uri || nspaces == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2638 | chunk.area = "<BADREQ>"; |
| 2639 | chunk.data = strlen("<BADREQ>"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2640 | } else if (uri == end) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2641 | chunk.area = "HTTP/0.9"; |
| 2642 | chunk.data = strlen("HTTP/0.9"); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2643 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2644 | chunk.area = uri; |
| 2645 | chunk.data = end - uri; |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2646 | } |
| 2647 | |
Dragan Dosen | 835b921 | 2016-02-12 13:23:03 +0100 | [diff] [blame] | 2648 | ret = lf_encode_chunk(tmplog, dst + maxsize, '#', url_encode_map, &chunk, tmp); |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2649 | if (ret == NULL || *ret != '\0') |
| 2650 | goto out; |
| 2651 | |
| 2652 | tmplog = ret; |
Willy Tarreau | b7636d1 | 2015-06-17 19:58:02 +0200 | [diff] [blame] | 2653 | if (tmp->options & LOG_OPT_QUOTE) |
Andrew Hayworth | 0ebc55f | 2015-04-27 21:37:03 +0000 | [diff] [blame] | 2654 | LOGCHAR('"'); |
| 2655 | |
| 2656 | last_isspace = 0; |
| 2657 | break; |
| 2658 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2659 | case LOG_FMT_COUNTER: // %rt |
| 2660 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | 5cacab6 | 2018-09-05 15:52:59 +0200 | [diff] [blame] | 2661 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", uniq_id); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2662 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2663 | goto out; |
| 2664 | last_isspace = 0; |
| 2665 | tmplog += iret; |
| 2666 | } else { |
Willy Tarreau | 5cacab6 | 2018-09-05 15:52:59 +0200 | [diff] [blame] | 2667 | ret = ltoa_o(uniq_id, tmplog, dst + maxsize - tmplog); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2668 | if (ret == NULL) |
| 2669 | goto out; |
| 2670 | tmplog = ret; |
| 2671 | last_isspace = 0; |
| 2672 | } |
| 2673 | break; |
| 2674 | |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2675 | case LOG_FMT_LOGCNT: // %lc |
| 2676 | if (tmp->options & LOG_OPT_HEXA) { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2677 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", fe->log_count); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2678 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2679 | goto out; |
| 2680 | last_isspace = 0; |
| 2681 | tmplog += iret; |
| 2682 | } else { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2683 | ret = ultoa_o(fe->log_count, tmplog, dst + maxsize - tmplog); |
Willy Tarreau | 7346acb | 2014-08-28 15:03:15 +0200 | [diff] [blame] | 2684 | if (ret == NULL) |
| 2685 | goto out; |
| 2686 | tmplog = ret; |
| 2687 | last_isspace = 0; |
| 2688 | } |
| 2689 | break; |
| 2690 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 2691 | case LOG_FMT_HOSTNAME: // %H |
| 2692 | src = hostname; |
| 2693 | ret = lf_text(tmplog, src, dst + maxsize - tmplog, tmp); |
| 2694 | if (ret == NULL) |
| 2695 | goto out; |
| 2696 | tmplog = ret; |
| 2697 | last_isspace = 0; |
| 2698 | break; |
| 2699 | |
| 2700 | case LOG_FMT_PID: // %pid |
| 2701 | if (tmp->options & LOG_OPT_HEXA) { |
| 2702 | iret = snprintf(tmplog, dst + maxsize - tmplog, "%04X", pid); |
| 2703 | if (iret < 0 || iret > dst + maxsize - tmplog) |
| 2704 | goto out; |
| 2705 | last_isspace = 0; |
| 2706 | tmplog += iret; |
| 2707 | } else { |
| 2708 | ret = ltoa_o(pid, tmplog, dst + maxsize - tmplog); |
| 2709 | if (ret == NULL) |
| 2710 | goto out; |
| 2711 | tmplog = ret; |
| 2712 | last_isspace = 0; |
| 2713 | } |
| 2714 | break; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2715 | |
| 2716 | case LOG_FMT_UNIQUEID: // %ID |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2717 | ret = NULL; |
Willy Tarreau | 02fdf4f | 2018-09-05 15:49:01 +0200 | [diff] [blame] | 2718 | src = s ? s->unique_id : NULL; |
Thierry FOURNIER | 1be6910 | 2014-04-15 01:38:48 +0200 | [diff] [blame] | 2719 | ret = lf_text(tmplog, src, maxsize - (tmplog - dst), tmp); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2720 | if (ret == NULL) |
| 2721 | goto out; |
| 2722 | tmplog = ret; |
| 2723 | last_isspace = 0; |
| 2724 | break; |
| 2725 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | out: |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2730 | /* *tmplog is a unused character */ |
| 2731 | *tmplog = '\0'; |
Willy Tarreau | df97447 | 2012-12-28 02:44:01 +0100 | [diff] [blame] | 2732 | return tmplog - dst; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2733 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2734 | } |
| 2735 | |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2736 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2737 | * 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] | 2738 | * Will not log if the frontend has no log defined. |
| 2739 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2740 | void strm_log(struct stream *s) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2741 | { |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2742 | struct session *sess = s->sess; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2743 | int size, err, level; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2744 | int sd_size = 0; |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2745 | |
| 2746 | /* if we don't want to log normal traffic, return now */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 2747 | err = (s->flags & SF_REDISP) || |
| 2748 | ((s->flags & SF_ERR_MASK) > SF_ERR_LOCAL) || |
| 2749 | (((s->flags & SF_ERR_MASK) == SF_ERR_NONE) && |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 2750 | (s->si[1].conn_retries != s->be->conn_retries)) || |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 2751 | ((sess->fe->mode == PR_MODE_HTTP) && s->txn && s->txn->status >= 500); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2752 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2753 | if (!err && (sess->fe->options2 & PR_O2_NOLOGNORM)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2754 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2755 | |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2756 | if (LIST_ISEMPTY(&sess->fe->logsrvs)) |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2757 | return; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2758 | |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2759 | if (s->logs.level) { /* loglevel was overridden */ |
| 2760 | if (s->logs.level == -1) { |
| 2761 | s->logs.logwait = 0; /* logs disabled */ |
| 2762 | return; |
| 2763 | } |
| 2764 | level = s->logs.level - 1; |
| 2765 | } |
| 2766 | else { |
| 2767 | level = LOG_INFO; |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2768 | if (err && (sess->fe->options2 & PR_O2_LOGERRORS)) |
Willy Tarreau | abcd514 | 2013-06-11 17:18:02 +0200 | [diff] [blame] | 2769 | level = LOG_ERR; |
| 2770 | } |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2771 | |
William Lallemand | 5b7ea3a | 2013-08-28 15:44:19 +0200 | [diff] [blame] | 2772 | /* if unique-id was not generated */ |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2773 | if (!s->unique_id && !LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2774 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) != NULL) |
Willy Tarreau | e36cbcb | 2015-04-03 15:40:56 +0200 | [diff] [blame] | 2775 | 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] | 2776 | } |
| 2777 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2778 | if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) { |
| 2779 | sd_size = build_logline(s, logline_rfc5424, global.max_syslog_len, |
| 2780 | &sess->fe->logformat_sd); |
| 2781 | } |
| 2782 | |
Dragan Dosen | 59cee97 | 2015-09-19 22:09:02 +0200 | [diff] [blame] | 2783 | size = build_logline(s, logline, global.max_syslog_len, &sess->fe->logformat); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2784 | if (size > 0) { |
Olivier Houchard | d2ee3e7 | 2019-03-08 18:53:21 +0100 | [diff] [blame] | 2785 | _HA_ATOMIC_ADD(&sess->fe->log_count, 1); |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 2786 | __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size); |
William Lallemand | 1d70556 | 2012-03-12 12:46:41 +0100 | [diff] [blame] | 2787 | s->logs.logwait = 0; |
| 2788 | } |
| 2789 | } |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 2790 | |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 2791 | /* |
| 2792 | * send a minimalist log for the session. Will not log if the frontend has no |
| 2793 | * log defined. It is assumed that this is only used to report anomalies that |
| 2794 | * cannot lead to the creation of a regular stream. Because of this the log |
| 2795 | * level is LOG_INFO or LOG_ERR depending on the "log-separate-error" setting |
| 2796 | * in the frontend. The caller must simply know that it should not call this |
Willy Tarreau | 9fa267d | 2018-10-05 10:22:27 +0200 | [diff] [blame] | 2797 | * function to report unimportant events. It is safe to call this function with |
| 2798 | * sess==NULL (will not do anything). |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 2799 | */ |
| 2800 | void sess_log(struct session *sess) |
| 2801 | { |
| 2802 | int size, level; |
| 2803 | int sd_size = 0; |
| 2804 | |
Willy Tarreau | 9fa267d | 2018-10-05 10:22:27 +0200 | [diff] [blame] | 2805 | if (!sess) |
| 2806 | return; |
| 2807 | |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 2808 | if (LIST_ISEMPTY(&sess->fe->logsrvs)) |
| 2809 | return; |
| 2810 | |
| 2811 | level = LOG_INFO; |
| 2812 | if (sess->fe->options2 & PR_O2_LOGERRORS) |
| 2813 | level = LOG_ERR; |
| 2814 | |
| 2815 | if (!LIST_ISEMPTY(&sess->fe->logformat_sd)) { |
| 2816 | sd_size = sess_build_logline(sess, NULL, |
| 2817 | logline_rfc5424, global.max_syslog_len, |
| 2818 | &sess->fe->logformat_sd); |
| 2819 | } |
| 2820 | |
| 2821 | size = sess_build_logline(sess, NULL, logline, global.max_syslog_len, &sess->fe->logformat); |
| 2822 | if (size > 0) { |
Olivier Houchard | d2ee3e7 | 2019-03-08 18:53:21 +0100 | [diff] [blame] | 2823 | _HA_ATOMIC_ADD(&sess->fe->log_count, 1); |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 2824 | __send_log(sess->fe, level, logline, size + 1, logline_rfc5424, sd_size); |
| 2825 | } |
| 2826 | } |
| 2827 | |
Christopher Faulet | c1b730a | 2017-10-24 12:00:51 +0200 | [diff] [blame] | 2828 | static int cli_io_handler_show_startup_logs(struct appctx *appctx) |
| 2829 | { |
| 2830 | struct stream_interface *si = appctx->owner; |
| 2831 | const char *msg = (startup_logs ? startup_logs : "No startup alerts/warnings.\n"); |
| 2832 | |
| 2833 | if (ci_putstr(si_ic(si), msg) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 2834 | si_rx_room_blk(si); |
Christopher Faulet | c1b730a | 2017-10-24 12:00:51 +0200 | [diff] [blame] | 2835 | return 0; |
| 2836 | } |
| 2837 | return 1; |
| 2838 | } |
| 2839 | |
| 2840 | /* register cli keywords */ |
| 2841 | static struct cli_kw_list cli_kws = {{ },{ |
| 2842 | { { "show", "startup-logs", NULL }, |
| 2843 | "show startup-logs : report logs emitted during HAProxy startup", |
| 2844 | NULL, cli_io_handler_show_startup_logs }, |
| 2845 | {{},} |
| 2846 | }}; |
| 2847 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 2848 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 2849 | |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 2850 | REGISTER_PER_THREAD_INIT(init_log_buffers_per_thread); |
| 2851 | REGISTER_PER_THREAD_DEINIT(deinit_log_buffers_per_thread); |
| 2852 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2853 | /* |
| 2854 | * Local variables: |
| 2855 | * c-indent-level: 8 |
| 2856 | * c-basic-offset: 8 |
| 2857 | * End: |
| 2858 | */ |