blob: efceb72d194502fe0b7688ced2049d9438e4f77a [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include <common/cfgparse.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
34#include <common/ticks.h>
35#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38#include <common/base64.h>
39
40#include <types/applet.h>
William Lallemand9ed62032016-11-21 17:49:11 +010041#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010042#include <types/global.h>
43#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010044#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045
46#include <proto/backend.h>
47#include <proto/channel.h>
48#include <proto/checks.h>
49#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010050#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010051#include <proto/fd.h>
52#include <proto/freq_ctr.h>
53#include <proto/frontend.h>
54#include <proto/log.h>
55#include <proto/pattern.h>
56#include <proto/pipe.h>
57#include <proto/listener.h>
58#include <proto/map.h>
59#include <proto/proto_http.h>
60#include <proto/proto_uxst.h>
61#include <proto/proxy.h>
62#include <proto/sample.h>
63#include <proto/session.h>
64#include <proto/stream.h>
65#include <proto/server.h>
66#include <proto/raw_sock.h>
67#include <proto/stream_interface.h>
68#include <proto/task.h>
69
70#ifdef USE_OPENSSL
71#include <proto/ssl_sock.h>
72#include <types/ssl_sock.h>
73#endif
74
75/* These are the field names for each INF_* field position. Please pay attention
76 * to always use the exact same name except that the strings for new names must
77 * be lower case or CamelCase while the enum entries must be upper case.
78 */
79const char *info_field_names[INF_TOTAL_FIELDS] = {
80 [INF_NAME] = "Name",
81 [INF_VERSION] = "Version",
82 [INF_RELEASE_DATE] = "Release_date",
83 [INF_NBPROC] = "Nbproc",
84 [INF_PROCESS_NUM] = "Process_num",
85 [INF_PID] = "Pid",
86 [INF_UPTIME] = "Uptime",
87 [INF_UPTIME_SEC] = "Uptime_sec",
88 [INF_MEMMAX_MB] = "Memmax_MB",
89 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
90 [INF_POOL_USED_MB] = "PoolUsed_MB",
91 [INF_POOL_FAILED] = "PoolFailed",
92 [INF_ULIMIT_N] = "Ulimit-n",
93 [INF_MAXSOCK] = "Maxsock",
94 [INF_MAXCONN] = "Maxconn",
95 [INF_HARD_MAXCONN] = "Hard_maxconn",
96 [INF_CURR_CONN] = "CurrConns",
97 [INF_CUM_CONN] = "CumConns",
98 [INF_CUM_REQ] = "CumReq",
99 [INF_MAX_SSL_CONNS] = "MaxSslConns",
100 [INF_CURR_SSL_CONNS] = "CurrSslConns",
101 [INF_CUM_SSL_CONNS] = "CumSslConns",
102 [INF_MAXPIPES] = "Maxpipes",
103 [INF_PIPES_USED] = "PipesUsed",
104 [INF_PIPES_FREE] = "PipesFree",
105 [INF_CONN_RATE] = "ConnRate",
106 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
107 [INF_MAX_CONN_RATE] = "MaxConnRate",
108 [INF_SESS_RATE] = "SessRate",
109 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
110 [INF_MAX_SESS_RATE] = "MaxSessRate",
111 [INF_SSL_RATE] = "SslRate",
112 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
113 [INF_MAX_SSL_RATE] = "MaxSslRate",
114 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
115 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
116 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
117 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
118 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
119 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
120 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
121 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
122 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
123 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
124 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
125 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
126 [INF_TASKS] = "Tasks",
127 [INF_RUN_QUEUE] = "Run_queue",
128 [INF_IDLE_PCT] = "Idle_pct",
129 [INF_NODE] = "node",
130 [INF_DESCRIPTION] = "description",
131};
132
133/* one line of stats */
134static struct field info[INF_TOTAL_FIELDS];
135
William Lallemand74c24fb2016-11-21 17:18:36 +0100136static int stats_dump_env_to_buffer(struct stream_interface *si);
137static int stats_dump_info_to_buffer(struct stream_interface *si);
William Lallemand74c24fb2016-11-21 17:18:36 +0100138static int stats_dump_errors_to_buffer(struct stream_interface *si);
139static int stats_table_request(struct stream_interface *si, int show);
William Lallemand74c24fb2016-11-21 17:18:36 +0100140
William Lallemand74c24fb2016-11-21 17:18:36 +0100141
142static struct applet cli_applet;
143
144static const char stats_sock_usage_msg[] =
145 "Unknown command. Please enter one of the following commands only :\n"
146 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
147 " clear table : remove an entry from a table\n"
148 " help : this message\n"
149 " prompt : toggle interactive mode with prompt\n"
150 " quit : disconnect\n"
William Lallemand74c24fb2016-11-21 17:18:36 +0100151 " show env [var] : dump environment variables known to the process\n"
152 " show info : report information about the running process\n"
William Lallemand74c24fb2016-11-21 17:18:36 +0100153 " show stat : report counters for each proxy and server\n"
William Lallemand74c24fb2016-11-21 17:18:36 +0100154 " show errors : report last request and response errors for each proxy\n"
William Lallemand74c24fb2016-11-21 17:18:36 +0100155 " show table [id]: report table usage stats or dump this table's contents\n"
William Lallemand74c24fb2016-11-21 17:18:36 +0100156 " get weight : report a server's current weight\n"
157 " set weight : change a server's weight\n"
William Lallemand74c24fb2016-11-21 17:18:36 +0100158 " set table [id] : update or create a table entry's data\n"
159 " set timeout : change a timeout setting\n"
160 " set maxconn : change a maxconn setting\n"
161 " set rate-limit : change a rate limiting value\n"
162 " disable : put a server or frontend in maintenance mode\n"
163 " enable : re-enable a server or frontend which is in maintenance mode\n"
164 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
William Lallemand74c24fb2016-11-21 17:18:36 +0100165 "";
166
167static const char stats_permission_denied_msg[] =
168 "Permission denied\n"
169 "";
170
171
172static char *dynamic_usage_msg = NULL;
173
174/* List head of cli keywords */
175static struct cli_kw_list cli_keywords = {
176 .list = LIST_HEAD_INIT(cli_keywords.list)
177};
178
179extern const char *stat_status_codes[];
180
181char *cli_gen_usage_msg()
182{
183 struct cli_kw_list *kw_list;
184 struct cli_kw *kw;
185 struct chunk *tmp = get_trash_chunk();
186 struct chunk out;
187
188 free(dynamic_usage_msg);
189 dynamic_usage_msg = NULL;
190
191 if (LIST_ISEMPTY(&cli_keywords.list))
192 return NULL;
193
194 chunk_reset(tmp);
195 chunk_strcat(tmp, stats_sock_usage_msg);
196 list_for_each_entry(kw_list, &cli_keywords.list, list) {
197 kw = &kw_list->kw[0];
198 while (kw->usage) {
199 chunk_appendf(tmp, " %s\n", kw->usage);
200 kw++;
201 }
202 }
203 chunk_init(&out, NULL, 0);
204 chunk_dup(&out, tmp);
205 dynamic_usage_msg = out.str;
206 return dynamic_usage_msg;
207}
208
209struct cli_kw* cli_find_kw(char **args)
210{
211 struct cli_kw_list *kw_list;
212 struct cli_kw *kw;/* current cli_kw */
213 char **tmp_args;
214 const char **tmp_str_kw;
215 int found = 0;
216
217 if (LIST_ISEMPTY(&cli_keywords.list))
218 return NULL;
219
220 list_for_each_entry(kw_list, &cli_keywords.list, list) {
221 kw = &kw_list->kw[0];
222 while (*kw->str_kw) {
223 tmp_args = args;
224 tmp_str_kw = kw->str_kw;
225 while (*tmp_str_kw) {
226 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
227 found = 1;
228 } else {
229 found = 0;
230 break;
231 }
232 tmp_args++;
233 tmp_str_kw++;
234 }
235 if (found)
236 return (kw);
237 kw++;
238 }
239 }
240 return NULL;
241}
242
243void cli_register_kw(struct cli_kw_list *kw_list)
244{
245 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
246}
247
248
249/* allocate a new stats frontend named <name>, and return it
250 * (or NULL in case of lack of memory).
251 */
252static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
253{
254 struct proxy *fe;
255
256 fe = calloc(1, sizeof(*fe));
257 if (!fe)
258 return NULL;
259
260 init_new_proxy(fe);
261 fe->next = proxy;
262 proxy = fe;
263 fe->last_change = now.tv_sec;
264 fe->id = strdup("GLOBAL");
265 fe->cap = PR_CAP_FE;
266 fe->maxconn = 10; /* default to 10 concurrent connections */
267 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
268 fe->conf.file = strdup(file);
269 fe->conf.line = line;
270 fe->accept = frontend_accept;
271 fe->default_target = &cli_applet.obj_type;
272
273 /* the stats frontend is the only one able to assign ID #0 */
274 fe->conf.id.key = fe->uuid = 0;
275 eb32_insert(&used_proxy_id, &fe->conf.id);
276 return fe;
277}
278
279/* This function parses a "stats" statement in the "global" section. It returns
280 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
281 * error message into the <err> buffer which will be preallocated. The trailing
282 * '\n' must not be written. The function must be called with <args> pointing to
283 * the first word after "stats".
284 */
285static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
286 struct proxy *defpx, const char *file, int line,
287 char **err)
288{
289 struct bind_conf *bind_conf;
290 struct listener *l;
291
292 if (!strcmp(args[1], "socket")) {
293 int cur_arg;
294
295 if (*args[2] == 0) {
296 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
297 return -1;
298 }
299
300 if (!global.stats_fe) {
301 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
302 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
303 return -1;
304 }
305 }
306
307 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
308 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
309
310 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
311 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
312 file, line, args[0], args[1], err && *err ? *err : "error");
313 return -1;
314 }
315
316 cur_arg = 3;
317 while (*args[cur_arg]) {
318 static int bind_dumped;
319 struct bind_kw *kw;
320
321 kw = bind_find_kw(args[cur_arg]);
322 if (kw) {
323 if (!kw->parse) {
324 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
325 args[0], args[1], args[cur_arg]);
326 return -1;
327 }
328
329 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
330 if (err && *err)
331 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
332 else
333 memprintf(err, "'%s %s' : error encountered while processing '%s'",
334 args[0], args[1], args[cur_arg]);
335 return -1;
336 }
337
338 cur_arg += 1 + kw->skip;
339 continue;
340 }
341
342 if (!bind_dumped) {
343 bind_dump_kws(err);
344 indent_msg(err, 4);
345 bind_dumped = 1;
346 }
347
348 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
349 args[0], args[1], args[cur_arg],
350 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
351 return -1;
352 }
353
354 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
355 l->maxconn = global.stats_fe->maxconn;
356 l->backlog = global.stats_fe->backlog;
357 l->accept = session_accept_fd;
358 l->handler = process_stream;
359 l->default_target = global.stats_fe->default_target;
360 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
361 l->nice = -64; /* we want to boost priority for local stats */
362 global.maxsock += l->maxconn;
363 }
364 }
365 else if (!strcmp(args[1], "timeout")) {
366 unsigned timeout;
367 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
368
369 if (res) {
370 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
371 return -1;
372 }
373
374 if (!timeout) {
375 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
376 return -1;
377 }
378 if (!global.stats_fe) {
379 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
380 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
381 return -1;
382 }
383 }
384 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
385 }
386 else if (!strcmp(args[1], "maxconn")) {
387 int maxconn = atol(args[2]);
388
389 if (maxconn <= 0) {
390 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
391 return -1;
392 }
393
394 if (!global.stats_fe) {
395 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
396 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
397 return -1;
398 }
399 }
400 global.stats_fe->maxconn = maxconn;
401 }
402 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
403 int cur_arg = 2;
404 unsigned long set = 0;
405
406 if (!global.stats_fe) {
407 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
408 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
409 return -1;
410 }
411 }
412
413 while (*args[cur_arg]) {
414 unsigned int low, high;
415
416 if (strcmp(args[cur_arg], "all") == 0) {
417 set = 0;
418 break;
419 }
420 else if (strcmp(args[cur_arg], "odd") == 0) {
421 set |= ~0UL/3UL; /* 0x555....555 */
422 }
423 else if (strcmp(args[cur_arg], "even") == 0) {
424 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
425 }
426 else if (isdigit((int)*args[cur_arg])) {
427 char *dash = strchr(args[cur_arg], '-');
428
429 low = high = str2uic(args[cur_arg]);
430 if (dash)
431 high = str2uic(dash + 1);
432
433 if (high < low) {
434 unsigned int swap = low;
435 low = high;
436 high = swap;
437 }
438
439 if (low < 1 || high > LONGBITS) {
440 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
441 args[0], args[1], LONGBITS);
442 return -1;
443 }
444 while (low <= high)
445 set |= 1UL << (low++ - 1);
446 }
447 else {
448 memprintf(err,
449 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
450 args[0], args[1], LONGBITS);
451 return -1;
452 }
453 cur_arg++;
454 }
455 global.stats_fe->bind_proc = set;
456 }
457 else {
458 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
459 return -1;
460 }
461 return 0;
462}
463
Willy Tarreaude57a572016-11-23 17:01:39 +0100464/* Verifies that the CLI at least has a level at least as high as <level>
465 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
466 * failure, an error message is prepared and the appctx's state is adjusted
467 * to print it so that a return 1 is enough to abort any processing.
468 */
469int cli_has_level(struct appctx *appctx, int level)
470{
471 struct stream_interface *si = appctx->owner;
472 struct stream *s = si_strm(si);
473
474 if (strm_li(s)->bind_conf->level < level) {
475 appctx->ctx.cli.msg = stats_permission_denied_msg;
476 appctx->st0 = STAT_CLI_PRINT;
477 return 0;
478 }
479 return 1;
480}
481
William Lallemand74c24fb2016-11-21 17:18:36 +0100482
483/* print a string of text buffer to <out>. The format is :
484 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
485 * Other non-printable chars are encoded "\xHH". Space, '\', and '=' are also escaped.
486 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
487 */
488static int dump_text(struct chunk *out, const char *buf, int bsize)
489{
490 unsigned char c;
491 int ptr = 0;
492
493 while (buf[ptr] && ptr < bsize) {
494 c = buf[ptr];
495 if (isprint(c) && isascii(c) && c != '\\' && c != ' ' && c != '=') {
496 if (out->len > out->size - 1)
497 break;
498 out->str[out->len++] = c;
499 }
500 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ' || c == '=') {
501 if (out->len > out->size - 2)
502 break;
503 out->str[out->len++] = '\\';
504 switch (c) {
505 case ' ': c = ' '; break;
506 case '\t': c = 't'; break;
507 case '\n': c = 'n'; break;
508 case '\r': c = 'r'; break;
509 case '\e': c = 'e'; break;
510 case '\\': c = '\\'; break;
511 case '=': c = '='; break;
512 }
513 out->str[out->len++] = c;
514 }
515 else {
516 if (out->len > out->size - 4)
517 break;
518 out->str[out->len++] = '\\';
519 out->str[out->len++] = 'x';
520 out->str[out->len++] = hextab[(c >> 4) & 0xF];
521 out->str[out->len++] = hextab[c & 0xF];
522 }
523 ptr++;
524 }
525
526 return ptr;
527}
528
529/* print a buffer in hexa.
530 * Print stopped if <bsize> is reached, or if no more place in the chunk.
531 */
532static int dump_binary(struct chunk *out, const char *buf, int bsize)
533{
534 unsigned char c;
535 int ptr = 0;
536
537 while (ptr < bsize) {
538 c = buf[ptr];
539
540 if (out->len > out->size - 2)
541 break;
542 out->str[out->len++] = hextab[(c >> 4) & 0xF];
543 out->str[out->len++] = hextab[c & 0xF];
544
545 ptr++;
546 }
547 return ptr;
548}
549
550/* Dump the status of a table to a stream interface's
551 * read buffer. It returns 0 if the output buffer is full
552 * and needs to be called again, otherwise non-zero.
553 */
554static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
555 struct proxy *proxy, struct proxy *target)
556{
557 struct stream *s = si_strm(si);
558
559 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
560 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
561
562 /* any other information should be dumped here */
563
564 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
565 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
566
567 if (bi_putchk(si_ic(si), msg) == -1) {
568 si_applet_cant_put(si);
569 return 0;
570 }
571
572 return 1;
573}
574
575/* Dump the a table entry to a stream interface's
576 * read buffer. It returns 0 if the output buffer is full
577 * and needs to be called again, otherwise non-zero.
578 */
579static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
580 struct proxy *proxy, struct stksess *entry)
581{
582 int dt;
583
584 chunk_appendf(msg, "%p:", entry);
585
586 if (proxy->table.type == SMP_T_IPV4) {
587 char addr[INET_ADDRSTRLEN];
588 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
589 chunk_appendf(msg, " key=%s", addr);
590 }
591 else if (proxy->table.type == SMP_T_IPV6) {
592 char addr[INET6_ADDRSTRLEN];
593 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
594 chunk_appendf(msg, " key=%s", addr);
595 }
596 else if (proxy->table.type == SMP_T_SINT) {
597 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
598 }
599 else if (proxy->table.type == SMP_T_STR) {
600 chunk_appendf(msg, " key=");
601 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
602 }
603 else {
604 chunk_appendf(msg, " key=");
605 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
606 }
607
608 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
609
610 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
611 void *ptr;
612
613 if (proxy->table.data_ofs[dt] == 0)
614 continue;
615 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
616 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
617 else
618 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
619
620 ptr = stktable_data_ptr(&proxy->table, entry, dt);
621 switch (stktable_data_types[dt].std_type) {
622 case STD_T_SINT:
623 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
624 break;
625 case STD_T_UINT:
626 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
627 break;
628 case STD_T_ULL:
629 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
630 break;
631 case STD_T_FRQP:
632 chunk_appendf(msg, "%d",
633 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
634 proxy->table.data_arg[dt].u));
635 break;
636 }
637 }
638 chunk_appendf(msg, "\n");
639
640 if (bi_putchk(si_ic(si), msg) == -1) {
641 si_applet_cant_put(si);
642 return 0;
643 }
644
645 return 1;
646}
647
648static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
649{
650 struct stream *s = si_strm(si);
651 struct appctx *appctx = __objt_appctx(si->end);
652 struct proxy *px = appctx->ctx.table.target;
653 struct stksess *ts;
654 uint32_t uint32_key;
655 unsigned char ip6_key[sizeof(struct in6_addr)];
656 long long value;
657 int data_type;
658 int cur_arg;
659 void *ptr;
660 struct freq_ctr_period *frqp;
661
662 appctx->st0 = STAT_CLI_OUTPUT;
663
664 if (!*args[4]) {
665 appctx->ctx.cli.msg = "Key value expected\n";
666 appctx->st0 = STAT_CLI_PRINT;
667 return;
668 }
669
670 switch (px->table.type) {
671 case SMP_T_IPV4:
672 uint32_key = htonl(inetaddr_host(args[4]));
673 static_table_key->key = &uint32_key;
674 break;
675 case SMP_T_IPV6:
676 inet_pton(AF_INET6, args[4], ip6_key);
677 static_table_key->key = &ip6_key;
678 break;
679 case SMP_T_SINT:
680 {
681 char *endptr;
682 unsigned long val;
683 errno = 0;
684 val = strtoul(args[4], &endptr, 10);
685 if ((errno == ERANGE && val == ULONG_MAX) ||
686 (errno != 0 && val == 0) || endptr == args[4] ||
687 val > 0xffffffff) {
688 appctx->ctx.cli.msg = "Invalid key\n";
689 appctx->st0 = STAT_CLI_PRINT;
690 return;
691 }
692 uint32_key = (uint32_t) val;
693 static_table_key->key = &uint32_key;
694 break;
695 }
696 break;
697 case SMP_T_STR:
698 static_table_key->key = args[4];
699 static_table_key->key_len = strlen(args[4]);
700 break;
701 default:
702 switch (action) {
703 case STAT_CLI_O_TAB:
704 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
705 break;
706 case STAT_CLI_O_CLR:
707 appctx->ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
708 break;
709 default:
710 appctx->ctx.cli.msg = "Unknown action\n";
711 break;
712 }
713 appctx->st0 = STAT_CLI_PRINT;
714 return;
715 }
716
717 /* check permissions */
718 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
719 appctx->ctx.cli.msg = stats_permission_denied_msg;
720 appctx->st0 = STAT_CLI_PRINT;
721 return;
722 }
723
724 ts = stktable_lookup_key(&px->table, static_table_key);
725
726 switch (action) {
727 case STAT_CLI_O_TAB:
728 if (!ts)
729 return;
730 chunk_reset(&trash);
731 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
732 return;
733 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
734 return;
735
736 case STAT_CLI_O_CLR:
737 if (!ts)
738 return;
739 if (ts->ref_cnt) {
740 /* don't delete an entry which is currently referenced */
741 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
742 appctx->st0 = STAT_CLI_PRINT;
743 return;
744 }
745 stksess_kill(&px->table, ts);
746 break;
747
748 case STAT_CLI_O_SET:
749 if (ts)
750 stktable_touch(&px->table, ts, 1);
751 else {
752 ts = stksess_new(&px->table, static_table_key);
753 if (!ts) {
754 /* don't delete an entry which is currently referenced */
755 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
756 appctx->st0 = STAT_CLI_PRINT;
757 return;
758 }
759 stktable_store(&px->table, ts, 1);
760 }
761
762 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
763 if (strncmp(args[cur_arg], "data.", 5) != 0) {
764 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
765 appctx->st0 = STAT_CLI_PRINT;
766 return;
767 }
768
769 data_type = stktable_get_data_type(args[cur_arg] + 5);
770 if (data_type < 0) {
771 appctx->ctx.cli.msg = "Unknown data type\n";
772 appctx->st0 = STAT_CLI_PRINT;
773 return;
774 }
775
776 if (!px->table.data_ofs[data_type]) {
777 appctx->ctx.cli.msg = "Data type not stored in this table\n";
778 appctx->st0 = STAT_CLI_PRINT;
779 return;
780 }
781
782 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
783 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
784 appctx->st0 = STAT_CLI_PRINT;
785 return;
786 }
787
788 ptr = stktable_data_ptr(&px->table, ts, data_type);
789
790 switch (stktable_data_types[data_type].std_type) {
791 case STD_T_SINT:
792 stktable_data_cast(ptr, std_t_sint) = value;
793 break;
794 case STD_T_UINT:
795 stktable_data_cast(ptr, std_t_uint) = value;
796 break;
797 case STD_T_ULL:
798 stktable_data_cast(ptr, std_t_ull) = value;
799 break;
800 case STD_T_FRQP:
801 /* We set both the current and previous values. That way
802 * the reported frequency is stable during all the period
803 * then slowly fades out. This allows external tools to
804 * push measures without having to update them too often.
805 */
806 frqp = &stktable_data_cast(ptr, std_t_frqp);
807 frqp->curr_tick = now_ms;
808 frqp->prev_ctr = 0;
809 frqp->curr_ctr = value;
810 break;
811 }
812 }
813 break;
814
815 default:
816 appctx->ctx.cli.msg = "Unknown action\n";
817 appctx->st0 = STAT_CLI_PRINT;
818 break;
819 }
820}
821
822static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
823{
824 struct appctx *appctx = __objt_appctx(si->end);
825
826 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
827 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
828 appctx->st0 = STAT_CLI_PRINT;
829 return;
830 }
831
832 /* condition on stored data value */
833 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
834 if (appctx->ctx.table.data_type < 0) {
835 appctx->ctx.cli.msg = "Unknown data type\n";
836 appctx->st0 = STAT_CLI_PRINT;
837 return;
838 }
839
840 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
841 appctx->ctx.cli.msg = "Data type not stored in this table\n";
842 appctx->st0 = STAT_CLI_PRINT;
843 return;
844 }
845
846 appctx->ctx.table.data_op = get_std_op(args[4]);
847 if (appctx->ctx.table.data_op < 0) {
848 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
849 appctx->st0 = STAT_CLI_PRINT;
850 return;
851 }
852
853 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
854 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
855 appctx->st0 = STAT_CLI_PRINT;
856 return;
857 }
858}
859
860static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
861{
862 struct appctx *appctx = __objt_appctx(si->end);
863
864 appctx->ctx.table.data_type = -1;
865 appctx->st2 = STAT_ST_INIT;
866 appctx->ctx.table.target = NULL;
867 appctx->ctx.table.proxy = NULL;
868 appctx->ctx.table.entry = NULL;
869 appctx->st0 = action;
870
871 if (*args[2]) {
872 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
873 if (!appctx->ctx.table.target) {
874 appctx->ctx.cli.msg = "No such table\n";
875 appctx->st0 = STAT_CLI_PRINT;
876 return;
877 }
878 }
879 else {
880 if (action != STAT_CLI_O_TAB)
881 goto err_args;
882 return;
883 }
884
885 if (strcmp(args[3], "key") == 0)
886 stats_sock_table_key_request(si, args, action);
887 else if (strncmp(args[3], "data.", 5) == 0)
888 stats_sock_table_data_request(si, args, action);
889 else if (*args[3])
890 goto err_args;
891
892 return;
893
894err_args:
895 switch (action) {
896 case STAT_CLI_O_TAB:
897 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
898 break;
899 case STAT_CLI_O_CLR:
900 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
901 break;
902 default:
903 appctx->ctx.cli.msg = "Unknown action\n";
904 break;
905 }
906 appctx->st0 = STAT_CLI_PRINT;
907}
908
909/* Expects to find a frontend named <arg> and returns it, otherwise displays various
910 * adequate error messages and returns NULL. This function also expects the stream
911 * level to be admin.
912 */
913static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
914{
915 struct appctx *appctx = __objt_appctx(si->end);
916 struct proxy *px;
917
918 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
919 appctx->ctx.cli.msg = stats_permission_denied_msg;
920 appctx->st0 = STAT_CLI_PRINT;
921 return NULL;
922 }
923
924 if (!*arg) {
925 appctx->ctx.cli.msg = "A frontend name is expected.\n";
926 appctx->st0 = STAT_CLI_PRINT;
927 return NULL;
928 }
929
930 px = proxy_fe_by_name(arg);
931 if (!px) {
932 appctx->ctx.cli.msg = "No such frontend.\n";
933 appctx->st0 = STAT_CLI_PRINT;
934 return NULL;
935 }
936 return px;
937}
938
939/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
940 * and returns the pointer to the server. Otherwise, display adequate error messages
941 * and returns NULL. This function also expects the stream level to be admin. Note:
942 * the <arg> is modified to remove the '/'.
943 */
William Lallemand222baf22016-11-19 02:00:33 +0100944struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
William Lallemand74c24fb2016-11-21 17:18:36 +0100945{
946 struct appctx *appctx = __objt_appctx(si->end);
947 struct proxy *px;
948 struct server *sv;
949 char *line;
950
951 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
952 appctx->ctx.cli.msg = stats_permission_denied_msg;
953 appctx->st0 = STAT_CLI_PRINT;
954 return NULL;
955 }
956
957 /* split "backend/server" and make <line> point to server */
958 for (line = arg; *line; line++)
959 if (*line == '/') {
960 *line++ = '\0';
961 break;
962 }
963
964 if (!*line || !*arg) {
965 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
966 appctx->st0 = STAT_CLI_PRINT;
967 return NULL;
968 }
969
970 if (!get_backend_server(arg, line, &px, &sv)) {
971 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
972 appctx->st0 = STAT_CLI_PRINT;
973 return NULL;
974 }
975
976 if (px->state == PR_STSTOPPED) {
977 appctx->ctx.cli.msg = "Proxy is disabled.\n";
978 appctx->st0 = STAT_CLI_PRINT;
979 return NULL;
980 }
981
982 return sv;
983}
984
William Lallemand74c24fb2016-11-21 17:18:36 +0100985/* Processes the stats interpreter on the statistics socket. This function is
986 * called from an applet running in a stream interface. The function returns 1
987 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
988 * designating the function which will have to process the request, which can
989 * also be the print function to display the return message set into cli.msg.
990 */
991static int stats_sock_parse_request(struct stream_interface *si, char *line)
992{
993 struct stream *s = si_strm(si);
994 struct appctx *appctx = __objt_appctx(si->end);
995 char *args[MAX_STATS_ARGS + 1];
996 struct cli_kw *kw;
997 int arg;
998 int i, j;
999
1000 while (isspace((unsigned char)*line))
1001 line++;
1002
1003 arg = 0;
1004 args[arg] = line;
1005
1006 while (*line && arg < MAX_STATS_ARGS) {
1007 if (*line == '\\') {
1008 line++;
1009 if (*line == '\0')
1010 break;
1011 }
1012 else if (isspace((unsigned char)*line)) {
1013 *line++ = '\0';
1014
1015 while (isspace((unsigned char)*line))
1016 line++;
1017
1018 args[++arg] = line;
1019 continue;
1020 }
1021
1022 line++;
1023 }
1024
1025 while (++arg <= MAX_STATS_ARGS)
1026 args[arg] = line;
1027
1028 /* remove \ */
1029 arg = 0;
1030 while (*args[arg] != '\0') {
1031 j = 0;
1032 for (i=0; args[arg][i] != '\0'; i++) {
1033 if (args[arg][i] == '\\')
1034 continue;
1035 args[arg][j] = args[arg][i];
1036 j++;
1037 }
1038 args[arg][j] = '\0';
1039 arg++;
1040 }
1041
1042 appctx->ctx.stats.scope_str = 0;
1043 appctx->ctx.stats.scope_len = 0;
1044 appctx->ctx.stats.flags = 0;
1045 if ((kw = cli_find_kw(args))) {
1046 if (kw->parse) {
1047 if (kw->parse(args, appctx, kw->private) == 0 && kw->io_handler) {
1048 appctx->st0 = STAT_CLI_O_CUSTOM;
1049 appctx->io_handler = kw->io_handler;
1050 appctx->io_release = kw->io_release;
1051 }
1052 }
1053 } else if (strcmp(args[0], "show") == 0) {
William Lallemand933efcd2016-11-22 12:34:16 +01001054 if (strcmp(args[1], "env") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001055 extern char **environ;
1056
1057 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1058 appctx->ctx.cli.msg = stats_permission_denied_msg;
1059 appctx->st0 = STAT_CLI_PRINT;
1060 return 1;
1061 }
1062 appctx->ctx.env.var = environ;
1063 appctx->st2 = STAT_ST_INIT;
1064 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1065
1066 if (*args[2]) {
1067 int len = strlen(args[2]);
1068
1069 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1070 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1071 (*appctx->ctx.env.var)[len] == '=')
1072 break;
1073 }
1074 if (!*appctx->ctx.env.var) {
1075 appctx->ctx.cli.msg = "Variable not found\n";
1076 appctx->st0 = STAT_CLI_PRINT;
1077 return 1;
1078 }
1079 appctx->st2 = STAT_ST_END;
1080 }
1081 }
1082 else if (strcmp(args[1], "stat") == 0) {
William Lallemand69e96442016-11-19 00:58:54 +01001083 if (*args[2] && *args[3] && *args[4]) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001084 appctx->ctx.stats.flags |= STAT_BOUND;
1085 appctx->ctx.stats.iid = atoi(args[2]);
1086 appctx->ctx.stats.type = atoi(args[3]);
1087 appctx->ctx.stats.sid = atoi(args[4]);
1088 if (strcmp(args[5], "typed") == 0)
1089 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1090 }
1091 else if (strcmp(args[2], "typed") == 0)
1092 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1093
1094 appctx->st2 = STAT_ST_INIT;
1095 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
1096 }
1097 else if (strcmp(args[1], "info") == 0) {
1098 if (strcmp(args[2], "typed") == 0)
1099 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1100 appctx->st2 = STAT_ST_INIT;
1101 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
1102 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001103 else if (strcmp(args[1], "errors") == 0) {
1104 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1105 appctx->ctx.cli.msg = stats_permission_denied_msg;
1106 appctx->st0 = STAT_CLI_PRINT;
1107 return 1;
1108 }
1109 if (*args[2])
1110 appctx->ctx.errors.iid = atoi(args[2]);
1111 else
1112 appctx->ctx.errors.iid = -1;
1113 appctx->ctx.errors.px = NULL;
1114 appctx->st2 = STAT_ST_INIT;
1115 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
1116 }
1117 else if (strcmp(args[1], "table") == 0) {
1118 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
1119 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001120 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
1121 return 0;
1122 }
1123 }
1124 else if (strcmp(args[0], "clear") == 0) {
1125 if (strcmp(args[1], "counters") == 0) {
1126 struct proxy *px;
1127 struct server *sv;
1128 struct listener *li;
1129 int clrall = 0;
1130
1131 if (strcmp(args[2], "all") == 0)
1132 clrall = 1;
1133
1134 /* check permissions */
1135 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1136 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
1137 appctx->ctx.cli.msg = stats_permission_denied_msg;
1138 appctx->st0 = STAT_CLI_PRINT;
1139 return 1;
1140 }
1141
1142 for (px = proxy; px; px = px->next) {
1143 if (clrall) {
1144 memset(&px->be_counters, 0, sizeof(px->be_counters));
1145 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1146 }
1147 else {
1148 px->be_counters.conn_max = 0;
1149 px->be_counters.p.http.rps_max = 0;
1150 px->be_counters.sps_max = 0;
1151 px->be_counters.cps_max = 0;
1152 px->be_counters.nbpend_max = 0;
1153
1154 px->fe_counters.conn_max = 0;
1155 px->fe_counters.p.http.rps_max = 0;
1156 px->fe_counters.sps_max = 0;
1157 px->fe_counters.cps_max = 0;
1158 px->fe_counters.nbpend_max = 0;
1159 }
1160
1161 for (sv = px->srv; sv; sv = sv->next)
1162 if (clrall)
1163 memset(&sv->counters, 0, sizeof(sv->counters));
1164 else {
1165 sv->counters.cur_sess_max = 0;
1166 sv->counters.nbpend_max = 0;
1167 sv->counters.sps_max = 0;
1168 }
1169
1170 list_for_each_entry(li, &px->conf.listeners, by_fe)
1171 if (li->counters) {
1172 if (clrall)
1173 memset(li->counters, 0, sizeof(*li->counters));
1174 else
1175 li->counters->conn_max = 0;
1176 }
1177 }
1178
1179 global.cps_max = 0;
1180 global.sps_max = 0;
1181 return 1;
1182 }
1183 else if (strcmp(args[1], "table") == 0) {
1184 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
1185 /* end of processing */
1186 return 1;
1187 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001188 else {
1189 /* unknown "clear" argument */
1190 return 0;
1191 }
1192 }
1193 else if (strcmp(args[0], "get") == 0) {
1194 if (strcmp(args[1], "weight") == 0) {
1195 struct proxy *px;
1196 struct server *sv;
1197
1198 /* split "backend/server" and make <line> point to server */
1199 for (line = args[2]; *line; line++)
1200 if (*line == '/') {
1201 *line++ = '\0';
1202 break;
1203 }
1204
1205 if (!*line) {
1206 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1207 appctx->st0 = STAT_CLI_PRINT;
1208 return 1;
1209 }
1210
1211 if (!get_backend_server(args[2], line, &px, &sv)) {
1212 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1213 appctx->st0 = STAT_CLI_PRINT;
1214 return 1;
1215 }
1216
1217 /* return server's effective weight at the moment */
1218 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1219 if (bi_putstr(si_ic(si), trash.str) == -1)
1220 si_applet_cant_put(si);
1221
1222 return 1;
1223 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001224 else { /* not "get weight" */
1225 return 0;
1226 }
1227 }
1228 else if (strcmp(args[0], "set") == 0) {
1229 if (strcmp(args[1], "weight") == 0) {
1230 struct server *sv;
1231 const char *warning;
1232
1233 sv = expect_server_admin(s, si, args[2]);
1234 if (!sv)
1235 return 1;
1236
1237 warning = server_parse_weight_change_request(sv, args[3]);
1238 if (warning) {
1239 appctx->ctx.cli.msg = warning;
1240 appctx->st0 = STAT_CLI_PRINT;
1241 }
1242 return 1;
1243 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001244 else if (strcmp(args[1], "timeout") == 0) {
1245 if (strcmp(args[2], "cli") == 0) {
1246 unsigned timeout;
1247 const char *res;
1248
1249 if (!*args[3]) {
1250 appctx->ctx.cli.msg = "Expects an integer value.\n";
1251 appctx->st0 = STAT_CLI_PRINT;
1252 return 1;
1253 }
1254
1255 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1256 if (res || timeout < 1) {
1257 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1258 appctx->st0 = STAT_CLI_PRINT;
1259 return 1;
1260 }
1261
1262 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
1263 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
1264 return 1;
1265 }
1266 else {
1267 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1268 appctx->st0 = STAT_CLI_PRINT;
1269 return 1;
1270 }
1271 }
1272 else if (strcmp(args[1], "maxconn") == 0) {
1273 if (strcmp(args[2], "frontend") == 0) {
1274 struct proxy *px;
1275 struct listener *l;
1276 int v;
1277
1278 px = expect_frontend_admin(s, si, args[3]);
1279 if (!px)
1280 return 1;
1281
1282 if (!*args[4]) {
1283 appctx->ctx.cli.msg = "Integer value expected.\n";
1284 appctx->st0 = STAT_CLI_PRINT;
1285 return 1;
1286 }
1287
1288 v = atoi(args[4]);
1289 if (v < 0) {
1290 appctx->ctx.cli.msg = "Value out of range.\n";
1291 appctx->st0 = STAT_CLI_PRINT;
1292 return 1;
1293 }
1294
1295 /* OK, the value is fine, so we assign it to the proxy and to all of
1296 * its listeners. The blocked ones will be dequeued.
1297 */
1298 px->maxconn = v;
1299 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1300 l->maxconn = v;
1301 if (l->state == LI_FULL)
1302 resume_listener(l);
1303 }
1304
1305 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&px->listener_queue))
1306 dequeue_all_listeners(&px->listener_queue);
1307
1308 return 1;
1309 }
1310 else if (strcmp(args[2], "server") == 0) {
1311 struct server *sv;
1312 const char *warning;
1313
1314 sv = expect_server_admin(s, si, args[3]);
1315 if (!sv)
1316 return 1;
1317
1318 warning = server_parse_maxconn_change_request(sv, args[4]);
1319 if (warning) {
1320 appctx->ctx.cli.msg = warning;
1321 appctx->st0 = STAT_CLI_PRINT;
1322 }
1323
1324 return 1;
1325 }
1326 else if (strcmp(args[2], "global") == 0) {
1327 int v;
1328
1329 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1330 appctx->ctx.cli.msg = stats_permission_denied_msg;
1331 appctx->st0 = STAT_CLI_PRINT;
1332 return 1;
1333 }
1334
1335 if (!*args[3]) {
1336 appctx->ctx.cli.msg = "Expects an integer value.\n";
1337 appctx->st0 = STAT_CLI_PRINT;
1338 return 1;
1339 }
1340
1341 v = atoi(args[3]);
1342 if (v > global.hardmaxconn) {
1343 appctx->ctx.cli.msg = "Value out of range.\n";
1344 appctx->st0 = STAT_CLI_PRINT;
1345 return 1;
1346 }
1347
1348 /* check for unlimited values */
1349 if (v <= 0)
1350 v = global.hardmaxconn;
1351
1352 global.maxconn = v;
1353
1354 /* Dequeues all of the listeners waiting for a resource */
1355 if (!LIST_ISEMPTY(&global_listener_queue))
1356 dequeue_all_listeners(&global_listener_queue);
1357
1358 return 1;
1359 }
1360 else {
1361 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
1362 appctx->st0 = STAT_CLI_PRINT;
1363 return 1;
1364 }
1365 }
1366 else if (strcmp(args[1], "rate-limit") == 0) {
1367 if (strcmp(args[2], "connections") == 0) {
1368 if (strcmp(args[3], "global") == 0) {
1369 int v;
1370
1371 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1372 appctx->ctx.cli.msg = stats_permission_denied_msg;
1373 appctx->st0 = STAT_CLI_PRINT;
1374 return 1;
1375 }
1376
1377 if (!*args[4]) {
1378 appctx->ctx.cli.msg = "Expects an integer value.\n";
1379 appctx->st0 = STAT_CLI_PRINT;
1380 return 1;
1381 }
1382
1383 v = atoi(args[4]);
1384 if (v < 0) {
1385 appctx->ctx.cli.msg = "Value out of range.\n";
1386 appctx->st0 = STAT_CLI_PRINT;
1387 return 1;
1388 }
1389
1390 global.cps_lim = v;
1391
1392 /* Dequeues all of the listeners waiting for a resource */
1393 if (!LIST_ISEMPTY(&global_listener_queue))
1394 dequeue_all_listeners(&global_listener_queue);
1395
1396 return 1;
1397 }
1398 else {
1399 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1400 appctx->st0 = STAT_CLI_PRINT;
1401 return 1;
1402 }
1403 }
1404 else if (strcmp(args[2], "sessions") == 0) {
1405 if (strcmp(args[3], "global") == 0) {
1406 int v;
1407
1408 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1409 appctx->ctx.cli.msg = stats_permission_denied_msg;
1410 appctx->st0 = STAT_CLI_PRINT;
1411 return 1;
1412 }
1413
1414 if (!*args[4]) {
1415 appctx->ctx.cli.msg = "Expects an integer value.\n";
1416 appctx->st0 = STAT_CLI_PRINT;
1417 return 1;
1418 }
1419
1420 v = atoi(args[4]);
1421 if (v < 0) {
1422 appctx->ctx.cli.msg = "Value out of range.\n";
1423 appctx->st0 = STAT_CLI_PRINT;
1424 return 1;
1425 }
1426
1427 global.sps_lim = v;
1428
1429 /* Dequeues all of the listeners waiting for a resource */
1430 if (!LIST_ISEMPTY(&global_listener_queue))
1431 dequeue_all_listeners(&global_listener_queue);
1432
1433 return 1;
1434 }
1435 else {
1436 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1437 appctx->st0 = STAT_CLI_PRINT;
1438 return 1;
1439 }
1440 }
1441#ifdef USE_OPENSSL
1442 else if (strcmp(args[2], "ssl-sessions") == 0) {
1443 if (strcmp(args[3], "global") == 0) {
1444 int v;
1445
1446 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1447 appctx->ctx.cli.msg = stats_permission_denied_msg;
1448 appctx->st0 = STAT_CLI_PRINT;
1449 return 1;
1450 }
1451
1452 if (!*args[4]) {
1453 appctx->ctx.cli.msg = "Expects an integer value.\n";
1454 appctx->st0 = STAT_CLI_PRINT;
1455 return 1;
1456 }
1457
1458 v = atoi(args[4]);
1459 if (v < 0) {
1460 appctx->ctx.cli.msg = "Value out of range.\n";
1461 appctx->st0 = STAT_CLI_PRINT;
1462 return 1;
1463 }
1464
1465 global.ssl_lim = v;
1466
1467 /* Dequeues all of the listeners waiting for a resource */
1468 if (!LIST_ISEMPTY(&global_listener_queue))
1469 dequeue_all_listeners(&global_listener_queue);
1470
1471 return 1;
1472 }
1473 else {
1474 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1475 appctx->st0 = STAT_CLI_PRINT;
1476 return 1;
1477 }
1478 }
1479#endif
1480 else if (strcmp(args[2], "http-compression") == 0) {
1481 if (strcmp(args[3], "global") == 0) {
1482 int v;
1483
1484 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1485 appctx->ctx.cli.msg = stats_permission_denied_msg;
1486 appctx->st0 = STAT_CLI_PRINT;
1487 return 1;
1488 }
1489
1490 if (!*args[4]) {
1491 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1492 appctx->st0 = STAT_CLI_PRINT;
1493 return 1;
1494 }
1495
1496 v = atoi(args[4]);
1497 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1498 }
1499 else {
1500 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1501 appctx->st0 = STAT_CLI_PRINT;
1502 return 1;
1503 }
1504 }
1505 else {
1506 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
1507 appctx->st0 = STAT_CLI_PRINT;
1508 return 1;
1509 }
1510 }
1511 else if (strcmp(args[1], "table") == 0) {
1512 stats_sock_table_request(si, args, STAT_CLI_O_SET);
William Lallemand32af2032016-10-29 18:09:35 +02001513 } else { /* unknown "set" parameter */
William Lallemand74c24fb2016-11-21 17:18:36 +01001514 return 0;
1515 }
1516 }
1517 else if (strcmp(args[0], "enable") == 0) {
1518 if (strcmp(args[1], "agent") == 0) {
1519 struct server *sv;
1520
1521 sv = expect_server_admin(s, si, args[2]);
1522 if (!sv)
1523 return 1;
1524
1525 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1526 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1527 appctx->st0 = STAT_CLI_PRINT;
1528 return 1;
1529 }
1530
1531 sv->agent.state |= CHK_ST_ENABLED;
1532 return 1;
1533 }
1534 else if (strcmp(args[1], "health") == 0) {
1535 struct server *sv;
1536
1537 sv = expect_server_admin(s, si, args[2]);
1538 if (!sv)
1539 return 1;
1540
1541 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1542 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1543 appctx->st0 = STAT_CLI_PRINT;
1544 return 1;
1545 }
1546
1547 sv->check.state |= CHK_ST_ENABLED;
1548 return 1;
1549 }
1550 else if (strcmp(args[1], "server") == 0) {
1551 struct server *sv;
1552
1553 sv = expect_server_admin(s, si, args[2]);
1554 if (!sv)
1555 return 1;
1556
1557 srv_adm_set_ready(sv);
1558 return 1;
1559 }
1560 else if (strcmp(args[1], "frontend") == 0) {
1561 struct proxy *px;
1562
1563 px = expect_frontend_admin(s, si, args[2]);
1564 if (!px)
1565 return 1;
1566
1567 if (px->state == PR_STSTOPPED) {
1568 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1569 appctx->st0 = STAT_CLI_PRINT;
1570 return 1;
1571 }
1572
1573 if (px->state != PR_STPAUSED) {
1574 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1575 appctx->st0 = STAT_CLI_PRINT;
1576 return 1;
1577 }
1578
1579 if (!resume_proxy(px)) {
1580 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1581 appctx->st0 = STAT_CLI_PRINT;
1582 return 1;
1583 }
1584 return 1;
1585 }
1586 else { /* unknown "enable" parameter */
1587 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
1588 appctx->st0 = STAT_CLI_PRINT;
1589 return 1;
1590 }
1591 }
1592 else if (strcmp(args[0], "disable") == 0) {
1593 if (strcmp(args[1], "agent") == 0) {
1594 struct server *sv;
1595
1596 sv = expect_server_admin(s, si, args[2]);
1597 if (!sv)
1598 return 1;
1599
1600 sv->agent.state &= ~CHK_ST_ENABLED;
1601 return 1;
1602 }
1603 else if (strcmp(args[1], "health") == 0) {
1604 struct server *sv;
1605
1606 sv = expect_server_admin(s, si, args[2]);
1607 if (!sv)
1608 return 1;
1609
1610 sv->check.state &= ~CHK_ST_ENABLED;
1611 return 1;
1612 }
1613 else if (strcmp(args[1], "server") == 0) {
1614 struct server *sv;
1615
1616 sv = expect_server_admin(s, si, args[2]);
1617 if (!sv)
1618 return 1;
1619
1620 srv_adm_set_maint(sv);
1621 return 1;
1622 }
1623 else if (strcmp(args[1], "frontend") == 0) {
1624 struct proxy *px;
1625
1626 px = expect_frontend_admin(s, si, args[2]);
1627 if (!px)
1628 return 1;
1629
1630 if (px->state == PR_STSTOPPED) {
1631 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1632 appctx->st0 = STAT_CLI_PRINT;
1633 return 1;
1634 }
1635
1636 if (px->state == PR_STPAUSED) {
1637 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1638 appctx->st0 = STAT_CLI_PRINT;
1639 return 1;
1640 }
1641
1642 if (!pause_proxy(px)) {
1643 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1644 appctx->st0 = STAT_CLI_PRINT;
1645 return 1;
1646 }
1647 return 1;
1648 }
1649 else { /* unknown "disable" parameter */
1650 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
1651 appctx->st0 = STAT_CLI_PRINT;
1652 return 1;
1653 }
1654 }
1655 else if (strcmp(args[0], "shutdown") == 0) {
1656 if (strcmp(args[1], "frontend") == 0) {
1657 struct proxy *px;
1658
1659 px = expect_frontend_admin(s, si, args[2]);
1660 if (!px)
1661 return 1;
1662
1663 if (px->state == PR_STSTOPPED) {
1664 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1665 appctx->st0 = STAT_CLI_PRINT;
1666 return 1;
1667 }
1668
1669 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1670 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1671 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1672 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1673 stop_proxy(px);
1674 return 1;
1675 }
1676 else if (strcmp(args[1], "session") == 0) {
1677 struct stream *sess, *ptr;
1678
1679 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1680 appctx->ctx.cli.msg = stats_permission_denied_msg;
1681 appctx->st0 = STAT_CLI_PRINT;
1682 return 1;
1683 }
1684
1685 if (!*args[2]) {
1686 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1687 appctx->st0 = STAT_CLI_PRINT;
1688 return 1;
1689 }
1690
1691 ptr = (void *)strtoul(args[2], NULL, 0);
1692
1693 /* first, look for the requested stream in the stream table */
1694 list_for_each_entry(sess, &streams, list) {
1695 if (sess == ptr)
1696 break;
1697 }
1698
1699 /* do we have the stream ? */
1700 if (sess != ptr) {
1701 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1702 appctx->st0 = STAT_CLI_PRINT;
1703 return 1;
1704 }
1705
1706 stream_shutdown(sess, SF_ERR_KILLED);
1707 return 1;
1708 }
1709 else if (strcmp(args[1], "sessions") == 0) {
1710 if (strcmp(args[2], "server") == 0) {
1711 struct server *sv;
1712 struct stream *sess, *sess_bck;
1713
1714 sv = expect_server_admin(s, si, args[3]);
1715 if (!sv)
1716 return 1;
1717
1718 /* kill all the stream that are on this server */
1719 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1720 if (sess->srv_conn == sv)
1721 stream_shutdown(sess, SF_ERR_KILLED);
1722
1723 return 1;
1724 }
1725 else {
1726 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1727 appctx->st0 = STAT_CLI_PRINT;
1728 return 1;
1729 }
1730 }
1731 else { /* unknown "disable" parameter */
1732 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1733 appctx->st0 = STAT_CLI_PRINT;
1734 return 1;
1735 }
1736 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001737 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
1738 return 0;
1739 }
1740 return 1;
1741}
1742
1743/* This I/O handler runs as an applet embedded in a stream interface. It is
1744 * used to processes I/O from/to the stats unix socket. The system relies on a
1745 * state machine handling requests and various responses. We read a request,
1746 * then we process it and send the response, and we possibly display a prompt.
1747 * Then we can read again. The state is stored in appctx->st0 and is one of the
1748 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
1749 * or not.
1750 */
1751static void cli_io_handler(struct appctx *appctx)
1752{
1753 struct stream_interface *si = appctx->owner;
1754 struct channel *req = si_oc(si);
1755 struct channel *res = si_ic(si);
1756 int reql;
1757 int len;
1758
1759 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1760 goto out;
1761
1762 while (1) {
1763 if (appctx->st0 == STAT_CLI_INIT) {
1764 /* Stats output not initialized yet */
1765 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1766 appctx->st0 = STAT_CLI_GETREQ;
1767 }
1768 else if (appctx->st0 == STAT_CLI_END) {
1769 /* Let's close for real now. We just close the request
1770 * side, the conditions below will complete if needed.
1771 */
1772 si_shutw(si);
1773 break;
1774 }
1775 else if (appctx->st0 == STAT_CLI_GETREQ) {
1776 /* ensure we have some output room left in the event we
1777 * would want to return some info right after parsing.
1778 */
1779 if (buffer_almost_full(si_ib(si))) {
1780 si_applet_cant_put(si);
1781 break;
1782 }
1783
1784 reql = bo_getline(si_oc(si), trash.str, trash.size);
1785 if (reql <= 0) { /* closed or EOL not found */
1786 if (reql == 0)
1787 break;
1788 appctx->st0 = STAT_CLI_END;
1789 continue;
1790 }
1791
1792 /* seek for a possible unescaped semi-colon. If we find
1793 * one, we replace it with an LF and skip only this part.
1794 */
1795 for (len = 0; len < reql; len++) {
1796 if (trash.str[len] == '\\') {
1797 len++;
1798 continue;
1799 }
1800 if (trash.str[len] == ';') {
1801 trash.str[len] = '\n';
1802 reql = len + 1;
1803 break;
1804 }
1805 }
1806
1807 /* now it is time to check that we have a full line,
1808 * remove the trailing \n and possibly \r, then cut the
1809 * line.
1810 */
1811 len = reql - 1;
1812 if (trash.str[len] != '\n') {
1813 appctx->st0 = STAT_CLI_END;
1814 continue;
1815 }
1816
1817 if (len && trash.str[len-1] == '\r')
1818 len--;
1819
1820 trash.str[len] = '\0';
1821
1822 appctx->st0 = STAT_CLI_PROMPT;
1823 if (len) {
1824 if (strcmp(trash.str, "quit") == 0) {
1825 appctx->st0 = STAT_CLI_END;
1826 continue;
1827 }
1828 else if (strcmp(trash.str, "prompt") == 0)
1829 appctx->st1 = !appctx->st1;
1830 else if (strcmp(trash.str, "help") == 0 ||
1831 !stats_sock_parse_request(si, trash.str)) {
1832 cli_gen_usage_msg();
1833 if (dynamic_usage_msg)
1834 appctx->ctx.cli.msg = dynamic_usage_msg;
1835 else
1836 appctx->ctx.cli.msg = stats_sock_usage_msg;
1837 appctx->st0 = STAT_CLI_PRINT;
1838 }
1839 /* NB: stats_sock_parse_request() may have put
1840 * another STAT_CLI_O_* into appctx->st0.
1841 */
1842 }
1843 else if (!appctx->st1) {
1844 /* if prompt is disabled, print help on empty lines,
1845 * so that the user at least knows how to enable
1846 * prompt and find help.
1847 */
1848 cli_gen_usage_msg();
1849 if (dynamic_usage_msg)
1850 appctx->ctx.cli.msg = dynamic_usage_msg;
1851 else
1852 appctx->ctx.cli.msg = stats_sock_usage_msg;
1853 appctx->st0 = STAT_CLI_PRINT;
1854 }
1855
1856 /* re-adjust req buffer */
1857 bo_skip(si_oc(si), reql);
1858 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
1859 }
1860 else { /* output functions */
1861 switch (appctx->st0) {
1862 case STAT_CLI_PROMPT:
1863 break;
1864 case STAT_CLI_PRINT:
1865 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
1866 appctx->st0 = STAT_CLI_PROMPT;
1867 else
1868 si_applet_cant_put(si);
1869 break;
1870 case STAT_CLI_PRINT_FREE:
1871 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
1872 free(appctx->ctx.cli.err);
1873 appctx->st0 = STAT_CLI_PROMPT;
1874 }
1875 else
1876 si_applet_cant_put(si);
1877 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001878 case STAT_CLI_O_INFO:
1879 if (stats_dump_info_to_buffer(si))
1880 appctx->st0 = STAT_CLI_PROMPT;
1881 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001882 case STAT_CLI_O_STAT:
1883 if (stats_dump_stat_to_buffer(si, NULL))
1884 appctx->st0 = STAT_CLI_PROMPT;
1885 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001886 case STAT_CLI_O_ERR: /* errors dump */
1887 if (stats_dump_errors_to_buffer(si))
1888 appctx->st0 = STAT_CLI_PROMPT;
1889 break;
1890 case STAT_CLI_O_TAB:
1891 case STAT_CLI_O_CLR:
1892 if (stats_table_request(si, appctx->st0))
1893 appctx->st0 = STAT_CLI_PROMPT;
1894 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001895 case STAT_CLI_O_ENV: /* environment dump */
1896 if (stats_dump_env_to_buffer(si))
1897 appctx->st0 = STAT_CLI_PROMPT;
1898 break;
1899 case STAT_CLI_O_CUSTOM: /* use custom pointer */
1900 if (appctx->io_handler)
1901 if (appctx->io_handler(appctx)) {
1902 appctx->st0 = STAT_CLI_PROMPT;
1903 if (appctx->io_release) {
1904 appctx->io_release(appctx);
1905 appctx->io_release = NULL;
1906 }
1907 }
1908 break;
1909 default: /* abnormal state */
1910 si->flags |= SI_FL_ERR;
1911 break;
1912 }
1913
1914 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
1915 if (appctx->st0 == STAT_CLI_PROMPT) {
1916 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
1917 appctx->st0 = STAT_CLI_GETREQ;
1918 else
1919 si_applet_cant_put(si);
1920 }
1921
1922 /* If the output functions are still there, it means they require more room. */
1923 if (appctx->st0 >= STAT_CLI_OUTPUT)
1924 break;
1925
1926 /* Now we close the output if one of the writers did so,
1927 * or if we're not in interactive mode and the request
1928 * buffer is empty. This still allows pipelined requests
1929 * to be sent in non-interactive mode.
1930 */
1931 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
1932 appctx->st0 = STAT_CLI_END;
1933 continue;
1934 }
1935
1936 /* switch state back to GETREQ to read next requests */
1937 appctx->st0 = STAT_CLI_GETREQ;
1938 }
1939 }
1940
1941 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
1942 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
1943 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1944 /* Other side has closed, let's abort if we have no more processing to do
1945 * and nothing more to consume. This is comparable to a broken pipe, so
1946 * we forward the close to the request side so that it flows upstream to
1947 * the client.
1948 */
1949 si_shutw(si);
1950 }
1951
1952 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
1953 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
1954 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
1955 /* We have no more processing to do, and nothing more to send, and
1956 * the client side has closed. So we'll forward this state downstream
1957 * on the response buffer.
1958 */
1959 si_shutr(si);
1960 res->flags |= CF_READ_NULL;
1961 }
1962
1963 out:
1964 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
1965 __FUNCTION__, __LINE__,
1966 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
1967}
1968
1969/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
1970static int stats_dump_info_fields(struct chunk *out, const struct field *info)
1971{
1972 int field;
1973
1974 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
1975 if (!field_format(info, field))
1976 continue;
1977
1978 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
1979 return 0;
1980 if (!stats_emit_raw_data_field(out, &info[field]))
1981 return 0;
1982 if (!chunk_strcat(out, "\n"))
1983 return 0;
1984 }
1985 return 1;
1986}
1987
1988/* Dump all fields from <info> into <out> using the "show info typed" format */
1989static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
1990{
1991 int field;
1992
1993 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
1994 if (!field_format(info, field))
1995 continue;
1996
1997 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
1998 return 0;
1999 if (!stats_emit_field_tags(out, &info[field], ':'))
2000 return 0;
2001 if (!stats_emit_typed_data_field(out, &info[field]))
2002 return 0;
2003 if (!chunk_strcat(out, "\n"))
2004 return 0;
2005 }
2006 return 1;
2007}
2008
2009/* Fill <info> with HAProxy global info. <info> is preallocated
2010 * array of length <len>. The length of the aray must be
2011 * INF_TOTAL_FIELDS. If this length is less then this value, the
2012 * function returns 0, otherwise, it returns 1.
2013 */
2014int stats_fill_info(struct field *info, int len)
2015{
2016 unsigned int up = (now.tv_sec - start_date.tv_sec);
2017 struct chunk *out = get_trash_chunk();
2018
2019#ifdef USE_OPENSSL
2020 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2021 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2022 int ssl_reuse = 0;
2023
2024 if (ssl_key_rate < ssl_sess_rate) {
2025 /* count the ssl reuse ratio and avoid overflows in both directions */
2026 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2027 }
2028#endif
2029
2030 if (len < INF_TOTAL_FIELDS)
2031 return 0;
2032
2033 chunk_reset(out);
2034 memset(info, 0, sizeof(*info) * len);
2035
2036 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
2037 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
2038 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
2039
2040 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
2041 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
2042 info[INF_PID] = mkf_u32(FO_STATUS, pid);
2043
2044 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
2045 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
2046
2047 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
2048 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
2049 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
2050 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
2051 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
2052 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
2053 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
2054 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
2055 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
2056 info[INF_CURR_CONN] = mkf_u32(0, actconn);
2057 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
2058 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
2059#ifdef USE_OPENSSL
2060 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
2061 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
2062 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
2063#endif
2064 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
2065 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
2066 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
2067 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
2068 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
2069 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
2070 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
2071 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
2072 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
2073
2074#ifdef USE_OPENSSL
2075 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
2076 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
2077 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
2078 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
2079 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
2080 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
2081 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
2082 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
2083 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
2084 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
2085#endif
2086 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
2087 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
2088 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
2089#ifdef USE_ZLIB
2090 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
2091 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
2092#endif
2093 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
2094 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
2095 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
2096 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
2097 if (global.desc)
2098 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
2099
2100 return 1;
2101}
2102
2103/* This function dumps information onto the stream interface's read buffer.
2104 * It returns 0 as long as it does not complete, non-zero upon completion.
2105 * No state is used.
2106 */
2107static int stats_dump_info_to_buffer(struct stream_interface *si)
2108{
2109 struct appctx *appctx = __objt_appctx(si->end);
2110
2111 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
2112 return 0;
2113
2114 chunk_reset(&trash);
2115
2116 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
2117 stats_dump_typed_info_fields(&trash, info);
2118 else
2119 stats_dump_info_fields(&trash, info);
2120
2121 if (bi_putchk(si_ic(si), &trash) == -1) {
2122 si_applet_cant_put(si);
2123 return 0;
2124 }
2125
2126 return 1;
2127}
2128
William Lallemand74c24fb2016-11-21 17:18:36 +01002129
William Lallemand74c24fb2016-11-21 17:18:36 +01002130/* This is called when the stream interface is closed. For instance, upon an
2131 * external abort, we won't call the i/o handler anymore so we may need to
2132 * remove back references to the stream currently being dumped.
2133 */
2134static void cli_release_handler(struct appctx *appctx)
2135{
2136 if (appctx->io_release) {
2137 appctx->io_release(appctx);
2138 appctx->io_release = NULL;
2139 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002140 else if ((appctx->st0 == STAT_CLI_O_TAB || appctx->st0 == STAT_CLI_O_CLR) &&
2141 appctx->st2 == STAT_ST_LIST) {
2142 appctx->ctx.table.entry->ref_cnt--;
2143 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2144 }
2145 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
2146 free(appctx->ctx.cli.err);
2147 appctx->ctx.cli.err = NULL;
2148 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002149}
2150
2151/* This function is used to either dump tables states (when action is set
2152 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
2153 * It returns 0 if the output buffer is full and it needs to be called
2154 * again, otherwise non-zero.
2155 */
2156static int stats_table_request(struct stream_interface *si, int action)
2157{
2158 struct appctx *appctx = __objt_appctx(si->end);
2159 struct stream *s = si_strm(si);
2160 struct ebmb_node *eb;
2161 int dt;
2162 int skip_entry;
2163 int show = action == STAT_CLI_O_TAB;
2164
2165 /*
2166 * We have 3 possible states in appctx->st2 :
2167 * - STAT_ST_INIT : the first call
2168 * - STAT_ST_INFO : the proxy pointer points to the next table to
2169 * dump, the entry pointer is NULL ;
2170 * - STAT_ST_LIST : the proxy pointer points to the current table
2171 * and the entry pointer points to the next entry to be dumped,
2172 * and the refcount on the next entry is held ;
2173 * - STAT_ST_END : nothing left to dump, the buffer may contain some
2174 * data though.
2175 */
2176
2177 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
2178 /* in case of abort, remove any refcount we might have set on an entry */
2179 if (appctx->st2 == STAT_ST_LIST) {
2180 appctx->ctx.table.entry->ref_cnt--;
2181 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2182 }
2183 return 1;
2184 }
2185
2186 chunk_reset(&trash);
2187
2188 while (appctx->st2 != STAT_ST_FIN) {
2189 switch (appctx->st2) {
2190 case STAT_ST_INIT:
2191 appctx->ctx.table.proxy = appctx->ctx.table.target;
2192 if (!appctx->ctx.table.proxy)
2193 appctx->ctx.table.proxy = proxy;
2194
2195 appctx->ctx.table.entry = NULL;
2196 appctx->st2 = STAT_ST_INFO;
2197 break;
2198
2199 case STAT_ST_INFO:
2200 if (!appctx->ctx.table.proxy ||
2201 (appctx->ctx.table.target &&
2202 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
2203 appctx->st2 = STAT_ST_END;
2204 break;
2205 }
2206
2207 if (appctx->ctx.table.proxy->table.size) {
2208 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
2209 appctx->ctx.table.target))
2210 return 0;
2211
2212 if (appctx->ctx.table.target &&
2213 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
2214 /* dump entries only if table explicitly requested */
2215 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
2216 if (eb) {
2217 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2218 appctx->ctx.table.entry->ref_cnt++;
2219 appctx->st2 = STAT_ST_LIST;
2220 break;
2221 }
2222 }
2223 }
2224 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
2225 break;
2226
2227 case STAT_ST_LIST:
2228 skip_entry = 0;
2229
2230 if (appctx->ctx.table.data_type >= 0) {
2231 /* we're filtering on some data contents */
2232 void *ptr;
2233 long long data;
2234
2235 dt = appctx->ctx.table.data_type;
2236 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
2237 appctx->ctx.table.entry,
2238 dt);
2239
2240 data = 0;
2241 switch (stktable_data_types[dt].std_type) {
2242 case STD_T_SINT:
2243 data = stktable_data_cast(ptr, std_t_sint);
2244 break;
2245 case STD_T_UINT:
2246 data = stktable_data_cast(ptr, std_t_uint);
2247 break;
2248 case STD_T_ULL:
2249 data = stktable_data_cast(ptr, std_t_ull);
2250 break;
2251 case STD_T_FRQP:
2252 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
2253 appctx->ctx.table.proxy->table.data_arg[dt].u);
2254 break;
2255 }
2256
2257 /* skip the entry if the data does not match the test and the value */
2258 if ((data < appctx->ctx.table.value &&
2259 (appctx->ctx.table.data_op == STD_OP_EQ ||
2260 appctx->ctx.table.data_op == STD_OP_GT ||
2261 appctx->ctx.table.data_op == STD_OP_GE)) ||
2262 (data == appctx->ctx.table.value &&
2263 (appctx->ctx.table.data_op == STD_OP_NE ||
2264 appctx->ctx.table.data_op == STD_OP_GT ||
2265 appctx->ctx.table.data_op == STD_OP_LT)) ||
2266 (data > appctx->ctx.table.value &&
2267 (appctx->ctx.table.data_op == STD_OP_EQ ||
2268 appctx->ctx.table.data_op == STD_OP_LT ||
2269 appctx->ctx.table.data_op == STD_OP_LE)))
2270 skip_entry = 1;
2271 }
2272
2273 if (show && !skip_entry &&
2274 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
2275 appctx->ctx.table.entry))
2276 return 0;
2277
2278 appctx->ctx.table.entry->ref_cnt--;
2279
2280 eb = ebmb_next(&appctx->ctx.table.entry->key);
2281 if (eb) {
2282 struct stksess *old = appctx->ctx.table.entry;
2283 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2284 if (show)
2285 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
2286 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
2287 stksess_kill(&appctx->ctx.table.proxy->table, old);
2288 appctx->ctx.table.entry->ref_cnt++;
2289 break;
2290 }
2291
2292
2293 if (show)
2294 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2295 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
2296 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2297
2298 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
2299 appctx->st2 = STAT_ST_INFO;
2300 break;
2301
2302 case STAT_ST_END:
2303 appctx->st2 = STAT_ST_FIN;
2304 break;
2305 }
2306 }
2307 return 1;
2308}
2309
2310/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
2311 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
2312 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
2313 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
2314 * lines are respected within the limit of 70 output chars. Lines that are
2315 * continuation of a previous truncated line begin with "+" instead of " "
2316 * after the offset. The new pointer is returned.
2317 */
2318static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
2319 int *line, int ptr)
2320{
2321 int end;
2322 unsigned char c;
2323
2324 end = out->len + 80;
2325 if (end > out->size)
2326 return ptr;
2327
2328 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
2329
2330 while (ptr < len && ptr < bsize) {
2331 c = buf[ptr];
2332 if (isprint(c) && isascii(c) && c != '\\') {
2333 if (out->len > end - 2)
2334 break;
2335 out->str[out->len++] = c;
2336 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
2337 if (out->len > end - 3)
2338 break;
2339 out->str[out->len++] = '\\';
2340 switch (c) {
2341 case '\t': c = 't'; break;
2342 case '\n': c = 'n'; break;
2343 case '\r': c = 'r'; break;
2344 case '\e': c = 'e'; break;
2345 case '\\': c = '\\'; break;
2346 }
2347 out->str[out->len++] = c;
2348 } else {
2349 if (out->len > end - 5)
2350 break;
2351 out->str[out->len++] = '\\';
2352 out->str[out->len++] = 'x';
2353 out->str[out->len++] = hextab[(c >> 4) & 0xF];
2354 out->str[out->len++] = hextab[c & 0xF];
2355 }
2356 if (buf[ptr++] == '\n') {
2357 /* we had a line break, let's return now */
2358 out->str[out->len++] = '\n';
2359 *line = ptr;
2360 return ptr;
2361 }
2362 }
2363 /* we have an incomplete line, we return it as-is */
2364 out->str[out->len++] = '\n';
2365 return ptr;
2366}
2367
William Lallemand74c24fb2016-11-21 17:18:36 +01002368/* This function dumps all captured errors onto the stream interface's
2369 * read buffer. It returns 0 if the output buffer is full and it needs
2370 * to be called again, otherwise non-zero.
2371 */
2372static int stats_dump_errors_to_buffer(struct stream_interface *si)
2373{
2374 struct appctx *appctx = __objt_appctx(si->end);
2375 extern const char *monthname[12];
2376
2377 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
2378 return 1;
2379
2380 chunk_reset(&trash);
2381
2382 if (!appctx->ctx.errors.px) {
2383 /* the function had not been called yet, let's prepare the
2384 * buffer for a response.
2385 */
2386 struct tm tm;
2387
2388 get_localtime(date.tv_sec, &tm);
2389 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
2390 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2391 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
2392 error_snapshot_id);
2393
2394 if (bi_putchk(si_ic(si), &trash) == -1) {
2395 /* Socket buffer full. Let's try again later from the same point */
2396 si_applet_cant_put(si);
2397 return 0;
2398 }
2399
2400 appctx->ctx.errors.px = proxy;
2401 appctx->ctx.errors.buf = 0;
2402 appctx->ctx.errors.bol = 0;
2403 appctx->ctx.errors.ptr = -1;
2404 }
2405
2406 /* we have two inner loops here, one for the proxy, the other one for
2407 * the buffer.
2408 */
2409 while (appctx->ctx.errors.px) {
2410 struct error_snapshot *es;
2411
2412 if (appctx->ctx.errors.buf == 0)
2413 es = &appctx->ctx.errors.px->invalid_req;
2414 else
2415 es = &appctx->ctx.errors.px->invalid_rep;
2416
2417 if (!es->when.tv_sec)
2418 goto next;
2419
2420 if (appctx->ctx.errors.iid >= 0 &&
2421 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
2422 es->oe->uuid != appctx->ctx.errors.iid)
2423 goto next;
2424
2425 if (appctx->ctx.errors.ptr < 0) {
2426 /* just print headers now */
2427
2428 char pn[INET6_ADDRSTRLEN];
2429 struct tm tm;
2430 int port;
2431
2432 get_localtime(es->when.tv_sec, &tm);
2433 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
2434 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2435 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
2436
2437 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
2438 case AF_INET:
2439 case AF_INET6:
2440 port = get_host_port(&es->src);
2441 break;
2442 default:
2443 port = 0;
2444 }
2445
2446 switch (appctx->ctx.errors.buf) {
2447 case 0:
2448 chunk_appendf(&trash,
2449 " frontend %s (#%d): invalid request\n"
2450 " backend %s (#%d)",
2451 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
2452 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
2453 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
2454 break;
2455 case 1:
2456 chunk_appendf(&trash,
2457 " backend %s (#%d): invalid response\n"
2458 " frontend %s (#%d)",
2459 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
2460 es->oe->id, es->oe->uuid);
2461 break;
2462 }
2463
2464 chunk_appendf(&trash,
2465 ", server %s (#%d), event #%u\n"
2466 " src %s:%d, session #%d, session flags 0x%08x\n"
2467 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
2468 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
2469 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
2470 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
2471 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
2472 es->ev_id,
2473 pn, port, es->sid, es->s_flags,
2474 es->state, es->m_flags, es->t_flags,
2475 es->m_clen, es->m_blen,
2476 es->b_flags, es->b_out, es->b_tot,
2477 es->len, es->b_wrap, es->pos);
2478
2479 if (bi_putchk(si_ic(si), &trash) == -1) {
2480 /* Socket buffer full. Let's try again later from the same point */
2481 si_applet_cant_put(si);
2482 return 0;
2483 }
2484 appctx->ctx.errors.ptr = 0;
2485 appctx->ctx.errors.sid = es->sid;
2486 }
2487
2488 if (appctx->ctx.errors.sid != es->sid) {
2489 /* the snapshot changed while we were dumping it */
2490 chunk_appendf(&trash,
2491 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
2492 if (bi_putchk(si_ic(si), &trash) == -1) {
2493 si_applet_cant_put(si);
2494 return 0;
2495 }
2496 goto next;
2497 }
2498
2499 /* OK, ptr >= 0, so we have to dump the current line */
2500 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
2501 int newptr;
2502 int newline;
2503
2504 newline = appctx->ctx.errors.bol;
2505 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
2506 if (newptr == appctx->ctx.errors.ptr)
2507 return 0;
2508
2509 if (bi_putchk(si_ic(si), &trash) == -1) {
2510 /* Socket buffer full. Let's try again later from the same point */
2511 si_applet_cant_put(si);
2512 return 0;
2513 }
2514 appctx->ctx.errors.ptr = newptr;
2515 appctx->ctx.errors.bol = newline;
2516 };
2517 next:
2518 appctx->ctx.errors.bol = 0;
2519 appctx->ctx.errors.ptr = -1;
2520 appctx->ctx.errors.buf++;
2521 if (appctx->ctx.errors.buf > 1) {
2522 appctx->ctx.errors.buf = 0;
2523 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
2524 }
2525 }
2526
2527 /* dump complete */
2528 return 1;
2529}
2530
2531/* This function dumps all environmnent variables to the buffer. It returns 0
2532 * if the output buffer is full and it needs to be called again, otherwise
2533 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
2534 */
2535static int stats_dump_env_to_buffer(struct stream_interface *si)
2536{
2537 struct appctx *appctx = __objt_appctx(si->end);
2538
2539 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
2540 return 1;
2541
2542 chunk_reset(&trash);
2543
2544 /* we have two inner loops here, one for the proxy, the other one for
2545 * the buffer.
2546 */
2547 while (*appctx->ctx.env.var) {
2548 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
2549
2550 if (bi_putchk(si_ic(si), &trash) == -1) {
2551 si_applet_cant_put(si);
2552 return 0;
2553 }
2554 if (appctx->st2 == STAT_ST_END)
2555 break;
2556 appctx->ctx.env.var++;
2557 }
2558
2559 /* dump complete */
2560 return 1;
2561}
2562
2563/* parse the "level" argument on the bind lines */
2564static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
2565{
2566 if (!*args[cur_arg + 1]) {
2567 memprintf(err, "'%s' : missing level", args[cur_arg]);
2568 return ERR_ALERT | ERR_FATAL;
2569 }
2570
2571 if (!strcmp(args[cur_arg+1], "user"))
2572 conf->level = ACCESS_LVL_USER;
2573 else if (!strcmp(args[cur_arg+1], "operator"))
2574 conf->level = ACCESS_LVL_OPER;
2575 else if (!strcmp(args[cur_arg+1], "admin"))
2576 conf->level = ACCESS_LVL_ADMIN;
2577 else {
2578 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
2579 args[cur_arg], args[cur_arg+1]);
2580 return ERR_ALERT | ERR_FATAL;
2581 }
2582
2583 return 0;
2584}
2585
2586static struct applet cli_applet = {
2587 .obj_type = OBJ_TYPE_APPLET,
2588 .name = "<CLI>", /* used for logging */
2589 .fct = cli_io_handler,
2590 .release = cli_release_handler,
2591};
2592
2593static struct cfg_kw_list cfg_kws = {ILH, {
2594 { CFG_GLOBAL, "stats", stats_parse_global },
2595 { 0, NULL, NULL },
2596}};
2597
2598static struct bind_kw_list bind_kws = { "STAT", { }, {
2599 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
2600 { NULL, NULL, 0 },
2601}};
2602
2603__attribute__((constructor))
2604static void __dumpstats_module_init(void)
2605{
2606 cfg_register_keywords(&cfg_kws);
2607 bind_register_keywords(&bind_kws);
2608}
2609
2610/*
2611 * Local variables:
2612 * c-indent-level: 8
2613 * c-basic-offset: 8
2614 * End:
2615 */