blob: 559f229652c43036a646c7d953f33955bedadfc9 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Emeric Brun4147b2e2014-06-16 18:36:30 +020038#include <common/base64.h>
Willy Tarreau91861262007-10-17 17:06:05 +020039
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <types/applet.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020042
43#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020044#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010046#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
48#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010049#include <proto/freq_ctr.h>
Willy Tarreau32b60d42015-03-13 16:14:57 +010050#include <proto/frontend.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020051#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010052#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010053#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020054#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010055#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010056#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020057#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020058#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010059#include <proto/sample.h>
Willy Tarreau9903f0e2015-04-04 18:50:31 +020060#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020061#include <proto/stream.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020062#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020063#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010064#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010065#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020066
Willy Tarreau7a0169a2012-11-19 17:13:16 +010067#ifdef USE_OPENSSL
68#include <proto/ssl_sock.h>
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020069#include <types/ssl_sock.h>
Willy Tarreau7a0169a2012-11-19 17:13:16 +010070#endif
71
Willy Tarreau91b843d2014-01-28 16:27:17 +010072/* stats socket states */
73enum {
74 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
75 STAT_CLI_END, /* final state, let's close */
76 STAT_CLI_GETREQ, /* wait for a request */
77 STAT_CLI_OUTPUT, /* all states after this one are responses */
78 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
79 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010080 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010081 STAT_CLI_O_INFO, /* dump info */
Willy Tarreau87b09662015-04-03 00:22:06 +020082 STAT_CLI_O_SESS, /* dump streams */
Willy Tarreau91b843d2014-01-28 16:27:17 +010083 STAT_CLI_O_ERR, /* dump errors */
84 STAT_CLI_O_TAB, /* dump tables */
85 STAT_CLI_O_CLR, /* clear tables */
86 STAT_CLI_O_SET, /* set entries in tables */
87 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010088 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
89 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010090 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010091 STAT_CLI_O_POOLS, /* dump memory pools */
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020092 STAT_CLI_O_TLSK, /* list all TLS ticket keys references */
Willy Tarreau91b843d2014-01-28 16:27:17 +010093};
94
Willy Tarreaued7df902014-05-22 18:04:49 +020095/* Actions available for the stats admin forms */
96enum {
97 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +020098
99 /* enable/disable health checks */
100 ST_ADM_ACTION_DHLTH,
101 ST_ADM_ACTION_EHLTH,
102
103 /* force health check status */
104 ST_ADM_ACTION_HRUNN,
105 ST_ADM_ACTION_HNOLB,
106 ST_ADM_ACTION_HDOWN,
107
108 /* enable/disable agent checks */
109 ST_ADM_ACTION_DAGENT,
110 ST_ADM_ACTION_EAGENT,
111
112 /* force agent check status */
113 ST_ADM_ACTION_ARUNN,
114 ST_ADM_ACTION_ADOWN,
115
116 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200117 ST_ADM_ACTION_READY,
118 ST_ADM_ACTION_DRAIN,
119 ST_ADM_ACTION_MAINT,
120 ST_ADM_ACTION_SHUTDOWN,
121 /* these are the ancient actions, still available for compatibility */
122 ST_ADM_ACTION_DISABLE,
123 ST_ADM_ACTION_ENABLE,
124 ST_ADM_ACTION_STOP,
125 ST_ADM_ACTION_START,
126};
127
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100128static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100129static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200130static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900131static int stats_dump_sess_to_buffer(struct stream_interface *si);
132static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100133static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100134static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
135static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100136static int stats_pats_list(struct stream_interface *si);
137static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100138static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200139#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
140static int stats_tlskeys_list(struct stream_interface *si);
141#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200142static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900143
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100144/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100145 * cli_io_handler()
146 * -> stats_dump_sess_to_buffer() // "show sess"
147 * -> stats_dump_errors_to_buffer() // "show errors"
148 * -> stats_dump_info_to_buffer() // "show info"
149 * -> stats_dump_stat_to_buffer() // "show stat"
150 * -> stats_dump_csv_header()
151 * -> stats_dump_proxy_to_buffer()
152 * -> stats_dump_fe_stats()
153 * -> stats_dump_li_stats()
154 * -> stats_dump_sv_stats()
155 * -> stats_dump_be_stats()
156 *
157 * http_stats_io_handler()
158 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
159 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
160 * -> stats_dump_html_head() // emits the HTML headers
161 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
162 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
163 * -> stats_dump_html_px_hdr()
164 * -> stats_dump_fe_stats()
165 * -> stats_dump_li_stats()
166 * -> stats_dump_sv_stats()
167 * -> stats_dump_be_stats()
168 * -> stats_dump_html_px_end()
169 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100170 */
171
Willy Tarreau30576452015-04-13 13:50:30 +0200172static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900173
174static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200175 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200176 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200177 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200178 " help : this message\n"
179 " prompt : toggle interactive mode with prompt\n"
180 " quit : disconnect\n"
181 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100182 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200183 " show stat : report counters for each proxy and server\n"
184 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100185 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200186 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200187 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200188 " set weight : change a server's weight\n"
Willy Tarreau2a4b70f2014-05-22 18:42:35 +0200189 " set server : change a server's state or weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200190 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100191 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200192 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200193 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200194 " disable : put a server or frontend in maintenance mode\n"
195 " enable : re-enable a server or frontend which is in maintenance mode\n"
196 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100197 " show acl [id] : report avalaible acls or dump an acl's contents\n"
198 " get acl : reports the patterns matching a sample for an ACL\n"
199 " add acl : add acl entry\n"
200 " del acl : delete acl entry\n"
201 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100202 " show map [id] : report avalaible maps or dump a map's contents\n"
203 " get map : reports the keys and values matching a sample for a map\n"
204 " set map : modify map entry\n"
205 " add map : add map entry\n"
206 " del map : delete map entry\n"
207 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200208 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200209 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200210
Simon Horman9bd2c732011-06-15 15:18:44 +0900211static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200212 "Permission denied\n"
213 "";
214
Willy Tarreau295a8372011-03-10 11:25:07 +0100215/* data transmission states for the stats responses */
216enum {
217 STAT_ST_INIT = 0,
218 STAT_ST_HEAD,
219 STAT_ST_INFO,
220 STAT_ST_LIST,
221 STAT_ST_END,
222 STAT_ST_FIN,
223};
224
225/* data transmission states for the stats responses inside a proxy */
226enum {
227 STAT_PX_ST_INIT = 0,
228 STAT_PX_ST_TH,
229 STAT_PX_ST_FE,
230 STAT_PX_ST_LI,
231 STAT_PX_ST_SV,
232 STAT_PX_ST_BE,
233 STAT_PX_ST_END,
234 STAT_PX_ST_FIN,
235};
236
Cyril Bonté19979e12012-04-04 12:57:21 +0200237extern const char *stat_status_codes[];
238
Willy Tarreau07e9e642010-08-17 21:48:17 +0200239/* allocate a new stats frontend named <name>, and return it
240 * (or NULL in case of lack of memory).
241 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200242static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200243{
244 struct proxy *fe;
245
246 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
247 if (!fe)
248 return NULL;
249
Willy Tarreau237250c2011-07-29 01:49:03 +0200250 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200251 fe->next = proxy;
252 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200253 fe->last_change = now.tv_sec;
254 fe->id = strdup("GLOBAL");
255 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200256 fe->maxconn = 10; /* default to 10 concurrent connections */
257 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200258 fe->conf.file = strdup(file);
259 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100260 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100261 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200262
263 /* the stats frontend is the only one able to assign ID #0 */
264 fe->conf.id.key = fe->uuid = 0;
265 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200266 return fe;
267}
268
Willy Tarreaufbee7132007-10-18 13:53:22 +0200269/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200270 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
271 * error message into the <err> buffer which will be preallocated. The trailing
272 * '\n' must not be written. The function must be called with <args> pointing to
273 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200274 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200275static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200276 struct proxy *defpx, const char *file, int line,
277 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200279 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200280 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200281
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200282 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200283 int cur_arg;
284
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200285 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200286 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200287 return -1;
288 }
289
Willy Tarreau89a63132009-08-16 17:41:45 +0200290 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200291 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200292 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200293 return -1;
294 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200295 }
296
Willy Tarreau4348fad2012-09-20 16:48:07 +0200297 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200298 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200299
Willy Tarreauc53d4222012-09-20 20:19:28 +0200300 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
301 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
302 file, line, args[0], args[1], err && *err ? *err : "error");
303 return -1;
304 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200305
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200306 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200307 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200308 static int bind_dumped;
309 struct bind_kw *kw;
310
311 kw = bind_find_kw(args[cur_arg]);
312 if (kw) {
313 if (!kw->parse) {
314 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
315 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200316 return -1;
317 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200318
319 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
320 if (err && *err)
321 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
322 else
323 memprintf(err, "'%s %s' : error encountered while processing '%s'",
324 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200325 return -1;
326 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200327
328 cur_arg += 1 + kw->skip;
329 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200330 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200331
332 if (!bind_dumped) {
333 bind_dump_kws(err);
334 indent_msg(err, 4);
335 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200336 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200337
338 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
339 args[0], args[1], args[cur_arg],
340 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
341 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200342 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100343
Willy Tarreauc53d4222012-09-20 20:19:28 +0200344 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
345 l->maxconn = global.stats_fe->maxconn;
346 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200347 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200348 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100349 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200350 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
351 l->nice = -64; /* we want to boost priority for local stats */
352 global.maxsock += l->maxconn;
353 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200354 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200355 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100356 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200357 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100358
359 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200360 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100361 return -1;
362 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200363
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100364 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200365 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200366 return -1;
367 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200368 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200369 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200370 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200371 return -1;
372 }
373 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200374 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200375 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200376 else if (!strcmp(args[1], "maxconn")) {
377 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200378
379 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200380 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200381 return -1;
382 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200383
384 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200385 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200386 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200387 return -1;
388 }
389 }
390 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200391 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200392 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
393 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100394 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200395
Willy Tarreau91319572013-04-20 09:48:50 +0200396 if (!global.stats_fe) {
397 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
398 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
399 return -1;
400 }
401 }
402
Willy Tarreau35b7b162012-10-22 23:17:18 +0200403 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100404 unsigned int low, high;
405
Willy Tarreau35b7b162012-10-22 23:17:18 +0200406 if (strcmp(args[cur_arg], "all") == 0) {
407 set = 0;
408 break;
409 }
410 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100411 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200412 }
413 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100414 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200415 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100416 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100417 char *dash = strchr(args[cur_arg], '-');
418
419 low = high = str2uic(args[cur_arg]);
420 if (dash)
421 high = str2uic(dash + 1);
422
423 if (high < low) {
424 unsigned int swap = low;
425 low = high;
426 high = swap;
427 }
428
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100429 if (low < 1 || high > LONGBITS) {
430 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
431 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200432 return -1;
433 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100434 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100435 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100436 }
437 else {
438 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100439 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
440 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100441 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200442 }
443 cur_arg++;
444 }
445 global.stats_fe->bind_proc = set;
446 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200447 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200448 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200449 return -1;
450 }
451 return 0;
452}
453
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100454/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
455 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100456 * NOTE: Some tools happen to rely on the field position instead of its name,
457 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100458 */
459static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100460{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100461 chunk_appendf(&trash,
462 "# pxname,svname,"
463 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100464 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100465 "bin,bout,"
466 "dreq,dresp,"
467 "ereq,econ,eresp,"
468 "wretr,wredis,"
469 "status,weight,act,bck,"
470 "chkfail,chkdown,lastchg,downtime,qlimit,"
471 "pid,iid,sid,throttle,lbtot,tracked,type,"
472 "rate,rate_lim,rate_max,"
473 "check_status,check_code,check_duration,"
474 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
475 "req_rate,req_rate_max,req_tot,"
476 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200477 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100478 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100479}
480
Simon Hormand9366582011-06-15 15:18:45 +0900481/* print a string of text buffer to <out>. The format is :
482 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
483 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
484 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
485 */
486static int dump_text(struct chunk *out, const char *buf, int bsize)
487{
488 unsigned char c;
489 int ptr = 0;
490
491 while (buf[ptr] && ptr < bsize) {
492 c = buf[ptr];
493 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
494 if (out->len > out->size - 1)
495 break;
496 out->str[out->len++] = c;
497 }
498 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
499 if (out->len > out->size - 2)
500 break;
501 out->str[out->len++] = '\\';
502 switch (c) {
503 case ' ': c = ' '; break;
504 case '\t': c = 't'; break;
505 case '\n': c = 'n'; break;
506 case '\r': c = 'r'; break;
507 case '\e': c = 'e'; break;
508 case '\\': c = '\\'; break;
509 }
510 out->str[out->len++] = c;
511 }
512 else {
513 if (out->len > out->size - 4)
514 break;
515 out->str[out->len++] = '\\';
516 out->str[out->len++] = 'x';
517 out->str[out->len++] = hextab[(c >> 4) & 0xF];
518 out->str[out->len++] = hextab[c & 0xF];
519 }
520 ptr++;
521 }
522
523 return ptr;
524}
525
526/* print a buffer in hexa.
527 * Print stopped if <bsize> is reached, or if no more place in the chunk.
528 */
529static int dump_binary(struct chunk *out, const char *buf, int bsize)
530{
531 unsigned char c;
532 int ptr = 0;
533
534 while (ptr < bsize) {
535 c = buf[ptr];
536
537 if (out->len > out->size - 2)
538 break;
539 out->str[out->len++] = hextab[(c >> 4) & 0xF];
540 out->str[out->len++] = hextab[c & 0xF];
541
542 ptr++;
543 }
544 return ptr;
545}
546
547/* Dump the status of a table to a stream interface's
548 * read buffer. It returns 0 if the output buffer is full
549 * and needs to be called again, otherwise non-zero.
550 */
551static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
552 struct proxy *proxy, struct proxy *target)
553{
Willy Tarreau87b09662015-04-03 00:22:06 +0200554 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900555
Willy Tarreau77804732012-10-29 16:14:26 +0100556 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900557 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
558
559 /* any other information should be dumped here */
560
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200561 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100562 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900563
Willy Tarreaubc18da12015-03-13 14:00:47 +0100564 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200565 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900566 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100567 }
Simon Hormand9366582011-06-15 15:18:45 +0900568
569 return 1;
570}
571
572/* Dump the a table entry to a stream interface's
573 * read buffer. It returns 0 if the output buffer is full
574 * and needs to be called again, otherwise non-zero.
575 */
576static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
577 struct proxy *proxy, struct stksess *entry)
578{
579 int dt;
580
Willy Tarreau77804732012-10-29 16:14:26 +0100581 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900582
583 if (proxy->table.type == STKTABLE_TYPE_IP) {
584 char addr[INET_ADDRSTRLEN];
585 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100586 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900587 }
588 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
589 char addr[INET6_ADDRSTRLEN];
590 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100591 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900592 }
593 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100594 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900595 }
596 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100597 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900598 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
599 }
600 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100601 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900602 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
603 }
604
Willy Tarreau77804732012-10-29 16:14:26 +0100605 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900606
607 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
608 void *ptr;
609
610 if (proxy->table.data_ofs[dt] == 0)
611 continue;
612 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100613 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900614 else
Willy Tarreau77804732012-10-29 16:14:26 +0100615 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900616
617 ptr = stktable_data_ptr(&proxy->table, entry, dt);
618 switch (stktable_data_types[dt].std_type) {
619 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100620 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900621 break;
622 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100623 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900624 break;
625 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100626 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900627 break;
628 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100629 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900630 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
631 proxy->table.data_arg[dt].u));
632 break;
633 }
634 }
Willy Tarreau77804732012-10-29 16:14:26 +0100635 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900636
Willy Tarreaubc18da12015-03-13 14:00:47 +0100637 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200638 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900639 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100640 }
Simon Hormand9366582011-06-15 15:18:45 +0900641
642 return 1;
643}
644
Willy Tarreaudec98142012-06-06 23:37:08 +0200645static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900646{
Willy Tarreau87b09662015-04-03 00:22:06 +0200647 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100648 struct appctx *appctx = __objt_appctx(si->end);
649 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900650 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900651 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900652 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200653 long long value;
654 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200655 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200656 void *ptr;
657 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900658
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100659 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900660
661 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100662 appctx->ctx.cli.msg = "Key value expected\n";
663 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900664 return;
665 }
666
Simon Hormanc5b89f62011-06-15 15:18:50 +0900667 switch (px->table.type) {
668 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900669 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100670 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900671 break;
672 case STKTABLE_TYPE_IPV6:
673 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100674 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900675 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900676 case STKTABLE_TYPE_INTEGER:
677 {
678 char *endptr;
679 unsigned long val;
680 errno = 0;
681 val = strtoul(args[4], &endptr, 10);
682 if ((errno == ERANGE && val == ULONG_MAX) ||
683 (errno != 0 && val == 0) || endptr == args[4] ||
684 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100685 appctx->ctx.cli.msg = "Invalid key\n";
686 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900687 return;
688 }
689 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100690 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900691 break;
692 }
693 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900694 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100695 static_table_key->key = args[4];
696 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900697 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900698 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200699 switch (action) {
700 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100701 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200702 break;
703 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100704 appctx->ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200705 break;
706 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100707 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200708 break;
709 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100710 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900711 return;
712 }
713
714 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200715 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100716 appctx->ctx.cli.msg = stats_permission_denied_msg;
717 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900718 return;
719 }
720
Willy Tarreau07115412012-10-29 21:56:59 +0100721 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900722
Willy Tarreaudec98142012-06-06 23:37:08 +0200723 switch (action) {
724 case STAT_CLI_O_TAB:
725 if (!ts)
726 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100727 chunk_reset(&trash);
728 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900729 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100730 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900731 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200732
733 case STAT_CLI_O_CLR:
734 if (!ts)
735 return;
736 if (ts->ref_cnt) {
737 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100738 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
739 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200740 return;
741 }
742 stksess_kill(&px->table, ts);
743 break;
Simon Horman17bce342011-06-15 15:18:47 +0900744
Willy Tarreau654694e2012-06-07 01:03:16 +0200745 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200746 if (ts)
747 stktable_touch(&px->table, ts, 1);
748 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100749 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200750 if (!ts) {
751 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100752 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
753 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200754 return;
755 }
756 stktable_store(&px->table, ts, 1);
757 }
758
Willy Tarreau47060b62013-08-01 21:11:42 +0200759 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
760 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100761 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
762 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200763 return;
764 }
765
766 data_type = stktable_get_data_type(args[cur_arg] + 5);
767 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100768 appctx->ctx.cli.msg = "Unknown data type\n";
769 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200770 return;
771 }
772
773 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100774 appctx->ctx.cli.msg = "Data type not stored in this table\n";
775 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200776 return;
777 }
778
779 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100780 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
781 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200782 return;
783 }
784
785 ptr = stktable_data_ptr(&px->table, ts, data_type);
786
787 switch (stktable_data_types[data_type].std_type) {
788 case STD_T_SINT:
789 stktable_data_cast(ptr, std_t_sint) = value;
790 break;
791 case STD_T_UINT:
792 stktable_data_cast(ptr, std_t_uint) = value;
793 break;
794 case STD_T_ULL:
795 stktable_data_cast(ptr, std_t_ull) = value;
796 break;
797 case STD_T_FRQP:
798 /* We set both the current and previous values. That way
799 * the reported frequency is stable during all the period
800 * then slowly fades out. This allows external tools to
801 * push measures without having to update them too often.
802 */
803 frqp = &stktable_data_cast(ptr, std_t_frqp);
804 frqp->curr_tick = now_ms;
805 frqp->prev_ctr = 0;
806 frqp->curr_ctr = value;
807 break;
808 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200809 }
810 break;
811
Willy Tarreaudec98142012-06-06 23:37:08 +0200812 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100813 appctx->ctx.cli.msg = "Unknown action\n";
814 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200815 break;
Simon Horman121f3052011-06-15 15:18:46 +0900816 }
Simon Horman121f3052011-06-15 15:18:46 +0900817}
818
Willy Tarreau654694e2012-06-07 01:03:16 +0200819static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900820{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100821 struct appctx *appctx = __objt_appctx(si->end);
822
Willy Tarreau04b3a192013-04-13 09:41:37 +0200823 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100824 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
825 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200826 return;
827 }
828
Simon Hormand5b9fd92011-06-15 15:18:48 +0900829 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100830 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
831 if (appctx->ctx.table.data_type < 0) {
832 appctx->ctx.cli.msg = "Unknown data type\n";
833 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900834 return;
835 }
836
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100837 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
838 appctx->ctx.cli.msg = "Data type not stored in this table\n";
839 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900840 return;
841 }
842
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100843 appctx->ctx.table.data_op = get_std_op(args[4]);
844 if (appctx->ctx.table.data_op < 0) {
845 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
846 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900847 return;
848 }
849
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100850 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
851 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
852 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900853 return;
854 }
855}
856
Willy Tarreaudec98142012-06-06 23:37:08 +0200857static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900858{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100859 struct appctx *appctx = __objt_appctx(si->end);
860
861 appctx->ctx.table.data_type = -1;
862 appctx->st2 = STAT_ST_INIT;
863 appctx->ctx.table.target = NULL;
864 appctx->ctx.table.proxy = NULL;
865 appctx->ctx.table.entry = NULL;
866 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900867
868 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100869 appctx->ctx.table.target = find_stktable(args[2]);
870 if (!appctx->ctx.table.target) {
871 appctx->ctx.cli.msg = "No such table\n";
872 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900873 return;
874 }
875 }
876 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200877 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900878 goto err_args;
879 return;
880 }
881
882 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200883 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900884 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200885 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900886 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900887 goto err_args;
888
889 return;
890
891err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200892 switch (action) {
893 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100894 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200895 break;
896 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100897 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200898 break;
899 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100900 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200901 break;
902 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100903 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900904}
905
Willy Tarreau532a4502011-09-07 22:37:44 +0200906/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +0200907 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +0200908 * level to be admin.
909 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200910static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +0200911{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100912 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200913 struct proxy *px;
914
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200915 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100916 appctx->ctx.cli.msg = stats_permission_denied_msg;
917 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200918 return NULL;
919 }
920
921 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100922 appctx->ctx.cli.msg = "A frontend name is expected.\n";
923 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200924 return NULL;
925 }
926
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200927 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +0200928 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100929 appctx->ctx.cli.msg = "No such frontend.\n";
930 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200931 return NULL;
932 }
933 return px;
934}
935
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200936/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
937 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +0200938 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200939 * the <arg> is modified to remove the '/'.
940 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200941static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200942{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100943 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200944 struct proxy *px;
945 struct server *sv;
946 char *line;
947
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200948 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100949 appctx->ctx.cli.msg = stats_permission_denied_msg;
950 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200951 return NULL;
952 }
953
954 /* split "backend/server" and make <line> point to server */
955 for (line = arg; *line; line++)
956 if (*line == '/') {
957 *line++ = '\0';
958 break;
959 }
960
961 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100962 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
963 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200964 return NULL;
965 }
966
967 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100968 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
969 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200970 return NULL;
971 }
972
973 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100974 appctx->ctx.cli.msg = "Proxy is disabled.\n";
975 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200976 return NULL;
977 }
978
979 return sv;
980}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200981
982/* This function is used with TLS ticket keys management. It permits to browse
983 * each reference. The variable <getnext> must contain the current node,
984 * <end> point to the root node.
985 */
986#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
987static inline
988struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
989{
990 struct tls_keys_ref *ref = getnext;
991
992 while (1) {
993
994 /* Get next list entry. */
995 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
996
997 /* If the entry is the last of the list, return NULL. */
998 if (&ref->list == end)
999 return NULL;
1000
1001 return ref;
1002 }
1003}
1004
1005static inline
1006struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1007{
1008 int id;
1009 char *error;
1010
1011 /* If the reference starts by a '#', this is numeric id. */
1012 if (reference[0] == '#') {
1013 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1014 id = strtol(reference + 1, &error, 10);
1015 if (*error != '\0')
1016 return NULL;
1017
1018 /* Perform the unique id lookup. */
1019 return tlskeys_ref_lookupid(id);
1020 }
1021
1022 /* Perform the string lookup. */
1023 return tlskeys_ref_lookup(reference);
1024}
1025#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001026
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001027/* This function is used with map and acl management. It permits to browse
1028 * each reference. The variable <getnext> must contain the current node,
1029 * <end> point to the root node and the <flags> permit to filter required
1030 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001031 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001032static inline
1033struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1034 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001035{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001036 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001037
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001038 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001039
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001040 /* Get next list entry. */
1041 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001042
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001043 /* If the entry is the last of the list, return NULL. */
1044 if (&ref->list == end)
1045 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001046
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001047 /* If the entry match the flag, return it. */
1048 if (ref->flags & flags)
1049 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001050 }
1051}
1052
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001053static inline
1054struct pat_ref *pat_ref_lookup_ref(const char *reference)
1055{
1056 int id;
1057 char *error;
1058
1059 /* If the reference starts by a '#', this is numeric id. */
1060 if (reference[0] == '#') {
1061 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1062 id = strtol(reference + 1, &error, 10);
1063 if (*error != '\0')
1064 return NULL;
1065
1066 /* Perform the unique id lookup. */
1067 return pat_ref_lookupid(id);
1068 }
1069
1070 /* Perform the string lookup. */
1071 return pat_ref_lookup(reference);
1072}
1073
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001074/* This function is used with map and acl management. It permits to browse
1075 * each reference.
1076 */
1077static inline
1078struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1079{
1080 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001081 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1082 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001083 return NULL;
1084 return expr;
1085}
1086
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001087/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001088 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001089 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001090 * designating the function which will have to process the request, which can
1091 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001092 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001093static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001094{
Willy Tarreau87b09662015-04-03 00:22:06 +02001095 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001096 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001097 char *args[MAX_STATS_ARGS + 1];
1098 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001099 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001100
1101 while (isspace((unsigned char)*line))
1102 line++;
1103
1104 arg = 0;
1105 args[arg] = line;
1106
1107 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001108 if (*line == '\\') {
1109 line++;
1110 if (*line == '\0')
1111 break;
1112 }
1113 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001114 *line++ = '\0';
1115
1116 while (isspace((unsigned char)*line))
1117 line++;
1118
1119 args[++arg] = line;
1120 continue;
1121 }
1122
1123 line++;
1124 }
1125
1126 while (++arg <= MAX_STATS_ARGS)
1127 args[arg] = line;
1128
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001129 /* remove \ */
1130 arg = 0;
1131 while (*args[arg] != '\0') {
1132 j = 0;
1133 for (i=0; args[arg][i] != '\0'; i++) {
1134 if (args[arg][i] == '\\')
1135 continue;
1136 args[arg][j] = args[arg][i];
1137 j++;
1138 }
1139 args[arg][j] = '\0';
1140 arg++;
1141 }
1142
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001143 appctx->ctx.stats.scope_str = 0;
1144 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001145 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001146 if (strcmp(args[0], "show") == 0) {
1147 if (strcmp(args[1], "stat") == 0) {
1148 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001149 appctx->ctx.stats.flags |= STAT_BOUND;
1150 appctx->ctx.stats.iid = atoi(args[2]);
1151 appctx->ctx.stats.type = atoi(args[3]);
1152 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001153 }
1154
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001155 appctx->st2 = STAT_ST_INIT;
1156 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001157 }
1158 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001159 appctx->st2 = STAT_ST_INIT;
1160 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001161 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001162 else if (strcmp(args[1], "pools") == 0) {
1163 appctx->st2 = STAT_ST_INIT;
1164 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1165 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001166 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001167 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001168 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001169 appctx->ctx.cli.msg = stats_permission_denied_msg;
1170 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001171 return 1;
1172 }
Willy Tarreau76153662012-11-26 01:16:39 +01001173 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001174 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001175 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001176 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001177 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001178 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001179 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001180 appctx->ctx.sess.pos = 0;
1181 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001182 }
1183 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001184 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001185 appctx->ctx.cli.msg = stats_permission_denied_msg;
1186 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001187 return 1;
1188 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001189 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001190 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001191 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001192 appctx->ctx.errors.iid = -1;
1193 appctx->ctx.errors.px = NULL;
1194 appctx->st2 = STAT_ST_INIT;
1195 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001196 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001197 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001198 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001199 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001200 else if (strcmp(args[1], "tls-keys") == 0) {
1201#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1202 appctx->st2 = STAT_ST_INIT;
1203 appctx->st0 = STAT_CLI_O_TLSK;
1204#else
1205 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1206 "that doesn't support specifying TLS ticket keys\n";
1207 appctx->st0 = STAT_CLI_PRINT;
1208#endif
1209 return 1;
1210 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001211 else if (strcmp(args[1], "map") == 0 ||
1212 strcmp(args[1], "acl") == 0) {
1213
1214 /* Set ACL or MAP flags. */
1215 if (args[1][0] == 'm')
1216 appctx->ctx.map.display_flags = PAT_REF_MAP;
1217 else
1218 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001219
1220 /* no parameter: display all map avalaible */
1221 if (!*args[2]) {
1222 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001223 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001224 return 1;
1225 }
1226
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001227 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001228 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001229 if (!appctx->ctx.map.ref ||
1230 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1231 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001232 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001233 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001234 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001235 appctx->st0 = STAT_CLI_PRINT;
1236 return 1;
1237 }
1238 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001239 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001240 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001241 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001242 return 0;
1243 }
1244 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001245 else if (strcmp(args[0], "clear") == 0) {
1246 if (strcmp(args[1], "counters") == 0) {
1247 struct proxy *px;
1248 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001249 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001250 int clrall = 0;
1251
1252 if (strcmp(args[2], "all") == 0)
1253 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001254
Willy Tarreau6162db22009-10-10 17:13:00 +02001255 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001256 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1257 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001258 appctx->ctx.cli.msg = stats_permission_denied_msg;
1259 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001260 return 1;
1261 }
1262
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001263 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001264 if (clrall) {
1265 memset(&px->be_counters, 0, sizeof(px->be_counters));
1266 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1267 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001268 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001269 px->be_counters.conn_max = 0;
1270 px->be_counters.p.http.rps_max = 0;
1271 px->be_counters.sps_max = 0;
1272 px->be_counters.cps_max = 0;
1273 px->be_counters.nbpend_max = 0;
1274
1275 px->fe_counters.conn_max = 0;
1276 px->fe_counters.p.http.rps_max = 0;
1277 px->fe_counters.sps_max = 0;
1278 px->fe_counters.cps_max = 0;
1279 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001280 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001281
1282 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001283 if (clrall)
1284 memset(&sv->counters, 0, sizeof(sv->counters));
1285 else {
1286 sv->counters.cur_sess_max = 0;
1287 sv->counters.nbpend_max = 0;
1288 sv->counters.sps_max = 0;
1289 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001290
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001291 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001292 if (li->counters) {
1293 if (clrall)
1294 memset(li->counters, 0, sizeof(*li->counters));
1295 else
1296 li->counters->conn_max = 0;
1297 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001298 }
1299
Willy Tarreau81c25d02011-09-07 15:17:21 +02001300 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001301 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001302 return 1;
1303 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001304 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001305 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001306 /* end of processing */
1307 return 1;
1308 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001309 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1310 /* Set ACL or MAP flags. */
1311 if (args[1][0] == 'm')
1312 appctx->ctx.map.display_flags = PAT_REF_MAP;
1313 else
1314 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001315
1316 /* no parameter */
1317 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001318 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1319 appctx->ctx.cli.msg = "Missing map identifier.\n";
1320 else
1321 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001322 appctx->st0 = STAT_CLI_PRINT;
1323 return 1;
1324 }
1325
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001326 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001327 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001328 if (!appctx->ctx.map.ref ||
1329 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1330 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001331 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001332 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001333 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001334 appctx->st0 = STAT_CLI_PRINT;
1335 return 1;
1336 }
1337
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001338 /* Clear all. */
1339 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001340
1341 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001342 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001343 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001344 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001345 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001346 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001347 return 0;
1348 }
1349 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001350 else if (strcmp(args[0], "get") == 0) {
1351 if (strcmp(args[1], "weight") == 0) {
1352 struct proxy *px;
1353 struct server *sv;
1354
1355 /* split "backend/server" and make <line> point to server */
1356 for (line = args[2]; *line; line++)
1357 if (*line == '/') {
1358 *line++ = '\0';
1359 break;
1360 }
1361
1362 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001363 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1364 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001365 return 1;
1366 }
1367
1368 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001369 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1370 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001371 return 1;
1372 }
1373
1374 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001375 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001376 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001377 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001378
Willy Tarreau38338fa2009-10-10 18:37:29 +02001379 return 1;
1380 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001381 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1382 /* Set flags. */
1383 if (args[1][0] == 'm')
1384 appctx->ctx.map.display_flags = PAT_REF_MAP;
1385 else
1386 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001387
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001388 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001389 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001390 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1391 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1392 else
1393 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001394 appctx->st0 = STAT_CLI_PRINT;
1395 return 1;
1396 }
1397
1398 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001399 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001400 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001401 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001402 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001403 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001404 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001405 appctx->st0 = STAT_CLI_PRINT;
1406 return 1;
1407 }
1408
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001409 /* copy input string. The string must be allocated because
1410 * it may be used over multiple iterations. It's released
1411 * at the end and upon abort anyway.
1412 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001413 appctx->ctx.map.chunk.len = strlen(args[3]);
1414 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1415 appctx->ctx.map.chunk.str = strdup(args[3]);
1416 if (!appctx->ctx.map.chunk.str) {
1417 appctx->ctx.cli.msg = "Out of memory error.\n";
1418 appctx->st0 = STAT_CLI_PRINT;
1419 return 1;
1420 }
1421
1422 /* prepare response */
1423 appctx->st2 = STAT_ST_INIT;
1424 appctx->st0 = STAT_CLI_O_MLOOK;
1425 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001426 else { /* not "get weight" */
1427 return 0;
1428 }
1429 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001430 else if (strcmp(args[0], "set") == 0) {
1431 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001432 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001433 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001434
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001435 sv = expect_server_admin(s, si, args[2]);
1436 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001437 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001438
Simon Horman7d09b9a2013-02-12 10:45:51 +09001439 warning = server_parse_weight_change_request(sv, args[3]);
1440 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001441 appctx->ctx.cli.msg = warning;
1442 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001443 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001444 return 1;
1445 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001446 else if (strcmp(args[1], "server") == 0) {
1447 struct server *sv;
1448 const char *warning;
1449
1450 sv = expect_server_admin(s, si, args[2]);
1451 if (!sv)
1452 return 1;
1453
1454 if (strcmp(args[3], "weight") == 0) {
1455 warning = server_parse_weight_change_request(sv, args[4]);
1456 if (warning) {
1457 appctx->ctx.cli.msg = warning;
1458 appctx->st0 = STAT_CLI_PRINT;
1459 }
1460 }
1461 else if (strcmp(args[3], "state") == 0) {
1462 if (strcmp(args[4], "ready") == 0)
1463 srv_adm_set_ready(sv);
1464 else if (strcmp(args[4], "drain") == 0)
1465 srv_adm_set_drain(sv);
1466 else if (strcmp(args[4], "maint") == 0)
1467 srv_adm_set_maint(sv);
1468 else {
1469 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1470 appctx->st0 = STAT_CLI_PRINT;
1471 }
1472 }
1473 else if (strcmp(args[3], "health") == 0) {
1474 if (sv->track) {
1475 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1476 appctx->st0 = STAT_CLI_PRINT;
1477 }
1478 else if (strcmp(args[4], "up") == 0) {
1479 sv->check.health = sv->check.rise + sv->check.fall - 1;
1480 srv_set_running(sv, "changed from CLI");
1481 }
1482 else if (strcmp(args[4], "stopping") == 0) {
1483 sv->check.health = sv->check.rise + sv->check.fall - 1;
1484 srv_set_stopping(sv, "changed from CLI");
1485 }
1486 else if (strcmp(args[4], "down") == 0) {
1487 sv->check.health = 0;
1488 srv_set_stopped(sv, "changed from CLI");
1489 }
1490 else {
1491 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1492 appctx->st0 = STAT_CLI_PRINT;
1493 }
1494 }
1495 else if (strcmp(args[3], "agent") == 0) {
1496 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1497 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1498 appctx->st0 = STAT_CLI_PRINT;
1499 }
1500 else if (strcmp(args[4], "up") == 0) {
1501 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1502 srv_set_running(sv, "changed from CLI");
1503 }
1504 else if (strcmp(args[4], "down") == 0) {
1505 sv->agent.health = 0;
1506 srv_set_stopped(sv, "changed from CLI");
1507 }
1508 else {
1509 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1510 appctx->st0 = STAT_CLI_PRINT;
1511 }
1512 }
1513 else {
1514 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1515 appctx->st0 = STAT_CLI_PRINT;
1516 }
1517 return 1;
1518 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001519 else if (strcmp(args[1], "timeout") == 0) {
1520 if (strcmp(args[2], "cli") == 0) {
1521 unsigned timeout;
1522 const char *res;
1523
1524 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001525 appctx->ctx.cli.msg = "Expects an integer value.\n";
1526 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001527 return 1;
1528 }
1529
1530 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1531 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001532 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1533 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001534 return 1;
1535 }
1536
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001537 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001538 return 1;
1539 }
1540 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001541 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1542 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001543 return 1;
1544 }
1545 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001546 else if (strcmp(args[1], "maxconn") == 0) {
1547 if (strcmp(args[2], "frontend") == 0) {
1548 struct proxy *px;
1549 struct listener *l;
1550 int v;
1551
Willy Tarreau532a4502011-09-07 22:37:44 +02001552 px = expect_frontend_admin(s, si, args[3]);
1553 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001554 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001555
1556 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001557 appctx->ctx.cli.msg = "Integer value expected.\n";
1558 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001559 return 1;
1560 }
1561
1562 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001563 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001564 appctx->ctx.cli.msg = "Value out of range.\n";
1565 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001566 return 1;
1567 }
1568
1569 /* OK, the value is fine, so we assign it to the proxy and to all of
1570 * its listeners. The blocked ones will be dequeued.
1571 */
1572 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001573 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001574 l->maxconn = v;
1575 if (l->state == LI_FULL)
1576 resume_listener(l);
1577 }
1578
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001579 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1580 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001581
1582 return 1;
1583 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001584 else if (strcmp(args[2], "global") == 0) {
1585 int v;
1586
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001587 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001588 appctx->ctx.cli.msg = stats_permission_denied_msg;
1589 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001590 return 1;
1591 }
1592
1593 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001594 appctx->ctx.cli.msg = "Expects an integer value.\n";
1595 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001596 return 1;
1597 }
1598
1599 v = atoi(args[3]);
1600 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001601 appctx->ctx.cli.msg = "Value out of range.\n";
1602 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001603 return 1;
1604 }
1605
1606 /* check for unlimited values */
1607 if (v <= 0)
1608 v = global.hardmaxconn;
1609
1610 global.maxconn = v;
1611
1612 /* Dequeues all of the listeners waiting for a resource */
1613 if (!LIST_ISEMPTY(&global_listener_queue))
1614 dequeue_all_listeners(&global_listener_queue);
1615
1616 return 1;
1617 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001618 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001619 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1620 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001621 return 1;
1622 }
1623 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001624 else if (strcmp(args[1], "rate-limit") == 0) {
1625 if (strcmp(args[2], "connections") == 0) {
1626 if (strcmp(args[3], "global") == 0) {
1627 int v;
1628
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001629 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001630 appctx->ctx.cli.msg = stats_permission_denied_msg;
1631 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001632 return 1;
1633 }
1634
1635 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001636 appctx->ctx.cli.msg = "Expects an integer value.\n";
1637 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001638 return 1;
1639 }
1640
1641 v = atoi(args[4]);
1642 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001643 appctx->ctx.cli.msg = "Value out of range.\n";
1644 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001645 return 1;
1646 }
1647
1648 global.cps_lim = v;
1649
1650 /* Dequeues all of the listeners waiting for a resource */
1651 if (!LIST_ISEMPTY(&global_listener_queue))
1652 dequeue_all_listeners(&global_listener_queue);
1653
1654 return 1;
1655 }
1656 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001657 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1658 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001659 return 1;
1660 }
1661 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001662 else if (strcmp(args[2], "sessions") == 0) {
1663 if (strcmp(args[3], "global") == 0) {
1664 int v;
1665
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001666 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001667 appctx->ctx.cli.msg = stats_permission_denied_msg;
1668 appctx->st0 = STAT_CLI_PRINT;
1669 return 1;
1670 }
1671
1672 if (!*args[4]) {
1673 appctx->ctx.cli.msg = "Expects an integer value.\n";
1674 appctx->st0 = STAT_CLI_PRINT;
1675 return 1;
1676 }
1677
1678 v = atoi(args[4]);
1679 if (v < 0) {
1680 appctx->ctx.cli.msg = "Value out of range.\n";
1681 appctx->st0 = STAT_CLI_PRINT;
1682 return 1;
1683 }
1684
1685 global.sps_lim = v;
1686
1687 /* Dequeues all of the listeners waiting for a resource */
1688 if (!LIST_ISEMPTY(&global_listener_queue))
1689 dequeue_all_listeners(&global_listener_queue);
1690
1691 return 1;
1692 }
1693 else {
1694 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1695 appctx->st0 = STAT_CLI_PRINT;
1696 return 1;
1697 }
1698 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001699#ifdef USE_OPENSSL
1700 else if (strcmp(args[2], "ssl-sessions") == 0) {
1701 if (strcmp(args[3], "global") == 0) {
1702 int v;
1703
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001704 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001705 appctx->ctx.cli.msg = stats_permission_denied_msg;
1706 appctx->st0 = STAT_CLI_PRINT;
1707 return 1;
1708 }
1709
1710 if (!*args[4]) {
1711 appctx->ctx.cli.msg = "Expects an integer value.\n";
1712 appctx->st0 = STAT_CLI_PRINT;
1713 return 1;
1714 }
1715
1716 v = atoi(args[4]);
1717 if (v < 0) {
1718 appctx->ctx.cli.msg = "Value out of range.\n";
1719 appctx->st0 = STAT_CLI_PRINT;
1720 return 1;
1721 }
1722
1723 global.ssl_lim = v;
1724
1725 /* Dequeues all of the listeners waiting for a resource */
1726 if (!LIST_ISEMPTY(&global_listener_queue))
1727 dequeue_all_listeners(&global_listener_queue);
1728
1729 return 1;
1730 }
1731 else {
1732 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1733 appctx->st0 = STAT_CLI_PRINT;
1734 return 1;
1735 }
1736 }
1737#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001738 else if (strcmp(args[2], "http-compression") == 0) {
1739 if (strcmp(args[3], "global") == 0) {
1740 int v;
1741
Willy Tarreau85d47f92012-11-21 00:29:50 +01001742 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001743 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1744 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001745 return 1;
1746 }
1747
William Lallemandd85f9172012-11-09 17:05:39 +01001748 v = atoi(args[4]);
1749 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1750 }
1751 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001752 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1753 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001754 return 1;
1755 }
1756 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001757 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001758 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001759 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001760 return 1;
1761 }
1762 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001763 else if (strcmp(args[1], "table") == 0) {
1764 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1765 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001766 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001767 char *err;
1768
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001769 /* Set flags. */
1770 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001771
1772 /* Expect three parameters: map name, key and new value. */
1773 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001774 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001775 appctx->st0 = STAT_CLI_PRINT;
1776 return 1;
1777 }
1778
1779 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001780 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001781 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001782 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001783 appctx->st0 = STAT_CLI_PRINT;
1784 return 1;
1785 }
1786
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001787 /* If the entry identifier start with a '#', it is considered as
1788 * pointer id
1789 */
1790 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1791 struct pat_ref_elt *ref;
1792 long long int conv;
1793 char *error;
1794
1795 /* Convert argument to integer value. */
1796 conv = strtoll(&args[3][1], &error, 16);
1797 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001798 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001799 appctx->st0 = STAT_CLI_PRINT;
1800 return 1;
1801 }
1802
1803 /* Convert and check integer to pointer. */
1804 ref = (struct pat_ref_elt *)(long)conv;
1805 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001806 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001807 appctx->st0 = STAT_CLI_PRINT;
1808 return 1;
1809 }
1810
1811 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001812 err = NULL;
1813 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1814 if (err)
1815 memprintf(&err, "%s.\n", err);
1816 appctx->ctx.cli.err = err;
1817 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001818 return 1;
1819 }
1820 }
1821 else {
1822 /* Else, use the entry identifier as pattern
1823 * string, and update the value.
1824 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001825 err = NULL;
1826 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1827 if (err)
1828 memprintf(&err, "%s.\n", err);
1829 appctx->ctx.cli.err = err;
1830 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001831 return 1;
1832 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001833 }
1834
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001835 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001836 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001837 return 1;
1838 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001839#ifdef USE_OPENSSL
1840 else if (strcmp(args[1], "ssl") == 0) {
1841 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01001842#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001843 char *err = NULL;
1844
Emeric Brunaf4ef742014-06-19 14:10:45 +02001845 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001846 if (!*args[3]) {
1847 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1848 appctx->st0 = STAT_CLI_PRINT;
1849 return 1;
1850 }
1851
1852 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1853 if (trash.len < 0) {
1854 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1855 appctx->st0 = STAT_CLI_PRINT;
1856 return 1;
1857 }
1858
1859 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1860 if (err) {
1861 memprintf(&err, "%s.\n", err);
1862 appctx->ctx.cli.err = err;
1863 appctx->st0 = STAT_CLI_PRINT_FREE;
1864 }
1865 return 1;
1866 }
1867 appctx->ctx.cli.msg = "OCSP Response updated!";
1868 appctx->st0 = STAT_CLI_PRINT;
1869 return 1;
1870#else
1871 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1872 appctx->st0 = STAT_CLI_PRINT;
1873 return 1;
1874#endif
1875 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001876 else if (strcmp(args[2], "tls-key") == 0) {
1877#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1878 /* Expect two parameters: the filename and the new new TLS key in encoding */
1879 if (!*args[3] || !*args[4]) {
1880 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
1881 appctx->st0 = STAT_CLI_PRINT;
1882 return 1;
1883 }
1884
1885 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
1886 if(!appctx->ctx.tlskeys.ref) {
1887 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
1888 appctx->st0 = STAT_CLI_PRINT;
1889 return 1;
1890 }
1891
1892 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
1893 if (trash.len != sizeof(struct tls_sess_key)) {
1894 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
1895 appctx->st0 = STAT_CLI_PRINT;
1896 return 1;
1897 }
1898
1899 memcpy(appctx->ctx.tlskeys.ref->tlskeys + 2 % TLS_TICKETS_NO, trash.str, trash.len);
1900 appctx->ctx.tlskeys.ref->tls_ticket_enc_index = appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 1 % TLS_TICKETS_NO;
1901
1902 appctx->ctx.cli.msg = "TLS ticket key updated!";
1903 appctx->st0 = STAT_CLI_PRINT;
1904 return 1;
1905#else
1906 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1907 "that doesn't support specifying TLS ticket keys\n";
1908 appctx->st0 = STAT_CLI_PRINT;
1909 return 1;
1910#endif
1911 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001912 else {
1913 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1914 appctx->st0 = STAT_CLI_PRINT;
1915 return 1;
1916 }
1917 }
1918#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001919 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001920 return 0;
1921 }
1922 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001923 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001924 if (strcmp(args[1], "agent") == 0) {
1925 struct server *sv;
1926
1927 sv = expect_server_admin(s, si, args[2]);
1928 if (!sv)
1929 return 1;
1930
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001931 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1932 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1933 appctx->st0 = STAT_CLI_PRINT;
1934 return 1;
1935 }
1936
1937 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001938 return 1;
1939 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001940 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001941 struct server *sv;
1942
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001943 sv = expect_server_admin(s, si, args[2]);
1944 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001945 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001946
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001947 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1948 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1949 appctx->st0 = STAT_CLI_PRINT;
1950 return 1;
1951 }
1952
1953 sv->check.state |= CHK_ST_ENABLED;
1954 return 1;
1955 }
1956 else if (strcmp(args[1], "server") == 0) {
1957 struct server *sv;
1958
1959 sv = expect_server_admin(s, si, args[2]);
1960 if (!sv)
1961 return 1;
1962
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001963 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001964 return 1;
1965 }
1966 else if (strcmp(args[1], "frontend") == 0) {
1967 struct proxy *px;
1968
1969 px = expect_frontend_admin(s, si, args[2]);
1970 if (!px)
1971 return 1;
1972
1973 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001974 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1975 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001976 return 1;
1977 }
1978
1979 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001980 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1981 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001982 return 1;
1983 }
1984
1985 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001986 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1987 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001988 return 1;
1989 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001990 return 1;
1991 }
1992 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001993 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001994 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001995 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001996 }
1997 }
1998 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001999 if (strcmp(args[1], "agent") == 0) {
2000 struct server *sv;
2001
2002 sv = expect_server_admin(s, si, args[2]);
2003 if (!sv)
2004 return 1;
2005
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002006 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002007 return 1;
2008 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002009 else if (strcmp(args[1], "health") == 0) {
2010 struct server *sv;
2011
2012 sv = expect_server_admin(s, si, args[2]);
2013 if (!sv)
2014 return 1;
2015
2016 sv->check.state &= ~CHK_ST_ENABLED;
2017 return 1;
2018 }
Simon Horman671b6f02013-11-25 10:46:39 +09002019 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002020 struct server *sv;
2021
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002022 sv = expect_server_admin(s, si, args[2]);
2023 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002024 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002025
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002026 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002027 return 1;
2028 }
2029 else if (strcmp(args[1], "frontend") == 0) {
2030 struct proxy *px;
2031
2032 px = expect_frontend_admin(s, si, args[2]);
2033 if (!px)
2034 return 1;
2035
2036 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002037 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2038 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002039 return 1;
2040 }
2041
2042 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002043 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2044 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002045 return 1;
2046 }
2047
2048 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002049 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2050 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002051 return 1;
2052 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002053 return 1;
2054 }
2055 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002056 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002057 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002058 return 1;
2059 }
2060 }
2061 else if (strcmp(args[0], "shutdown") == 0) {
2062 if (strcmp(args[1], "frontend") == 0) {
2063 struct proxy *px;
2064
2065 px = expect_frontend_admin(s, si, args[2]);
2066 if (!px)
2067 return 1;
2068
2069 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002070 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2071 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002072 return 1;
2073 }
2074
2075 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2076 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2077 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2078 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2079 stop_proxy(px);
2080 return 1;
2081 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002082 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002083 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002084
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002085 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002086 appctx->ctx.cli.msg = stats_permission_denied_msg;
2087 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002088 return 1;
2089 }
2090
2091 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002092 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2093 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002094 return 1;
2095 }
2096
2097 ptr = (void *)strtoul(args[2], NULL, 0);
2098
Willy Tarreau87b09662015-04-03 00:22:06 +02002099 /* first, look for the requested stream in the stream table */
2100 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002101 if (sess == ptr)
2102 break;
2103 }
2104
Willy Tarreau87b09662015-04-03 00:22:06 +02002105 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002106 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002107 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2108 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002109 return 1;
2110 }
2111
Willy Tarreaue7dff022015-04-03 01:14:29 +02002112 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002113 return 1;
2114 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002115 else if (strcmp(args[1], "sessions") == 0) {
2116 if (strcmp(args[2], "server") == 0) {
2117 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002118 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002119
2120 sv = expect_server_admin(s, si, args[3]);
2121 if (!sv)
2122 return 1;
2123
Willy Tarreau87b09662015-04-03 00:22:06 +02002124 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002125 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2126 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002127 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002128
2129 return 1;
2130 }
2131 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002132 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2133 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002134 return 1;
2135 }
2136 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002137 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002138 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2139 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002140 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002141 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002142 }
2143 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002144 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2145 if (args[1][0] == 'm')
2146 appctx->ctx.map.display_flags = PAT_REF_MAP;
2147 else
2148 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002149
2150 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002151 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2152 if (!*args[2] || !*args[3]) {
2153 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2154 appctx->st0 = STAT_CLI_PRINT;
2155 return 1;
2156 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002157 }
2158
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002159 else {
2160 if (!*args[2] || !*args[3]) {
2161 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2162 appctx->st0 = STAT_CLI_PRINT;
2163 return 1;
2164 }
2165 }
2166
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002167 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002168 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002169 if (!appctx->ctx.map.ref ||
2170 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002171 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002172 appctx->st0 = STAT_CLI_PRINT;
2173 return 1;
2174 }
2175
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002176 /* If the entry identifier start with a '#', it is considered as
2177 * pointer id
2178 */
2179 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2180 struct pat_ref_elt *ref;
2181 long long int conv;
2182 char *error;
2183
2184 /* Convert argument to integer value. */
2185 conv = strtoll(&args[3][1], &error, 16);
2186 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002187 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002188 appctx->st0 = STAT_CLI_PRINT;
2189 return 1;
2190 }
2191
2192 /* Convert and check integer to pointer. */
2193 ref = (struct pat_ref_elt *)(long)conv;
2194 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002195 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002196 appctx->st0 = STAT_CLI_PRINT;
2197 return 1;
2198 }
2199
2200 /* Try to delete the entry. */
2201 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2202 /* The entry is not found, send message. */
2203 appctx->ctx.cli.msg = "Key not found.\n";
2204 appctx->st0 = STAT_CLI_PRINT;
2205 return 1;
2206 }
2207 }
2208 else {
2209 /* Else, use the entry identifier as pattern
2210 * string and try to delete the entry.
2211 */
2212 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2213 /* The entry is not found, send message. */
2214 appctx->ctx.cli.msg = "Key not found.\n";
2215 appctx->st0 = STAT_CLI_PRINT;
2216 return 1;
2217 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002218 }
2219
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002220 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002221 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002222 return 1;
2223 }
2224 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002225 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002226 appctx->st0 = STAT_CLI_PRINT;
2227 return 1;
2228 }
2229 }
2230 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002231 if (strcmp(args[1], "map") == 0 ||
2232 strcmp(args[1], "acl") == 0) {
2233 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002234 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002235
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002236 /* Set flags. */
2237 if (args[1][0] == 'm')
2238 appctx->ctx.map.display_flags = PAT_REF_MAP;
2239 else
2240 appctx->ctx.map.display_flags = PAT_REF_ACL;
2241
2242 /* If the keywork is "map", we expect three parameters, if it
2243 * is "acl", we expect only two parameters
2244 */
2245 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2246 if (!*args[2] || !*args[3] || !*args[4]) {
2247 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2248 appctx->st0 = STAT_CLI_PRINT;
2249 return 1;
2250 }
2251 }
2252 else {
2253 if (!*args[2] || !*args[3]) {
2254 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2255 appctx->st0 = STAT_CLI_PRINT;
2256 return 1;
2257 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002258 }
2259
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002260 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002261 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002262 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002263 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002264 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002265 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002266 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002267 appctx->st0 = STAT_CLI_PRINT;
2268 return 1;
2269 }
2270
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002271 /* The command "add acl" is prohibited if the reference
2272 * use samples.
2273 */
2274 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2275 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2276 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2277 "You must use the command 'add map' to add values.\n";
2278 appctx->st0 = STAT_CLI_PRINT;
2279 return 1;
2280 }
2281
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002282 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002283 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002284 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002285 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002286 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002287 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002288 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002289 if (err)
2290 memprintf(&err, "%s.\n", err);
2291 appctx->ctx.cli.err = err;
2292 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002293 return 1;
2294 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002295
2296 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002297 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002298 return 1;
2299 }
2300 else { /* unknown "del" parameter */
2301 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2302 appctx->st0 = STAT_CLI_PRINT;
2303 return 1;
2304 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002305 }
2306 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002307 return 0;
2308 }
2309 return 1;
2310}
2311
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002312/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002313 * used to processes I/O from/to the stats unix socket. The system relies on a
2314 * state machine handling requests and various responses. We read a request,
2315 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002316 * Then we can read again. The state is stored in appctx->st0 and is one of the
2317 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002318 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002319 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002320static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002321{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002322 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002323 struct channel *req = si_oc(si);
2324 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002325 int reql;
2326 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002327
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002328 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2329 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002330
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002331 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002332 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002333 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002334 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2335 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002336 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002337 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002338 /* Let's close for real now. We just close the request
2339 * side, the conditions below will complete if needed.
2340 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002341 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002342 break;
2343 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002344 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002345 /* ensure we have some output room left in the event we
2346 * would want to return some info right after parsing.
2347 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002348 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002349 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002350 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002351 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002352
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002353 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002354 if (reql <= 0) { /* closed or EOL not found */
2355 if (reql == 0)
2356 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002357 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002358 continue;
2359 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002360
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002361 /* seek for a possible semi-colon. If we find one, we
2362 * replace it with an LF and skip only this part.
2363 */
2364 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002365 if (trash.str[len] == ';') {
2366 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002367 reql = len + 1;
2368 break;
2369 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002370
Willy Tarreau816fc222009-10-04 07:36:58 +02002371 /* now it is time to check that we have a full line,
2372 * remove the trailing \n and possibly \r, then cut the
2373 * line.
2374 */
2375 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002376 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002377 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002378 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002379 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002380
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002381 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002382 len--;
2383
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002384 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002385
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002386 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002387 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002388 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002389 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002390 continue;
2391 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002392 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002393 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002394 else if (strcmp(trash.str, "help") == 0 ||
2395 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002396 appctx->ctx.cli.msg = stats_sock_usage_msg;
2397 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002398 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002399 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002400 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002401 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002402 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002403 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002404 /* if prompt is disabled, print help on empty lines,
2405 * so that the user at least knows how to enable
2406 * prompt and find help.
2407 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002408 appctx->ctx.cli.msg = stats_sock_usage_msg;
2409 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002410 }
2411
2412 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002413 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002414 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002415 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002416 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002417 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau00a37f02015-04-13 12:05:19 +02002418 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002419 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002420 continue;
2421 }
2422
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002423 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002424 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002425 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002426 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002427 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002428 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002429 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002430 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002431 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002432 free(appctx->ctx.cli.err);
2433 appctx->st0 = STAT_CLI_PROMPT;
2434 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002435 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002436 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002437 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002438 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002439 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002440 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002441 break;
2442 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002443 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002444 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002445 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002446 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002447 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002448 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002449 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002450 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002451 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002452 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002453 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002454 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002455 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002456 if (stats_table_request(si, appctx->st0))
2457 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002458 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002459 case STAT_CLI_O_PATS:
2460 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002461 appctx->st0 = STAT_CLI_PROMPT;
2462 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002463 case STAT_CLI_O_PAT:
2464 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002465 appctx->st0 = STAT_CLI_PROMPT;
2466 break;
2467 case STAT_CLI_O_MLOOK:
2468 if (stats_map_lookup(si))
2469 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002470 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002471 case STAT_CLI_O_POOLS:
2472 if (stats_dump_pools_to_buffer(si))
2473 appctx->st0 = STAT_CLI_PROMPT;
2474 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002475#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2476 case STAT_CLI_O_TLSK:
2477 if (stats_tlskeys_list(si))
2478 appctx->st0 = STAT_CLI_PROMPT;
2479 break;
2480#endif
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002481 default: /* abnormal state */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002482 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002483 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002484 break;
2485 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002486
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002487 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002488 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002489 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002490 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002491 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002492 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002493 }
2494
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002495 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002496 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002497 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002498
2499 /* Now we close the output if one of the writers did so,
2500 * or if we're not in interactive mode and the request
2501 * buffer is empty. This still allows pipelined requests
2502 * to be sent in non-interactive mode.
2503 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002504 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2505 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002506 continue;
2507 }
2508
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002509 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002510 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002511 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002512 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002513
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002514 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002515 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2516 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002517 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002518 * and nothing more to consume. This is comparable to a broken pipe, so
2519 * we forward the close to the request side so that it flows upstream to
2520 * the client.
2521 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002522 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002523 }
2524
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002525 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002526 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2527 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2528 /* We have no more processing to do, and nothing more to send, and
2529 * the client side has closed. So we'll forward this state downstream
2530 * on the response buffer.
2531 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002532 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002533 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002534 }
2535
Willy Tarreau828824a2015-04-19 17:20:03 +02002536 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002537 DPRINTF(stderr, "%s@%d: st=%d, rqf=%x, rpf=%x, rqh=%d, rqs=%d, rh=%d, rs=%d\n",
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002538 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002539 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002540}
2541
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002542/* This function dumps information onto the stream interface's read buffer.
2543 * It returns 0 as long as it does not complete, non-zero upon completion.
2544 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002545 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002546static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002547{
2548 unsigned int up = (now.tv_sec - start_date.tv_sec);
2549
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002550#ifdef USE_OPENSSL
2551 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2552 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2553 int ssl_reuse = 0;
2554
2555 if (ssl_key_rate < ssl_sess_rate) {
2556 /* count the ssl reuse ratio and avoid overflows in both directions */
2557 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2558 }
2559#endif
2560
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002561 chunk_printf(&trash,
2562 "Name: " PRODUCT_NAME "\n"
2563 "Version: " HAPROXY_VERSION "\n"
2564 "Release_date: " HAPROXY_DATE "\n"
2565 "Nbproc: %d\n"
2566 "Process_num: %d\n"
2567 "Pid: %d\n"
2568 "Uptime: %dd %dh%02dm%02ds\n"
2569 "Uptime_sec: %d\n"
2570 "Memmax_MB: %d\n"
2571 "Ulimit-n: %d\n"
2572 "Maxsock: %d\n"
2573 "Maxconn: %d\n"
2574 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002575 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002576 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002577 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002578#ifdef USE_OPENSSL
2579 "MaxSslConns: %d\n"
2580 "CurrSslConns: %d\n"
2581 "CumSslConns: %d\n"
2582#endif
2583 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002584 "PipesUsed: %d\n"
2585 "PipesFree: %d\n"
2586 "ConnRate: %d\n"
2587 "ConnRateLimit: %d\n"
2588 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002589 "SessRate: %d\n"
2590 "SessRateLimit: %d\n"
2591 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002592#ifdef USE_OPENSSL
2593 "SslRate: %d\n"
2594 "SslRateLimit: %d\n"
2595 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002596 "SslFrontendKeyRate: %d\n"
2597 "SslFrontendMaxKeyRate: %d\n"
2598 "SslFrontendSessionReuse_pct: %d\n"
2599 "SslBackendKeyRate: %d\n"
2600 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002601 "SslCacheLookups: %u\n"
2602 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002603#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002604 "CompressBpsIn: %u\n"
2605 "CompressBpsOut: %u\n"
2606 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002607#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002608 "ZlibMemUsage: %ld\n"
2609 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002610#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002611 "Tasks: %d\n"
2612 "Run_queue: %d\n"
2613 "Idle_pct: %d\n"
2614 "node: %s\n"
2615 "description: %s\n"
2616 "",
2617 global.nbproc,
2618 relative_pid,
2619 pid,
2620 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2621 up,
2622 global.rlimit_memmax,
2623 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002624 global.maxsock, global.maxconn, global.hardmaxconn,
2625 actconn, totalconn, global.req_count,
2626#ifdef USE_OPENSSL
2627 global.maxsslconn, sslconns, totalsslconns,
2628#endif
2629 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002630 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002631 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002632#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002633 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2634 ssl_key_rate, global.ssl_fe_keys_max,
2635 ssl_reuse,
2636 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002637 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002638#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002639 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2640 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002641#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002642 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002643#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002644 nb_tasks_cur, run_queue_cur, idle_pct,
2645 global.node, global.desc ? global.desc : ""
2646 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002647
Willy Tarreaubc18da12015-03-13 14:00:47 +01002648 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002649 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002650 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002651 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002652
2653 return 1;
2654}
2655
Willy Tarreau12833bb2014-01-28 16:49:56 +01002656/* This function dumps memory usage information onto the stream interface's
2657 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2658 * completion. No state is used.
2659 */
2660static int stats_dump_pools_to_buffer(struct stream_interface *si)
2661{
2662 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01002663 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002664 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01002665 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002666 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01002667 return 1;
2668}
2669
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002670/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2671 * the state from stream interface <si>. The caller is responsible for clearing
2672 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002673 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002674static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002675{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002676 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002677 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002678
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002679 if (!(px->cap & PR_CAP_FE))
2680 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002681
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002682 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002683 return 0;
2684
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002685 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002686 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002687 /* name, queue */
2688 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002689
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002690 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002691 /* Column sub-heading for Enable or Disable server */
2692 chunk_appendf(&trash, "<td></td>");
2693 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002694
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002695 chunk_appendf(&trash,
2696 "<td class=ac>"
2697 "<a name=\"%s/Frontend\"></a>"
2698 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2699 "<td colspan=3></td>"
2700 "",
2701 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002702
Willy Tarreau466c9b52012-12-23 02:25:03 +01002703 chunk_appendf(&trash,
2704 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002705 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002706 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2707 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2708 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002709 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2710 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2711 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002712
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002713 if (px->mode == PR_MODE_HTTP)
2714 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002715 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002716 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002717
2718 chunk_appendf(&trash,
2719 "</table></div></u></td>"
2720 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002721 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002722 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2723 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2724 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002725 U2H(px->fe_counters.sps_max),
2726 U2H(px->fe_counters.cps_max),
2727 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002728
2729 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002730 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002731 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002732 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002733
2734 chunk_appendf(&trash,
2735 "</table></div></u></td>"
2736 /* sessions rate : limit */
2737 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002738 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002739
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002740 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002741 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002742 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002743 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002744 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2745 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002746 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002747 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2748 U2H(px->fe_counters.cum_sess),
2749 U2H(px->fe_counters.cum_conn),
2750 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002751
Willy Tarreau466c9b52012-12-23 02:25:03 +01002752 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002753 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002754 chunk_appendf(&trash,
2755 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2756 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2757 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2758 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2759 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2760 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2761 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2762 "<tr><th>- other responses:</th><td>%s</td></tr>"
2763 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2764 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002765 U2H(px->fe_counters.p.http.cum_req),
2766 U2H(px->fe_counters.p.http.rsp[1]),
2767 U2H(px->fe_counters.p.http.rsp[2]),
2768 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002769 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002770 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002771 U2H(px->fe_counters.p.http.rsp[3]),
2772 U2H(px->fe_counters.p.http.rsp[4]),
2773 U2H(px->fe_counters.p.http.rsp[5]),
2774 U2H(px->fe_counters.p.http.rsp[0]),
2775 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002776 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002777
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002778 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002779 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002780 /* sessions: lbtot, lastsess */
2781 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002782 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002783 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002784 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002785 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002786
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002787 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002788 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002789 "<td>%s%s<div class=tips>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002790 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002791 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002792 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2793 px->fe_counters.comp_in ?
2794 (int)((px->fe_counters.comp_in - px->fe_counters.comp_out)*100/px->fe_counters.comp_in) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002795 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002796
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002797 chunk_appendf(&trash,
2798 /* denied: req, resp */
2799 "<td>%s</td><td>%s</td>"
2800 /* errors : request, connect, response */
2801 "<td>%s</td><td></td><td></td>"
2802 /* warnings: retries, redispatches */
2803 "<td></td><td></td>"
2804 /* server status : reflect frontend status */
2805 "<td class=ac>%s</td>"
2806 /* rest of server: nothing */
2807 "<td class=ac colspan=8></td></tr>"
2808 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002809 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2810 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002811 px->state == PR_STREADY ? "OPEN" :
2812 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002813 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002814 else { /* CSV mode */
2815 chunk_appendf(&trash,
2816 /* pxid, name, queue cur, queue max, */
2817 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002818 /* sessions : current, max, limit, total */
2819 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002820 /* bytes : in, out */
2821 "%lld,%lld,"
2822 /* denied: req, resp */
2823 "%lld,%lld,"
2824 /* errors : request, connect, response */
2825 "%lld,,,"
2826 /* warnings: retries, redispatches */
2827 ",,"
2828 /* server status : reflect frontend status */
2829 "%s,"
2830 /* rest of server: nothing */
2831 ",,,,,,,,"
2832 /* pid, iid, sid, throttle, lbtot, tracked, type */
2833 "%d,%d,0,,,,%d,"
2834 /* rate, rate_lim, rate_max */
2835 "%u,%u,%u,"
2836 /* check_status, check_code, check_duration */
2837 ",,,",
2838 px->id,
2839 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2840 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2841 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2842 px->fe_counters.failed_req,
2843 px->state == PR_STREADY ? "OPEN" :
2844 px->state == PR_STFULL ? "FULL" : "STOP",
2845 relative_pid, px->uuid, STATS_TYPE_FE,
2846 read_freq_ctr(&px->fe_sess_per_sec),
2847 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002848
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002849 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2850 if (px->mode == PR_MODE_HTTP) {
2851 for (i=1; i<6; i++)
2852 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2853 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2854 }
2855 else
2856 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002857
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002858 /* failed health analyses */
2859 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002860
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002861 /* requests : req_rate, req_rate_max, req_tot, */
2862 chunk_appendf(&trash, "%u,%u,%lld,",
2863 read_freq_ctr(&px->fe_req_per_sec),
2864 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2865
2866 /* errors: cli_aborts, srv_aborts */
2867 chunk_appendf(&trash, ",,");
2868
2869 /* compression: in, out, bypassed */
2870 chunk_appendf(&trash, "%lld,%lld,%lld,",
2871 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2872
2873 /* compression: comp_rsp */
2874 chunk_appendf(&trash, "%lld,",
2875 px->fe_counters.p.http.comp_rsp);
2876
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002877 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2878 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002879
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002880 /* finish with EOL */
2881 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002882 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002883 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002884}
2885
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002886/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2887 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2888 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2889 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002890 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002891static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002892{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002893 struct appctx *appctx = __objt_appctx(si->end);
2894
2895 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002896 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002897 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002898 /* Column sub-heading for Enable or Disable server */
2899 chunk_appendf(&trash, "<td></td>");
2900 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002901 chunk_appendf(&trash,
2902 /* frontend name, listener name */
2903 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2904 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2905 "",
2906 px->id, l->name,
2907 (flags & ST_SHLGNDS)?"<u>":"",
2908 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002909
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002910 if (flags & ST_SHLGNDS) {
2911 char str[INET6_ADDRSTRLEN];
2912 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002913
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002914 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002915
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002916 port = get_host_port(&l->addr);
2917 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2918 case AF_INET:
2919 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2920 break;
2921 case AF_INET6:
2922 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2923 break;
2924 case AF_UNIX:
2925 chunk_appendf(&trash, "unix, ");
2926 break;
2927 case -1:
2928 chunk_appendf(&trash, "(%s), ", strerror(errno));
2929 break;
2930 }
Willy Tarreau91861262007-10-17 17:06:05 +02002931
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002932 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002933 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002934 }
Willy Tarreau91861262007-10-17 17:06:05 +02002935
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002936 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002937 /* queue */
2938 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002939 /* sessions rate: current, max, limit */
2940 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002941 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002942 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002943 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002944 /* bytes: in, out */
2945 "<td>%s</td><td>%s</td>"
2946 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002947 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002948 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2949 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002950
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002951 chunk_appendf(&trash,
2952 /* denied: req, resp */
2953 "<td>%s</td><td>%s</td>"
2954 /* errors: request, connect, response */
2955 "<td>%s</td><td></td><td></td>"
2956 /* warnings: retries, redispatches */
2957 "<td></td><td></td>"
2958 /* server status: reflect listener status */
2959 "<td class=ac>%s</td>"
2960 /* rest of server: nothing */
2961 "<td class=ac colspan=8></td></tr>"
2962 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002963 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2964 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002965 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2966 }
2967 else { /* CSV mode */
2968 chunk_appendf(&trash,
2969 /* pxid, name, queue cur, queue max, */
2970 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002971 /* sessions: current, max, limit, total */
2972 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002973 /* bytes: in, out */
2974 "%lld,%lld,"
2975 /* denied: req, resp */
2976 "%lld,%lld,"
2977 /* errors: request, connect, response */
2978 "%lld,,,"
2979 /* warnings: retries, redispatches */
2980 ",,"
2981 /* server status: reflect listener status */
2982 "%s,"
2983 /* rest of server: nothing */
2984 ",,,,,,,,"
2985 /* pid, iid, sid, throttle, lbtot, tracked, type */
2986 "%d,%d,%d,,,,%d,"
2987 /* rate, rate_lim, rate_max */
2988 ",,,"
2989 /* check_status, check_code, check_duration */
2990 ",,,"
2991 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2992 ",,,,,,"
2993 /* failed health analyses */
2994 ","
2995 /* requests : req_rate, req_rate_max, req_tot, */
2996 ",,,"
2997 /* errors: cli_aborts, srv_aborts */
2998 ",,"
2999 /* compression: in, out, bypassed, comp_rsp */
3000 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003001 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3002 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003003 "\n",
3004 px->id, l->name,
3005 l->nbconn, l->counters->conn_max,
3006 l->maxconn, l->counters->cum_conn,
3007 l->counters->bytes_in, l->counters->bytes_out,
3008 l->counters->denied_req, l->counters->denied_resp,
3009 l->counters->failed_req,
3010 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
3011 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
3012 }
3013 return 1;
3014}
Willy Tarreau91861262007-10-17 17:06:05 +02003015
Simon Horman4d2eab62015-04-23 14:51:26 +09003016enum srv_stats_state {
3017 SRV_STATS_STATE_DOWN = 0,
3018 SRV_STATS_STATE_DOWN_AGENT,
3019 SRV_STATS_STATE_GOING_UP,
3020 SRV_STATS_STATE_UP_GOING_DOWN,
3021 SRV_STATS_STATE_UP,
3022 SRV_STATS_STATE_NOLB_GOING_DOWN,
3023 SRV_STATS_STATE_NOLB,
3024 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3025 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003026 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003027 SRV_STATS_STATE_NO_CHECK,
3028
3029 SRV_STATS_STATE_COUNT, /* Must be last */
3030};
3031
Simon Horman837bfa72015-04-23 14:51:27 +09003032enum srv_stats_colour {
3033 SRV_STATS_COLOUR_DOWN = 0,
3034 SRV_STATS_COLOUR_GOING_UP,
3035 SRV_STATS_COLOUR_GOING_DOWN,
3036 SRV_STATS_COLOUR_UP,
3037 SRV_STATS_COLOUR_NOLB,
3038 SRV_STATS_COLOUR_DRAINING,
3039 SRV_STATS_COLOUR_NO_CHECK,
3040
3041 SRV_STATS_COLOUR_COUNT, /* Must be last */
3042};
3043
3044static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3045 [SRV_STATS_COLOUR_DOWN] = "down",
3046 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3047 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3048 [SRV_STATS_COLOUR_UP] = "up",
3049 [SRV_STATS_COLOUR_NOLB] = "nolb",
3050 [SRV_STATS_COLOUR_DRAINING] = "draining",
3051 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3052};
3053
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003054/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3055 * from stream interface <si>, stats flags <flags>, and server state <state>.
3056 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003057 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003058 */
Simon Horman4d2eab62015-04-23 14:51:26 +09003059static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09003060 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003061{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003062 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003063 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003064 char str[INET6_ADDRSTRLEN];
3065 struct chunk src;
3066 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02003067
Willy Tarreau32091232014-05-16 13:52:00 +02003068 /* we have "via" which is the tracked server as described in the configuration,
3069 * and "ref" which is the checked server and the end of the chain.
3070 */
3071 via = sv->track ? sv->track : sv;
3072 ref = via;
3073 while (ref->track)
3074 ref = ref->track;
3075
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003076 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003077 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3078 [SRV_STATS_STATE_DOWN] = "DOWN",
3079 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3080 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
3081 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
3082 [SRV_STATS_STATE_UP] = "UP",
3083 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
3084 [SRV_STATS_STATE_NOLB] = "NOLB",
3085 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
3086 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003087 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003088 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003089 };
Willy Tarreau91861262007-10-17 17:06:05 +02003090
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003091 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003092 chunk_appendf(&trash, "<tr class=\"maintain\">");
3093 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003094 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003095 "<tr class=\"%s_%s\">",
3096 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003097
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003098 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003099 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003100 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3101 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003102
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003103 chunk_appendf(&trash,
3104 "<td class=ac><a name=\"%s/%s\"></a>%s"
3105 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3106 "",
3107 px->id, sv->id,
3108 (flags & ST_SHLGNDS) ? "<u>" : "",
3109 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003110
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003111 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003112 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003113
3114 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3115 case AF_INET:
3116 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3117 break;
3118 case AF_INET6:
3119 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3120 break;
3121 case AF_UNIX:
3122 chunk_appendf(&trash, "unix, ");
3123 break;
3124 case -1:
3125 chunk_appendf(&trash, "(%s), ", strerror(errno));
3126 break;
3127 default: /* address family not supported */
3128 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003129 }
Willy Tarreau91861262007-10-17 17:06:05 +02003130
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003131 /* id */
3132 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003133
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003134 /* cookie */
3135 if (sv->cookie) {
3136 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003137
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003138 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3139 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003140
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003141 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003142 }
3143
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003144 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003145 }
Willy Tarreau91861262007-10-17 17:06:05 +02003146
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003147 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003148 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003149 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003150 /* sessions rate : current, max, limit */
3151 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003152 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003153 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003154 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3155 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003156
Willy Tarreau466c9b52012-12-23 02:25:03 +01003157
3158 chunk_appendf(&trash,
3159 /* sessions: current, max, limit, total */
3160 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003161 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003162 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3163 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003164 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3165 U2H(sv->counters.cum_sess),
3166 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003167
Willy Tarreau466c9b52012-12-23 02:25:03 +01003168 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3169 if (px->mode == PR_MODE_HTTP) {
3170 unsigned long long tot;
3171 for (tot = i = 0; i < 6; i++)
3172 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003173
Willy Tarreau466c9b52012-12-23 02:25:03 +01003174 chunk_appendf(&trash,
3175 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3176 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3177 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3178 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3179 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3180 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3181 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3182 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003183 U2H(tot),
3184 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3185 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3186 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3187 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3188 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3189 U2H(sv->counters.p.http.rsp[0]), tot ? (int)(100*sv->counters.p.http.rsp[0] / tot) : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02003190 }
Willy Tarreau91861262007-10-17 17:06:05 +02003191
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003192 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3193 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES)));
3194 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES)));
3195 if (px->mode == PR_MODE_HTTP)
3196 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES)));
3197 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES)));
3198
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003199 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003200 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003201 /* sessions: lbtot, last */
3202 "<td>%s</td><td>%s</td>",
3203 U2H(sv->counters.cum_lbconn),
3204 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003205
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003206 chunk_appendf(&trash,
3207 /* bytes : in, out */
3208 "<td>%s</td><td>%s</td>"
3209 /* denied: req, resp */
3210 "<td></td><td>%s</td>"
3211 /* errors : request, connect */
3212 "<td></td><td>%s</td>"
3213 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003214 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003215 /* warnings: retries, redispatches */
3216 "<td>%lld</td><td>%lld</td>"
3217 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003218 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3219 U2H(sv->counters.failed_secu),
3220 U2H(sv->counters.failed_conns),
3221 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003222 sv->counters.cli_aborts,
3223 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003224 sv->counters.retries, sv->counters.redispatches);
3225
3226 /* status, lest check */
3227 chunk_appendf(&trash, "<td class=ac>");
3228
Willy Tarreau20125212014-05-13 19:44:56 +02003229 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003230 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3231 chunk_appendf(&trash, "MAINT");
3232 }
Simon Horman0766e442014-11-12 15:55:54 +09003233 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003234 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003235 /* DOWN (agent) */
3236 chunk_appendf(&trash, srv_hlt_st[1], "GCC: your -Werror=format-security is bogus, annoying, and hides real bugs, I don't thank you, really!");
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003237 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003238 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003239 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3240 chunk_appendf(&trash,
3241 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003242 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3243 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003244 }
Willy Tarreau91861262007-10-17 17:06:05 +02003245
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003246 if ((sv->state == SRV_ST_STOPPED) &&
3247 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3248 chunk_appendf(&trash,
3249 "</td><td class=ac><u> %s%s",
3250 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3251 get_check_status_info(sv->agent.status));
3252
3253 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3254 chunk_appendf(&trash, "/%d", sv->agent.code);
3255
3256 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3257 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3258
3259 chunk_appendf(&trash, "<div class=tips>%s",
3260 get_check_status_description(sv->agent.status));
3261 if (*sv->agent.desc) {
3262 chunk_appendf(&trash, ": ");
3263 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3264 chunk_htmlencode(&trash, &src);
3265 }
3266 chunk_appendf(&trash, "</div></u>");
3267 }
3268 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003269 chunk_appendf(&trash,
3270 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003271 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003272 get_check_status_info(sv->check.status));
3273
3274 if (sv->check.status >= HCHK_STATUS_L57DATA)
3275 chunk_appendf(&trash, "/%d", sv->check.code);
3276
3277 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003278 chunk_appendf(&trash, " in %lums", sv->check.duration);
3279
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003280 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003281 get_check_status_description(sv->check.status));
3282 if (*sv->check.desc) {
3283 chunk_appendf(&trash, ": ");
3284 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3285 chunk_htmlencode(&trash, &src);
3286 }
3287 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003288 }
3289 else
3290 chunk_appendf(&trash, "</td><td>");
3291
3292 chunk_appendf(&trash,
3293 /* weight */
3294 "</td><td class=ac>%d</td>"
3295 /* act, bck */
3296 "<td class=ac>%s</td><td class=ac>%s</td>"
3297 "",
3298 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003299 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3300 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003301
3302 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003303 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003304 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003305
3306 if (ref->observe)
3307 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3308
3309 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003310 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003311 "<td>%lld</td><td>%s</td>"
3312 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003313 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003314 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3315 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003316 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3317 /* tracking a server */
3318 chunk_appendf(&trash,
3319 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003320 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003321 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003322 else
3323 chunk_appendf(&trash, "<td colspan=3></td>");
3324
3325 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003326 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003327 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003328 else
3329 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3330 }
3331 else { /* CSV mode */
Simon Horman4d2eab62015-04-23 14:51:26 +09003332 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3333 [SRV_STATS_STATE_DOWN] = "DOWN,",
3334 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3335 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3336 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3337 [SRV_STATS_STATE_UP] = "UP,",
3338 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3339 [SRV_STATS_STATE_NOLB] = "NOLB,",
3340 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3341 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003342 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003343 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003344 };
3345
3346 chunk_appendf(&trash,
3347 /* pxid, name */
3348 "%s,%s,"
3349 /* queue : current, max */
3350 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003351 /* sessions : current, max, limit, total */
3352 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003353 /* bytes : in, out */
3354 "%lld,%lld,"
3355 /* denied: req, resp */
3356 ",%lld,"
3357 /* errors : request, connect, response */
3358 ",%lld,%lld,"
3359 /* warnings: retries, redispatches */
3360 "%lld,%lld,"
3361 "",
3362 px->id, sv->id,
3363 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003364 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003365 sv->counters.bytes_in, sv->counters.bytes_out,
3366 sv->counters.failed_secu,
3367 sv->counters.failed_conns, sv->counters.failed_resp,
3368 sv->counters.retries, sv->counters.redispatches);
3369
3370 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003371 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003372 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003373 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003374 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003375 else
3376 chunk_appendf(&trash,
3377 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003378 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3379 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003380
3381 chunk_appendf(&trash,
3382 /* weight, active, backup */
3383 "%d,%d,%d,"
3384 "",
3385 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003386 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3387 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003388
3389 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003390 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003391 chunk_appendf(&trash,
3392 "%lld,%lld,%d,%d,",
3393 sv->counters.failed_checks, sv->counters.down_trans,
3394 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3395 else
3396 chunk_appendf(&trash, ",,,,");
3397
3398 /* queue limit, pid, iid, sid, */
3399 chunk_appendf(&trash,
3400 "%s,"
3401 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003402 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 relative_pid, px->uuid, sv->puid);
3404
3405 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003406 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003407 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003408
3409 /* sessions: lbtot */
3410 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3411
3412 /* tracked */
3413 if (sv->track)
3414 chunk_appendf(&trash, "%s/%s,",
3415 sv->track->proxy->id, sv->track->id);
3416 else
3417 chunk_appendf(&trash, ",");
3418
3419 /* type */
3420 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3421
3422 /* rate */
3423 chunk_appendf(&trash, "%u,,%u,",
3424 read_freq_ctr(&sv->sess_per_sec),
3425 sv->counters.sps_max);
3426
Willy Tarreauff5ae352013-12-11 20:36:34 +01003427 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 /* check_status */
3429 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3430
3431 /* check_code */
3432 if (sv->check.status >= HCHK_STATUS_L57DATA)
3433 chunk_appendf(&trash, "%u,", sv->check.code);
3434 else
3435 chunk_appendf(&trash, ",");
3436
3437 /* check_duration */
3438 if (sv->check.status >= HCHK_STATUS_CHECKED)
3439 chunk_appendf(&trash, "%lu,", sv->check.duration);
3440 else
3441 chunk_appendf(&trash, ",");
3442
3443 }
3444 else
3445 chunk_appendf(&trash, ",,,");
3446
3447 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3448 if (px->mode == PR_MODE_HTTP) {
3449 for (i=1; i<6; i++)
3450 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3451
3452 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3453 }
3454 else
3455 chunk_appendf(&trash, ",,,,,,");
3456
3457 /* failed health analyses */
3458 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3459
3460 /* requests : req_rate, req_rate_max, req_tot, */
3461 chunk_appendf(&trash, ",,,");
3462
3463 /* errors: cli_aborts, srv_aborts */
3464 chunk_appendf(&trash, "%lld,%lld,",
3465 sv->counters.cli_aborts, sv->counters.srv_aborts);
3466
3467 /* compression: in, out, bypassed, comp_rsp */
3468 chunk_appendf(&trash, ",,,,");
3469
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003470 /* lastsess */
3471 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3472
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003473 /* capture of last check and agent statuses */
3474 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->check.desc) : "");
3475 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->agent.desc) : "");
3476
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003477 /* qtime, ctime, rtime, ttime, */
3478 chunk_appendf(&trash, "%u,%u,%u,%u,",
3479 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3480 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3481 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3482 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3483
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003484 /* finish with EOL */
3485 chunk_appendf(&trash, "\n");
3486 }
3487 return 1;
3488}
3489
3490/* Dumps a line for backend <px> to the trash for and uses the state from stream
3491 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3492 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3493 */
3494static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3495{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003496 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003497 struct chunk src;
3498 int i;
3499
3500 if (!(px->cap & PR_CAP_BE))
3501 return 0;
3502
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003503 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003504 return 0;
3505
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003506 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003507 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003508 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003509 /* Column sub-heading for Enable or Disable server */
3510 chunk_appendf(&trash, "<td></td>");
3511 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003512 chunk_appendf(&trash,
3513 "<td class=ac>"
3514 /* name */
3515 "%s<a name=\"%s/Backend\"></a>"
3516 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3517 "",
3518 (flags & ST_SHLGNDS)?"<u>":"",
3519 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003520
3521 if (flags & ST_SHLGNDS) {
3522 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003523 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003524 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3525
3526 /* cookie */
3527 if (px->cookie_name) {
3528 chunk_appendf(&trash, ", cookie: '");
3529 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3530 chunk_htmlencode(&trash, &src);
3531 chunk_appendf(&trash, "'");
3532 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003533 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003534 }
3535
3536 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003537 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003538 /* queue : current, max */
3539 "<td>%s</td><td>%s</td><td></td>"
3540 /* sessions rate : current, max, limit */
3541 "<td>%s</td><td>%s</td><td></td>"
3542 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003543 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003544 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3545 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003546
3547 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003548 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003549 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003550 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003551 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003552 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003553 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3554 U2H(px->be_counters.cum_conn),
3555 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003556
Willy Tarreau466c9b52012-12-23 02:25:03 +01003557 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003558 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003559 chunk_appendf(&trash,
3560 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3561 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3562 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3563 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3564 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3565 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3566 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3567 "<tr><th>- other responses:</th><td>%s</td></tr>"
3568 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003569 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003570 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003571 U2H(px->be_counters.p.http.cum_req),
3572 U2H(px->be_counters.p.http.rsp[1]),
3573 U2H(px->be_counters.p.http.rsp[2]),
3574 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003575 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003576 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003577 U2H(px->be_counters.p.http.rsp[3]),
3578 U2H(px->be_counters.p.http.rsp[4]),
3579 U2H(px->be_counters.p.http.rsp[5]),
3580 U2H(px->be_counters.p.http.rsp[0]),
3581 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003582 }
3583
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003584 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)));
3585 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)));
3586 if (px->mode == PR_MODE_HTTP)
3587 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)));
3588 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)));
3589
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003590 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003591 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003592 /* sessions: lbtot, last */
3593 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003594 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003595 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003596 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003597 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003598 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003599 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003600
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003601 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003602 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003603 "<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 +01003604 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003605 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003606 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3607 px->be_counters.comp_in ?
3608 (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 +01003609 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3610
3611 chunk_appendf(&trash,
3612 /* denied: req, resp */
3613 "<td>%s</td><td>%s</td>"
3614 /* errors : request, connect */
3615 "<td></td><td>%s</td>"
3616 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003617 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003618 /* warnings: retries, redispatches */
3619 "<td>%lld</td><td>%lld</td>"
3620 /* backend status: reflect backend status (up/down): we display UP
3621 * if the backend has known working servers or if it has no server at
3622 * all (eg: for stats). Then we display the total weight, number of
3623 * active and backups. */
3624 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3625 "<td class=ac>%d</td><td class=ac>%d</td>"
3626 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003627 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3628 U2H(px->be_counters.failed_conns),
3629 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003630 px->be_counters.cli_aborts,
3631 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003632 px->be_counters.retries, px->be_counters.redispatches,
3633 human_time(now.tv_sec - px->last_change, 1),
3634 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3635 "<font color=\"red\"><b>DOWN</b></font>",
3636 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3637 px->srv_act, px->srv_bck);
3638
3639 chunk_appendf(&trash,
3640 /* rest of backend: nothing, down transitions, total downtime, throttle */
3641 "<td class=ac>&nbsp;</td><td>%d</td>"
3642 "<td>%s</td>"
3643 "<td></td>"
3644 "</tr>",
3645 px->down_trans,
3646 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3647 }
3648 else { /* CSV mode */
3649 chunk_appendf(&trash,
3650 /* pxid, name */
3651 "%s,BACKEND,"
3652 /* queue : current, max */
3653 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003654 /* sessions : current, max, limit, total */
3655 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003656 /* bytes : in, out */
3657 "%lld,%lld,"
3658 /* denied: req, resp */
3659 "%lld,%lld,"
3660 /* errors : request, connect, response */
3661 ",%lld,%lld,"
3662 /* warnings: retries, redispatches */
3663 "%lld,%lld,"
3664 /* backend status: reflect backend status (up/down): we display UP
3665 * if the backend has known working servers or if it has no server at
3666 * all (eg: for stats). Then we display the total weight, number of
3667 * active and backups. */
3668 "%s,"
3669 "%d,%d,%d,"
3670 /* rest of backend: nothing, down transitions, last change, total downtime */
3671 ",%d,%d,%d,,"
3672 /* pid, iid, sid, throttle, lbtot, tracked, type */
3673 "%d,%d,0,,%lld,,%d,"
3674 /* rate, rate_lim, rate_max, */
3675 "%u,,%u,"
3676 /* check_status, check_code, check_duration */
3677 ",,,",
3678 px->id,
3679 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3680 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3681 px->be_counters.bytes_in, px->be_counters.bytes_out,
3682 px->be_counters.denied_req, px->be_counters.denied_resp,
3683 px->be_counters.failed_conns, px->be_counters.failed_resp,
3684 px->be_counters.retries, px->be_counters.redispatches,
3685 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3686 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3687 px->srv_act, px->srv_bck,
3688 px->down_trans, (int)(now.tv_sec - px->last_change),
3689 px->srv?be_downtime(px):0,
3690 relative_pid, px->uuid,
3691 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3692 read_freq_ctr(&px->be_sess_per_sec),
3693 px->be_counters.sps_max);
3694
3695 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3696 if (px->mode == PR_MODE_HTTP) {
3697 for (i=1; i<6; i++)
3698 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3699 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3700 }
3701 else
3702 chunk_appendf(&trash, ",,,,,,");
3703
3704 /* failed health analyses */
3705 chunk_appendf(&trash, ",");
3706
3707 /* requests : req_rate, req_rate_max, req_tot, */
3708 chunk_appendf(&trash, ",,,");
3709
3710 /* errors: cli_aborts, srv_aborts */
3711 chunk_appendf(&trash, "%lld,%lld,",
3712 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3713
3714 /* compression: in, out, bypassed */
3715 chunk_appendf(&trash, "%lld,%lld,%lld,",
3716 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3717
3718 /* compression: comp_rsp */
3719 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3720
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003721 /* lastsess, last_chk, last_agt, */
3722 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003723
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003724 /* qtime, ctime, rtime, ttime, */
3725 chunk_appendf(&trash, "%u,%u,%u,%u,",
3726 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3727 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3728 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3729 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3730
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003731 /* finish with EOL */
3732 chunk_appendf(&trash, "\n");
3733 }
3734 return 1;
3735}
3736
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003737/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003738 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003739 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003740 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003741static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003742{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003743 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003744 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3745
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003746 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003747 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003748
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003749 /* 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 +02003750 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003751 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003752 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003753 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 +01003754 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003755 }
3756
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003757 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05003758 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003759 }
3760
3761 /* print a new table */
3762 chunk_appendf(&trash,
3763 "<table class=\"tbl\" width=\"100%%\">\n"
3764 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003765 "<th class=\"pxname\" width=\"10%%\">");
3766
3767 chunk_appendf(&trash,
3768 "<a name=\"%s\"></a>%s"
3769 "<a class=px href=\"#%s\">%s</a>",
3770 px->id,
3771 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3772 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003773
3774 if (uri->flags & ST_SHLGNDS) {
3775 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003776 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003777 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3778 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003779 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003780 }
3781
3782 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003783 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003784 "<th class=\"%s\" width=\"90%%\">%s</th>"
3785 "</tr>\n"
3786 "</table>\n"
3787 "<table class=\"tbl\" width=\"100%%\">\n"
3788 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003789 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3790 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3791
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003792 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003793 /* Column heading for Enable or Disable server */
3794 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003795 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003796
3797 chunk_appendf(&trash,
3798 "<th rowspan=2></th>"
3799 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003800 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003801 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3802 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3803 "<th colspan=9>Server</th>"
3804 "</tr>\n"
3805 "<tr class=\"titre\">"
3806 "<th>Cur</th><th>Max</th><th>Limit</th>"
3807 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003808 "<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 +01003809 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3810 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3811 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3812 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3813 "<th>Thrtle</th>\n"
3814 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003815}
3816
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003817/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003818 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003819 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003820static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003821{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003822 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003823 chunk_appendf(&trash, "</table>");
3824
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003825 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003826 /* close the form used to enable/disable this proxy servers */
3827 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003828 "Choose the action to perform on the checked servers : "
3829 "<select name=action>"
3830 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003831 "<option value=\"ready\">Set state to READY</option>"
3832 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02003833 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003834 "<option value=\"dhlth\">Health: disable checks</option>"
3835 "<option value=\"ehlth\">Health: enable checks</option>"
3836 "<option value=\"hrunn\">Health: force UP</option>"
3837 "<option value=\"hnolb\">Health: force NOLB</option>"
3838 "<option value=\"hdown\">Health: force DOWN</option>"
3839 "<option value=\"dagent\">Agent: disable checks</option>"
3840 "<option value=\"eagent\">Agent: enable checks</option>"
3841 "<option value=\"arunn\">Agent: force UP</option>"
3842 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003843 "<option value=\"shutdown\">Kill Sessions</option>"
3844 "</select>"
3845 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3846 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3847 "</form>",
3848 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003849 }
3850
3851 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003852}
Willy Tarreau91861262007-10-17 17:06:05 +02003853
3854/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003855 * Dumps statistics for a proxy. The output is sent to the stream interface's
3856 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3857 * buffer space, or non-zero if everything completed. This function is used
3858 * both by the CLI and the HTTP entry points, and is able to dump the output
3859 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003860 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003861static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003862{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003863 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02003864 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003865 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02003866 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003867 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003868
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003869 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003870
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003871 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003872 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003873 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003874 if (uri && uri->scope) {
3875 /* we have a limited scope, we have to check the proxy name */
3876 struct stat_scope *scope;
3877 int len;
3878
3879 len = strlen(px->id);
3880 scope = uri->scope;
3881
3882 while (scope) {
3883 /* match exact proxy name */
3884 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3885 break;
3886
3887 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003888 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003889 break;
3890 scope = scope->next;
3891 }
3892
3893 /* proxy name not found : don't dump anything */
3894 if (scope == NULL)
3895 return 1;
3896 }
3897
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003898 /* if the user has requested a limited output and the proxy
3899 * name does not match, skip it.
3900 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003901 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003902 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 +02003903 return 1;
3904
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003905 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3906 (appctx->ctx.stats.iid != -1) &&
3907 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003908 return 1;
3909
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003910 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003911 /* fall through */
3912
Willy Tarreau295a8372011-03-10 11:25:07 +01003913 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003914 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003915 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003916 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003917 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02003918 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003919 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003920 }
Willy Tarreau91861262007-10-17 17:06:05 +02003921
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003922 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003923 /* fall through */
3924
Willy Tarreau295a8372011-03-10 11:25:07 +01003925 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003926 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003927 if (stats_dump_fe_stats(si, px)) {
3928 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003929 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003930 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003931 }
3932 }
Willy Tarreau91861262007-10-17 17:06:05 +02003933
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003934 appctx->ctx.stats.l = px->conf.listeners.n;
3935 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003936 /* fall through */
3937
Willy Tarreau295a8372011-03-10 11:25:07 +01003938 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003939 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003940 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003941 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003942 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02003943 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003944 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003945
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003946 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003947 if (!l->counters)
3948 continue;
3949
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003950 if (appctx->ctx.stats.flags & STAT_BOUND) {
3951 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003952 break;
3953
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003954 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003955 continue;
3956 }
3957
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003958 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003959 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
3960 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003961 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003962 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003963 }
3964 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003965 }
3966
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003967 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3968 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003969 /* fall through */
3970
Willy Tarreau295a8372011-03-10 11:25:07 +01003971 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003972 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003973 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003974 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09003975 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02003976
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003977 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003978 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02003979 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003980 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003981
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003982 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003983
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003984 if (appctx->ctx.stats.flags & STAT_BOUND) {
3985 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003986 break;
3987
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003988 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003989 continue;
3990 }
3991
Willy Tarreau32091232014-05-16 13:52:00 +02003992 svs = sv;
3993 while (svs->track)
3994 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003995
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003996 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003997 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09003998 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003999 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004000 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4001 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004002 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004003 sv_colour = SRV_STATS_COLOUR_UP;
4004 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004005
Simon Horman4463d192015-04-23 14:51:28 +09004006 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
4007 sv_colour = SRV_STATS_COLOUR_DRAINING;
4008
4009 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09004010 if (svs->agent.state & CHK_ST_ENABLED)
4011 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
4012 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09004013 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09004014 else
Simon Horman4d2eab62015-04-23 14:51:26 +09004015 sv_state = SRV_STATS_STATE_DRAIN;
4016 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004017
Simon Horman837bfa72015-04-23 14:51:27 +09004018 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004019 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09004020 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
4021 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004022 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004023 else if (sv->state == SRV_ST_STOPPING) {
4024 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09004025 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004026 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09004027 sv_colour = SRV_STATS_COLOUR_NOLB;
4028 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004029 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004030 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4031 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004032 }
4033 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09004034 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004035 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09004036 sv_colour = SRV_STATS_COLOUR_DOWN;
4037 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004038 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09004039 sv_colour = SRV_STATS_COLOUR_DOWN;
4040 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004041 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004042 sv_colour = SRV_STATS_COLOUR_GOING_UP;
4043 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004044 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09004045 sv_colour = SRV_STATS_COLOUR_DOWN;
4046 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004047 }
Willy Tarreau91861262007-10-17 17:06:05 +02004048
Willy Tarreau9638efa2014-05-23 11:19:57 +02004049 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02004050 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004051 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02004052 continue;
4053 }
4054
Simon Horman837bfa72015-04-23 14:51:27 +09004055 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004056 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004057 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004058 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004059 }
4060 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004061 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004062
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004063 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004064 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004065
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004066 case STAT_PX_ST_BE:
4067 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004068 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
4069 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004070 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004071 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004072 }
4073 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004074
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004075 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004076 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004077
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004078 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004079 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004080 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004081 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004082 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004083 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004084 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004085 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004086
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004087 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004088 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004089
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004090 case STAT_PX_ST_FIN:
4091 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004092
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004093 default:
4094 /* unknown state, we should put an abort() here ! */
4095 return 1;
4096 }
4097}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004098
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004099/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4100 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004101 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004102static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004103{
4104 /* WARNING! This must fit in the first buffer !!! */
4105 chunk_appendf(&trash,
4106 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4107 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4108 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4109 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4110 "<style type=\"text/css\"><!--\n"
4111 "body {"
4112 " font-family: arial, helvetica, sans-serif;"
4113 " font-size: 12px;"
4114 " font-weight: normal;"
4115 " color: black;"
4116 " background: white;"
4117 "}\n"
4118 "th,td {"
4119 " font-size: 10px;"
4120 "}\n"
4121 "h1 {"
4122 " font-size: x-large;"
4123 " margin-bottom: 0.5em;"
4124 "}\n"
4125 "h2 {"
4126 " font-family: helvetica, arial;"
4127 " font-size: x-large;"
4128 " font-weight: bold;"
4129 " font-style: italic;"
4130 " color: #6020a0;"
4131 " margin-top: 0em;"
4132 " margin-bottom: 0em;"
4133 "}\n"
4134 "h3 {"
4135 " font-family: helvetica, arial;"
4136 " font-size: 16px;"
4137 " font-weight: bold;"
4138 " color: #b00040;"
4139 " background: #e8e8d0;"
4140 " margin-top: 0em;"
4141 " margin-bottom: 0em;"
4142 "}\n"
4143 "li {"
4144 " margin-top: 0.25em;"
4145 " margin-right: 2em;"
4146 "}\n"
4147 ".hr {margin-top: 0.25em;"
4148 " border-color: black;"
4149 " border-bottom-style: solid;"
4150 "}\n"
4151 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4152 ".total {background: #20D0D0;color: #ffff80;}\n"
4153 ".frontend {background: #e8e8d0;}\n"
4154 ".socket {background: #d0d0d0;}\n"
4155 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004156 ".active_down {background: #ff9090;}\n"
4157 ".active_going_up {background: #ffd020;}\n"
4158 ".active_going_down {background: #ffffa0;}\n"
4159 ".active_up {background: #c0ffc0;}\n"
4160 ".active_nolb {background: #20a0ff;}\n"
4161 ".active_draining {background: #20a0FF;}\n"
4162 ".active_no_check {background: #e0e0e0;}\n"
4163 ".backup_down {background: #ff9090;}\n"
4164 ".backup_going_up {background: #ff80ff;}\n"
4165 ".backup_going_down {background: #c060ff;}\n"
4166 ".backup_up {background: #b0d0ff;}\n"
4167 ".backup_nolb {background: #90b0e0;}\n"
4168 ".backup_draining {background: #cc9900;}\n"
4169 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004170 ".maintain {background: #c07820;}\n"
4171 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4172 "\n"
4173 "a.px:link {color: #ffff40; text-decoration: none;}"
4174 "a.px:visited {color: #ffff40; text-decoration: none;}"
4175 "a.px:hover {color: #ffffff; text-decoration: none;}"
4176 "a.lfsb:link {color: #000000; text-decoration: none;}"
4177 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4178 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4179 "\n"
4180 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4181 "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"
4182 "table.tbl td.ac { text-align: center;}\n"
4183 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4184 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4185 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4186 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4187 "\n"
4188 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4189 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4190 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004191 "table.det { border-collapse: collapse; border-style: none; }\n"
4192 "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 +01004193 "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 +01004194 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004195 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004196 " display:block;\n"
4197 " visibility:hidden;\n"
4198 " z-index:2147483647;\n"
4199 " position:absolute;\n"
4200 " padding:2px 4px 3px;\n"
4201 " background:#f0f060; color:#000000;\n"
4202 " border:1px solid #7040c0;\n"
4203 " white-space:nowrap;\n"
4204 " font-style:normal;font-size:11px;font-weight:normal;\n"
4205 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4206 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4207 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004208 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004209 "-->\n"
4210 "</style></head>\n",
4211 (uri->flags & ST_SHNODE) ? " on " : "",
4212 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4213 );
4214}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004215
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004216/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004217 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004218 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004219 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004220static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004221{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004222 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004223 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004224 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004225
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004226 /* WARNING! this has to fit the first packet too.
4227 * We are around 3.5 kB, add adding entries will
4228 * become tricky if we want to support 4kB buffers !
4229 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004230 chunk_appendf(&trash,
4231 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4232 PRODUCT_NAME "%s</a></h1>\n"
4233 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4234 "<hr width=\"100%%\" class=\"hr\">\n"
4235 "<h3>&gt; General process information</h3>\n"
4236 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4237 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4238 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4239 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4240 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4241 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4242 "Running tasks: %d/%d; idle = %d %%<br>\n"
4243 "</td><td align=\"center\" nowrap>\n"
4244 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004245 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4246 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004247 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004248 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4249 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004250 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004251 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4252 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004253 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004254 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4255 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004256 "</tr><tr>\n"
4257 "<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 -07004258 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004259 "<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 +01004260 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004261 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004262 "</td>"
4263 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4264 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4265 "",
4266 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4267 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4268 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4269 (uri->flags & ST_SHDESC) ? ": " : "",
4270 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4271 pid, relative_pid, global.nbproc,
4272 up / 86400, (up % 86400) / 3600,
4273 (up % 3600) / 60, (up % 60),
4274 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4275 global.rlimit_memmax ? " MB" : "",
4276 global.rlimit_nofile,
4277 global.maxsock, global.maxconn, global.maxpipes,
4278 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4279 run_queue_cur, nb_tasks_cur, idle_pct
4280 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004281
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004282 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004283 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 +01004284 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004285
4286 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004287 "<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 +01004288 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004289 STAT_SCOPE_TXT_MAXLEN);
4290
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004291 /* 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 +02004292 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004293 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004294 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004295 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 +01004296 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004297 }
4298
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004299 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004300 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004301 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004302 uri->uri_prefix,
4303 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004304 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004305 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004306 else
4307 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004308 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004309 uri->uri_prefix,
4310 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004311 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004312 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004313
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004314 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004315 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004316 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004317 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004318 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004319 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004320 "",
4321 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004322 else
4323 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004324 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004325 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004326 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004327 ";norefresh",
4328 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004329 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004330
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004331 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004332 "<li><a href=\"%s%s%s%s\">Refresh now</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" : "",
4335 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004336 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004337
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004338 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004339 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004340 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004341 (uri->refresh > 0) ? ";norefresh" : "",
4342 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004343
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004344 chunk_appendf(&trash,
4345 "</ul></td>"
4346 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4347 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4348 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4349 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4350 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4351 "</ul>"
4352 "</td>"
4353 "</tr></table>\n"
4354 ""
4355 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004356
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004357 if (appctx->ctx.stats.st_code) {
4358 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004359 case STAT_STATUS_DONE:
4360 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004361 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004362 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004363 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004364 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004365 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4366 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004367 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004368 break;
4369 case STAT_STATUS_NONE:
4370 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004371 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004372 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004373 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004374 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004375 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4376 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004377 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004378 break;
4379 case STAT_STATUS_PART:
4380 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004381 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004382 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004383 "Action partially processed.<br>"
4384 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004385 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004386 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4387 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004388 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004389 break;
4390 case STAT_STATUS_ERRP:
4391 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004392 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004393 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004394 "Action not processed because of invalid parameters."
4395 "<ul>"
4396 "<li>The action is maybe unknown.</li>"
4397 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4398 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4399 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004400 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004401 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4402 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004403 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004404 break;
4405 case STAT_STATUS_EXCD:
4406 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004407 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004408 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004409 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4410 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004411 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004412 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4413 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004414 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004415 break;
4416 case STAT_STATUS_DENY:
4417 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004418 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004419 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004420 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004421 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004422 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4423 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004424 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004425 break;
4426 default:
4427 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004428 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004429 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004430 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004431 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004432 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4433 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004434 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004435 }
4436 chunk_appendf(&trash, "<p>\n");
4437 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004438}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004439
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004440/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4441 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004442 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004443static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004444{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004445 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004446}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004447
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004448/* This function dumps statistics onto the stream interface's read buffer in
4449 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004450 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4451 * 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 +02004452 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004453 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004454 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004455static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004456{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004457 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004458 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004459 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004460
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004461 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004462
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004463 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004464 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004465 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004466 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004467
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004468 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004469 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004470 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004471 else
4472 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004473
Willy Tarreaubc18da12015-03-13 14:00:47 +01004474 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004475 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004476 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004477 }
Willy Tarreauae526782010-03-04 20:34:23 +01004478
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004479 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004480 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004481
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004482 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004483 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004484 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004485 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004486 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004487 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004488 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004489 }
Willy Tarreau91861262007-10-17 17:06:05 +02004490
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004491 appctx->ctx.stats.px = proxy;
4492 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4493 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004494 /* fall through */
4495
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004496 case STAT_ST_LIST:
4497 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004498 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004499 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004500 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004501 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004502 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004503
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004504 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004505 /* skip the disabled proxies, global frontend and non-networked ones */
4506 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004507 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004508 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004509
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004510 appctx->ctx.stats.px = px->next;
4511 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004512 }
4513 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004514
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004515 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004516 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004517
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004518 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004519 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004520 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01004521 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004522 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004523 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004524 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004525 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004526
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004527 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004528 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004529
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004530 case STAT_ST_FIN:
4531 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004532
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004533 default:
4534 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004535 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004536 return -1;
4537 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004538}
Willy Tarreauae526782010-03-04 20:34:23 +01004539
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004540/* We reached the stats page through a POST request. The appctx is
4541 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004542 * Parse the posted data and enable/disable servers if necessary.
4543 * Returns 1 if request was parsed or zero if it needs more data.
4544 */
4545static int stats_process_http_post(struct stream_interface *si)
4546{
Willy Tarreau87b09662015-04-03 00:22:06 +02004547 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004548 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004549
4550 struct proxy *px = NULL;
4551 struct server *sv = NULL;
4552
4553 char key[LINESIZE];
4554 int action = ST_ADM_ACTION_NONE;
4555 int reprocess = 0;
4556
4557 int total_servers = 0;
4558 int altered_servers = 0;
4559
4560 char *first_param, *cur_param, *next_param, *end_params;
4561 char *st_cur_param = NULL;
4562 char *st_next_param = NULL;
4563
4564 struct chunk *temp;
4565 int reql;
4566
4567 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02004568 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004569 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004570 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004571 goto out;
4572 }
4573
Willy Tarreaueee5b512015-04-03 23:46:31 +02004574 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 +01004575 if (reql <= 0) {
4576 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004577 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004578 return 0;
4579 }
4580
4581 first_param = temp->str;
4582 end_params = temp->str + reql;
4583 cur_param = next_param = end_params;
4584 *end_params = '\0';
4585
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004586 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004587
4588 /*
4589 * Parse the parameters in reverse order to only store the last value.
4590 * From the html form, the backend and the action are at the end.
4591 */
4592 while (cur_param > first_param) {
4593 char *value;
4594 int poffset, plen;
4595
4596 cur_param--;
4597
4598 if ((*cur_param == '&') || (cur_param == first_param)) {
4599 reprocess_servers:
4600 /* Parse the key */
4601 poffset = (cur_param != first_param ? 1 : 0);
4602 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4603 if ((plen > 0) && (plen <= sizeof(key))) {
4604 strncpy(key, cur_param + poffset, plen);
4605 key[plen - 1] = '\0';
4606 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004607 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004608 goto out;
4609 }
4610
4611 /* Parse the value */
4612 value = key;
4613 while (*value != '\0' && *value != '=') {
4614 value++;
4615 }
4616 if (*value == '=') {
4617 /* Ok, a value is found, we can mark the end of the key */
4618 *value++ = '\0';
4619 }
4620 if (url_decode(key) < 0 || url_decode(value) < 0)
4621 break;
4622
4623 /* Now we can check the key to see what to do */
4624 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02004625 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004626 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004627 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004628 goto out;
4629 }
4630 }
4631 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004632 if (strcmp(value, "ready") == 0) {
4633 action = ST_ADM_ACTION_READY;
4634 }
4635 else if (strcmp(value, "drain") == 0) {
4636 action = ST_ADM_ACTION_DRAIN;
4637 }
4638 else if (strcmp(value, "maint") == 0) {
4639 action = ST_ADM_ACTION_MAINT;
4640 }
4641 else if (strcmp(value, "shutdown") == 0) {
4642 action = ST_ADM_ACTION_SHUTDOWN;
4643 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004644 else if (strcmp(value, "dhlth") == 0) {
4645 action = ST_ADM_ACTION_DHLTH;
4646 }
4647 else if (strcmp(value, "ehlth") == 0) {
4648 action = ST_ADM_ACTION_EHLTH;
4649 }
4650 else if (strcmp(value, "hrunn") == 0) {
4651 action = ST_ADM_ACTION_HRUNN;
4652 }
4653 else if (strcmp(value, "hnolb") == 0) {
4654 action = ST_ADM_ACTION_HNOLB;
4655 }
4656 else if (strcmp(value, "hdown") == 0) {
4657 action = ST_ADM_ACTION_HDOWN;
4658 }
4659 else if (strcmp(value, "dagent") == 0) {
4660 action = ST_ADM_ACTION_DAGENT;
4661 }
4662 else if (strcmp(value, "eagent") == 0) {
4663 action = ST_ADM_ACTION_EAGENT;
4664 }
4665 else if (strcmp(value, "arunn") == 0) {
4666 action = ST_ADM_ACTION_ARUNN;
4667 }
4668 else if (strcmp(value, "adown") == 0) {
4669 action = ST_ADM_ACTION_ADOWN;
4670 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004671 /* else these are the old supported methods */
4672 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004673 action = ST_ADM_ACTION_DISABLE;
4674 }
4675 else if (strcmp(value, "enable") == 0) {
4676 action = ST_ADM_ACTION_ENABLE;
4677 }
4678 else if (strcmp(value, "stop") == 0) {
4679 action = ST_ADM_ACTION_STOP;
4680 }
4681 else if (strcmp(value, "start") == 0) {
4682 action = ST_ADM_ACTION_START;
4683 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004684 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004685 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004686 goto out;
4687 }
4688 }
4689 else if (strcmp(key, "s") == 0) {
4690 if (!(px && action)) {
4691 /*
4692 * Indicates that we'll need to reprocess the parameters
4693 * as soon as backend and action are known
4694 */
4695 if (!reprocess) {
4696 st_cur_param = cur_param;
4697 st_next_param = next_param;
4698 }
4699 reprocess = 1;
4700 }
4701 else if ((sv = findserver(px, value)) != NULL) {
4702 switch (action) {
4703 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004704 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004705 altered_servers++;
4706 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004707 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004708 }
4709 break;
4710 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004711 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004712 altered_servers++;
4713 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004714 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004715 }
4716 break;
4717 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004718 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4719 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4720 altered_servers++;
4721 total_servers++;
4722 }
4723 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004724 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004725 if (sv->admin & SRV_ADMF_FDRAIN) {
4726 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4727 altered_servers++;
4728 total_servers++;
4729 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004730 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004731 case ST_ADM_ACTION_DHLTH:
4732 if (sv->check.state & CHK_ST_CONFIGURED) {
4733 sv->check.state &= ~CHK_ST_ENABLED;
4734 altered_servers++;
4735 total_servers++;
4736 }
4737 break;
4738 case ST_ADM_ACTION_EHLTH:
4739 if (sv->check.state & CHK_ST_CONFIGURED) {
4740 sv->check.state |= CHK_ST_ENABLED;
4741 altered_servers++;
4742 total_servers++;
4743 }
4744 break;
4745 case ST_ADM_ACTION_HRUNN:
4746 if (!(sv->track)) {
4747 sv->check.health = sv->check.rise + sv->check.fall - 1;
4748 srv_set_running(sv, "changed from Web interface");
4749 altered_servers++;
4750 total_servers++;
4751 }
4752 break;
4753 case ST_ADM_ACTION_HNOLB:
4754 if (!(sv->track)) {
4755 sv->check.health = sv->check.rise + sv->check.fall - 1;
4756 srv_set_stopping(sv, "changed from Web interface");
4757 altered_servers++;
4758 total_servers++;
4759 }
4760 break;
4761 case ST_ADM_ACTION_HDOWN:
4762 if (!(sv->track)) {
4763 sv->check.health = 0;
4764 srv_set_stopped(sv, "changed from Web interface");
4765 altered_servers++;
4766 total_servers++;
4767 }
4768 break;
4769 case ST_ADM_ACTION_DAGENT:
4770 if (sv->agent.state & CHK_ST_CONFIGURED) {
4771 sv->agent.state &= ~CHK_ST_ENABLED;
4772 altered_servers++;
4773 total_servers++;
4774 }
4775 break;
4776 case ST_ADM_ACTION_EAGENT:
4777 if (sv->agent.state & CHK_ST_CONFIGURED) {
4778 sv->agent.state |= CHK_ST_ENABLED;
4779 altered_servers++;
4780 total_servers++;
4781 }
4782 break;
4783 case ST_ADM_ACTION_ARUNN:
4784 if (sv->agent.state & CHK_ST_ENABLED) {
4785 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4786 srv_set_running(sv, "changed from Web interface");
4787 altered_servers++;
4788 total_servers++;
4789 }
4790 break;
4791 case ST_ADM_ACTION_ADOWN:
4792 if (sv->agent.state & CHK_ST_ENABLED) {
4793 sv->agent.health = 0;
4794 srv_set_stopped(sv, "changed from Web interface");
4795 altered_servers++;
4796 total_servers++;
4797 }
4798 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004799 case ST_ADM_ACTION_READY:
4800 srv_adm_set_ready(sv);
4801 altered_servers++;
4802 total_servers++;
4803 break;
4804 case ST_ADM_ACTION_DRAIN:
4805 srv_adm_set_drain(sv);
4806 altered_servers++;
4807 total_servers++;
4808 break;
4809 case ST_ADM_ACTION_MAINT:
4810 srv_adm_set_maint(sv);
4811 altered_servers++;
4812 total_servers++;
4813 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004814 case ST_ADM_ACTION_SHUTDOWN:
4815 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004816 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004817
4818 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4819 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004820 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004821
4822 altered_servers++;
4823 total_servers++;
4824 }
4825 break;
4826 }
4827 } else {
4828 /* the server name is unknown or ambiguous (duplicate names) */
4829 total_servers++;
4830 }
4831 }
4832 if (reprocess && px && action) {
4833 /* Now, we know the backend and the action chosen by the user.
4834 * We can safely restart from the first server parameter
4835 * to reprocess them
4836 */
4837 cur_param = st_cur_param;
4838 next_param = st_next_param;
4839 reprocess = 0;
4840 goto reprocess_servers;
4841 }
4842
4843 next_param = cur_param;
4844 }
4845 }
4846
4847 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004848 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004849 }
4850 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004851 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004852 }
4853 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004854 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004855 }
4856 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004857 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004858 }
4859 out:
4860 return 1;
4861}
4862
4863
4864static int stats_send_http_headers(struct stream_interface *si)
4865{
Willy Tarreau87b09662015-04-03 00:22:06 +02004866 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004867 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004868 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004869
4870 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004871 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004872 "Cache-Control: no-cache\r\n"
4873 "Connection: close\r\n"
4874 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004875 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004876
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004877 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004878 chunk_appendf(&trash, "Refresh: %d\r\n",
4879 uri->refresh);
4880
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004881 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4882
4883 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4884 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4885 else
4886 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004887
Willy Tarreaueee5b512015-04-03 23:46:31 +02004888 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004889 s->logs.tv_request = now;
4890
Willy Tarreaubc18da12015-03-13 14:00:47 +01004891 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004892 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004893 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004894 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004895
4896 return 1;
4897}
4898
4899static int stats_send_http_redirect(struct stream_interface *si)
4900{
4901 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02004902 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004903 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004904 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004905
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004906 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004907 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004908 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004909 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004910 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 +01004911 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004912 }
4913
4914 /* We don't want to land on the posted stats page because a refresh will
4915 * repost the data. We don't want this to happen on accident so we redirect
4916 * the browse to the stats page with a GET.
4917 */
4918 chunk_printf(&trash,
4919 "HTTP/1.1 303 See Other\r\n"
4920 "Cache-Control: no-cache\r\n"
4921 "Content-Type: text/plain\r\n"
4922 "Connection: close\r\n"
4923 "Location: %s;st=%s%s%s%s\r\n"
4924 "\r\n",
4925 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004926 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4927 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4928 stat_status_codes[appctx->ctx.stats.st_code]) ?
4929 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004930 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004931 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4932 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004933 scope_txt);
4934
Willy Tarreaueee5b512015-04-03 23:46:31 +02004935 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004936 s->logs.tv_request = now;
4937
Willy Tarreaubc18da12015-03-13 14:00:47 +01004938 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004939 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004940 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004941 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004942
4943 return 1;
4944}
4945
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004946/* This I/O handler runs as an applet embedded in a stream interface. It is
4947 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004948 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004949 * automatically unregisters itself once transfer is complete.
4950 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02004951static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004952{
Willy Tarreau00a37f02015-04-13 12:05:19 +02004953 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02004954 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004955 struct channel *req = si_oc(si);
4956 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01004957
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004958 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4959 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004960
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004961 /* check that the output is not closed */
4962 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004963 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004964
Willy Tarreau347a35d2013-11-22 17:51:09 +01004965 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004966 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004967 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02004968 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004969 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004970 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004971 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004972 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004973 }
4974
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004975 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004976 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004977 unsigned int data_len;
4978 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004979 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004980
4981 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4982 /* One difficulty we're facing is that we must prevent
4983 * the input data from being automatically forwarded to
4984 * the output area. For this, we temporarily disable
4985 * forwarding on the channel.
4986 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004987 last_fwd = si_ic(si)->to_forward;
4988 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004989 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004990 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004991 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004992 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02004993 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004994 }
4995 }
4996
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004997 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004998 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004999 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005000
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005001 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005002
5003 /* Now we must either adjust or remove the chunk size. This is
5004 * not easy because the chunk size might wrap at the end of the
5005 * buffer, so we pretend we have nothing in the buffer, we write
5006 * the size, then restore the buffer's contents. Note that we can
5007 * only do that because no forwarding is scheduled on the stats
5008 * applet.
5009 */
5010 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005011 si_ic(si)->total -= (last_len - prev_len);
5012 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005013
5014 if (last_len != data_len) {
5015 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005016 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005017 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005018
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005019 si_ic(si)->total += (last_len - data_len);
5020 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005021 }
5022 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005023 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005024 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005025 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005026
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005027 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005028 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005029 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005030 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005031 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005032 }
5033
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005034 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005035 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005036 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005037 }
5038
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005039 if (appctx->st0 == STAT_HTTP_DONE) {
5040 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5041 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005042 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005043 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005044 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005045 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005046 }
5047 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005048 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005049 res->flags |= CF_READ_NULL;
5050 si_shutr(si);
5051 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005052
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005053 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5054 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005055
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005056 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005057 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5058 si_shutr(si);
5059 res->flags |= CF_READ_NULL;
5060 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005061 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005062 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005063 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005064}
5065
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005066
Willy Tarreau909d5172012-11-26 03:04:41 +01005067static inline const char *get_conn_ctrl_name(const struct connection *conn)
5068{
Willy Tarreau3c728722014-01-23 13:50:42 +01005069 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005070 return "NONE";
5071 return conn->ctrl->name;
5072}
5073
5074static inline const char *get_conn_xprt_name(const struct connection *conn)
5075{
5076 static char ptr[17];
5077
Willy Tarreauaad69382014-01-23 14:21:42 +01005078 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005079 return "NONE";
5080
5081 if (conn->xprt == &raw_sock)
5082 return "RAW";
5083
5084#ifdef USE_OPENSSL
5085 if (conn->xprt == &ssl_sock)
5086 return "SSL";
5087#endif
5088 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5089 return ptr;
5090}
5091
5092static inline const char *get_conn_data_name(const struct connection *conn)
5093{
5094 static char ptr[17];
5095
5096 if (!conn->data)
5097 return "NONE";
5098
5099 if (conn->data == &sess_conn_cb)
5100 return "SESS";
5101
5102 if (conn->data == &si_conn_cb)
5103 return "STRM";
5104
5105 if (conn->data == &check_conn_cb)
5106 return "CHCK";
5107
5108 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5109 return ptr;
5110}
5111
Willy Tarreau87b09662015-04-03 00:22:06 +02005112/* This function dumps a complete stream state onto the stream interface's
5113 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005114 * 0 if the output buffer is full and it needs to be called again, otherwise
5115 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005116 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005117static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005118{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005119 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005120 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005121 extern const char *monthname[12];
5122 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005123 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005124 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005125
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005126 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005127
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005128 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005129 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005130 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005131 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005132 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005133 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005134 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005135 appctx->ctx.sess.uid = 0;
5136 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005137 return 1;
5138 }
5139
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005140 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005141 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005142 appctx->ctx.sess.uid = sess->uniq_id;
5143 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005144 /* fall through */
5145
5146 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005147 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005148 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005149 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005150 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005151 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5152 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005153 sess->uniq_id,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005154 strm_li(sess) && strm_li(sess)->proto->name ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005155
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005156 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005157 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005158 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005159 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005160 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005161 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005162 break;
5163 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005164 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005165 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005166 default:
5167 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005168 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005169 break;
5170 }
5171
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005172 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005173 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005174 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005175
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005176 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005177 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005178 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5179 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5180 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005181
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005182 if (conn)
5183 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005184
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005185 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005186 case AF_INET:
5187 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005188 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005189 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005190 break;
5191 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005192 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005193 break;
5194 default:
5195 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005196 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005197 break;
5198 }
5199
Willy Tarreau50943332011-09-02 17:33:05 +02005200 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005201 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005202 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005203 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005204 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5205 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005206 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005207
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005208 conn = objt_conn(sess->si[1].end);
5209 if (conn)
5210 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005211
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005212 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005213 case AF_INET:
5214 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005215 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005216 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005217 break;
5218 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005219 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005220 break;
5221 default:
5222 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005223 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005224 break;
5225 }
5226
5227 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005228 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005229 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005230 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5231 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005232 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005233 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005234
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005235 if (conn)
5236 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005237
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005238 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005239 case AF_INET:
5240 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005241 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005242 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005243 break;
5244 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005245 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005246 break;
5247 default:
5248 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005249 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005250 break;
5251 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005252
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005253 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005254 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005255 sess->task,
5256 sess->task->state,
5257 sess->task->nice, sess->task->calls,
5258 sess->task->expire ?
5259 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5260 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5261 TICKS_TO_MS(1000)) : "<NEVER>",
5262 task_in_rq(sess->task) ? ", running" : "");
5263
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005264 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005265 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005266 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5267
Willy Tarreaueee5b512015-04-03 23:46:31 +02005268 if (sess->txn)
5269 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005270 " 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 +02005271 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5272 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 +01005273
5274 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005275 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005276 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005277 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005278 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005279 obj_type_name(sess->si[0].end),
5280 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005281 sess->si[0].exp ?
5282 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5283 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5284 TICKS_TO_MS(1000)) : "<NEVER>",
5285 sess->si[0].err_type);
5286
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005287 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005288 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005289 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005290 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005291 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005292 obj_type_name(sess->si[1].end),
5293 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005294 sess->si[1].exp ?
5295 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5296 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5297 TICKS_TO_MS(1000)) : "<NEVER>",
5298 sess->si[1].err_type);
5299
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005300 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005301 chunk_appendf(&trash,
5302 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005303 conn,
5304 get_conn_ctrl_name(conn),
5305 get_conn_xprt_name(conn),
5306 get_conn_data_name(conn),
5307 obj_type_name(conn->target),
5308 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005309
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005310 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005311 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005312 conn->flags,
5313 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005314 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005315 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005316 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005317 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005318 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5319 chunk_appendf(&trash,
5320 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5321 tmpctx,
5322 tmpctx->st0,
5323 tmpctx->st1,
5324 tmpctx->st2,
5325 tmpctx->applet->name);
5326 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005327
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005328 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005329 chunk_appendf(&trash,
5330 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005331 conn,
5332 get_conn_ctrl_name(conn),
5333 get_conn_xprt_name(conn),
5334 get_conn_data_name(conn),
5335 obj_type_name(conn->target),
5336 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005337
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005338 chunk_appendf(&trash,
5339 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005340 conn->flags,
5341 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005342 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005343 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005344 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005345 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005346 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5347 chunk_appendf(&trash,
5348 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5349 tmpctx,
5350 tmpctx->st0,
5351 tmpctx->st1,
5352 tmpctx->st2,
5353 tmpctx->applet->name);
5354 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005355
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005356 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005357 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005358 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005359 &sess->req,
5360 sess->req.flags, sess->req.analysers,
5361 sess->req.pipe ? sess->req.pipe->data : 0,
5362 sess->req.to_forward, sess->req.total,
5363 sess->req.analyse_exp ?
5364 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005365 TICKS_TO_MS(1000)) : "<NEVER>");
5366
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005367 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005368 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005369 sess->req.rex ?
5370 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005371 TICKS_TO_MS(1000)) : "<NEVER>");
5372
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005373 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005374 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005375 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005376 sess->req.wex ?
5377 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005378 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005379 sess->req.buf,
5380 sess->req.buf->data, sess->req.buf->o,
5381 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005382 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005383 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005384
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005385 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005386 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005387 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005388 &sess->res,
5389 sess->res.flags, sess->res.analysers,
5390 sess->res.pipe ? sess->res.pipe->data : 0,
5391 sess->res.to_forward, sess->res.total,
5392 sess->res.analyse_exp ?
5393 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005394 TICKS_TO_MS(1000)) : "<NEVER>");
5395
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005396 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005397 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005398 sess->res.rex ?
5399 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005400 TICKS_TO_MS(1000)) : "<NEVER>");
5401
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005402 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005403 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005404 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005405 sess->res.wex ?
5406 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005407 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005408 sess->res.buf,
5409 sess->res.buf->data, sess->res.buf->o,
5410 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005411 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005412 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005413
Willy Tarreaubc18da12015-03-13 14:00:47 +01005414 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005415 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005416 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005417 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005418
5419 /* use other states to dump the contents */
5420 }
5421 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005422 appctx->ctx.sess.uid = 0;
5423 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005424 return 1;
5425}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005426
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005427#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5428static int stats_tlskeys_list(struct stream_interface *si) {
5429 struct appctx *appctx = __objt_appctx(si->end);
5430
5431 switch (appctx->st2) {
5432 case STAT_ST_INIT:
5433 /* Display the column headers. If the message cannot be sent,
5434 * quit the fucntion with returning 0. The function is called
5435 * later and restart at the state "STAT_ST_INIT".
5436 */
5437 chunk_reset(&trash);
5438 chunk_appendf(&trash, "# id (file)\n");
5439 if (bi_putchk(si_ic(si), &trash) == -1) {
5440 si_applet_cant_put(si);
5441 return 0;
5442 }
5443
5444 /* Now, we start the browsing of the references lists.
5445 * Note that the following call to LIST_ELEM return bad pointer. The only
5446 * avalaible field of this pointer is <list>. It is used with the function
5447 * tlskeys_list_get_next() for retruning the first avalaible entry
5448 */
5449 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
5450 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5451
5452 appctx->st2 = STAT_ST_LIST;
5453 /* fall through */
5454
5455 case STAT_ST_LIST:
5456 while (appctx->ctx.tlskeys.ref) {
5457 chunk_reset(&trash);
5458
5459 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
5460 appctx->ctx.tlskeys.ref->filename);
5461
5462 if (bi_putchk(si_ic(si), &trash) == -1) {
5463 /* let's try again later from this stream. We add ourselves into
5464 * this stream's users so that it can remove us upon termination.
5465 */
5466 si_applet_cant_put(si);
5467 return 0;
5468 }
5469
5470 /* get next list entry and check the end of the list */
5471 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5472 }
5473
5474 appctx->st2 = STAT_ST_FIN;
5475 /* fall through */
5476
5477 default:
5478 appctx->st2 = STAT_ST_FIN;
5479 return 1;
5480 }
5481 return 0;
5482}
5483#endif
5484
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005485static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005486{
5487 struct appctx *appctx = __objt_appctx(si->end);
5488
5489 switch (appctx->st2) {
5490 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005491 /* Display the column headers. If the message cannot be sent,
5492 * quit the fucntion with returning 0. The function is called
5493 * later and restart at the state "STAT_ST_INIT".
5494 */
5495 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005496 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005497 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005498 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005499 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005500 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005501
5502 /* Now, we start the browsing of the references lists.
5503 * Note that the following call to LIST_ELEM return bad pointer. The only
5504 * avalaible field of this pointer is <list>. It is used with the function
5505 * pat_list_get_next() for retruning the first avalaible entry
5506 */
5507 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5508 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5509 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005510 appctx->st2 = STAT_ST_LIST;
5511 /* fall through */
5512
5513 case STAT_ST_LIST:
5514 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005515 chunk_reset(&trash);
5516
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005517 /* Build messages. If the reference is used by another category than
5518 * the listed categorie, display the information in the massage.
5519 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005520 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005521 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5522 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005523
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005524 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005525 /* let's try again later from this stream. We add ourselves into
5526 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005527 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005528 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005529 return 0;
5530 }
5531
5532 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005533 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5534 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005535 }
5536
5537 appctx->st2 = STAT_ST_FIN;
5538 /* fall through */
5539
5540 default:
5541 appctx->st2 = STAT_ST_FIN;
5542 return 1;
5543 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005544 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005545}
5546
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005547static int stats_map_lookup(struct stream_interface *si)
5548{
5549 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005550 struct sample sample;
5551 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005552 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005553
5554 switch (appctx->st2) {
5555 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005556 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005557 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005558 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005559 appctx->st2 = STAT_ST_LIST;
5560 /* fall through */
5561
5562 case STAT_ST_LIST:
5563 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005564 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005565 /* initialise chunk to build new message */
5566 chunk_reset(&trash);
5567
5568 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005569 sample.type = SMP_T_STR;
5570 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005571 sample.data.str.len = appctx->ctx.map.chunk.len;
5572 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005573 if (appctx->ctx.map.expr->pat_head->match &&
5574 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005575 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5576 else
5577 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005578
5579 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005580 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5581 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5582 break;
5583 if (match_method >= PAT_MATCH_NUM)
5584 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5585 else
5586 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005587
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005588 /* case sensitive */
5589 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5590 chunk_appendf(&trash, ", case=insensitive");
5591 else
5592 chunk_appendf(&trash, ", case=sensitive");
5593
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005594 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005595 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005596 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5597 chunk_appendf(&trash, ", found=no");
5598 else
5599 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005600 }
5601
5602 /* Display match and match info */
5603 else {
5604 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005605 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5606 chunk_appendf(&trash, ", found=yes");
5607 else
5608 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005609
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005610 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005611 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005612 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005613 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005614 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005615
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005616 /* display pattern */
5617 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5618 if (pat->ref && pat->ref->pattern)
5619 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5620 else
5621 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005622 }
5623 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005624 if (pat->ref && pat->ref->pattern)
5625 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5626 else
5627 chunk_appendf(&trash, ", pattern=unknown");
5628 }
5629
5630 /* display return value */
5631 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5632 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005633 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005634 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005635 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005636 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005637 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005638 }
5639
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005640 chunk_appendf(&trash, "\n");
5641
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005642 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005643 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005644 /* let's try again later from this stream. We add ourselves into
5645 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005646 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005647 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005648 return 0;
5649 }
5650
5651 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005652 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5653 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005654 }
5655
5656 appctx->st2 = STAT_ST_FIN;
5657 /* fall through */
5658
5659 default:
5660 appctx->st2 = STAT_ST_FIN;
5661 free(appctx->ctx.map.chunk.str);
5662 return 1;
5663 }
5664}
5665
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005666static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005667{
5668 struct appctx *appctx = __objt_appctx(si->end);
5669
5670 switch (appctx->st2) {
5671
5672 case STAT_ST_INIT:
5673 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005674 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5675 struct pat_ref_elt *, list);
5676 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5677 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005678 appctx->st2 = STAT_ST_LIST;
5679 /* fall through */
5680
5681 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005682 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005683 chunk_reset(&trash);
5684
5685 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005686 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005687 chunk_appendf(&trash, "%p %s %s\n",
5688 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5689 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005690 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005691 chunk_appendf(&trash, "%p %s\n",
5692 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005693
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005694 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005695 /* let's try again later from this stream. We add ourselves into
5696 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005697 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005698 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005699 return 0;
5700 }
5701
5702 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005703 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5704 struct pat_ref_elt *, list);
5705 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005706 break;
5707 }
5708
5709 appctx->st2 = STAT_ST_FIN;
5710 /* fall through */
5711
5712 default:
5713 appctx->st2 = STAT_ST_FIN;
5714 return 1;
5715 }
5716}
5717
Willy Tarreau87b09662015-04-03 00:22:06 +02005718/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005719 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005720 * to be called again, otherwise non-zero. It is designed to be called
5721 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005722 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005723static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005724{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005725 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005726 struct connection *conn;
5727
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005728 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005729 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02005730 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005731 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005732 if (appctx->st2 == STAT_ST_LIST) {
5733 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5734 LIST_DEL(&appctx->ctx.sess.bref.users);
5735 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005736 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005737 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005738 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005739 }
5740
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005741 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005742
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005743 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005744 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005745 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02005746 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005747 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02005748 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005749 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02005750 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005751 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005752 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02005753 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005754 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005755 /* fall through */
5756
Willy Tarreau295a8372011-03-10 11:25:07 +01005757 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02005758 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005759 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5760 LIST_DEL(&appctx->ctx.sess.bref.users);
5761 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005762 }
5763
5764 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02005765 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005766 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005767 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005768
Willy Tarreau87b09662015-04-03 00:22:06 +02005769 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005770
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005771 if (appctx->ctx.sess.target) {
5772 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005773 goto next_sess;
5774
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005775 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005776 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005777 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005778 return 0;
5779
Willy Tarreau87b09662015-04-03 00:22:06 +02005780 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005781 LIST_DEL(&appctx->ctx.sess.bref.users);
5782 LIST_INIT(&appctx->ctx.sess.bref.users);
5783 if (appctx->ctx.sess.target != (void *)-1) {
5784 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005785 break;
5786 }
5787 else
5788 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005789 }
5790
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005791 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005792 "%p: proto=%s",
5793 curr_sess,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005794 strm_li(curr_sess)->proto->name);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005795
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005796
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005797 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005798 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005799 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005800 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005801 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005802 " src=%s:%d fe=%s be=%s srv=%s",
5803 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005804 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005805 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005806 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005807 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005808 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005809 break;
5810 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005811 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005812 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005813 strm_li(curr_sess)->luid,
5814 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005815 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005816 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005817 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005818 break;
5819 }
5820
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005821 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005822 " ts=%02x age=%s calls=%d",
5823 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005824 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5825 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005826
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005827 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005828 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005829 curr_sess->req.flags,
5830 curr_sess->req.buf->i,
5831 curr_sess->req.analysers,
5832 curr_sess->req.rex ?
5833 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005834 TICKS_TO_MS(1000)) : "");
5835
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005836 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005837 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005838 curr_sess->req.wex ?
5839 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005840 TICKS_TO_MS(1000)) : "");
5841
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005842 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005843 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005844 curr_sess->req.analyse_exp ?
5845 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005846 TICKS_TO_MS(1000)) : "");
5847
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005848 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005849 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005850 curr_sess->res.flags,
5851 curr_sess->res.buf->i,
5852 curr_sess->res.analysers,
5853 curr_sess->res.rex ?
5854 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005855 TICKS_TO_MS(1000)) : "");
5856
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005857 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005858 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005859 curr_sess->res.wex ?
5860 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005861 TICKS_TO_MS(1000)) : "");
5862
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005863 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005864 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005865 curr_sess->res.analyse_exp ?
5866 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005867 TICKS_TO_MS(1000)) : "");
5868
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005869 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005870 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005871 " s0=[%d,%1xh,fd=%d,ex=%s]",
5872 curr_sess->si[0].state,
5873 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005874 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005875 curr_sess->si[0].exp ?
5876 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5877 TICKS_TO_MS(1000)) : "");
5878
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005879 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005880 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005881 " s1=[%d,%1xh,fd=%d,ex=%s]",
5882 curr_sess->si[1].state,
5883 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005884 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005885 curr_sess->si[1].exp ?
5886 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5887 TICKS_TO_MS(1000)) : "");
5888
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005889 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005890 " exp=%s",
5891 curr_sess->task->expire ?
5892 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5893 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005894 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005895 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005896
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005897 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005898
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005899 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005900 /* let's try again later from this stream. We add ourselves into
5901 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005902 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005903 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005904 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005905 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005906 }
5907
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005908 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005909 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005910 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005911
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005912 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005913 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005914 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005915 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005916 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005917 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005918
Willy Tarreaubc18da12015-03-13 14:00:47 +01005919 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005920 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005921 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005922 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005923
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005924 appctx->ctx.sess.target = NULL;
5925 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005926 return 1;
5927 }
5928
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005929 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005930 /* fall through */
5931
5932 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005933 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005934 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005935 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005936}
5937
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005938/* This is called when the stream interface is closed. For instance, upon an
5939 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02005940 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005941 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005942static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005943{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005944 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5945 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5946 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005947 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005948 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5949 free(appctx->ctx.cli.err);
5950 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005951 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5952 free(appctx->ctx.map.chunk.str);
5953 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005954}
5955
Willy Tarreau20e99322013-04-13 09:22:25 +02005956/* This function is used to either dump tables states (when action is set
5957 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005958 * It returns 0 if the output buffer is full and it needs to be called
5959 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005960 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005961static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005962{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005963 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02005964 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005965 struct ebmb_node *eb;
5966 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005967 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005968 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005969
5970 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005971 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005972 * - STAT_ST_INIT : the first call
5973 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005974 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005975 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005976 * and the entry pointer points to the next entry to be dumped,
5977 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005978 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005979 * data though.
5980 */
5981
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005982 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005983 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005984 if (appctx->st2 == STAT_ST_LIST) {
5985 appctx->ctx.table.entry->ref_cnt--;
5986 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005987 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005988 return 1;
5989 }
5990
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005991 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005992
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005993 while (appctx->st2 != STAT_ST_FIN) {
5994 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005995 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005996 appctx->ctx.table.proxy = appctx->ctx.table.target;
5997 if (!appctx->ctx.table.proxy)
5998 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005999
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006000 appctx->ctx.table.entry = NULL;
6001 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006002 break;
6003
Willy Tarreau295a8372011-03-10 11:25:07 +01006004 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006005 if (!appctx->ctx.table.proxy ||
6006 (appctx->ctx.table.target &&
6007 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6008 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006009 break;
6010 }
6011
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006012 if (appctx->ctx.table.proxy->table.size) {
6013 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6014 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006015 return 0;
6016
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006017 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006018 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006019 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006020 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006021 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006022 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6023 appctx->ctx.table.entry->ref_cnt++;
6024 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006025 break;
6026 }
6027 }
6028 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006029 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006030 break;
6031
Willy Tarreau295a8372011-03-10 11:25:07 +01006032 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006033 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006034
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006035 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006036 /* we're filtering on some data contents */
6037 void *ptr;
6038 long long data;
6039
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006040 dt = appctx->ctx.table.data_type;
6041 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6042 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006043 dt);
6044
6045 data = 0;
6046 switch (stktable_data_types[dt].std_type) {
6047 case STD_T_SINT:
6048 data = stktable_data_cast(ptr, std_t_sint);
6049 break;
6050 case STD_T_UINT:
6051 data = stktable_data_cast(ptr, std_t_uint);
6052 break;
6053 case STD_T_ULL:
6054 data = stktable_data_cast(ptr, std_t_ull);
6055 break;
6056 case STD_T_FRQP:
6057 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006058 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006059 break;
6060 }
6061
6062 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006063 if ((data < appctx->ctx.table.value &&
6064 (appctx->ctx.table.data_op == STD_OP_EQ ||
6065 appctx->ctx.table.data_op == STD_OP_GT ||
6066 appctx->ctx.table.data_op == STD_OP_GE)) ||
6067 (data == appctx->ctx.table.value &&
6068 (appctx->ctx.table.data_op == STD_OP_NE ||
6069 appctx->ctx.table.data_op == STD_OP_GT ||
6070 appctx->ctx.table.data_op == STD_OP_LT)) ||
6071 (data > appctx->ctx.table.value &&
6072 (appctx->ctx.table.data_op == STD_OP_EQ ||
6073 appctx->ctx.table.data_op == STD_OP_LT ||
6074 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006075 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006076 }
6077
Simon Hormanc88b8872011-06-15 15:18:49 +09006078 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006079 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6080 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006081 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006082
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006083 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006084
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006085 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006086 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006087 struct stksess *old = appctx->ctx.table.entry;
6088 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006089 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006090 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6091 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6092 stksess_kill(&appctx->ctx.table.proxy->table, old);
6093 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006094 break;
6095 }
6096
Simon Hormanc88b8872011-06-15 15:18:49 +09006097
6098 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006099 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6100 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6101 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006102
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006103 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6104 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006105 break;
6106
Willy Tarreau295a8372011-03-10 11:25:07 +01006107 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006108 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006109 break;
6110 }
6111 }
6112 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006113}
6114
Willy Tarreaud426a182010-03-05 14:58:26 +01006115/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006116 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6117 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6118 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6119 * lines are respected within the limit of 70 output chars. Lines that are
6120 * continuation of a previous truncated line begin with "+" instead of " "
6121 * after the offset. The new pointer is returned.
6122 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006123static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6124 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006125{
6126 int end;
6127 unsigned char c;
6128
6129 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006130 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006131 return ptr;
6132
Willy Tarreau77804732012-10-29 16:14:26 +01006133 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006134
Willy Tarreaud426a182010-03-05 14:58:26 +01006135 while (ptr < len && ptr < bsize) {
6136 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006137 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006138 if (out->len > end - 2)
6139 break;
6140 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006141 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006142 if (out->len > end - 3)
6143 break;
6144 out->str[out->len++] = '\\';
6145 switch (c) {
6146 case '\t': c = 't'; break;
6147 case '\n': c = 'n'; break;
6148 case '\r': c = 'r'; break;
6149 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006150 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006151 }
6152 out->str[out->len++] = c;
6153 } else {
6154 if (out->len > end - 5)
6155 break;
6156 out->str[out->len++] = '\\';
6157 out->str[out->len++] = 'x';
6158 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6159 out->str[out->len++] = hextab[c & 0xF];
6160 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006161 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006162 /* we had a line break, let's return now */
6163 out->str[out->len++] = '\n';
6164 *line = ptr;
6165 return ptr;
6166 }
6167 }
6168 /* we have an incomplete line, we return it as-is */
6169 out->str[out->len++] = '\n';
6170 return ptr;
6171}
6172
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006173/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006174 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006175 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006176 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006177static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006178{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006179 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006180 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006181
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006182 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006183 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006184
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006185 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006186
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006187 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006188 /* the function had not been called yet, let's prepare the
6189 * buffer for a response.
6190 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006191 struct tm tm;
6192
6193 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006194 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006195 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6196 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6197 error_snapshot_id);
6198
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006199 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006200 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006201 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006202 return 0;
6203 }
6204
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006205 appctx->ctx.errors.px = proxy;
6206 appctx->ctx.errors.buf = 0;
6207 appctx->ctx.errors.bol = 0;
6208 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006209 }
6210
6211 /* we have two inner loops here, one for the proxy, the other one for
6212 * the buffer.
6213 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006214 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006215 struct error_snapshot *es;
6216
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006217 if (appctx->ctx.errors.buf == 0)
6218 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006219 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006220 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006221
6222 if (!es->when.tv_sec)
6223 goto next;
6224
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006225 if (appctx->ctx.errors.iid >= 0 &&
6226 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6227 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006228 goto next;
6229
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006230 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006231 /* just print headers now */
6232
6233 char pn[INET6_ADDRSTRLEN];
6234 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006235 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006236
6237 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006238 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006239 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006240 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006241
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006242 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6243 case AF_INET:
6244 case AF_INET6:
6245 port = get_host_port(&es->src);
6246 break;
6247 default:
6248 port = 0;
6249 }
6250
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006251 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006252 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006253 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006254 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006255 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006256 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006257 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6258 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006259 break;
6260 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006261 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006262 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006263 " frontend %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->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006266 break;
6267 }
6268
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006269 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006270 ", server %s (#%d), event #%u\n"
6271 " src %s:%d, session #%d, session flags 0x%08x\n"
6272 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6273 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6274 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6275 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6276 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6277 es->ev_id,
6278 pn, port, es->sid, es->s_flags,
6279 es->state, es->m_flags, es->t_flags,
6280 es->m_clen, es->m_blen,
6281 es->b_flags, es->b_out, es->b_tot,
6282 es->len, es->b_wrap, es->pos);
6283
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006284 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006285 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006286 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006287 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006288 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006289 appctx->ctx.errors.ptr = 0;
6290 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006291 }
6292
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006293 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006294 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006295 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006296 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006297 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006298 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006299 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006300 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006301 goto next;
6302 }
6303
6304 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006305 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006306 int newptr;
6307 int newline;
6308
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006309 newline = appctx->ctx.errors.bol;
6310 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6311 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006312 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006313
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006314 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006315 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006316 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006317 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006318 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006319 appctx->ctx.errors.ptr = newptr;
6320 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006321 };
6322 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006323 appctx->ctx.errors.bol = 0;
6324 appctx->ctx.errors.ptr = -1;
6325 appctx->ctx.errors.buf++;
6326 if (appctx->ctx.errors.buf > 1) {
6327 appctx->ctx.errors.buf = 0;
6328 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006329 }
6330 }
6331
6332 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006333 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006334}
6335
Willy Tarreaud5781202012-09-22 19:32:35 +02006336/* parse the "level" argument on the bind lines */
6337static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6338{
6339 if (!*args[cur_arg + 1]) {
6340 memprintf(err, "'%s' : missing level", args[cur_arg]);
6341 return ERR_ALERT | ERR_FATAL;
6342 }
6343
6344 if (!strcmp(args[cur_arg+1], "user"))
6345 conf->level = ACCESS_LVL_USER;
6346 else if (!strcmp(args[cur_arg+1], "operator"))
6347 conf->level = ACCESS_LVL_OPER;
6348 else if (!strcmp(args[cur_arg+1], "admin"))
6349 conf->level = ACCESS_LVL_ADMIN;
6350 else {
6351 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6352 args[cur_arg], args[cur_arg+1]);
6353 return ERR_ALERT | ERR_FATAL;
6354 }
6355
6356 return 0;
6357}
6358
Willy Tarreau30576452015-04-13 13:50:30 +02006359struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006360 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006361 .name = "<STATS>", /* used for logging */
6362 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006363 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006364};
6365
Willy Tarreau30576452015-04-13 13:50:30 +02006366static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006367 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006368 .name = "<CLI>", /* used for logging */
6369 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006370 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006371};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006372
Willy Tarreaudc13c112013-06-21 23:16:39 +02006373static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006374 { CFG_GLOBAL, "stats", stats_parse_global },
6375 { 0, NULL, NULL },
6376}};
6377
Willy Tarreaud5781202012-09-22 19:32:35 +02006378static struct bind_kw_list bind_kws = { "STAT", { }, {
6379 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6380 { NULL, NULL, 0 },
6381}};
6382
Willy Tarreau10522fd2008-07-09 20:12:41 +02006383__attribute__((constructor))
6384static void __dumpstats_module_init(void)
6385{
6386 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006387 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006388}
6389
Willy Tarreau91861262007-10-17 17:06:05 +02006390/*
6391 * Local variables:
6392 * c-indent-level: 8
6393 * c-basic-offset: 8
6394 * End:
6395 */