blob: 31bc0ba6ca2f54144cc481acb74d8d8ab02af4c4 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
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>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#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>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Emeric Brun4147b2e2014-06-16 18:36:30 +020038#include <common/base64.h>
Willy Tarreau91861262007-10-17 17:06:05 +020039
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <types/applet.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020042
43#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020044#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010046#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
48#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010049#include <proto/freq_ctr.h>
Willy Tarreau32b60d42015-03-13 16:14:57 +010050#include <proto/frontend.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020051#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010052#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010053#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020054#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010055#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010056#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020057#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020058#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010059#include <proto/sample.h>
Willy Tarreau9903f0e2015-04-04 18:50:31 +020060#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020061#include <proto/stream.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020062#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020063#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010064#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010065#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020066
Willy Tarreau7a0169a2012-11-19 17:13:16 +010067#ifdef USE_OPENSSL
68#include <proto/ssl_sock.h>
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020069#include <types/ssl_sock.h>
Willy Tarreau7a0169a2012-11-19 17:13:16 +010070#endif
71
Willy Tarreau91b843d2014-01-28 16:27:17 +010072/* stats socket states */
73enum {
74 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
75 STAT_CLI_END, /* final state, let's close */
76 STAT_CLI_GETREQ, /* wait for a request */
77 STAT_CLI_OUTPUT, /* all states after this one are responses */
78 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
79 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010080 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010081 STAT_CLI_O_INFO, /* dump info */
Willy Tarreau87b09662015-04-03 00:22:06 +020082 STAT_CLI_O_SESS, /* dump streams */
Willy Tarreau91b843d2014-01-28 16:27:17 +010083 STAT_CLI_O_ERR, /* dump errors */
84 STAT_CLI_O_TAB, /* dump tables */
85 STAT_CLI_O_CLR, /* clear tables */
86 STAT_CLI_O_SET, /* set entries in tables */
87 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010088 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
89 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010090 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010091 STAT_CLI_O_POOLS, /* dump memory pools */
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020092 STAT_CLI_O_TLSK, /* list all TLS ticket keys references */
Willy Tarreau91b843d2014-01-28 16:27:17 +010093};
94
Willy Tarreaued7df902014-05-22 18:04:49 +020095/* Actions available for the stats admin forms */
96enum {
97 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +020098
99 /* enable/disable health checks */
100 ST_ADM_ACTION_DHLTH,
101 ST_ADM_ACTION_EHLTH,
102
103 /* force health check status */
104 ST_ADM_ACTION_HRUNN,
105 ST_ADM_ACTION_HNOLB,
106 ST_ADM_ACTION_HDOWN,
107
108 /* enable/disable agent checks */
109 ST_ADM_ACTION_DAGENT,
110 ST_ADM_ACTION_EAGENT,
111
112 /* force agent check status */
113 ST_ADM_ACTION_ARUNN,
114 ST_ADM_ACTION_ADOWN,
115
116 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200117 ST_ADM_ACTION_READY,
118 ST_ADM_ACTION_DRAIN,
119 ST_ADM_ACTION_MAINT,
120 ST_ADM_ACTION_SHUTDOWN,
121 /* these are the ancient actions, still available for compatibility */
122 ST_ADM_ACTION_DISABLE,
123 ST_ADM_ACTION_ENABLE,
124 ST_ADM_ACTION_STOP,
125 ST_ADM_ACTION_START,
126};
127
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100128static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100129static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200130static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900131static int stats_dump_sess_to_buffer(struct stream_interface *si);
132static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100133static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100134static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
135static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100136static int stats_pats_list(struct stream_interface *si);
137static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100138static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200139#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
140static int stats_tlskeys_list(struct stream_interface *si);
141#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200142static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900143
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100144/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100145 * cli_io_handler()
146 * -> stats_dump_sess_to_buffer() // "show sess"
147 * -> stats_dump_errors_to_buffer() // "show errors"
148 * -> stats_dump_info_to_buffer() // "show info"
149 * -> stats_dump_stat_to_buffer() // "show stat"
150 * -> stats_dump_csv_header()
151 * -> stats_dump_proxy_to_buffer()
152 * -> stats_dump_fe_stats()
153 * -> stats_dump_li_stats()
154 * -> stats_dump_sv_stats()
155 * -> stats_dump_be_stats()
156 *
157 * http_stats_io_handler()
158 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
159 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
160 * -> stats_dump_html_head() // emits the HTML headers
161 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
162 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
163 * -> stats_dump_html_px_hdr()
164 * -> stats_dump_fe_stats()
165 * -> stats_dump_li_stats()
166 * -> stats_dump_sv_stats()
167 * -> stats_dump_be_stats()
168 * -> stats_dump_html_px_end()
169 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100170 */
171
Willy Tarreau30576452015-04-13 13:50:30 +0200172static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900173
174static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200175 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200176 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200177 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200178 " help : this message\n"
179 " prompt : toggle interactive mode with prompt\n"
180 " quit : disconnect\n"
181 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100182 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200183 " show stat : report counters for each proxy and server\n"
184 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100185 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200186 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200187 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200188 " set weight : change a server's weight\n"
Willy Tarreau2a4b70f2014-05-22 18:42:35 +0200189 " set server : change a server's state or weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200190 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100191 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200192 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200193 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200194 " disable : put a server or frontend in maintenance mode\n"
195 " enable : re-enable a server or frontend which is in maintenance mode\n"
196 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100197 " show acl [id] : report avalaible acls or dump an acl's contents\n"
198 " get acl : reports the patterns matching a sample for an ACL\n"
199 " add acl : add acl entry\n"
200 " del acl : delete acl entry\n"
201 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100202 " show map [id] : report avalaible maps or dump a map's contents\n"
203 " get map : reports the keys and values matching a sample for a map\n"
204 " set map : modify map entry\n"
205 " add map : add map entry\n"
206 " del map : delete map entry\n"
207 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200208 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200209 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200210
Simon Horman9bd2c732011-06-15 15:18:44 +0900211static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200212 "Permission denied\n"
213 "";
214
Willy Tarreau295a8372011-03-10 11:25:07 +0100215/* data transmission states for the stats responses */
216enum {
217 STAT_ST_INIT = 0,
218 STAT_ST_HEAD,
219 STAT_ST_INFO,
220 STAT_ST_LIST,
221 STAT_ST_END,
222 STAT_ST_FIN,
223};
224
225/* data transmission states for the stats responses inside a proxy */
226enum {
227 STAT_PX_ST_INIT = 0,
228 STAT_PX_ST_TH,
229 STAT_PX_ST_FE,
230 STAT_PX_ST_LI,
231 STAT_PX_ST_SV,
232 STAT_PX_ST_BE,
233 STAT_PX_ST_END,
234 STAT_PX_ST_FIN,
235};
236
Cyril Bonté19979e12012-04-04 12:57:21 +0200237extern const char *stat_status_codes[];
238
Willy Tarreau07e9e642010-08-17 21:48:17 +0200239/* allocate a new stats frontend named <name>, and return it
240 * (or NULL in case of lack of memory).
241 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200242static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200243{
244 struct proxy *fe;
245
246 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
247 if (!fe)
248 return NULL;
249
Willy Tarreau237250c2011-07-29 01:49:03 +0200250 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200251 fe->next = proxy;
252 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200253 fe->last_change = now.tv_sec;
254 fe->id = strdup("GLOBAL");
255 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200256 fe->maxconn = 10; /* default to 10 concurrent connections */
257 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200258 fe->conf.file = strdup(file);
259 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100260 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100261 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200262
263 /* the stats frontend is the only one able to assign ID #0 */
264 fe->conf.id.key = fe->uuid = 0;
265 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200266 return fe;
267}
268
Willy Tarreaufbee7132007-10-18 13:53:22 +0200269/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200270 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
271 * error message into the <err> buffer which will be preallocated. The trailing
272 * '\n' must not be written. The function must be called with <args> pointing to
273 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200274 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200275static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200276 struct proxy *defpx, const char *file, int line,
277 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200279 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200280 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200281
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200282 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200283 int cur_arg;
284
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200285 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200286 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200287 return -1;
288 }
289
Willy Tarreau89a63132009-08-16 17:41:45 +0200290 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200291 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200292 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200293 return -1;
294 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200295 }
296
Willy Tarreau4348fad2012-09-20 16:48:07 +0200297 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200298 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200299
Willy Tarreauc53d4222012-09-20 20:19:28 +0200300 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
301 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
302 file, line, args[0], args[1], err && *err ? *err : "error");
303 return -1;
304 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200305
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200306 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200307 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200308 static int bind_dumped;
309 struct bind_kw *kw;
310
311 kw = bind_find_kw(args[cur_arg]);
312 if (kw) {
313 if (!kw->parse) {
314 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
315 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200316 return -1;
317 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200318
319 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
320 if (err && *err)
321 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
322 else
323 memprintf(err, "'%s %s' : error encountered while processing '%s'",
324 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200325 return -1;
326 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200327
328 cur_arg += 1 + kw->skip;
329 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200330 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200331
332 if (!bind_dumped) {
333 bind_dump_kws(err);
334 indent_msg(err, 4);
335 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200336 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200337
338 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
339 args[0], args[1], args[cur_arg],
340 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
341 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200342 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100343
Willy Tarreauc53d4222012-09-20 20:19:28 +0200344 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
345 l->maxconn = global.stats_fe->maxconn;
346 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200347 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200348 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100349 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200350 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
351 l->nice = -64; /* we want to boost priority for local stats */
352 global.maxsock += l->maxconn;
353 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200354 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200355 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100356 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200357 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100358
359 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200360 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100361 return -1;
362 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200363
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100364 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200365 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200366 return -1;
367 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200368 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200369 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200370 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200371 return -1;
372 }
373 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200374 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200375 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200376 else if (!strcmp(args[1], "maxconn")) {
377 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200378
379 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200380 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200381 return -1;
382 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200383
384 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200385 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200386 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200387 return -1;
388 }
389 }
390 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200391 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200392 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
393 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100394 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200395
Willy Tarreau91319572013-04-20 09:48:50 +0200396 if (!global.stats_fe) {
397 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
398 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
399 return -1;
400 }
401 }
402
Willy Tarreau35b7b162012-10-22 23:17:18 +0200403 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100404 unsigned int low, high;
405
Willy Tarreau35b7b162012-10-22 23:17:18 +0200406 if (strcmp(args[cur_arg], "all") == 0) {
407 set = 0;
408 break;
409 }
410 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100411 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200412 }
413 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100414 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200415 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100416 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100417 char *dash = strchr(args[cur_arg], '-');
418
419 low = high = str2uic(args[cur_arg]);
420 if (dash)
421 high = str2uic(dash + 1);
422
423 if (high < low) {
424 unsigned int swap = low;
425 low = high;
426 high = swap;
427 }
428
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100429 if (low < 1 || high > LONGBITS) {
430 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
431 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200432 return -1;
433 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100434 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100435 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100436 }
437 else {
438 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100439 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
440 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100441 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200442 }
443 cur_arg++;
444 }
445 global.stats_fe->bind_proc = set;
446 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200447 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200448 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200449 return -1;
450 }
451 return 0;
452}
453
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100454/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
455 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100456 * NOTE: Some tools happen to rely on the field position instead of its name,
457 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100458 */
459static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100460{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100461 chunk_appendf(&trash,
462 "# pxname,svname,"
463 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100464 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100465 "bin,bout,"
466 "dreq,dresp,"
467 "ereq,econ,eresp,"
468 "wretr,wredis,"
469 "status,weight,act,bck,"
470 "chkfail,chkdown,lastchg,downtime,qlimit,"
471 "pid,iid,sid,throttle,lbtot,tracked,type,"
472 "rate,rate_lim,rate_max,"
473 "check_status,check_code,check_duration,"
474 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
475 "req_rate,req_rate_max,req_tot,"
476 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200477 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100478 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100479}
480
Simon Hormand9366582011-06-15 15:18:45 +0900481/* print a string of text buffer to <out>. The format is :
482 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
483 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
484 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
485 */
486static int dump_text(struct chunk *out, const char *buf, int bsize)
487{
488 unsigned char c;
489 int ptr = 0;
490
491 while (buf[ptr] && ptr < bsize) {
492 c = buf[ptr];
493 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
494 if (out->len > out->size - 1)
495 break;
496 out->str[out->len++] = c;
497 }
498 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
499 if (out->len > out->size - 2)
500 break;
501 out->str[out->len++] = '\\';
502 switch (c) {
503 case ' ': c = ' '; break;
504 case '\t': c = 't'; break;
505 case '\n': c = 'n'; break;
506 case '\r': c = 'r'; break;
507 case '\e': c = 'e'; break;
508 case '\\': c = '\\'; break;
509 }
510 out->str[out->len++] = c;
511 }
512 else {
513 if (out->len > out->size - 4)
514 break;
515 out->str[out->len++] = '\\';
516 out->str[out->len++] = 'x';
517 out->str[out->len++] = hextab[(c >> 4) & 0xF];
518 out->str[out->len++] = hextab[c & 0xF];
519 }
520 ptr++;
521 }
522
523 return ptr;
524}
525
526/* print a buffer in hexa.
527 * Print stopped if <bsize> is reached, or if no more place in the chunk.
528 */
529static int dump_binary(struct chunk *out, const char *buf, int bsize)
530{
531 unsigned char c;
532 int ptr = 0;
533
534 while (ptr < bsize) {
535 c = buf[ptr];
536
537 if (out->len > out->size - 2)
538 break;
539 out->str[out->len++] = hextab[(c >> 4) & 0xF];
540 out->str[out->len++] = hextab[c & 0xF];
541
542 ptr++;
543 }
544 return ptr;
545}
546
547/* Dump the status of a table to a stream interface's
548 * read buffer. It returns 0 if the output buffer is full
549 * and needs to be called again, otherwise non-zero.
550 */
551static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
552 struct proxy *proxy, struct proxy *target)
553{
Willy Tarreau87b09662015-04-03 00:22:06 +0200554 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900555
Willy Tarreau77804732012-10-29 16:14:26 +0100556 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900557 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
558
559 /* any other information should be dumped here */
560
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200561 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100562 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900563
Willy Tarreaubc18da12015-03-13 14:00:47 +0100564 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200565 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900566 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100567 }
Simon Hormand9366582011-06-15 15:18:45 +0900568
569 return 1;
570}
571
572/* Dump the a table entry to a stream interface's
573 * read buffer. It returns 0 if the output buffer is full
574 * and needs to be called again, otherwise non-zero.
575 */
576static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
577 struct proxy *proxy, struct stksess *entry)
578{
579 int dt;
580
Willy Tarreau77804732012-10-29 16:14:26 +0100581 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900582
583 if (proxy->table.type == STKTABLE_TYPE_IP) {
584 char addr[INET_ADDRSTRLEN];
585 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100586 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900587 }
588 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
589 char addr[INET6_ADDRSTRLEN];
590 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100591 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900592 }
593 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100594 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900595 }
596 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100597 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900598 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
599 }
600 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100601 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900602 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
603 }
604
Willy Tarreau77804732012-10-29 16:14:26 +0100605 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900606
607 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
608 void *ptr;
609
610 if (proxy->table.data_ofs[dt] == 0)
611 continue;
612 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100613 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900614 else
Willy Tarreau77804732012-10-29 16:14:26 +0100615 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900616
617 ptr = stktable_data_ptr(&proxy->table, entry, dt);
618 switch (stktable_data_types[dt].std_type) {
619 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100620 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900621 break;
622 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100623 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900624 break;
625 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100626 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900627 break;
628 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100629 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900630 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
631 proxy->table.data_arg[dt].u));
632 break;
633 }
634 }
Willy Tarreau77804732012-10-29 16:14:26 +0100635 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900636
Willy Tarreaubc18da12015-03-13 14:00:47 +0100637 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200638 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900639 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100640 }
Simon Hormand9366582011-06-15 15:18:45 +0900641
642 return 1;
643}
644
Willy Tarreaudec98142012-06-06 23:37:08 +0200645static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900646{
Willy Tarreau87b09662015-04-03 00:22:06 +0200647 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100648 struct appctx *appctx = __objt_appctx(si->end);
649 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900650 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900651 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900652 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200653 long long value;
654 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200655 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200656 void *ptr;
657 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900658
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100659 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900660
661 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100662 appctx->ctx.cli.msg = "Key value expected\n";
663 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900664 return;
665 }
666
Simon Hormanc5b89f62011-06-15 15:18:50 +0900667 switch (px->table.type) {
668 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900669 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100670 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900671 break;
672 case STKTABLE_TYPE_IPV6:
673 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100674 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900675 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900676 case STKTABLE_TYPE_INTEGER:
677 {
678 char *endptr;
679 unsigned long val;
680 errno = 0;
681 val = strtoul(args[4], &endptr, 10);
682 if ((errno == ERANGE && val == ULONG_MAX) ||
683 (errno != 0 && val == 0) || endptr == args[4] ||
684 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100685 appctx->ctx.cli.msg = "Invalid key\n";
686 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900687 return;
688 }
689 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100690 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900691 break;
692 }
693 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900694 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100695 static_table_key->key = args[4];
696 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900697 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900698 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200699 switch (action) {
700 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100701 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200702 break;
703 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100704 appctx->ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200705 break;
706 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100707 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200708 break;
709 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100710 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900711 return;
712 }
713
714 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200715 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100716 appctx->ctx.cli.msg = stats_permission_denied_msg;
717 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900718 return;
719 }
720
Willy Tarreau07115412012-10-29 21:56:59 +0100721 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900722
Willy Tarreaudec98142012-06-06 23:37:08 +0200723 switch (action) {
724 case STAT_CLI_O_TAB:
725 if (!ts)
726 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100727 chunk_reset(&trash);
728 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900729 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100730 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900731 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200732
733 case STAT_CLI_O_CLR:
734 if (!ts)
735 return;
736 if (ts->ref_cnt) {
737 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100738 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
739 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200740 return;
741 }
742 stksess_kill(&px->table, ts);
743 break;
Simon Horman17bce342011-06-15 15:18:47 +0900744
Willy Tarreau654694e2012-06-07 01:03:16 +0200745 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200746 if (ts)
747 stktable_touch(&px->table, ts, 1);
748 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100749 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200750 if (!ts) {
751 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100752 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
753 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200754 return;
755 }
756 stktable_store(&px->table, ts, 1);
757 }
758
Willy Tarreau47060b62013-08-01 21:11:42 +0200759 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
760 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100761 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
762 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200763 return;
764 }
765
766 data_type = stktable_get_data_type(args[cur_arg] + 5);
767 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100768 appctx->ctx.cli.msg = "Unknown data type\n";
769 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200770 return;
771 }
772
773 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100774 appctx->ctx.cli.msg = "Data type not stored in this table\n";
775 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200776 return;
777 }
778
779 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100780 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
781 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200782 return;
783 }
784
785 ptr = stktable_data_ptr(&px->table, ts, data_type);
786
787 switch (stktable_data_types[data_type].std_type) {
788 case STD_T_SINT:
789 stktable_data_cast(ptr, std_t_sint) = value;
790 break;
791 case STD_T_UINT:
792 stktable_data_cast(ptr, std_t_uint) = value;
793 break;
794 case STD_T_ULL:
795 stktable_data_cast(ptr, std_t_ull) = value;
796 break;
797 case STD_T_FRQP:
798 /* We set both the current and previous values. That way
799 * the reported frequency is stable during all the period
800 * then slowly fades out. This allows external tools to
801 * push measures without having to update them too often.
802 */
803 frqp = &stktable_data_cast(ptr, std_t_frqp);
804 frqp->curr_tick = now_ms;
805 frqp->prev_ctr = 0;
806 frqp->curr_ctr = value;
807 break;
808 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200809 }
810 break;
811
Willy Tarreaudec98142012-06-06 23:37:08 +0200812 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100813 appctx->ctx.cli.msg = "Unknown action\n";
814 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200815 break;
Simon Horman121f3052011-06-15 15:18:46 +0900816 }
Simon Horman121f3052011-06-15 15:18:46 +0900817}
818
Willy Tarreau654694e2012-06-07 01:03:16 +0200819static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900820{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100821 struct appctx *appctx = __objt_appctx(si->end);
822
Willy Tarreau04b3a192013-04-13 09:41:37 +0200823 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100824 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
825 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200826 return;
827 }
828
Simon Hormand5b9fd92011-06-15 15:18:48 +0900829 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100830 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
831 if (appctx->ctx.table.data_type < 0) {
832 appctx->ctx.cli.msg = "Unknown data type\n";
833 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900834 return;
835 }
836
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100837 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
838 appctx->ctx.cli.msg = "Data type not stored in this table\n";
839 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900840 return;
841 }
842
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100843 appctx->ctx.table.data_op = get_std_op(args[4]);
844 if (appctx->ctx.table.data_op < 0) {
845 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
846 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900847 return;
848 }
849
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100850 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
851 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
852 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900853 return;
854 }
855}
856
Willy Tarreaudec98142012-06-06 23:37:08 +0200857static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900858{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100859 struct appctx *appctx = __objt_appctx(si->end);
860
861 appctx->ctx.table.data_type = -1;
862 appctx->st2 = STAT_ST_INIT;
863 appctx->ctx.table.target = NULL;
864 appctx->ctx.table.proxy = NULL;
865 appctx->ctx.table.entry = NULL;
866 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900867
868 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200869 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100870 if (!appctx->ctx.table.target) {
871 appctx->ctx.cli.msg = "No such table\n";
872 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900873 return;
874 }
875 }
876 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200877 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900878 goto err_args;
879 return;
880 }
881
882 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200883 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900884 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200885 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900886 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900887 goto err_args;
888
889 return;
890
891err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200892 switch (action) {
893 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100894 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200895 break;
896 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100897 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200898 break;
899 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100900 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200901 break;
902 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100903 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900904}
905
Willy Tarreau532a4502011-09-07 22:37:44 +0200906/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +0200907 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +0200908 * level to be admin.
909 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200910static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +0200911{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100912 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200913 struct proxy *px;
914
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200915 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100916 appctx->ctx.cli.msg = stats_permission_denied_msg;
917 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200918 return NULL;
919 }
920
921 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100922 appctx->ctx.cli.msg = "A frontend name is expected.\n";
923 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200924 return NULL;
925 }
926
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200927 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +0200928 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100929 appctx->ctx.cli.msg = "No such frontend.\n";
930 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200931 return NULL;
932 }
933 return px;
934}
935
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200936/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
937 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +0200938 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200939 * the <arg> is modified to remove the '/'.
940 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200941static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200942{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100943 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200944 struct proxy *px;
945 struct server *sv;
946 char *line;
947
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200948 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100949 appctx->ctx.cli.msg = stats_permission_denied_msg;
950 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200951 return NULL;
952 }
953
954 /* split "backend/server" and make <line> point to server */
955 for (line = arg; *line; line++)
956 if (*line == '/') {
957 *line++ = '\0';
958 break;
959 }
960
961 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100962 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
963 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200964 return NULL;
965 }
966
967 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100968 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
969 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200970 return NULL;
971 }
972
973 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100974 appctx->ctx.cli.msg = "Proxy is disabled.\n";
975 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200976 return NULL;
977 }
978
979 return sv;
980}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200981
982/* This function is used with TLS ticket keys management. It permits to browse
983 * each reference. The variable <getnext> must contain the current node,
984 * <end> point to the root node.
985 */
986#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
987static inline
988struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
989{
990 struct tls_keys_ref *ref = getnext;
991
992 while (1) {
993
994 /* Get next list entry. */
995 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
996
997 /* If the entry is the last of the list, return NULL. */
998 if (&ref->list == end)
999 return NULL;
1000
1001 return ref;
1002 }
1003}
1004
1005static inline
1006struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1007{
1008 int id;
1009 char *error;
1010
1011 /* If the reference starts by a '#', this is numeric id. */
1012 if (reference[0] == '#') {
1013 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1014 id = strtol(reference + 1, &error, 10);
1015 if (*error != '\0')
1016 return NULL;
1017
1018 /* Perform the unique id lookup. */
1019 return tlskeys_ref_lookupid(id);
1020 }
1021
1022 /* Perform the string lookup. */
1023 return tlskeys_ref_lookup(reference);
1024}
1025#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001026
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001027/* This function is used with map and acl management. It permits to browse
1028 * each reference. The variable <getnext> must contain the current node,
1029 * <end> point to the root node and the <flags> permit to filter required
1030 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001031 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001032static inline
1033struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1034 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001035{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001036 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001037
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001038 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001039
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001040 /* Get next list entry. */
1041 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001042
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001043 /* If the entry is the last of the list, return NULL. */
1044 if (&ref->list == end)
1045 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001046
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001047 /* If the entry match the flag, return it. */
1048 if (ref->flags & flags)
1049 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001050 }
1051}
1052
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001053static inline
1054struct pat_ref *pat_ref_lookup_ref(const char *reference)
1055{
1056 int id;
1057 char *error;
1058
1059 /* If the reference starts by a '#', this is numeric id. */
1060 if (reference[0] == '#') {
1061 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1062 id = strtol(reference + 1, &error, 10);
1063 if (*error != '\0')
1064 return NULL;
1065
1066 /* Perform the unique id lookup. */
1067 return pat_ref_lookupid(id);
1068 }
1069
1070 /* Perform the string lookup. */
1071 return pat_ref_lookup(reference);
1072}
1073
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001074/* This function is used with map and acl management. It permits to browse
1075 * each reference.
1076 */
1077static inline
1078struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1079{
1080 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001081 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1082 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001083 return NULL;
1084 return expr;
1085}
1086
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001087/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001088 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001089 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001090 * designating the function which will have to process the request, which can
1091 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001092 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001093static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001094{
Willy Tarreau87b09662015-04-03 00:22:06 +02001095 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001096 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001097 char *args[MAX_STATS_ARGS + 1];
1098 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001099 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001100
1101 while (isspace((unsigned char)*line))
1102 line++;
1103
1104 arg = 0;
1105 args[arg] = line;
1106
1107 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001108 if (*line == '\\') {
1109 line++;
1110 if (*line == '\0')
1111 break;
1112 }
1113 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001114 *line++ = '\0';
1115
1116 while (isspace((unsigned char)*line))
1117 line++;
1118
1119 args[++arg] = line;
1120 continue;
1121 }
1122
1123 line++;
1124 }
1125
1126 while (++arg <= MAX_STATS_ARGS)
1127 args[arg] = line;
1128
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001129 /* remove \ */
1130 arg = 0;
1131 while (*args[arg] != '\0') {
1132 j = 0;
1133 for (i=0; args[arg][i] != '\0'; i++) {
1134 if (args[arg][i] == '\\')
1135 continue;
1136 args[arg][j] = args[arg][i];
1137 j++;
1138 }
1139 args[arg][j] = '\0';
1140 arg++;
1141 }
1142
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001143 appctx->ctx.stats.scope_str = 0;
1144 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001145 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001146 if (strcmp(args[0], "show") == 0) {
1147 if (strcmp(args[1], "stat") == 0) {
1148 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001149 appctx->ctx.stats.flags |= STAT_BOUND;
1150 appctx->ctx.stats.iid = atoi(args[2]);
1151 appctx->ctx.stats.type = atoi(args[3]);
1152 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001153 }
1154
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001155 appctx->st2 = STAT_ST_INIT;
1156 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001157 }
1158 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001159 appctx->st2 = STAT_ST_INIT;
1160 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001161 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001162 else if (strcmp(args[1], "pools") == 0) {
1163 appctx->st2 = STAT_ST_INIT;
1164 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1165 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001166 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001167 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001168 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001169 appctx->ctx.cli.msg = stats_permission_denied_msg;
1170 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001171 return 1;
1172 }
Willy Tarreau76153662012-11-26 01:16:39 +01001173 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001174 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001175 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001176 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001177 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001178 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001179 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001180 appctx->ctx.sess.pos = 0;
1181 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001182 }
1183 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001184 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001185 appctx->ctx.cli.msg = stats_permission_denied_msg;
1186 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001187 return 1;
1188 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001189 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001190 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001191 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001192 appctx->ctx.errors.iid = -1;
1193 appctx->ctx.errors.px = NULL;
1194 appctx->st2 = STAT_ST_INIT;
1195 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001196 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001197 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001198 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001199 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001200 else if (strcmp(args[1], "tls-keys") == 0) {
1201#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1202 appctx->st2 = STAT_ST_INIT;
1203 appctx->st0 = STAT_CLI_O_TLSK;
1204#else
1205 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1206 "that doesn't support specifying TLS ticket keys\n";
1207 appctx->st0 = STAT_CLI_PRINT;
1208#endif
1209 return 1;
1210 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001211 else if (strcmp(args[1], "map") == 0 ||
1212 strcmp(args[1], "acl") == 0) {
1213
1214 /* Set ACL or MAP flags. */
1215 if (args[1][0] == 'm')
1216 appctx->ctx.map.display_flags = PAT_REF_MAP;
1217 else
1218 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001219
1220 /* no parameter: display all map avalaible */
1221 if (!*args[2]) {
1222 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001223 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001224 return 1;
1225 }
1226
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001227 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001228 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001229 if (!appctx->ctx.map.ref ||
1230 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1231 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001232 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001233 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001234 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001235 appctx->st0 = STAT_CLI_PRINT;
1236 return 1;
1237 }
1238 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001239 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001240 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001241 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001242 return 0;
1243 }
1244 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001245 else if (strcmp(args[0], "clear") == 0) {
1246 if (strcmp(args[1], "counters") == 0) {
1247 struct proxy *px;
1248 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001249 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001250 int clrall = 0;
1251
1252 if (strcmp(args[2], "all") == 0)
1253 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001254
Willy Tarreau6162db22009-10-10 17:13:00 +02001255 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001256 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1257 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001258 appctx->ctx.cli.msg = stats_permission_denied_msg;
1259 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001260 return 1;
1261 }
1262
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001263 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001264 if (clrall) {
1265 memset(&px->be_counters, 0, sizeof(px->be_counters));
1266 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1267 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001268 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001269 px->be_counters.conn_max = 0;
1270 px->be_counters.p.http.rps_max = 0;
1271 px->be_counters.sps_max = 0;
1272 px->be_counters.cps_max = 0;
1273 px->be_counters.nbpend_max = 0;
1274
1275 px->fe_counters.conn_max = 0;
1276 px->fe_counters.p.http.rps_max = 0;
1277 px->fe_counters.sps_max = 0;
1278 px->fe_counters.cps_max = 0;
1279 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001280 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001281
1282 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001283 if (clrall)
1284 memset(&sv->counters, 0, sizeof(sv->counters));
1285 else {
1286 sv->counters.cur_sess_max = 0;
1287 sv->counters.nbpend_max = 0;
1288 sv->counters.sps_max = 0;
1289 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001290
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001291 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001292 if (li->counters) {
1293 if (clrall)
1294 memset(li->counters, 0, sizeof(*li->counters));
1295 else
1296 li->counters->conn_max = 0;
1297 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001298 }
1299
Willy Tarreau81c25d02011-09-07 15:17:21 +02001300 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001301 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001302 return 1;
1303 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001304 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001305 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001306 /* end of processing */
1307 return 1;
1308 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001309 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1310 /* Set ACL or MAP flags. */
1311 if (args[1][0] == 'm')
1312 appctx->ctx.map.display_flags = PAT_REF_MAP;
1313 else
1314 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001315
1316 /* no parameter */
1317 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001318 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1319 appctx->ctx.cli.msg = "Missing map identifier.\n";
1320 else
1321 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001322 appctx->st0 = STAT_CLI_PRINT;
1323 return 1;
1324 }
1325
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001326 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001327 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001328 if (!appctx->ctx.map.ref ||
1329 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1330 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001331 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001332 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001333 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001334 appctx->st0 = STAT_CLI_PRINT;
1335 return 1;
1336 }
1337
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001338 /* Clear all. */
1339 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001340
1341 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001342 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001343 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001344 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001345 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001346 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001347 return 0;
1348 }
1349 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001350 else if (strcmp(args[0], "get") == 0) {
1351 if (strcmp(args[1], "weight") == 0) {
1352 struct proxy *px;
1353 struct server *sv;
1354
1355 /* split "backend/server" and make <line> point to server */
1356 for (line = args[2]; *line; line++)
1357 if (*line == '/') {
1358 *line++ = '\0';
1359 break;
1360 }
1361
1362 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001363 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1364 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001365 return 1;
1366 }
1367
1368 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001369 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1370 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001371 return 1;
1372 }
1373
1374 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001375 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001376 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001377 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001378
Willy Tarreau38338fa2009-10-10 18:37:29 +02001379 return 1;
1380 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001381 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1382 /* Set flags. */
1383 if (args[1][0] == 'm')
1384 appctx->ctx.map.display_flags = PAT_REF_MAP;
1385 else
1386 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001387
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001388 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001389 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001390 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1391 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1392 else
1393 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001394 appctx->st0 = STAT_CLI_PRINT;
1395 return 1;
1396 }
1397
1398 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001399 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001400 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001401 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001402 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001403 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001404 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001405 appctx->st0 = STAT_CLI_PRINT;
1406 return 1;
1407 }
1408
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001409 /* copy input string. The string must be allocated because
1410 * it may be used over multiple iterations. It's released
1411 * at the end and upon abort anyway.
1412 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001413 appctx->ctx.map.chunk.len = strlen(args[3]);
1414 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1415 appctx->ctx.map.chunk.str = strdup(args[3]);
1416 if (!appctx->ctx.map.chunk.str) {
1417 appctx->ctx.cli.msg = "Out of memory error.\n";
1418 appctx->st0 = STAT_CLI_PRINT;
1419 return 1;
1420 }
1421
1422 /* prepare response */
1423 appctx->st2 = STAT_ST_INIT;
1424 appctx->st0 = STAT_CLI_O_MLOOK;
1425 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001426 else { /* not "get weight" */
1427 return 0;
1428 }
1429 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001430 else if (strcmp(args[0], "set") == 0) {
1431 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001432 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001433 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001434
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001435 sv = expect_server_admin(s, si, args[2]);
1436 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001437 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001438
Simon Horman7d09b9a2013-02-12 10:45:51 +09001439 warning = server_parse_weight_change_request(sv, args[3]);
1440 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001441 appctx->ctx.cli.msg = warning;
1442 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001443 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001444 return 1;
1445 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001446 else if (strcmp(args[1], "server") == 0) {
1447 struct server *sv;
1448 const char *warning;
1449
1450 sv = expect_server_admin(s, si, args[2]);
1451 if (!sv)
1452 return 1;
1453
1454 if (strcmp(args[3], "weight") == 0) {
1455 warning = server_parse_weight_change_request(sv, args[4]);
1456 if (warning) {
1457 appctx->ctx.cli.msg = warning;
1458 appctx->st0 = STAT_CLI_PRINT;
1459 }
1460 }
1461 else if (strcmp(args[3], "state") == 0) {
1462 if (strcmp(args[4], "ready") == 0)
1463 srv_adm_set_ready(sv);
1464 else if (strcmp(args[4], "drain") == 0)
1465 srv_adm_set_drain(sv);
1466 else if (strcmp(args[4], "maint") == 0)
1467 srv_adm_set_maint(sv);
1468 else {
1469 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1470 appctx->st0 = STAT_CLI_PRINT;
1471 }
1472 }
1473 else if (strcmp(args[3], "health") == 0) {
1474 if (sv->track) {
1475 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1476 appctx->st0 = STAT_CLI_PRINT;
1477 }
1478 else if (strcmp(args[4], "up") == 0) {
1479 sv->check.health = sv->check.rise + sv->check.fall - 1;
1480 srv_set_running(sv, "changed from CLI");
1481 }
1482 else if (strcmp(args[4], "stopping") == 0) {
1483 sv->check.health = sv->check.rise + sv->check.fall - 1;
1484 srv_set_stopping(sv, "changed from CLI");
1485 }
1486 else if (strcmp(args[4], "down") == 0) {
1487 sv->check.health = 0;
1488 srv_set_stopped(sv, "changed from CLI");
1489 }
1490 else {
1491 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1492 appctx->st0 = STAT_CLI_PRINT;
1493 }
1494 }
1495 else if (strcmp(args[3], "agent") == 0) {
1496 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1497 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1498 appctx->st0 = STAT_CLI_PRINT;
1499 }
1500 else if (strcmp(args[4], "up") == 0) {
1501 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1502 srv_set_running(sv, "changed from CLI");
1503 }
1504 else if (strcmp(args[4], "down") == 0) {
1505 sv->agent.health = 0;
1506 srv_set_stopped(sv, "changed from CLI");
1507 }
1508 else {
1509 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1510 appctx->st0 = STAT_CLI_PRINT;
1511 }
1512 }
1513 else {
1514 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1515 appctx->st0 = STAT_CLI_PRINT;
1516 }
1517 return 1;
1518 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001519 else if (strcmp(args[1], "timeout") == 0) {
1520 if (strcmp(args[2], "cli") == 0) {
1521 unsigned timeout;
1522 const char *res;
1523
1524 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001525 appctx->ctx.cli.msg = "Expects an integer value.\n";
1526 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001527 return 1;
1528 }
1529
1530 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1531 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001532 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1533 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001534 return 1;
1535 }
1536
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001537 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001538 return 1;
1539 }
1540 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001541 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1542 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001543 return 1;
1544 }
1545 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001546 else if (strcmp(args[1], "maxconn") == 0) {
1547 if (strcmp(args[2], "frontend") == 0) {
1548 struct proxy *px;
1549 struct listener *l;
1550 int v;
1551
Willy Tarreau532a4502011-09-07 22:37:44 +02001552 px = expect_frontend_admin(s, si, args[3]);
1553 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001554 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001555
1556 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001557 appctx->ctx.cli.msg = "Integer value expected.\n";
1558 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001559 return 1;
1560 }
1561
1562 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001563 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001564 appctx->ctx.cli.msg = "Value out of range.\n";
1565 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001566 return 1;
1567 }
1568
1569 /* OK, the value is fine, so we assign it to the proxy and to all of
1570 * its listeners. The blocked ones will be dequeued.
1571 */
1572 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001573 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001574 l->maxconn = v;
1575 if (l->state == LI_FULL)
1576 resume_listener(l);
1577 }
1578
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001579 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1580 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001581
1582 return 1;
1583 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001584 else if (strcmp(args[2], "global") == 0) {
1585 int v;
1586
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001587 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001588 appctx->ctx.cli.msg = stats_permission_denied_msg;
1589 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001590 return 1;
1591 }
1592
1593 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001594 appctx->ctx.cli.msg = "Expects an integer value.\n";
1595 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001596 return 1;
1597 }
1598
1599 v = atoi(args[3]);
1600 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001601 appctx->ctx.cli.msg = "Value out of range.\n";
1602 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001603 return 1;
1604 }
1605
1606 /* check for unlimited values */
1607 if (v <= 0)
1608 v = global.hardmaxconn;
1609
1610 global.maxconn = v;
1611
1612 /* Dequeues all of the listeners waiting for a resource */
1613 if (!LIST_ISEMPTY(&global_listener_queue))
1614 dequeue_all_listeners(&global_listener_queue);
1615
1616 return 1;
1617 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001618 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001619 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1620 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001621 return 1;
1622 }
1623 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001624 else if (strcmp(args[1], "rate-limit") == 0) {
1625 if (strcmp(args[2], "connections") == 0) {
1626 if (strcmp(args[3], "global") == 0) {
1627 int v;
1628
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001629 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001630 appctx->ctx.cli.msg = stats_permission_denied_msg;
1631 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001632 return 1;
1633 }
1634
1635 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001636 appctx->ctx.cli.msg = "Expects an integer value.\n";
1637 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001638 return 1;
1639 }
1640
1641 v = atoi(args[4]);
1642 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001643 appctx->ctx.cli.msg = "Value out of range.\n";
1644 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001645 return 1;
1646 }
1647
1648 global.cps_lim = v;
1649
1650 /* Dequeues all of the listeners waiting for a resource */
1651 if (!LIST_ISEMPTY(&global_listener_queue))
1652 dequeue_all_listeners(&global_listener_queue);
1653
1654 return 1;
1655 }
1656 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001657 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1658 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001659 return 1;
1660 }
1661 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001662 else if (strcmp(args[2], "sessions") == 0) {
1663 if (strcmp(args[3], "global") == 0) {
1664 int v;
1665
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001666 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001667 appctx->ctx.cli.msg = stats_permission_denied_msg;
1668 appctx->st0 = STAT_CLI_PRINT;
1669 return 1;
1670 }
1671
1672 if (!*args[4]) {
1673 appctx->ctx.cli.msg = "Expects an integer value.\n";
1674 appctx->st0 = STAT_CLI_PRINT;
1675 return 1;
1676 }
1677
1678 v = atoi(args[4]);
1679 if (v < 0) {
1680 appctx->ctx.cli.msg = "Value out of range.\n";
1681 appctx->st0 = STAT_CLI_PRINT;
1682 return 1;
1683 }
1684
1685 global.sps_lim = v;
1686
1687 /* Dequeues all of the listeners waiting for a resource */
1688 if (!LIST_ISEMPTY(&global_listener_queue))
1689 dequeue_all_listeners(&global_listener_queue);
1690
1691 return 1;
1692 }
1693 else {
1694 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1695 appctx->st0 = STAT_CLI_PRINT;
1696 return 1;
1697 }
1698 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001699#ifdef USE_OPENSSL
1700 else if (strcmp(args[2], "ssl-sessions") == 0) {
1701 if (strcmp(args[3], "global") == 0) {
1702 int v;
1703
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001704 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001705 appctx->ctx.cli.msg = stats_permission_denied_msg;
1706 appctx->st0 = STAT_CLI_PRINT;
1707 return 1;
1708 }
1709
1710 if (!*args[4]) {
1711 appctx->ctx.cli.msg = "Expects an integer value.\n";
1712 appctx->st0 = STAT_CLI_PRINT;
1713 return 1;
1714 }
1715
1716 v = atoi(args[4]);
1717 if (v < 0) {
1718 appctx->ctx.cli.msg = "Value out of range.\n";
1719 appctx->st0 = STAT_CLI_PRINT;
1720 return 1;
1721 }
1722
1723 global.ssl_lim = v;
1724
1725 /* Dequeues all of the listeners waiting for a resource */
1726 if (!LIST_ISEMPTY(&global_listener_queue))
1727 dequeue_all_listeners(&global_listener_queue);
1728
1729 return 1;
1730 }
1731 else {
1732 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1733 appctx->st0 = STAT_CLI_PRINT;
1734 return 1;
1735 }
1736 }
1737#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001738 else if (strcmp(args[2], "http-compression") == 0) {
1739 if (strcmp(args[3], "global") == 0) {
1740 int v;
1741
Willy Tarreau85d47f92012-11-21 00:29:50 +01001742 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001743 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1744 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001745 return 1;
1746 }
1747
William Lallemandd85f9172012-11-09 17:05:39 +01001748 v = atoi(args[4]);
1749 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1750 }
1751 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001752 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1753 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001754 return 1;
1755 }
1756 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001757 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001758 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001759 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001760 return 1;
1761 }
1762 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001763 else if (strcmp(args[1], "table") == 0) {
1764 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1765 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001766 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001767 char *err;
1768
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001769 /* Set flags. */
1770 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001771
1772 /* Expect three parameters: map name, key and new value. */
1773 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001774 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001775 appctx->st0 = STAT_CLI_PRINT;
1776 return 1;
1777 }
1778
1779 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001780 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001781 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001782 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001783 appctx->st0 = STAT_CLI_PRINT;
1784 return 1;
1785 }
1786
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001787 /* If the entry identifier start with a '#', it is considered as
1788 * pointer id
1789 */
1790 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1791 struct pat_ref_elt *ref;
1792 long long int conv;
1793 char *error;
1794
1795 /* Convert argument to integer value. */
1796 conv = strtoll(&args[3][1], &error, 16);
1797 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001798 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001799 appctx->st0 = STAT_CLI_PRINT;
1800 return 1;
1801 }
1802
1803 /* Convert and check integer to pointer. */
1804 ref = (struct pat_ref_elt *)(long)conv;
1805 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001806 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001807 appctx->st0 = STAT_CLI_PRINT;
1808 return 1;
1809 }
1810
1811 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001812 err = NULL;
1813 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1814 if (err)
1815 memprintf(&err, "%s.\n", err);
1816 appctx->ctx.cli.err = err;
1817 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001818 return 1;
1819 }
1820 }
1821 else {
1822 /* Else, use the entry identifier as pattern
1823 * string, and update the value.
1824 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001825 err = NULL;
1826 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1827 if (err)
1828 memprintf(&err, "%s.\n", err);
1829 appctx->ctx.cli.err = err;
1830 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001831 return 1;
1832 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001833 }
1834
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001835 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001836 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001837 return 1;
1838 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001839#ifdef USE_OPENSSL
1840 else if (strcmp(args[1], "ssl") == 0) {
1841 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01001842#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001843 char *err = NULL;
1844
Emeric Brunaf4ef742014-06-19 14:10:45 +02001845 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001846 if (!*args[3]) {
1847 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1848 appctx->st0 = STAT_CLI_PRINT;
1849 return 1;
1850 }
1851
1852 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1853 if (trash.len < 0) {
1854 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1855 appctx->st0 = STAT_CLI_PRINT;
1856 return 1;
1857 }
1858
1859 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1860 if (err) {
1861 memprintf(&err, "%s.\n", err);
1862 appctx->ctx.cli.err = err;
1863 appctx->st0 = STAT_CLI_PRINT_FREE;
1864 }
1865 return 1;
1866 }
1867 appctx->ctx.cli.msg = "OCSP Response updated!";
1868 appctx->st0 = STAT_CLI_PRINT;
1869 return 1;
1870#else
1871 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1872 appctx->st0 = STAT_CLI_PRINT;
1873 return 1;
1874#endif
1875 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001876 else if (strcmp(args[2], "tls-key") == 0) {
1877#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1878 /* Expect two parameters: the filename and the new new TLS key in encoding */
1879 if (!*args[3] || !*args[4]) {
1880 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
1881 appctx->st0 = STAT_CLI_PRINT;
1882 return 1;
1883 }
1884
1885 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
1886 if(!appctx->ctx.tlskeys.ref) {
1887 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
1888 appctx->st0 = STAT_CLI_PRINT;
1889 return 1;
1890 }
1891
1892 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
1893 if (trash.len != sizeof(struct tls_sess_key)) {
1894 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
1895 appctx->st0 = STAT_CLI_PRINT;
1896 return 1;
1897 }
1898
1899 memcpy(appctx->ctx.tlskeys.ref->tlskeys + 2 % TLS_TICKETS_NO, trash.str, trash.len);
1900 appctx->ctx.tlskeys.ref->tls_ticket_enc_index = appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 1 % TLS_TICKETS_NO;
1901
1902 appctx->ctx.cli.msg = "TLS ticket key updated!";
1903 appctx->st0 = STAT_CLI_PRINT;
1904 return 1;
1905#else
1906 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1907 "that doesn't support specifying TLS ticket keys\n";
1908 appctx->st0 = STAT_CLI_PRINT;
1909 return 1;
1910#endif
1911 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001912 else {
1913 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1914 appctx->st0 = STAT_CLI_PRINT;
1915 return 1;
1916 }
1917 }
1918#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001919 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001920 return 0;
1921 }
1922 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001923 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001924 if (strcmp(args[1], "agent") == 0) {
1925 struct server *sv;
1926
1927 sv = expect_server_admin(s, si, args[2]);
1928 if (!sv)
1929 return 1;
1930
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001931 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1932 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1933 appctx->st0 = STAT_CLI_PRINT;
1934 return 1;
1935 }
1936
1937 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001938 return 1;
1939 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001940 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001941 struct server *sv;
1942
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001943 sv = expect_server_admin(s, si, args[2]);
1944 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001945 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001946
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001947 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1948 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1949 appctx->st0 = STAT_CLI_PRINT;
1950 return 1;
1951 }
1952
1953 sv->check.state |= CHK_ST_ENABLED;
1954 return 1;
1955 }
1956 else if (strcmp(args[1], "server") == 0) {
1957 struct server *sv;
1958
1959 sv = expect_server_admin(s, si, args[2]);
1960 if (!sv)
1961 return 1;
1962
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001963 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001964 return 1;
1965 }
1966 else if (strcmp(args[1], "frontend") == 0) {
1967 struct proxy *px;
1968
1969 px = expect_frontend_admin(s, si, args[2]);
1970 if (!px)
1971 return 1;
1972
1973 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001974 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1975 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001976 return 1;
1977 }
1978
1979 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001980 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1981 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001982 return 1;
1983 }
1984
1985 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001986 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1987 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001988 return 1;
1989 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001990 return 1;
1991 }
1992 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001993 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001994 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001995 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001996 }
1997 }
1998 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001999 if (strcmp(args[1], "agent") == 0) {
2000 struct server *sv;
2001
2002 sv = expect_server_admin(s, si, args[2]);
2003 if (!sv)
2004 return 1;
2005
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002006 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002007 return 1;
2008 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002009 else if (strcmp(args[1], "health") == 0) {
2010 struct server *sv;
2011
2012 sv = expect_server_admin(s, si, args[2]);
2013 if (!sv)
2014 return 1;
2015
2016 sv->check.state &= ~CHK_ST_ENABLED;
2017 return 1;
2018 }
Simon Horman671b6f02013-11-25 10:46:39 +09002019 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002020 struct server *sv;
2021
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002022 sv = expect_server_admin(s, si, args[2]);
2023 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002024 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002025
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002026 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002027 return 1;
2028 }
2029 else if (strcmp(args[1], "frontend") == 0) {
2030 struct proxy *px;
2031
2032 px = expect_frontend_admin(s, si, args[2]);
2033 if (!px)
2034 return 1;
2035
2036 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002037 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2038 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002039 return 1;
2040 }
2041
2042 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002043 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2044 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002045 return 1;
2046 }
2047
2048 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002049 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2050 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002051 return 1;
2052 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002053 return 1;
2054 }
2055 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002056 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002057 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002058 return 1;
2059 }
2060 }
2061 else if (strcmp(args[0], "shutdown") == 0) {
2062 if (strcmp(args[1], "frontend") == 0) {
2063 struct proxy *px;
2064
2065 px = expect_frontend_admin(s, si, args[2]);
2066 if (!px)
2067 return 1;
2068
2069 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002070 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2071 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002072 return 1;
2073 }
2074
2075 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2076 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2077 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2078 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2079 stop_proxy(px);
2080 return 1;
2081 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002082 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002083 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002084
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002085 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002086 appctx->ctx.cli.msg = stats_permission_denied_msg;
2087 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002088 return 1;
2089 }
2090
2091 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002092 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2093 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002094 return 1;
2095 }
2096
2097 ptr = (void *)strtoul(args[2], NULL, 0);
2098
Willy Tarreau87b09662015-04-03 00:22:06 +02002099 /* first, look for the requested stream in the stream table */
2100 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002101 if (sess == ptr)
2102 break;
2103 }
2104
Willy Tarreau87b09662015-04-03 00:22:06 +02002105 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002106 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002107 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2108 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002109 return 1;
2110 }
2111
Willy Tarreaue7dff022015-04-03 01:14:29 +02002112 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002113 return 1;
2114 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002115 else if (strcmp(args[1], "sessions") == 0) {
2116 if (strcmp(args[2], "server") == 0) {
2117 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002118 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002119
2120 sv = expect_server_admin(s, si, args[3]);
2121 if (!sv)
2122 return 1;
2123
Willy Tarreau87b09662015-04-03 00:22:06 +02002124 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002125 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2126 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002127 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002128
2129 return 1;
2130 }
2131 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002132 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2133 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002134 return 1;
2135 }
2136 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002137 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002138 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2139 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002140 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002141 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002142 }
2143 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002144 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2145 if (args[1][0] == 'm')
2146 appctx->ctx.map.display_flags = PAT_REF_MAP;
2147 else
2148 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002149
2150 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002151 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2152 if (!*args[2] || !*args[3]) {
2153 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2154 appctx->st0 = STAT_CLI_PRINT;
2155 return 1;
2156 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002157 }
2158
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002159 else {
2160 if (!*args[2] || !*args[3]) {
2161 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2162 appctx->st0 = STAT_CLI_PRINT;
2163 return 1;
2164 }
2165 }
2166
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002167 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002168 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002169 if (!appctx->ctx.map.ref ||
2170 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002171 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002172 appctx->st0 = STAT_CLI_PRINT;
2173 return 1;
2174 }
2175
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002176 /* If the entry identifier start with a '#', it is considered as
2177 * pointer id
2178 */
2179 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2180 struct pat_ref_elt *ref;
2181 long long int conv;
2182 char *error;
2183
2184 /* Convert argument to integer value. */
2185 conv = strtoll(&args[3][1], &error, 16);
2186 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002187 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002188 appctx->st0 = STAT_CLI_PRINT;
2189 return 1;
2190 }
2191
2192 /* Convert and check integer to pointer. */
2193 ref = (struct pat_ref_elt *)(long)conv;
2194 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002195 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002196 appctx->st0 = STAT_CLI_PRINT;
2197 return 1;
2198 }
2199
2200 /* Try to delete the entry. */
2201 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2202 /* The entry is not found, send message. */
2203 appctx->ctx.cli.msg = "Key not found.\n";
2204 appctx->st0 = STAT_CLI_PRINT;
2205 return 1;
2206 }
2207 }
2208 else {
2209 /* Else, use the entry identifier as pattern
2210 * string and try to delete the entry.
2211 */
2212 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2213 /* The entry is not found, send message. */
2214 appctx->ctx.cli.msg = "Key not found.\n";
2215 appctx->st0 = STAT_CLI_PRINT;
2216 return 1;
2217 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002218 }
2219
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002220 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002221 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002222 return 1;
2223 }
2224 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002225 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002226 appctx->st0 = STAT_CLI_PRINT;
2227 return 1;
2228 }
2229 }
2230 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002231 if (strcmp(args[1], "map") == 0 ||
2232 strcmp(args[1], "acl") == 0) {
2233 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002234 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002235
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002236 /* Set flags. */
2237 if (args[1][0] == 'm')
2238 appctx->ctx.map.display_flags = PAT_REF_MAP;
2239 else
2240 appctx->ctx.map.display_flags = PAT_REF_ACL;
2241
2242 /* If the keywork is "map", we expect three parameters, if it
2243 * is "acl", we expect only two parameters
2244 */
2245 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2246 if (!*args[2] || !*args[3] || !*args[4]) {
2247 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2248 appctx->st0 = STAT_CLI_PRINT;
2249 return 1;
2250 }
2251 }
2252 else {
2253 if (!*args[2] || !*args[3]) {
2254 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2255 appctx->st0 = STAT_CLI_PRINT;
2256 return 1;
2257 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002258 }
2259
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002260 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002261 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002262 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002263 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002264 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002265 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002266 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002267 appctx->st0 = STAT_CLI_PRINT;
2268 return 1;
2269 }
2270
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002271 /* The command "add acl" is prohibited if the reference
2272 * use samples.
2273 */
2274 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2275 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2276 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2277 "You must use the command 'add map' to add values.\n";
2278 appctx->st0 = STAT_CLI_PRINT;
2279 return 1;
2280 }
2281
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002282 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002283 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002284 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002285 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002286 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002287 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002288 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002289 if (err)
2290 memprintf(&err, "%s.\n", err);
2291 appctx->ctx.cli.err = err;
2292 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002293 return 1;
2294 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002295
2296 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002297 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002298 return 1;
2299 }
2300 else { /* unknown "del" parameter */
2301 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2302 appctx->st0 = STAT_CLI_PRINT;
2303 return 1;
2304 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002305 }
2306 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002307 return 0;
2308 }
2309 return 1;
2310}
2311
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002312/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002313 * used to processes I/O from/to the stats unix socket. The system relies on a
2314 * state machine handling requests and various responses. We read a request,
2315 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002316 * Then we can read again. The state is stored in appctx->st0 and is one of the
2317 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002318 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002319 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002320static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002321{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002322 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002323 struct channel *req = si_oc(si);
2324 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002325 int reql;
2326 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002327
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002328 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2329 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002330
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002331 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002332 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002333 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002334 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2335 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002336 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002337 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002338 /* Let's close for real now. We just close the request
2339 * side, the conditions below will complete if needed.
2340 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002341 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002342 break;
2343 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002344 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002345 /* ensure we have some output room left in the event we
2346 * would want to return some info right after parsing.
2347 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002348 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002349 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002350 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002351 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002352
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002353 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002354 if (reql <= 0) { /* closed or EOL not found */
2355 if (reql == 0)
2356 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002357 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002358 continue;
2359 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002360
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002361 /* seek for a possible semi-colon. If we find one, we
2362 * replace it with an LF and skip only this part.
2363 */
2364 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002365 if (trash.str[len] == ';') {
2366 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002367 reql = len + 1;
2368 break;
2369 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002370
Willy Tarreau816fc222009-10-04 07:36:58 +02002371 /* now it is time to check that we have a full line,
2372 * remove the trailing \n and possibly \r, then cut the
2373 * line.
2374 */
2375 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002376 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002377 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002378 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002379 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002380
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002381 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002382 len--;
2383
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002384 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002385
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002386 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002387 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002388 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002389 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002390 continue;
2391 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002392 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002393 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002394 else if (strcmp(trash.str, "help") == 0 ||
2395 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002396 appctx->ctx.cli.msg = stats_sock_usage_msg;
2397 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002398 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002399 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002400 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002401 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002402 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002403 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002404 /* if prompt is disabled, print help on empty lines,
2405 * so that the user at least knows how to enable
2406 * prompt and find help.
2407 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002408 appctx->ctx.cli.msg = stats_sock_usage_msg;
2409 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002410 }
2411
2412 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002413 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002414 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002415 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002416 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002417 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau00a37f02015-04-13 12:05:19 +02002418 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002419 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002420 continue;
2421 }
2422
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002423 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002424 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002425 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002426 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002427 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002428 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002429 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002430 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002431 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002432 free(appctx->ctx.cli.err);
2433 appctx->st0 = STAT_CLI_PROMPT;
2434 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002435 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002436 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002437 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002438 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002439 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002440 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002441 break;
2442 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002443 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002444 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002445 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002446 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002447 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002448 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002449 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002450 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002451 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002452 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002453 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002454 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002455 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002456 if (stats_table_request(si, appctx->st0))
2457 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002458 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002459 case STAT_CLI_O_PATS:
2460 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002461 appctx->st0 = STAT_CLI_PROMPT;
2462 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002463 case STAT_CLI_O_PAT:
2464 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002465 appctx->st0 = STAT_CLI_PROMPT;
2466 break;
2467 case STAT_CLI_O_MLOOK:
2468 if (stats_map_lookup(si))
2469 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002470 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002471 case STAT_CLI_O_POOLS:
2472 if (stats_dump_pools_to_buffer(si))
2473 appctx->st0 = STAT_CLI_PROMPT;
2474 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002475#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2476 case STAT_CLI_O_TLSK:
2477 if (stats_tlskeys_list(si))
2478 appctx->st0 = STAT_CLI_PROMPT;
2479 break;
2480#endif
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002481 default: /* abnormal state */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002482 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002483 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002484 break;
2485 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002486
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002487 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002488 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002489 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002490 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002491 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002492 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002493 }
2494
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002495 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002496 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002497 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002498
2499 /* Now we close the output if one of the writers did so,
2500 * or if we're not in interactive mode and the request
2501 * buffer is empty. This still allows pipelined requests
2502 * to be sent in non-interactive mode.
2503 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002504 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2505 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002506 continue;
2507 }
2508
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002509 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002510 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002511 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002512 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002513
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002514 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002515 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2516 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002517 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002518 * and nothing more to consume. This is comparable to a broken pipe, so
2519 * we forward the close to the request side so that it flows upstream to
2520 * the client.
2521 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002522 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002523 }
2524
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002525 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002526 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2527 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2528 /* We have no more processing to do, and nothing more to send, and
2529 * the client side has closed. So we'll forward this state downstream
2530 * on the response buffer.
2531 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002532 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002533 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002534 }
2535
Willy Tarreau828824a2015-04-19 17:20:03 +02002536 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002537 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002538 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002539 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002540}
2541
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002542/* This function dumps information onto the stream interface's read buffer.
2543 * It returns 0 as long as it does not complete, non-zero upon completion.
2544 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002545 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002546static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002547{
2548 unsigned int up = (now.tv_sec - start_date.tv_sec);
2549
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002550#ifdef USE_OPENSSL
2551 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2552 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2553 int ssl_reuse = 0;
2554
2555 if (ssl_key_rate < ssl_sess_rate) {
2556 /* count the ssl reuse ratio and avoid overflows in both directions */
2557 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2558 }
2559#endif
2560
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002561 chunk_printf(&trash,
2562 "Name: " PRODUCT_NAME "\n"
2563 "Version: " HAPROXY_VERSION "\n"
2564 "Release_date: " HAPROXY_DATE "\n"
2565 "Nbproc: %d\n"
2566 "Process_num: %d\n"
2567 "Pid: %d\n"
2568 "Uptime: %dd %dh%02dm%02ds\n"
2569 "Uptime_sec: %d\n"
2570 "Memmax_MB: %d\n"
2571 "Ulimit-n: %d\n"
2572 "Maxsock: %d\n"
2573 "Maxconn: %d\n"
2574 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002575 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002576 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002577 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002578#ifdef USE_OPENSSL
2579 "MaxSslConns: %d\n"
2580 "CurrSslConns: %d\n"
2581 "CumSslConns: %d\n"
2582#endif
2583 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002584 "PipesUsed: %d\n"
2585 "PipesFree: %d\n"
2586 "ConnRate: %d\n"
2587 "ConnRateLimit: %d\n"
2588 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002589 "SessRate: %d\n"
2590 "SessRateLimit: %d\n"
2591 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002592#ifdef USE_OPENSSL
2593 "SslRate: %d\n"
2594 "SslRateLimit: %d\n"
2595 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002596 "SslFrontendKeyRate: %d\n"
2597 "SslFrontendMaxKeyRate: %d\n"
2598 "SslFrontendSessionReuse_pct: %d\n"
2599 "SslBackendKeyRate: %d\n"
2600 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002601 "SslCacheLookups: %u\n"
2602 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002603#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002604 "CompressBpsIn: %u\n"
2605 "CompressBpsOut: %u\n"
2606 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002607#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002608 "ZlibMemUsage: %ld\n"
2609 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002610#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002611 "Tasks: %d\n"
2612 "Run_queue: %d\n"
2613 "Idle_pct: %d\n"
2614 "node: %s\n"
2615 "description: %s\n"
2616 "",
2617 global.nbproc,
2618 relative_pid,
2619 pid,
2620 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2621 up,
2622 global.rlimit_memmax,
2623 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002624 global.maxsock, global.maxconn, global.hardmaxconn,
2625 actconn, totalconn, global.req_count,
2626#ifdef USE_OPENSSL
2627 global.maxsslconn, sslconns, totalsslconns,
2628#endif
2629 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002630 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002631 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002632#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002633 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2634 ssl_key_rate, global.ssl_fe_keys_max,
2635 ssl_reuse,
2636 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002637 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002638#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002639 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2640 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002641#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002642 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002643#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002644 nb_tasks_cur, run_queue_cur, idle_pct,
2645 global.node, global.desc ? global.desc : ""
2646 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002647
Willy Tarreaubc18da12015-03-13 14:00:47 +01002648 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002649 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002650 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002651 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002652
2653 return 1;
2654}
2655
Willy Tarreau12833bb2014-01-28 16:49:56 +01002656/* This function dumps memory usage information onto the stream interface's
2657 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2658 * completion. No state is used.
2659 */
2660static int stats_dump_pools_to_buffer(struct stream_interface *si)
2661{
2662 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01002663 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002664 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01002665 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002666 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01002667 return 1;
2668}
2669
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002670/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2671 * the state from stream interface <si>. The caller is responsible for clearing
2672 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002673 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002674static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002675{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002676 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002677 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002678
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002679 if (!(px->cap & PR_CAP_FE))
2680 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002681
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002682 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002683 return 0;
2684
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002685 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002686 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002687 /* name, queue */
2688 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002689
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002690 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002691 /* Column sub-heading for Enable or Disable server */
2692 chunk_appendf(&trash, "<td></td>");
2693 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002694
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002695 chunk_appendf(&trash,
2696 "<td class=ac>"
2697 "<a name=\"%s/Frontend\"></a>"
2698 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2699 "<td colspan=3></td>"
2700 "",
2701 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002702
Willy Tarreau466c9b52012-12-23 02:25:03 +01002703 chunk_appendf(&trash,
2704 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002705 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002706 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2707 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2708 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002709 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2710 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2711 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002712
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002713 if (px->mode == PR_MODE_HTTP)
2714 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002715 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002716 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002717
2718 chunk_appendf(&trash,
2719 "</table></div></u></td>"
2720 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002721 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002722 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2723 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2724 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002725 U2H(px->fe_counters.sps_max),
2726 U2H(px->fe_counters.cps_max),
2727 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002728
2729 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002730 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002731 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002732 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002733
2734 chunk_appendf(&trash,
2735 "</table></div></u></td>"
2736 /* sessions rate : limit */
2737 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002738 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002739
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002740 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002741 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002742 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002743 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002744 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2745 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002746 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002747 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2748 U2H(px->fe_counters.cum_sess),
2749 U2H(px->fe_counters.cum_conn),
2750 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002751
Willy Tarreau466c9b52012-12-23 02:25:03 +01002752 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002753 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002754 chunk_appendf(&trash,
2755 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2756 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2757 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2758 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2759 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2760 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2761 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2762 "<tr><th>- other responses:</th><td>%s</td></tr>"
2763 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2764 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002765 U2H(px->fe_counters.p.http.cum_req),
2766 U2H(px->fe_counters.p.http.rsp[1]),
2767 U2H(px->fe_counters.p.http.rsp[2]),
2768 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002769 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002770 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002771 U2H(px->fe_counters.p.http.rsp[3]),
2772 U2H(px->fe_counters.p.http.rsp[4]),
2773 U2H(px->fe_counters.p.http.rsp[5]),
2774 U2H(px->fe_counters.p.http.rsp[0]),
2775 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002776 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002777
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002778 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002779 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002780 /* sessions: lbtot, lastsess */
2781 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002782 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002783 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002784 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002785 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002786
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002787 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002788 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002789 "<td>%s%s<div class=tips>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002790 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002791 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002792 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2793 px->fe_counters.comp_in ?
2794 (int)((px->fe_counters.comp_in - px->fe_counters.comp_out)*100/px->fe_counters.comp_in) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002795 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002796
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002797 chunk_appendf(&trash,
2798 /* denied: req, resp */
2799 "<td>%s</td><td>%s</td>"
2800 /* errors : request, connect, response */
2801 "<td>%s</td><td></td><td></td>"
2802 /* warnings: retries, redispatches */
2803 "<td></td><td></td>"
2804 /* server status : reflect frontend status */
2805 "<td class=ac>%s</td>"
2806 /* rest of server: nothing */
2807 "<td class=ac colspan=8></td></tr>"
2808 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002809 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2810 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002811 px->state == PR_STREADY ? "OPEN" :
2812 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002813 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002814 else { /* CSV mode */
2815 chunk_appendf(&trash,
2816 /* pxid, name, queue cur, queue max, */
2817 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002818 /* sessions : current, max, limit, total */
2819 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002820 /* bytes : in, out */
2821 "%lld,%lld,"
2822 /* denied: req, resp */
2823 "%lld,%lld,"
2824 /* errors : request, connect, response */
2825 "%lld,,,"
2826 /* warnings: retries, redispatches */
2827 ",,"
2828 /* server status : reflect frontend status */
2829 "%s,"
2830 /* rest of server: nothing */
2831 ",,,,,,,,"
2832 /* pid, iid, sid, throttle, lbtot, tracked, type */
2833 "%d,%d,0,,,,%d,"
2834 /* rate, rate_lim, rate_max */
2835 "%u,%u,%u,"
2836 /* check_status, check_code, check_duration */
2837 ",,,",
2838 px->id,
2839 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2840 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2841 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2842 px->fe_counters.failed_req,
2843 px->state == PR_STREADY ? "OPEN" :
2844 px->state == PR_STFULL ? "FULL" : "STOP",
2845 relative_pid, px->uuid, STATS_TYPE_FE,
2846 read_freq_ctr(&px->fe_sess_per_sec),
2847 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002848
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002849 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2850 if (px->mode == PR_MODE_HTTP) {
2851 for (i=1; i<6; i++)
2852 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2853 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2854 }
2855 else
2856 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002857
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002858 /* failed health analyses */
2859 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002860
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002861 /* requests : req_rate, req_rate_max, req_tot, */
2862 chunk_appendf(&trash, "%u,%u,%lld,",
2863 read_freq_ctr(&px->fe_req_per_sec),
2864 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2865
2866 /* errors: cli_aborts, srv_aborts */
2867 chunk_appendf(&trash, ",,");
2868
2869 /* compression: in, out, bypassed */
2870 chunk_appendf(&trash, "%lld,%lld,%lld,",
2871 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2872
2873 /* compression: comp_rsp */
2874 chunk_appendf(&trash, "%lld,",
2875 px->fe_counters.p.http.comp_rsp);
2876
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002877 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2878 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002879
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002880 /* finish with EOL */
2881 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002882 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002883 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002884}
2885
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002886/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2887 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2888 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2889 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002890 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002891static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002892{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002893 struct appctx *appctx = __objt_appctx(si->end);
2894
2895 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002896 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002897 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002898 /* Column sub-heading for Enable or Disable server */
2899 chunk_appendf(&trash, "<td></td>");
2900 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002901 chunk_appendf(&trash,
2902 /* frontend name, listener name */
2903 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2904 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2905 "",
2906 px->id, l->name,
2907 (flags & ST_SHLGNDS)?"<u>":"",
2908 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002909
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002910 if (flags & ST_SHLGNDS) {
2911 char str[INET6_ADDRSTRLEN];
2912 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002913
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002914 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002915
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002916 port = get_host_port(&l->addr);
2917 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2918 case AF_INET:
2919 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2920 break;
2921 case AF_INET6:
2922 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2923 break;
2924 case AF_UNIX:
2925 chunk_appendf(&trash, "unix, ");
2926 break;
2927 case -1:
2928 chunk_appendf(&trash, "(%s), ", strerror(errno));
2929 break;
2930 }
Willy Tarreau91861262007-10-17 17:06:05 +02002931
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002932 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002933 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002934 }
Willy Tarreau91861262007-10-17 17:06:05 +02002935
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002936 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002937 /* queue */
2938 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002939 /* sessions rate: current, max, limit */
2940 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002941 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002942 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002943 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002944 /* bytes: in, out */
2945 "<td>%s</td><td>%s</td>"
2946 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002947 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002948 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2949 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002950
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002951 chunk_appendf(&trash,
2952 /* denied: req, resp */
2953 "<td>%s</td><td>%s</td>"
2954 /* errors: request, connect, response */
2955 "<td>%s</td><td></td><td></td>"
2956 /* warnings: retries, redispatches */
2957 "<td></td><td></td>"
2958 /* server status: reflect listener status */
2959 "<td class=ac>%s</td>"
2960 /* rest of server: nothing */
2961 "<td class=ac colspan=8></td></tr>"
2962 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002963 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2964 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002965 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2966 }
2967 else { /* CSV mode */
2968 chunk_appendf(&trash,
2969 /* pxid, name, queue cur, queue max, */
2970 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002971 /* sessions: current, max, limit, total */
2972 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002973 /* bytes: in, out */
2974 "%lld,%lld,"
2975 /* denied: req, resp */
2976 "%lld,%lld,"
2977 /* errors: request, connect, response */
2978 "%lld,,,"
2979 /* warnings: retries, redispatches */
2980 ",,"
2981 /* server status: reflect listener status */
2982 "%s,"
2983 /* rest of server: nothing */
2984 ",,,,,,,,"
2985 /* pid, iid, sid, throttle, lbtot, tracked, type */
2986 "%d,%d,%d,,,,%d,"
2987 /* rate, rate_lim, rate_max */
2988 ",,,"
2989 /* check_status, check_code, check_duration */
2990 ",,,"
2991 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2992 ",,,,,,"
2993 /* failed health analyses */
2994 ","
2995 /* requests : req_rate, req_rate_max, req_tot, */
2996 ",,,"
2997 /* errors: cli_aborts, srv_aborts */
2998 ",,"
2999 /* compression: in, out, bypassed, comp_rsp */
3000 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003001 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3002 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003003 "\n",
3004 px->id, l->name,
3005 l->nbconn, l->counters->conn_max,
3006 l->maxconn, l->counters->cum_conn,
3007 l->counters->bytes_in, l->counters->bytes_out,
3008 l->counters->denied_req, l->counters->denied_resp,
3009 l->counters->failed_req,
3010 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
3011 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
3012 }
3013 return 1;
3014}
Willy Tarreau91861262007-10-17 17:06:05 +02003015
Simon Horman4d2eab62015-04-23 14:51:26 +09003016enum srv_stats_state {
3017 SRV_STATS_STATE_DOWN = 0,
3018 SRV_STATS_STATE_DOWN_AGENT,
3019 SRV_STATS_STATE_GOING_UP,
3020 SRV_STATS_STATE_UP_GOING_DOWN,
3021 SRV_STATS_STATE_UP,
3022 SRV_STATS_STATE_NOLB_GOING_DOWN,
3023 SRV_STATS_STATE_NOLB,
3024 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3025 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003026 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003027 SRV_STATS_STATE_NO_CHECK,
3028
3029 SRV_STATS_STATE_COUNT, /* Must be last */
3030};
3031
Simon Horman837bfa72015-04-23 14:51:27 +09003032enum srv_stats_colour {
3033 SRV_STATS_COLOUR_DOWN = 0,
3034 SRV_STATS_COLOUR_GOING_UP,
3035 SRV_STATS_COLOUR_GOING_DOWN,
3036 SRV_STATS_COLOUR_UP,
3037 SRV_STATS_COLOUR_NOLB,
3038 SRV_STATS_COLOUR_DRAINING,
3039 SRV_STATS_COLOUR_NO_CHECK,
3040
3041 SRV_STATS_COLOUR_COUNT, /* Must be last */
3042};
3043
3044static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3045 [SRV_STATS_COLOUR_DOWN] = "down",
3046 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3047 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3048 [SRV_STATS_COLOUR_UP] = "up",
3049 [SRV_STATS_COLOUR_NOLB] = "nolb",
3050 [SRV_STATS_COLOUR_DRAINING] = "draining",
3051 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3052};
3053
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003054/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3055 * from stream interface <si>, stats flags <flags>, and server state <state>.
3056 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003057 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003058 */
Simon Horman4d2eab62015-04-23 14:51:26 +09003059static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09003060 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003061{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003062 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003063 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003064 char str[INET6_ADDRSTRLEN];
3065 struct chunk src;
3066 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02003067
Willy Tarreau32091232014-05-16 13:52:00 +02003068 /* we have "via" which is the tracked server as described in the configuration,
3069 * and "ref" which is the checked server and the end of the chain.
3070 */
3071 via = sv->track ? sv->track : sv;
3072 ref = via;
3073 while (ref->track)
3074 ref = ref->track;
3075
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003076 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003077 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3078 [SRV_STATS_STATE_DOWN] = "DOWN",
3079 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3080 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
3081 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
3082 [SRV_STATS_STATE_UP] = "UP",
3083 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
3084 [SRV_STATS_STATE_NOLB] = "NOLB",
3085 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
3086 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003087 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003088 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003089 };
Willy Tarreau91861262007-10-17 17:06:05 +02003090
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003091 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003092 chunk_appendf(&trash, "<tr class=\"maintain\">");
3093 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003094 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003095 "<tr class=\"%s_%s\">",
3096 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003097
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003098 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003099 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003100 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3101 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003102
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003103 chunk_appendf(&trash,
3104 "<td class=ac><a name=\"%s/%s\"></a>%s"
3105 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3106 "",
3107 px->id, sv->id,
3108 (flags & ST_SHLGNDS) ? "<u>" : "",
3109 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003110
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003111 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003112 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003113
3114 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3115 case AF_INET:
3116 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3117 break;
3118 case AF_INET6:
3119 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3120 break;
3121 case AF_UNIX:
3122 chunk_appendf(&trash, "unix, ");
3123 break;
3124 case -1:
3125 chunk_appendf(&trash, "(%s), ", strerror(errno));
3126 break;
3127 default: /* address family not supported */
3128 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003129 }
Willy Tarreau91861262007-10-17 17:06:05 +02003130
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003131 /* id */
3132 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003133
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003134 /* cookie */
3135 if (sv->cookie) {
3136 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003137
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003138 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3139 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003140
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003141 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003142 }
3143
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003144 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003145 }
Willy Tarreau91861262007-10-17 17:06:05 +02003146
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003147 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003148 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003149 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003150 /* sessions rate : current, max, limit */
3151 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003152 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003153 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003154 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3155 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003156
Willy Tarreau466c9b52012-12-23 02:25:03 +01003157
3158 chunk_appendf(&trash,
3159 /* sessions: current, max, limit, total */
3160 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003161 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003162 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3163 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003164 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3165 U2H(sv->counters.cum_sess),
3166 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003167
Willy Tarreau466c9b52012-12-23 02:25:03 +01003168 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3169 if (px->mode == PR_MODE_HTTP) {
3170 unsigned long long tot;
3171 for (tot = i = 0; i < 6; i++)
3172 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003173
Willy Tarreau466c9b52012-12-23 02:25:03 +01003174 chunk_appendf(&trash,
3175 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3176 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3177 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3178 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3179 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3180 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3181 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3182 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003183 U2H(tot),
3184 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3185 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3186 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3187 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3188 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3189 U2H(sv->counters.p.http.rsp[0]), tot ? (int)(100*sv->counters.p.http.rsp[0] / tot) : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02003190 }
Willy Tarreau91861262007-10-17 17:06:05 +02003191
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003192 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3193 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES)));
3194 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES)));
3195 if (px->mode == PR_MODE_HTTP)
3196 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES)));
3197 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES)));
3198
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003199 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003200 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003201 /* sessions: lbtot, last */
3202 "<td>%s</td><td>%s</td>",
3203 U2H(sv->counters.cum_lbconn),
3204 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003205
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003206 chunk_appendf(&trash,
3207 /* bytes : in, out */
3208 "<td>%s</td><td>%s</td>"
3209 /* denied: req, resp */
3210 "<td></td><td>%s</td>"
3211 /* errors : request, connect */
3212 "<td></td><td>%s</td>"
3213 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003214 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003215 /* warnings: retries, redispatches */
3216 "<td>%lld</td><td>%lld</td>"
3217 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003218 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3219 U2H(sv->counters.failed_secu),
3220 U2H(sv->counters.failed_conns),
3221 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003222 sv->counters.cli_aborts,
3223 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003224 sv->counters.retries, sv->counters.redispatches);
3225
3226 /* status, lest check */
3227 chunk_appendf(&trash, "<td class=ac>");
3228
Willy Tarreau20125212014-05-13 19:44:56 +02003229 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003230 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3231 chunk_appendf(&trash, "MAINT");
3232 }
Simon Horman0766e442014-11-12 15:55:54 +09003233 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003234 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003235 /* DOWN (agent) */
3236 chunk_appendf(&trash, srv_hlt_st[1], "GCC: your -Werror=format-security is bogus, annoying, and hides real bugs, I don't thank you, really!");
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003237 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003238 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003239 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3240 chunk_appendf(&trash,
3241 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003242 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3243 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003244 }
Willy Tarreau91861262007-10-17 17:06:05 +02003245
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003246 if ((sv->state == SRV_ST_STOPPED) &&
3247 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3248 chunk_appendf(&trash,
3249 "</td><td class=ac><u> %s%s",
3250 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3251 get_check_status_info(sv->agent.status));
3252
3253 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3254 chunk_appendf(&trash, "/%d", sv->agent.code);
3255
3256 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3257 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3258
3259 chunk_appendf(&trash, "<div class=tips>%s",
3260 get_check_status_description(sv->agent.status));
3261 if (*sv->agent.desc) {
3262 chunk_appendf(&trash, ": ");
3263 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3264 chunk_htmlencode(&trash, &src);
3265 }
3266 chunk_appendf(&trash, "</div></u>");
3267 }
3268 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003269 chunk_appendf(&trash,
3270 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003271 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003272 get_check_status_info(sv->check.status));
3273
3274 if (sv->check.status >= HCHK_STATUS_L57DATA)
3275 chunk_appendf(&trash, "/%d", sv->check.code);
3276
3277 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003278 chunk_appendf(&trash, " in %lums", sv->check.duration);
3279
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003280 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003281 get_check_status_description(sv->check.status));
3282 if (*sv->check.desc) {
3283 chunk_appendf(&trash, ": ");
3284 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3285 chunk_htmlencode(&trash, &src);
3286 }
3287 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003288 }
3289 else
3290 chunk_appendf(&trash, "</td><td>");
3291
3292 chunk_appendf(&trash,
3293 /* weight */
3294 "</td><td class=ac>%d</td>"
3295 /* act, bck */
3296 "<td class=ac>%s</td><td class=ac>%s</td>"
3297 "",
3298 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003299 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3300 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003301
3302 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003303 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003304 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003305
3306 if (ref->observe)
3307 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3308
3309 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003310 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003311 "<td>%lld</td><td>%s</td>"
3312 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003313 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003314 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3315 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003316 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3317 /* tracking a server */
3318 chunk_appendf(&trash,
3319 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003320 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003321 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003322 else
3323 chunk_appendf(&trash, "<td colspan=3></td>");
3324
3325 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003326 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003327 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003328 else
3329 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3330 }
3331 else { /* CSV mode */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003332 struct chunk *out = get_trash_chunk();
Simon Horman4d2eab62015-04-23 14:51:26 +09003333 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3334 [SRV_STATS_STATE_DOWN] = "DOWN,",
3335 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3336 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3337 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3338 [SRV_STATS_STATE_UP] = "UP,",
3339 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3340 [SRV_STATS_STATE_NOLB] = "NOLB,",
3341 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3342 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003343 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003344 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003345 };
3346
3347 chunk_appendf(&trash,
3348 /* pxid, name */
3349 "%s,%s,"
3350 /* queue : current, max */
3351 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003352 /* sessions : current, max, limit, total */
3353 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003354 /* bytes : in, out */
3355 "%lld,%lld,"
3356 /* denied: req, resp */
3357 ",%lld,"
3358 /* errors : request, connect, response */
3359 ",%lld,%lld,"
3360 /* warnings: retries, redispatches */
3361 "%lld,%lld,"
3362 "",
3363 px->id, sv->id,
3364 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003365 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003366 sv->counters.bytes_in, sv->counters.bytes_out,
3367 sv->counters.failed_secu,
3368 sv->counters.failed_conns, sv->counters.failed_resp,
3369 sv->counters.retries, sv->counters.redispatches);
3370
3371 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003372 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003373 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003374 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003375 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003376 else
3377 chunk_appendf(&trash,
3378 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003379 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3380 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003381
3382 chunk_appendf(&trash,
3383 /* weight, active, backup */
3384 "%d,%d,%d,"
3385 "",
3386 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003387 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3388 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003389
3390 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003391 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003392 chunk_appendf(&trash,
3393 "%lld,%lld,%d,%d,",
3394 sv->counters.failed_checks, sv->counters.down_trans,
3395 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3396 else
3397 chunk_appendf(&trash, ",,,,");
3398
3399 /* queue limit, pid, iid, sid, */
3400 chunk_appendf(&trash,
3401 "%s,"
3402 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003403 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003404 relative_pid, px->uuid, sv->puid);
3405
3406 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003407 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003408 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003409
3410 /* sessions: lbtot */
3411 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3412
3413 /* tracked */
3414 if (sv->track)
3415 chunk_appendf(&trash, "%s/%s,",
3416 sv->track->proxy->id, sv->track->id);
3417 else
3418 chunk_appendf(&trash, ",");
3419
3420 /* type */
3421 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3422
3423 /* rate */
3424 chunk_appendf(&trash, "%u,,%u,",
3425 read_freq_ctr(&sv->sess_per_sec),
3426 sv->counters.sps_max);
3427
Willy Tarreauff5ae352013-12-11 20:36:34 +01003428 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003429 /* check_status */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003430 chunk_appendf(&trash, "%s,", csv_enc(get_check_status_info(sv->check.status), 1, out));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003431
3432 /* check_code */
3433 if (sv->check.status >= HCHK_STATUS_L57DATA)
3434 chunk_appendf(&trash, "%u,", sv->check.code);
3435 else
3436 chunk_appendf(&trash, ",");
3437
3438 /* check_duration */
3439 if (sv->check.status >= HCHK_STATUS_CHECKED)
3440 chunk_appendf(&trash, "%lu,", sv->check.duration);
3441 else
3442 chunk_appendf(&trash, ",");
3443
3444 }
3445 else
3446 chunk_appendf(&trash, ",,,");
3447
3448 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3449 if (px->mode == PR_MODE_HTTP) {
3450 for (i=1; i<6; i++)
3451 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3452
3453 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3454 }
3455 else
3456 chunk_appendf(&trash, ",,,,,,");
3457
3458 /* failed health analyses */
3459 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3460
3461 /* requests : req_rate, req_rate_max, req_tot, */
3462 chunk_appendf(&trash, ",,,");
3463
3464 /* errors: cli_aborts, srv_aborts */
3465 chunk_appendf(&trash, "%lld,%lld,",
3466 sv->counters.cli_aborts, sv->counters.srv_aborts);
3467
3468 /* compression: in, out, bypassed, comp_rsp */
3469 chunk_appendf(&trash, ",,,,");
3470
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003471 /* lastsess */
3472 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3473
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003474 /* capture of last check and agent statuses */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003475 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->check.desc), 1, out) : "");
3476 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->agent.desc), 1, out) : "");
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003477
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003478 /* qtime, ctime, rtime, ttime, */
3479 chunk_appendf(&trash, "%u,%u,%u,%u,",
3480 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3481 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3482 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3483 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3484
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003485 /* finish with EOL */
3486 chunk_appendf(&trash, "\n");
3487 }
3488 return 1;
3489}
3490
3491/* Dumps a line for backend <px> to the trash for and uses the state from stream
3492 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3493 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3494 */
3495static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3496{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003497 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003498 struct chunk src;
3499 int i;
3500
3501 if (!(px->cap & PR_CAP_BE))
3502 return 0;
3503
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003504 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003505 return 0;
3506
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003507 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003508 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003509 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003510 /* Column sub-heading for Enable or Disable server */
3511 chunk_appendf(&trash, "<td></td>");
3512 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003513 chunk_appendf(&trash,
3514 "<td class=ac>"
3515 /* name */
3516 "%s<a name=\"%s/Backend\"></a>"
3517 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3518 "",
3519 (flags & ST_SHLGNDS)?"<u>":"",
3520 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003521
3522 if (flags & ST_SHLGNDS) {
3523 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003524 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003525 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3526
3527 /* cookie */
3528 if (px->cookie_name) {
3529 chunk_appendf(&trash, ", cookie: '");
3530 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3531 chunk_htmlencode(&trash, &src);
3532 chunk_appendf(&trash, "'");
3533 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003534 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003535 }
3536
3537 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003538 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003539 /* queue : current, max */
3540 "<td>%s</td><td>%s</td><td></td>"
3541 /* sessions rate : current, max, limit */
3542 "<td>%s</td><td>%s</td><td></td>"
3543 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003544 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003545 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3546 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003547
3548 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003549 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003550 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003551 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003552 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003553 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003554 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3555 U2H(px->be_counters.cum_conn),
3556 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003557
Willy Tarreau466c9b52012-12-23 02:25:03 +01003558 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003559 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003560 chunk_appendf(&trash,
3561 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3562 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3563 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3564 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3565 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3566 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3567 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3568 "<tr><th>- other responses:</th><td>%s</td></tr>"
3569 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003570 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003571 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003572 U2H(px->be_counters.p.http.cum_req),
3573 U2H(px->be_counters.p.http.rsp[1]),
3574 U2H(px->be_counters.p.http.rsp[2]),
3575 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003576 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003577 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003578 U2H(px->be_counters.p.http.rsp[3]),
3579 U2H(px->be_counters.p.http.rsp[4]),
3580 U2H(px->be_counters.p.http.rsp[5]),
3581 U2H(px->be_counters.p.http.rsp[0]),
3582 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003583 }
3584
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003585 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES)));
3586 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES)));
3587 if (px->mode == PR_MODE_HTTP)
3588 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES)));
3589 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES)));
3590
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003591 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003592 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003593 /* sessions: lbtot, last */
3594 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003595 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003596 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003597 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003598 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003599 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003600 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003601
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003602 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003603 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003604 "<td>%s%s<div class=tips>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003605 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003606 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003607 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3608 px->be_counters.comp_in ?
3609 (int)((px->be_counters.comp_in - px->be_counters.comp_out)*100/px->be_counters.comp_in) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003610 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3611
3612 chunk_appendf(&trash,
3613 /* denied: req, resp */
3614 "<td>%s</td><td>%s</td>"
3615 /* errors : request, connect */
3616 "<td></td><td>%s</td>"
3617 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003618 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003619 /* warnings: retries, redispatches */
3620 "<td>%lld</td><td>%lld</td>"
3621 /* backend status: reflect backend status (up/down): we display UP
3622 * if the backend has known working servers or if it has no server at
3623 * all (eg: for stats). Then we display the total weight, number of
3624 * active and backups. */
3625 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3626 "<td class=ac>%d</td><td class=ac>%d</td>"
3627 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003628 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3629 U2H(px->be_counters.failed_conns),
3630 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003631 px->be_counters.cli_aborts,
3632 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003633 px->be_counters.retries, px->be_counters.redispatches,
3634 human_time(now.tv_sec - px->last_change, 1),
3635 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3636 "<font color=\"red\"><b>DOWN</b></font>",
3637 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3638 px->srv_act, px->srv_bck);
3639
3640 chunk_appendf(&trash,
3641 /* rest of backend: nothing, down transitions, total downtime, throttle */
3642 "<td class=ac>&nbsp;</td><td>%d</td>"
3643 "<td>%s</td>"
3644 "<td></td>"
3645 "</tr>",
3646 px->down_trans,
3647 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3648 }
3649 else { /* CSV mode */
3650 chunk_appendf(&trash,
3651 /* pxid, name */
3652 "%s,BACKEND,"
3653 /* queue : current, max */
3654 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003655 /* sessions : current, max, limit, total */
3656 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003657 /* bytes : in, out */
3658 "%lld,%lld,"
3659 /* denied: req, resp */
3660 "%lld,%lld,"
3661 /* errors : request, connect, response */
3662 ",%lld,%lld,"
3663 /* warnings: retries, redispatches */
3664 "%lld,%lld,"
3665 /* backend status: reflect backend status (up/down): we display UP
3666 * if the backend has known working servers or if it has no server at
3667 * all (eg: for stats). Then we display the total weight, number of
3668 * active and backups. */
3669 "%s,"
3670 "%d,%d,%d,"
3671 /* rest of backend: nothing, down transitions, last change, total downtime */
3672 ",%d,%d,%d,,"
3673 /* pid, iid, sid, throttle, lbtot, tracked, type */
3674 "%d,%d,0,,%lld,,%d,"
3675 /* rate, rate_lim, rate_max, */
3676 "%u,,%u,"
3677 /* check_status, check_code, check_duration */
3678 ",,,",
3679 px->id,
3680 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3681 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3682 px->be_counters.bytes_in, px->be_counters.bytes_out,
3683 px->be_counters.denied_req, px->be_counters.denied_resp,
3684 px->be_counters.failed_conns, px->be_counters.failed_resp,
3685 px->be_counters.retries, px->be_counters.redispatches,
3686 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3687 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3688 px->srv_act, px->srv_bck,
3689 px->down_trans, (int)(now.tv_sec - px->last_change),
3690 px->srv?be_downtime(px):0,
3691 relative_pid, px->uuid,
3692 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3693 read_freq_ctr(&px->be_sess_per_sec),
3694 px->be_counters.sps_max);
3695
3696 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3697 if (px->mode == PR_MODE_HTTP) {
3698 for (i=1; i<6; i++)
3699 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3700 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3701 }
3702 else
3703 chunk_appendf(&trash, ",,,,,,");
3704
3705 /* failed health analyses */
3706 chunk_appendf(&trash, ",");
3707
3708 /* requests : req_rate, req_rate_max, req_tot, */
3709 chunk_appendf(&trash, ",,,");
3710
3711 /* errors: cli_aborts, srv_aborts */
3712 chunk_appendf(&trash, "%lld,%lld,",
3713 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3714
3715 /* compression: in, out, bypassed */
3716 chunk_appendf(&trash, "%lld,%lld,%lld,",
3717 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3718
3719 /* compression: comp_rsp */
3720 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3721
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003722 /* lastsess, last_chk, last_agt, */
3723 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003724
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003725 /* qtime, ctime, rtime, ttime, */
3726 chunk_appendf(&trash, "%u,%u,%u,%u,",
3727 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3728 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3729 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3730 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3731
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003732 /* finish with EOL */
3733 chunk_appendf(&trash, "\n");
3734 }
3735 return 1;
3736}
3737
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003738/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003739 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003740 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003741 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003742static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003743{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003744 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003745 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3746
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003747 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003748 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003749
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003750 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003751 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003752 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003753 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003754 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003755 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003756 }
3757
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003758 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05003759 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003760 }
3761
3762 /* print a new table */
3763 chunk_appendf(&trash,
3764 "<table class=\"tbl\" width=\"100%%\">\n"
3765 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003766 "<th class=\"pxname\" width=\"10%%\">");
3767
3768 chunk_appendf(&trash,
3769 "<a name=\"%s\"></a>%s"
3770 "<a class=px href=\"#%s\">%s</a>",
3771 px->id,
3772 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3773 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003774
3775 if (uri->flags & ST_SHLGNDS) {
3776 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003777 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003778 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3779 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003780 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003781 }
3782
3783 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003784 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003785 "<th class=\"%s\" width=\"90%%\">%s</th>"
3786 "</tr>\n"
3787 "</table>\n"
3788 "<table class=\"tbl\" width=\"100%%\">\n"
3789 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003790 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3791 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3792
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003793 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003794 /* Column heading for Enable or Disable server */
3795 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003796 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003797
3798 chunk_appendf(&trash,
3799 "<th rowspan=2></th>"
3800 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003801 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003802 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3803 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3804 "<th colspan=9>Server</th>"
3805 "</tr>\n"
3806 "<tr class=\"titre\">"
3807 "<th>Cur</th><th>Max</th><th>Limit</th>"
3808 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003809 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003810 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3811 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3812 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3813 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3814 "<th>Thrtle</th>\n"
3815 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003816}
3817
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003818/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003819 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003820 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003821static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003822{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003823 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003824 chunk_appendf(&trash, "</table>");
3825
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003826 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003827 /* close the form used to enable/disable this proxy servers */
3828 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003829 "Choose the action to perform on the checked servers : "
3830 "<select name=action>"
3831 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003832 "<option value=\"ready\">Set state to READY</option>"
3833 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02003834 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003835 "<option value=\"dhlth\">Health: disable checks</option>"
3836 "<option value=\"ehlth\">Health: enable checks</option>"
3837 "<option value=\"hrunn\">Health: force UP</option>"
3838 "<option value=\"hnolb\">Health: force NOLB</option>"
3839 "<option value=\"hdown\">Health: force DOWN</option>"
3840 "<option value=\"dagent\">Agent: disable checks</option>"
3841 "<option value=\"eagent\">Agent: enable checks</option>"
3842 "<option value=\"arunn\">Agent: force UP</option>"
3843 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003844 "<option value=\"shutdown\">Kill Sessions</option>"
3845 "</select>"
3846 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3847 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3848 "</form>",
3849 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003850 }
3851
3852 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003853}
Willy Tarreau91861262007-10-17 17:06:05 +02003854
3855/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003856 * Dumps statistics for a proxy. The output is sent to the stream interface's
3857 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3858 * buffer space, or non-zero if everything completed. This function is used
3859 * both by the CLI and the HTTP entry points, and is able to dump the output
3860 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003861 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003862static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003863{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003864 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02003865 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003866 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02003867 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003868 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003869
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003870 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003871
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003872 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003873 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003874 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003875 if (uri && uri->scope) {
3876 /* we have a limited scope, we have to check the proxy name */
3877 struct stat_scope *scope;
3878 int len;
3879
3880 len = strlen(px->id);
3881 scope = uri->scope;
3882
3883 while (scope) {
3884 /* match exact proxy name */
3885 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3886 break;
3887
3888 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003889 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003890 break;
3891 scope = scope->next;
3892 }
3893
3894 /* proxy name not found : don't dump anything */
3895 if (scope == NULL)
3896 return 1;
3897 }
3898
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003899 /* if the user has requested a limited output and the proxy
3900 * name does not match, skip it.
3901 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003902 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003903 strnistr(px->id, strlen(px->id), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003904 return 1;
3905
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003906 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3907 (appctx->ctx.stats.iid != -1) &&
3908 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003909 return 1;
3910
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003911 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003912 /* fall through */
3913
Willy Tarreau295a8372011-03-10 11:25:07 +01003914 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003915 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003916 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003917 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003918 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02003919 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003920 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003921 }
Willy Tarreau91861262007-10-17 17:06:05 +02003922
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003923 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003924 /* fall through */
3925
Willy Tarreau295a8372011-03-10 11:25:07 +01003926 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003927 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003928 if (stats_dump_fe_stats(si, px)) {
3929 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003930 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003931 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003932 }
3933 }
Willy Tarreau91861262007-10-17 17:06:05 +02003934
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003935 appctx->ctx.stats.l = px->conf.listeners.n;
3936 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003937 /* fall through */
3938
Willy Tarreau295a8372011-03-10 11:25:07 +01003939 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003940 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003941 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003942 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003943 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02003944 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003945 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003946
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003947 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003948 if (!l->counters)
3949 continue;
3950
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003951 if (appctx->ctx.stats.flags & STAT_BOUND) {
3952 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003953 break;
3954
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003955 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003956 continue;
3957 }
3958
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003959 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003960 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
3961 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003962 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003963 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003964 }
3965 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003966 }
3967
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003968 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3969 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003970 /* fall through */
3971
Willy Tarreau295a8372011-03-10 11:25:07 +01003972 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003973 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003974 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003975 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09003976 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02003977
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003978 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003979 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02003980 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003981 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003982
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003983 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003984
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003985 if (appctx->ctx.stats.flags & STAT_BOUND) {
3986 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003987 break;
3988
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003989 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003990 continue;
3991 }
3992
Willy Tarreau32091232014-05-16 13:52:00 +02003993 svs = sv;
3994 while (svs->track)
3995 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003996
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003997 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003998 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09003999 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004000 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004001 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4002 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004003 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004004 sv_colour = SRV_STATS_COLOUR_UP;
4005 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004006
Simon Horman4463d192015-04-23 14:51:28 +09004007 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
4008 sv_colour = SRV_STATS_COLOUR_DRAINING;
4009
4010 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09004011 if (svs->agent.state & CHK_ST_ENABLED)
4012 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
4013 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09004014 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09004015 else
Simon Horman4d2eab62015-04-23 14:51:26 +09004016 sv_state = SRV_STATS_STATE_DRAIN;
4017 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004018
Simon Horman837bfa72015-04-23 14:51:27 +09004019 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004020 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09004021 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
4022 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004023 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004024 else if (sv->state == SRV_ST_STOPPING) {
4025 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09004026 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004027 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09004028 sv_colour = SRV_STATS_COLOUR_NOLB;
4029 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004030 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004031 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4032 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004033 }
4034 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09004035 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004036 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09004037 sv_colour = SRV_STATS_COLOUR_DOWN;
4038 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004039 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09004040 sv_colour = SRV_STATS_COLOUR_DOWN;
4041 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004042 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004043 sv_colour = SRV_STATS_COLOUR_GOING_UP;
4044 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004045 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09004046 sv_colour = SRV_STATS_COLOUR_DOWN;
4047 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004048 }
Willy Tarreau91861262007-10-17 17:06:05 +02004049
Willy Tarreau9638efa2014-05-23 11:19:57 +02004050 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02004051 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004052 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02004053 continue;
4054 }
4055
Simon Horman837bfa72015-04-23 14:51:27 +09004056 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004057 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004058 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004059 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004060 }
4061 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004062 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004063
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004064 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004065 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004066
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004067 case STAT_PX_ST_BE:
4068 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004069 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
4070 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004071 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004072 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004073 }
4074 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004075
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004076 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004077 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004078
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004079 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004080 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004081 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004082 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004083 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004084 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004085 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004086 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004087
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004088 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004089 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004090
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004091 case STAT_PX_ST_FIN:
4092 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004093
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004094 default:
4095 /* unknown state, we should put an abort() here ! */
4096 return 1;
4097 }
4098}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004099
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004100/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4101 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004102 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004103static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004104{
4105 /* WARNING! This must fit in the first buffer !!! */
4106 chunk_appendf(&trash,
4107 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4108 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4109 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4110 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4111 "<style type=\"text/css\"><!--\n"
4112 "body {"
4113 " font-family: arial, helvetica, sans-serif;"
4114 " font-size: 12px;"
4115 " font-weight: normal;"
4116 " color: black;"
4117 " background: white;"
4118 "}\n"
4119 "th,td {"
4120 " font-size: 10px;"
4121 "}\n"
4122 "h1 {"
4123 " font-size: x-large;"
4124 " margin-bottom: 0.5em;"
4125 "}\n"
4126 "h2 {"
4127 " font-family: helvetica, arial;"
4128 " font-size: x-large;"
4129 " font-weight: bold;"
4130 " font-style: italic;"
4131 " color: #6020a0;"
4132 " margin-top: 0em;"
4133 " margin-bottom: 0em;"
4134 "}\n"
4135 "h3 {"
4136 " font-family: helvetica, arial;"
4137 " font-size: 16px;"
4138 " font-weight: bold;"
4139 " color: #b00040;"
4140 " background: #e8e8d0;"
4141 " margin-top: 0em;"
4142 " margin-bottom: 0em;"
4143 "}\n"
4144 "li {"
4145 " margin-top: 0.25em;"
4146 " margin-right: 2em;"
4147 "}\n"
4148 ".hr {margin-top: 0.25em;"
4149 " border-color: black;"
4150 " border-bottom-style: solid;"
4151 "}\n"
4152 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4153 ".total {background: #20D0D0;color: #ffff80;}\n"
4154 ".frontend {background: #e8e8d0;}\n"
4155 ".socket {background: #d0d0d0;}\n"
4156 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004157 ".active_down {background: #ff9090;}\n"
4158 ".active_going_up {background: #ffd020;}\n"
4159 ".active_going_down {background: #ffffa0;}\n"
4160 ".active_up {background: #c0ffc0;}\n"
4161 ".active_nolb {background: #20a0ff;}\n"
4162 ".active_draining {background: #20a0FF;}\n"
4163 ".active_no_check {background: #e0e0e0;}\n"
4164 ".backup_down {background: #ff9090;}\n"
4165 ".backup_going_up {background: #ff80ff;}\n"
4166 ".backup_going_down {background: #c060ff;}\n"
4167 ".backup_up {background: #b0d0ff;}\n"
4168 ".backup_nolb {background: #90b0e0;}\n"
4169 ".backup_draining {background: #cc9900;}\n"
4170 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004171 ".maintain {background: #c07820;}\n"
4172 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4173 "\n"
4174 "a.px:link {color: #ffff40; text-decoration: none;}"
4175 "a.px:visited {color: #ffff40; text-decoration: none;}"
4176 "a.px:hover {color: #ffffff; text-decoration: none;}"
4177 "a.lfsb:link {color: #000000; text-decoration: none;}"
4178 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4179 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4180 "\n"
4181 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4182 "table.tbl td { text-align: right; border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray; white-space: nowrap;}\n"
4183 "table.tbl td.ac { text-align: center;}\n"
4184 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4185 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4186 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4187 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4188 "\n"
4189 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4190 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4191 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004192 "table.det { border-collapse: collapse; border-style: none; }\n"
4193 "table.det th { text-align: left; border-width: 0px; padding: 0px 1px 0px 0px; font-style:normal;font-size:11px;font-weight:bold;font-family: sans-serif;}\n"
Willy Tarreau6b9d3a82013-12-16 09:00:35 +01004194 "table.det td { text-align: right; border-width: 0px; padding: 0px 0px 0px 4px; white-space: nowrap; font-style:normal;font-size:11px;font-weight:normal;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004195 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004196 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004197 " display:block;\n"
4198 " visibility:hidden;\n"
4199 " z-index:2147483647;\n"
4200 " position:absolute;\n"
4201 " padding:2px 4px 3px;\n"
4202 " background:#f0f060; color:#000000;\n"
4203 " border:1px solid #7040c0;\n"
4204 " white-space:nowrap;\n"
4205 " font-style:normal;font-size:11px;font-weight:normal;\n"
4206 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4207 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4208 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004209 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004210 "-->\n"
4211 "</style></head>\n",
4212 (uri->flags & ST_SHNODE) ? " on " : "",
4213 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4214 );
4215}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004216
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004217/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004218 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004219 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004220 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004221static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004222{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004223 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004224 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004225 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004226
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004227 /* WARNING! this has to fit the first packet too.
4228 * We are around 3.5 kB, add adding entries will
4229 * become tricky if we want to support 4kB buffers !
4230 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004231 chunk_appendf(&trash,
4232 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4233 PRODUCT_NAME "%s</a></h1>\n"
4234 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4235 "<hr width=\"100%%\" class=\"hr\">\n"
4236 "<h3>&gt; General process information</h3>\n"
4237 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4238 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4239 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4240 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4241 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4242 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4243 "Running tasks: %d/%d; idle = %d %%<br>\n"
4244 "</td><td align=\"center\" nowrap>\n"
4245 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004246 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4247 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004248 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004249 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4250 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004251 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004252 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4253 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004254 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004255 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4256 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004257 "</tr><tr>\n"
4258 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
Geoff Bucarcc8bb922013-04-18 13:53:16 -07004259 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004260 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004261 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004262 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004263 "</td>"
4264 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4265 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4266 "",
4267 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4268 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4269 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4270 (uri->flags & ST_SHDESC) ? ": " : "",
4271 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4272 pid, relative_pid, global.nbproc,
4273 up / 86400, (up % 86400) / 3600,
4274 (up % 3600) / 60, (up % 60),
4275 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4276 global.rlimit_memmax ? " MB" : "",
4277 global.rlimit_nofile,
4278 global.maxsock, global.maxconn, global.maxpipes,
4279 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4280 run_queue_cur, nb_tasks_cur, idle_pct
4281 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004282
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004283 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004284 memcpy(scope_txt, bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004285 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004286
4287 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004288 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004289 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004290 STAT_SCOPE_TXT_MAXLEN);
4291
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004292 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004293 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004294 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004295 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004296 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004297 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004298 }
4299
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004300 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004301 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004302 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004303 uri->uri_prefix,
4304 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004305 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004306 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004307 else
4308 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004309 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004310 uri->uri_prefix,
4311 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004312 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004313 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004314
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004315 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004316 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004317 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004318 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004319 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004320 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004321 "",
4322 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004323 else
4324 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004325 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004326 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004327 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004328 ";norefresh",
4329 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004330 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004331
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004332 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004333 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004334 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004335 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4336 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004337 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004338
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004339 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004340 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004341 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004342 (uri->refresh > 0) ? ";norefresh" : "",
4343 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004344
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004345 chunk_appendf(&trash,
4346 "</ul></td>"
4347 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4348 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4349 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4350 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4351 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4352 "</ul>"
4353 "</td>"
4354 "</tr></table>\n"
4355 ""
4356 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004357
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004358 if (appctx->ctx.stats.st_code) {
4359 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004360 case STAT_STATUS_DONE:
4361 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004362 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004363 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004364 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004365 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004366 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4367 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004368 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004369 break;
4370 case STAT_STATUS_NONE:
4371 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004372 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004373 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004374 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004375 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004376 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4377 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004378 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004379 break;
4380 case STAT_STATUS_PART:
4381 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004382 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004383 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004384 "Action partially processed.<br>"
4385 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004386 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004387 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4388 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004389 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004390 break;
4391 case STAT_STATUS_ERRP:
4392 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004393 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004394 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004395 "Action not processed because of invalid parameters."
4396 "<ul>"
4397 "<li>The action is maybe unknown.</li>"
4398 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4399 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4400 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004401 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004402 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4403 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004404 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004405 break;
4406 case STAT_STATUS_EXCD:
4407 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004408 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004409 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004410 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4411 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004412 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004413 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4414 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004415 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004416 break;
4417 case STAT_STATUS_DENY:
4418 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004419 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004420 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004421 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004422 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004423 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4424 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004425 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004426 break;
4427 default:
4428 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004429 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004430 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004431 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004432 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004433 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4434 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004435 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004436 }
4437 chunk_appendf(&trash, "<p>\n");
4438 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004439}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004440
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004441/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4442 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004443 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004444static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004445{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004446 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004447}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004448
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004449/* This function dumps statistics onto the stream interface's read buffer in
4450 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004451 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4452 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
Willy Tarreau87b09662015-04-03 00:22:06 +02004453 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004454 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004455 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004456static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004457{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004458 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004459 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004460 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004461
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004462 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004463
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004464 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004465 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004466 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004467 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004468
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004469 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004470 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004471 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004472 else
4473 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004474
Willy Tarreaubc18da12015-03-13 14:00:47 +01004475 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004476 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004477 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004478 }
Willy Tarreauae526782010-03-04 20:34:23 +01004479
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004480 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004481 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004482
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004483 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004484 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004485 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004486 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004487 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004488 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004489 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004490 }
Willy Tarreau91861262007-10-17 17:06:05 +02004491
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004492 appctx->ctx.stats.px = proxy;
4493 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4494 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004495 /* fall through */
4496
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004497 case STAT_ST_LIST:
4498 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004499 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004500 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004501 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004502 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004503 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004504
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004505 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004506 /* skip the disabled proxies, global frontend and non-networked ones */
4507 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004508 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004509 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004510
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004511 appctx->ctx.stats.px = px->next;
4512 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004513 }
4514 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004515
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004516 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004517 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004518
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004519 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004520 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004521 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01004522 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004523 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004524 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004525 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004526 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004527
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004528 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004529 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004530
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004531 case STAT_ST_FIN:
4532 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004533
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004534 default:
4535 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004536 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004537 return -1;
4538 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004539}
Willy Tarreauae526782010-03-04 20:34:23 +01004540
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004541/* We reached the stats page through a POST request. The appctx is
4542 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004543 * Parse the posted data and enable/disable servers if necessary.
4544 * Returns 1 if request was parsed or zero if it needs more data.
4545 */
4546static int stats_process_http_post(struct stream_interface *si)
4547{
Willy Tarreau87b09662015-04-03 00:22:06 +02004548 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004549 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004550
4551 struct proxy *px = NULL;
4552 struct server *sv = NULL;
4553
4554 char key[LINESIZE];
4555 int action = ST_ADM_ACTION_NONE;
4556 int reprocess = 0;
4557
4558 int total_servers = 0;
4559 int altered_servers = 0;
4560
4561 char *first_param, *cur_param, *next_param, *end_params;
4562 char *st_cur_param = NULL;
4563 char *st_next_param = NULL;
4564
4565 struct chunk *temp;
4566 int reql;
4567
4568 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02004569 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004570 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004571 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004572 goto out;
4573 }
4574
Willy Tarreaueee5b512015-04-03 23:46:31 +02004575 reql = bo_getblk(si_oc(si), temp->str, s->txn->req.body_len, s->txn->req.eoh + 2);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004576 if (reql <= 0) {
4577 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004578 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004579 return 0;
4580 }
4581
4582 first_param = temp->str;
4583 end_params = temp->str + reql;
4584 cur_param = next_param = end_params;
4585 *end_params = '\0';
4586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004587 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004588
4589 /*
4590 * Parse the parameters in reverse order to only store the last value.
4591 * From the html form, the backend and the action are at the end.
4592 */
4593 while (cur_param > first_param) {
4594 char *value;
4595 int poffset, plen;
4596
4597 cur_param--;
4598
4599 if ((*cur_param == '&') || (cur_param == first_param)) {
4600 reprocess_servers:
4601 /* Parse the key */
4602 poffset = (cur_param != first_param ? 1 : 0);
4603 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4604 if ((plen > 0) && (plen <= sizeof(key))) {
4605 strncpy(key, cur_param + poffset, plen);
4606 key[plen - 1] = '\0';
4607 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004608 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004609 goto out;
4610 }
4611
4612 /* Parse the value */
4613 value = key;
4614 while (*value != '\0' && *value != '=') {
4615 value++;
4616 }
4617 if (*value == '=') {
4618 /* Ok, a value is found, we can mark the end of the key */
4619 *value++ = '\0';
4620 }
4621 if (url_decode(key) < 0 || url_decode(value) < 0)
4622 break;
4623
4624 /* Now we can check the key to see what to do */
4625 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02004626 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004627 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004628 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004629 goto out;
4630 }
4631 }
4632 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004633 if (strcmp(value, "ready") == 0) {
4634 action = ST_ADM_ACTION_READY;
4635 }
4636 else if (strcmp(value, "drain") == 0) {
4637 action = ST_ADM_ACTION_DRAIN;
4638 }
4639 else if (strcmp(value, "maint") == 0) {
4640 action = ST_ADM_ACTION_MAINT;
4641 }
4642 else if (strcmp(value, "shutdown") == 0) {
4643 action = ST_ADM_ACTION_SHUTDOWN;
4644 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004645 else if (strcmp(value, "dhlth") == 0) {
4646 action = ST_ADM_ACTION_DHLTH;
4647 }
4648 else if (strcmp(value, "ehlth") == 0) {
4649 action = ST_ADM_ACTION_EHLTH;
4650 }
4651 else if (strcmp(value, "hrunn") == 0) {
4652 action = ST_ADM_ACTION_HRUNN;
4653 }
4654 else if (strcmp(value, "hnolb") == 0) {
4655 action = ST_ADM_ACTION_HNOLB;
4656 }
4657 else if (strcmp(value, "hdown") == 0) {
4658 action = ST_ADM_ACTION_HDOWN;
4659 }
4660 else if (strcmp(value, "dagent") == 0) {
4661 action = ST_ADM_ACTION_DAGENT;
4662 }
4663 else if (strcmp(value, "eagent") == 0) {
4664 action = ST_ADM_ACTION_EAGENT;
4665 }
4666 else if (strcmp(value, "arunn") == 0) {
4667 action = ST_ADM_ACTION_ARUNN;
4668 }
4669 else if (strcmp(value, "adown") == 0) {
4670 action = ST_ADM_ACTION_ADOWN;
4671 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004672 /* else these are the old supported methods */
4673 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004674 action = ST_ADM_ACTION_DISABLE;
4675 }
4676 else if (strcmp(value, "enable") == 0) {
4677 action = ST_ADM_ACTION_ENABLE;
4678 }
4679 else if (strcmp(value, "stop") == 0) {
4680 action = ST_ADM_ACTION_STOP;
4681 }
4682 else if (strcmp(value, "start") == 0) {
4683 action = ST_ADM_ACTION_START;
4684 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004685 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004686 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004687 goto out;
4688 }
4689 }
4690 else if (strcmp(key, "s") == 0) {
4691 if (!(px && action)) {
4692 /*
4693 * Indicates that we'll need to reprocess the parameters
4694 * as soon as backend and action are known
4695 */
4696 if (!reprocess) {
4697 st_cur_param = cur_param;
4698 st_next_param = next_param;
4699 }
4700 reprocess = 1;
4701 }
4702 else if ((sv = findserver(px, value)) != NULL) {
4703 switch (action) {
4704 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004705 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004706 altered_servers++;
4707 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004708 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004709 }
4710 break;
4711 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004712 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004713 altered_servers++;
4714 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004715 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004716 }
4717 break;
4718 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004719 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4720 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4721 altered_servers++;
4722 total_servers++;
4723 }
4724 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004725 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004726 if (sv->admin & SRV_ADMF_FDRAIN) {
4727 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4728 altered_servers++;
4729 total_servers++;
4730 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004731 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004732 case ST_ADM_ACTION_DHLTH:
4733 if (sv->check.state & CHK_ST_CONFIGURED) {
4734 sv->check.state &= ~CHK_ST_ENABLED;
4735 altered_servers++;
4736 total_servers++;
4737 }
4738 break;
4739 case ST_ADM_ACTION_EHLTH:
4740 if (sv->check.state & CHK_ST_CONFIGURED) {
4741 sv->check.state |= CHK_ST_ENABLED;
4742 altered_servers++;
4743 total_servers++;
4744 }
4745 break;
4746 case ST_ADM_ACTION_HRUNN:
4747 if (!(sv->track)) {
4748 sv->check.health = sv->check.rise + sv->check.fall - 1;
4749 srv_set_running(sv, "changed from Web interface");
4750 altered_servers++;
4751 total_servers++;
4752 }
4753 break;
4754 case ST_ADM_ACTION_HNOLB:
4755 if (!(sv->track)) {
4756 sv->check.health = sv->check.rise + sv->check.fall - 1;
4757 srv_set_stopping(sv, "changed from Web interface");
4758 altered_servers++;
4759 total_servers++;
4760 }
4761 break;
4762 case ST_ADM_ACTION_HDOWN:
4763 if (!(sv->track)) {
4764 sv->check.health = 0;
4765 srv_set_stopped(sv, "changed from Web interface");
4766 altered_servers++;
4767 total_servers++;
4768 }
4769 break;
4770 case ST_ADM_ACTION_DAGENT:
4771 if (sv->agent.state & CHK_ST_CONFIGURED) {
4772 sv->agent.state &= ~CHK_ST_ENABLED;
4773 altered_servers++;
4774 total_servers++;
4775 }
4776 break;
4777 case ST_ADM_ACTION_EAGENT:
4778 if (sv->agent.state & CHK_ST_CONFIGURED) {
4779 sv->agent.state |= CHK_ST_ENABLED;
4780 altered_servers++;
4781 total_servers++;
4782 }
4783 break;
4784 case ST_ADM_ACTION_ARUNN:
4785 if (sv->agent.state & CHK_ST_ENABLED) {
4786 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4787 srv_set_running(sv, "changed from Web interface");
4788 altered_servers++;
4789 total_servers++;
4790 }
4791 break;
4792 case ST_ADM_ACTION_ADOWN:
4793 if (sv->agent.state & CHK_ST_ENABLED) {
4794 sv->agent.health = 0;
4795 srv_set_stopped(sv, "changed from Web interface");
4796 altered_servers++;
4797 total_servers++;
4798 }
4799 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004800 case ST_ADM_ACTION_READY:
4801 srv_adm_set_ready(sv);
4802 altered_servers++;
4803 total_servers++;
4804 break;
4805 case ST_ADM_ACTION_DRAIN:
4806 srv_adm_set_drain(sv);
4807 altered_servers++;
4808 total_servers++;
4809 break;
4810 case ST_ADM_ACTION_MAINT:
4811 srv_adm_set_maint(sv);
4812 altered_servers++;
4813 total_servers++;
4814 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004815 case ST_ADM_ACTION_SHUTDOWN:
4816 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004817 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004818
4819 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4820 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004821 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004822
4823 altered_servers++;
4824 total_servers++;
4825 }
4826 break;
4827 }
4828 } else {
4829 /* the server name is unknown or ambiguous (duplicate names) */
4830 total_servers++;
4831 }
4832 }
4833 if (reprocess && px && action) {
4834 /* Now, we know the backend and the action chosen by the user.
4835 * We can safely restart from the first server parameter
4836 * to reprocess them
4837 */
4838 cur_param = st_cur_param;
4839 next_param = st_next_param;
4840 reprocess = 0;
4841 goto reprocess_servers;
4842 }
4843
4844 next_param = cur_param;
4845 }
4846 }
4847
4848 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004849 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004850 }
4851 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004852 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004853 }
4854 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004855 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004856 }
4857 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004858 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004859 }
4860 out:
4861 return 1;
4862}
4863
4864
4865static int stats_send_http_headers(struct stream_interface *si)
4866{
Willy Tarreau87b09662015-04-03 00:22:06 +02004867 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004868 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004869 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004870
4871 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004872 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004873 "Cache-Control: no-cache\r\n"
4874 "Connection: close\r\n"
4875 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004876 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004877
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004878 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004879 chunk_appendf(&trash, "Refresh: %d\r\n",
4880 uri->refresh);
4881
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004882 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4883
4884 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4885 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4886 else
4887 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004888
Willy Tarreaueee5b512015-04-03 23:46:31 +02004889 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004890 s->logs.tv_request = now;
4891
Willy Tarreaubc18da12015-03-13 14:00:47 +01004892 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004893 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004894 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004895 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004896
4897 return 1;
4898}
4899
4900static int stats_send_http_redirect(struct stream_interface *si)
4901{
4902 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02004903 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004904 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004905 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004906
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004907 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004908 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004909 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004910 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004911 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004912 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004913 }
4914
4915 /* We don't want to land on the posted stats page because a refresh will
4916 * repost the data. We don't want this to happen on accident so we redirect
4917 * the browse to the stats page with a GET.
4918 */
4919 chunk_printf(&trash,
4920 "HTTP/1.1 303 See Other\r\n"
4921 "Cache-Control: no-cache\r\n"
4922 "Content-Type: text/plain\r\n"
4923 "Connection: close\r\n"
4924 "Location: %s;st=%s%s%s%s\r\n"
4925 "\r\n",
4926 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004927 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4928 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4929 stat_status_codes[appctx->ctx.stats.st_code]) ?
4930 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004931 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004932 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4933 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004934 scope_txt);
4935
Willy Tarreaueee5b512015-04-03 23:46:31 +02004936 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004937 s->logs.tv_request = now;
4938
Willy Tarreaubc18da12015-03-13 14:00:47 +01004939 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004940 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004941 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004942 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004943
4944 return 1;
4945}
4946
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004947/* This I/O handler runs as an applet embedded in a stream interface. It is
4948 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004949 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004950 * automatically unregisters itself once transfer is complete.
4951 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02004952static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004953{
Willy Tarreau00a37f02015-04-13 12:05:19 +02004954 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02004955 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004956 struct channel *req = si_oc(si);
4957 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01004958
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004959 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4960 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004961
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004962 /* check that the output is not closed */
4963 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004964 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004965
Willy Tarreau347a35d2013-11-22 17:51:09 +01004966 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004967 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004968 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02004969 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004970 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004971 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004972 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004973 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004974 }
4975
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004976 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004977 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004978 unsigned int data_len;
4979 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004980 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004981
4982 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4983 /* One difficulty we're facing is that we must prevent
4984 * the input data from being automatically forwarded to
4985 * the output area. For this, we temporarily disable
4986 * forwarding on the channel.
4987 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004988 last_fwd = si_ic(si)->to_forward;
4989 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004990 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004991 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004992 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004993 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02004994 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004995 }
4996 }
4997
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004998 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004999 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005000 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005001
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005002 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005003
5004 /* Now we must either adjust or remove the chunk size. This is
5005 * not easy because the chunk size might wrap at the end of the
5006 * buffer, so we pretend we have nothing in the buffer, we write
5007 * the size, then restore the buffer's contents. Note that we can
5008 * only do that because no forwarding is scheduled on the stats
5009 * applet.
5010 */
5011 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005012 si_ic(si)->total -= (last_len - prev_len);
5013 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005014
5015 if (last_len != data_len) {
5016 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005017 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005018 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005019
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005020 si_ic(si)->total += (last_len - data_len);
5021 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005022 }
5023 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005024 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005025 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005026 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005027
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005028 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005029 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005030 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005031 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005032 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005033 }
5034
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005035 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005036 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005037 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005038 }
5039
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005040 if (appctx->st0 == STAT_HTTP_DONE) {
5041 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5042 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005043 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005044 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005045 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005046 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005047 }
5048 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005049 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005050 res->flags |= CF_READ_NULL;
5051 si_shutr(si);
5052 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005053
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005054 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5055 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005056
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005057 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005058 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5059 si_shutr(si);
5060 res->flags |= CF_READ_NULL;
5061 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005062 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005063 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005064 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005065}
5066
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005067
Willy Tarreau909d5172012-11-26 03:04:41 +01005068static inline const char *get_conn_ctrl_name(const struct connection *conn)
5069{
Willy Tarreau3c728722014-01-23 13:50:42 +01005070 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005071 return "NONE";
5072 return conn->ctrl->name;
5073}
5074
5075static inline const char *get_conn_xprt_name(const struct connection *conn)
5076{
5077 static char ptr[17];
5078
Willy Tarreauaad69382014-01-23 14:21:42 +01005079 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005080 return "NONE";
5081
5082 if (conn->xprt == &raw_sock)
5083 return "RAW";
5084
5085#ifdef USE_OPENSSL
5086 if (conn->xprt == &ssl_sock)
5087 return "SSL";
5088#endif
5089 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5090 return ptr;
5091}
5092
5093static inline const char *get_conn_data_name(const struct connection *conn)
5094{
5095 static char ptr[17];
5096
5097 if (!conn->data)
5098 return "NONE";
5099
5100 if (conn->data == &sess_conn_cb)
5101 return "SESS";
5102
5103 if (conn->data == &si_conn_cb)
5104 return "STRM";
5105
5106 if (conn->data == &check_conn_cb)
5107 return "CHCK";
5108
5109 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5110 return ptr;
5111}
5112
Willy Tarreau87b09662015-04-03 00:22:06 +02005113/* This function dumps a complete stream state onto the stream interface's
5114 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005115 * 0 if the output buffer is full and it needs to be called again, otherwise
5116 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005117 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005118static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005119{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005120 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005121 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005122 extern const char *monthname[12];
5123 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005124 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005125 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005126
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005127 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005128
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005129 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005130 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005131 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005132 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005133 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005134 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005135 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005136 appctx->ctx.sess.uid = 0;
5137 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005138 return 1;
5139 }
5140
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005141 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005142 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005143 appctx->ctx.sess.uid = sess->uniq_id;
5144 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005145 /* fall through */
5146
5147 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005148 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005149 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005150 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005151 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005152 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5153 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005154 sess->uniq_id,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005155 strm_li(sess) && strm_li(sess)->proto->name ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005156
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005157 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005158 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005159 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005160 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005161 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005162 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005163 break;
5164 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005165 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005166 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005167 default:
5168 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005169 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005170 break;
5171 }
5172
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005173 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005174 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005175 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005176
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005177 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005178 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005179 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5180 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5181 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005182
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005183 if (conn)
5184 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005185
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005186 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005187 case AF_INET:
5188 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005189 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005190 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005191 break;
5192 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005193 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005194 break;
5195 default:
5196 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005197 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005198 break;
5199 }
5200
Willy Tarreau50943332011-09-02 17:33:05 +02005201 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005202 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005203 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005204 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005205 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5206 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005207 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005208
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005209 conn = objt_conn(sess->si[1].end);
5210 if (conn)
5211 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005212
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005213 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005214 case AF_INET:
5215 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005216 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005217 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005218 break;
5219 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005220 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005221 break;
5222 default:
5223 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005224 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005225 break;
5226 }
5227
5228 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005229 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005230 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005231 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5232 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005233 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005234 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005235
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005236 if (conn)
5237 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005238
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005239 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005240 case AF_INET:
5241 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005242 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005243 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005244 break;
5245 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005246 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005247 break;
5248 default:
5249 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005250 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005251 break;
5252 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005253
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005254 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005255 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005256 sess->task,
5257 sess->task->state,
5258 sess->task->nice, sess->task->calls,
5259 sess->task->expire ?
5260 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5261 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5262 TICKS_TO_MS(1000)) : "<NEVER>",
5263 task_in_rq(sess->task) ? ", running" : "");
5264
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005265 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005266 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005267 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5268
Willy Tarreaueee5b512015-04-03 23:46:31 +02005269 if (sess->txn)
5270 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005271 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s waiting=%d\n",
Willy Tarreaueee5b512015-04-03 23:46:31 +02005272 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5273 http_msg_state_str(sess->txn->req.msg_state), http_msg_state_str(sess->txn->rsp.msg_state), !LIST_ISEMPTY(&sess->buffer_wait));
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005274
5275 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005276 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005277 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005278 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005279 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005280 obj_type_name(sess->si[0].end),
5281 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005282 sess->si[0].exp ?
5283 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5284 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5285 TICKS_TO_MS(1000)) : "<NEVER>",
5286 sess->si[0].err_type);
5287
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005288 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005289 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005290 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005291 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005292 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005293 obj_type_name(sess->si[1].end),
5294 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005295 sess->si[1].exp ?
5296 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5297 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5298 TICKS_TO_MS(1000)) : "<NEVER>",
5299 sess->si[1].err_type);
5300
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005301 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005302 chunk_appendf(&trash,
5303 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005304 conn,
5305 get_conn_ctrl_name(conn),
5306 get_conn_xprt_name(conn),
5307 get_conn_data_name(conn),
5308 obj_type_name(conn->target),
5309 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005310
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005311 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005312 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005313 conn->flags,
5314 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005315 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005316 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005317 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005318 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005319 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5320 chunk_appendf(&trash,
5321 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5322 tmpctx,
5323 tmpctx->st0,
5324 tmpctx->st1,
5325 tmpctx->st2,
5326 tmpctx->applet->name);
5327 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005328
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005329 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005330 chunk_appendf(&trash,
5331 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005332 conn,
5333 get_conn_ctrl_name(conn),
5334 get_conn_xprt_name(conn),
5335 get_conn_data_name(conn),
5336 obj_type_name(conn->target),
5337 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005338
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005339 chunk_appendf(&trash,
5340 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005341 conn->flags,
5342 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005343 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005344 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005345 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005346 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005347 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5348 chunk_appendf(&trash,
5349 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5350 tmpctx,
5351 tmpctx->st0,
5352 tmpctx->st1,
5353 tmpctx->st2,
5354 tmpctx->applet->name);
5355 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005356
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005357 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005358 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005359 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005360 &sess->req,
5361 sess->req.flags, sess->req.analysers,
5362 sess->req.pipe ? sess->req.pipe->data : 0,
5363 sess->req.to_forward, sess->req.total,
5364 sess->req.analyse_exp ?
5365 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005366 TICKS_TO_MS(1000)) : "<NEVER>");
5367
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005368 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005369 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005370 sess->req.rex ?
5371 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005372 TICKS_TO_MS(1000)) : "<NEVER>");
5373
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005374 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005375 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005376 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005377 sess->req.wex ?
5378 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005379 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005380 sess->req.buf,
5381 sess->req.buf->data, sess->req.buf->o,
5382 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005383 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005384 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005385
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005386 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005387 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005388 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005389 &sess->res,
5390 sess->res.flags, sess->res.analysers,
5391 sess->res.pipe ? sess->res.pipe->data : 0,
5392 sess->res.to_forward, sess->res.total,
5393 sess->res.analyse_exp ?
5394 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005395 TICKS_TO_MS(1000)) : "<NEVER>");
5396
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005397 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005398 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005399 sess->res.rex ?
5400 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005401 TICKS_TO_MS(1000)) : "<NEVER>");
5402
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005403 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005404 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005405 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005406 sess->res.wex ?
5407 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005408 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005409 sess->res.buf,
5410 sess->res.buf->data, sess->res.buf->o,
5411 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005412 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005413 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005414
Willy Tarreaubc18da12015-03-13 14:00:47 +01005415 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005416 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005417 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005418 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005419
5420 /* use other states to dump the contents */
5421 }
5422 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005423 appctx->ctx.sess.uid = 0;
5424 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005425 return 1;
5426}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005427
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005428#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5429static int stats_tlskeys_list(struct stream_interface *si) {
5430 struct appctx *appctx = __objt_appctx(si->end);
5431
5432 switch (appctx->st2) {
5433 case STAT_ST_INIT:
5434 /* Display the column headers. If the message cannot be sent,
5435 * quit the fucntion with returning 0. The function is called
5436 * later and restart at the state "STAT_ST_INIT".
5437 */
5438 chunk_reset(&trash);
5439 chunk_appendf(&trash, "# id (file)\n");
5440 if (bi_putchk(si_ic(si), &trash) == -1) {
5441 si_applet_cant_put(si);
5442 return 0;
5443 }
5444
5445 /* Now, we start the browsing of the references lists.
5446 * Note that the following call to LIST_ELEM return bad pointer. The only
5447 * avalaible field of this pointer is <list>. It is used with the function
5448 * tlskeys_list_get_next() for retruning the first avalaible entry
5449 */
5450 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
5451 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5452
5453 appctx->st2 = STAT_ST_LIST;
5454 /* fall through */
5455
5456 case STAT_ST_LIST:
5457 while (appctx->ctx.tlskeys.ref) {
5458 chunk_reset(&trash);
5459
5460 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
5461 appctx->ctx.tlskeys.ref->filename);
5462
5463 if (bi_putchk(si_ic(si), &trash) == -1) {
5464 /* let's try again later from this stream. We add ourselves into
5465 * this stream's users so that it can remove us upon termination.
5466 */
5467 si_applet_cant_put(si);
5468 return 0;
5469 }
5470
5471 /* get next list entry and check the end of the list */
5472 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5473 }
5474
5475 appctx->st2 = STAT_ST_FIN;
5476 /* fall through */
5477
5478 default:
5479 appctx->st2 = STAT_ST_FIN;
5480 return 1;
5481 }
5482 return 0;
5483}
5484#endif
5485
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005486static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005487{
5488 struct appctx *appctx = __objt_appctx(si->end);
5489
5490 switch (appctx->st2) {
5491 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005492 /* Display the column headers. If the message cannot be sent,
5493 * quit the fucntion with returning 0. The function is called
5494 * later and restart at the state "STAT_ST_INIT".
5495 */
5496 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005497 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005498 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005499 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005500 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005501 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005502
5503 /* Now, we start the browsing of the references lists.
5504 * Note that the following call to LIST_ELEM return bad pointer. The only
5505 * avalaible field of this pointer is <list>. It is used with the function
5506 * pat_list_get_next() for retruning the first avalaible entry
5507 */
5508 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5509 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5510 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005511 appctx->st2 = STAT_ST_LIST;
5512 /* fall through */
5513
5514 case STAT_ST_LIST:
5515 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005516 chunk_reset(&trash);
5517
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005518 /* Build messages. If the reference is used by another category than
5519 * the listed categorie, display the information in the massage.
5520 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005521 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005522 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5523 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005524
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005525 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005526 /* let's try again later from this stream. We add ourselves into
5527 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005528 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005529 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005530 return 0;
5531 }
5532
5533 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005534 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5535 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005536 }
5537
5538 appctx->st2 = STAT_ST_FIN;
5539 /* fall through */
5540
5541 default:
5542 appctx->st2 = STAT_ST_FIN;
5543 return 1;
5544 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005545 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005546}
5547
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005548static int stats_map_lookup(struct stream_interface *si)
5549{
5550 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005551 struct sample sample;
5552 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005553 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005554
5555 switch (appctx->st2) {
5556 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005557 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005558 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005559 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005560 appctx->st2 = STAT_ST_LIST;
5561 /* fall through */
5562
5563 case STAT_ST_LIST:
5564 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005565 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005566 /* initialise chunk to build new message */
5567 chunk_reset(&trash);
5568
5569 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005570 sample.type = SMP_T_STR;
5571 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005572 sample.data.str.len = appctx->ctx.map.chunk.len;
5573 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005574 if (appctx->ctx.map.expr->pat_head->match &&
5575 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005576 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5577 else
5578 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005579
5580 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005581 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5582 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5583 break;
5584 if (match_method >= PAT_MATCH_NUM)
5585 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5586 else
5587 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005588
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005589 /* case sensitive */
5590 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5591 chunk_appendf(&trash, ", case=insensitive");
5592 else
5593 chunk_appendf(&trash, ", case=sensitive");
5594
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005595 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005596 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005597 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5598 chunk_appendf(&trash, ", found=no");
5599 else
5600 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005601 }
5602
5603 /* Display match and match info */
5604 else {
5605 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005606 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5607 chunk_appendf(&trash, ", found=yes");
5608 else
5609 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005610
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005611 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005612 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005613 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005614 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005615 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005616
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005617 /* display pattern */
5618 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5619 if (pat->ref && pat->ref->pattern)
5620 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5621 else
5622 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005623 }
5624 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005625 if (pat->ref && pat->ref->pattern)
5626 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5627 else
5628 chunk_appendf(&trash, ", pattern=unknown");
5629 }
5630
5631 /* display return value */
5632 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5633 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005634 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005635 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005636 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005637 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005638 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005639 }
5640
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005641 chunk_appendf(&trash, "\n");
5642
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005643 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005644 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005645 /* let's try again later from this stream. We add ourselves into
5646 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005647 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005648 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005649 return 0;
5650 }
5651
5652 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005653 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5654 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005655 }
5656
5657 appctx->st2 = STAT_ST_FIN;
5658 /* fall through */
5659
5660 default:
5661 appctx->st2 = STAT_ST_FIN;
5662 free(appctx->ctx.map.chunk.str);
5663 return 1;
5664 }
5665}
5666
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005667static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005668{
5669 struct appctx *appctx = __objt_appctx(si->end);
5670
5671 switch (appctx->st2) {
5672
5673 case STAT_ST_INIT:
5674 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005675 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5676 struct pat_ref_elt *, list);
5677 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5678 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005679 appctx->st2 = STAT_ST_LIST;
5680 /* fall through */
5681
5682 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005683 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005684 chunk_reset(&trash);
5685
5686 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005687 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005688 chunk_appendf(&trash, "%p %s %s\n",
5689 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5690 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005691 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005692 chunk_appendf(&trash, "%p %s\n",
5693 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005694
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005695 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005696 /* let's try again later from this stream. We add ourselves into
5697 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005698 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005699 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005700 return 0;
5701 }
5702
5703 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005704 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5705 struct pat_ref_elt *, list);
5706 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005707 break;
5708 }
5709
5710 appctx->st2 = STAT_ST_FIN;
5711 /* fall through */
5712
5713 default:
5714 appctx->st2 = STAT_ST_FIN;
5715 return 1;
5716 }
5717}
5718
Willy Tarreau87b09662015-04-03 00:22:06 +02005719/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005720 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005721 * to be called again, otherwise non-zero. It is designed to be called
5722 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005723 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005724static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005725{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005726 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005727 struct connection *conn;
5728
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005729 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005730 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02005731 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005732 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005733 if (appctx->st2 == STAT_ST_LIST) {
5734 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5735 LIST_DEL(&appctx->ctx.sess.bref.users);
5736 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005737 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005738 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005739 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005740 }
5741
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005742 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005743
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005744 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005745 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005746 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02005747 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005748 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02005749 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005750 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02005751 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005752 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005753 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02005754 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005755 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005756 /* fall through */
5757
Willy Tarreau295a8372011-03-10 11:25:07 +01005758 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02005759 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005760 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5761 LIST_DEL(&appctx->ctx.sess.bref.users);
5762 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005763 }
5764
5765 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02005766 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005767 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005768 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005769
Willy Tarreau87b09662015-04-03 00:22:06 +02005770 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005771
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005772 if (appctx->ctx.sess.target) {
5773 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005774 goto next_sess;
5775
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005776 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005777 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005778 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005779 return 0;
5780
Willy Tarreau87b09662015-04-03 00:22:06 +02005781 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005782 LIST_DEL(&appctx->ctx.sess.bref.users);
5783 LIST_INIT(&appctx->ctx.sess.bref.users);
5784 if (appctx->ctx.sess.target != (void *)-1) {
5785 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005786 break;
5787 }
5788 else
5789 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005790 }
5791
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005792 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005793 "%p: proto=%s",
5794 curr_sess,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005795 strm_li(curr_sess)->proto->name);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005796
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005797
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005798 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005799 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005800 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005801 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005802 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005803 " src=%s:%d fe=%s be=%s srv=%s",
5804 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005805 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005806 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005807 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005808 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005809 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005810 break;
5811 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005812 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005813 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005814 strm_li(curr_sess)->luid,
5815 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005816 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005817 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005818 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005819 break;
5820 }
5821
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005822 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005823 " ts=%02x age=%s calls=%d",
5824 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005825 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5826 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005827
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005828 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005829 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005830 curr_sess->req.flags,
5831 curr_sess->req.buf->i,
5832 curr_sess->req.analysers,
5833 curr_sess->req.rex ?
5834 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005835 TICKS_TO_MS(1000)) : "");
5836
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005837 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005838 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005839 curr_sess->req.wex ?
5840 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005841 TICKS_TO_MS(1000)) : "");
5842
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005843 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005844 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005845 curr_sess->req.analyse_exp ?
5846 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005847 TICKS_TO_MS(1000)) : "");
5848
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005849 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005850 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005851 curr_sess->res.flags,
5852 curr_sess->res.buf->i,
5853 curr_sess->res.analysers,
5854 curr_sess->res.rex ?
5855 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005856 TICKS_TO_MS(1000)) : "");
5857
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005858 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005859 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005860 curr_sess->res.wex ?
5861 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005862 TICKS_TO_MS(1000)) : "");
5863
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005864 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005865 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005866 curr_sess->res.analyse_exp ?
5867 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005868 TICKS_TO_MS(1000)) : "");
5869
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005870 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005871 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005872 " s0=[%d,%1xh,fd=%d,ex=%s]",
5873 curr_sess->si[0].state,
5874 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005875 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005876 curr_sess->si[0].exp ?
5877 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5878 TICKS_TO_MS(1000)) : "");
5879
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005880 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005881 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005882 " s1=[%d,%1xh,fd=%d,ex=%s]",
5883 curr_sess->si[1].state,
5884 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005885 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005886 curr_sess->si[1].exp ?
5887 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5888 TICKS_TO_MS(1000)) : "");
5889
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005890 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005891 " exp=%s",
5892 curr_sess->task->expire ?
5893 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5894 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005895 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005896 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005897
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005898 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005899
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005900 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005901 /* let's try again later from this stream. We add ourselves into
5902 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005903 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005904 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005905 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005906 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005907 }
5908
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005909 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005910 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005911 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005912
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005913 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005914 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005915 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005916 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005917 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005918 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005919
Willy Tarreaubc18da12015-03-13 14:00:47 +01005920 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005921 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005922 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005923 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005924
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005925 appctx->ctx.sess.target = NULL;
5926 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005927 return 1;
5928 }
5929
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005930 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005931 /* fall through */
5932
5933 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005934 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005935 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005936 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005937}
5938
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005939/* This is called when the stream interface is closed. For instance, upon an
5940 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02005941 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005942 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005943static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005944{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005945 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5946 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5947 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005948 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005949 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5950 free(appctx->ctx.cli.err);
5951 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005952 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5953 free(appctx->ctx.map.chunk.str);
5954 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005955}
5956
Willy Tarreau20e99322013-04-13 09:22:25 +02005957/* This function is used to either dump tables states (when action is set
5958 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005959 * It returns 0 if the output buffer is full and it needs to be called
5960 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005961 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005962static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005963{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005964 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02005965 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005966 struct ebmb_node *eb;
5967 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005968 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005969 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005970
5971 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005972 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005973 * - STAT_ST_INIT : the first call
5974 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005975 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005976 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005977 * and the entry pointer points to the next entry to be dumped,
5978 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005979 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005980 * data though.
5981 */
5982
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005983 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005984 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005985 if (appctx->st2 == STAT_ST_LIST) {
5986 appctx->ctx.table.entry->ref_cnt--;
5987 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005988 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005989 return 1;
5990 }
5991
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005992 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005993
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005994 while (appctx->st2 != STAT_ST_FIN) {
5995 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005996 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005997 appctx->ctx.table.proxy = appctx->ctx.table.target;
5998 if (!appctx->ctx.table.proxy)
5999 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006000
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006001 appctx->ctx.table.entry = NULL;
6002 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006003 break;
6004
Willy Tarreau295a8372011-03-10 11:25:07 +01006005 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006006 if (!appctx->ctx.table.proxy ||
6007 (appctx->ctx.table.target &&
6008 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6009 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006010 break;
6011 }
6012
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006013 if (appctx->ctx.table.proxy->table.size) {
6014 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6015 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006016 return 0;
6017
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006018 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006019 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006020 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006021 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006022 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006023 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6024 appctx->ctx.table.entry->ref_cnt++;
6025 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006026 break;
6027 }
6028 }
6029 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006030 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006031 break;
6032
Willy Tarreau295a8372011-03-10 11:25:07 +01006033 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006034 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006035
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006036 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006037 /* we're filtering on some data contents */
6038 void *ptr;
6039 long long data;
6040
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006041 dt = appctx->ctx.table.data_type;
6042 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6043 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006044 dt);
6045
6046 data = 0;
6047 switch (stktable_data_types[dt].std_type) {
6048 case STD_T_SINT:
6049 data = stktable_data_cast(ptr, std_t_sint);
6050 break;
6051 case STD_T_UINT:
6052 data = stktable_data_cast(ptr, std_t_uint);
6053 break;
6054 case STD_T_ULL:
6055 data = stktable_data_cast(ptr, std_t_ull);
6056 break;
6057 case STD_T_FRQP:
6058 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006059 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006060 break;
6061 }
6062
6063 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006064 if ((data < appctx->ctx.table.value &&
6065 (appctx->ctx.table.data_op == STD_OP_EQ ||
6066 appctx->ctx.table.data_op == STD_OP_GT ||
6067 appctx->ctx.table.data_op == STD_OP_GE)) ||
6068 (data == appctx->ctx.table.value &&
6069 (appctx->ctx.table.data_op == STD_OP_NE ||
6070 appctx->ctx.table.data_op == STD_OP_GT ||
6071 appctx->ctx.table.data_op == STD_OP_LT)) ||
6072 (data > appctx->ctx.table.value &&
6073 (appctx->ctx.table.data_op == STD_OP_EQ ||
6074 appctx->ctx.table.data_op == STD_OP_LT ||
6075 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006076 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006077 }
6078
Simon Hormanc88b8872011-06-15 15:18:49 +09006079 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006080 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6081 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006082 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006083
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006084 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006085
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006086 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006087 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006088 struct stksess *old = appctx->ctx.table.entry;
6089 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006090 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006091 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6092 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6093 stksess_kill(&appctx->ctx.table.proxy->table, old);
6094 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006095 break;
6096 }
6097
Simon Hormanc88b8872011-06-15 15:18:49 +09006098
6099 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006100 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6101 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6102 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006103
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006104 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6105 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006106 break;
6107
Willy Tarreau295a8372011-03-10 11:25:07 +01006108 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006109 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006110 break;
6111 }
6112 }
6113 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006114}
6115
Willy Tarreaud426a182010-03-05 14:58:26 +01006116/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006117 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6118 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6119 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6120 * lines are respected within the limit of 70 output chars. Lines that are
6121 * continuation of a previous truncated line begin with "+" instead of " "
6122 * after the offset. The new pointer is returned.
6123 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006124static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6125 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006126{
6127 int end;
6128 unsigned char c;
6129
6130 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006131 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006132 return ptr;
6133
Willy Tarreau77804732012-10-29 16:14:26 +01006134 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006135
Willy Tarreaud426a182010-03-05 14:58:26 +01006136 while (ptr < len && ptr < bsize) {
6137 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006138 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006139 if (out->len > end - 2)
6140 break;
6141 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006142 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006143 if (out->len > end - 3)
6144 break;
6145 out->str[out->len++] = '\\';
6146 switch (c) {
6147 case '\t': c = 't'; break;
6148 case '\n': c = 'n'; break;
6149 case '\r': c = 'r'; break;
6150 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006151 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006152 }
6153 out->str[out->len++] = c;
6154 } else {
6155 if (out->len > end - 5)
6156 break;
6157 out->str[out->len++] = '\\';
6158 out->str[out->len++] = 'x';
6159 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6160 out->str[out->len++] = hextab[c & 0xF];
6161 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006162 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006163 /* we had a line break, let's return now */
6164 out->str[out->len++] = '\n';
6165 *line = ptr;
6166 return ptr;
6167 }
6168 }
6169 /* we have an incomplete line, we return it as-is */
6170 out->str[out->len++] = '\n';
6171 return ptr;
6172}
6173
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006174/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006175 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006176 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006177 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006178static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006179{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006180 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006181 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006182
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006183 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006184 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006185
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006186 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006187
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006188 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006189 /* the function had not been called yet, let's prepare the
6190 * buffer for a response.
6191 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006192 struct tm tm;
6193
6194 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006195 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006196 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6197 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6198 error_snapshot_id);
6199
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006200 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006201 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006202 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006203 return 0;
6204 }
6205
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006206 appctx->ctx.errors.px = proxy;
6207 appctx->ctx.errors.buf = 0;
6208 appctx->ctx.errors.bol = 0;
6209 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006210 }
6211
6212 /* we have two inner loops here, one for the proxy, the other one for
6213 * the buffer.
6214 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006215 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006216 struct error_snapshot *es;
6217
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006218 if (appctx->ctx.errors.buf == 0)
6219 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006220 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006221 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006222
6223 if (!es->when.tv_sec)
6224 goto next;
6225
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006226 if (appctx->ctx.errors.iid >= 0 &&
6227 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6228 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006229 goto next;
6230
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006231 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006232 /* just print headers now */
6233
6234 char pn[INET6_ADDRSTRLEN];
6235 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006236 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006237
6238 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006239 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006240 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006241 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006242
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006243 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6244 case AF_INET:
6245 case AF_INET6:
6246 port = get_host_port(&es->src);
6247 break;
6248 default:
6249 port = 0;
6250 }
6251
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006252 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006253 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006254 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006255 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006256 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006257 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006258 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6259 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006260 break;
6261 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006262 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006263 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006264 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006265 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006266 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006267 break;
6268 }
6269
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006270 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006271 ", server %s (#%d), event #%u\n"
6272 " src %s:%d, session #%d, session flags 0x%08x\n"
6273 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6274 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6275 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6276 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6277 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6278 es->ev_id,
6279 pn, port, es->sid, es->s_flags,
6280 es->state, es->m_flags, es->t_flags,
6281 es->m_clen, es->m_blen,
6282 es->b_flags, es->b_out, es->b_tot,
6283 es->len, es->b_wrap, es->pos);
6284
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006285 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006286 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006287 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006288 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006289 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006290 appctx->ctx.errors.ptr = 0;
6291 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006292 }
6293
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006294 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006295 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006296 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006297 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006298 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006299 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006300 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006301 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006302 goto next;
6303 }
6304
6305 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006306 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006307 int newptr;
6308 int newline;
6309
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006310 newline = appctx->ctx.errors.bol;
6311 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6312 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006313 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006314
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006315 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006316 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006317 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006318 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006319 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006320 appctx->ctx.errors.ptr = newptr;
6321 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006322 };
6323 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006324 appctx->ctx.errors.bol = 0;
6325 appctx->ctx.errors.ptr = -1;
6326 appctx->ctx.errors.buf++;
6327 if (appctx->ctx.errors.buf > 1) {
6328 appctx->ctx.errors.buf = 0;
6329 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006330 }
6331 }
6332
6333 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006334 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006335}
6336
Willy Tarreaud5781202012-09-22 19:32:35 +02006337/* parse the "level" argument on the bind lines */
6338static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6339{
6340 if (!*args[cur_arg + 1]) {
6341 memprintf(err, "'%s' : missing level", args[cur_arg]);
6342 return ERR_ALERT | ERR_FATAL;
6343 }
6344
6345 if (!strcmp(args[cur_arg+1], "user"))
6346 conf->level = ACCESS_LVL_USER;
6347 else if (!strcmp(args[cur_arg+1], "operator"))
6348 conf->level = ACCESS_LVL_OPER;
6349 else if (!strcmp(args[cur_arg+1], "admin"))
6350 conf->level = ACCESS_LVL_ADMIN;
6351 else {
6352 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6353 args[cur_arg], args[cur_arg+1]);
6354 return ERR_ALERT | ERR_FATAL;
6355 }
6356
6357 return 0;
6358}
6359
Willy Tarreau30576452015-04-13 13:50:30 +02006360struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006361 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006362 .name = "<STATS>", /* used for logging */
6363 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006364 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006365};
6366
Willy Tarreau30576452015-04-13 13:50:30 +02006367static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006368 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006369 .name = "<CLI>", /* used for logging */
6370 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006371 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006372};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006373
Willy Tarreaudc13c112013-06-21 23:16:39 +02006374static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006375 { CFG_GLOBAL, "stats", stats_parse_global },
6376 { 0, NULL, NULL },
6377}};
6378
Willy Tarreaud5781202012-09-22 19:32:35 +02006379static struct bind_kw_list bind_kws = { "STAT", { }, {
6380 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6381 { NULL, NULL, 0 },
6382}};
6383
Willy Tarreau10522fd2008-07-09 20:12:41 +02006384__attribute__((constructor))
6385static void __dumpstats_module_init(void)
6386{
6387 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006388 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006389}
6390
Willy Tarreau91861262007-10-17 17:06:05 +02006391/*
6392 * Local variables:
6393 * c-indent-level: 8
6394 * c-basic-offset: 8
6395 * End:
6396 */