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