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