Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Event sink management |
| 3 | * |
| 4 | * Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation, version 2.1 |
| 9 | * exclusively. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 21 | #include <common/compat.h> |
| 22 | #include <common/config.h> |
| 23 | #include <common/ist.h> |
| 24 | #include <common/mini-clist.h> |
Willy Tarreau | 53ba9d9 | 2019-09-26 08:03:58 +0200 | [diff] [blame] | 25 | #include <common/time.h> |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 26 | #include <proto/cli.h> |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 27 | #include <proto/log.h> |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 28 | #include <proto/ring.h> |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 29 | #include <proto/sink.h> |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 30 | #include <proto/stream_interface.h> |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 31 | |
| 32 | struct list sink_list = LIST_HEAD_INIT(sink_list); |
| 33 | |
| 34 | struct sink *sink_find(const char *name) |
| 35 | { |
| 36 | struct sink *sink; |
| 37 | |
| 38 | list_for_each_entry(sink, &sink_list, sink_list) |
| 39 | if (strcmp(sink->name, name) == 0) |
| 40 | return sink; |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | /* creates a new sink and adds it to the list, it's still generic and not fully |
| 45 | * initialized. Returns NULL on allocation failure. If another one already |
| 46 | * exists with the same name, it will be returned. The caller can detect it as |
| 47 | * a newly created one has type SINK_TYPE_NEW. |
| 48 | */ |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 49 | static struct sink *__sink_new(const char *name, const char *desc, enum sink_fmt fmt) |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 50 | { |
| 51 | struct sink *sink; |
| 52 | |
| 53 | sink = sink_find(name); |
| 54 | if (sink) |
| 55 | goto end; |
| 56 | |
| 57 | sink = malloc(sizeof(*sink)); |
| 58 | if (!sink) |
| 59 | goto end; |
| 60 | |
| 61 | sink->name = name; |
| 62 | sink->desc = desc; |
| 63 | sink->fmt = fmt; |
| 64 | sink->type = SINK_TYPE_NEW; |
Christopher Faulet | a63a5c2 | 2019-11-15 15:10:12 +0100 | [diff] [blame] | 65 | sink->maxlen = BUFSIZE; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 66 | /* address will be filled by the caller if needed */ |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 67 | sink->ctx.fd = -1; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 68 | sink->ctx.dropped = 0; |
| 69 | HA_RWLOCK_INIT(&sink->ctx.lock); |
| 70 | LIST_ADDQ(&sink_list, &sink->sink_list); |
| 71 | end: |
| 72 | return sink; |
| 73 | } |
| 74 | |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 75 | /* creates a sink called <name> of type FD associated to fd <fd>, format <fmt>, |
| 76 | * and description <desc>. Returns NULL on allocation failure or conflict. |
| 77 | * Perfect duplicates are merged (same type, fd, and name). |
| 78 | */ |
| 79 | struct sink *sink_new_fd(const char *name, const char *desc, enum sink_fmt fmt, int fd) |
| 80 | { |
| 81 | struct sink *sink; |
| 82 | |
| 83 | sink = __sink_new(name, desc, fmt); |
| 84 | if (!sink || (sink->type == SINK_TYPE_FD && sink->ctx.fd == fd)) |
| 85 | goto end; |
| 86 | |
| 87 | if (sink->type != SINK_TYPE_NEW) { |
| 88 | sink = NULL; |
| 89 | goto end; |
| 90 | } |
| 91 | |
| 92 | sink->type = SINK_TYPE_FD; |
| 93 | sink->ctx.fd = fd; |
| 94 | end: |
| 95 | return sink; |
| 96 | } |
| 97 | |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 98 | /* creates a sink called <name> of type BUF of size <size>, format <fmt>, |
| 99 | * and description <desc>. Returns NULL on allocation failure or conflict. |
| 100 | * Perfect duplicates are merged (same type and name). If sizes differ, the |
| 101 | * largest one is kept. |
| 102 | */ |
| 103 | struct sink *sink_new_buf(const char *name, const char *desc, enum sink_fmt fmt, size_t size) |
| 104 | { |
| 105 | struct sink *sink; |
| 106 | |
| 107 | sink = __sink_new(name, desc, fmt); |
| 108 | if (!sink) |
| 109 | goto fail; |
| 110 | |
| 111 | if (sink->type == SINK_TYPE_BUFFER) { |
| 112 | /* such a buffer already exists, we may have to resize it */ |
| 113 | if (!ring_resize(sink->ctx.ring, size)) |
| 114 | goto fail; |
| 115 | goto end; |
| 116 | } |
| 117 | |
| 118 | if (sink->type != SINK_TYPE_NEW) { |
| 119 | /* already exists of another type */ |
| 120 | goto fail; |
| 121 | } |
| 122 | |
| 123 | sink->ctx.ring = ring_new(size); |
| 124 | if (!sink->ctx.ring) { |
| 125 | LIST_DEL(&sink->sink_list); |
| 126 | free(sink); |
| 127 | goto fail; |
| 128 | } |
| 129 | |
| 130 | sink->type = SINK_TYPE_BUFFER; |
| 131 | end: |
| 132 | return sink; |
| 133 | fail: |
| 134 | return NULL; |
| 135 | } |
| 136 | |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 137 | /* tries to send <nmsg> message parts (up to 8, ignored above) from message |
| 138 | * array <msg> to sink <sink>. Formating according to the sink's preference is |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 139 | * done here. Lost messages are NOT accounted for. It is preferable to call |
| 140 | * sink_write() instead which will also try to emit the number of dropped |
| 141 | * messages when there are any. It returns >0 if it could write anything, |
| 142 | * <=0 otherwise. |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 143 | */ |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 144 | ssize_t __sink_write(struct sink *sink, const struct ist msg[], size_t nmsg, |
| 145 | int level, int facility, struct ist *tag, |
| 146 | struct ist *pid, struct ist *sd) |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 147 | { |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 148 | int log_format; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 149 | char short_hdr[4]; |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 150 | struct ist pfx[6]; |
Willy Tarreau | a1426de | 2019-08-27 14:21:02 +0200 | [diff] [blame] | 151 | size_t npfx = 0; |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 152 | char *hdr_ptr; |
| 153 | int fac_level; |
| 154 | |
| 155 | if (sink->fmt == SINK_FMT_RAW) |
| 156 | goto send; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 157 | |
Willy Tarreau | 53ba9d9 | 2019-09-26 08:03:58 +0200 | [diff] [blame] | 158 | if (sink->fmt == SINK_FMT_SHORT || sink->fmt == SINK_FMT_TIMED) { |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 159 | short_hdr[0] = '<'; |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 160 | short_hdr[1] = '0' + level; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 161 | short_hdr[2] = '>'; |
| 162 | |
Willy Tarreau | a1426de | 2019-08-27 14:21:02 +0200 | [diff] [blame] | 163 | pfx[npfx].ptr = short_hdr; |
| 164 | pfx[npfx].len = 3; |
| 165 | npfx++; |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 166 | if (sink->fmt == SINK_FMT_SHORT) |
| 167 | goto send; |
Willy Tarreau | a1426de | 2019-08-27 14:21:02 +0200 | [diff] [blame] | 168 | } |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 169 | |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 170 | |
Willy Tarreau | 53ba9d9 | 2019-09-26 08:03:58 +0200 | [diff] [blame] | 171 | if (sink->fmt == SINK_FMT_ISO || sink->fmt == SINK_FMT_TIMED) { |
| 172 | pfx[npfx].ptr = timeofday_as_iso_us(1); |
| 173 | pfx[npfx].len = 27; |
| 174 | npfx++; |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 175 | goto send; |
Willy Tarreau | 53ba9d9 | 2019-09-26 08:03:58 +0200 | [diff] [blame] | 176 | } |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 177 | else if (sink->fmt == SINK_FMT_RFC5424) { |
| 178 | pfx[npfx].ptr = logheader_rfc5424; |
| 179 | pfx[npfx].len = update_log_hdr_rfc5424(date.tv_sec) - pfx[npfx].ptr; |
| 180 | log_format = LOG_FORMAT_RFC5424; |
| 181 | } |
| 182 | else { |
| 183 | pfx[npfx].ptr = logheader; |
| 184 | pfx[npfx].len = update_log_hdr(date.tv_sec) - pfx[npfx].ptr; |
| 185 | log_format = LOG_FORMAT_RFC3164; |
| 186 | sd = NULL; |
| 187 | } |
| 188 | |
| 189 | fac_level = (facility << 3) + level; |
| 190 | hdr_ptr = pfx[npfx].ptr + 3; /* last digit of the log level */ |
| 191 | do { |
| 192 | *hdr_ptr = '0' + fac_level % 10; |
| 193 | fac_level /= 10; |
| 194 | hdr_ptr--; |
| 195 | } while (fac_level && hdr_ptr > pfx[npfx].ptr); |
| 196 | *hdr_ptr = '<'; |
| 197 | pfx[npfx].len -= hdr_ptr - pfx[npfx].ptr; |
| 198 | pfx[npfx].ptr = hdr_ptr; |
| 199 | npfx++; |
| 200 | |
| 201 | if (tag && tag->len) { |
| 202 | pfx[npfx].ptr = tag->ptr; |
| 203 | pfx[npfx].len = tag->len; |
| 204 | npfx++; |
| 205 | } |
| 206 | pfx[npfx].ptr = get_format_pid_sep1(log_format, &pfx[npfx].len); |
| 207 | if (pfx[npfx].len) |
| 208 | npfx++; |
Willy Tarreau | 53ba9d9 | 2019-09-26 08:03:58 +0200 | [diff] [blame] | 209 | |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 210 | if (pid && pid->len) { |
| 211 | pfx[npfx].ptr = pid->ptr; |
| 212 | pfx[npfx].len = pid->len; |
| 213 | npfx++; |
| 214 | } |
| 215 | |
| 216 | pfx[npfx].ptr = get_format_pid_sep2(log_format, &pfx[npfx].len); |
| 217 | if (pfx[npfx].len) |
| 218 | npfx++; |
| 219 | |
| 220 | if (sd && sd->len) { |
| 221 | pfx[npfx].ptr = sd->ptr; |
| 222 | pfx[npfx].len = sd->len; |
| 223 | npfx++; |
| 224 | } |
| 225 | |
| 226 | send: |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 227 | if (sink->type == SINK_TYPE_FD) { |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 228 | return fd_write_frag_line(sink->ctx.fd, sink->maxlen, pfx, npfx, msg, nmsg, 1); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 229 | } |
| 230 | else if (sink->type == SINK_TYPE_BUFFER) { |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 231 | return ring_write(sink->ctx.ring, sink->maxlen, pfx, npfx, msg, nmsg); |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 232 | } |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 233 | return 0; |
| 234 | } |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 235 | |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 236 | /* Tries to emit a message indicating the number of dropped events. In case of |
| 237 | * success, the amount of drops is reduced by as much. It's supposed to be |
| 238 | * called under an exclusive lock on the sink to avoid multiple produces doing |
| 239 | * the same. On success, >0 is returned, otherwise <=0 on failure. |
| 240 | */ |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 241 | int sink_announce_dropped(struct sink *sink, int facility, struct ist *pid) |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 242 | { |
| 243 | unsigned int dropped; |
| 244 | struct buffer msg; |
| 245 | struct ist msgvec[1]; |
| 246 | char logbuf[64]; |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 247 | struct ist sd; |
| 248 | struct ist tag; |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 249 | |
| 250 | while (unlikely((dropped = sink->ctx.dropped) > 0)) { |
| 251 | chunk_init(&msg, logbuf, sizeof(logbuf)); |
| 252 | chunk_printf(&msg, "%u event%s dropped", dropped, dropped > 1 ? "s" : ""); |
| 253 | msgvec[0] = ist2(msg.area, msg.data); |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 254 | |
| 255 | sd.ptr = default_rfc5424_sd_log_format; |
| 256 | sd.len = 2; |
| 257 | tag.ptr = global.log_tag.area; |
| 258 | tag.len = global.log_tag.data; |
| 259 | if (__sink_write(sink, msgvec, 1, LOG_NOTICE, facility, &tag, pid, &sd) <= 0) |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 260 | return 0; |
| 261 | /* success! */ |
| 262 | HA_ATOMIC_SUB(&sink->ctx.dropped, dropped); |
| 263 | } |
| 264 | return 1; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 267 | /* parse the "show events" command, returns 1 if a message is returned, otherwise zero */ |
| 268 | static int cli_parse_show_events(char **args, char *payload, struct appctx *appctx, void *private) |
| 269 | { |
| 270 | struct sink *sink; |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 271 | int arg; |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 272 | |
| 273 | args++; // make args[1] the 1st arg |
| 274 | |
| 275 | if (!*args[1]) { |
| 276 | /* no arg => report the list of supported sink */ |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 277 | chunk_printf(&trash, "Supported events sinks are listed below. Add -w(wait), -n(new). Any key to stop\n"); |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 278 | list_for_each_entry(sink, &sink_list, sink_list) { |
| 279 | chunk_appendf(&trash, " %-10s : type=%s, %u dropped, %s\n", |
| 280 | sink->name, |
| 281 | sink->type == SINK_TYPE_NEW ? "init" : |
| 282 | sink->type == SINK_TYPE_FD ? "fd" : |
| 283 | sink->type == SINK_TYPE_BUFFER ? "buffer" : "?", |
| 284 | sink->ctx.dropped, sink->desc); |
| 285 | } |
| 286 | |
| 287 | trash.area[trash.data] = 0; |
| 288 | return cli_msg(appctx, LOG_WARNING, trash.area); |
| 289 | } |
| 290 | |
| 291 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 292 | return 1; |
| 293 | |
| 294 | sink = sink_find(args[1]); |
| 295 | if (!sink) |
| 296 | return cli_err(appctx, "No such event sink"); |
| 297 | |
| 298 | if (sink->type != SINK_TYPE_BUFFER) |
| 299 | return cli_msg(appctx, LOG_NOTICE, "Nothing to report for this sink"); |
| 300 | |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 301 | for (arg = 2; *args[arg]; arg++) { |
| 302 | if (strcmp(args[arg], "-w") == 0) |
| 303 | appctx->ctx.cli.i0 |= 1; // wait mode |
| 304 | else if (strcmp(args[arg], "-n") == 0) |
| 305 | appctx->ctx.cli.i0 |= 2; // seek to new |
| 306 | else if (strcmp(args[arg], "-nw") == 0 || strcmp(args[arg], "-wn") == 0) |
| 307 | appctx->ctx.cli.i0 |= 3; // seek to new + wait |
| 308 | else |
| 309 | return cli_err(appctx, "unknown option"); |
| 310 | } |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 311 | return ring_attach_cli(sink->ctx.ring, appctx); |
| 312 | } |
| 313 | |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 314 | static void sink_init() |
| 315 | { |
| 316 | sink_new_fd("stdout", "standard output (fd#1)", SINK_FMT_RAW, 1); |
| 317 | sink_new_fd("stderr", "standard output (fd#2)", SINK_FMT_RAW, 2); |
Willy Tarreau | f8340e3 | 2019-09-26 08:05:15 +0200 | [diff] [blame] | 318 | sink_new_buf("buf0", "in-memory ring buffer", SINK_FMT_TIMED, 1048576); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static void sink_deinit() |
| 322 | { |
| 323 | struct sink *sink, *sb; |
| 324 | |
| 325 | list_for_each_entry_safe(sink, sb, &sink_list, sink_list) { |
| 326 | if (sink->type == SINK_TYPE_BUFFER) |
| 327 | ring_free(sink->ctx.ring); |
| 328 | LIST_DEL(&sink->sink_list); |
| 329 | free(sink); |
| 330 | } |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | INITCALL0(STG_REGISTER, sink_init); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 334 | REGISTER_POST_DEINIT(sink_deinit); |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 335 | |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 336 | static struct cli_kw_list cli_kws = {{ },{ |
Willy Tarreau | fcf9498 | 2019-11-15 15:07:21 +0100 | [diff] [blame] | 337 | { { "show", "events", NULL }, "show events [<sink>] : show event sink state", cli_parse_show_events, NULL, NULL }, |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 338 | {{},} |
| 339 | }}; |
| 340 | |
| 341 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 342 | |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 343 | /* |
| 344 | * Local variables: |
| 345 | * c-indent-level: 8 |
| 346 | * c-basic-offset: 8 |
| 347 | * End: |
| 348 | */ |