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 | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 25 | #include <proto/cli.h> |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 26 | #include <proto/log.h> |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 27 | #include <proto/ring.h> |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 28 | #include <proto/sink.h> |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 29 | #include <proto/stream_interface.h> |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 30 | |
| 31 | struct list sink_list = LIST_HEAD_INIT(sink_list); |
| 32 | |
| 33 | struct sink *sink_find(const char *name) |
| 34 | { |
| 35 | struct sink *sink; |
| 36 | |
| 37 | list_for_each_entry(sink, &sink_list, sink_list) |
| 38 | if (strcmp(sink->name, name) == 0) |
| 39 | return sink; |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | /* creates a new sink and adds it to the list, it's still generic and not fully |
| 44 | * initialized. Returns NULL on allocation failure. If another one already |
| 45 | * exists with the same name, it will be returned. The caller can detect it as |
| 46 | * a newly created one has type SINK_TYPE_NEW. |
| 47 | */ |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 48 | 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] | 49 | { |
| 50 | struct sink *sink; |
| 51 | |
| 52 | sink = sink_find(name); |
| 53 | if (sink) |
| 54 | goto end; |
| 55 | |
| 56 | sink = malloc(sizeof(*sink)); |
| 57 | if (!sink) |
| 58 | goto end; |
| 59 | |
| 60 | sink->name = name; |
| 61 | sink->desc = desc; |
| 62 | sink->fmt = fmt; |
| 63 | sink->type = SINK_TYPE_NEW; |
| 64 | /* set defaults for syslog ones */ |
| 65 | sink->syslog_facility = 0; |
| 66 | sink->syslog_minlvl = 0; |
| 67 | sink->maxlen = MAX_SYSLOG_LEN; |
| 68 | /* address will be filled by the caller if needed */ |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 69 | sink->ctx.fd = -1; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 70 | sink->ctx.dropped = 0; |
| 71 | HA_RWLOCK_INIT(&sink->ctx.lock); |
| 72 | LIST_ADDQ(&sink_list, &sink->sink_list); |
| 73 | end: |
| 74 | return sink; |
| 75 | } |
| 76 | |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 77 | /* creates a sink called <name> of type FD associated to fd <fd>, format <fmt>, |
| 78 | * and description <desc>. Returns NULL on allocation failure or conflict. |
| 79 | * Perfect duplicates are merged (same type, fd, and name). |
| 80 | */ |
| 81 | struct sink *sink_new_fd(const char *name, const char *desc, enum sink_fmt fmt, int fd) |
| 82 | { |
| 83 | struct sink *sink; |
| 84 | |
| 85 | sink = __sink_new(name, desc, fmt); |
| 86 | if (!sink || (sink->type == SINK_TYPE_FD && sink->ctx.fd == fd)) |
| 87 | goto end; |
| 88 | |
| 89 | if (sink->type != SINK_TYPE_NEW) { |
| 90 | sink = NULL; |
| 91 | goto end; |
| 92 | } |
| 93 | |
| 94 | sink->type = SINK_TYPE_FD; |
| 95 | sink->ctx.fd = fd; |
| 96 | end: |
| 97 | return sink; |
| 98 | } |
| 99 | |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 100 | /* creates a sink called <name> of type BUF of size <size>, format <fmt>, |
| 101 | * and description <desc>. Returns NULL on allocation failure or conflict. |
| 102 | * Perfect duplicates are merged (same type and name). If sizes differ, the |
| 103 | * largest one is kept. |
| 104 | */ |
| 105 | struct sink *sink_new_buf(const char *name, const char *desc, enum sink_fmt fmt, size_t size) |
| 106 | { |
| 107 | struct sink *sink; |
| 108 | |
| 109 | sink = __sink_new(name, desc, fmt); |
| 110 | if (!sink) |
| 111 | goto fail; |
| 112 | |
| 113 | if (sink->type == SINK_TYPE_BUFFER) { |
| 114 | /* such a buffer already exists, we may have to resize it */ |
| 115 | if (!ring_resize(sink->ctx.ring, size)) |
| 116 | goto fail; |
| 117 | goto end; |
| 118 | } |
| 119 | |
| 120 | if (sink->type != SINK_TYPE_NEW) { |
| 121 | /* already exists of another type */ |
| 122 | goto fail; |
| 123 | } |
| 124 | |
| 125 | sink->ctx.ring = ring_new(size); |
| 126 | if (!sink->ctx.ring) { |
| 127 | LIST_DEL(&sink->sink_list); |
| 128 | free(sink); |
| 129 | goto fail; |
| 130 | } |
| 131 | |
| 132 | sink->type = SINK_TYPE_BUFFER; |
| 133 | end: |
| 134 | return sink; |
| 135 | fail: |
| 136 | return NULL; |
| 137 | } |
| 138 | |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 139 | /* tries to send <nmsg> message parts (up to 8, ignored above) from message |
| 140 | * 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] | 141 | * done here. Lost messages are NOT accounted for. It is preferable to call |
| 142 | * sink_write() instead which will also try to emit the number of dropped |
| 143 | * messages when there are any. It returns >0 if it could write anything, |
| 144 | * <=0 otherwise. |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 145 | */ |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 146 | ssize_t __sink_write(struct sink *sink, const struct ist msg[], size_t nmsg) |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 147 | { |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 148 | char short_hdr[4]; |
Willy Tarreau | a1426de | 2019-08-27 14:21:02 +0200 | [diff] [blame] | 149 | struct ist pfx[4]; |
| 150 | size_t npfx = 0; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 151 | |
| 152 | if (sink->fmt == SINK_FMT_SHORT) { |
| 153 | short_hdr[0] = '<'; |
| 154 | short_hdr[1] = '0' + sink->syslog_minlvl; |
| 155 | short_hdr[2] = '>'; |
| 156 | |
Willy Tarreau | a1426de | 2019-08-27 14:21:02 +0200 | [diff] [blame] | 157 | pfx[npfx].ptr = short_hdr; |
| 158 | pfx[npfx].len = 3; |
| 159 | npfx++; |
| 160 | } |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 161 | |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 162 | if (sink->type == SINK_TYPE_FD) { |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 163 | 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] | 164 | } |
| 165 | else if (sink->type == SINK_TYPE_BUFFER) { |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 166 | return ring_write(sink->ctx.ring, sink->maxlen, pfx, npfx, msg, nmsg); |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 167 | } |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 168 | return 0; |
| 169 | } |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 170 | |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 171 | /* Tries to emit a message indicating the number of dropped events. In case of |
| 172 | * success, the amount of drops is reduced by as much. It's supposed to be |
| 173 | * called under an exclusive lock on the sink to avoid multiple produces doing |
| 174 | * the same. On success, >0 is returned, otherwise <=0 on failure. |
| 175 | */ |
| 176 | int sink_announce_dropped(struct sink *sink) |
| 177 | { |
| 178 | unsigned int dropped; |
| 179 | struct buffer msg; |
| 180 | struct ist msgvec[1]; |
| 181 | char logbuf[64]; |
| 182 | |
| 183 | while (unlikely((dropped = sink->ctx.dropped) > 0)) { |
| 184 | chunk_init(&msg, logbuf, sizeof(logbuf)); |
| 185 | chunk_printf(&msg, "%u event%s dropped", dropped, dropped > 1 ? "s" : ""); |
| 186 | msgvec[0] = ist2(msg.area, msg.data); |
| 187 | if (__sink_write(sink, msgvec, 1) <= 0) |
| 188 | return 0; |
| 189 | /* success! */ |
| 190 | HA_ATOMIC_SUB(&sink->ctx.dropped, dropped); |
| 191 | } |
| 192 | return 1; |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 193 | } |
| 194 | |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 195 | /* parse the "show events" command, returns 1 if a message is returned, otherwise zero */ |
| 196 | static int cli_parse_show_events(char **args, char *payload, struct appctx *appctx, void *private) |
| 197 | { |
| 198 | struct sink *sink; |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 199 | int arg; |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 200 | |
| 201 | args++; // make args[1] the 1st arg |
| 202 | |
| 203 | if (!*args[1]) { |
| 204 | /* no arg => report the list of supported sink */ |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 205 | 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] | 206 | list_for_each_entry(sink, &sink_list, sink_list) { |
| 207 | chunk_appendf(&trash, " %-10s : type=%s, %u dropped, %s\n", |
| 208 | sink->name, |
| 209 | sink->type == SINK_TYPE_NEW ? "init" : |
| 210 | sink->type == SINK_TYPE_FD ? "fd" : |
| 211 | sink->type == SINK_TYPE_BUFFER ? "buffer" : "?", |
| 212 | sink->ctx.dropped, sink->desc); |
| 213 | } |
| 214 | |
| 215 | trash.area[trash.data] = 0; |
| 216 | return cli_msg(appctx, LOG_WARNING, trash.area); |
| 217 | } |
| 218 | |
| 219 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 220 | return 1; |
| 221 | |
| 222 | sink = sink_find(args[1]); |
| 223 | if (!sink) |
| 224 | return cli_err(appctx, "No such event sink"); |
| 225 | |
| 226 | if (sink->type != SINK_TYPE_BUFFER) |
| 227 | return cli_msg(appctx, LOG_NOTICE, "Nothing to report for this sink"); |
| 228 | |
Willy Tarreau | 1d181e4 | 2019-08-30 11:17:01 +0200 | [diff] [blame] | 229 | for (arg = 2; *args[arg]; arg++) { |
| 230 | if (strcmp(args[arg], "-w") == 0) |
| 231 | appctx->ctx.cli.i0 |= 1; // wait mode |
| 232 | else if (strcmp(args[arg], "-n") == 0) |
| 233 | appctx->ctx.cli.i0 |= 2; // seek to new |
| 234 | else if (strcmp(args[arg], "-nw") == 0 || strcmp(args[arg], "-wn") == 0) |
| 235 | appctx->ctx.cli.i0 |= 3; // seek to new + wait |
| 236 | else |
| 237 | return cli_err(appctx, "unknown option"); |
| 238 | } |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 239 | return ring_attach_cli(sink->ctx.ring, appctx); |
| 240 | } |
| 241 | |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 242 | static void sink_init() |
| 243 | { |
| 244 | sink_new_fd("stdout", "standard output (fd#1)", SINK_FMT_RAW, 1); |
| 245 | sink_new_fd("stderr", "standard output (fd#2)", SINK_FMT_RAW, 2); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 246 | sink_new_buf("buf0", "in-memory ring buffer", SINK_FMT_RAW, 1048576); |
| 247 | } |
| 248 | |
| 249 | static void sink_deinit() |
| 250 | { |
| 251 | struct sink *sink, *sb; |
| 252 | |
| 253 | list_for_each_entry_safe(sink, sb, &sink_list, sink_list) { |
| 254 | if (sink->type == SINK_TYPE_BUFFER) |
| 255 | ring_free(sink->ctx.ring); |
| 256 | LIST_DEL(&sink->sink_list); |
| 257 | free(sink); |
| 258 | } |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | INITCALL0(STG_REGISTER, sink_init); |
Willy Tarreau | 4ed23ca | 2019-08-23 15:47:49 +0200 | [diff] [blame] | 262 | REGISTER_POST_DEINIT(sink_deinit); |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 263 | |
Willy Tarreau | 9f830d7 | 2019-08-26 18:17:04 +0200 | [diff] [blame] | 264 | static struct cli_kw_list cli_kws = {{ },{ |
| 265 | { { "show", "events", NULL }, "show events [<sink>] : show event sink state", cli_parse_show_events, cli_io_handler_show_ring, cli_io_release_show_ring }, |
| 266 | {{},} |
| 267 | }}; |
| 268 | |
| 269 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 270 | |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 271 | /* |
| 272 | * Local variables: |
| 273 | * c-indent-level: 8 |
| 274 | * c-basic-offset: 8 |
| 275 | * End: |
| 276 | */ |