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