blob: 7c847081032466feb0c67c5b17a31abb82d02ed5 [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
136static int stats_dump_backend_to_buffer(struct stream_interface *si);
137static int stats_dump_env_to_buffer(struct stream_interface *si);
138static int stats_dump_info_to_buffer(struct stream_interface *si);
139static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
140static int stats_dump_pools_to_buffer(struct stream_interface *si);
141static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
142static int stats_dump_sess_to_buffer(struct stream_interface *si);
143static int stats_dump_errors_to_buffer(struct stream_interface *si);
144static int stats_table_request(struct stream_interface *si, int show);
William Lallemand74c24fb2016-11-21 17:18:36 +0100145
146static int dump_servers_state(struct stream_interface *si, struct chunk *buf);
147
148static struct applet cli_applet;
149
150static const char stats_sock_usage_msg[] =
151 "Unknown command. Please enter one of the following commands only :\n"
152 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
153 " clear table : remove an entry from a table\n"
154 " help : this message\n"
155 " prompt : toggle interactive mode with prompt\n"
156 " quit : disconnect\n"
157 " show backend : list backends in the current running config\n"
158 " show env [var] : dump environment variables known to the process\n"
159 " show info : report information about the running process\n"
160 " show pools : report information about the memory pools usage\n"
161 " show stat : report counters for each proxy and server\n"
William Lallemand74c24fb2016-11-21 17:18:36 +0100162 " show errors : report last request and response errors for each proxy\n"
163 " show sess [id] : report the list of current sessions or dump this session\n"
164 " show table [id]: report table usage stats or dump this table's contents\n"
165 " show servers state [id]: dump volatile server information (for backend <id>)\n"
166 " get weight : report a server's current weight\n"
167 " set weight : change a server's weight\n"
William Lallemand74c24fb2016-11-21 17:18:36 +0100168 " set table [id] : update or create a table entry's data\n"
169 " set timeout : change a timeout setting\n"
170 " set maxconn : change a maxconn setting\n"
171 " set rate-limit : change a rate limiting value\n"
172 " disable : put a server or frontend in maintenance mode\n"
173 " enable : re-enable a server or frontend which is in maintenance mode\n"
174 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
William Lallemand74c24fb2016-11-21 17:18:36 +0100175 "";
176
177static const char stats_permission_denied_msg[] =
178 "Permission denied\n"
179 "";
180
181
182static char *dynamic_usage_msg = NULL;
183
184/* List head of cli keywords */
185static struct cli_kw_list cli_keywords = {
186 .list = LIST_HEAD_INIT(cli_keywords.list)
187};
188
189extern const char *stat_status_codes[];
190
191char *cli_gen_usage_msg()
192{
193 struct cli_kw_list *kw_list;
194 struct cli_kw *kw;
195 struct chunk *tmp = get_trash_chunk();
196 struct chunk out;
197
198 free(dynamic_usage_msg);
199 dynamic_usage_msg = NULL;
200
201 if (LIST_ISEMPTY(&cli_keywords.list))
202 return NULL;
203
204 chunk_reset(tmp);
205 chunk_strcat(tmp, stats_sock_usage_msg);
206 list_for_each_entry(kw_list, &cli_keywords.list, list) {
207 kw = &kw_list->kw[0];
208 while (kw->usage) {
209 chunk_appendf(tmp, " %s\n", kw->usage);
210 kw++;
211 }
212 }
213 chunk_init(&out, NULL, 0);
214 chunk_dup(&out, tmp);
215 dynamic_usage_msg = out.str;
216 return dynamic_usage_msg;
217}
218
219struct cli_kw* cli_find_kw(char **args)
220{
221 struct cli_kw_list *kw_list;
222 struct cli_kw *kw;/* current cli_kw */
223 char **tmp_args;
224 const char **tmp_str_kw;
225 int found = 0;
226
227 if (LIST_ISEMPTY(&cli_keywords.list))
228 return NULL;
229
230 list_for_each_entry(kw_list, &cli_keywords.list, list) {
231 kw = &kw_list->kw[0];
232 while (*kw->str_kw) {
233 tmp_args = args;
234 tmp_str_kw = kw->str_kw;
235 while (*tmp_str_kw) {
236 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
237 found = 1;
238 } else {
239 found = 0;
240 break;
241 }
242 tmp_args++;
243 tmp_str_kw++;
244 }
245 if (found)
246 return (kw);
247 kw++;
248 }
249 }
250 return NULL;
251}
252
253void cli_register_kw(struct cli_kw_list *kw_list)
254{
255 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
256}
257
258
259/* allocate a new stats frontend named <name>, and return it
260 * (or NULL in case of lack of memory).
261 */
262static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
263{
264 struct proxy *fe;
265
266 fe = calloc(1, sizeof(*fe));
267 if (!fe)
268 return NULL;
269
270 init_new_proxy(fe);
271 fe->next = proxy;
272 proxy = fe;
273 fe->last_change = now.tv_sec;
274 fe->id = strdup("GLOBAL");
275 fe->cap = PR_CAP_FE;
276 fe->maxconn = 10; /* default to 10 concurrent connections */
277 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
278 fe->conf.file = strdup(file);
279 fe->conf.line = line;
280 fe->accept = frontend_accept;
281 fe->default_target = &cli_applet.obj_type;
282
283 /* the stats frontend is the only one able to assign ID #0 */
284 fe->conf.id.key = fe->uuid = 0;
285 eb32_insert(&used_proxy_id, &fe->conf.id);
286 return fe;
287}
288
289/* This function parses a "stats" statement in the "global" section. It returns
290 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
291 * error message into the <err> buffer which will be preallocated. The trailing
292 * '\n' must not be written. The function must be called with <args> pointing to
293 * the first word after "stats".
294 */
295static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
296 struct proxy *defpx, const char *file, int line,
297 char **err)
298{
299 struct bind_conf *bind_conf;
300 struct listener *l;
301
302 if (!strcmp(args[1], "socket")) {
303 int cur_arg;
304
305 if (*args[2] == 0) {
306 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
307 return -1;
308 }
309
310 if (!global.stats_fe) {
311 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
312 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
313 return -1;
314 }
315 }
316
317 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
318 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
319
320 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
321 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
322 file, line, args[0], args[1], err && *err ? *err : "error");
323 return -1;
324 }
325
326 cur_arg = 3;
327 while (*args[cur_arg]) {
328 static int bind_dumped;
329 struct bind_kw *kw;
330
331 kw = bind_find_kw(args[cur_arg]);
332 if (kw) {
333 if (!kw->parse) {
334 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
335 args[0], args[1], args[cur_arg]);
336 return -1;
337 }
338
339 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
340 if (err && *err)
341 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
342 else
343 memprintf(err, "'%s %s' : error encountered while processing '%s'",
344 args[0], args[1], args[cur_arg]);
345 return -1;
346 }
347
348 cur_arg += 1 + kw->skip;
349 continue;
350 }
351
352 if (!bind_dumped) {
353 bind_dump_kws(err);
354 indent_msg(err, 4);
355 bind_dumped = 1;
356 }
357
358 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
359 args[0], args[1], args[cur_arg],
360 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
361 return -1;
362 }
363
364 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
365 l->maxconn = global.stats_fe->maxconn;
366 l->backlog = global.stats_fe->backlog;
367 l->accept = session_accept_fd;
368 l->handler = process_stream;
369 l->default_target = global.stats_fe->default_target;
370 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
371 l->nice = -64; /* we want to boost priority for local stats */
372 global.maxsock += l->maxconn;
373 }
374 }
375 else if (!strcmp(args[1], "timeout")) {
376 unsigned timeout;
377 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
378
379 if (res) {
380 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
381 return -1;
382 }
383
384 if (!timeout) {
385 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
386 return -1;
387 }
388 if (!global.stats_fe) {
389 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
390 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
391 return -1;
392 }
393 }
394 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
395 }
396 else if (!strcmp(args[1], "maxconn")) {
397 int maxconn = atol(args[2]);
398
399 if (maxconn <= 0) {
400 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
401 return -1;
402 }
403
404 if (!global.stats_fe) {
405 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
406 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
407 return -1;
408 }
409 }
410 global.stats_fe->maxconn = maxconn;
411 }
412 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
413 int cur_arg = 2;
414 unsigned long set = 0;
415
416 if (!global.stats_fe) {
417 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
418 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
419 return -1;
420 }
421 }
422
423 while (*args[cur_arg]) {
424 unsigned int low, high;
425
426 if (strcmp(args[cur_arg], "all") == 0) {
427 set = 0;
428 break;
429 }
430 else if (strcmp(args[cur_arg], "odd") == 0) {
431 set |= ~0UL/3UL; /* 0x555....555 */
432 }
433 else if (strcmp(args[cur_arg], "even") == 0) {
434 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
435 }
436 else if (isdigit((int)*args[cur_arg])) {
437 char *dash = strchr(args[cur_arg], '-');
438
439 low = high = str2uic(args[cur_arg]);
440 if (dash)
441 high = str2uic(dash + 1);
442
443 if (high < low) {
444 unsigned int swap = low;
445 low = high;
446 high = swap;
447 }
448
449 if (low < 1 || high > LONGBITS) {
450 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
451 args[0], args[1], LONGBITS);
452 return -1;
453 }
454 while (low <= high)
455 set |= 1UL << (low++ - 1);
456 }
457 else {
458 memprintf(err,
459 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
460 args[0], args[1], LONGBITS);
461 return -1;
462 }
463 cur_arg++;
464 }
465 global.stats_fe->bind_proc = set;
466 }
467 else {
468 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
469 return -1;
470 }
471 return 0;
472}
473
Willy Tarreaude57a572016-11-23 17:01:39 +0100474/* Verifies that the CLI at least has a level at least as high as <level>
475 * (typically ACCESS_LVL_ADMIN). Returns 1 if OK, otherwise 0. In case of
476 * failure, an error message is prepared and the appctx's state is adjusted
477 * to print it so that a return 1 is enough to abort any processing.
478 */
479int cli_has_level(struct appctx *appctx, int level)
480{
481 struct stream_interface *si = appctx->owner;
482 struct stream *s = si_strm(si);
483
484 if (strm_li(s)->bind_conf->level < level) {
485 appctx->ctx.cli.msg = stats_permission_denied_msg;
486 appctx->st0 = STAT_CLI_PRINT;
487 return 0;
488 }
489 return 1;
490}
491
William Lallemand74c24fb2016-11-21 17:18:36 +0100492
493/* print a string of text buffer to <out>. The format is :
494 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
495 * Other non-printable chars are encoded "\xHH". Space, '\', and '=' are also escaped.
496 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
497 */
498static int dump_text(struct chunk *out, const char *buf, int bsize)
499{
500 unsigned char c;
501 int ptr = 0;
502
503 while (buf[ptr] && ptr < bsize) {
504 c = buf[ptr];
505 if (isprint(c) && isascii(c) && c != '\\' && c != ' ' && c != '=') {
506 if (out->len > out->size - 1)
507 break;
508 out->str[out->len++] = c;
509 }
510 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ' || c == '=') {
511 if (out->len > out->size - 2)
512 break;
513 out->str[out->len++] = '\\';
514 switch (c) {
515 case ' ': c = ' '; break;
516 case '\t': c = 't'; break;
517 case '\n': c = 'n'; break;
518 case '\r': c = 'r'; break;
519 case '\e': c = 'e'; break;
520 case '\\': c = '\\'; break;
521 case '=': c = '='; break;
522 }
523 out->str[out->len++] = c;
524 }
525 else {
526 if (out->len > out->size - 4)
527 break;
528 out->str[out->len++] = '\\';
529 out->str[out->len++] = 'x';
530 out->str[out->len++] = hextab[(c >> 4) & 0xF];
531 out->str[out->len++] = hextab[c & 0xF];
532 }
533 ptr++;
534 }
535
536 return ptr;
537}
538
539/* print a buffer in hexa.
540 * Print stopped if <bsize> is reached, or if no more place in the chunk.
541 */
542static int dump_binary(struct chunk *out, const char *buf, int bsize)
543{
544 unsigned char c;
545 int ptr = 0;
546
547 while (ptr < bsize) {
548 c = buf[ptr];
549
550 if (out->len > out->size - 2)
551 break;
552 out->str[out->len++] = hextab[(c >> 4) & 0xF];
553 out->str[out->len++] = hextab[c & 0xF];
554
555 ptr++;
556 }
557 return ptr;
558}
559
560/* Dump the status of a table to a stream interface's
561 * read buffer. It returns 0 if the output buffer is full
562 * and needs to be called again, otherwise non-zero.
563 */
564static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
565 struct proxy *proxy, struct proxy *target)
566{
567 struct stream *s = si_strm(si);
568
569 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
570 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
571
572 /* any other information should be dumped here */
573
574 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
575 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
576
577 if (bi_putchk(si_ic(si), msg) == -1) {
578 si_applet_cant_put(si);
579 return 0;
580 }
581
582 return 1;
583}
584
585/* Dump the a table entry to a stream interface's
586 * read buffer. It returns 0 if the output buffer is full
587 * and needs to be called again, otherwise non-zero.
588 */
589static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
590 struct proxy *proxy, struct stksess *entry)
591{
592 int dt;
593
594 chunk_appendf(msg, "%p:", entry);
595
596 if (proxy->table.type == SMP_T_IPV4) {
597 char addr[INET_ADDRSTRLEN];
598 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
599 chunk_appendf(msg, " key=%s", addr);
600 }
601 else if (proxy->table.type == SMP_T_IPV6) {
602 char addr[INET6_ADDRSTRLEN];
603 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
604 chunk_appendf(msg, " key=%s", addr);
605 }
606 else if (proxy->table.type == SMP_T_SINT) {
607 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
608 }
609 else if (proxy->table.type == SMP_T_STR) {
610 chunk_appendf(msg, " key=");
611 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
612 }
613 else {
614 chunk_appendf(msg, " key=");
615 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
616 }
617
618 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
619
620 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
621 void *ptr;
622
623 if (proxy->table.data_ofs[dt] == 0)
624 continue;
625 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
626 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
627 else
628 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
629
630 ptr = stktable_data_ptr(&proxy->table, entry, dt);
631 switch (stktable_data_types[dt].std_type) {
632 case STD_T_SINT:
633 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
634 break;
635 case STD_T_UINT:
636 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
637 break;
638 case STD_T_ULL:
639 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
640 break;
641 case STD_T_FRQP:
642 chunk_appendf(msg, "%d",
643 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
644 proxy->table.data_arg[dt].u));
645 break;
646 }
647 }
648 chunk_appendf(msg, "\n");
649
650 if (bi_putchk(si_ic(si), msg) == -1) {
651 si_applet_cant_put(si);
652 return 0;
653 }
654
655 return 1;
656}
657
658static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
659{
660 struct stream *s = si_strm(si);
661 struct appctx *appctx = __objt_appctx(si->end);
662 struct proxy *px = appctx->ctx.table.target;
663 struct stksess *ts;
664 uint32_t uint32_key;
665 unsigned char ip6_key[sizeof(struct in6_addr)];
666 long long value;
667 int data_type;
668 int cur_arg;
669 void *ptr;
670 struct freq_ctr_period *frqp;
671
672 appctx->st0 = STAT_CLI_OUTPUT;
673
674 if (!*args[4]) {
675 appctx->ctx.cli.msg = "Key value expected\n";
676 appctx->st0 = STAT_CLI_PRINT;
677 return;
678 }
679
680 switch (px->table.type) {
681 case SMP_T_IPV4:
682 uint32_key = htonl(inetaddr_host(args[4]));
683 static_table_key->key = &uint32_key;
684 break;
685 case SMP_T_IPV6:
686 inet_pton(AF_INET6, args[4], ip6_key);
687 static_table_key->key = &ip6_key;
688 break;
689 case SMP_T_SINT:
690 {
691 char *endptr;
692 unsigned long val;
693 errno = 0;
694 val = strtoul(args[4], &endptr, 10);
695 if ((errno == ERANGE && val == ULONG_MAX) ||
696 (errno != 0 && val == 0) || endptr == args[4] ||
697 val > 0xffffffff) {
698 appctx->ctx.cli.msg = "Invalid key\n";
699 appctx->st0 = STAT_CLI_PRINT;
700 return;
701 }
702 uint32_key = (uint32_t) val;
703 static_table_key->key = &uint32_key;
704 break;
705 }
706 break;
707 case SMP_T_STR:
708 static_table_key->key = args[4];
709 static_table_key->key_len = strlen(args[4]);
710 break;
711 default:
712 switch (action) {
713 case STAT_CLI_O_TAB:
714 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
715 break;
716 case STAT_CLI_O_CLR:
717 appctx->ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
718 break;
719 default:
720 appctx->ctx.cli.msg = "Unknown action\n";
721 break;
722 }
723 appctx->st0 = STAT_CLI_PRINT;
724 return;
725 }
726
727 /* check permissions */
728 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
729 appctx->ctx.cli.msg = stats_permission_denied_msg;
730 appctx->st0 = STAT_CLI_PRINT;
731 return;
732 }
733
734 ts = stktable_lookup_key(&px->table, static_table_key);
735
736 switch (action) {
737 case STAT_CLI_O_TAB:
738 if (!ts)
739 return;
740 chunk_reset(&trash);
741 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
742 return;
743 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
744 return;
745
746 case STAT_CLI_O_CLR:
747 if (!ts)
748 return;
749 if (ts->ref_cnt) {
750 /* don't delete an entry which is currently referenced */
751 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
752 appctx->st0 = STAT_CLI_PRINT;
753 return;
754 }
755 stksess_kill(&px->table, ts);
756 break;
757
758 case STAT_CLI_O_SET:
759 if (ts)
760 stktable_touch(&px->table, ts, 1);
761 else {
762 ts = stksess_new(&px->table, static_table_key);
763 if (!ts) {
764 /* don't delete an entry which is currently referenced */
765 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
766 appctx->st0 = STAT_CLI_PRINT;
767 return;
768 }
769 stktable_store(&px->table, ts, 1);
770 }
771
772 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
773 if (strncmp(args[cur_arg], "data.", 5) != 0) {
774 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
775 appctx->st0 = STAT_CLI_PRINT;
776 return;
777 }
778
779 data_type = stktable_get_data_type(args[cur_arg] + 5);
780 if (data_type < 0) {
781 appctx->ctx.cli.msg = "Unknown data type\n";
782 appctx->st0 = STAT_CLI_PRINT;
783 return;
784 }
785
786 if (!px->table.data_ofs[data_type]) {
787 appctx->ctx.cli.msg = "Data type not stored in this table\n";
788 appctx->st0 = STAT_CLI_PRINT;
789 return;
790 }
791
792 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
793 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
794 appctx->st0 = STAT_CLI_PRINT;
795 return;
796 }
797
798 ptr = stktable_data_ptr(&px->table, ts, data_type);
799
800 switch (stktable_data_types[data_type].std_type) {
801 case STD_T_SINT:
802 stktable_data_cast(ptr, std_t_sint) = value;
803 break;
804 case STD_T_UINT:
805 stktable_data_cast(ptr, std_t_uint) = value;
806 break;
807 case STD_T_ULL:
808 stktable_data_cast(ptr, std_t_ull) = value;
809 break;
810 case STD_T_FRQP:
811 /* We set both the current and previous values. That way
812 * the reported frequency is stable during all the period
813 * then slowly fades out. This allows external tools to
814 * push measures without having to update them too often.
815 */
816 frqp = &stktable_data_cast(ptr, std_t_frqp);
817 frqp->curr_tick = now_ms;
818 frqp->prev_ctr = 0;
819 frqp->curr_ctr = value;
820 break;
821 }
822 }
823 break;
824
825 default:
826 appctx->ctx.cli.msg = "Unknown action\n";
827 appctx->st0 = STAT_CLI_PRINT;
828 break;
829 }
830}
831
832static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
833{
834 struct appctx *appctx = __objt_appctx(si->end);
835
836 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
837 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
838 appctx->st0 = STAT_CLI_PRINT;
839 return;
840 }
841
842 /* condition on stored data value */
843 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
844 if (appctx->ctx.table.data_type < 0) {
845 appctx->ctx.cli.msg = "Unknown data type\n";
846 appctx->st0 = STAT_CLI_PRINT;
847 return;
848 }
849
850 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
851 appctx->ctx.cli.msg = "Data type not stored in this table\n";
852 appctx->st0 = STAT_CLI_PRINT;
853 return;
854 }
855
856 appctx->ctx.table.data_op = get_std_op(args[4]);
857 if (appctx->ctx.table.data_op < 0) {
858 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
859 appctx->st0 = STAT_CLI_PRINT;
860 return;
861 }
862
863 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
864 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
865 appctx->st0 = STAT_CLI_PRINT;
866 return;
867 }
868}
869
870static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
871{
872 struct appctx *appctx = __objt_appctx(si->end);
873
874 appctx->ctx.table.data_type = -1;
875 appctx->st2 = STAT_ST_INIT;
876 appctx->ctx.table.target = NULL;
877 appctx->ctx.table.proxy = NULL;
878 appctx->ctx.table.entry = NULL;
879 appctx->st0 = action;
880
881 if (*args[2]) {
882 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
883 if (!appctx->ctx.table.target) {
884 appctx->ctx.cli.msg = "No such table\n";
885 appctx->st0 = STAT_CLI_PRINT;
886 return;
887 }
888 }
889 else {
890 if (action != STAT_CLI_O_TAB)
891 goto err_args;
892 return;
893 }
894
895 if (strcmp(args[3], "key") == 0)
896 stats_sock_table_key_request(si, args, action);
897 else if (strncmp(args[3], "data.", 5) == 0)
898 stats_sock_table_data_request(si, args, action);
899 else if (*args[3])
900 goto err_args;
901
902 return;
903
904err_args:
905 switch (action) {
906 case STAT_CLI_O_TAB:
907 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
908 break;
909 case STAT_CLI_O_CLR:
910 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
911 break;
912 default:
913 appctx->ctx.cli.msg = "Unknown action\n";
914 break;
915 }
916 appctx->st0 = STAT_CLI_PRINT;
917}
918
919/* Expects to find a frontend named <arg> and returns it, otherwise displays various
920 * adequate error messages and returns NULL. This function also expects the stream
921 * level to be admin.
922 */
923static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
924{
925 struct appctx *appctx = __objt_appctx(si->end);
926 struct proxy *px;
927
928 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
929 appctx->ctx.cli.msg = stats_permission_denied_msg;
930 appctx->st0 = STAT_CLI_PRINT;
931 return NULL;
932 }
933
934 if (!*arg) {
935 appctx->ctx.cli.msg = "A frontend name is expected.\n";
936 appctx->st0 = STAT_CLI_PRINT;
937 return NULL;
938 }
939
940 px = proxy_fe_by_name(arg);
941 if (!px) {
942 appctx->ctx.cli.msg = "No such frontend.\n";
943 appctx->st0 = STAT_CLI_PRINT;
944 return NULL;
945 }
946 return px;
947}
948
949/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
950 * and returns the pointer to the server. Otherwise, display adequate error messages
951 * and returns NULL. This function also expects the stream level to be admin. Note:
952 * the <arg> is modified to remove the '/'.
953 */
William Lallemand222baf22016-11-19 02:00:33 +0100954struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
William Lallemand74c24fb2016-11-21 17:18:36 +0100955{
956 struct appctx *appctx = __objt_appctx(si->end);
957 struct proxy *px;
958 struct server *sv;
959 char *line;
960
961 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
962 appctx->ctx.cli.msg = stats_permission_denied_msg;
963 appctx->st0 = STAT_CLI_PRINT;
964 return NULL;
965 }
966
967 /* split "backend/server" and make <line> point to server */
968 for (line = arg; *line; line++)
969 if (*line == '/') {
970 *line++ = '\0';
971 break;
972 }
973
974 if (!*line || !*arg) {
975 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
976 appctx->st0 = STAT_CLI_PRINT;
977 return NULL;
978 }
979
980 if (!get_backend_server(arg, line, &px, &sv)) {
981 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
982 appctx->st0 = STAT_CLI_PRINT;
983 return NULL;
984 }
985
986 if (px->state == PR_STSTOPPED) {
987 appctx->ctx.cli.msg = "Proxy is disabled.\n";
988 appctx->st0 = STAT_CLI_PRINT;
989 return NULL;
990 }
991
992 return sv;
993}
994
William Lallemand74c24fb2016-11-21 17:18:36 +0100995/* Processes the stats interpreter on the statistics socket. This function is
996 * called from an applet running in a stream interface. The function returns 1
997 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
998 * designating the function which will have to process the request, which can
999 * also be the print function to display the return message set into cli.msg.
1000 */
1001static int stats_sock_parse_request(struct stream_interface *si, char *line)
1002{
1003 struct stream *s = si_strm(si);
1004 struct appctx *appctx = __objt_appctx(si->end);
1005 char *args[MAX_STATS_ARGS + 1];
1006 struct cli_kw *kw;
1007 int arg;
1008 int i, j;
1009
1010 while (isspace((unsigned char)*line))
1011 line++;
1012
1013 arg = 0;
1014 args[arg] = line;
1015
1016 while (*line && arg < MAX_STATS_ARGS) {
1017 if (*line == '\\') {
1018 line++;
1019 if (*line == '\0')
1020 break;
1021 }
1022 else if (isspace((unsigned char)*line)) {
1023 *line++ = '\0';
1024
1025 while (isspace((unsigned char)*line))
1026 line++;
1027
1028 args[++arg] = line;
1029 continue;
1030 }
1031
1032 line++;
1033 }
1034
1035 while (++arg <= MAX_STATS_ARGS)
1036 args[arg] = line;
1037
1038 /* remove \ */
1039 arg = 0;
1040 while (*args[arg] != '\0') {
1041 j = 0;
1042 for (i=0; args[arg][i] != '\0'; i++) {
1043 if (args[arg][i] == '\\')
1044 continue;
1045 args[arg][j] = args[arg][i];
1046 j++;
1047 }
1048 args[arg][j] = '\0';
1049 arg++;
1050 }
1051
1052 appctx->ctx.stats.scope_str = 0;
1053 appctx->ctx.stats.scope_len = 0;
1054 appctx->ctx.stats.flags = 0;
1055 if ((kw = cli_find_kw(args))) {
1056 if (kw->parse) {
1057 if (kw->parse(args, appctx, kw->private) == 0 && kw->io_handler) {
1058 appctx->st0 = STAT_CLI_O_CUSTOM;
1059 appctx->io_handler = kw->io_handler;
1060 appctx->io_release = kw->io_release;
1061 }
1062 }
1063 } else if (strcmp(args[0], "show") == 0) {
1064 if (strcmp(args[1], "backend") == 0) {
1065 appctx->ctx.be.px = NULL;
1066 appctx->st2 = STAT_ST_INIT;
1067 appctx->st0 = STAT_CLI_O_BACKEND;
1068 }
1069 else if (strcmp(args[1], "env") == 0) {
1070 extern char **environ;
1071
1072 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1073 appctx->ctx.cli.msg = stats_permission_denied_msg;
1074 appctx->st0 = STAT_CLI_PRINT;
1075 return 1;
1076 }
1077 appctx->ctx.env.var = environ;
1078 appctx->st2 = STAT_ST_INIT;
1079 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1080
1081 if (*args[2]) {
1082 int len = strlen(args[2]);
1083
1084 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1085 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1086 (*appctx->ctx.env.var)[len] == '=')
1087 break;
1088 }
1089 if (!*appctx->ctx.env.var) {
1090 appctx->ctx.cli.msg = "Variable not found\n";
1091 appctx->st0 = STAT_CLI_PRINT;
1092 return 1;
1093 }
1094 appctx->st2 = STAT_ST_END;
1095 }
1096 }
1097 else if (strcmp(args[1], "stat") == 0) {
William Lallemand69e96442016-11-19 00:58:54 +01001098 if (*args[2] && *args[3] && *args[4]) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001099 appctx->ctx.stats.flags |= STAT_BOUND;
1100 appctx->ctx.stats.iid = atoi(args[2]);
1101 appctx->ctx.stats.type = atoi(args[3]);
1102 appctx->ctx.stats.sid = atoi(args[4]);
1103 if (strcmp(args[5], "typed") == 0)
1104 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1105 }
1106 else if (strcmp(args[2], "typed") == 0)
1107 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1108
1109 appctx->st2 = STAT_ST_INIT;
1110 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
1111 }
1112 else if (strcmp(args[1], "info") == 0) {
1113 if (strcmp(args[2], "typed") == 0)
1114 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
1115 appctx->st2 = STAT_ST_INIT;
1116 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
1117 }
1118 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
1119 appctx->ctx.server_state.iid = 0;
1120 appctx->ctx.server_state.px = NULL;
1121 appctx->ctx.server_state.sv = NULL;
1122
1123 /* check if a backend name has been provided */
1124 if (*args[3]) {
1125 /* read server state from local file */
1126 appctx->ctx.server_state.px = proxy_be_by_name(args[3]);
1127
1128 if (!appctx->ctx.server_state.px) {
1129 appctx->ctx.cli.msg = "Can't find backend.\n";
1130 appctx->st0 = STAT_CLI_PRINT;
1131 return 1;
1132 }
1133 appctx->ctx.server_state.iid = appctx->ctx.server_state.px->uuid;
1134 }
1135 appctx->st2 = STAT_ST_INIT;
1136 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1137 return 1;
1138 }
1139 else if (strcmp(args[1], "pools") == 0) {
1140 appctx->st2 = STAT_ST_INIT;
1141 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1142 }
1143 else if (strcmp(args[1], "sess") == 0) {
1144 appctx->st2 = STAT_ST_INIT;
1145 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1146 appctx->ctx.cli.msg = stats_permission_denied_msg;
1147 appctx->st0 = STAT_CLI_PRINT;
1148 return 1;
1149 }
1150 if (*args[2] && strcmp(args[2], "all") == 0)
1151 appctx->ctx.sess.target = (void *)-1;
1152 else if (*args[2])
1153 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
1154 else
1155 appctx->ctx.sess.target = NULL;
1156 appctx->ctx.sess.section = 0; /* start with stream status */
1157 appctx->ctx.sess.pos = 0;
1158 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
1159 }
1160 else if (strcmp(args[1], "errors") == 0) {
1161 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1162 appctx->ctx.cli.msg = stats_permission_denied_msg;
1163 appctx->st0 = STAT_CLI_PRINT;
1164 return 1;
1165 }
1166 if (*args[2])
1167 appctx->ctx.errors.iid = atoi(args[2]);
1168 else
1169 appctx->ctx.errors.iid = -1;
1170 appctx->ctx.errors.px = NULL;
1171 appctx->st2 = STAT_ST_INIT;
1172 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
1173 }
1174 else if (strcmp(args[1], "table") == 0) {
1175 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
1176 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001177 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
1178 return 0;
1179 }
1180 }
1181 else if (strcmp(args[0], "clear") == 0) {
1182 if (strcmp(args[1], "counters") == 0) {
1183 struct proxy *px;
1184 struct server *sv;
1185 struct listener *li;
1186 int clrall = 0;
1187
1188 if (strcmp(args[2], "all") == 0)
1189 clrall = 1;
1190
1191 /* check permissions */
1192 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1193 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
1194 appctx->ctx.cli.msg = stats_permission_denied_msg;
1195 appctx->st0 = STAT_CLI_PRINT;
1196 return 1;
1197 }
1198
1199 for (px = proxy; px; px = px->next) {
1200 if (clrall) {
1201 memset(&px->be_counters, 0, sizeof(px->be_counters));
1202 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1203 }
1204 else {
1205 px->be_counters.conn_max = 0;
1206 px->be_counters.p.http.rps_max = 0;
1207 px->be_counters.sps_max = 0;
1208 px->be_counters.cps_max = 0;
1209 px->be_counters.nbpend_max = 0;
1210
1211 px->fe_counters.conn_max = 0;
1212 px->fe_counters.p.http.rps_max = 0;
1213 px->fe_counters.sps_max = 0;
1214 px->fe_counters.cps_max = 0;
1215 px->fe_counters.nbpend_max = 0;
1216 }
1217
1218 for (sv = px->srv; sv; sv = sv->next)
1219 if (clrall)
1220 memset(&sv->counters, 0, sizeof(sv->counters));
1221 else {
1222 sv->counters.cur_sess_max = 0;
1223 sv->counters.nbpend_max = 0;
1224 sv->counters.sps_max = 0;
1225 }
1226
1227 list_for_each_entry(li, &px->conf.listeners, by_fe)
1228 if (li->counters) {
1229 if (clrall)
1230 memset(li->counters, 0, sizeof(*li->counters));
1231 else
1232 li->counters->conn_max = 0;
1233 }
1234 }
1235
1236 global.cps_max = 0;
1237 global.sps_max = 0;
1238 return 1;
1239 }
1240 else if (strcmp(args[1], "table") == 0) {
1241 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
1242 /* end of processing */
1243 return 1;
1244 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001245 else {
1246 /* unknown "clear" argument */
1247 return 0;
1248 }
1249 }
1250 else if (strcmp(args[0], "get") == 0) {
1251 if (strcmp(args[1], "weight") == 0) {
1252 struct proxy *px;
1253 struct server *sv;
1254
1255 /* split "backend/server" and make <line> point to server */
1256 for (line = args[2]; *line; line++)
1257 if (*line == '/') {
1258 *line++ = '\0';
1259 break;
1260 }
1261
1262 if (!*line) {
1263 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1264 appctx->st0 = STAT_CLI_PRINT;
1265 return 1;
1266 }
1267
1268 if (!get_backend_server(args[2], line, &px, &sv)) {
1269 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1270 appctx->st0 = STAT_CLI_PRINT;
1271 return 1;
1272 }
1273
1274 /* return server's effective weight at the moment */
1275 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1276 if (bi_putstr(si_ic(si), trash.str) == -1)
1277 si_applet_cant_put(si);
1278
1279 return 1;
1280 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001281 else { /* not "get weight" */
1282 return 0;
1283 }
1284 }
1285 else if (strcmp(args[0], "set") == 0) {
1286 if (strcmp(args[1], "weight") == 0) {
1287 struct server *sv;
1288 const char *warning;
1289
1290 sv = expect_server_admin(s, si, args[2]);
1291 if (!sv)
1292 return 1;
1293
1294 warning = server_parse_weight_change_request(sv, args[3]);
1295 if (warning) {
1296 appctx->ctx.cli.msg = warning;
1297 appctx->st0 = STAT_CLI_PRINT;
1298 }
1299 return 1;
1300 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001301 else if (strcmp(args[1], "timeout") == 0) {
1302 if (strcmp(args[2], "cli") == 0) {
1303 unsigned timeout;
1304 const char *res;
1305
1306 if (!*args[3]) {
1307 appctx->ctx.cli.msg = "Expects an integer value.\n";
1308 appctx->st0 = STAT_CLI_PRINT;
1309 return 1;
1310 }
1311
1312 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1313 if (res || timeout < 1) {
1314 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1315 appctx->st0 = STAT_CLI_PRINT;
1316 return 1;
1317 }
1318
1319 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
1320 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
1321 return 1;
1322 }
1323 else {
1324 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1325 appctx->st0 = STAT_CLI_PRINT;
1326 return 1;
1327 }
1328 }
1329 else if (strcmp(args[1], "maxconn") == 0) {
1330 if (strcmp(args[2], "frontend") == 0) {
1331 struct proxy *px;
1332 struct listener *l;
1333 int v;
1334
1335 px = expect_frontend_admin(s, si, args[3]);
1336 if (!px)
1337 return 1;
1338
1339 if (!*args[4]) {
1340 appctx->ctx.cli.msg = "Integer value expected.\n";
1341 appctx->st0 = STAT_CLI_PRINT;
1342 return 1;
1343 }
1344
1345 v = atoi(args[4]);
1346 if (v < 0) {
1347 appctx->ctx.cli.msg = "Value out of range.\n";
1348 appctx->st0 = STAT_CLI_PRINT;
1349 return 1;
1350 }
1351
1352 /* OK, the value is fine, so we assign it to the proxy and to all of
1353 * its listeners. The blocked ones will be dequeued.
1354 */
1355 px->maxconn = v;
1356 list_for_each_entry(l, &px->conf.listeners, by_fe) {
1357 l->maxconn = v;
1358 if (l->state == LI_FULL)
1359 resume_listener(l);
1360 }
1361
1362 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&px->listener_queue))
1363 dequeue_all_listeners(&px->listener_queue);
1364
1365 return 1;
1366 }
1367 else if (strcmp(args[2], "server") == 0) {
1368 struct server *sv;
1369 const char *warning;
1370
1371 sv = expect_server_admin(s, si, args[3]);
1372 if (!sv)
1373 return 1;
1374
1375 warning = server_parse_maxconn_change_request(sv, args[4]);
1376 if (warning) {
1377 appctx->ctx.cli.msg = warning;
1378 appctx->st0 = STAT_CLI_PRINT;
1379 }
1380
1381 return 1;
1382 }
1383 else if (strcmp(args[2], "global") == 0) {
1384 int v;
1385
1386 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1387 appctx->ctx.cli.msg = stats_permission_denied_msg;
1388 appctx->st0 = STAT_CLI_PRINT;
1389 return 1;
1390 }
1391
1392 if (!*args[3]) {
1393 appctx->ctx.cli.msg = "Expects an integer value.\n";
1394 appctx->st0 = STAT_CLI_PRINT;
1395 return 1;
1396 }
1397
1398 v = atoi(args[3]);
1399 if (v > global.hardmaxconn) {
1400 appctx->ctx.cli.msg = "Value out of range.\n";
1401 appctx->st0 = STAT_CLI_PRINT;
1402 return 1;
1403 }
1404
1405 /* check for unlimited values */
1406 if (v <= 0)
1407 v = global.hardmaxconn;
1408
1409 global.maxconn = v;
1410
1411 /* Dequeues all of the listeners waiting for a resource */
1412 if (!LIST_ISEMPTY(&global_listener_queue))
1413 dequeue_all_listeners(&global_listener_queue);
1414
1415 return 1;
1416 }
1417 else {
1418 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
1419 appctx->st0 = STAT_CLI_PRINT;
1420 return 1;
1421 }
1422 }
1423 else if (strcmp(args[1], "rate-limit") == 0) {
1424 if (strcmp(args[2], "connections") == 0) {
1425 if (strcmp(args[3], "global") == 0) {
1426 int v;
1427
1428 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1429 appctx->ctx.cli.msg = stats_permission_denied_msg;
1430 appctx->st0 = STAT_CLI_PRINT;
1431 return 1;
1432 }
1433
1434 if (!*args[4]) {
1435 appctx->ctx.cli.msg = "Expects an integer value.\n";
1436 appctx->st0 = STAT_CLI_PRINT;
1437 return 1;
1438 }
1439
1440 v = atoi(args[4]);
1441 if (v < 0) {
1442 appctx->ctx.cli.msg = "Value out of range.\n";
1443 appctx->st0 = STAT_CLI_PRINT;
1444 return 1;
1445 }
1446
1447 global.cps_lim = v;
1448
1449 /* Dequeues all of the listeners waiting for a resource */
1450 if (!LIST_ISEMPTY(&global_listener_queue))
1451 dequeue_all_listeners(&global_listener_queue);
1452
1453 return 1;
1454 }
1455 else {
1456 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1457 appctx->st0 = STAT_CLI_PRINT;
1458 return 1;
1459 }
1460 }
1461 else if (strcmp(args[2], "sessions") == 0) {
1462 if (strcmp(args[3], "global") == 0) {
1463 int v;
1464
1465 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1466 appctx->ctx.cli.msg = stats_permission_denied_msg;
1467 appctx->st0 = STAT_CLI_PRINT;
1468 return 1;
1469 }
1470
1471 if (!*args[4]) {
1472 appctx->ctx.cli.msg = "Expects an integer value.\n";
1473 appctx->st0 = STAT_CLI_PRINT;
1474 return 1;
1475 }
1476
1477 v = atoi(args[4]);
1478 if (v < 0) {
1479 appctx->ctx.cli.msg = "Value out of range.\n";
1480 appctx->st0 = STAT_CLI_PRINT;
1481 return 1;
1482 }
1483
1484 global.sps_lim = v;
1485
1486 /* Dequeues all of the listeners waiting for a resource */
1487 if (!LIST_ISEMPTY(&global_listener_queue))
1488 dequeue_all_listeners(&global_listener_queue);
1489
1490 return 1;
1491 }
1492 else {
1493 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1494 appctx->st0 = STAT_CLI_PRINT;
1495 return 1;
1496 }
1497 }
1498#ifdef USE_OPENSSL
1499 else if (strcmp(args[2], "ssl-sessions") == 0) {
1500 if (strcmp(args[3], "global") == 0) {
1501 int v;
1502
1503 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1504 appctx->ctx.cli.msg = stats_permission_denied_msg;
1505 appctx->st0 = STAT_CLI_PRINT;
1506 return 1;
1507 }
1508
1509 if (!*args[4]) {
1510 appctx->ctx.cli.msg = "Expects an integer value.\n";
1511 appctx->st0 = STAT_CLI_PRINT;
1512 return 1;
1513 }
1514
1515 v = atoi(args[4]);
1516 if (v < 0) {
1517 appctx->ctx.cli.msg = "Value out of range.\n";
1518 appctx->st0 = STAT_CLI_PRINT;
1519 return 1;
1520 }
1521
1522 global.ssl_lim = v;
1523
1524 /* Dequeues all of the listeners waiting for a resource */
1525 if (!LIST_ISEMPTY(&global_listener_queue))
1526 dequeue_all_listeners(&global_listener_queue);
1527
1528 return 1;
1529 }
1530 else {
1531 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1532 appctx->st0 = STAT_CLI_PRINT;
1533 return 1;
1534 }
1535 }
1536#endif
1537 else if (strcmp(args[2], "http-compression") == 0) {
1538 if (strcmp(args[3], "global") == 0) {
1539 int v;
1540
1541 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1542 appctx->ctx.cli.msg = stats_permission_denied_msg;
1543 appctx->st0 = STAT_CLI_PRINT;
1544 return 1;
1545 }
1546
1547 if (!*args[4]) {
1548 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1549 appctx->st0 = STAT_CLI_PRINT;
1550 return 1;
1551 }
1552
1553 v = atoi(args[4]);
1554 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1555 }
1556 else {
1557 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1558 appctx->st0 = STAT_CLI_PRINT;
1559 return 1;
1560 }
1561 }
1562 else {
1563 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
1564 appctx->st0 = STAT_CLI_PRINT;
1565 return 1;
1566 }
1567 }
1568 else if (strcmp(args[1], "table") == 0) {
1569 stats_sock_table_request(si, args, STAT_CLI_O_SET);
William Lallemand32af2032016-10-29 18:09:35 +02001570 } else { /* unknown "set" parameter */
William Lallemand74c24fb2016-11-21 17:18:36 +01001571 return 0;
1572 }
1573 }
1574 else if (strcmp(args[0], "enable") == 0) {
1575 if (strcmp(args[1], "agent") == 0) {
1576 struct server *sv;
1577
1578 sv = expect_server_admin(s, si, args[2]);
1579 if (!sv)
1580 return 1;
1581
1582 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1583 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1584 appctx->st0 = STAT_CLI_PRINT;
1585 return 1;
1586 }
1587
1588 sv->agent.state |= CHK_ST_ENABLED;
1589 return 1;
1590 }
1591 else if (strcmp(args[1], "health") == 0) {
1592 struct server *sv;
1593
1594 sv = expect_server_admin(s, si, args[2]);
1595 if (!sv)
1596 return 1;
1597
1598 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1599 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1600 appctx->st0 = STAT_CLI_PRINT;
1601 return 1;
1602 }
1603
1604 sv->check.state |= CHK_ST_ENABLED;
1605 return 1;
1606 }
1607 else if (strcmp(args[1], "server") == 0) {
1608 struct server *sv;
1609
1610 sv = expect_server_admin(s, si, args[2]);
1611 if (!sv)
1612 return 1;
1613
1614 srv_adm_set_ready(sv);
1615 return 1;
1616 }
1617 else if (strcmp(args[1], "frontend") == 0) {
1618 struct proxy *px;
1619
1620 px = expect_frontend_admin(s, si, args[2]);
1621 if (!px)
1622 return 1;
1623
1624 if (px->state == PR_STSTOPPED) {
1625 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1626 appctx->st0 = STAT_CLI_PRINT;
1627 return 1;
1628 }
1629
1630 if (px->state != PR_STPAUSED) {
1631 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1632 appctx->st0 = STAT_CLI_PRINT;
1633 return 1;
1634 }
1635
1636 if (!resume_proxy(px)) {
1637 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1638 appctx->st0 = STAT_CLI_PRINT;
1639 return 1;
1640 }
1641 return 1;
1642 }
1643 else { /* unknown "enable" parameter */
1644 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
1645 appctx->st0 = STAT_CLI_PRINT;
1646 return 1;
1647 }
1648 }
1649 else if (strcmp(args[0], "disable") == 0) {
1650 if (strcmp(args[1], "agent") == 0) {
1651 struct server *sv;
1652
1653 sv = expect_server_admin(s, si, args[2]);
1654 if (!sv)
1655 return 1;
1656
1657 sv->agent.state &= ~CHK_ST_ENABLED;
1658 return 1;
1659 }
1660 else if (strcmp(args[1], "health") == 0) {
1661 struct server *sv;
1662
1663 sv = expect_server_admin(s, si, args[2]);
1664 if (!sv)
1665 return 1;
1666
1667 sv->check.state &= ~CHK_ST_ENABLED;
1668 return 1;
1669 }
1670 else if (strcmp(args[1], "server") == 0) {
1671 struct server *sv;
1672
1673 sv = expect_server_admin(s, si, args[2]);
1674 if (!sv)
1675 return 1;
1676
1677 srv_adm_set_maint(sv);
1678 return 1;
1679 }
1680 else if (strcmp(args[1], "frontend") == 0) {
1681 struct proxy *px;
1682
1683 px = expect_frontend_admin(s, si, args[2]);
1684 if (!px)
1685 return 1;
1686
1687 if (px->state == PR_STSTOPPED) {
1688 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1689 appctx->st0 = STAT_CLI_PRINT;
1690 return 1;
1691 }
1692
1693 if (px->state == PR_STPAUSED) {
1694 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1695 appctx->st0 = STAT_CLI_PRINT;
1696 return 1;
1697 }
1698
1699 if (!pause_proxy(px)) {
1700 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1701 appctx->st0 = STAT_CLI_PRINT;
1702 return 1;
1703 }
1704 return 1;
1705 }
1706 else { /* unknown "disable" parameter */
1707 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
1708 appctx->st0 = STAT_CLI_PRINT;
1709 return 1;
1710 }
1711 }
1712 else if (strcmp(args[0], "shutdown") == 0) {
1713 if (strcmp(args[1], "frontend") == 0) {
1714 struct proxy *px;
1715
1716 px = expect_frontend_admin(s, si, args[2]);
1717 if (!px)
1718 return 1;
1719
1720 if (px->state == PR_STSTOPPED) {
1721 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1722 appctx->st0 = STAT_CLI_PRINT;
1723 return 1;
1724 }
1725
1726 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1727 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1728 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1729 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1730 stop_proxy(px);
1731 return 1;
1732 }
1733 else if (strcmp(args[1], "session") == 0) {
1734 struct stream *sess, *ptr;
1735
1736 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1737 appctx->ctx.cli.msg = stats_permission_denied_msg;
1738 appctx->st0 = STAT_CLI_PRINT;
1739 return 1;
1740 }
1741
1742 if (!*args[2]) {
1743 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1744 appctx->st0 = STAT_CLI_PRINT;
1745 return 1;
1746 }
1747
1748 ptr = (void *)strtoul(args[2], NULL, 0);
1749
1750 /* first, look for the requested stream in the stream table */
1751 list_for_each_entry(sess, &streams, list) {
1752 if (sess == ptr)
1753 break;
1754 }
1755
1756 /* do we have the stream ? */
1757 if (sess != ptr) {
1758 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1759 appctx->st0 = STAT_CLI_PRINT;
1760 return 1;
1761 }
1762
1763 stream_shutdown(sess, SF_ERR_KILLED);
1764 return 1;
1765 }
1766 else if (strcmp(args[1], "sessions") == 0) {
1767 if (strcmp(args[2], "server") == 0) {
1768 struct server *sv;
1769 struct stream *sess, *sess_bck;
1770
1771 sv = expect_server_admin(s, si, args[3]);
1772 if (!sv)
1773 return 1;
1774
1775 /* kill all the stream that are on this server */
1776 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1777 if (sess->srv_conn == sv)
1778 stream_shutdown(sess, SF_ERR_KILLED);
1779
1780 return 1;
1781 }
1782 else {
1783 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1784 appctx->st0 = STAT_CLI_PRINT;
1785 return 1;
1786 }
1787 }
1788 else { /* unknown "disable" parameter */
1789 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1790 appctx->st0 = STAT_CLI_PRINT;
1791 return 1;
1792 }
1793 }
William Lallemand74c24fb2016-11-21 17:18:36 +01001794 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
1795 return 0;
1796 }
1797 return 1;
1798}
1799
1800/* This I/O handler runs as an applet embedded in a stream interface. It is
1801 * used to processes I/O from/to the stats unix socket. The system relies on a
1802 * state machine handling requests and various responses. We read a request,
1803 * then we process it and send the response, and we possibly display a prompt.
1804 * Then we can read again. The state is stored in appctx->st0 and is one of the
1805 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
1806 * or not.
1807 */
1808static void cli_io_handler(struct appctx *appctx)
1809{
1810 struct stream_interface *si = appctx->owner;
1811 struct channel *req = si_oc(si);
1812 struct channel *res = si_ic(si);
1813 int reql;
1814 int len;
1815
1816 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1817 goto out;
1818
1819 while (1) {
1820 if (appctx->st0 == STAT_CLI_INIT) {
1821 /* Stats output not initialized yet */
1822 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1823 appctx->st0 = STAT_CLI_GETREQ;
1824 }
1825 else if (appctx->st0 == STAT_CLI_END) {
1826 /* Let's close for real now. We just close the request
1827 * side, the conditions below will complete if needed.
1828 */
1829 si_shutw(si);
1830 break;
1831 }
1832 else if (appctx->st0 == STAT_CLI_GETREQ) {
1833 /* ensure we have some output room left in the event we
1834 * would want to return some info right after parsing.
1835 */
1836 if (buffer_almost_full(si_ib(si))) {
1837 si_applet_cant_put(si);
1838 break;
1839 }
1840
1841 reql = bo_getline(si_oc(si), trash.str, trash.size);
1842 if (reql <= 0) { /* closed or EOL not found */
1843 if (reql == 0)
1844 break;
1845 appctx->st0 = STAT_CLI_END;
1846 continue;
1847 }
1848
1849 /* seek for a possible unescaped semi-colon. If we find
1850 * one, we replace it with an LF and skip only this part.
1851 */
1852 for (len = 0; len < reql; len++) {
1853 if (trash.str[len] == '\\') {
1854 len++;
1855 continue;
1856 }
1857 if (trash.str[len] == ';') {
1858 trash.str[len] = '\n';
1859 reql = len + 1;
1860 break;
1861 }
1862 }
1863
1864 /* now it is time to check that we have a full line,
1865 * remove the trailing \n and possibly \r, then cut the
1866 * line.
1867 */
1868 len = reql - 1;
1869 if (trash.str[len] != '\n') {
1870 appctx->st0 = STAT_CLI_END;
1871 continue;
1872 }
1873
1874 if (len && trash.str[len-1] == '\r')
1875 len--;
1876
1877 trash.str[len] = '\0';
1878
1879 appctx->st0 = STAT_CLI_PROMPT;
1880 if (len) {
1881 if (strcmp(trash.str, "quit") == 0) {
1882 appctx->st0 = STAT_CLI_END;
1883 continue;
1884 }
1885 else if (strcmp(trash.str, "prompt") == 0)
1886 appctx->st1 = !appctx->st1;
1887 else if (strcmp(trash.str, "help") == 0 ||
1888 !stats_sock_parse_request(si, trash.str)) {
1889 cli_gen_usage_msg();
1890 if (dynamic_usage_msg)
1891 appctx->ctx.cli.msg = dynamic_usage_msg;
1892 else
1893 appctx->ctx.cli.msg = stats_sock_usage_msg;
1894 appctx->st0 = STAT_CLI_PRINT;
1895 }
1896 /* NB: stats_sock_parse_request() may have put
1897 * another STAT_CLI_O_* into appctx->st0.
1898 */
1899 }
1900 else if (!appctx->st1) {
1901 /* if prompt is disabled, print help on empty lines,
1902 * so that the user at least knows how to enable
1903 * prompt and find help.
1904 */
1905 cli_gen_usage_msg();
1906 if (dynamic_usage_msg)
1907 appctx->ctx.cli.msg = dynamic_usage_msg;
1908 else
1909 appctx->ctx.cli.msg = stats_sock_usage_msg;
1910 appctx->st0 = STAT_CLI_PRINT;
1911 }
1912
1913 /* re-adjust req buffer */
1914 bo_skip(si_oc(si), reql);
1915 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
1916 }
1917 else { /* output functions */
1918 switch (appctx->st0) {
1919 case STAT_CLI_PROMPT:
1920 break;
1921 case STAT_CLI_PRINT:
1922 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
1923 appctx->st0 = STAT_CLI_PROMPT;
1924 else
1925 si_applet_cant_put(si);
1926 break;
1927 case STAT_CLI_PRINT_FREE:
1928 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
1929 free(appctx->ctx.cli.err);
1930 appctx->st0 = STAT_CLI_PROMPT;
1931 }
1932 else
1933 si_applet_cant_put(si);
1934 break;
1935 case STAT_CLI_O_BACKEND:
1936 if (stats_dump_backend_to_buffer(si))
1937 appctx->st0 = STAT_CLI_PROMPT;
1938 break;
1939 case STAT_CLI_O_INFO:
1940 if (stats_dump_info_to_buffer(si))
1941 appctx->st0 = STAT_CLI_PROMPT;
1942 break;
1943 case STAT_CLI_O_SERVERS_STATE:
1944 if (stats_dump_servers_state_to_buffer(si))
1945 appctx->st0 = STAT_CLI_PROMPT;
1946 break;
1947 case STAT_CLI_O_STAT:
1948 if (stats_dump_stat_to_buffer(si, NULL))
1949 appctx->st0 = STAT_CLI_PROMPT;
1950 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001951 case STAT_CLI_O_SESS:
1952 if (stats_dump_sess_to_buffer(si))
1953 appctx->st0 = STAT_CLI_PROMPT;
1954 break;
1955 case STAT_CLI_O_ERR: /* errors dump */
1956 if (stats_dump_errors_to_buffer(si))
1957 appctx->st0 = STAT_CLI_PROMPT;
1958 break;
1959 case STAT_CLI_O_TAB:
1960 case STAT_CLI_O_CLR:
1961 if (stats_table_request(si, appctx->st0))
1962 appctx->st0 = STAT_CLI_PROMPT;
1963 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001964 case STAT_CLI_O_POOLS:
1965 if (stats_dump_pools_to_buffer(si))
1966 appctx->st0 = STAT_CLI_PROMPT;
1967 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01001968 case STAT_CLI_O_ENV: /* environment dump */
1969 if (stats_dump_env_to_buffer(si))
1970 appctx->st0 = STAT_CLI_PROMPT;
1971 break;
1972 case STAT_CLI_O_CUSTOM: /* use custom pointer */
1973 if (appctx->io_handler)
1974 if (appctx->io_handler(appctx)) {
1975 appctx->st0 = STAT_CLI_PROMPT;
1976 if (appctx->io_release) {
1977 appctx->io_release(appctx);
1978 appctx->io_release = NULL;
1979 }
1980 }
1981 break;
1982 default: /* abnormal state */
1983 si->flags |= SI_FL_ERR;
1984 break;
1985 }
1986
1987 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
1988 if (appctx->st0 == STAT_CLI_PROMPT) {
1989 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
1990 appctx->st0 = STAT_CLI_GETREQ;
1991 else
1992 si_applet_cant_put(si);
1993 }
1994
1995 /* If the output functions are still there, it means they require more room. */
1996 if (appctx->st0 >= STAT_CLI_OUTPUT)
1997 break;
1998
1999 /* Now we close the output if one of the writers did so,
2000 * or if we're not in interactive mode and the request
2001 * buffer is empty. This still allows pipelined requests
2002 * to be sent in non-interactive mode.
2003 */
2004 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2005 appctx->st0 = STAT_CLI_END;
2006 continue;
2007 }
2008
2009 /* switch state back to GETREQ to read next requests */
2010 appctx->st0 = STAT_CLI_GETREQ;
2011 }
2012 }
2013
2014 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
2015 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2016 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2017 /* Other side has closed, let's abort if we have no more processing to do
2018 * and nothing more to consume. This is comparable to a broken pipe, so
2019 * we forward the close to the request side so that it flows upstream to
2020 * the client.
2021 */
2022 si_shutw(si);
2023 }
2024
2025 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
2026 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2027 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2028 /* We have no more processing to do, and nothing more to send, and
2029 * the client side has closed. So we'll forward this state downstream
2030 * on the response buffer.
2031 */
2032 si_shutr(si);
2033 res->flags |= CF_READ_NULL;
2034 }
2035
2036 out:
2037 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
2038 __FUNCTION__, __LINE__,
2039 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
2040}
2041
2042/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2043static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2044{
2045 int field;
2046
2047 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2048 if (!field_format(info, field))
2049 continue;
2050
2051 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2052 return 0;
2053 if (!stats_emit_raw_data_field(out, &info[field]))
2054 return 0;
2055 if (!chunk_strcat(out, "\n"))
2056 return 0;
2057 }
2058 return 1;
2059}
2060
2061/* Dump all fields from <info> into <out> using the "show info typed" format */
2062static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
2063{
2064 int field;
2065
2066 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2067 if (!field_format(info, field))
2068 continue;
2069
2070 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
2071 return 0;
2072 if (!stats_emit_field_tags(out, &info[field], ':'))
2073 return 0;
2074 if (!stats_emit_typed_data_field(out, &info[field]))
2075 return 0;
2076 if (!chunk_strcat(out, "\n"))
2077 return 0;
2078 }
2079 return 1;
2080}
2081
2082/* Fill <info> with HAProxy global info. <info> is preallocated
2083 * array of length <len>. The length of the aray must be
2084 * INF_TOTAL_FIELDS. If this length is less then this value, the
2085 * function returns 0, otherwise, it returns 1.
2086 */
2087int stats_fill_info(struct field *info, int len)
2088{
2089 unsigned int up = (now.tv_sec - start_date.tv_sec);
2090 struct chunk *out = get_trash_chunk();
2091
2092#ifdef USE_OPENSSL
2093 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2094 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2095 int ssl_reuse = 0;
2096
2097 if (ssl_key_rate < ssl_sess_rate) {
2098 /* count the ssl reuse ratio and avoid overflows in both directions */
2099 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2100 }
2101#endif
2102
2103 if (len < INF_TOTAL_FIELDS)
2104 return 0;
2105
2106 chunk_reset(out);
2107 memset(info, 0, sizeof(*info) * len);
2108
2109 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
2110 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
2111 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
2112
2113 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
2114 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
2115 info[INF_PID] = mkf_u32(FO_STATUS, pid);
2116
2117 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
2118 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
2119
2120 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
2121 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
2122 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
2123 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
2124 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
2125 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
2126 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
2127 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
2128 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
2129 info[INF_CURR_CONN] = mkf_u32(0, actconn);
2130 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
2131 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
2132#ifdef USE_OPENSSL
2133 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
2134 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
2135 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
2136#endif
2137 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
2138 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
2139 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
2140 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
2141 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
2142 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
2143 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
2144 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
2145 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
2146
2147#ifdef USE_OPENSSL
2148 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
2149 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
2150 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
2151 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
2152 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
2153 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
2154 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
2155 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
2156 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
2157 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
2158#endif
2159 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
2160 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
2161 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
2162#ifdef USE_ZLIB
2163 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
2164 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
2165#endif
2166 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
2167 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
2168 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
2169 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
2170 if (global.desc)
2171 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
2172
2173 return 1;
2174}
2175
2176/* This function dumps information onto the stream interface's read buffer.
2177 * It returns 0 as long as it does not complete, non-zero upon completion.
2178 * No state is used.
2179 */
2180static int stats_dump_info_to_buffer(struct stream_interface *si)
2181{
2182 struct appctx *appctx = __objt_appctx(si->end);
2183
2184 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
2185 return 0;
2186
2187 chunk_reset(&trash);
2188
2189 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
2190 stats_dump_typed_info_fields(&trash, info);
2191 else
2192 stats_dump_info_fields(&trash, info);
2193
2194 if (bi_putchk(si_ic(si), &trash) == -1) {
2195 si_applet_cant_put(si);
2196 return 0;
2197 }
2198
2199 return 1;
2200}
2201
2202/* dumps server state information into <buf> for all the servers found in <backend>
2203 * These information are all the parameters which may change during HAProxy runtime.
2204 * By default, we only export to the last known server state file format.
2205 * These information can be used at next startup to recover same level of server state.
2206 */
2207static int dump_servers_state(struct stream_interface *si, struct chunk *buf)
2208{
2209 struct appctx *appctx = __objt_appctx(si->end);
2210 struct server *srv;
2211 char srv_addr[INET6_ADDRSTRLEN + 1];
2212 time_t srv_time_since_last_change;
2213 int bk_f_forced_id, srv_f_forced_id;
2214
2215
2216 /* we don't want to report any state if the backend is not enabled on this process */
2217 if (appctx->ctx.server_state.px->bind_proc && !(appctx->ctx.server_state.px->bind_proc & (1UL << (relative_pid - 1))))
2218 return 1;
2219
2220 if (!appctx->ctx.server_state.sv)
2221 appctx->ctx.server_state.sv = appctx->ctx.server_state.px->srv;
2222
2223 for (; appctx->ctx.server_state.sv != NULL; appctx->ctx.server_state.sv = srv->next) {
2224 srv = appctx->ctx.server_state.sv;
2225 srv_addr[0] = '\0';
2226
2227 switch (srv->addr.ss_family) {
2228 case AF_INET:
2229 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
2230 srv_addr, INET_ADDRSTRLEN + 1);
2231 break;
2232 case AF_INET6:
2233 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
2234 srv_addr, INET6_ADDRSTRLEN + 1);
2235 break;
2236 }
2237 srv_time_since_last_change = now.tv_sec - srv->last_change;
2238 bk_f_forced_id = appctx->ctx.server_state.px->options & PR_O_FORCED_ID ? 1 : 0;
2239 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
2240
2241 chunk_appendf(buf,
2242 "%d %s "
2243 "%d %s %s "
2244 "%d %d %d %d %ld "
2245 "%d %d %d %d %d "
2246 "%d %d"
2247 "\n",
2248 appctx->ctx.server_state.px->uuid, appctx->ctx.server_state.px->id,
2249 srv->puid, srv->id, srv_addr,
2250 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
2251 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
2252 bk_f_forced_id, srv_f_forced_id);
2253 if (bi_putchk(si_ic(si), &trash) == -1) {
2254 si_applet_cant_put(si);
2255 return 0;
2256 }
2257 }
2258 return 1;
2259}
2260
2261/* Parses backend list and simply report backend names */
2262static int stats_dump_backend_to_buffer(struct stream_interface *si)
2263{
2264 struct appctx *appctx = __objt_appctx(si->end);
2265 extern struct proxy *proxy;
2266 struct proxy *curproxy;
2267
2268 chunk_reset(&trash);
2269
2270 if (!appctx->ctx.be.px) {
2271 chunk_printf(&trash, "# name\n");
2272 if (bi_putchk(si_ic(si), &trash) == -1) {
2273 si_applet_cant_put(si);
2274 return 0;
2275 }
2276 appctx->ctx.be.px = proxy;
2277 }
2278
2279 for (; appctx->ctx.be.px != NULL; appctx->ctx.be.px = curproxy->next) {
2280 curproxy = appctx->ctx.be.px;
2281
2282 /* looking for backends only */
2283 if (!(curproxy->cap & PR_CAP_BE))
2284 continue;
2285
2286 /* we don't want to list a backend which is bound to this process */
2287 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
2288 continue;
2289
2290 chunk_appendf(&trash, "%s\n", curproxy->id);
2291 if (bi_putchk(si_ic(si), &trash) == -1) {
2292 si_applet_cant_put(si);
2293 return 0;
2294 }
2295 }
2296
2297 return 1;
2298}
2299
2300/* Parses backend list or simply use backend name provided by the user to return
2301 * states of servers to stdout.
2302 */
2303static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
2304{
2305 struct appctx *appctx = __objt_appctx(si->end);
2306 extern struct proxy *proxy;
2307 struct proxy *curproxy;
2308
2309 chunk_reset(&trash);
2310
2311 if (appctx->st2 == STAT_ST_INIT) {
2312 if (!appctx->ctx.server_state.px)
2313 appctx->ctx.server_state.px = proxy;
2314 appctx->st2 = STAT_ST_HEAD;
2315 }
2316
2317 if (appctx->st2 == STAT_ST_HEAD) {
2318 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
2319 if (bi_putchk(si_ic(si), &trash) == -1) {
2320 si_applet_cant_put(si);
2321 return 0;
2322 }
2323 appctx->st2 = STAT_ST_INFO;
2324 }
2325
2326 /* STAT_ST_INFO */
2327 for (; appctx->ctx.server_state.px != NULL; appctx->ctx.server_state.px = curproxy->next) {
2328 curproxy = appctx->ctx.server_state.px;
2329 /* servers are only in backends */
2330 if (curproxy->cap & PR_CAP_BE) {
2331 if (!dump_servers_state(si, &trash))
2332 return 0;
2333
2334 if (bi_putchk(si_ic(si), &trash) == -1) {
2335 si_applet_cant_put(si);
2336 return 0;
2337 }
2338 }
2339 /* only the selected proxy is dumped */
2340 if (appctx->ctx.server_state.iid)
2341 break;
2342 }
2343
2344 return 1;
2345}
2346
2347/* This function dumps memory usage information onto the stream interface's
2348 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2349 * completion. No state is used.
2350 */
2351static int stats_dump_pools_to_buffer(struct stream_interface *si)
2352{
2353 dump_pools_to_trash();
2354 if (bi_putchk(si_ic(si), &trash) == -1) {
2355 si_applet_cant_put(si);
2356 return 0;
2357 }
2358 return 1;
2359}
2360
2361static inline const char *get_conn_ctrl_name(const struct connection *conn)
2362{
2363 if (!conn_ctrl_ready(conn))
2364 return "NONE";
2365 return conn->ctrl->name;
2366}
2367
2368static inline const char *get_conn_xprt_name(const struct connection *conn)
2369{
2370 static char ptr[19];
2371
2372 if (!conn_xprt_ready(conn))
2373 return "NONE";
2374
2375 if (conn->xprt == &raw_sock)
2376 return "RAW";
2377
2378#ifdef USE_OPENSSL
2379 if (conn->xprt == &ssl_sock)
2380 return "SSL";
2381#endif
2382 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
2383 return ptr;
2384}
2385
2386static inline const char *get_conn_data_name(const struct connection *conn)
2387{
2388 static char ptr[19];
2389
2390 if (!conn->data)
2391 return "NONE";
2392
2393 if (conn->data == &sess_conn_cb)
2394 return "SESS";
2395
2396 if (conn->data == &si_conn_cb)
2397 return "STRM";
2398
2399 if (conn->data == &check_conn_cb)
2400 return "CHCK";
2401
2402 snprintf(ptr, sizeof(ptr), "%p", conn->data);
2403 return ptr;
2404}
2405
2406/* This function dumps a complete stream state onto the stream interface's
2407 * read buffer. The stream has to be set in sess->target. It returns
2408 * 0 if the output buffer is full and it needs to be called again, otherwise
2409 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
2410 */
2411static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
2412{
2413 struct appctx *appctx = __objt_appctx(si->end);
2414 struct tm tm;
2415 extern const char *monthname[12];
2416 char pn[INET6_ADDRSTRLEN];
2417 struct connection *conn;
2418 struct appctx *tmpctx;
2419
2420 chunk_reset(&trash);
2421
2422 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
2423 /* stream changed, no need to go any further */
2424 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
2425 if (bi_putchk(si_ic(si), &trash) == -1) {
2426 si_applet_cant_put(si);
2427 return 0;
2428 }
2429 appctx->ctx.sess.uid = 0;
2430 appctx->ctx.sess.section = 0;
2431 return 1;
2432 }
2433
2434 switch (appctx->ctx.sess.section) {
2435 case 0: /* main status of the stream */
2436 appctx->ctx.sess.uid = sess->uniq_id;
2437 appctx->ctx.sess.section = 1;
2438 /* fall through */
2439
2440 case 1:
2441 get_localtime(sess->logs.accept_date.tv_sec, &tm);
2442 chunk_appendf(&trash,
2443 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
2444 sess,
2445 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2446 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
2447 sess->uniq_id,
2448 strm_li(sess) ? strm_li(sess)->proto->name : "?");
2449
2450 conn = objt_conn(strm_orig(sess));
2451 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
2452 case AF_INET:
2453 case AF_INET6:
2454 chunk_appendf(&trash, " source=%s:%d\n",
2455 pn, get_host_port(&conn->addr.from));
2456 break;
2457 case AF_UNIX:
2458 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
2459 break;
2460 default:
2461 /* no more information to print right now */
2462 chunk_appendf(&trash, "\n");
2463 break;
2464 }
2465
2466 chunk_appendf(&trash,
2467 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
2468 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
2469
2470 chunk_appendf(&trash,
2471 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
2472 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
2473 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
2474 strm_li(sess) ? strm_li(sess)->luid : 0);
2475
2476 if (conn)
2477 conn_get_to_addr(conn);
2478
2479 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
2480 case AF_INET:
2481 case AF_INET6:
2482 chunk_appendf(&trash, " addr=%s:%d\n",
2483 pn, get_host_port(&conn->addr.to));
2484 break;
2485 case AF_UNIX:
2486 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
2487 break;
2488 default:
2489 /* no more information to print right now */
2490 chunk_appendf(&trash, "\n");
2491 break;
2492 }
2493
2494 if (sess->be->cap & PR_CAP_BE)
2495 chunk_appendf(&trash,
2496 " backend=%s (id=%u mode=%s)",
2497 sess->be->id,
2498 sess->be->uuid, sess->be->mode ? "http" : "tcp");
2499 else
2500 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
2501
2502 conn = objt_conn(sess->si[1].end);
2503 if (conn)
2504 conn_get_from_addr(conn);
2505
2506 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
2507 case AF_INET:
2508 case AF_INET6:
2509 chunk_appendf(&trash, " addr=%s:%d\n",
2510 pn, get_host_port(&conn->addr.from));
2511 break;
2512 case AF_UNIX:
2513 chunk_appendf(&trash, " addr=unix\n");
2514 break;
2515 default:
2516 /* no more information to print right now */
2517 chunk_appendf(&trash, "\n");
2518 break;
2519 }
2520
2521 if (sess->be->cap & PR_CAP_BE)
2522 chunk_appendf(&trash,
2523 " server=%s (id=%u)",
2524 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
2525 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
2526 else
2527 chunk_appendf(&trash, " server=<NONE> (id=-1)");
2528
2529 if (conn)
2530 conn_get_to_addr(conn);
2531
2532 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
2533 case AF_INET:
2534 case AF_INET6:
2535 chunk_appendf(&trash, " addr=%s:%d\n",
2536 pn, get_host_port(&conn->addr.to));
2537 break;
2538 case AF_UNIX:
2539 chunk_appendf(&trash, " addr=unix\n");
2540 break;
2541 default:
2542 /* no more information to print right now */
2543 chunk_appendf(&trash, "\n");
2544 break;
2545 }
2546
2547 chunk_appendf(&trash,
2548 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
2549 sess->task,
2550 sess->task->state,
2551 sess->task->nice, sess->task->calls,
2552 sess->task->expire ?
2553 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
2554 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
2555 TICKS_TO_MS(1000)) : "<NEVER>",
2556 task_in_rq(sess->task) ? ", running" : "");
2557
2558 chunk_appendf(&trash,
2559 " age=%s)\n",
2560 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
2561
2562 if (sess->txn)
2563 chunk_appendf(&trash,
2564 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s waiting=%d\n",
2565 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
2566 http_msg_state_str(sess->txn->req.msg_state), http_msg_state_str(sess->txn->rsp.msg_state), !LIST_ISEMPTY(&sess->buffer_wait));
2567
2568 chunk_appendf(&trash,
2569 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
2570 &sess->si[0],
2571 si_state_str(sess->si[0].state),
2572 sess->si[0].flags,
2573 obj_type_name(sess->si[0].end),
2574 obj_base_ptr(sess->si[0].end),
2575 sess->si[0].exp ?
2576 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
2577 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
2578 TICKS_TO_MS(1000)) : "<NEVER>",
2579 sess->si[0].err_type);
2580
2581 chunk_appendf(&trash,
2582 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
2583 &sess->si[1],
2584 si_state_str(sess->si[1].state),
2585 sess->si[1].flags,
2586 obj_type_name(sess->si[1].end),
2587 obj_base_ptr(sess->si[1].end),
2588 sess->si[1].exp ?
2589 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
2590 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
2591 TICKS_TO_MS(1000)) : "<NEVER>",
2592 sess->si[1].err_type);
2593
2594 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
2595 chunk_appendf(&trash,
2596 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
2597 conn,
2598 get_conn_ctrl_name(conn),
2599 get_conn_xprt_name(conn),
2600 get_conn_data_name(conn),
2601 obj_type_name(conn->target),
2602 obj_base_ptr(conn->target));
2603
2604 chunk_appendf(&trash, " flags=0x%08x", conn->flags);
2605
2606 if (conn->t.sock.fd >= 0) {
2607 chunk_appendf(&trash, " fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
2608 conn->t.sock.fd, fdtab[conn->t.sock.fd].state,
2609 fdtab[conn->t.sock.fd].cache, fdtab[conn->t.sock.fd].updated);
2610 }
2611 else
2612 chunk_appendf(&trash, " fd=<dead>\n");
2613 }
2614 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
2615 chunk_appendf(&trash,
2616 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
2617 tmpctx,
2618 tmpctx->st0,
2619 tmpctx->st1,
2620 tmpctx->st2,
2621 tmpctx->applet->name);
2622 }
2623
2624 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
2625 chunk_appendf(&trash,
2626 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
2627 conn,
2628 get_conn_ctrl_name(conn),
2629 get_conn_xprt_name(conn),
2630 get_conn_data_name(conn),
2631 obj_type_name(conn->target),
2632 obj_base_ptr(conn->target));
2633
2634 chunk_appendf(&trash, " flags=0x%08x", conn->flags);
2635
2636 if (conn->t.sock.fd >= 0) {
2637 chunk_appendf(&trash, " fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
2638 conn->t.sock.fd, fdtab[conn->t.sock.fd].state,
2639 fdtab[conn->t.sock.fd].cache, fdtab[conn->t.sock.fd].updated);
2640 }
2641 else
2642 chunk_appendf(&trash, " fd=<dead>\n");
2643 }
2644 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
2645 chunk_appendf(&trash,
2646 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
2647 tmpctx,
2648 tmpctx->st0,
2649 tmpctx->st1,
2650 tmpctx->st2,
2651 tmpctx->applet->name);
2652 }
2653
2654 chunk_appendf(&trash,
2655 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
2656 " an_exp=%s",
2657 &sess->req,
2658 sess->req.flags, sess->req.analysers,
2659 sess->req.pipe ? sess->req.pipe->data : 0,
2660 sess->req.to_forward, sess->req.total,
2661 sess->req.analyse_exp ?
2662 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
2663 TICKS_TO_MS(1000)) : "<NEVER>");
2664
2665 chunk_appendf(&trash,
2666 " rex=%s",
2667 sess->req.rex ?
2668 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
2669 TICKS_TO_MS(1000)) : "<NEVER>");
2670
2671 chunk_appendf(&trash,
2672 " wex=%s\n"
2673 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
2674 sess->req.wex ?
2675 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
2676 TICKS_TO_MS(1000)) : "<NEVER>",
2677 sess->req.buf,
2678 sess->req.buf->data, sess->req.buf->o,
2679 (int)(sess->req.buf->p - sess->req.buf->data),
2680 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
2681 sess->req.buf->size);
2682
2683 chunk_appendf(&trash,
2684 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
2685 " an_exp=%s",
2686 &sess->res,
2687 sess->res.flags, sess->res.analysers,
2688 sess->res.pipe ? sess->res.pipe->data : 0,
2689 sess->res.to_forward, sess->res.total,
2690 sess->res.analyse_exp ?
2691 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
2692 TICKS_TO_MS(1000)) : "<NEVER>");
2693
2694 chunk_appendf(&trash,
2695 " rex=%s",
2696 sess->res.rex ?
2697 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
2698 TICKS_TO_MS(1000)) : "<NEVER>");
2699
2700 chunk_appendf(&trash,
2701 " wex=%s\n"
2702 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
2703 sess->res.wex ?
2704 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
2705 TICKS_TO_MS(1000)) : "<NEVER>",
2706 sess->res.buf,
2707 sess->res.buf->data, sess->res.buf->o,
2708 (int)(sess->res.buf->p - sess->res.buf->data),
2709 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
2710 sess->res.buf->size);
2711
2712 if (bi_putchk(si_ic(si), &trash) == -1) {
2713 si_applet_cant_put(si);
2714 return 0;
2715 }
2716
2717 /* use other states to dump the contents */
2718 }
2719 /* end of dump */
2720 appctx->ctx.sess.uid = 0;
2721 appctx->ctx.sess.section = 0;
2722 return 1;
2723}
2724
William Lallemand74c24fb2016-11-21 17:18:36 +01002725/* This function dumps all streams' states onto the stream interface's
2726 * read buffer. It returns 0 if the output buffer is full and it needs
2727 * to be called again, otherwise non-zero. It is designed to be called
2728 * from stats_dump_sess_to_buffer() below.
2729 */
2730static int stats_dump_sess_to_buffer(struct stream_interface *si)
2731{
2732 struct appctx *appctx = __objt_appctx(si->end);
2733 struct connection *conn;
2734
2735 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
2736 /* If we're forced to shut down, we might have to remove our
2737 * reference to the last stream being dumped.
2738 */
2739 if (appctx->st2 == STAT_ST_LIST) {
2740 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
2741 LIST_DEL(&appctx->ctx.sess.bref.users);
2742 LIST_INIT(&appctx->ctx.sess.bref.users);
2743 }
2744 }
2745 return 1;
2746 }
2747
2748 chunk_reset(&trash);
2749
2750 switch (appctx->st2) {
2751 case STAT_ST_INIT:
2752 /* the function had not been called yet, let's prepare the
2753 * buffer for a response. We initialize the current stream
2754 * pointer to the first in the global list. When a target
2755 * stream is being destroyed, it is responsible for updating
2756 * this pointer. We know we have reached the end when this
2757 * pointer points back to the head of the streams list.
2758 */
2759 LIST_INIT(&appctx->ctx.sess.bref.users);
2760 appctx->ctx.sess.bref.ref = streams.n;
2761 appctx->st2 = STAT_ST_LIST;
2762 /* fall through */
2763
2764 case STAT_ST_LIST:
2765 /* first, let's detach the back-ref from a possible previous stream */
2766 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
2767 LIST_DEL(&appctx->ctx.sess.bref.users);
2768 LIST_INIT(&appctx->ctx.sess.bref.users);
2769 }
2770
2771 /* and start from where we stopped */
2772 while (appctx->ctx.sess.bref.ref != &streams) {
2773 char pn[INET6_ADDRSTRLEN];
2774 struct stream *curr_sess;
2775
2776 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
2777
2778 if (appctx->ctx.sess.target) {
2779 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
2780 goto next_sess;
2781
2782 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
2783 /* call the proper dump() function and return if we're missing space */
2784 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
2785 return 0;
2786
2787 /* stream dump complete */
2788 LIST_DEL(&appctx->ctx.sess.bref.users);
2789 LIST_INIT(&appctx->ctx.sess.bref.users);
2790 if (appctx->ctx.sess.target != (void *)-1) {
2791 appctx->ctx.sess.target = NULL;
2792 break;
2793 }
2794 else
2795 goto next_sess;
2796 }
2797
2798 chunk_appendf(&trash,
2799 "%p: proto=%s",
2800 curr_sess,
2801 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
2802
2803 conn = objt_conn(strm_orig(curr_sess));
2804 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
2805 case AF_INET:
2806 case AF_INET6:
2807 chunk_appendf(&trash,
2808 " src=%s:%d fe=%s be=%s srv=%s",
2809 pn,
2810 get_host_port(&conn->addr.from),
2811 strm_fe(curr_sess)->id,
2812 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
2813 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
2814 );
2815 break;
2816 case AF_UNIX:
2817 chunk_appendf(&trash,
2818 " src=unix:%d fe=%s be=%s srv=%s",
2819 strm_li(curr_sess)->luid,
2820 strm_fe(curr_sess)->id,
2821 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
2822 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
2823 );
2824 break;
2825 }
2826
2827 chunk_appendf(&trash,
2828 " ts=%02x age=%s calls=%d",
2829 curr_sess->task->state,
2830 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
2831 curr_sess->task->calls);
2832
2833 chunk_appendf(&trash,
2834 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
2835 curr_sess->req.flags,
2836 curr_sess->req.buf->i,
2837 curr_sess->req.analysers,
2838 curr_sess->req.rex ?
2839 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
2840 TICKS_TO_MS(1000)) : "");
2841
2842 chunk_appendf(&trash,
2843 ",wx=%s",
2844 curr_sess->req.wex ?
2845 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
2846 TICKS_TO_MS(1000)) : "");
2847
2848 chunk_appendf(&trash,
2849 ",ax=%s]",
2850 curr_sess->req.analyse_exp ?
2851 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
2852 TICKS_TO_MS(1000)) : "");
2853
2854 chunk_appendf(&trash,
2855 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
2856 curr_sess->res.flags,
2857 curr_sess->res.buf->i,
2858 curr_sess->res.analysers,
2859 curr_sess->res.rex ?
2860 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
2861 TICKS_TO_MS(1000)) : "");
2862
2863 chunk_appendf(&trash,
2864 ",wx=%s",
2865 curr_sess->res.wex ?
2866 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
2867 TICKS_TO_MS(1000)) : "");
2868
2869 chunk_appendf(&trash,
2870 ",ax=%s]",
2871 curr_sess->res.analyse_exp ?
2872 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
2873 TICKS_TO_MS(1000)) : "");
2874
2875 conn = objt_conn(curr_sess->si[0].end);
2876 chunk_appendf(&trash,
2877 " s0=[%d,%1xh,fd=%d,ex=%s]",
2878 curr_sess->si[0].state,
2879 curr_sess->si[0].flags,
2880 (conn && conn->t.sock.fd >= 0) ? conn->t.sock.fd : -1,
2881 curr_sess->si[0].exp ?
2882 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
2883 TICKS_TO_MS(1000)) : "");
2884
2885 conn = objt_conn(curr_sess->si[1].end);
2886 chunk_appendf(&trash,
2887 " s1=[%d,%1xh,fd=%d,ex=%s]",
2888 curr_sess->si[1].state,
2889 curr_sess->si[1].flags,
2890 (conn && conn->t.sock.fd >= 0) ? conn->t.sock.fd : -1,
2891 curr_sess->si[1].exp ?
2892 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
2893 TICKS_TO_MS(1000)) : "");
2894
2895 chunk_appendf(&trash,
2896 " exp=%s",
2897 curr_sess->task->expire ?
2898 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
2899 TICKS_TO_MS(1000)) : "");
2900 if (task_in_rq(curr_sess->task))
2901 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
2902
2903 chunk_appendf(&trash, "\n");
2904
2905 if (bi_putchk(si_ic(si), &trash) == -1) {
2906 /* let's try again later from this stream. We add ourselves into
2907 * this stream's users so that it can remove us upon termination.
2908 */
2909 si_applet_cant_put(si);
2910 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
2911 return 0;
2912 }
2913
2914 next_sess:
2915 appctx->ctx.sess.bref.ref = curr_sess->list.n;
2916 }
2917
2918 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
2919 /* specified stream not found */
2920 if (appctx->ctx.sess.section > 0)
2921 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
2922 else
2923 chunk_appendf(&trash, "Session not found.\n");
2924
2925 if (bi_putchk(si_ic(si), &trash) == -1) {
2926 si_applet_cant_put(si);
2927 return 0;
2928 }
2929
2930 appctx->ctx.sess.target = NULL;
2931 appctx->ctx.sess.uid = 0;
2932 return 1;
2933 }
2934
2935 appctx->st2 = STAT_ST_FIN;
2936 /* fall through */
2937
2938 default:
2939 appctx->st2 = STAT_ST_FIN;
2940 return 1;
2941 }
2942}
2943
2944/* This is called when the stream interface is closed. For instance, upon an
2945 * external abort, we won't call the i/o handler anymore so we may need to
2946 * remove back references to the stream currently being dumped.
2947 */
2948static void cli_release_handler(struct appctx *appctx)
2949{
2950 if (appctx->io_release) {
2951 appctx->io_release(appctx);
2952 appctx->io_release = NULL;
2953 }
2954 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
2955 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
2956 LIST_DEL(&appctx->ctx.sess.bref.users);
2957 }
2958 else if ((appctx->st0 == STAT_CLI_O_TAB || appctx->st0 == STAT_CLI_O_CLR) &&
2959 appctx->st2 == STAT_ST_LIST) {
2960 appctx->ctx.table.entry->ref_cnt--;
2961 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2962 }
2963 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
2964 free(appctx->ctx.cli.err);
2965 appctx->ctx.cli.err = NULL;
2966 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002967}
2968
2969/* This function is used to either dump tables states (when action is set
2970 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
2971 * It returns 0 if the output buffer is full and it needs to be called
2972 * again, otherwise non-zero.
2973 */
2974static int stats_table_request(struct stream_interface *si, int action)
2975{
2976 struct appctx *appctx = __objt_appctx(si->end);
2977 struct stream *s = si_strm(si);
2978 struct ebmb_node *eb;
2979 int dt;
2980 int skip_entry;
2981 int show = action == STAT_CLI_O_TAB;
2982
2983 /*
2984 * We have 3 possible states in appctx->st2 :
2985 * - STAT_ST_INIT : the first call
2986 * - STAT_ST_INFO : the proxy pointer points to the next table to
2987 * dump, the entry pointer is NULL ;
2988 * - STAT_ST_LIST : the proxy pointer points to the current table
2989 * and the entry pointer points to the next entry to be dumped,
2990 * and the refcount on the next entry is held ;
2991 * - STAT_ST_END : nothing left to dump, the buffer may contain some
2992 * data though.
2993 */
2994
2995 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
2996 /* in case of abort, remove any refcount we might have set on an entry */
2997 if (appctx->st2 == STAT_ST_LIST) {
2998 appctx->ctx.table.entry->ref_cnt--;
2999 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
3000 }
3001 return 1;
3002 }
3003
3004 chunk_reset(&trash);
3005
3006 while (appctx->st2 != STAT_ST_FIN) {
3007 switch (appctx->st2) {
3008 case STAT_ST_INIT:
3009 appctx->ctx.table.proxy = appctx->ctx.table.target;
3010 if (!appctx->ctx.table.proxy)
3011 appctx->ctx.table.proxy = proxy;
3012
3013 appctx->ctx.table.entry = NULL;
3014 appctx->st2 = STAT_ST_INFO;
3015 break;
3016
3017 case STAT_ST_INFO:
3018 if (!appctx->ctx.table.proxy ||
3019 (appctx->ctx.table.target &&
3020 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
3021 appctx->st2 = STAT_ST_END;
3022 break;
3023 }
3024
3025 if (appctx->ctx.table.proxy->table.size) {
3026 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
3027 appctx->ctx.table.target))
3028 return 0;
3029
3030 if (appctx->ctx.table.target &&
3031 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
3032 /* dump entries only if table explicitly requested */
3033 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
3034 if (eb) {
3035 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3036 appctx->ctx.table.entry->ref_cnt++;
3037 appctx->st2 = STAT_ST_LIST;
3038 break;
3039 }
3040 }
3041 }
3042 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
3043 break;
3044
3045 case STAT_ST_LIST:
3046 skip_entry = 0;
3047
3048 if (appctx->ctx.table.data_type >= 0) {
3049 /* we're filtering on some data contents */
3050 void *ptr;
3051 long long data;
3052
3053 dt = appctx->ctx.table.data_type;
3054 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
3055 appctx->ctx.table.entry,
3056 dt);
3057
3058 data = 0;
3059 switch (stktable_data_types[dt].std_type) {
3060 case STD_T_SINT:
3061 data = stktable_data_cast(ptr, std_t_sint);
3062 break;
3063 case STD_T_UINT:
3064 data = stktable_data_cast(ptr, std_t_uint);
3065 break;
3066 case STD_T_ULL:
3067 data = stktable_data_cast(ptr, std_t_ull);
3068 break;
3069 case STD_T_FRQP:
3070 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
3071 appctx->ctx.table.proxy->table.data_arg[dt].u);
3072 break;
3073 }
3074
3075 /* skip the entry if the data does not match the test and the value */
3076 if ((data < appctx->ctx.table.value &&
3077 (appctx->ctx.table.data_op == STD_OP_EQ ||
3078 appctx->ctx.table.data_op == STD_OP_GT ||
3079 appctx->ctx.table.data_op == STD_OP_GE)) ||
3080 (data == appctx->ctx.table.value &&
3081 (appctx->ctx.table.data_op == STD_OP_NE ||
3082 appctx->ctx.table.data_op == STD_OP_GT ||
3083 appctx->ctx.table.data_op == STD_OP_LT)) ||
3084 (data > appctx->ctx.table.value &&
3085 (appctx->ctx.table.data_op == STD_OP_EQ ||
3086 appctx->ctx.table.data_op == STD_OP_LT ||
3087 appctx->ctx.table.data_op == STD_OP_LE)))
3088 skip_entry = 1;
3089 }
3090
3091 if (show && !skip_entry &&
3092 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
3093 appctx->ctx.table.entry))
3094 return 0;
3095
3096 appctx->ctx.table.entry->ref_cnt--;
3097
3098 eb = ebmb_next(&appctx->ctx.table.entry->key);
3099 if (eb) {
3100 struct stksess *old = appctx->ctx.table.entry;
3101 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
3102 if (show)
3103 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
3104 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
3105 stksess_kill(&appctx->ctx.table.proxy->table, old);
3106 appctx->ctx.table.entry->ref_cnt++;
3107 break;
3108 }
3109
3110
3111 if (show)
3112 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
3113 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
3114 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
3115
3116 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
3117 appctx->st2 = STAT_ST_INFO;
3118 break;
3119
3120 case STAT_ST_END:
3121 appctx->st2 = STAT_ST_FIN;
3122 break;
3123 }
3124 }
3125 return 1;
3126}
3127
3128/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
3129 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
3130 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
3131 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
3132 * lines are respected within the limit of 70 output chars. Lines that are
3133 * continuation of a previous truncated line begin with "+" instead of " "
3134 * after the offset. The new pointer is returned.
3135 */
3136static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
3137 int *line, int ptr)
3138{
3139 int end;
3140 unsigned char c;
3141
3142 end = out->len + 80;
3143 if (end > out->size)
3144 return ptr;
3145
3146 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
3147
3148 while (ptr < len && ptr < bsize) {
3149 c = buf[ptr];
3150 if (isprint(c) && isascii(c) && c != '\\') {
3151 if (out->len > end - 2)
3152 break;
3153 out->str[out->len++] = c;
3154 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
3155 if (out->len > end - 3)
3156 break;
3157 out->str[out->len++] = '\\';
3158 switch (c) {
3159 case '\t': c = 't'; break;
3160 case '\n': c = 'n'; break;
3161 case '\r': c = 'r'; break;
3162 case '\e': c = 'e'; break;
3163 case '\\': c = '\\'; break;
3164 }
3165 out->str[out->len++] = c;
3166 } else {
3167 if (out->len > end - 5)
3168 break;
3169 out->str[out->len++] = '\\';
3170 out->str[out->len++] = 'x';
3171 out->str[out->len++] = hextab[(c >> 4) & 0xF];
3172 out->str[out->len++] = hextab[c & 0xF];
3173 }
3174 if (buf[ptr++] == '\n') {
3175 /* we had a line break, let's return now */
3176 out->str[out->len++] = '\n';
3177 *line = ptr;
3178 return ptr;
3179 }
3180 }
3181 /* we have an incomplete line, we return it as-is */
3182 out->str[out->len++] = '\n';
3183 return ptr;
3184}
3185
William Lallemand74c24fb2016-11-21 17:18:36 +01003186/* This function dumps all captured errors onto the stream interface's
3187 * read buffer. It returns 0 if the output buffer is full and it needs
3188 * to be called again, otherwise non-zero.
3189 */
3190static int stats_dump_errors_to_buffer(struct stream_interface *si)
3191{
3192 struct appctx *appctx = __objt_appctx(si->end);
3193 extern const char *monthname[12];
3194
3195 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
3196 return 1;
3197
3198 chunk_reset(&trash);
3199
3200 if (!appctx->ctx.errors.px) {
3201 /* the function had not been called yet, let's prepare the
3202 * buffer for a response.
3203 */
3204 struct tm tm;
3205
3206 get_localtime(date.tv_sec, &tm);
3207 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3208 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3209 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3210 error_snapshot_id);
3211
3212 if (bi_putchk(si_ic(si), &trash) == -1) {
3213 /* Socket buffer full. Let's try again later from the same point */
3214 si_applet_cant_put(si);
3215 return 0;
3216 }
3217
3218 appctx->ctx.errors.px = proxy;
3219 appctx->ctx.errors.buf = 0;
3220 appctx->ctx.errors.bol = 0;
3221 appctx->ctx.errors.ptr = -1;
3222 }
3223
3224 /* we have two inner loops here, one for the proxy, the other one for
3225 * the buffer.
3226 */
3227 while (appctx->ctx.errors.px) {
3228 struct error_snapshot *es;
3229
3230 if (appctx->ctx.errors.buf == 0)
3231 es = &appctx->ctx.errors.px->invalid_req;
3232 else
3233 es = &appctx->ctx.errors.px->invalid_rep;
3234
3235 if (!es->when.tv_sec)
3236 goto next;
3237
3238 if (appctx->ctx.errors.iid >= 0 &&
3239 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
3240 es->oe->uuid != appctx->ctx.errors.iid)
3241 goto next;
3242
3243 if (appctx->ctx.errors.ptr < 0) {
3244 /* just print headers now */
3245
3246 char pn[INET6_ADDRSTRLEN];
3247 struct tm tm;
3248 int port;
3249
3250 get_localtime(es->when.tv_sec, &tm);
3251 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
3252 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3253 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
3254
3255 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
3256 case AF_INET:
3257 case AF_INET6:
3258 port = get_host_port(&es->src);
3259 break;
3260 default:
3261 port = 0;
3262 }
3263
3264 switch (appctx->ctx.errors.buf) {
3265 case 0:
3266 chunk_appendf(&trash,
3267 " frontend %s (#%d): invalid request\n"
3268 " backend %s (#%d)",
3269 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
3270 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3271 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
3272 break;
3273 case 1:
3274 chunk_appendf(&trash,
3275 " backend %s (#%d): invalid response\n"
3276 " frontend %s (#%d)",
3277 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
3278 es->oe->id, es->oe->uuid);
3279 break;
3280 }
3281
3282 chunk_appendf(&trash,
3283 ", server %s (#%d), event #%u\n"
3284 " src %s:%d, session #%d, session flags 0x%08x\n"
3285 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
3286 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
3287 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
3288 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
3289 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
3290 es->ev_id,
3291 pn, port, es->sid, es->s_flags,
3292 es->state, es->m_flags, es->t_flags,
3293 es->m_clen, es->m_blen,
3294 es->b_flags, es->b_out, es->b_tot,
3295 es->len, es->b_wrap, es->pos);
3296
3297 if (bi_putchk(si_ic(si), &trash) == -1) {
3298 /* Socket buffer full. Let's try again later from the same point */
3299 si_applet_cant_put(si);
3300 return 0;
3301 }
3302 appctx->ctx.errors.ptr = 0;
3303 appctx->ctx.errors.sid = es->sid;
3304 }
3305
3306 if (appctx->ctx.errors.sid != es->sid) {
3307 /* the snapshot changed while we were dumping it */
3308 chunk_appendf(&trash,
3309 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
3310 if (bi_putchk(si_ic(si), &trash) == -1) {
3311 si_applet_cant_put(si);
3312 return 0;
3313 }
3314 goto next;
3315 }
3316
3317 /* OK, ptr >= 0, so we have to dump the current line */
3318 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
3319 int newptr;
3320 int newline;
3321
3322 newline = appctx->ctx.errors.bol;
3323 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
3324 if (newptr == appctx->ctx.errors.ptr)
3325 return 0;
3326
3327 if (bi_putchk(si_ic(si), &trash) == -1) {
3328 /* Socket buffer full. Let's try again later from the same point */
3329 si_applet_cant_put(si);
3330 return 0;
3331 }
3332 appctx->ctx.errors.ptr = newptr;
3333 appctx->ctx.errors.bol = newline;
3334 };
3335 next:
3336 appctx->ctx.errors.bol = 0;
3337 appctx->ctx.errors.ptr = -1;
3338 appctx->ctx.errors.buf++;
3339 if (appctx->ctx.errors.buf > 1) {
3340 appctx->ctx.errors.buf = 0;
3341 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
3342 }
3343 }
3344
3345 /* dump complete */
3346 return 1;
3347}
3348
3349/* This function dumps all environmnent variables to the buffer. It returns 0
3350 * if the output buffer is full and it needs to be called again, otherwise
3351 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
3352 */
3353static int stats_dump_env_to_buffer(struct stream_interface *si)
3354{
3355 struct appctx *appctx = __objt_appctx(si->end);
3356
3357 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
3358 return 1;
3359
3360 chunk_reset(&trash);
3361
3362 /* we have two inner loops here, one for the proxy, the other one for
3363 * the buffer.
3364 */
3365 while (*appctx->ctx.env.var) {
3366 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
3367
3368 if (bi_putchk(si_ic(si), &trash) == -1) {
3369 si_applet_cant_put(si);
3370 return 0;
3371 }
3372 if (appctx->st2 == STAT_ST_END)
3373 break;
3374 appctx->ctx.env.var++;
3375 }
3376
3377 /* dump complete */
3378 return 1;
3379}
3380
3381/* parse the "level" argument on the bind lines */
3382static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
3383{
3384 if (!*args[cur_arg + 1]) {
3385 memprintf(err, "'%s' : missing level", args[cur_arg]);
3386 return ERR_ALERT | ERR_FATAL;
3387 }
3388
3389 if (!strcmp(args[cur_arg+1], "user"))
3390 conf->level = ACCESS_LVL_USER;
3391 else if (!strcmp(args[cur_arg+1], "operator"))
3392 conf->level = ACCESS_LVL_OPER;
3393 else if (!strcmp(args[cur_arg+1], "admin"))
3394 conf->level = ACCESS_LVL_ADMIN;
3395 else {
3396 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
3397 args[cur_arg], args[cur_arg+1]);
3398 return ERR_ALERT | ERR_FATAL;
3399 }
3400
3401 return 0;
3402}
3403
3404static struct applet cli_applet = {
3405 .obj_type = OBJ_TYPE_APPLET,
3406 .name = "<CLI>", /* used for logging */
3407 .fct = cli_io_handler,
3408 .release = cli_release_handler,
3409};
3410
3411static struct cfg_kw_list cfg_kws = {ILH, {
3412 { CFG_GLOBAL, "stats", stats_parse_global },
3413 { 0, NULL, NULL },
3414}};
3415
3416static struct bind_kw_list bind_kws = { "STAT", { }, {
3417 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
3418 { NULL, NULL, 0 },
3419}};
3420
3421__attribute__((constructor))
3422static void __dumpstats_module_init(void)
3423{
3424 cfg_register_keywords(&cfg_kws);
3425 bind_register_keywords(&bind_kws);
3426}
3427
3428/*
3429 * Local variables:
3430 * c-indent-level: 8
3431 * c-basic-offset: 8
3432 * End:
3433 */