blob: cd19279558b9ac68bdc84d6676eae6dd25500e01 [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"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200189 " set server : change a server's state, weight or address\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 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001513 else if (strcmp(args[3], "addr") == 0) {
1514 warning = server_parse_addr_change_request(sv, args[4]);
1515 if (warning) {
1516 appctx->ctx.cli.msg = warning;
1517 appctx->st0 = STAT_CLI_PRINT;
1518 }
1519 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001520 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001521 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001522 appctx->st0 = STAT_CLI_PRINT;
1523 }
1524 return 1;
1525 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001526 else if (strcmp(args[1], "timeout") == 0) {
1527 if (strcmp(args[2], "cli") == 0) {
1528 unsigned timeout;
1529 const char *res;
1530
1531 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001532 appctx->ctx.cli.msg = "Expects an integer value.\n";
1533 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001534 return 1;
1535 }
1536
1537 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1538 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001539 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1540 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001541 return 1;
1542 }
1543
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001544 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001545 return 1;
1546 }
1547 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001548 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1549 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001550 return 1;
1551 }
1552 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001553 else if (strcmp(args[1], "maxconn") == 0) {
1554 if (strcmp(args[2], "frontend") == 0) {
1555 struct proxy *px;
1556 struct listener *l;
1557 int v;
1558
Willy Tarreau532a4502011-09-07 22:37:44 +02001559 px = expect_frontend_admin(s, si, args[3]);
1560 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001561 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001562
1563 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001564 appctx->ctx.cli.msg = "Integer value expected.\n";
1565 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001566 return 1;
1567 }
1568
1569 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001570 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001571 appctx->ctx.cli.msg = "Value out of range.\n";
1572 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001573 return 1;
1574 }
1575
1576 /* OK, the value is fine, so we assign it to the proxy and to all of
1577 * its listeners. The blocked ones will be dequeued.
1578 */
1579 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001580 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001581 l->maxconn = v;
1582 if (l->state == LI_FULL)
1583 resume_listener(l);
1584 }
1585
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001586 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1587 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001588
1589 return 1;
1590 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001591 else if (strcmp(args[2], "global") == 0) {
1592 int v;
1593
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001594 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001595 appctx->ctx.cli.msg = stats_permission_denied_msg;
1596 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001597 return 1;
1598 }
1599
1600 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001601 appctx->ctx.cli.msg = "Expects an integer value.\n";
1602 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001603 return 1;
1604 }
1605
1606 v = atoi(args[3]);
1607 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001608 appctx->ctx.cli.msg = "Value out of range.\n";
1609 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001610 return 1;
1611 }
1612
1613 /* check for unlimited values */
1614 if (v <= 0)
1615 v = global.hardmaxconn;
1616
1617 global.maxconn = v;
1618
1619 /* Dequeues all of the listeners waiting for a resource */
1620 if (!LIST_ISEMPTY(&global_listener_queue))
1621 dequeue_all_listeners(&global_listener_queue);
1622
1623 return 1;
1624 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001625 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001626 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1627 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001628 return 1;
1629 }
1630 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001631 else if (strcmp(args[1], "rate-limit") == 0) {
1632 if (strcmp(args[2], "connections") == 0) {
1633 if (strcmp(args[3], "global") == 0) {
1634 int v;
1635
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001636 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001637 appctx->ctx.cli.msg = stats_permission_denied_msg;
1638 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001639 return 1;
1640 }
1641
1642 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001643 appctx->ctx.cli.msg = "Expects an integer value.\n";
1644 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001645 return 1;
1646 }
1647
1648 v = atoi(args[4]);
1649 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001650 appctx->ctx.cli.msg = "Value out of range.\n";
1651 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001652 return 1;
1653 }
1654
1655 global.cps_lim = v;
1656
1657 /* Dequeues all of the listeners waiting for a resource */
1658 if (!LIST_ISEMPTY(&global_listener_queue))
1659 dequeue_all_listeners(&global_listener_queue);
1660
1661 return 1;
1662 }
1663 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001664 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1665 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001666 return 1;
1667 }
1668 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001669 else if (strcmp(args[2], "sessions") == 0) {
1670 if (strcmp(args[3], "global") == 0) {
1671 int v;
1672
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001673 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001674 appctx->ctx.cli.msg = stats_permission_denied_msg;
1675 appctx->st0 = STAT_CLI_PRINT;
1676 return 1;
1677 }
1678
1679 if (!*args[4]) {
1680 appctx->ctx.cli.msg = "Expects an integer value.\n";
1681 appctx->st0 = STAT_CLI_PRINT;
1682 return 1;
1683 }
1684
1685 v = atoi(args[4]);
1686 if (v < 0) {
1687 appctx->ctx.cli.msg = "Value out of range.\n";
1688 appctx->st0 = STAT_CLI_PRINT;
1689 return 1;
1690 }
1691
1692 global.sps_lim = v;
1693
1694 /* Dequeues all of the listeners waiting for a resource */
1695 if (!LIST_ISEMPTY(&global_listener_queue))
1696 dequeue_all_listeners(&global_listener_queue);
1697
1698 return 1;
1699 }
1700 else {
1701 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1702 appctx->st0 = STAT_CLI_PRINT;
1703 return 1;
1704 }
1705 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001706#ifdef USE_OPENSSL
1707 else if (strcmp(args[2], "ssl-sessions") == 0) {
1708 if (strcmp(args[3], "global") == 0) {
1709 int v;
1710
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001711 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001712 appctx->ctx.cli.msg = stats_permission_denied_msg;
1713 appctx->st0 = STAT_CLI_PRINT;
1714 return 1;
1715 }
1716
1717 if (!*args[4]) {
1718 appctx->ctx.cli.msg = "Expects an integer value.\n";
1719 appctx->st0 = STAT_CLI_PRINT;
1720 return 1;
1721 }
1722
1723 v = atoi(args[4]);
1724 if (v < 0) {
1725 appctx->ctx.cli.msg = "Value out of range.\n";
1726 appctx->st0 = STAT_CLI_PRINT;
1727 return 1;
1728 }
1729
1730 global.ssl_lim = v;
1731
1732 /* Dequeues all of the listeners waiting for a resource */
1733 if (!LIST_ISEMPTY(&global_listener_queue))
1734 dequeue_all_listeners(&global_listener_queue);
1735
1736 return 1;
1737 }
1738 else {
1739 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1740 appctx->st0 = STAT_CLI_PRINT;
1741 return 1;
1742 }
1743 }
1744#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001745 else if (strcmp(args[2], "http-compression") == 0) {
1746 if (strcmp(args[3], "global") == 0) {
1747 int v;
1748
Willy Tarreau85d47f92012-11-21 00:29:50 +01001749 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001750 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1751 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001752 return 1;
1753 }
1754
William Lallemandd85f9172012-11-09 17:05:39 +01001755 v = atoi(args[4]);
1756 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1757 }
1758 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001759 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1760 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001761 return 1;
1762 }
1763 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001764 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001765 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001766 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001767 return 1;
1768 }
1769 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001770 else if (strcmp(args[1], "table") == 0) {
1771 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1772 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001773 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001774 char *err;
1775
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001776 /* Set flags. */
1777 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001778
1779 /* Expect three parameters: map name, key and new value. */
1780 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001781 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001782 appctx->st0 = STAT_CLI_PRINT;
1783 return 1;
1784 }
1785
1786 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001787 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001788 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001789 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001790 appctx->st0 = STAT_CLI_PRINT;
1791 return 1;
1792 }
1793
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001794 /* If the entry identifier start with a '#', it is considered as
1795 * pointer id
1796 */
1797 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1798 struct pat_ref_elt *ref;
1799 long long int conv;
1800 char *error;
1801
1802 /* Convert argument to integer value. */
1803 conv = strtoll(&args[3][1], &error, 16);
1804 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001805 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001806 appctx->st0 = STAT_CLI_PRINT;
1807 return 1;
1808 }
1809
1810 /* Convert and check integer to pointer. */
1811 ref = (struct pat_ref_elt *)(long)conv;
1812 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001813 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001814 appctx->st0 = STAT_CLI_PRINT;
1815 return 1;
1816 }
1817
1818 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001819 err = NULL;
1820 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1821 if (err)
1822 memprintf(&err, "%s.\n", err);
1823 appctx->ctx.cli.err = err;
1824 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001825 return 1;
1826 }
1827 }
1828 else {
1829 /* Else, use the entry identifier as pattern
1830 * string, and update the value.
1831 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001832 err = NULL;
1833 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1834 if (err)
1835 memprintf(&err, "%s.\n", err);
1836 appctx->ctx.cli.err = err;
1837 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001838 return 1;
1839 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001840 }
1841
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001842 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001843 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001844 return 1;
1845 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001846#ifdef USE_OPENSSL
1847 else if (strcmp(args[1], "ssl") == 0) {
1848 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01001849#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001850 char *err = NULL;
1851
Emeric Brunaf4ef742014-06-19 14:10:45 +02001852 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001853 if (!*args[3]) {
1854 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1855 appctx->st0 = STAT_CLI_PRINT;
1856 return 1;
1857 }
1858
1859 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1860 if (trash.len < 0) {
1861 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1862 appctx->st0 = STAT_CLI_PRINT;
1863 return 1;
1864 }
1865
1866 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1867 if (err) {
1868 memprintf(&err, "%s.\n", err);
1869 appctx->ctx.cli.err = err;
1870 appctx->st0 = STAT_CLI_PRINT_FREE;
1871 }
1872 return 1;
1873 }
1874 appctx->ctx.cli.msg = "OCSP Response updated!";
1875 appctx->st0 = STAT_CLI_PRINT;
1876 return 1;
1877#else
1878 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1879 appctx->st0 = STAT_CLI_PRINT;
1880 return 1;
1881#endif
1882 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001883 else if (strcmp(args[2], "tls-key") == 0) {
1884#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1885 /* Expect two parameters: the filename and the new new TLS key in encoding */
1886 if (!*args[3] || !*args[4]) {
1887 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
1888 appctx->st0 = STAT_CLI_PRINT;
1889 return 1;
1890 }
1891
1892 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
1893 if(!appctx->ctx.tlskeys.ref) {
1894 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
1895 appctx->st0 = STAT_CLI_PRINT;
1896 return 1;
1897 }
1898
1899 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
1900 if (trash.len != sizeof(struct tls_sess_key)) {
1901 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
1902 appctx->st0 = STAT_CLI_PRINT;
1903 return 1;
1904 }
1905
1906 memcpy(appctx->ctx.tlskeys.ref->tlskeys + 2 % TLS_TICKETS_NO, trash.str, trash.len);
1907 appctx->ctx.tlskeys.ref->tls_ticket_enc_index = appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 1 % TLS_TICKETS_NO;
1908
1909 appctx->ctx.cli.msg = "TLS ticket key updated!";
1910 appctx->st0 = STAT_CLI_PRINT;
1911 return 1;
1912#else
1913 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1914 "that doesn't support specifying TLS ticket keys\n";
1915 appctx->st0 = STAT_CLI_PRINT;
1916 return 1;
1917#endif
1918 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001919 else {
1920 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1921 appctx->st0 = STAT_CLI_PRINT;
1922 return 1;
1923 }
1924 }
1925#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001926 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001927 return 0;
1928 }
1929 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001930 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001931 if (strcmp(args[1], "agent") == 0) {
1932 struct server *sv;
1933
1934 sv = expect_server_admin(s, si, args[2]);
1935 if (!sv)
1936 return 1;
1937
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001938 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1939 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1940 appctx->st0 = STAT_CLI_PRINT;
1941 return 1;
1942 }
1943
1944 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001945 return 1;
1946 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001947 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001948 struct server *sv;
1949
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001950 sv = expect_server_admin(s, si, args[2]);
1951 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001952 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001953
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001954 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1955 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1956 appctx->st0 = STAT_CLI_PRINT;
1957 return 1;
1958 }
1959
1960 sv->check.state |= CHK_ST_ENABLED;
1961 return 1;
1962 }
1963 else if (strcmp(args[1], "server") == 0) {
1964 struct server *sv;
1965
1966 sv = expect_server_admin(s, si, args[2]);
1967 if (!sv)
1968 return 1;
1969
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001970 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001971 return 1;
1972 }
1973 else if (strcmp(args[1], "frontend") == 0) {
1974 struct proxy *px;
1975
1976 px = expect_frontend_admin(s, si, args[2]);
1977 if (!px)
1978 return 1;
1979
1980 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001981 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1982 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001983 return 1;
1984 }
1985
1986 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001987 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1988 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001989 return 1;
1990 }
1991
1992 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001993 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1994 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001995 return 1;
1996 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001997 return 1;
1998 }
1999 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002000 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002001 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002002 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002003 }
2004 }
2005 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002006 if (strcmp(args[1], "agent") == 0) {
2007 struct server *sv;
2008
2009 sv = expect_server_admin(s, si, args[2]);
2010 if (!sv)
2011 return 1;
2012
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002013 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002014 return 1;
2015 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002016 else if (strcmp(args[1], "health") == 0) {
2017 struct server *sv;
2018
2019 sv = expect_server_admin(s, si, args[2]);
2020 if (!sv)
2021 return 1;
2022
2023 sv->check.state &= ~CHK_ST_ENABLED;
2024 return 1;
2025 }
Simon Horman671b6f02013-11-25 10:46:39 +09002026 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002027 struct server *sv;
2028
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002029 sv = expect_server_admin(s, si, args[2]);
2030 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002031 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002032
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002033 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002034 return 1;
2035 }
2036 else if (strcmp(args[1], "frontend") == 0) {
2037 struct proxy *px;
2038
2039 px = expect_frontend_admin(s, si, args[2]);
2040 if (!px)
2041 return 1;
2042
2043 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002044 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2045 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002046 return 1;
2047 }
2048
2049 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002050 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2051 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002052 return 1;
2053 }
2054
2055 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002056 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2057 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002058 return 1;
2059 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002060 return 1;
2061 }
2062 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002063 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002064 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002065 return 1;
2066 }
2067 }
2068 else if (strcmp(args[0], "shutdown") == 0) {
2069 if (strcmp(args[1], "frontend") == 0) {
2070 struct proxy *px;
2071
2072 px = expect_frontend_admin(s, si, args[2]);
2073 if (!px)
2074 return 1;
2075
2076 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002077 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2078 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002079 return 1;
2080 }
2081
2082 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2083 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2084 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2085 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2086 stop_proxy(px);
2087 return 1;
2088 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002089 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002090 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002091
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002092 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002093 appctx->ctx.cli.msg = stats_permission_denied_msg;
2094 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002095 return 1;
2096 }
2097
2098 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002099 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2100 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002101 return 1;
2102 }
2103
2104 ptr = (void *)strtoul(args[2], NULL, 0);
2105
Willy Tarreau87b09662015-04-03 00:22:06 +02002106 /* first, look for the requested stream in the stream table */
2107 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002108 if (sess == ptr)
2109 break;
2110 }
2111
Willy Tarreau87b09662015-04-03 00:22:06 +02002112 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002113 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002114 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2115 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002116 return 1;
2117 }
2118
Willy Tarreaue7dff022015-04-03 01:14:29 +02002119 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002120 return 1;
2121 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002122 else if (strcmp(args[1], "sessions") == 0) {
2123 if (strcmp(args[2], "server") == 0) {
2124 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002125 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002126
2127 sv = expect_server_admin(s, si, args[3]);
2128 if (!sv)
2129 return 1;
2130
Willy Tarreau87b09662015-04-03 00:22:06 +02002131 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002132 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2133 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002134 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002135
2136 return 1;
2137 }
2138 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002139 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2140 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002141 return 1;
2142 }
2143 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002144 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002145 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2146 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002147 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002148 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002149 }
2150 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002151 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2152 if (args[1][0] == 'm')
2153 appctx->ctx.map.display_flags = PAT_REF_MAP;
2154 else
2155 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002156
2157 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002158 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2159 if (!*args[2] || !*args[3]) {
2160 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2161 appctx->st0 = STAT_CLI_PRINT;
2162 return 1;
2163 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002164 }
2165
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002166 else {
2167 if (!*args[2] || !*args[3]) {
2168 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2169 appctx->st0 = STAT_CLI_PRINT;
2170 return 1;
2171 }
2172 }
2173
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002174 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002175 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002176 if (!appctx->ctx.map.ref ||
2177 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002178 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002179 appctx->st0 = STAT_CLI_PRINT;
2180 return 1;
2181 }
2182
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002183 /* If the entry identifier start with a '#', it is considered as
2184 * pointer id
2185 */
2186 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2187 struct pat_ref_elt *ref;
2188 long long int conv;
2189 char *error;
2190
2191 /* Convert argument to integer value. */
2192 conv = strtoll(&args[3][1], &error, 16);
2193 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002194 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002195 appctx->st0 = STAT_CLI_PRINT;
2196 return 1;
2197 }
2198
2199 /* Convert and check integer to pointer. */
2200 ref = (struct pat_ref_elt *)(long)conv;
2201 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002202 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002203 appctx->st0 = STAT_CLI_PRINT;
2204 return 1;
2205 }
2206
2207 /* Try to delete the entry. */
2208 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2209 /* The entry is not found, send message. */
2210 appctx->ctx.cli.msg = "Key not found.\n";
2211 appctx->st0 = STAT_CLI_PRINT;
2212 return 1;
2213 }
2214 }
2215 else {
2216 /* Else, use the entry identifier as pattern
2217 * string and try to delete the entry.
2218 */
2219 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2220 /* The entry is not found, send message. */
2221 appctx->ctx.cli.msg = "Key not found.\n";
2222 appctx->st0 = STAT_CLI_PRINT;
2223 return 1;
2224 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002225 }
2226
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002227 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002228 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002229 return 1;
2230 }
2231 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002232 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002233 appctx->st0 = STAT_CLI_PRINT;
2234 return 1;
2235 }
2236 }
2237 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002238 if (strcmp(args[1], "map") == 0 ||
2239 strcmp(args[1], "acl") == 0) {
2240 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002241 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002242
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002243 /* Set flags. */
2244 if (args[1][0] == 'm')
2245 appctx->ctx.map.display_flags = PAT_REF_MAP;
2246 else
2247 appctx->ctx.map.display_flags = PAT_REF_ACL;
2248
2249 /* If the keywork is "map", we expect three parameters, if it
2250 * is "acl", we expect only two parameters
2251 */
2252 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2253 if (!*args[2] || !*args[3] || !*args[4]) {
2254 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2255 appctx->st0 = STAT_CLI_PRINT;
2256 return 1;
2257 }
2258 }
2259 else {
2260 if (!*args[2] || !*args[3]) {
2261 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2262 appctx->st0 = STAT_CLI_PRINT;
2263 return 1;
2264 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002265 }
2266
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002267 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002268 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002269 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002270 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002271 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002272 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002273 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002274 appctx->st0 = STAT_CLI_PRINT;
2275 return 1;
2276 }
2277
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002278 /* The command "add acl" is prohibited if the reference
2279 * use samples.
2280 */
2281 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2282 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2283 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2284 "You must use the command 'add map' to add values.\n";
2285 appctx->st0 = STAT_CLI_PRINT;
2286 return 1;
2287 }
2288
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002289 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002290 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002291 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002292 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002293 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002294 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002295 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002296 if (err)
2297 memprintf(&err, "%s.\n", err);
2298 appctx->ctx.cli.err = err;
2299 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002300 return 1;
2301 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002302
2303 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002304 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002305 return 1;
2306 }
2307 else { /* unknown "del" parameter */
2308 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2309 appctx->st0 = STAT_CLI_PRINT;
2310 return 1;
2311 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002312 }
2313 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002314 return 0;
2315 }
2316 return 1;
2317}
2318
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002319/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002320 * used to processes I/O from/to the stats unix socket. The system relies on a
2321 * state machine handling requests and various responses. We read a request,
2322 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002323 * Then we can read again. The state is stored in appctx->st0 and is one of the
2324 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002325 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002326 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002327static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002328{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002329 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002330 struct channel *req = si_oc(si);
2331 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002332 int reql;
2333 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002334
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002335 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2336 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002337
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002338 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002339 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002340 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002341 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2342 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002343 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002344 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002345 /* Let's close for real now. We just close the request
2346 * side, the conditions below will complete if needed.
2347 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002348 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002349 break;
2350 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002351 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002352 /* ensure we have some output room left in the event we
2353 * would want to return some info right after parsing.
2354 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002355 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002356 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002357 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002358 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002359
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002360 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002361 if (reql <= 0) { /* closed or EOL not found */
2362 if (reql == 0)
2363 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002364 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002365 continue;
2366 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002367
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002368 /* seek for a possible semi-colon. If we find one, we
2369 * replace it with an LF and skip only this part.
2370 */
2371 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002372 if (trash.str[len] == ';') {
2373 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002374 reql = len + 1;
2375 break;
2376 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002377
Willy Tarreau816fc222009-10-04 07:36:58 +02002378 /* now it is time to check that we have a full line,
2379 * remove the trailing \n and possibly \r, then cut the
2380 * line.
2381 */
2382 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002383 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002384 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002385 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002386 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002387
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002388 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002389 len--;
2390
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002391 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002392
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002393 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002394 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002395 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002396 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002397 continue;
2398 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002399 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002400 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002401 else if (strcmp(trash.str, "help") == 0 ||
2402 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002403 appctx->ctx.cli.msg = stats_sock_usage_msg;
2404 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002405 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002406 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002407 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002408 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002409 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002410 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002411 /* if prompt is disabled, print help on empty lines,
2412 * so that the user at least knows how to enable
2413 * prompt and find help.
2414 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002415 appctx->ctx.cli.msg = stats_sock_usage_msg;
2416 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002417 }
2418
2419 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002420 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002421 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002422 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002423 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002424 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau00a37f02015-04-13 12:05:19 +02002425 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002426 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002427 continue;
2428 }
2429
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002430 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002431 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002432 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002433 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002434 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002435 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002436 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002437 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002438 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002439 free(appctx->ctx.cli.err);
2440 appctx->st0 = STAT_CLI_PROMPT;
2441 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002442 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002443 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002444 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002445 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002446 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002447 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002448 break;
2449 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002450 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002451 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002452 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002453 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002454 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002455 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002456 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002457 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002458 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002459 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002460 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002461 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002462 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002463 if (stats_table_request(si, appctx->st0))
2464 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002465 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002466 case STAT_CLI_O_PATS:
2467 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002468 appctx->st0 = STAT_CLI_PROMPT;
2469 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002470 case STAT_CLI_O_PAT:
2471 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002472 appctx->st0 = STAT_CLI_PROMPT;
2473 break;
2474 case STAT_CLI_O_MLOOK:
2475 if (stats_map_lookup(si))
2476 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002477 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002478 case STAT_CLI_O_POOLS:
2479 if (stats_dump_pools_to_buffer(si))
2480 appctx->st0 = STAT_CLI_PROMPT;
2481 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002482#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2483 case STAT_CLI_O_TLSK:
2484 if (stats_tlskeys_list(si))
2485 appctx->st0 = STAT_CLI_PROMPT;
2486 break;
2487#endif
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002488 default: /* abnormal state */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002489 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002490 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002491 break;
2492 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002493
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002494 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002495 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002496 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002497 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002498 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002499 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002500 }
2501
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002502 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002503 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002504 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002505
2506 /* Now we close the output if one of the writers did so,
2507 * or if we're not in interactive mode and the request
2508 * buffer is empty. This still allows pipelined requests
2509 * to be sent in non-interactive mode.
2510 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002511 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2512 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002513 continue;
2514 }
2515
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002516 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002517 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002518 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002519 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002520
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002521 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002522 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2523 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002524 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002525 * and nothing more to consume. This is comparable to a broken pipe, so
2526 * we forward the close to the request side so that it flows upstream to
2527 * the client.
2528 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002529 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002530 }
2531
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002532 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002533 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2534 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2535 /* We have no more processing to do, and nothing more to send, and
2536 * the client side has closed. So we'll forward this state downstream
2537 * on the response buffer.
2538 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002539 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002540 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002541 }
2542
Willy Tarreau828824a2015-04-19 17:20:03 +02002543 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002544 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 +02002545 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002546 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 +02002547}
2548
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002549/* This function dumps information onto the stream interface's read buffer.
2550 * It returns 0 as long as it does not complete, non-zero upon completion.
2551 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002552 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002553static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002554{
2555 unsigned int up = (now.tv_sec - start_date.tv_sec);
2556
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002557#ifdef USE_OPENSSL
2558 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2559 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2560 int ssl_reuse = 0;
2561
2562 if (ssl_key_rate < ssl_sess_rate) {
2563 /* count the ssl reuse ratio and avoid overflows in both directions */
2564 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2565 }
2566#endif
2567
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002568 chunk_printf(&trash,
2569 "Name: " PRODUCT_NAME "\n"
2570 "Version: " HAPROXY_VERSION "\n"
2571 "Release_date: " HAPROXY_DATE "\n"
2572 "Nbproc: %d\n"
2573 "Process_num: %d\n"
2574 "Pid: %d\n"
2575 "Uptime: %dd %dh%02dm%02ds\n"
2576 "Uptime_sec: %d\n"
2577 "Memmax_MB: %d\n"
2578 "Ulimit-n: %d\n"
2579 "Maxsock: %d\n"
2580 "Maxconn: %d\n"
2581 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002582 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002583 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002584 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002585#ifdef USE_OPENSSL
2586 "MaxSslConns: %d\n"
2587 "CurrSslConns: %d\n"
2588 "CumSslConns: %d\n"
2589#endif
2590 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002591 "PipesUsed: %d\n"
2592 "PipesFree: %d\n"
2593 "ConnRate: %d\n"
2594 "ConnRateLimit: %d\n"
2595 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002596 "SessRate: %d\n"
2597 "SessRateLimit: %d\n"
2598 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002599#ifdef USE_OPENSSL
2600 "SslRate: %d\n"
2601 "SslRateLimit: %d\n"
2602 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002603 "SslFrontendKeyRate: %d\n"
2604 "SslFrontendMaxKeyRate: %d\n"
2605 "SslFrontendSessionReuse_pct: %d\n"
2606 "SslBackendKeyRate: %d\n"
2607 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002608 "SslCacheLookups: %u\n"
2609 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002610#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002611 "CompressBpsIn: %u\n"
2612 "CompressBpsOut: %u\n"
2613 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002614#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002615 "ZlibMemUsage: %ld\n"
2616 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002617#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002618 "Tasks: %d\n"
2619 "Run_queue: %d\n"
2620 "Idle_pct: %d\n"
2621 "node: %s\n"
2622 "description: %s\n"
2623 "",
2624 global.nbproc,
2625 relative_pid,
2626 pid,
2627 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2628 up,
2629 global.rlimit_memmax,
2630 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002631 global.maxsock, global.maxconn, global.hardmaxconn,
2632 actconn, totalconn, global.req_count,
2633#ifdef USE_OPENSSL
2634 global.maxsslconn, sslconns, totalsslconns,
2635#endif
2636 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002637 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002638 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002639#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002640 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2641 ssl_key_rate, global.ssl_fe_keys_max,
2642 ssl_reuse,
2643 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002644 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002645#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002646 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2647 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002648#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002649 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002650#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002651 nb_tasks_cur, run_queue_cur, idle_pct,
2652 global.node, global.desc ? global.desc : ""
2653 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002654
Willy Tarreaubc18da12015-03-13 14:00:47 +01002655 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002656 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002657 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002658 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002659
2660 return 1;
2661}
2662
Willy Tarreau12833bb2014-01-28 16:49:56 +01002663/* This function dumps memory usage information onto the stream interface's
2664 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2665 * completion. No state is used.
2666 */
2667static int stats_dump_pools_to_buffer(struct stream_interface *si)
2668{
2669 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01002670 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002671 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01002672 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002673 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01002674 return 1;
2675}
2676
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002677/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2678 * the state from stream interface <si>. The caller is responsible for clearing
2679 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002680 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002681static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002682{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002683 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002684 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002685
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002686 if (!(px->cap & PR_CAP_FE))
2687 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002688
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002689 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002690 return 0;
2691
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002692 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002693 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002694 /* name, queue */
2695 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002696
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002697 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002698 /* Column sub-heading for Enable or Disable server */
2699 chunk_appendf(&trash, "<td></td>");
2700 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002701
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002702 chunk_appendf(&trash,
2703 "<td class=ac>"
2704 "<a name=\"%s/Frontend\"></a>"
2705 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2706 "<td colspan=3></td>"
2707 "",
2708 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002709
Willy Tarreau466c9b52012-12-23 02:25:03 +01002710 chunk_appendf(&trash,
2711 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002712 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002713 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2714 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2715 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002716 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2717 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2718 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002719
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002720 if (px->mode == PR_MODE_HTTP)
2721 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002722 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002723 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002724
2725 chunk_appendf(&trash,
2726 "</table></div></u></td>"
2727 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002728 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002729 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2730 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2731 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002732 U2H(px->fe_counters.sps_max),
2733 U2H(px->fe_counters.cps_max),
2734 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002735
2736 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002737 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002738 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002739 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002740
2741 chunk_appendf(&trash,
2742 "</table></div></u></td>"
2743 /* sessions rate : limit */
2744 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002745 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002746
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002747 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002748 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002749 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002750 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002751 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2752 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002753 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002754 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2755 U2H(px->fe_counters.cum_sess),
2756 U2H(px->fe_counters.cum_conn),
2757 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002758
Willy Tarreau466c9b52012-12-23 02:25:03 +01002759 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002760 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002761 chunk_appendf(&trash,
2762 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2763 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2764 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2765 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2766 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2767 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2768 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2769 "<tr><th>- other responses:</th><td>%s</td></tr>"
2770 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2771 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002772 U2H(px->fe_counters.p.http.cum_req),
2773 U2H(px->fe_counters.p.http.rsp[1]),
2774 U2H(px->fe_counters.p.http.rsp[2]),
2775 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002776 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002777 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002778 U2H(px->fe_counters.p.http.rsp[3]),
2779 U2H(px->fe_counters.p.http.rsp[4]),
2780 U2H(px->fe_counters.p.http.rsp[5]),
2781 U2H(px->fe_counters.p.http.rsp[0]),
2782 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002783 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002784
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002785 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002786 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002787 /* sessions: lbtot, lastsess */
2788 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002789 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002790 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002791 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002792 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002793
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002794 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002795 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002796 "<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 +01002797 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002798 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002799 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2800 px->fe_counters.comp_in ?
2801 (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 +01002802 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002803
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002804 chunk_appendf(&trash,
2805 /* denied: req, resp */
2806 "<td>%s</td><td>%s</td>"
2807 /* errors : request, connect, response */
2808 "<td>%s</td><td></td><td></td>"
2809 /* warnings: retries, redispatches */
2810 "<td></td><td></td>"
2811 /* server status : reflect frontend status */
2812 "<td class=ac>%s</td>"
2813 /* rest of server: nothing */
2814 "<td class=ac colspan=8></td></tr>"
2815 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002816 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2817 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002818 px->state == PR_STREADY ? "OPEN" :
2819 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002820 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002821 else { /* CSV mode */
2822 chunk_appendf(&trash,
2823 /* pxid, name, queue cur, queue max, */
2824 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002825 /* sessions : current, max, limit, total */
2826 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002827 /* bytes : in, out */
2828 "%lld,%lld,"
2829 /* denied: req, resp */
2830 "%lld,%lld,"
2831 /* errors : request, connect, response */
2832 "%lld,,,"
2833 /* warnings: retries, redispatches */
2834 ",,"
2835 /* server status : reflect frontend status */
2836 "%s,"
2837 /* rest of server: nothing */
2838 ",,,,,,,,"
2839 /* pid, iid, sid, throttle, lbtot, tracked, type */
2840 "%d,%d,0,,,,%d,"
2841 /* rate, rate_lim, rate_max */
2842 "%u,%u,%u,"
2843 /* check_status, check_code, check_duration */
2844 ",,,",
2845 px->id,
2846 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2847 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2848 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2849 px->fe_counters.failed_req,
2850 px->state == PR_STREADY ? "OPEN" :
2851 px->state == PR_STFULL ? "FULL" : "STOP",
2852 relative_pid, px->uuid, STATS_TYPE_FE,
2853 read_freq_ctr(&px->fe_sess_per_sec),
2854 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002855
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002856 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2857 if (px->mode == PR_MODE_HTTP) {
2858 for (i=1; i<6; i++)
2859 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2860 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2861 }
2862 else
2863 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002864
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002865 /* failed health analyses */
2866 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002867
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002868 /* requests : req_rate, req_rate_max, req_tot, */
2869 chunk_appendf(&trash, "%u,%u,%lld,",
2870 read_freq_ctr(&px->fe_req_per_sec),
2871 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2872
2873 /* errors: cli_aborts, srv_aborts */
2874 chunk_appendf(&trash, ",,");
2875
2876 /* compression: in, out, bypassed */
2877 chunk_appendf(&trash, "%lld,%lld,%lld,",
2878 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2879
2880 /* compression: comp_rsp */
2881 chunk_appendf(&trash, "%lld,",
2882 px->fe_counters.p.http.comp_rsp);
2883
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002884 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2885 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002886
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002887 /* finish with EOL */
2888 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002889 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002890 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002891}
2892
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002893/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2894 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2895 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2896 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002897 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002898static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002899{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002900 struct appctx *appctx = __objt_appctx(si->end);
2901
2902 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002903 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002904 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002905 /* Column sub-heading for Enable or Disable server */
2906 chunk_appendf(&trash, "<td></td>");
2907 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002908 chunk_appendf(&trash,
2909 /* frontend name, listener name */
2910 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2911 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2912 "",
2913 px->id, l->name,
2914 (flags & ST_SHLGNDS)?"<u>":"",
2915 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002916
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002917 if (flags & ST_SHLGNDS) {
2918 char str[INET6_ADDRSTRLEN];
2919 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002920
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002921 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002922
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002923 port = get_host_port(&l->addr);
2924 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2925 case AF_INET:
2926 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2927 break;
2928 case AF_INET6:
2929 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2930 break;
2931 case AF_UNIX:
2932 chunk_appendf(&trash, "unix, ");
2933 break;
2934 case -1:
2935 chunk_appendf(&trash, "(%s), ", strerror(errno));
2936 break;
2937 }
Willy Tarreau91861262007-10-17 17:06:05 +02002938
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002939 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002940 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002941 }
Willy Tarreau91861262007-10-17 17:06:05 +02002942
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002943 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002944 /* queue */
2945 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002946 /* sessions rate: current, max, limit */
2947 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002948 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002949 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002950 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002951 /* bytes: in, out */
2952 "<td>%s</td><td>%s</td>"
2953 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002954 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002955 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2956 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002957
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002958 chunk_appendf(&trash,
2959 /* denied: req, resp */
2960 "<td>%s</td><td>%s</td>"
2961 /* errors: request, connect, response */
2962 "<td>%s</td><td></td><td></td>"
2963 /* warnings: retries, redispatches */
2964 "<td></td><td></td>"
2965 /* server status: reflect listener status */
2966 "<td class=ac>%s</td>"
2967 /* rest of server: nothing */
2968 "<td class=ac colspan=8></td></tr>"
2969 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002970 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2971 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002972 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2973 }
2974 else { /* CSV mode */
2975 chunk_appendf(&trash,
2976 /* pxid, name, queue cur, queue max, */
2977 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002978 /* sessions: current, max, limit, total */
2979 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002980 /* bytes: in, out */
2981 "%lld,%lld,"
2982 /* denied: req, resp */
2983 "%lld,%lld,"
2984 /* errors: request, connect, response */
2985 "%lld,,,"
2986 /* warnings: retries, redispatches */
2987 ",,"
2988 /* server status: reflect listener status */
2989 "%s,"
2990 /* rest of server: nothing */
2991 ",,,,,,,,"
2992 /* pid, iid, sid, throttle, lbtot, tracked, type */
2993 "%d,%d,%d,,,,%d,"
2994 /* rate, rate_lim, rate_max */
2995 ",,,"
2996 /* check_status, check_code, check_duration */
2997 ",,,"
2998 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2999 ",,,,,,"
3000 /* failed health analyses */
3001 ","
3002 /* requests : req_rate, req_rate_max, req_tot, */
3003 ",,,"
3004 /* errors: cli_aborts, srv_aborts */
3005 ",,"
3006 /* compression: in, out, bypassed, comp_rsp */
3007 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003008 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3009 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003010 "\n",
3011 px->id, l->name,
3012 l->nbconn, l->counters->conn_max,
3013 l->maxconn, l->counters->cum_conn,
3014 l->counters->bytes_in, l->counters->bytes_out,
3015 l->counters->denied_req, l->counters->denied_resp,
3016 l->counters->failed_req,
3017 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
3018 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
3019 }
3020 return 1;
3021}
Willy Tarreau91861262007-10-17 17:06:05 +02003022
Simon Horman4d2eab62015-04-23 14:51:26 +09003023enum srv_stats_state {
3024 SRV_STATS_STATE_DOWN = 0,
3025 SRV_STATS_STATE_DOWN_AGENT,
3026 SRV_STATS_STATE_GOING_UP,
3027 SRV_STATS_STATE_UP_GOING_DOWN,
3028 SRV_STATS_STATE_UP,
3029 SRV_STATS_STATE_NOLB_GOING_DOWN,
3030 SRV_STATS_STATE_NOLB,
3031 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3032 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003033 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003034 SRV_STATS_STATE_NO_CHECK,
3035
3036 SRV_STATS_STATE_COUNT, /* Must be last */
3037};
3038
Simon Horman837bfa72015-04-23 14:51:27 +09003039enum srv_stats_colour {
3040 SRV_STATS_COLOUR_DOWN = 0,
3041 SRV_STATS_COLOUR_GOING_UP,
3042 SRV_STATS_COLOUR_GOING_DOWN,
3043 SRV_STATS_COLOUR_UP,
3044 SRV_STATS_COLOUR_NOLB,
3045 SRV_STATS_COLOUR_DRAINING,
3046 SRV_STATS_COLOUR_NO_CHECK,
3047
3048 SRV_STATS_COLOUR_COUNT, /* Must be last */
3049};
3050
3051static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3052 [SRV_STATS_COLOUR_DOWN] = "down",
3053 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3054 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3055 [SRV_STATS_COLOUR_UP] = "up",
3056 [SRV_STATS_COLOUR_NOLB] = "nolb",
3057 [SRV_STATS_COLOUR_DRAINING] = "draining",
3058 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3059};
3060
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003061/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3062 * from stream interface <si>, stats flags <flags>, and server state <state>.
3063 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003064 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003065 */
Simon Horman4d2eab62015-04-23 14:51:26 +09003066static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09003067 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003068{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003069 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003070 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003071 char str[INET6_ADDRSTRLEN];
3072 struct chunk src;
3073 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02003074
Willy Tarreau32091232014-05-16 13:52:00 +02003075 /* we have "via" which is the tracked server as described in the configuration,
3076 * and "ref" which is the checked server and the end of the chain.
3077 */
3078 via = sv->track ? sv->track : sv;
3079 ref = via;
3080 while (ref->track)
3081 ref = ref->track;
3082
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003083 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003084 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3085 [SRV_STATS_STATE_DOWN] = "DOWN",
3086 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3087 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
3088 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
3089 [SRV_STATS_STATE_UP] = "UP",
3090 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
3091 [SRV_STATS_STATE_NOLB] = "NOLB",
3092 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
3093 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003094 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003095 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003096 };
Willy Tarreau91861262007-10-17 17:06:05 +02003097
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003098 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003099 chunk_appendf(&trash, "<tr class=\"maintain\">");
3100 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003101 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003102 "<tr class=\"%s_%s\">",
3103 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003104
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003105 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003106 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003107 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3108 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003109
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003110 chunk_appendf(&trash,
3111 "<td class=ac><a name=\"%s/%s\"></a>%s"
3112 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3113 "",
3114 px->id, sv->id,
3115 (flags & ST_SHLGNDS) ? "<u>" : "",
3116 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003117
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003118 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003119 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003120
3121 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3122 case AF_INET:
3123 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3124 break;
3125 case AF_INET6:
3126 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3127 break;
3128 case AF_UNIX:
3129 chunk_appendf(&trash, "unix, ");
3130 break;
3131 case -1:
3132 chunk_appendf(&trash, "(%s), ", strerror(errno));
3133 break;
3134 default: /* address family not supported */
3135 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003136 }
Willy Tarreau91861262007-10-17 17:06:05 +02003137
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003138 /* id */
3139 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003140
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003141 /* cookie */
3142 if (sv->cookie) {
3143 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003144
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003145 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3146 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003147
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003148 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003149 }
3150
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003151 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003152 }
Willy Tarreau91861262007-10-17 17:06:05 +02003153
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003154 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003155 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003156 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003157 /* sessions rate : current, max, limit */
3158 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003159 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003160 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003161 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3162 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003163
Willy Tarreau466c9b52012-12-23 02:25:03 +01003164
3165 chunk_appendf(&trash,
3166 /* sessions: current, max, limit, total */
3167 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003168 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003169 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3170 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003171 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3172 U2H(sv->counters.cum_sess),
3173 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003174
Willy Tarreau466c9b52012-12-23 02:25:03 +01003175 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3176 if (px->mode == PR_MODE_HTTP) {
3177 unsigned long long tot;
3178 for (tot = i = 0; i < 6; i++)
3179 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003180
Willy Tarreau466c9b52012-12-23 02:25:03 +01003181 chunk_appendf(&trash,
3182 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3183 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3184 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3185 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3186 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3187 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3188 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3189 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003190 U2H(tot),
3191 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3192 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3193 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3194 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3195 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3196 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 +02003197 }
Willy Tarreau91861262007-10-17 17:06:05 +02003198
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003199 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3200 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)));
3201 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)));
3202 if (px->mode == PR_MODE_HTTP)
3203 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)));
3204 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)));
3205
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003206 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003207 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003208 /* sessions: lbtot, last */
3209 "<td>%s</td><td>%s</td>",
3210 U2H(sv->counters.cum_lbconn),
3211 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003212
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003213 chunk_appendf(&trash,
3214 /* bytes : in, out */
3215 "<td>%s</td><td>%s</td>"
3216 /* denied: req, resp */
3217 "<td></td><td>%s</td>"
3218 /* errors : request, connect */
3219 "<td></td><td>%s</td>"
3220 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003221 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003222 /* warnings: retries, redispatches */
3223 "<td>%lld</td><td>%lld</td>"
3224 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003225 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3226 U2H(sv->counters.failed_secu),
3227 U2H(sv->counters.failed_conns),
3228 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003229 sv->counters.cli_aborts,
3230 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003231 sv->counters.retries, sv->counters.redispatches);
3232
3233 /* status, lest check */
3234 chunk_appendf(&trash, "<td class=ac>");
3235
Willy Tarreau20125212014-05-13 19:44:56 +02003236 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003237 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3238 chunk_appendf(&trash, "MAINT");
3239 }
Simon Horman0766e442014-11-12 15:55:54 +09003240 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003241 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003242 /* DOWN (agent) */
3243 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 +02003244 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003245 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003246 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3247 chunk_appendf(&trash,
3248 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003249 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3250 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003251 }
Willy Tarreau91861262007-10-17 17:06:05 +02003252
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003253 if ((sv->state == SRV_ST_STOPPED) &&
3254 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3255 chunk_appendf(&trash,
3256 "</td><td class=ac><u> %s%s",
3257 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3258 get_check_status_info(sv->agent.status));
3259
3260 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3261 chunk_appendf(&trash, "/%d", sv->agent.code);
3262
3263 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3264 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3265
3266 chunk_appendf(&trash, "<div class=tips>%s",
3267 get_check_status_description(sv->agent.status));
3268 if (*sv->agent.desc) {
3269 chunk_appendf(&trash, ": ");
3270 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3271 chunk_htmlencode(&trash, &src);
3272 }
3273 chunk_appendf(&trash, "</div></u>");
3274 }
3275 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003276 chunk_appendf(&trash,
3277 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003278 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003279 get_check_status_info(sv->check.status));
3280
3281 if (sv->check.status >= HCHK_STATUS_L57DATA)
3282 chunk_appendf(&trash, "/%d", sv->check.code);
3283
3284 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003285 chunk_appendf(&trash, " in %lums", sv->check.duration);
3286
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003287 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003288 get_check_status_description(sv->check.status));
3289 if (*sv->check.desc) {
3290 chunk_appendf(&trash, ": ");
3291 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3292 chunk_htmlencode(&trash, &src);
3293 }
3294 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003295 }
3296 else
3297 chunk_appendf(&trash, "</td><td>");
3298
3299 chunk_appendf(&trash,
3300 /* weight */
3301 "</td><td class=ac>%d</td>"
3302 /* act, bck */
3303 "<td class=ac>%s</td><td class=ac>%s</td>"
3304 "",
3305 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003306 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3307 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003308
3309 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003310 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003311 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003312
3313 if (ref->observe)
3314 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3315
3316 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003317 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003318 "<td>%lld</td><td>%s</td>"
3319 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003320 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003321 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3322 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003323 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3324 /* tracking a server */
3325 chunk_appendf(&trash,
3326 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003327 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003328 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003329 else
3330 chunk_appendf(&trash, "<td colspan=3></td>");
3331
3332 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003333 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003334 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003335 else
3336 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3337 }
3338 else { /* CSV mode */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003339 struct chunk *out = get_trash_chunk();
Simon Horman4d2eab62015-04-23 14:51:26 +09003340 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3341 [SRV_STATS_STATE_DOWN] = "DOWN,",
3342 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3343 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3344 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3345 [SRV_STATS_STATE_UP] = "UP,",
3346 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3347 [SRV_STATS_STATE_NOLB] = "NOLB,",
3348 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3349 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003350 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003351 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003352 };
3353
3354 chunk_appendf(&trash,
3355 /* pxid, name */
3356 "%s,%s,"
3357 /* queue : current, max */
3358 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003359 /* sessions : current, max, limit, total */
3360 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003361 /* bytes : in, out */
3362 "%lld,%lld,"
3363 /* denied: req, resp */
3364 ",%lld,"
3365 /* errors : request, connect, response */
3366 ",%lld,%lld,"
3367 /* warnings: retries, redispatches */
3368 "%lld,%lld,"
3369 "",
3370 px->id, sv->id,
3371 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003372 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 sv->counters.bytes_in, sv->counters.bytes_out,
3374 sv->counters.failed_secu,
3375 sv->counters.failed_conns, sv->counters.failed_resp,
3376 sv->counters.retries, sv->counters.redispatches);
3377
3378 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003379 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003380 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003381 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003382 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003383 else
3384 chunk_appendf(&trash,
3385 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003386 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3387 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003388
3389 chunk_appendf(&trash,
3390 /* weight, active, backup */
3391 "%d,%d,%d,"
3392 "",
3393 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003394 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3395 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003396
3397 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003398 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 chunk_appendf(&trash,
3400 "%lld,%lld,%d,%d,",
3401 sv->counters.failed_checks, sv->counters.down_trans,
3402 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3403 else
3404 chunk_appendf(&trash, ",,,,");
3405
3406 /* queue limit, pid, iid, sid, */
3407 chunk_appendf(&trash,
3408 "%s,"
3409 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003410 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003411 relative_pid, px->uuid, sv->puid);
3412
3413 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003414 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003415 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003416
3417 /* sessions: lbtot */
3418 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3419
3420 /* tracked */
3421 if (sv->track)
3422 chunk_appendf(&trash, "%s/%s,",
3423 sv->track->proxy->id, sv->track->id);
3424 else
3425 chunk_appendf(&trash, ",");
3426
3427 /* type */
3428 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3429
3430 /* rate */
3431 chunk_appendf(&trash, "%u,,%u,",
3432 read_freq_ctr(&sv->sess_per_sec),
3433 sv->counters.sps_max);
3434
Willy Tarreauff5ae352013-12-11 20:36:34 +01003435 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003436 /* check_status */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003437 chunk_appendf(&trash, "%s,", csv_enc(get_check_status_info(sv->check.status), 1, out));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438
3439 /* check_code */
3440 if (sv->check.status >= HCHK_STATUS_L57DATA)
3441 chunk_appendf(&trash, "%u,", sv->check.code);
3442 else
3443 chunk_appendf(&trash, ",");
3444
3445 /* check_duration */
3446 if (sv->check.status >= HCHK_STATUS_CHECKED)
3447 chunk_appendf(&trash, "%lu,", sv->check.duration);
3448 else
3449 chunk_appendf(&trash, ",");
3450
3451 }
3452 else
3453 chunk_appendf(&trash, ",,,");
3454
3455 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3456 if (px->mode == PR_MODE_HTTP) {
3457 for (i=1; i<6; i++)
3458 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3459
3460 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3461 }
3462 else
3463 chunk_appendf(&trash, ",,,,,,");
3464
3465 /* failed health analyses */
3466 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3467
3468 /* requests : req_rate, req_rate_max, req_tot, */
3469 chunk_appendf(&trash, ",,,");
3470
3471 /* errors: cli_aborts, srv_aborts */
3472 chunk_appendf(&trash, "%lld,%lld,",
3473 sv->counters.cli_aborts, sv->counters.srv_aborts);
3474
3475 /* compression: in, out, bypassed, comp_rsp */
3476 chunk_appendf(&trash, ",,,,");
3477
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003478 /* lastsess */
3479 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3480
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003481 /* capture of last check and agent statuses */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003482 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->check.desc), 1, out) : "");
3483 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 +02003484
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003485 /* qtime, ctime, rtime, ttime, */
3486 chunk_appendf(&trash, "%u,%u,%u,%u,",
3487 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3488 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3489 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3490 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3491
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003492 /* finish with EOL */
3493 chunk_appendf(&trash, "\n");
3494 }
3495 return 1;
3496}
3497
3498/* Dumps a line for backend <px> to the trash for and uses the state from stream
3499 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3500 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3501 */
3502static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3503{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003504 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003505 struct chunk src;
3506 int i;
3507
3508 if (!(px->cap & PR_CAP_BE))
3509 return 0;
3510
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003511 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003512 return 0;
3513
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003514 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003515 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003516 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003517 /* Column sub-heading for Enable or Disable server */
3518 chunk_appendf(&trash, "<td></td>");
3519 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003520 chunk_appendf(&trash,
3521 "<td class=ac>"
3522 /* name */
3523 "%s<a name=\"%s/Backend\"></a>"
3524 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3525 "",
3526 (flags & ST_SHLGNDS)?"<u>":"",
3527 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003528
3529 if (flags & ST_SHLGNDS) {
3530 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003531 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003532 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3533
3534 /* cookie */
3535 if (px->cookie_name) {
3536 chunk_appendf(&trash, ", cookie: '");
3537 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3538 chunk_htmlencode(&trash, &src);
3539 chunk_appendf(&trash, "'");
3540 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003541 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003542 }
3543
3544 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003545 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003546 /* queue : current, max */
3547 "<td>%s</td><td>%s</td><td></td>"
3548 /* sessions rate : current, max, limit */
3549 "<td>%s</td><td>%s</td><td></td>"
3550 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003551 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003552 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3553 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003554
3555 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003556 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003557 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003558 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003559 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003560 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003561 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3562 U2H(px->be_counters.cum_conn),
3563 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003564
Willy Tarreau466c9b52012-12-23 02:25:03 +01003565 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003566 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003567 chunk_appendf(&trash,
3568 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3569 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3570 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3571 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3572 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3573 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3574 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3575 "<tr><th>- other responses:</th><td>%s</td></tr>"
3576 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003577 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003578 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003579 U2H(px->be_counters.p.http.cum_req),
3580 U2H(px->be_counters.p.http.rsp[1]),
3581 U2H(px->be_counters.p.http.rsp[2]),
3582 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003583 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003584 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003585 U2H(px->be_counters.p.http.rsp[3]),
3586 U2H(px->be_counters.p.http.rsp[4]),
3587 U2H(px->be_counters.p.http.rsp[5]),
3588 U2H(px->be_counters.p.http.rsp[0]),
3589 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003590 }
3591
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003592 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)));
3593 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)));
3594 if (px->mode == PR_MODE_HTTP)
3595 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)));
3596 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)));
3597
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003598 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003599 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003600 /* sessions: lbtot, last */
3601 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003602 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003603 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003604 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003605 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003606 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003607 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003608
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003609 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003610 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003611 "<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 +01003612 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003613 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003614 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3615 px->be_counters.comp_in ?
3616 (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 +01003617 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3618
3619 chunk_appendf(&trash,
3620 /* denied: req, resp */
3621 "<td>%s</td><td>%s</td>"
3622 /* errors : request, connect */
3623 "<td></td><td>%s</td>"
3624 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003625 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003626 /* warnings: retries, redispatches */
3627 "<td>%lld</td><td>%lld</td>"
3628 /* backend status: reflect backend status (up/down): we display UP
3629 * if the backend has known working servers or if it has no server at
3630 * all (eg: for stats). Then we display the total weight, number of
3631 * active and backups. */
3632 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3633 "<td class=ac>%d</td><td class=ac>%d</td>"
3634 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003635 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3636 U2H(px->be_counters.failed_conns),
3637 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003638 px->be_counters.cli_aborts,
3639 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003640 px->be_counters.retries, px->be_counters.redispatches,
3641 human_time(now.tv_sec - px->last_change, 1),
3642 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3643 "<font color=\"red\"><b>DOWN</b></font>",
3644 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3645 px->srv_act, px->srv_bck);
3646
3647 chunk_appendf(&trash,
3648 /* rest of backend: nothing, down transitions, total downtime, throttle */
3649 "<td class=ac>&nbsp;</td><td>%d</td>"
3650 "<td>%s</td>"
3651 "<td></td>"
3652 "</tr>",
3653 px->down_trans,
3654 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3655 }
3656 else { /* CSV mode */
3657 chunk_appendf(&trash,
3658 /* pxid, name */
3659 "%s,BACKEND,"
3660 /* queue : current, max */
3661 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003662 /* sessions : current, max, limit, total */
3663 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003664 /* bytes : in, out */
3665 "%lld,%lld,"
3666 /* denied: req, resp */
3667 "%lld,%lld,"
3668 /* errors : request, connect, response */
3669 ",%lld,%lld,"
3670 /* warnings: retries, redispatches */
3671 "%lld,%lld,"
3672 /* backend status: reflect backend status (up/down): we display UP
3673 * if the backend has known working servers or if it has no server at
3674 * all (eg: for stats). Then we display the total weight, number of
3675 * active and backups. */
3676 "%s,"
3677 "%d,%d,%d,"
3678 /* rest of backend: nothing, down transitions, last change, total downtime */
3679 ",%d,%d,%d,,"
3680 /* pid, iid, sid, throttle, lbtot, tracked, type */
3681 "%d,%d,0,,%lld,,%d,"
3682 /* rate, rate_lim, rate_max, */
3683 "%u,,%u,"
3684 /* check_status, check_code, check_duration */
3685 ",,,",
3686 px->id,
3687 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3688 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3689 px->be_counters.bytes_in, px->be_counters.bytes_out,
3690 px->be_counters.denied_req, px->be_counters.denied_resp,
3691 px->be_counters.failed_conns, px->be_counters.failed_resp,
3692 px->be_counters.retries, px->be_counters.redispatches,
3693 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3694 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3695 px->srv_act, px->srv_bck,
3696 px->down_trans, (int)(now.tv_sec - px->last_change),
3697 px->srv?be_downtime(px):0,
3698 relative_pid, px->uuid,
3699 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3700 read_freq_ctr(&px->be_sess_per_sec),
3701 px->be_counters.sps_max);
3702
3703 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3704 if (px->mode == PR_MODE_HTTP) {
3705 for (i=1; i<6; i++)
3706 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3707 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3708 }
3709 else
3710 chunk_appendf(&trash, ",,,,,,");
3711
3712 /* failed health analyses */
3713 chunk_appendf(&trash, ",");
3714
3715 /* requests : req_rate, req_rate_max, req_tot, */
3716 chunk_appendf(&trash, ",,,");
3717
3718 /* errors: cli_aborts, srv_aborts */
3719 chunk_appendf(&trash, "%lld,%lld,",
3720 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3721
3722 /* compression: in, out, bypassed */
3723 chunk_appendf(&trash, "%lld,%lld,%lld,",
3724 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3725
3726 /* compression: comp_rsp */
3727 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3728
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003729 /* lastsess, last_chk, last_agt, */
3730 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003731
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003732 /* qtime, ctime, rtime, ttime, */
3733 chunk_appendf(&trash, "%u,%u,%u,%u,",
3734 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3735 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3736 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3737 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3738
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003739 /* finish with EOL */
3740 chunk_appendf(&trash, "\n");
3741 }
3742 return 1;
3743}
3744
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003745/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003746 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003747 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003748 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003749static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003750{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003751 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003752 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3753
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003754 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003755 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003756
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003757 /* 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 +02003758 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003759 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003760 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003761 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 +01003762 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003763 }
3764
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003765 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05003766 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003767 }
3768
3769 /* print a new table */
3770 chunk_appendf(&trash,
3771 "<table class=\"tbl\" width=\"100%%\">\n"
3772 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003773 "<th class=\"pxname\" width=\"10%%\">");
3774
3775 chunk_appendf(&trash,
3776 "<a name=\"%s\"></a>%s"
3777 "<a class=px href=\"#%s\">%s</a>",
3778 px->id,
3779 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3780 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003781
3782 if (uri->flags & ST_SHLGNDS) {
3783 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003784 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003785 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3786 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003787 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003788 }
3789
3790 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003791 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003792 "<th class=\"%s\" width=\"90%%\">%s</th>"
3793 "</tr>\n"
3794 "</table>\n"
3795 "<table class=\"tbl\" width=\"100%%\">\n"
3796 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003797 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3798 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3799
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003800 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003801 /* Column heading for Enable or Disable server */
3802 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003803 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003804
3805 chunk_appendf(&trash,
3806 "<th rowspan=2></th>"
3807 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003808 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003809 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3810 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3811 "<th colspan=9>Server</th>"
3812 "</tr>\n"
3813 "<tr class=\"titre\">"
3814 "<th>Cur</th><th>Max</th><th>Limit</th>"
3815 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003816 "<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 +01003817 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3818 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3819 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3820 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3821 "<th>Thrtle</th>\n"
3822 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003823}
3824
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003825/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003826 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003827 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003828static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003829{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003830 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003831 chunk_appendf(&trash, "</table>");
3832
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003833 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003834 /* close the form used to enable/disable this proxy servers */
3835 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003836 "Choose the action to perform on the checked servers : "
3837 "<select name=action>"
3838 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003839 "<option value=\"ready\">Set state to READY</option>"
3840 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02003841 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003842 "<option value=\"dhlth\">Health: disable checks</option>"
3843 "<option value=\"ehlth\">Health: enable checks</option>"
3844 "<option value=\"hrunn\">Health: force UP</option>"
3845 "<option value=\"hnolb\">Health: force NOLB</option>"
3846 "<option value=\"hdown\">Health: force DOWN</option>"
3847 "<option value=\"dagent\">Agent: disable checks</option>"
3848 "<option value=\"eagent\">Agent: enable checks</option>"
3849 "<option value=\"arunn\">Agent: force UP</option>"
3850 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003851 "<option value=\"shutdown\">Kill Sessions</option>"
3852 "</select>"
3853 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3854 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3855 "</form>",
3856 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003857 }
3858
3859 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003860}
Willy Tarreau91861262007-10-17 17:06:05 +02003861
3862/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003863 * Dumps statistics for a proxy. The output is sent to the stream interface's
3864 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3865 * buffer space, or non-zero if everything completed. This function is used
3866 * both by the CLI and the HTTP entry points, and is able to dump the output
3867 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003868 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003869static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003870{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003871 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02003872 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003873 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02003874 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003875 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003876
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003877 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003878
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003879 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003880 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003881 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003882 if (uri && uri->scope) {
3883 /* we have a limited scope, we have to check the proxy name */
3884 struct stat_scope *scope;
3885 int len;
3886
3887 len = strlen(px->id);
3888 scope = uri->scope;
3889
3890 while (scope) {
3891 /* match exact proxy name */
3892 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3893 break;
3894
3895 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003896 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003897 break;
3898 scope = scope->next;
3899 }
3900
3901 /* proxy name not found : don't dump anything */
3902 if (scope == NULL)
3903 return 1;
3904 }
3905
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003906 /* if the user has requested a limited output and the proxy
3907 * name does not match, skip it.
3908 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003909 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003910 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 +02003911 return 1;
3912
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003913 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3914 (appctx->ctx.stats.iid != -1) &&
3915 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003916 return 1;
3917
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003918 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003919 /* fall through */
3920
Willy Tarreau295a8372011-03-10 11:25:07 +01003921 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003922 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003923 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003924 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003925 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02003926 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003927 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003928 }
Willy Tarreau91861262007-10-17 17:06:05 +02003929
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003930 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003931 /* fall through */
3932
Willy Tarreau295a8372011-03-10 11:25:07 +01003933 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003934 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003935 if (stats_dump_fe_stats(si, px)) {
3936 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003937 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003938 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003939 }
3940 }
Willy Tarreau91861262007-10-17 17:06:05 +02003941
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003942 appctx->ctx.stats.l = px->conf.listeners.n;
3943 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003944 /* fall through */
3945
Willy Tarreau295a8372011-03-10 11:25:07 +01003946 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003947 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003948 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003949 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003950 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02003951 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003952 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003953
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003954 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003955 if (!l->counters)
3956 continue;
3957
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003958 if (appctx->ctx.stats.flags & STAT_BOUND) {
3959 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003960 break;
3961
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003962 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003963 continue;
3964 }
3965
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003966 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003967 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
3968 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003969 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003970 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003971 }
3972 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003973 }
3974
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003975 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3976 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003977 /* fall through */
3978
Willy Tarreau295a8372011-03-10 11:25:07 +01003979 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003980 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003981 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003982 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09003983 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02003984
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003985 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003986 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02003987 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003988 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003989
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003990 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003991
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003992 if (appctx->ctx.stats.flags & STAT_BOUND) {
3993 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003994 break;
3995
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003996 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003997 continue;
3998 }
3999
Willy Tarreau32091232014-05-16 13:52:00 +02004000 svs = sv;
4001 while (svs->track)
4002 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004003
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004004 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004005 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09004006 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004007 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004008 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4009 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004010 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004011 sv_colour = SRV_STATS_COLOUR_UP;
4012 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004013
Simon Horman4463d192015-04-23 14:51:28 +09004014 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
4015 sv_colour = SRV_STATS_COLOUR_DRAINING;
4016
4017 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09004018 if (svs->agent.state & CHK_ST_ENABLED)
4019 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
4020 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09004021 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09004022 else
Simon Horman4d2eab62015-04-23 14:51:26 +09004023 sv_state = SRV_STATS_STATE_DRAIN;
4024 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004025
Simon Horman837bfa72015-04-23 14:51:27 +09004026 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004027 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09004028 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
4029 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004030 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004031 else if (sv->state == SRV_ST_STOPPING) {
4032 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09004033 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004034 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09004035 sv_colour = SRV_STATS_COLOUR_NOLB;
4036 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004037 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004038 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4039 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004040 }
4041 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09004042 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004043 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09004044 sv_colour = SRV_STATS_COLOUR_DOWN;
4045 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004046 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09004047 sv_colour = SRV_STATS_COLOUR_DOWN;
4048 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004049 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004050 sv_colour = SRV_STATS_COLOUR_GOING_UP;
4051 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004052 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09004053 sv_colour = SRV_STATS_COLOUR_DOWN;
4054 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004055 }
Willy Tarreau91861262007-10-17 17:06:05 +02004056
Willy Tarreau9638efa2014-05-23 11:19:57 +02004057 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02004058 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004059 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02004060 continue;
4061 }
4062
Simon Horman837bfa72015-04-23 14:51:27 +09004063 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004064 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004065 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004066 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004067 }
4068 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004069 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004070
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004071 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004072 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004073
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004074 case STAT_PX_ST_BE:
4075 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004076 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
4077 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004078 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004079 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004080 }
4081 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004082
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004083 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004084 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004085
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004086 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004087 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004088 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004089 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004090 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004091 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004092 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004093 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004094
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004095 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004096 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004097
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004098 case STAT_PX_ST_FIN:
4099 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004100
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004101 default:
4102 /* unknown state, we should put an abort() here ! */
4103 return 1;
4104 }
4105}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004106
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004107/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4108 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004109 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004110static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004111{
4112 /* WARNING! This must fit in the first buffer !!! */
4113 chunk_appendf(&trash,
4114 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4115 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4116 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4117 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4118 "<style type=\"text/css\"><!--\n"
4119 "body {"
4120 " font-family: arial, helvetica, sans-serif;"
4121 " font-size: 12px;"
4122 " font-weight: normal;"
4123 " color: black;"
4124 " background: white;"
4125 "}\n"
4126 "th,td {"
4127 " font-size: 10px;"
4128 "}\n"
4129 "h1 {"
4130 " font-size: x-large;"
4131 " margin-bottom: 0.5em;"
4132 "}\n"
4133 "h2 {"
4134 " font-family: helvetica, arial;"
4135 " font-size: x-large;"
4136 " font-weight: bold;"
4137 " font-style: italic;"
4138 " color: #6020a0;"
4139 " margin-top: 0em;"
4140 " margin-bottom: 0em;"
4141 "}\n"
4142 "h3 {"
4143 " font-family: helvetica, arial;"
4144 " font-size: 16px;"
4145 " font-weight: bold;"
4146 " color: #b00040;"
4147 " background: #e8e8d0;"
4148 " margin-top: 0em;"
4149 " margin-bottom: 0em;"
4150 "}\n"
4151 "li {"
4152 " margin-top: 0.25em;"
4153 " margin-right: 2em;"
4154 "}\n"
4155 ".hr {margin-top: 0.25em;"
4156 " border-color: black;"
4157 " border-bottom-style: solid;"
4158 "}\n"
4159 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4160 ".total {background: #20D0D0;color: #ffff80;}\n"
4161 ".frontend {background: #e8e8d0;}\n"
4162 ".socket {background: #d0d0d0;}\n"
4163 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004164 ".active_down {background: #ff9090;}\n"
4165 ".active_going_up {background: #ffd020;}\n"
4166 ".active_going_down {background: #ffffa0;}\n"
4167 ".active_up {background: #c0ffc0;}\n"
4168 ".active_nolb {background: #20a0ff;}\n"
4169 ".active_draining {background: #20a0FF;}\n"
4170 ".active_no_check {background: #e0e0e0;}\n"
4171 ".backup_down {background: #ff9090;}\n"
4172 ".backup_going_up {background: #ff80ff;}\n"
4173 ".backup_going_down {background: #c060ff;}\n"
4174 ".backup_up {background: #b0d0ff;}\n"
4175 ".backup_nolb {background: #90b0e0;}\n"
4176 ".backup_draining {background: #cc9900;}\n"
4177 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004178 ".maintain {background: #c07820;}\n"
4179 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4180 "\n"
4181 "a.px:link {color: #ffff40; text-decoration: none;}"
4182 "a.px:visited {color: #ffff40; text-decoration: none;}"
4183 "a.px:hover {color: #ffffff; text-decoration: none;}"
4184 "a.lfsb:link {color: #000000; text-decoration: none;}"
4185 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4186 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4187 "\n"
4188 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4189 "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"
4190 "table.tbl td.ac { text-align: center;}\n"
4191 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4192 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4193 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4194 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4195 "\n"
4196 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4197 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4198 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004199 "table.det { border-collapse: collapse; border-style: none; }\n"
4200 "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 +01004201 "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 +01004202 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004203 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004204 " display:block;\n"
4205 " visibility:hidden;\n"
4206 " z-index:2147483647;\n"
4207 " position:absolute;\n"
4208 " padding:2px 4px 3px;\n"
4209 " background:#f0f060; color:#000000;\n"
4210 " border:1px solid #7040c0;\n"
4211 " white-space:nowrap;\n"
4212 " font-style:normal;font-size:11px;font-weight:normal;\n"
4213 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4214 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4215 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004216 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004217 "-->\n"
4218 "</style></head>\n",
4219 (uri->flags & ST_SHNODE) ? " on " : "",
4220 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4221 );
4222}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004223
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004224/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004225 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004226 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004227 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004228static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004229{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004230 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004231 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004232 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004233
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004234 /* WARNING! this has to fit the first packet too.
4235 * We are around 3.5 kB, add adding entries will
4236 * become tricky if we want to support 4kB buffers !
4237 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004238 chunk_appendf(&trash,
4239 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4240 PRODUCT_NAME "%s</a></h1>\n"
4241 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4242 "<hr width=\"100%%\" class=\"hr\">\n"
4243 "<h3>&gt; General process information</h3>\n"
4244 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4245 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4246 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4247 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4248 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4249 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4250 "Running tasks: %d/%d; idle = %d %%<br>\n"
4251 "</td><td align=\"center\" nowrap>\n"
4252 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004253 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4254 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004255 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004256 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4257 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004258 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004259 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4260 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004261 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004262 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4263 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004264 "</tr><tr>\n"
4265 "<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 -07004266 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004267 "<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 +01004268 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004269 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004270 "</td>"
4271 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4272 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4273 "",
4274 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4275 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4276 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4277 (uri->flags & ST_SHDESC) ? ": " : "",
4278 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4279 pid, relative_pid, global.nbproc,
4280 up / 86400, (up % 86400) / 3600,
4281 (up % 3600) / 60, (up % 60),
4282 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4283 global.rlimit_memmax ? " MB" : "",
4284 global.rlimit_nofile,
4285 global.maxsock, global.maxconn, global.maxpipes,
4286 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4287 run_queue_cur, nb_tasks_cur, idle_pct
4288 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004289
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004290 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004291 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 +01004292 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004293
4294 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004295 "<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 +01004296 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004297 STAT_SCOPE_TXT_MAXLEN);
4298
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004299 /* 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 +02004300 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004301 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004302 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004303 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 +01004304 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004305 }
4306
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004307 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004308 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004309 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004310 uri->uri_prefix,
4311 "",
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 Tarreaud9bdcd52012-12-22 20:31:10 +01004314 else
4315 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004316 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004317 uri->uri_prefix,
4318 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004319 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004320 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004321
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004322 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004323 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004324 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004325 "<li><a href=\"%s%s%s%s\">Enable 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 "",
4329 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004330 else
4331 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004332 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004333 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004334 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004335 ";norefresh",
4336 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004337 }
Willy Tarreau55bb8452007-10-17 18:44:57 +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%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004341 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004342 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4343 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004344 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004345
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004346 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004347 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004348 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004349 (uri->refresh > 0) ? ";norefresh" : "",
4350 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004351
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004352 chunk_appendf(&trash,
4353 "</ul></td>"
4354 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4355 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4356 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4357 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4358 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4359 "</ul>"
4360 "</td>"
4361 "</tr></table>\n"
4362 ""
4363 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004364
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004365 if (appctx->ctx.stats.st_code) {
4366 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004367 case STAT_STATUS_DONE:
4368 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004369 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004370 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004371 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004372 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004373 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4374 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004375 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004376 break;
4377 case STAT_STATUS_NONE:
4378 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004379 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004380 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004381 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004382 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004383 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4384 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004385 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004386 break;
4387 case STAT_STATUS_PART:
4388 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004389 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004390 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004391 "Action partially processed.<br>"
4392 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004393 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004394 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4395 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004396 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004397 break;
4398 case STAT_STATUS_ERRP:
4399 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004400 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004401 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004402 "Action not processed because of invalid parameters."
4403 "<ul>"
4404 "<li>The action is maybe unknown.</li>"
4405 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4406 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4407 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004408 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004409 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4410 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004411 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004412 break;
4413 case STAT_STATUS_EXCD:
4414 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004415 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004416 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004417 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4418 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004419 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004420 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4421 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004422 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004423 break;
4424 case STAT_STATUS_DENY:
4425 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004426 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004427 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004428 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004429 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004430 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4431 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004432 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004433 break;
4434 default:
4435 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004436 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004437 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004438 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004439 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004440 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4441 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004442 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004443 }
4444 chunk_appendf(&trash, "<p>\n");
4445 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004446}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004447
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004448/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4449 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004450 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004451static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004452{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004453 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004454}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004455
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004456/* This function dumps statistics onto the stream interface's read buffer in
4457 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004458 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4459 * 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 +02004460 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004461 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004462 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004463static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004464{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004465 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004466 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004467 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004468
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004469 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004470
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004471 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004472 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004473 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004474 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004475
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004476 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004477 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004478 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004479 else
4480 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004481
Willy Tarreaubc18da12015-03-13 14:00:47 +01004482 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004483 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004484 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004485 }
Willy Tarreauae526782010-03-04 20:34:23 +01004486
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004487 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004488 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004489
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004490 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004491 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004492 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004493 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004494 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004495 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004496 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004497 }
Willy Tarreau91861262007-10-17 17:06:05 +02004498
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004499 appctx->ctx.stats.px = proxy;
4500 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4501 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004502 /* fall through */
4503
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004504 case STAT_ST_LIST:
4505 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004506 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004507 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004508 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004509 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004510 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004511
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004512 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004513 /* skip the disabled proxies, global frontend and non-networked ones */
4514 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004515 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004516 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004517
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004518 appctx->ctx.stats.px = px->next;
4519 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004520 }
4521 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004522
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004523 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004524 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004525
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004526 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004527 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004528 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01004529 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004530 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004531 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004532 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004533 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004534
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004535 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004536 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004537
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004538 case STAT_ST_FIN:
4539 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004540
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004541 default:
4542 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004543 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004544 return -1;
4545 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004546}
Willy Tarreauae526782010-03-04 20:34:23 +01004547
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004548/* We reached the stats page through a POST request. The appctx is
4549 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004550 * Parse the posted data and enable/disable servers if necessary.
4551 * Returns 1 if request was parsed or zero if it needs more data.
4552 */
4553static int stats_process_http_post(struct stream_interface *si)
4554{
Willy Tarreau87b09662015-04-03 00:22:06 +02004555 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004556 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004557
4558 struct proxy *px = NULL;
4559 struct server *sv = NULL;
4560
4561 char key[LINESIZE];
4562 int action = ST_ADM_ACTION_NONE;
4563 int reprocess = 0;
4564
4565 int total_servers = 0;
4566 int altered_servers = 0;
4567
4568 char *first_param, *cur_param, *next_param, *end_params;
4569 char *st_cur_param = NULL;
4570 char *st_next_param = NULL;
4571
4572 struct chunk *temp;
4573 int reql;
4574
4575 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02004576 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004577 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004578 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004579 goto out;
4580 }
4581
Willy Tarreaueee5b512015-04-03 23:46:31 +02004582 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 +01004583 if (reql <= 0) {
4584 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004585 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004586 return 0;
4587 }
4588
4589 first_param = temp->str;
4590 end_params = temp->str + reql;
4591 cur_param = next_param = end_params;
4592 *end_params = '\0';
4593
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004594 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004595
4596 /*
4597 * Parse the parameters in reverse order to only store the last value.
4598 * From the html form, the backend and the action are at the end.
4599 */
4600 while (cur_param > first_param) {
4601 char *value;
4602 int poffset, plen;
4603
4604 cur_param--;
4605
4606 if ((*cur_param == '&') || (cur_param == first_param)) {
4607 reprocess_servers:
4608 /* Parse the key */
4609 poffset = (cur_param != first_param ? 1 : 0);
4610 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4611 if ((plen > 0) && (plen <= sizeof(key))) {
4612 strncpy(key, cur_param + poffset, plen);
4613 key[plen - 1] = '\0';
4614 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004615 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004616 goto out;
4617 }
4618
4619 /* Parse the value */
4620 value = key;
4621 while (*value != '\0' && *value != '=') {
4622 value++;
4623 }
4624 if (*value == '=') {
4625 /* Ok, a value is found, we can mark the end of the key */
4626 *value++ = '\0';
4627 }
4628 if (url_decode(key) < 0 || url_decode(value) < 0)
4629 break;
4630
4631 /* Now we can check the key to see what to do */
4632 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02004633 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004634 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004635 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004636 goto out;
4637 }
4638 }
4639 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004640 if (strcmp(value, "ready") == 0) {
4641 action = ST_ADM_ACTION_READY;
4642 }
4643 else if (strcmp(value, "drain") == 0) {
4644 action = ST_ADM_ACTION_DRAIN;
4645 }
4646 else if (strcmp(value, "maint") == 0) {
4647 action = ST_ADM_ACTION_MAINT;
4648 }
4649 else if (strcmp(value, "shutdown") == 0) {
4650 action = ST_ADM_ACTION_SHUTDOWN;
4651 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004652 else if (strcmp(value, "dhlth") == 0) {
4653 action = ST_ADM_ACTION_DHLTH;
4654 }
4655 else if (strcmp(value, "ehlth") == 0) {
4656 action = ST_ADM_ACTION_EHLTH;
4657 }
4658 else if (strcmp(value, "hrunn") == 0) {
4659 action = ST_ADM_ACTION_HRUNN;
4660 }
4661 else if (strcmp(value, "hnolb") == 0) {
4662 action = ST_ADM_ACTION_HNOLB;
4663 }
4664 else if (strcmp(value, "hdown") == 0) {
4665 action = ST_ADM_ACTION_HDOWN;
4666 }
4667 else if (strcmp(value, "dagent") == 0) {
4668 action = ST_ADM_ACTION_DAGENT;
4669 }
4670 else if (strcmp(value, "eagent") == 0) {
4671 action = ST_ADM_ACTION_EAGENT;
4672 }
4673 else if (strcmp(value, "arunn") == 0) {
4674 action = ST_ADM_ACTION_ARUNN;
4675 }
4676 else if (strcmp(value, "adown") == 0) {
4677 action = ST_ADM_ACTION_ADOWN;
4678 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004679 /* else these are the old supported methods */
4680 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004681 action = ST_ADM_ACTION_DISABLE;
4682 }
4683 else if (strcmp(value, "enable") == 0) {
4684 action = ST_ADM_ACTION_ENABLE;
4685 }
4686 else if (strcmp(value, "stop") == 0) {
4687 action = ST_ADM_ACTION_STOP;
4688 }
4689 else if (strcmp(value, "start") == 0) {
4690 action = ST_ADM_ACTION_START;
4691 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004692 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004693 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004694 goto out;
4695 }
4696 }
4697 else if (strcmp(key, "s") == 0) {
4698 if (!(px && action)) {
4699 /*
4700 * Indicates that we'll need to reprocess the parameters
4701 * as soon as backend and action are known
4702 */
4703 if (!reprocess) {
4704 st_cur_param = cur_param;
4705 st_next_param = next_param;
4706 }
4707 reprocess = 1;
4708 }
4709 else if ((sv = findserver(px, value)) != NULL) {
4710 switch (action) {
4711 case ST_ADM_ACTION_DISABLE:
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_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004716 }
4717 break;
4718 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004719 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004720 altered_servers++;
4721 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004722 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004723 }
4724 break;
4725 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004726 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4727 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4728 altered_servers++;
4729 total_servers++;
4730 }
4731 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004732 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004733 if (sv->admin & SRV_ADMF_FDRAIN) {
4734 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4735 altered_servers++;
4736 total_servers++;
4737 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004738 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004739 case ST_ADM_ACTION_DHLTH:
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_EHLTH:
4747 if (sv->check.state & CHK_ST_CONFIGURED) {
4748 sv->check.state |= CHK_ST_ENABLED;
4749 altered_servers++;
4750 total_servers++;
4751 }
4752 break;
4753 case ST_ADM_ACTION_HRUNN:
4754 if (!(sv->track)) {
4755 sv->check.health = sv->check.rise + sv->check.fall - 1;
4756 srv_set_running(sv, "changed from Web interface");
4757 altered_servers++;
4758 total_servers++;
4759 }
4760 break;
4761 case ST_ADM_ACTION_HNOLB:
4762 if (!(sv->track)) {
4763 sv->check.health = sv->check.rise + sv->check.fall - 1;
4764 srv_set_stopping(sv, "changed from Web interface");
4765 altered_servers++;
4766 total_servers++;
4767 }
4768 break;
4769 case ST_ADM_ACTION_HDOWN:
4770 if (!(sv->track)) {
4771 sv->check.health = 0;
4772 srv_set_stopped(sv, "changed from Web interface");
4773 altered_servers++;
4774 total_servers++;
4775 }
4776 break;
4777 case ST_ADM_ACTION_DAGENT:
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_EAGENT:
4785 if (sv->agent.state & CHK_ST_CONFIGURED) {
4786 sv->agent.state |= CHK_ST_ENABLED;
4787 altered_servers++;
4788 total_servers++;
4789 }
4790 break;
4791 case ST_ADM_ACTION_ARUNN:
4792 if (sv->agent.state & CHK_ST_ENABLED) {
4793 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4794 srv_set_running(sv, "changed from Web interface");
4795 altered_servers++;
4796 total_servers++;
4797 }
4798 break;
4799 case ST_ADM_ACTION_ADOWN:
4800 if (sv->agent.state & CHK_ST_ENABLED) {
4801 sv->agent.health = 0;
4802 srv_set_stopped(sv, "changed from Web interface");
4803 altered_servers++;
4804 total_servers++;
4805 }
4806 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004807 case ST_ADM_ACTION_READY:
4808 srv_adm_set_ready(sv);
4809 altered_servers++;
4810 total_servers++;
4811 break;
4812 case ST_ADM_ACTION_DRAIN:
4813 srv_adm_set_drain(sv);
4814 altered_servers++;
4815 total_servers++;
4816 break;
4817 case ST_ADM_ACTION_MAINT:
4818 srv_adm_set_maint(sv);
4819 altered_servers++;
4820 total_servers++;
4821 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004822 case ST_ADM_ACTION_SHUTDOWN:
4823 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004824 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004825
4826 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4827 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004828 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004829
4830 altered_servers++;
4831 total_servers++;
4832 }
4833 break;
4834 }
4835 } else {
4836 /* the server name is unknown or ambiguous (duplicate names) */
4837 total_servers++;
4838 }
4839 }
4840 if (reprocess && px && action) {
4841 /* Now, we know the backend and the action chosen by the user.
4842 * We can safely restart from the first server parameter
4843 * to reprocess them
4844 */
4845 cur_param = st_cur_param;
4846 next_param = st_next_param;
4847 reprocess = 0;
4848 goto reprocess_servers;
4849 }
4850
4851 next_param = cur_param;
4852 }
4853 }
4854
4855 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004856 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004857 }
4858 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004859 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004860 }
4861 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004862 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004863 }
4864 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004865 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004866 }
4867 out:
4868 return 1;
4869}
4870
4871
4872static int stats_send_http_headers(struct stream_interface *si)
4873{
Willy Tarreau87b09662015-04-03 00:22:06 +02004874 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004875 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004876 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004877
4878 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004879 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004880 "Cache-Control: no-cache\r\n"
4881 "Connection: close\r\n"
4882 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004883 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004884
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004885 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004886 chunk_appendf(&trash, "Refresh: %d\r\n",
4887 uri->refresh);
4888
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004889 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4890
4891 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4892 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4893 else
4894 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004895
Willy Tarreaueee5b512015-04-03 23:46:31 +02004896 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004897 s->logs.tv_request = now;
4898
Willy Tarreaubc18da12015-03-13 14:00:47 +01004899 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004900 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004901 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004902 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004903
4904 return 1;
4905}
4906
4907static int stats_send_http_redirect(struct stream_interface *si)
4908{
4909 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02004910 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004911 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004912 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004913
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004914 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004915 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004916 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004917 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004918 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 +01004919 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004920 }
4921
4922 /* We don't want to land on the posted stats page because a refresh will
4923 * repost the data. We don't want this to happen on accident so we redirect
4924 * the browse to the stats page with a GET.
4925 */
4926 chunk_printf(&trash,
4927 "HTTP/1.1 303 See Other\r\n"
4928 "Cache-Control: no-cache\r\n"
4929 "Content-Type: text/plain\r\n"
4930 "Connection: close\r\n"
4931 "Location: %s;st=%s%s%s%s\r\n"
4932 "\r\n",
4933 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004934 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4935 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4936 stat_status_codes[appctx->ctx.stats.st_code]) ?
4937 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004938 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004939 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4940 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004941 scope_txt);
4942
Willy Tarreaueee5b512015-04-03 23:46:31 +02004943 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004944 s->logs.tv_request = now;
4945
Willy Tarreaubc18da12015-03-13 14:00:47 +01004946 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004947 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004948 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004949 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004950
4951 return 1;
4952}
4953
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004954/* This I/O handler runs as an applet embedded in a stream interface. It is
4955 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004956 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004957 * automatically unregisters itself once transfer is complete.
4958 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02004959static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004960{
Willy Tarreau00a37f02015-04-13 12:05:19 +02004961 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02004962 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004963 struct channel *req = si_oc(si);
4964 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01004965
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004966 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4967 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004968
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004969 /* check that the output is not closed */
4970 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004971 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004972
Willy Tarreau347a35d2013-11-22 17:51:09 +01004973 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004974 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004975 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02004976 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004977 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004978 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004979 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004980 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004981 }
4982
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004983 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004984 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004985 unsigned int data_len;
4986 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004987 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004988
4989 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4990 /* One difficulty we're facing is that we must prevent
4991 * the input data from being automatically forwarded to
4992 * the output area. For this, we temporarily disable
4993 * forwarding on the channel.
4994 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004995 last_fwd = si_ic(si)->to_forward;
4996 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004997 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004998 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004999 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005000 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005001 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005002 }
5003 }
5004
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005005 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005006 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005007 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005008
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005009 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005010
5011 /* Now we must either adjust or remove the chunk size. This is
5012 * not easy because the chunk size might wrap at the end of the
5013 * buffer, so we pretend we have nothing in the buffer, we write
5014 * the size, then restore the buffer's contents. Note that we can
5015 * only do that because no forwarding is scheduled on the stats
5016 * applet.
5017 */
5018 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005019 si_ic(si)->total -= (last_len - prev_len);
5020 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005021
5022 if (last_len != data_len) {
5023 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005024 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005025 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005026
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005027 si_ic(si)->total += (last_len - data_len);
5028 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005029 }
5030 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005031 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005032 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005033 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005034
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005035 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005036 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005037 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005038 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005039 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005040 }
5041
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005042 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005043 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005044 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005045 }
5046
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005047 if (appctx->st0 == STAT_HTTP_DONE) {
5048 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5049 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005050 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005051 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005052 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005053 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005054 }
5055 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005056 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005057 res->flags |= CF_READ_NULL;
5058 si_shutr(si);
5059 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005060
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005061 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5062 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005063
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005064 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005065 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5066 si_shutr(si);
5067 res->flags |= CF_READ_NULL;
5068 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005069 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005070 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005071 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005072}
5073
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005074
Willy Tarreau909d5172012-11-26 03:04:41 +01005075static inline const char *get_conn_ctrl_name(const struct connection *conn)
5076{
Willy Tarreau3c728722014-01-23 13:50:42 +01005077 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005078 return "NONE";
5079 return conn->ctrl->name;
5080}
5081
5082static inline const char *get_conn_xprt_name(const struct connection *conn)
5083{
5084 static char ptr[17];
5085
Willy Tarreauaad69382014-01-23 14:21:42 +01005086 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005087 return "NONE";
5088
5089 if (conn->xprt == &raw_sock)
5090 return "RAW";
5091
5092#ifdef USE_OPENSSL
5093 if (conn->xprt == &ssl_sock)
5094 return "SSL";
5095#endif
5096 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5097 return ptr;
5098}
5099
5100static inline const char *get_conn_data_name(const struct connection *conn)
5101{
5102 static char ptr[17];
5103
5104 if (!conn->data)
5105 return "NONE";
5106
5107 if (conn->data == &sess_conn_cb)
5108 return "SESS";
5109
5110 if (conn->data == &si_conn_cb)
5111 return "STRM";
5112
5113 if (conn->data == &check_conn_cb)
5114 return "CHCK";
5115
5116 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5117 return ptr;
5118}
5119
Willy Tarreau87b09662015-04-03 00:22:06 +02005120/* This function dumps a complete stream state onto the stream interface's
5121 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005122 * 0 if the output buffer is full and it needs to be called again, otherwise
5123 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005124 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005125static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005126{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005127 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005128 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005129 extern const char *monthname[12];
5130 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005131 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005132 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005133
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005134 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005135
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005136 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005137 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005138 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005139 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005140 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005141 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005142 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005143 appctx->ctx.sess.uid = 0;
5144 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005145 return 1;
5146 }
5147
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005148 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005149 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005150 appctx->ctx.sess.uid = sess->uniq_id;
5151 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005152 /* fall through */
5153
5154 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005155 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005156 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005157 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005158 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005159 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5160 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005161 sess->uniq_id,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005162 strm_li(sess) && strm_li(sess)->proto->name ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005163
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005164 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005165 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005166 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005167 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005168 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005169 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005170 break;
5171 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005172 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005173 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005174 default:
5175 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005176 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005177 break;
5178 }
5179
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005180 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005181 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005182 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005183
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005184 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005185 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005186 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5187 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5188 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005189
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005190 if (conn)
5191 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005192
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005193 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005194 case AF_INET:
5195 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005196 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005197 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005198 break;
5199 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005200 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005201 break;
5202 default:
5203 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005204 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005205 break;
5206 }
5207
Willy Tarreau50943332011-09-02 17:33:05 +02005208 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005209 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005210 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005211 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005212 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5213 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005214 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005215
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005216 conn = objt_conn(sess->si[1].end);
5217 if (conn)
5218 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005219
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005220 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005221 case AF_INET:
5222 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005223 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005224 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005225 break;
5226 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005227 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005228 break;
5229 default:
5230 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005231 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005232 break;
5233 }
5234
5235 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005236 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005237 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005238 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5239 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005240 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005241 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005242
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005243 if (conn)
5244 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005245
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005246 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005247 case AF_INET:
5248 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005249 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005250 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005251 break;
5252 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005253 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005254 break;
5255 default:
5256 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005257 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005258 break;
5259 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005260
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005261 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005262 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005263 sess->task,
5264 sess->task->state,
5265 sess->task->nice, sess->task->calls,
5266 sess->task->expire ?
5267 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5268 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5269 TICKS_TO_MS(1000)) : "<NEVER>",
5270 task_in_rq(sess->task) ? ", running" : "");
5271
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005272 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005273 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005274 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5275
Willy Tarreaueee5b512015-04-03 23:46:31 +02005276 if (sess->txn)
5277 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005278 " 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 +02005279 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5280 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 +01005281
5282 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005283 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005284 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005285 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005286 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005287 obj_type_name(sess->si[0].end),
5288 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005289 sess->si[0].exp ?
5290 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5291 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5292 TICKS_TO_MS(1000)) : "<NEVER>",
5293 sess->si[0].err_type);
5294
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005295 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005296 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005297 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005298 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005299 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005300 obj_type_name(sess->si[1].end),
5301 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005302 sess->si[1].exp ?
5303 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5304 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5305 TICKS_TO_MS(1000)) : "<NEVER>",
5306 sess->si[1].err_type);
5307
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005308 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005309 chunk_appendf(&trash,
5310 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005311 conn,
5312 get_conn_ctrl_name(conn),
5313 get_conn_xprt_name(conn),
5314 get_conn_data_name(conn),
5315 obj_type_name(conn->target),
5316 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005317
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005318 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005319 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005320 conn->flags,
5321 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005322 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005323 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005324 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005325 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005326 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5327 chunk_appendf(&trash,
5328 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5329 tmpctx,
5330 tmpctx->st0,
5331 tmpctx->st1,
5332 tmpctx->st2,
5333 tmpctx->applet->name);
5334 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005335
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005336 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005337 chunk_appendf(&trash,
5338 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005339 conn,
5340 get_conn_ctrl_name(conn),
5341 get_conn_xprt_name(conn),
5342 get_conn_data_name(conn),
5343 obj_type_name(conn->target),
5344 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005345
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005346 chunk_appendf(&trash,
5347 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005348 conn->flags,
5349 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005350 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005351 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005352 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005353 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005354 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5355 chunk_appendf(&trash,
5356 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5357 tmpctx,
5358 tmpctx->st0,
5359 tmpctx->st1,
5360 tmpctx->st2,
5361 tmpctx->applet->name);
5362 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005363
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005364 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005365 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005366 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005367 &sess->req,
5368 sess->req.flags, sess->req.analysers,
5369 sess->req.pipe ? sess->req.pipe->data : 0,
5370 sess->req.to_forward, sess->req.total,
5371 sess->req.analyse_exp ?
5372 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005373 TICKS_TO_MS(1000)) : "<NEVER>");
5374
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005375 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005376 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005377 sess->req.rex ?
5378 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005379 TICKS_TO_MS(1000)) : "<NEVER>");
5380
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005381 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005382 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005383 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005384 sess->req.wex ?
5385 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005386 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005387 sess->req.buf,
5388 sess->req.buf->data, sess->req.buf->o,
5389 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005390 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005391 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005392
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005393 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005394 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005395 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005396 &sess->res,
5397 sess->res.flags, sess->res.analysers,
5398 sess->res.pipe ? sess->res.pipe->data : 0,
5399 sess->res.to_forward, sess->res.total,
5400 sess->res.analyse_exp ?
5401 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005402 TICKS_TO_MS(1000)) : "<NEVER>");
5403
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005404 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005405 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005406 sess->res.rex ?
5407 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005408 TICKS_TO_MS(1000)) : "<NEVER>");
5409
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005410 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005411 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005412 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005413 sess->res.wex ?
5414 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005415 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005416 sess->res.buf,
5417 sess->res.buf->data, sess->res.buf->o,
5418 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005419 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005420 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005421
Willy Tarreaubc18da12015-03-13 14:00:47 +01005422 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005423 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005424 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005425 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005426
5427 /* use other states to dump the contents */
5428 }
5429 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005430 appctx->ctx.sess.uid = 0;
5431 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005432 return 1;
5433}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005434
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005435#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5436static int stats_tlskeys_list(struct stream_interface *si) {
5437 struct appctx *appctx = __objt_appctx(si->end);
5438
5439 switch (appctx->st2) {
5440 case STAT_ST_INIT:
5441 /* Display the column headers. If the message cannot be sent,
5442 * quit the fucntion with returning 0. The function is called
5443 * later and restart at the state "STAT_ST_INIT".
5444 */
5445 chunk_reset(&trash);
5446 chunk_appendf(&trash, "# id (file)\n");
5447 if (bi_putchk(si_ic(si), &trash) == -1) {
5448 si_applet_cant_put(si);
5449 return 0;
5450 }
5451
5452 /* Now, we start the browsing of the references lists.
5453 * Note that the following call to LIST_ELEM return bad pointer. The only
5454 * avalaible field of this pointer is <list>. It is used with the function
5455 * tlskeys_list_get_next() for retruning the first avalaible entry
5456 */
5457 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
5458 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5459
5460 appctx->st2 = STAT_ST_LIST;
5461 /* fall through */
5462
5463 case STAT_ST_LIST:
5464 while (appctx->ctx.tlskeys.ref) {
5465 chunk_reset(&trash);
5466
5467 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
5468 appctx->ctx.tlskeys.ref->filename);
5469
5470 if (bi_putchk(si_ic(si), &trash) == -1) {
5471 /* let's try again later from this stream. We add ourselves into
5472 * this stream's users so that it can remove us upon termination.
5473 */
5474 si_applet_cant_put(si);
5475 return 0;
5476 }
5477
5478 /* get next list entry and check the end of the list */
5479 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5480 }
5481
5482 appctx->st2 = STAT_ST_FIN;
5483 /* fall through */
5484
5485 default:
5486 appctx->st2 = STAT_ST_FIN;
5487 return 1;
5488 }
5489 return 0;
5490}
5491#endif
5492
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005493static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005494{
5495 struct appctx *appctx = __objt_appctx(si->end);
5496
5497 switch (appctx->st2) {
5498 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005499 /* Display the column headers. If the message cannot be sent,
5500 * quit the fucntion with returning 0. The function is called
5501 * later and restart at the state "STAT_ST_INIT".
5502 */
5503 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005504 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005505 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005506 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005507 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005508 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005509
5510 /* Now, we start the browsing of the references lists.
5511 * Note that the following call to LIST_ELEM return bad pointer. The only
5512 * avalaible field of this pointer is <list>. It is used with the function
5513 * pat_list_get_next() for retruning the first avalaible entry
5514 */
5515 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5516 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5517 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005518 appctx->st2 = STAT_ST_LIST;
5519 /* fall through */
5520
5521 case STAT_ST_LIST:
5522 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005523 chunk_reset(&trash);
5524
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005525 /* Build messages. If the reference is used by another category than
5526 * the listed categorie, display the information in the massage.
5527 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005528 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005529 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5530 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005531
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005532 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005533 /* let's try again later from this stream. We add ourselves into
5534 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005535 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005536 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005537 return 0;
5538 }
5539
5540 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005541 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5542 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005543 }
5544
5545 appctx->st2 = STAT_ST_FIN;
5546 /* fall through */
5547
5548 default:
5549 appctx->st2 = STAT_ST_FIN;
5550 return 1;
5551 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005552 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005553}
5554
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005555static int stats_map_lookup(struct stream_interface *si)
5556{
5557 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005558 struct sample sample;
5559 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005560 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005561
5562 switch (appctx->st2) {
5563 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005564 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005565 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005566 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005567 appctx->st2 = STAT_ST_LIST;
5568 /* fall through */
5569
5570 case STAT_ST_LIST:
5571 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005572 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005573 /* initialise chunk to build new message */
5574 chunk_reset(&trash);
5575
5576 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005577 sample.type = SMP_T_STR;
5578 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005579 sample.data.str.len = appctx->ctx.map.chunk.len;
5580 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005581 if (appctx->ctx.map.expr->pat_head->match &&
5582 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005583 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5584 else
5585 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005586
5587 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005588 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5589 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5590 break;
5591 if (match_method >= PAT_MATCH_NUM)
5592 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5593 else
5594 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005595
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005596 /* case sensitive */
5597 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5598 chunk_appendf(&trash, ", case=insensitive");
5599 else
5600 chunk_appendf(&trash, ", case=sensitive");
5601
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005602 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005603 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005604 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5605 chunk_appendf(&trash, ", found=no");
5606 else
5607 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005608 }
5609
5610 /* Display match and match info */
5611 else {
5612 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005613 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5614 chunk_appendf(&trash, ", found=yes");
5615 else
5616 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005617
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005618 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005619 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005620 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005621 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005622 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005623
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005624 /* display pattern */
5625 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5626 if (pat->ref && pat->ref->pattern)
5627 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5628 else
5629 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005630 }
5631 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005632 if (pat->ref && pat->ref->pattern)
5633 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5634 else
5635 chunk_appendf(&trash, ", pattern=unknown");
5636 }
5637
5638 /* display return value */
5639 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5640 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005641 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005642 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005643 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005644 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005645 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005646 }
5647
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005648 chunk_appendf(&trash, "\n");
5649
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005650 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005651 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005652 /* let's try again later from this stream. We add ourselves into
5653 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005654 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005655 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005656 return 0;
5657 }
5658
5659 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005660 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5661 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005662 }
5663
5664 appctx->st2 = STAT_ST_FIN;
5665 /* fall through */
5666
5667 default:
5668 appctx->st2 = STAT_ST_FIN;
5669 free(appctx->ctx.map.chunk.str);
5670 return 1;
5671 }
5672}
5673
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005674static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005675{
5676 struct appctx *appctx = __objt_appctx(si->end);
5677
5678 switch (appctx->st2) {
5679
5680 case STAT_ST_INIT:
5681 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005682 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5683 struct pat_ref_elt *, list);
5684 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5685 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005686 appctx->st2 = STAT_ST_LIST;
5687 /* fall through */
5688
5689 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005690 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005691 chunk_reset(&trash);
5692
5693 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005694 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005695 chunk_appendf(&trash, "%p %s %s\n",
5696 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5697 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005698 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005699 chunk_appendf(&trash, "%p %s\n",
5700 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005701
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005702 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005703 /* let's try again later from this stream. We add ourselves into
5704 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005705 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005706 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005707 return 0;
5708 }
5709
5710 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005711 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5712 struct pat_ref_elt *, list);
5713 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005714 break;
5715 }
5716
5717 appctx->st2 = STAT_ST_FIN;
5718 /* fall through */
5719
5720 default:
5721 appctx->st2 = STAT_ST_FIN;
5722 return 1;
5723 }
5724}
5725
Willy Tarreau87b09662015-04-03 00:22:06 +02005726/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005727 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005728 * to be called again, otherwise non-zero. It is designed to be called
5729 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005730 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005731static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005732{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005733 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005734 struct connection *conn;
5735
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005736 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005737 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02005738 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005739 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005740 if (appctx->st2 == STAT_ST_LIST) {
5741 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5742 LIST_DEL(&appctx->ctx.sess.bref.users);
5743 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005744 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005745 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005746 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005747 }
5748
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005749 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005750
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005751 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005752 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005753 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02005754 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005755 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02005756 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005757 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02005758 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005759 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005760 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02005761 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005762 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005763 /* fall through */
5764
Willy Tarreau295a8372011-03-10 11:25:07 +01005765 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02005766 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005767 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5768 LIST_DEL(&appctx->ctx.sess.bref.users);
5769 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005770 }
5771
5772 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02005773 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005774 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005775 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005776
Willy Tarreau87b09662015-04-03 00:22:06 +02005777 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005778
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005779 if (appctx->ctx.sess.target) {
5780 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005781 goto next_sess;
5782
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005783 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005784 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005785 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005786 return 0;
5787
Willy Tarreau87b09662015-04-03 00:22:06 +02005788 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005789 LIST_DEL(&appctx->ctx.sess.bref.users);
5790 LIST_INIT(&appctx->ctx.sess.bref.users);
5791 if (appctx->ctx.sess.target != (void *)-1) {
5792 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005793 break;
5794 }
5795 else
5796 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005797 }
5798
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005799 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005800 "%p: proto=%s",
5801 curr_sess,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005802 strm_li(curr_sess)->proto->name);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005803
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005804
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005805 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005806 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005807 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005808 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005809 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005810 " src=%s:%d fe=%s be=%s srv=%s",
5811 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005812 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005813 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005814 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005815 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005816 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005817 break;
5818 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005819 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005820 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005821 strm_li(curr_sess)->luid,
5822 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005823 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005824 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005825 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005826 break;
5827 }
5828
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005829 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005830 " ts=%02x age=%s calls=%d",
5831 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005832 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5833 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005834
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005835 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005836 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005837 curr_sess->req.flags,
5838 curr_sess->req.buf->i,
5839 curr_sess->req.analysers,
5840 curr_sess->req.rex ?
5841 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005842 TICKS_TO_MS(1000)) : "");
5843
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005844 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005845 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005846 curr_sess->req.wex ?
5847 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005848 TICKS_TO_MS(1000)) : "");
5849
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005850 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005851 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005852 curr_sess->req.analyse_exp ?
5853 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005854 TICKS_TO_MS(1000)) : "");
5855
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005856 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005857 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005858 curr_sess->res.flags,
5859 curr_sess->res.buf->i,
5860 curr_sess->res.analysers,
5861 curr_sess->res.rex ?
5862 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005863 TICKS_TO_MS(1000)) : "");
5864
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005865 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005866 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005867 curr_sess->res.wex ?
5868 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005869 TICKS_TO_MS(1000)) : "");
5870
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005871 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005872 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005873 curr_sess->res.analyse_exp ?
5874 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005875 TICKS_TO_MS(1000)) : "");
5876
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005877 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005878 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005879 " s0=[%d,%1xh,fd=%d,ex=%s]",
5880 curr_sess->si[0].state,
5881 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005882 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005883 curr_sess->si[0].exp ?
5884 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5885 TICKS_TO_MS(1000)) : "");
5886
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005887 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005888 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005889 " s1=[%d,%1xh,fd=%d,ex=%s]",
5890 curr_sess->si[1].state,
5891 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005892 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005893 curr_sess->si[1].exp ?
5894 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5895 TICKS_TO_MS(1000)) : "");
5896
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005897 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005898 " exp=%s",
5899 curr_sess->task->expire ?
5900 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5901 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005902 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005903 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005904
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005905 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005906
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005907 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005908 /* let's try again later from this stream. We add ourselves into
5909 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005910 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005911 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005912 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005913 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005914 }
5915
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005916 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005917 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005918 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005919
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005920 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005921 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005922 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005923 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005924 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005925 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005926
Willy Tarreaubc18da12015-03-13 14:00:47 +01005927 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005928 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005929 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005930 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005931
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005932 appctx->ctx.sess.target = NULL;
5933 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005934 return 1;
5935 }
5936
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005937 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005938 /* fall through */
5939
5940 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005941 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005942 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005943 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005944}
5945
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005946/* This is called when the stream interface is closed. For instance, upon an
5947 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02005948 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005949 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005950static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005951{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005952 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5953 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5954 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005955 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005956 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5957 free(appctx->ctx.cli.err);
5958 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005959 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5960 free(appctx->ctx.map.chunk.str);
5961 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005962}
5963
Willy Tarreau20e99322013-04-13 09:22:25 +02005964/* This function is used to either dump tables states (when action is set
5965 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005966 * It returns 0 if the output buffer is full and it needs to be called
5967 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005968 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005969static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005970{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005971 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02005972 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005973 struct ebmb_node *eb;
5974 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005975 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005976 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005977
5978 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005979 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005980 * - STAT_ST_INIT : the first call
5981 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005982 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005983 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005984 * and the entry pointer points to the next entry to be dumped,
5985 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005986 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005987 * data though.
5988 */
5989
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005990 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005991 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005992 if (appctx->st2 == STAT_ST_LIST) {
5993 appctx->ctx.table.entry->ref_cnt--;
5994 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005995 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005996 return 1;
5997 }
5998
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005999 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006000
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006001 while (appctx->st2 != STAT_ST_FIN) {
6002 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006003 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006004 appctx->ctx.table.proxy = appctx->ctx.table.target;
6005 if (!appctx->ctx.table.proxy)
6006 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006007
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006008 appctx->ctx.table.entry = NULL;
6009 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006010 break;
6011
Willy Tarreau295a8372011-03-10 11:25:07 +01006012 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006013 if (!appctx->ctx.table.proxy ||
6014 (appctx->ctx.table.target &&
6015 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6016 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006017 break;
6018 }
6019
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006020 if (appctx->ctx.table.proxy->table.size) {
6021 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6022 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006023 return 0;
6024
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006025 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006026 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006027 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006028 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006029 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006030 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6031 appctx->ctx.table.entry->ref_cnt++;
6032 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006033 break;
6034 }
6035 }
6036 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006037 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006038 break;
6039
Willy Tarreau295a8372011-03-10 11:25:07 +01006040 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006041 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006042
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006043 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006044 /* we're filtering on some data contents */
6045 void *ptr;
6046 long long data;
6047
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006048 dt = appctx->ctx.table.data_type;
6049 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6050 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006051 dt);
6052
6053 data = 0;
6054 switch (stktable_data_types[dt].std_type) {
6055 case STD_T_SINT:
6056 data = stktable_data_cast(ptr, std_t_sint);
6057 break;
6058 case STD_T_UINT:
6059 data = stktable_data_cast(ptr, std_t_uint);
6060 break;
6061 case STD_T_ULL:
6062 data = stktable_data_cast(ptr, std_t_ull);
6063 break;
6064 case STD_T_FRQP:
6065 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006066 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006067 break;
6068 }
6069
6070 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006071 if ((data < appctx->ctx.table.value &&
6072 (appctx->ctx.table.data_op == STD_OP_EQ ||
6073 appctx->ctx.table.data_op == STD_OP_GT ||
6074 appctx->ctx.table.data_op == STD_OP_GE)) ||
6075 (data == appctx->ctx.table.value &&
6076 (appctx->ctx.table.data_op == STD_OP_NE ||
6077 appctx->ctx.table.data_op == STD_OP_GT ||
6078 appctx->ctx.table.data_op == STD_OP_LT)) ||
6079 (data > appctx->ctx.table.value &&
6080 (appctx->ctx.table.data_op == STD_OP_EQ ||
6081 appctx->ctx.table.data_op == STD_OP_LT ||
6082 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006083 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006084 }
6085
Simon Hormanc88b8872011-06-15 15:18:49 +09006086 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006087 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6088 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006089 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006090
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006091 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006092
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006093 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006094 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006095 struct stksess *old = appctx->ctx.table.entry;
6096 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006097 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006098 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6099 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6100 stksess_kill(&appctx->ctx.table.proxy->table, old);
6101 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006102 break;
6103 }
6104
Simon Hormanc88b8872011-06-15 15:18:49 +09006105
6106 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006107 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6108 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6109 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006110
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006111 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6112 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006113 break;
6114
Willy Tarreau295a8372011-03-10 11:25:07 +01006115 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006116 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006117 break;
6118 }
6119 }
6120 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006121}
6122
Willy Tarreaud426a182010-03-05 14:58:26 +01006123/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006124 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6125 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6126 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6127 * lines are respected within the limit of 70 output chars. Lines that are
6128 * continuation of a previous truncated line begin with "+" instead of " "
6129 * after the offset. The new pointer is returned.
6130 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006131static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6132 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006133{
6134 int end;
6135 unsigned char c;
6136
6137 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006138 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006139 return ptr;
6140
Willy Tarreau77804732012-10-29 16:14:26 +01006141 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006142
Willy Tarreaud426a182010-03-05 14:58:26 +01006143 while (ptr < len && ptr < bsize) {
6144 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006145 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006146 if (out->len > end - 2)
6147 break;
6148 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006149 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006150 if (out->len > end - 3)
6151 break;
6152 out->str[out->len++] = '\\';
6153 switch (c) {
6154 case '\t': c = 't'; break;
6155 case '\n': c = 'n'; break;
6156 case '\r': c = 'r'; break;
6157 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006158 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006159 }
6160 out->str[out->len++] = c;
6161 } else {
6162 if (out->len > end - 5)
6163 break;
6164 out->str[out->len++] = '\\';
6165 out->str[out->len++] = 'x';
6166 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6167 out->str[out->len++] = hextab[c & 0xF];
6168 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006169 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006170 /* we had a line break, let's return now */
6171 out->str[out->len++] = '\n';
6172 *line = ptr;
6173 return ptr;
6174 }
6175 }
6176 /* we have an incomplete line, we return it as-is */
6177 out->str[out->len++] = '\n';
6178 return ptr;
6179}
6180
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006181/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006182 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006183 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006184 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006185static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006186{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006187 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006188 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006189
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006190 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006191 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006192
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006193 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006194
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006195 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006196 /* the function had not been called yet, let's prepare the
6197 * buffer for a response.
6198 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006199 struct tm tm;
6200
6201 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006202 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006203 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6204 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6205 error_snapshot_id);
6206
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006207 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006208 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006209 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006210 return 0;
6211 }
6212
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006213 appctx->ctx.errors.px = proxy;
6214 appctx->ctx.errors.buf = 0;
6215 appctx->ctx.errors.bol = 0;
6216 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006217 }
6218
6219 /* we have two inner loops here, one for the proxy, the other one for
6220 * the buffer.
6221 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006222 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006223 struct error_snapshot *es;
6224
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006225 if (appctx->ctx.errors.buf == 0)
6226 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006227 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006228 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006229
6230 if (!es->when.tv_sec)
6231 goto next;
6232
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006233 if (appctx->ctx.errors.iid >= 0 &&
6234 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6235 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006236 goto next;
6237
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006238 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006239 /* just print headers now */
6240
6241 char pn[INET6_ADDRSTRLEN];
6242 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006243 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006244
6245 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006246 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006247 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006248 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006249
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006250 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6251 case AF_INET:
6252 case AF_INET6:
6253 port = get_host_port(&es->src);
6254 break;
6255 default:
6256 port = 0;
6257 }
6258
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006259 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006260 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006261 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006262 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006263 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006264 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006265 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6266 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006267 break;
6268 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006269 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006270 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006271 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006272 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006273 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006274 break;
6275 }
6276
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006277 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006278 ", server %s (#%d), event #%u\n"
6279 " src %s:%d, session #%d, session flags 0x%08x\n"
6280 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6281 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6282 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6283 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6284 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6285 es->ev_id,
6286 pn, port, es->sid, es->s_flags,
6287 es->state, es->m_flags, es->t_flags,
6288 es->m_clen, es->m_blen,
6289 es->b_flags, es->b_out, es->b_tot,
6290 es->len, es->b_wrap, es->pos);
6291
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006292 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006293 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006294 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006295 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006296 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006297 appctx->ctx.errors.ptr = 0;
6298 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006299 }
6300
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006301 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006302 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006303 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006304 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006305 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006306 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006307 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006308 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006309 goto next;
6310 }
6311
6312 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006313 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006314 int newptr;
6315 int newline;
6316
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006317 newline = appctx->ctx.errors.bol;
6318 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6319 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006320 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006321
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006322 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006323 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006324 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006325 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006326 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006327 appctx->ctx.errors.ptr = newptr;
6328 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006329 };
6330 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006331 appctx->ctx.errors.bol = 0;
6332 appctx->ctx.errors.ptr = -1;
6333 appctx->ctx.errors.buf++;
6334 if (appctx->ctx.errors.buf > 1) {
6335 appctx->ctx.errors.buf = 0;
6336 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006337 }
6338 }
6339
6340 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006341 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006342}
6343
Willy Tarreaud5781202012-09-22 19:32:35 +02006344/* parse the "level" argument on the bind lines */
6345static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6346{
6347 if (!*args[cur_arg + 1]) {
6348 memprintf(err, "'%s' : missing level", args[cur_arg]);
6349 return ERR_ALERT | ERR_FATAL;
6350 }
6351
6352 if (!strcmp(args[cur_arg+1], "user"))
6353 conf->level = ACCESS_LVL_USER;
6354 else if (!strcmp(args[cur_arg+1], "operator"))
6355 conf->level = ACCESS_LVL_OPER;
6356 else if (!strcmp(args[cur_arg+1], "admin"))
6357 conf->level = ACCESS_LVL_ADMIN;
6358 else {
6359 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6360 args[cur_arg], args[cur_arg+1]);
6361 return ERR_ALERT | ERR_FATAL;
6362 }
6363
6364 return 0;
6365}
6366
Willy Tarreau30576452015-04-13 13:50:30 +02006367struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006368 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006369 .name = "<STATS>", /* used for logging */
6370 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006371 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006372};
6373
Willy Tarreau30576452015-04-13 13:50:30 +02006374static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006375 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006376 .name = "<CLI>", /* used for logging */
6377 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006378 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006379};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006380
Willy Tarreaudc13c112013-06-21 23:16:39 +02006381static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006382 { CFG_GLOBAL, "stats", stats_parse_global },
6383 { 0, NULL, NULL },
6384}};
6385
Willy Tarreaud5781202012-09-22 19:32:35 +02006386static struct bind_kw_list bind_kws = { "STAT", { }, {
6387 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6388 { NULL, NULL, 0 },
6389}};
6390
Willy Tarreau10522fd2008-07-09 20:12:41 +02006391__attribute__((constructor))
6392static void __dumpstats_module_init(void)
6393{
6394 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006395 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006396}
6397
Willy Tarreau91861262007-10-17 17:06:05 +02006398/*
6399 * Local variables:
6400 * c-indent-level: 8
6401 * c-basic-offset: 8
6402 * End:
6403 */