blob: b505d4e2e17295288e0b7cd4a1bebc76b491d269 [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 Tarreau91861262007-10-17 17:06:05 +020040#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041
42#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020043#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010045#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
47#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/freq_ctr.h>
Willy Tarreau32b60d42015-03-13 16:14:57 +010049#include <proto/frontend.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020050#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010051#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010052#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020053#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010054#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010055#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020056#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020057#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010058#include <proto/sample.h>
Willy Tarreau9903f0e2015-04-04 18:50:31 +020059#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020060#include <proto/stream.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020061#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020062#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010063#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010064#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020065
Willy Tarreau7a0169a2012-11-19 17:13:16 +010066#ifdef USE_OPENSSL
67#include <proto/ssl_sock.h>
68#endif
69
Willy Tarreau91b843d2014-01-28 16:27:17 +010070/* stats socket states */
71enum {
72 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
73 STAT_CLI_END, /* final state, let's close */
74 STAT_CLI_GETREQ, /* wait for a request */
75 STAT_CLI_OUTPUT, /* all states after this one are responses */
76 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
77 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010078 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010079 STAT_CLI_O_INFO, /* dump info */
Willy Tarreau87b09662015-04-03 00:22:06 +020080 STAT_CLI_O_SESS, /* dump streams */
Willy Tarreau91b843d2014-01-28 16:27:17 +010081 STAT_CLI_O_ERR, /* dump errors */
82 STAT_CLI_O_TAB, /* dump tables */
83 STAT_CLI_O_CLR, /* clear tables */
84 STAT_CLI_O_SET, /* set entries in tables */
85 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010086 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
87 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010088 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010089 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010090};
91
Willy Tarreaued7df902014-05-22 18:04:49 +020092/* Actions available for the stats admin forms */
93enum {
94 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +020095
96 /* enable/disable health checks */
97 ST_ADM_ACTION_DHLTH,
98 ST_ADM_ACTION_EHLTH,
99
100 /* force health check status */
101 ST_ADM_ACTION_HRUNN,
102 ST_ADM_ACTION_HNOLB,
103 ST_ADM_ACTION_HDOWN,
104
105 /* enable/disable agent checks */
106 ST_ADM_ACTION_DAGENT,
107 ST_ADM_ACTION_EAGENT,
108
109 /* force agent check status */
110 ST_ADM_ACTION_ARUNN,
111 ST_ADM_ACTION_ADOWN,
112
113 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200114 ST_ADM_ACTION_READY,
115 ST_ADM_ACTION_DRAIN,
116 ST_ADM_ACTION_MAINT,
117 ST_ADM_ACTION_SHUTDOWN,
118 /* these are the ancient actions, still available for compatibility */
119 ST_ADM_ACTION_DISABLE,
120 ST_ADM_ACTION_ENABLE,
121 ST_ADM_ACTION_STOP,
122 ST_ADM_ACTION_START,
123};
124
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100125static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100126static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200127static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900128static int stats_dump_sess_to_buffer(struct stream_interface *si);
129static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100130static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100131static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
132static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100133static int stats_pats_list(struct stream_interface *si);
134static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100135static int stats_map_lookup(struct stream_interface *si);
Willy Tarreau44cf5452014-10-22 19:06:31 +0200136static void cli_release_handler(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +0900137
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100138/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100139 * cli_io_handler()
140 * -> stats_dump_sess_to_buffer() // "show sess"
141 * -> stats_dump_errors_to_buffer() // "show errors"
142 * -> stats_dump_info_to_buffer() // "show info"
143 * -> stats_dump_stat_to_buffer() // "show stat"
144 * -> stats_dump_csv_header()
145 * -> stats_dump_proxy_to_buffer()
146 * -> stats_dump_fe_stats()
147 * -> stats_dump_li_stats()
148 * -> stats_dump_sv_stats()
149 * -> stats_dump_be_stats()
150 *
151 * http_stats_io_handler()
152 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
153 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
154 * -> stats_dump_html_head() // emits the HTML headers
155 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
156 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
157 * -> stats_dump_html_px_hdr()
158 * -> stats_dump_fe_stats()
159 * -> stats_dump_li_stats()
160 * -> stats_dump_sv_stats()
161 * -> stats_dump_be_stats()
162 * -> stats_dump_html_px_end()
163 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100164 */
165
Simon Horman9bd2c732011-06-15 15:18:44 +0900166static struct si_applet cli_applet;
167
168static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200169 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200170 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200171 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200172 " help : this message\n"
173 " prompt : toggle interactive mode with prompt\n"
174 " quit : disconnect\n"
175 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100176 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200177 " show stat : report counters for each proxy and server\n"
178 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100179 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200180 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200181 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200182 " set weight : change a server's weight\n"
Willy Tarreau2a4b70f2014-05-22 18:42:35 +0200183 " set server : change a server's state or weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200184 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100185 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200186 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200187 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200188 " disable : put a server or frontend in maintenance mode\n"
189 " enable : re-enable a server or frontend which is in maintenance mode\n"
190 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100191 " show acl [id] : report avalaible acls or dump an acl's contents\n"
192 " get acl : reports the patterns matching a sample for an ACL\n"
193 " add acl : add acl entry\n"
194 " del acl : delete acl entry\n"
195 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100196 " show map [id] : report avalaible maps or dump a map's contents\n"
197 " get map : reports the keys and values matching a sample for a map\n"
198 " set map : modify map entry\n"
199 " add map : add map entry\n"
200 " del map : delete map entry\n"
201 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200202 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200203 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200204
Simon Horman9bd2c732011-06-15 15:18:44 +0900205static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200206 "Permission denied\n"
207 "";
208
Willy Tarreau295a8372011-03-10 11:25:07 +0100209/* data transmission states for the stats responses */
210enum {
211 STAT_ST_INIT = 0,
212 STAT_ST_HEAD,
213 STAT_ST_INFO,
214 STAT_ST_LIST,
215 STAT_ST_END,
216 STAT_ST_FIN,
217};
218
219/* data transmission states for the stats responses inside a proxy */
220enum {
221 STAT_PX_ST_INIT = 0,
222 STAT_PX_ST_TH,
223 STAT_PX_ST_FE,
224 STAT_PX_ST_LI,
225 STAT_PX_ST_SV,
226 STAT_PX_ST_BE,
227 STAT_PX_ST_END,
228 STAT_PX_ST_FIN,
229};
230
Cyril Bonté19979e12012-04-04 12:57:21 +0200231extern const char *stat_status_codes[];
232
Willy Tarreau07e9e642010-08-17 21:48:17 +0200233/* allocate a new stats frontend named <name>, and return it
234 * (or NULL in case of lack of memory).
235 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200236static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200237{
238 struct proxy *fe;
239
240 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
241 if (!fe)
242 return NULL;
243
Willy Tarreau237250c2011-07-29 01:49:03 +0200244 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200245 fe->next = proxy;
246 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200247 fe->last_change = now.tv_sec;
248 fe->id = strdup("GLOBAL");
249 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200250 fe->maxconn = 10; /* default to 10 concurrent connections */
251 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200252 fe->conf.file = strdup(file);
253 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100254 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100255 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200256
257 /* the stats frontend is the only one able to assign ID #0 */
258 fe->conf.id.key = fe->uuid = 0;
259 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200260 return fe;
261}
262
Willy Tarreaufbee7132007-10-18 13:53:22 +0200263/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200264 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
265 * error message into the <err> buffer which will be preallocated. The trailing
266 * '\n' must not be written. The function must be called with <args> pointing to
267 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200268 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200269static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200270 struct proxy *defpx, const char *file, int line,
271 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200272{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200273 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200274 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200275
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200276 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200277 int cur_arg;
278
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200279 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200280 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 +0200281 return -1;
282 }
283
Willy Tarreau89a63132009-08-16 17:41:45 +0200284 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200285 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200286 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200287 return -1;
288 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200289 }
290
Willy Tarreau4348fad2012-09-20 16:48:07 +0200291 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200292 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200293
Willy Tarreauc53d4222012-09-20 20:19:28 +0200294 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
295 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
296 file, line, args[0], args[1], err && *err ? *err : "error");
297 return -1;
298 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200299
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200300 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200301 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200302 static int bind_dumped;
303 struct bind_kw *kw;
304
305 kw = bind_find_kw(args[cur_arg]);
306 if (kw) {
307 if (!kw->parse) {
308 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
309 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200310 return -1;
311 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200312
313 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
314 if (err && *err)
315 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
316 else
317 memprintf(err, "'%s %s' : error encountered while processing '%s'",
318 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200319 return -1;
320 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200321
322 cur_arg += 1 + kw->skip;
323 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200324 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200325
326 if (!bind_dumped) {
327 bind_dump_kws(err);
328 indent_msg(err, 4);
329 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200330 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200331
332 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
333 args[0], args[1], args[cur_arg],
334 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
335 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200336 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100337
Willy Tarreauc53d4222012-09-20 20:19:28 +0200338 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
339 l->maxconn = global.stats_fe->maxconn;
340 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200341 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200342 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100343 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200344 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
345 l->nice = -64; /* we want to boost priority for local stats */
346 global.maxsock += l->maxconn;
347 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200348 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200349 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100350 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200351 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100352
353 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200354 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100355 return -1;
356 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200357
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100358 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200359 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200360 return -1;
361 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200362 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200363 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200364 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200365 return -1;
366 }
367 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200368 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200369 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200370 else if (!strcmp(args[1], "maxconn")) {
371 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200372
373 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200374 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200375 return -1;
376 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200377
378 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200379 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200380 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200381 return -1;
382 }
383 }
384 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200385 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200386 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
387 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100388 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200389
Willy Tarreau91319572013-04-20 09:48:50 +0200390 if (!global.stats_fe) {
391 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
392 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
393 return -1;
394 }
395 }
396
Willy Tarreau35b7b162012-10-22 23:17:18 +0200397 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100398 unsigned int low, high;
399
Willy Tarreau35b7b162012-10-22 23:17:18 +0200400 if (strcmp(args[cur_arg], "all") == 0) {
401 set = 0;
402 break;
403 }
404 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100405 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200406 }
407 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100408 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200409 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100410 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100411 char *dash = strchr(args[cur_arg], '-');
412
413 low = high = str2uic(args[cur_arg]);
414 if (dash)
415 high = str2uic(dash + 1);
416
417 if (high < low) {
418 unsigned int swap = low;
419 low = high;
420 high = swap;
421 }
422
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100423 if (low < 1 || high > LONGBITS) {
424 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
425 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200426 return -1;
427 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100428 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100429 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100430 }
431 else {
432 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100433 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
434 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100435 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200436 }
437 cur_arg++;
438 }
439 global.stats_fe->bind_proc = set;
440 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200441 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200442 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200443 return -1;
444 }
445 return 0;
446}
447
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100448/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
449 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100450 * NOTE: Some tools happen to rely on the field position instead of its name,
451 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100452 */
453static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100454{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100455 chunk_appendf(&trash,
456 "# pxname,svname,"
457 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100458 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100459 "bin,bout,"
460 "dreq,dresp,"
461 "ereq,econ,eresp,"
462 "wretr,wredis,"
463 "status,weight,act,bck,"
464 "chkfail,chkdown,lastchg,downtime,qlimit,"
465 "pid,iid,sid,throttle,lbtot,tracked,type,"
466 "rate,rate_lim,rate_max,"
467 "check_status,check_code,check_duration,"
468 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
469 "req_rate,req_rate_max,req_tot,"
470 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200471 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100472 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100473}
474
Simon Hormand9366582011-06-15 15:18:45 +0900475/* print a string of text buffer to <out>. The format is :
476 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
477 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
478 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
479 */
480static int dump_text(struct chunk *out, const char *buf, int bsize)
481{
482 unsigned char c;
483 int ptr = 0;
484
485 while (buf[ptr] && ptr < bsize) {
486 c = buf[ptr];
487 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
488 if (out->len > out->size - 1)
489 break;
490 out->str[out->len++] = c;
491 }
492 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
493 if (out->len > out->size - 2)
494 break;
495 out->str[out->len++] = '\\';
496 switch (c) {
497 case ' ': c = ' '; break;
498 case '\t': c = 't'; break;
499 case '\n': c = 'n'; break;
500 case '\r': c = 'r'; break;
501 case '\e': c = 'e'; break;
502 case '\\': c = '\\'; break;
503 }
504 out->str[out->len++] = c;
505 }
506 else {
507 if (out->len > out->size - 4)
508 break;
509 out->str[out->len++] = '\\';
510 out->str[out->len++] = 'x';
511 out->str[out->len++] = hextab[(c >> 4) & 0xF];
512 out->str[out->len++] = hextab[c & 0xF];
513 }
514 ptr++;
515 }
516
517 return ptr;
518}
519
520/* print a buffer in hexa.
521 * Print stopped if <bsize> is reached, or if no more place in the chunk.
522 */
523static int dump_binary(struct chunk *out, const char *buf, int bsize)
524{
525 unsigned char c;
526 int ptr = 0;
527
528 while (ptr < bsize) {
529 c = buf[ptr];
530
531 if (out->len > out->size - 2)
532 break;
533 out->str[out->len++] = hextab[(c >> 4) & 0xF];
534 out->str[out->len++] = hextab[c & 0xF];
535
536 ptr++;
537 }
538 return ptr;
539}
540
541/* Dump the status of a table to a stream interface's
542 * read buffer. It returns 0 if the output buffer is full
543 * and needs to be called again, otherwise non-zero.
544 */
545static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
546 struct proxy *proxy, struct proxy *target)
547{
Willy Tarreau87b09662015-04-03 00:22:06 +0200548 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900549
Willy Tarreau77804732012-10-29 16:14:26 +0100550 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900551 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
552
553 /* any other information should be dumped here */
554
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200555 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100556 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900557
Willy Tarreaubc18da12015-03-13 14:00:47 +0100558 if (bi_putchk(si_ic(si), msg) == -1) {
559 si->flags |= SI_FL_WAIT_ROOM;
Simon Hormand9366582011-06-15 15:18:45 +0900560 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100561 }
Simon Hormand9366582011-06-15 15:18:45 +0900562
563 return 1;
564}
565
566/* Dump the a table entry to a stream interface's
567 * read buffer. It returns 0 if the output buffer is full
568 * and needs to be called again, otherwise non-zero.
569 */
570static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
571 struct proxy *proxy, struct stksess *entry)
572{
573 int dt;
574
Willy Tarreau77804732012-10-29 16:14:26 +0100575 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900576
577 if (proxy->table.type == STKTABLE_TYPE_IP) {
578 char addr[INET_ADDRSTRLEN];
579 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100580 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900581 }
582 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
583 char addr[INET6_ADDRSTRLEN];
584 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100585 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900586 }
587 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100588 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900589 }
590 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100591 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900592 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
593 }
594 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100595 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900596 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
597 }
598
Willy Tarreau77804732012-10-29 16:14:26 +0100599 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900600
601 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
602 void *ptr;
603
604 if (proxy->table.data_ofs[dt] == 0)
605 continue;
606 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100607 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900608 else
Willy Tarreau77804732012-10-29 16:14:26 +0100609 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900610
611 ptr = stktable_data_ptr(&proxy->table, entry, dt);
612 switch (stktable_data_types[dt].std_type) {
613 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100614 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900615 break;
616 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100617 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900618 break;
619 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100620 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900621 break;
622 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100623 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900624 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
625 proxy->table.data_arg[dt].u));
626 break;
627 }
628 }
Willy Tarreau77804732012-10-29 16:14:26 +0100629 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900630
Willy Tarreaubc18da12015-03-13 14:00:47 +0100631 if (bi_putchk(si_ic(si), msg) == -1) {
632 si->flags |= SI_FL_WAIT_ROOM;
Simon Hormand9366582011-06-15 15:18:45 +0900633 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100634 }
Simon Hormand9366582011-06-15 15:18:45 +0900635
636 return 1;
637}
638
Willy Tarreaudec98142012-06-06 23:37:08 +0200639static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900640{
Willy Tarreau87b09662015-04-03 00:22:06 +0200641 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100642 struct appctx *appctx = __objt_appctx(si->end);
643 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900644 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900645 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900646 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200647 long long value;
648 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200649 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200650 void *ptr;
651 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900652
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100653 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900654
655 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100656 appctx->ctx.cli.msg = "Key value expected\n";
657 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900658 return;
659 }
660
Simon Hormanc5b89f62011-06-15 15:18:50 +0900661 switch (px->table.type) {
662 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900663 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100664 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900665 break;
666 case STKTABLE_TYPE_IPV6:
667 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100668 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900669 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900670 case STKTABLE_TYPE_INTEGER:
671 {
672 char *endptr;
673 unsigned long val;
674 errno = 0;
675 val = strtoul(args[4], &endptr, 10);
676 if ((errno == ERANGE && val == ULONG_MAX) ||
677 (errno != 0 && val == 0) || endptr == args[4] ||
678 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100679 appctx->ctx.cli.msg = "Invalid key\n";
680 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900681 return;
682 }
683 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100684 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900685 break;
686 }
687 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900688 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100689 static_table_key->key = args[4];
690 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900691 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900692 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200693 switch (action) {
694 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100695 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 +0200696 break;
697 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100698 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 +0200699 break;
700 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100701 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200702 break;
703 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100704 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900705 return;
706 }
707
708 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200709 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100710 appctx->ctx.cli.msg = stats_permission_denied_msg;
711 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900712 return;
713 }
714
Willy Tarreau07115412012-10-29 21:56:59 +0100715 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900716
Willy Tarreaudec98142012-06-06 23:37:08 +0200717 switch (action) {
718 case STAT_CLI_O_TAB:
719 if (!ts)
720 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100721 chunk_reset(&trash);
722 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900723 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100724 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900725 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200726
727 case STAT_CLI_O_CLR:
728 if (!ts)
729 return;
730 if (ts->ref_cnt) {
731 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100732 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
733 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200734 return;
735 }
736 stksess_kill(&px->table, ts);
737 break;
Simon Horman17bce342011-06-15 15:18:47 +0900738
Willy Tarreau654694e2012-06-07 01:03:16 +0200739 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200740 if (ts)
741 stktable_touch(&px->table, ts, 1);
742 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100743 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200744 if (!ts) {
745 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100746 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
747 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200748 return;
749 }
750 stktable_store(&px->table, ts, 1);
751 }
752
Willy Tarreau47060b62013-08-01 21:11:42 +0200753 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
754 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100755 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
756 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200757 return;
758 }
759
760 data_type = stktable_get_data_type(args[cur_arg] + 5);
761 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100762 appctx->ctx.cli.msg = "Unknown data type\n";
763 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200764 return;
765 }
766
767 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100768 appctx->ctx.cli.msg = "Data type not stored in this table\n";
769 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200770 return;
771 }
772
773 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100774 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
775 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200776 return;
777 }
778
779 ptr = stktable_data_ptr(&px->table, ts, data_type);
780
781 switch (stktable_data_types[data_type].std_type) {
782 case STD_T_SINT:
783 stktable_data_cast(ptr, std_t_sint) = value;
784 break;
785 case STD_T_UINT:
786 stktable_data_cast(ptr, std_t_uint) = value;
787 break;
788 case STD_T_ULL:
789 stktable_data_cast(ptr, std_t_ull) = value;
790 break;
791 case STD_T_FRQP:
792 /* We set both the current and previous values. That way
793 * the reported frequency is stable during all the period
794 * then slowly fades out. This allows external tools to
795 * push measures without having to update them too often.
796 */
797 frqp = &stktable_data_cast(ptr, std_t_frqp);
798 frqp->curr_tick = now_ms;
799 frqp->prev_ctr = 0;
800 frqp->curr_ctr = value;
801 break;
802 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200803 }
804 break;
805
Willy Tarreaudec98142012-06-06 23:37:08 +0200806 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100807 appctx->ctx.cli.msg = "Unknown action\n";
808 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200809 break;
Simon Horman121f3052011-06-15 15:18:46 +0900810 }
Simon Horman121f3052011-06-15 15:18:46 +0900811}
812
Willy Tarreau654694e2012-06-07 01:03:16 +0200813static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900814{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100815 struct appctx *appctx = __objt_appctx(si->end);
816
Willy Tarreau04b3a192013-04-13 09:41:37 +0200817 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100818 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
819 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200820 return;
821 }
822
Simon Hormand5b9fd92011-06-15 15:18:48 +0900823 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100824 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
825 if (appctx->ctx.table.data_type < 0) {
826 appctx->ctx.cli.msg = "Unknown data type\n";
827 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900828 return;
829 }
830
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100831 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
832 appctx->ctx.cli.msg = "Data type not stored in this table\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 appctx->ctx.table.data_op = get_std_op(args[4]);
838 if (appctx->ctx.table.data_op < 0) {
839 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
840 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900841 return;
842 }
843
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100844 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
845 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
846 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900847 return;
848 }
849}
850
Willy Tarreaudec98142012-06-06 23:37:08 +0200851static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900852{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100853 struct appctx *appctx = __objt_appctx(si->end);
854
855 appctx->ctx.table.data_type = -1;
856 appctx->st2 = STAT_ST_INIT;
857 appctx->ctx.table.target = NULL;
858 appctx->ctx.table.proxy = NULL;
859 appctx->ctx.table.entry = NULL;
860 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900861
862 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100863 appctx->ctx.table.target = find_stktable(args[2]);
864 if (!appctx->ctx.table.target) {
865 appctx->ctx.cli.msg = "No such table\n";
866 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900867 return;
868 }
869 }
870 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200871 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900872 goto err_args;
873 return;
874 }
875
876 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200877 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900878 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200879 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900880 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900881 goto err_args;
882
883 return;
884
885err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200886 switch (action) {
887 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100888 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 +0200889 break;
890 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100891 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 +0200892 break;
893 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100894 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200895 break;
896 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100897 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900898}
899
Willy Tarreau532a4502011-09-07 22:37:44 +0200900/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +0200901 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +0200902 * level to be admin.
903 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200904static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +0200905{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100906 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200907 struct proxy *px;
908
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200909 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100910 appctx->ctx.cli.msg = stats_permission_denied_msg;
911 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200912 return NULL;
913 }
914
915 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100916 appctx->ctx.cli.msg = "A frontend name is expected.\n";
917 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200918 return NULL;
919 }
920
921 px = findproxy(arg, PR_CAP_FE);
922 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100923 appctx->ctx.cli.msg = "No such frontend.\n";
924 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200925 return NULL;
926 }
927 return px;
928}
929
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200930/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
931 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +0200932 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200933 * the <arg> is modified to remove the '/'.
934 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200935static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200936{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100937 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200938 struct proxy *px;
939 struct server *sv;
940 char *line;
941
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200942 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100943 appctx->ctx.cli.msg = stats_permission_denied_msg;
944 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200945 return NULL;
946 }
947
948 /* split "backend/server" and make <line> point to server */
949 for (line = arg; *line; line++)
950 if (*line == '/') {
951 *line++ = '\0';
952 break;
953 }
954
955 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100956 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
957 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200958 return NULL;
959 }
960
961 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100962 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
963 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200964 return NULL;
965 }
966
967 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100968 appctx->ctx.cli.msg = "Proxy is disabled.\n";
969 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200970 return NULL;
971 }
972
973 return sv;
974}
975
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100976/* This function is used with map and acl management. It permits to browse
977 * each reference. The variable <getnext> must contain the current node,
978 * <end> point to the root node and the <flags> permit to filter required
979 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100980 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100981static inline
982struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
983 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100984{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100985 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100986
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100987 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100988
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100989 /* Get next list entry. */
990 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100991
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100992 /* If the entry is the last of the list, return NULL. */
993 if (&ref->list == end)
994 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100995
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100996 /* If the entry match the flag, return it. */
997 if (ref->flags & flags)
998 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100999 }
1000}
1001
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001002static inline
1003struct pat_ref *pat_ref_lookup_ref(const char *reference)
1004{
1005 int id;
1006 char *error;
1007
1008 /* If the reference starts by a '#', this is numeric id. */
1009 if (reference[0] == '#') {
1010 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1011 id = strtol(reference + 1, &error, 10);
1012 if (*error != '\0')
1013 return NULL;
1014
1015 /* Perform the unique id lookup. */
1016 return pat_ref_lookupid(id);
1017 }
1018
1019 /* Perform the string lookup. */
1020 return pat_ref_lookup(reference);
1021}
1022
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001023/* This function is used with map and acl management. It permits to browse
1024 * each reference.
1025 */
1026static inline
1027struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1028{
1029 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001030 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1031 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001032 return NULL;
1033 return expr;
1034}
1035
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001036/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001037 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001038 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001039 * designating the function which will have to process the request, which can
1040 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001041 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001042static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001043{
Willy Tarreau87b09662015-04-03 00:22:06 +02001044 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001045 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001046 char *args[MAX_STATS_ARGS + 1];
1047 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001048 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001049
1050 while (isspace((unsigned char)*line))
1051 line++;
1052
1053 arg = 0;
1054 args[arg] = line;
1055
1056 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001057 if (*line == '\\') {
1058 line++;
1059 if (*line == '\0')
1060 break;
1061 }
1062 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001063 *line++ = '\0';
1064
1065 while (isspace((unsigned char)*line))
1066 line++;
1067
1068 args[++arg] = line;
1069 continue;
1070 }
1071
1072 line++;
1073 }
1074
1075 while (++arg <= MAX_STATS_ARGS)
1076 args[arg] = line;
1077
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001078 /* remove \ */
1079 arg = 0;
1080 while (*args[arg] != '\0') {
1081 j = 0;
1082 for (i=0; args[arg][i] != '\0'; i++) {
1083 if (args[arg][i] == '\\')
1084 continue;
1085 args[arg][j] = args[arg][i];
1086 j++;
1087 }
1088 args[arg][j] = '\0';
1089 arg++;
1090 }
1091
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001092 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001093 if (strcmp(args[0], "show") == 0) {
1094 if (strcmp(args[1], "stat") == 0) {
1095 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001096 appctx->ctx.stats.flags |= STAT_BOUND;
1097 appctx->ctx.stats.iid = atoi(args[2]);
1098 appctx->ctx.stats.type = atoi(args[3]);
1099 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001100 }
1101
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001102 appctx->st2 = STAT_ST_INIT;
1103 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001104 }
1105 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001106 appctx->st2 = STAT_ST_INIT;
1107 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001108 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001109 else if (strcmp(args[1], "pools") == 0) {
1110 appctx->st2 = STAT_ST_INIT;
1111 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1112 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001113 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001114 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001115 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001116 appctx->ctx.cli.msg = stats_permission_denied_msg;
1117 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001118 return 1;
1119 }
Willy Tarreau76153662012-11-26 01:16:39 +01001120 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001121 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001122 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001123 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001124 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001125 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001126 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001127 appctx->ctx.sess.pos = 0;
1128 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001129 }
1130 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001131 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001132 appctx->ctx.cli.msg = stats_permission_denied_msg;
1133 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001134 return 1;
1135 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001136 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001137 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001138 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001139 appctx->ctx.errors.iid = -1;
1140 appctx->ctx.errors.px = NULL;
1141 appctx->st2 = STAT_ST_INIT;
1142 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001143 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001144 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001145 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001146 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001147 else if (strcmp(args[1], "map") == 0 ||
1148 strcmp(args[1], "acl") == 0) {
1149
1150 /* Set ACL or MAP flags. */
1151 if (args[1][0] == 'm')
1152 appctx->ctx.map.display_flags = PAT_REF_MAP;
1153 else
1154 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001155
1156 /* no parameter: display all map avalaible */
1157 if (!*args[2]) {
1158 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001159 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001160 return 1;
1161 }
1162
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001163 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001164 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001165 if (!appctx->ctx.map.ref ||
1166 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1167 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001168 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001169 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001170 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001171 appctx->st0 = STAT_CLI_PRINT;
1172 return 1;
1173 }
1174 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001175 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001176 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001177 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001178 return 0;
1179 }
1180 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001181 else if (strcmp(args[0], "clear") == 0) {
1182 if (strcmp(args[1], "counters") == 0) {
1183 struct proxy *px;
1184 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001185 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001186 int clrall = 0;
1187
1188 if (strcmp(args[2], "all") == 0)
1189 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001190
Willy Tarreau6162db22009-10-10 17:13:00 +02001191 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001192 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1193 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001194 appctx->ctx.cli.msg = stats_permission_denied_msg;
1195 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001196 return 1;
1197 }
1198
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001199 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001200 if (clrall) {
1201 memset(&px->be_counters, 0, sizeof(px->be_counters));
1202 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1203 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001204 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001205 px->be_counters.conn_max = 0;
1206 px->be_counters.p.http.rps_max = 0;
1207 px->be_counters.sps_max = 0;
1208 px->be_counters.cps_max = 0;
1209 px->be_counters.nbpend_max = 0;
1210
1211 px->fe_counters.conn_max = 0;
1212 px->fe_counters.p.http.rps_max = 0;
1213 px->fe_counters.sps_max = 0;
1214 px->fe_counters.cps_max = 0;
1215 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001216 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001217
1218 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001219 if (clrall)
1220 memset(&sv->counters, 0, sizeof(sv->counters));
1221 else {
1222 sv->counters.cur_sess_max = 0;
1223 sv->counters.nbpend_max = 0;
1224 sv->counters.sps_max = 0;
1225 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001226
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001227 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001228 if (li->counters) {
1229 if (clrall)
1230 memset(li->counters, 0, sizeof(*li->counters));
1231 else
1232 li->counters->conn_max = 0;
1233 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001234 }
1235
Willy Tarreau81c25d02011-09-07 15:17:21 +02001236 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001237 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001238 return 1;
1239 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001240 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001241 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001242 /* end of processing */
1243 return 1;
1244 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001245 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1246 /* Set ACL or MAP flags. */
1247 if (args[1][0] == 'm')
1248 appctx->ctx.map.display_flags = PAT_REF_MAP;
1249 else
1250 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001251
1252 /* no parameter */
1253 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001254 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1255 appctx->ctx.cli.msg = "Missing map identifier.\n";
1256 else
1257 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001258 appctx->st0 = STAT_CLI_PRINT;
1259 return 1;
1260 }
1261
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001262 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001263 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001264 if (!appctx->ctx.map.ref ||
1265 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1266 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001267 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001268 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001269 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001270 appctx->st0 = STAT_CLI_PRINT;
1271 return 1;
1272 }
1273
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001274 /* Clear all. */
1275 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001276
1277 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001278 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001279 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001280 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001281 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001282 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001283 return 0;
1284 }
1285 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001286 else if (strcmp(args[0], "get") == 0) {
1287 if (strcmp(args[1], "weight") == 0) {
1288 struct proxy *px;
1289 struct server *sv;
1290
1291 /* split "backend/server" and make <line> point to server */
1292 for (line = args[2]; *line; line++)
1293 if (*line == '/') {
1294 *line++ = '\0';
1295 break;
1296 }
1297
1298 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001299 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1300 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001301 return 1;
1302 }
1303
1304 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001305 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1306 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001307 return 1;
1308 }
1309
1310 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001311 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001312 if (bi_putstr(si_ic(si), trash.str) == -1)
1313 si->flags |= SI_FL_WAIT_ROOM;
1314
Willy Tarreau38338fa2009-10-10 18:37:29 +02001315 return 1;
1316 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001317 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1318 /* Set flags. */
1319 if (args[1][0] == 'm')
1320 appctx->ctx.map.display_flags = PAT_REF_MAP;
1321 else
1322 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001323
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001324 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001325 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001326 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1327 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1328 else
1329 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001330 appctx->st0 = STAT_CLI_PRINT;
1331 return 1;
1332 }
1333
1334 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001335 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001336 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001337 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001338 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001339 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001340 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001341 appctx->st0 = STAT_CLI_PRINT;
1342 return 1;
1343 }
1344
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001345 /* copy input string. The string must be allocated because
1346 * it may be used over multiple iterations. It's released
1347 * at the end and upon abort anyway.
1348 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001349 appctx->ctx.map.chunk.len = strlen(args[3]);
1350 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1351 appctx->ctx.map.chunk.str = strdup(args[3]);
1352 if (!appctx->ctx.map.chunk.str) {
1353 appctx->ctx.cli.msg = "Out of memory error.\n";
1354 appctx->st0 = STAT_CLI_PRINT;
1355 return 1;
1356 }
1357
1358 /* prepare response */
1359 appctx->st2 = STAT_ST_INIT;
1360 appctx->st0 = STAT_CLI_O_MLOOK;
1361 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001362 else { /* not "get weight" */
1363 return 0;
1364 }
1365 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001366 else if (strcmp(args[0], "set") == 0) {
1367 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001368 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001369 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001370
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001371 sv = expect_server_admin(s, si, args[2]);
1372 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001373 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001374
Simon Horman7d09b9a2013-02-12 10:45:51 +09001375 warning = server_parse_weight_change_request(sv, args[3]);
1376 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001377 appctx->ctx.cli.msg = warning;
1378 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001379 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001380 return 1;
1381 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001382 else if (strcmp(args[1], "server") == 0) {
1383 struct server *sv;
1384 const char *warning;
1385
1386 sv = expect_server_admin(s, si, args[2]);
1387 if (!sv)
1388 return 1;
1389
1390 if (strcmp(args[3], "weight") == 0) {
1391 warning = server_parse_weight_change_request(sv, args[4]);
1392 if (warning) {
1393 appctx->ctx.cli.msg = warning;
1394 appctx->st0 = STAT_CLI_PRINT;
1395 }
1396 }
1397 else if (strcmp(args[3], "state") == 0) {
1398 if (strcmp(args[4], "ready") == 0)
1399 srv_adm_set_ready(sv);
1400 else if (strcmp(args[4], "drain") == 0)
1401 srv_adm_set_drain(sv);
1402 else if (strcmp(args[4], "maint") == 0)
1403 srv_adm_set_maint(sv);
1404 else {
1405 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1406 appctx->st0 = STAT_CLI_PRINT;
1407 }
1408 }
1409 else if (strcmp(args[3], "health") == 0) {
1410 if (sv->track) {
1411 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1412 appctx->st0 = STAT_CLI_PRINT;
1413 }
1414 else if (strcmp(args[4], "up") == 0) {
1415 sv->check.health = sv->check.rise + sv->check.fall - 1;
1416 srv_set_running(sv, "changed from CLI");
1417 }
1418 else if (strcmp(args[4], "stopping") == 0) {
1419 sv->check.health = sv->check.rise + sv->check.fall - 1;
1420 srv_set_stopping(sv, "changed from CLI");
1421 }
1422 else if (strcmp(args[4], "down") == 0) {
1423 sv->check.health = 0;
1424 srv_set_stopped(sv, "changed from CLI");
1425 }
1426 else {
1427 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1428 appctx->st0 = STAT_CLI_PRINT;
1429 }
1430 }
1431 else if (strcmp(args[3], "agent") == 0) {
1432 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1433 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1434 appctx->st0 = STAT_CLI_PRINT;
1435 }
1436 else if (strcmp(args[4], "up") == 0) {
1437 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1438 srv_set_running(sv, "changed from CLI");
1439 }
1440 else if (strcmp(args[4], "down") == 0) {
1441 sv->agent.health = 0;
1442 srv_set_stopped(sv, "changed from CLI");
1443 }
1444 else {
1445 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1446 appctx->st0 = STAT_CLI_PRINT;
1447 }
1448 }
1449 else {
1450 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1451 appctx->st0 = STAT_CLI_PRINT;
1452 }
1453 return 1;
1454 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001455 else if (strcmp(args[1], "timeout") == 0) {
1456 if (strcmp(args[2], "cli") == 0) {
1457 unsigned timeout;
1458 const char *res;
1459
1460 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001461 appctx->ctx.cli.msg = "Expects an integer value.\n";
1462 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001463 return 1;
1464 }
1465
1466 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1467 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001468 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1469 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001470 return 1;
1471 }
1472
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001473 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001474 return 1;
1475 }
1476 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001477 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1478 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001479 return 1;
1480 }
1481 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001482 else if (strcmp(args[1], "maxconn") == 0) {
1483 if (strcmp(args[2], "frontend") == 0) {
1484 struct proxy *px;
1485 struct listener *l;
1486 int v;
1487
Willy Tarreau532a4502011-09-07 22:37:44 +02001488 px = expect_frontend_admin(s, si, args[3]);
1489 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001490 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001491
1492 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001493 appctx->ctx.cli.msg = "Integer value expected.\n";
1494 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001495 return 1;
1496 }
1497
1498 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001499 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001500 appctx->ctx.cli.msg = "Value out of range.\n";
1501 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001502 return 1;
1503 }
1504
1505 /* OK, the value is fine, so we assign it to the proxy and to all of
1506 * its listeners. The blocked ones will be dequeued.
1507 */
1508 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001509 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001510 l->maxconn = v;
1511 if (l->state == LI_FULL)
1512 resume_listener(l);
1513 }
1514
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001515 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1516 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001517
1518 return 1;
1519 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001520 else if (strcmp(args[2], "global") == 0) {
1521 int v;
1522
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001523 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001524 appctx->ctx.cli.msg = stats_permission_denied_msg;
1525 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001526 return 1;
1527 }
1528
1529 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001530 appctx->ctx.cli.msg = "Expects an integer value.\n";
1531 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001532 return 1;
1533 }
1534
1535 v = atoi(args[3]);
1536 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001537 appctx->ctx.cli.msg = "Value out of range.\n";
1538 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001539 return 1;
1540 }
1541
1542 /* check for unlimited values */
1543 if (v <= 0)
1544 v = global.hardmaxconn;
1545
1546 global.maxconn = v;
1547
1548 /* Dequeues all of the listeners waiting for a resource */
1549 if (!LIST_ISEMPTY(&global_listener_queue))
1550 dequeue_all_listeners(&global_listener_queue);
1551
1552 return 1;
1553 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001554 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001555 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1556 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001557 return 1;
1558 }
1559 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001560 else if (strcmp(args[1], "rate-limit") == 0) {
1561 if (strcmp(args[2], "connections") == 0) {
1562 if (strcmp(args[3], "global") == 0) {
1563 int v;
1564
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001565 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001566 appctx->ctx.cli.msg = stats_permission_denied_msg;
1567 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001568 return 1;
1569 }
1570
1571 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001572 appctx->ctx.cli.msg = "Expects an integer value.\n";
1573 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001574 return 1;
1575 }
1576
1577 v = atoi(args[4]);
1578 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001579 appctx->ctx.cli.msg = "Value out of range.\n";
1580 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001581 return 1;
1582 }
1583
1584 global.cps_lim = v;
1585
1586 /* Dequeues all of the listeners waiting for a resource */
1587 if (!LIST_ISEMPTY(&global_listener_queue))
1588 dequeue_all_listeners(&global_listener_queue);
1589
1590 return 1;
1591 }
1592 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001593 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1594 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001595 return 1;
1596 }
1597 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001598 else if (strcmp(args[2], "sessions") == 0) {
1599 if (strcmp(args[3], "global") == 0) {
1600 int v;
1601
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001602 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001603 appctx->ctx.cli.msg = stats_permission_denied_msg;
1604 appctx->st0 = STAT_CLI_PRINT;
1605 return 1;
1606 }
1607
1608 if (!*args[4]) {
1609 appctx->ctx.cli.msg = "Expects an integer value.\n";
1610 appctx->st0 = STAT_CLI_PRINT;
1611 return 1;
1612 }
1613
1614 v = atoi(args[4]);
1615 if (v < 0) {
1616 appctx->ctx.cli.msg = "Value out of range.\n";
1617 appctx->st0 = STAT_CLI_PRINT;
1618 return 1;
1619 }
1620
1621 global.sps_lim = v;
1622
1623 /* Dequeues all of the listeners waiting for a resource */
1624 if (!LIST_ISEMPTY(&global_listener_queue))
1625 dequeue_all_listeners(&global_listener_queue);
1626
1627 return 1;
1628 }
1629 else {
1630 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1631 appctx->st0 = STAT_CLI_PRINT;
1632 return 1;
1633 }
1634 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001635#ifdef USE_OPENSSL
1636 else if (strcmp(args[2], "ssl-sessions") == 0) {
1637 if (strcmp(args[3], "global") == 0) {
1638 int v;
1639
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001640 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001641 appctx->ctx.cli.msg = stats_permission_denied_msg;
1642 appctx->st0 = STAT_CLI_PRINT;
1643 return 1;
1644 }
1645
1646 if (!*args[4]) {
1647 appctx->ctx.cli.msg = "Expects an integer value.\n";
1648 appctx->st0 = STAT_CLI_PRINT;
1649 return 1;
1650 }
1651
1652 v = atoi(args[4]);
1653 if (v < 0) {
1654 appctx->ctx.cli.msg = "Value out of range.\n";
1655 appctx->st0 = STAT_CLI_PRINT;
1656 return 1;
1657 }
1658
1659 global.ssl_lim = v;
1660
1661 /* Dequeues all of the listeners waiting for a resource */
1662 if (!LIST_ISEMPTY(&global_listener_queue))
1663 dequeue_all_listeners(&global_listener_queue);
1664
1665 return 1;
1666 }
1667 else {
1668 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1669 appctx->st0 = STAT_CLI_PRINT;
1670 return 1;
1671 }
1672 }
1673#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001674 else if (strcmp(args[2], "http-compression") == 0) {
1675 if (strcmp(args[3], "global") == 0) {
1676 int v;
1677
Willy Tarreau85d47f92012-11-21 00:29:50 +01001678 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001679 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1680 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001681 return 1;
1682 }
1683
William Lallemandd85f9172012-11-09 17:05:39 +01001684 v = atoi(args[4]);
1685 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1686 }
1687 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001688 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1689 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001690 return 1;
1691 }
1692 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001693 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001694 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001695 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001696 return 1;
1697 }
1698 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001699 else if (strcmp(args[1], "table") == 0) {
1700 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1701 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001702 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001703 char *err;
1704
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001705 /* Set flags. */
1706 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001707
1708 /* Expect three parameters: map name, key and new value. */
1709 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001710 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001711 appctx->st0 = STAT_CLI_PRINT;
1712 return 1;
1713 }
1714
1715 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001716 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001717 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001718 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001719 appctx->st0 = STAT_CLI_PRINT;
1720 return 1;
1721 }
1722
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001723 /* If the entry identifier start with a '#', it is considered as
1724 * pointer id
1725 */
1726 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1727 struct pat_ref_elt *ref;
1728 long long int conv;
1729 char *error;
1730
1731 /* Convert argument to integer value. */
1732 conv = strtoll(&args[3][1], &error, 16);
1733 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001734 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001735 appctx->st0 = STAT_CLI_PRINT;
1736 return 1;
1737 }
1738
1739 /* Convert and check integer to pointer. */
1740 ref = (struct pat_ref_elt *)(long)conv;
1741 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001742 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001743 appctx->st0 = STAT_CLI_PRINT;
1744 return 1;
1745 }
1746
1747 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001748 err = NULL;
1749 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1750 if (err)
1751 memprintf(&err, "%s.\n", err);
1752 appctx->ctx.cli.err = err;
1753 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001754 return 1;
1755 }
1756 }
1757 else {
1758 /* Else, use the entry identifier as pattern
1759 * string, and update the value.
1760 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001761 err = NULL;
1762 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1763 if (err)
1764 memprintf(&err, "%s.\n", err);
1765 appctx->ctx.cli.err = err;
1766 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001767 return 1;
1768 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001769 }
1770
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001771 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001772 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001773 return 1;
1774 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001775#ifdef USE_OPENSSL
1776 else if (strcmp(args[1], "ssl") == 0) {
1777 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01001778#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001779 char *err = NULL;
1780
Emeric Brunaf4ef742014-06-19 14:10:45 +02001781 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001782 if (!*args[3]) {
1783 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1784 appctx->st0 = STAT_CLI_PRINT;
1785 return 1;
1786 }
1787
1788 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1789 if (trash.len < 0) {
1790 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1791 appctx->st0 = STAT_CLI_PRINT;
1792 return 1;
1793 }
1794
1795 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1796 if (err) {
1797 memprintf(&err, "%s.\n", err);
1798 appctx->ctx.cli.err = err;
1799 appctx->st0 = STAT_CLI_PRINT_FREE;
1800 }
1801 return 1;
1802 }
1803 appctx->ctx.cli.msg = "OCSP Response updated!";
1804 appctx->st0 = STAT_CLI_PRINT;
1805 return 1;
1806#else
1807 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1808 appctx->st0 = STAT_CLI_PRINT;
1809 return 1;
1810#endif
1811 }
1812 else {
1813 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1814 appctx->st0 = STAT_CLI_PRINT;
1815 return 1;
1816 }
1817 }
1818#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001819 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001820 return 0;
1821 }
1822 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001823 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001824 if (strcmp(args[1], "agent") == 0) {
1825 struct server *sv;
1826
1827 sv = expect_server_admin(s, si, args[2]);
1828 if (!sv)
1829 return 1;
1830
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001831 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1832 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1833 appctx->st0 = STAT_CLI_PRINT;
1834 return 1;
1835 }
1836
1837 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001838 return 1;
1839 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001840 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001841 struct server *sv;
1842
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001843 sv = expect_server_admin(s, si, args[2]);
1844 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001845 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001846
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001847 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1848 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1849 appctx->st0 = STAT_CLI_PRINT;
1850 return 1;
1851 }
1852
1853 sv->check.state |= CHK_ST_ENABLED;
1854 return 1;
1855 }
1856 else if (strcmp(args[1], "server") == 0) {
1857 struct server *sv;
1858
1859 sv = expect_server_admin(s, si, args[2]);
1860 if (!sv)
1861 return 1;
1862
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001863 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001864 return 1;
1865 }
1866 else if (strcmp(args[1], "frontend") == 0) {
1867 struct proxy *px;
1868
1869 px = expect_frontend_admin(s, si, args[2]);
1870 if (!px)
1871 return 1;
1872
1873 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001874 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1875 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001876 return 1;
1877 }
1878
1879 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001880 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1881 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001882 return 1;
1883 }
1884
1885 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001886 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1887 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001888 return 1;
1889 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001890 return 1;
1891 }
1892 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001893 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001894 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001895 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001896 }
1897 }
1898 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001899 if (strcmp(args[1], "agent") == 0) {
1900 struct server *sv;
1901
1902 sv = expect_server_admin(s, si, args[2]);
1903 if (!sv)
1904 return 1;
1905
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001906 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001907 return 1;
1908 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001909 else if (strcmp(args[1], "health") == 0) {
1910 struct server *sv;
1911
1912 sv = expect_server_admin(s, si, args[2]);
1913 if (!sv)
1914 return 1;
1915
1916 sv->check.state &= ~CHK_ST_ENABLED;
1917 return 1;
1918 }
Simon Horman671b6f02013-11-25 10:46:39 +09001919 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001920 struct server *sv;
1921
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001922 sv = expect_server_admin(s, si, args[2]);
1923 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001924 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001925
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001926 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001927 return 1;
1928 }
1929 else if (strcmp(args[1], "frontend") == 0) {
1930 struct proxy *px;
1931
1932 px = expect_frontend_admin(s, si, args[2]);
1933 if (!px)
1934 return 1;
1935
1936 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001937 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1938 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001939 return 1;
1940 }
1941
1942 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001943 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1944 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001945 return 1;
1946 }
1947
1948 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001949 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1950 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001951 return 1;
1952 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001953 return 1;
1954 }
1955 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001956 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001957 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001958 return 1;
1959 }
1960 }
1961 else if (strcmp(args[0], "shutdown") == 0) {
1962 if (strcmp(args[1], "frontend") == 0) {
1963 struct proxy *px;
1964
1965 px = expect_frontend_admin(s, si, args[2]);
1966 if (!px)
1967 return 1;
1968
1969 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001970 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1971 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001972 return 1;
1973 }
1974
1975 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1976 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1977 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1978 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1979 stop_proxy(px);
1980 return 1;
1981 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001982 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001983 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001984
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001985 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001986 appctx->ctx.cli.msg = stats_permission_denied_msg;
1987 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001988 return 1;
1989 }
1990
1991 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001992 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1993 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001994 return 1;
1995 }
1996
1997 ptr = (void *)strtoul(args[2], NULL, 0);
1998
Willy Tarreau87b09662015-04-03 00:22:06 +02001999 /* first, look for the requested stream in the stream table */
2000 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002001 if (sess == ptr)
2002 break;
2003 }
2004
Willy Tarreau87b09662015-04-03 00:22:06 +02002005 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002006 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002007 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2008 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002009 return 1;
2010 }
2011
Willy Tarreaue7dff022015-04-03 01:14:29 +02002012 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002013 return 1;
2014 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002015 else if (strcmp(args[1], "sessions") == 0) {
2016 if (strcmp(args[2], "server") == 0) {
2017 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002018 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002019
2020 sv = expect_server_admin(s, si, args[3]);
2021 if (!sv)
2022 return 1;
2023
Willy Tarreau87b09662015-04-03 00:22:06 +02002024 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002025 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2026 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002027 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002028
2029 return 1;
2030 }
2031 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002032 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2033 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002034 return 1;
2035 }
2036 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002037 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002038 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2039 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002040 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002041 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002042 }
2043 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002044 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2045 if (args[1][0] == 'm')
2046 appctx->ctx.map.display_flags = PAT_REF_MAP;
2047 else
2048 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002049
2050 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002051 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2052 if (!*args[2] || !*args[3]) {
2053 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2054 appctx->st0 = STAT_CLI_PRINT;
2055 return 1;
2056 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002057 }
2058
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002059 else {
2060 if (!*args[2] || !*args[3]) {
2061 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2062 appctx->st0 = STAT_CLI_PRINT;
2063 return 1;
2064 }
2065 }
2066
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002067 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002068 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002069 if (!appctx->ctx.map.ref ||
2070 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002071 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002072 appctx->st0 = STAT_CLI_PRINT;
2073 return 1;
2074 }
2075
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002076 /* If the entry identifier start with a '#', it is considered as
2077 * pointer id
2078 */
2079 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2080 struct pat_ref_elt *ref;
2081 long long int conv;
2082 char *error;
2083
2084 /* Convert argument to integer value. */
2085 conv = strtoll(&args[3][1], &error, 16);
2086 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002087 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002088 appctx->st0 = STAT_CLI_PRINT;
2089 return 1;
2090 }
2091
2092 /* Convert and check integer to pointer. */
2093 ref = (struct pat_ref_elt *)(long)conv;
2094 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002095 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002096 appctx->st0 = STAT_CLI_PRINT;
2097 return 1;
2098 }
2099
2100 /* Try to delete the entry. */
2101 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2102 /* The entry is not found, send message. */
2103 appctx->ctx.cli.msg = "Key not found.\n";
2104 appctx->st0 = STAT_CLI_PRINT;
2105 return 1;
2106 }
2107 }
2108 else {
2109 /* Else, use the entry identifier as pattern
2110 * string and try to delete the entry.
2111 */
2112 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2113 /* The entry is not found, send message. */
2114 appctx->ctx.cli.msg = "Key not found.\n";
2115 appctx->st0 = STAT_CLI_PRINT;
2116 return 1;
2117 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002118 }
2119
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002120 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002121 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002122 return 1;
2123 }
2124 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002125 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002126 appctx->st0 = STAT_CLI_PRINT;
2127 return 1;
2128 }
2129 }
2130 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002131 if (strcmp(args[1], "map") == 0 ||
2132 strcmp(args[1], "acl") == 0) {
2133 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002134 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002135
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002136 /* Set flags. */
2137 if (args[1][0] == 'm')
2138 appctx->ctx.map.display_flags = PAT_REF_MAP;
2139 else
2140 appctx->ctx.map.display_flags = PAT_REF_ACL;
2141
2142 /* If the keywork is "map", we expect three parameters, if it
2143 * is "acl", we expect only two parameters
2144 */
2145 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2146 if (!*args[2] || !*args[3] || !*args[4]) {
2147 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2148 appctx->st0 = STAT_CLI_PRINT;
2149 return 1;
2150 }
2151 }
2152 else {
2153 if (!*args[2] || !*args[3]) {
2154 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2155 appctx->st0 = STAT_CLI_PRINT;
2156 return 1;
2157 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002158 }
2159
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002160 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002161 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002162 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002163 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002164 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002165 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002166 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002167 appctx->st0 = STAT_CLI_PRINT;
2168 return 1;
2169 }
2170
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002171 /* The command "add acl" is prohibited if the reference
2172 * use samples.
2173 */
2174 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2175 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2176 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2177 "You must use the command 'add map' to add values.\n";
2178 appctx->st0 = STAT_CLI_PRINT;
2179 return 1;
2180 }
2181
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002182 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002183 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002184 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002185 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002186 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002187 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002188 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002189 if (err)
2190 memprintf(&err, "%s.\n", err);
2191 appctx->ctx.cli.err = err;
2192 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002193 return 1;
2194 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002195
2196 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002197 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002198 return 1;
2199 }
2200 else { /* unknown "del" parameter */
2201 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2202 appctx->st0 = STAT_CLI_PRINT;
2203 return 1;
2204 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002205 }
2206 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002207 return 0;
2208 }
2209 return 1;
2210}
2211
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002212/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002213 * used to processes I/O from/to the stats unix socket. The system relies on a
2214 * state machine handling requests and various responses. We read a request,
2215 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002216 * Then we can read again. The state is stored in appctx->st0 and is one of the
2217 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002218 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002219 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002220static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002221{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002222 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002223 struct channel *req = si_oc(si);
2224 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002225 int reql;
2226 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002227
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002228 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2229 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002230
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002231 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002232 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002233 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002234 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2235 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002236 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002237 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002238 /* Let's close for real now. We just close the request
2239 * side, the conditions below will complete if needed.
2240 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002241 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002242 break;
2243 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002244 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002245 /* ensure we have some output room left in the event we
2246 * would want to return some info right after parsing.
2247 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002248 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002249 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002250 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002251 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002252
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002253 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002254 if (reql <= 0) { /* closed or EOL not found */
2255 if (reql == 0)
2256 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002257 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002258 continue;
2259 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002260
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002261 /* seek for a possible semi-colon. If we find one, we
2262 * replace it with an LF and skip only this part.
2263 */
2264 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002265 if (trash.str[len] == ';') {
2266 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002267 reql = len + 1;
2268 break;
2269 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002270
Willy Tarreau816fc222009-10-04 07:36:58 +02002271 /* now it is time to check that we have a full line,
2272 * remove the trailing \n and possibly \r, then cut the
2273 * line.
2274 */
2275 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002276 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002277 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002278 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002279 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002280
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002281 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002282 len--;
2283
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002284 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002285
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002286 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002287 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002288 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002289 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002290 continue;
2291 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002292 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002293 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002294 else if (strcmp(trash.str, "help") == 0 ||
2295 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002296 appctx->ctx.cli.msg = stats_sock_usage_msg;
2297 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002298 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002299 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002300 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002301 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002302 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002303 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002304 /* if prompt is disabled, print help on empty lines,
2305 * so that the user at least knows how to enable
2306 * prompt and find help.
2307 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002308 appctx->ctx.cli.msg = stats_sock_usage_msg;
2309 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002310 }
2311
2312 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002313 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002314 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002315 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002316 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002317 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau44cf5452014-10-22 19:06:31 +02002318 cli_release_handler(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002319 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002320 continue;
2321 }
2322
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002323 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002324 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002325 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002326 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002327 else
2328 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002329 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002330 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002331 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002332 free(appctx->ctx.cli.err);
2333 appctx->st0 = STAT_CLI_PROMPT;
2334 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002335 else
2336 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002337 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002338 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002339 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002340 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002341 break;
2342 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002343 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002344 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002345 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002346 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002347 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002348 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002349 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002350 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002351 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002352 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002353 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002354 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002355 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002356 if (stats_table_request(si, appctx->st0))
2357 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002358 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002359 case STAT_CLI_O_PATS:
2360 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002361 appctx->st0 = STAT_CLI_PROMPT;
2362 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002363 case STAT_CLI_O_PAT:
2364 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002365 appctx->st0 = STAT_CLI_PROMPT;
2366 break;
2367 case STAT_CLI_O_MLOOK:
2368 if (stats_map_lookup(si))
2369 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002370 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002371 case STAT_CLI_O_POOLS:
2372 if (stats_dump_pools_to_buffer(si))
2373 appctx->st0 = STAT_CLI_PROMPT;
2374 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002375 default: /* abnormal state */
Willy Tarreau44cf5452014-10-22 19:06:31 +02002376 cli_release_handler(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002377 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002378 break;
2379 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002380
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002381 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002382 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002383 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002384 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002385 else
2386 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002387 }
2388
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002389 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002390 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002391 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002392
2393 /* Now we close the output if one of the writers did so,
2394 * or if we're not in interactive mode and the request
2395 * buffer is empty. This still allows pipelined requests
2396 * to be sent in non-interactive mode.
2397 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002398 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2399 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002400 continue;
2401 }
2402
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002403 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002404 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002405 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002406 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002407
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002408 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002409 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2410 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002411 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002412 * and nothing more to consume. This is comparable to a broken pipe, so
2413 * we forward the close to the request side so that it flows upstream to
2414 * the client.
2415 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002416 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002417 }
2418
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002419 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002420 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2421 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2422 /* We have no more processing to do, and nothing more to send, and
2423 * the client side has closed. So we'll forward this state downstream
2424 * on the response buffer.
2425 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002426 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002427 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002428 }
2429
2430 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002431 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002432
2433 /* we don't want to expire timeouts while we're processing requests */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002434 si_ic(si)->rex = TICK_ETERNITY;
2435 si_oc(si)->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002436
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002437 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002438 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 +02002439 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002440 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002441
2442 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2443 /* check that we have released everything then unregister */
2444 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002445 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002446}
2447
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002448/* This function dumps information onto the stream interface's read buffer.
2449 * It returns 0 as long as it does not complete, non-zero upon completion.
2450 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002451 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002452static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002453{
2454 unsigned int up = (now.tv_sec - start_date.tv_sec);
2455
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002456#ifdef USE_OPENSSL
2457 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2458 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2459 int ssl_reuse = 0;
2460
2461 if (ssl_key_rate < ssl_sess_rate) {
2462 /* count the ssl reuse ratio and avoid overflows in both directions */
2463 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2464 }
2465#endif
2466
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002467 chunk_printf(&trash,
2468 "Name: " PRODUCT_NAME "\n"
2469 "Version: " HAPROXY_VERSION "\n"
2470 "Release_date: " HAPROXY_DATE "\n"
2471 "Nbproc: %d\n"
2472 "Process_num: %d\n"
2473 "Pid: %d\n"
2474 "Uptime: %dd %dh%02dm%02ds\n"
2475 "Uptime_sec: %d\n"
2476 "Memmax_MB: %d\n"
2477 "Ulimit-n: %d\n"
2478 "Maxsock: %d\n"
2479 "Maxconn: %d\n"
2480 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002481 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002482 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002483 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002484#ifdef USE_OPENSSL
2485 "MaxSslConns: %d\n"
2486 "CurrSslConns: %d\n"
2487 "CumSslConns: %d\n"
2488#endif
2489 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002490 "PipesUsed: %d\n"
2491 "PipesFree: %d\n"
2492 "ConnRate: %d\n"
2493 "ConnRateLimit: %d\n"
2494 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002495 "SessRate: %d\n"
2496 "SessRateLimit: %d\n"
2497 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002498#ifdef USE_OPENSSL
2499 "SslRate: %d\n"
2500 "SslRateLimit: %d\n"
2501 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002502 "SslFrontendKeyRate: %d\n"
2503 "SslFrontendMaxKeyRate: %d\n"
2504 "SslFrontendSessionReuse_pct: %d\n"
2505 "SslBackendKeyRate: %d\n"
2506 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002507 "SslCacheLookups: %u\n"
2508 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002509#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002510 "CompressBpsIn: %u\n"
2511 "CompressBpsOut: %u\n"
2512 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002513#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002514 "ZlibMemUsage: %ld\n"
2515 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002516#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002517 "Tasks: %d\n"
2518 "Run_queue: %d\n"
2519 "Idle_pct: %d\n"
2520 "node: %s\n"
2521 "description: %s\n"
2522 "",
2523 global.nbproc,
2524 relative_pid,
2525 pid,
2526 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2527 up,
2528 global.rlimit_memmax,
2529 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002530 global.maxsock, global.maxconn, global.hardmaxconn,
2531 actconn, totalconn, global.req_count,
2532#ifdef USE_OPENSSL
2533 global.maxsslconn, sslconns, totalsslconns,
2534#endif
2535 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002536 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002537 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002538#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002539 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2540 ssl_key_rate, global.ssl_fe_keys_max,
2541 ssl_reuse,
2542 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002543 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002544#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002545 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2546 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002547#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002548 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002549#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002550 nb_tasks_cur, run_queue_cur, idle_pct,
2551 global.node, global.desc ? global.desc : ""
2552 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002553
Willy Tarreaubc18da12015-03-13 14:00:47 +01002554 if (bi_putchk(si_ic(si), &trash) == -1) {
2555 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002556 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002557 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002558
2559 return 1;
2560}
2561
Willy Tarreau12833bb2014-01-28 16:49:56 +01002562/* This function dumps memory usage information onto the stream interface's
2563 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2564 * completion. No state is used.
2565 */
2566static int stats_dump_pools_to_buffer(struct stream_interface *si)
2567{
2568 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01002569 if (bi_putchk(si_ic(si), &trash) == -1) {
2570 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002571 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002572 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01002573 return 1;
2574}
2575
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002576/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2577 * the state from stream interface <si>. The caller is responsible for clearing
2578 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002579 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002580static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002581{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002582 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002583 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002584
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002585 if (!(px->cap & PR_CAP_FE))
2586 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002587
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002588 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002589 return 0;
2590
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002591 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002592 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002593 /* name, queue */
2594 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002595
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002596 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002597 /* Column sub-heading for Enable or Disable server */
2598 chunk_appendf(&trash, "<td></td>");
2599 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002600
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002601 chunk_appendf(&trash,
2602 "<td class=ac>"
2603 "<a name=\"%s/Frontend\"></a>"
2604 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2605 "<td colspan=3></td>"
2606 "",
2607 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002608
Willy Tarreau466c9b52012-12-23 02:25:03 +01002609 chunk_appendf(&trash,
2610 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002611 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002612 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2613 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2614 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002615 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2616 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2617 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002618
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002619 if (px->mode == PR_MODE_HTTP)
2620 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002621 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002622 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002623
2624 chunk_appendf(&trash,
2625 "</table></div></u></td>"
2626 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002627 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002628 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2629 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2630 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002631 U2H(px->fe_counters.sps_max),
2632 U2H(px->fe_counters.cps_max),
2633 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002634
2635 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002636 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002637 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002638 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002639
2640 chunk_appendf(&trash,
2641 "</table></div></u></td>"
2642 /* sessions rate : limit */
2643 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002644 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002645
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002646 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002647 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002648 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002649 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002650 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2651 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002652 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002653 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2654 U2H(px->fe_counters.cum_sess),
2655 U2H(px->fe_counters.cum_conn),
2656 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002657
Willy Tarreau466c9b52012-12-23 02:25:03 +01002658 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002659 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002660 chunk_appendf(&trash,
2661 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2662 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2663 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2664 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2665 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2666 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2667 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2668 "<tr><th>- other responses:</th><td>%s</td></tr>"
2669 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2670 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002671 U2H(px->fe_counters.p.http.cum_req),
2672 U2H(px->fe_counters.p.http.rsp[1]),
2673 U2H(px->fe_counters.p.http.rsp[2]),
2674 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002675 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002676 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002677 U2H(px->fe_counters.p.http.rsp[3]),
2678 U2H(px->fe_counters.p.http.rsp[4]),
2679 U2H(px->fe_counters.p.http.rsp[5]),
2680 U2H(px->fe_counters.p.http.rsp[0]),
2681 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002682 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002683
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002684 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002685 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002686 /* sessions: lbtot, lastsess */
2687 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002688 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002689 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002690 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002691 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002692
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002693 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002694 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002695 "<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 +01002696 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002697 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002698 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2699 px->fe_counters.comp_in ?
2700 (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 +01002701 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002702
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002703 chunk_appendf(&trash,
2704 /* denied: req, resp */
2705 "<td>%s</td><td>%s</td>"
2706 /* errors : request, connect, response */
2707 "<td>%s</td><td></td><td></td>"
2708 /* warnings: retries, redispatches */
2709 "<td></td><td></td>"
2710 /* server status : reflect frontend status */
2711 "<td class=ac>%s</td>"
2712 /* rest of server: nothing */
2713 "<td class=ac colspan=8></td></tr>"
2714 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002715 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2716 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002717 px->state == PR_STREADY ? "OPEN" :
2718 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002719 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002720 else { /* CSV mode */
2721 chunk_appendf(&trash,
2722 /* pxid, name, queue cur, queue max, */
2723 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002724 /* sessions : current, max, limit, total */
2725 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002726 /* bytes : in, out */
2727 "%lld,%lld,"
2728 /* denied: req, resp */
2729 "%lld,%lld,"
2730 /* errors : request, connect, response */
2731 "%lld,,,"
2732 /* warnings: retries, redispatches */
2733 ",,"
2734 /* server status : reflect frontend status */
2735 "%s,"
2736 /* rest of server: nothing */
2737 ",,,,,,,,"
2738 /* pid, iid, sid, throttle, lbtot, tracked, type */
2739 "%d,%d,0,,,,%d,"
2740 /* rate, rate_lim, rate_max */
2741 "%u,%u,%u,"
2742 /* check_status, check_code, check_duration */
2743 ",,,",
2744 px->id,
2745 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2746 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2747 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2748 px->fe_counters.failed_req,
2749 px->state == PR_STREADY ? "OPEN" :
2750 px->state == PR_STFULL ? "FULL" : "STOP",
2751 relative_pid, px->uuid, STATS_TYPE_FE,
2752 read_freq_ctr(&px->fe_sess_per_sec),
2753 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002754
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002755 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2756 if (px->mode == PR_MODE_HTTP) {
2757 for (i=1; i<6; i++)
2758 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2759 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2760 }
2761 else
2762 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002763
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002764 /* failed health analyses */
2765 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002766
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002767 /* requests : req_rate, req_rate_max, req_tot, */
2768 chunk_appendf(&trash, "%u,%u,%lld,",
2769 read_freq_ctr(&px->fe_req_per_sec),
2770 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2771
2772 /* errors: cli_aborts, srv_aborts */
2773 chunk_appendf(&trash, ",,");
2774
2775 /* compression: in, out, bypassed */
2776 chunk_appendf(&trash, "%lld,%lld,%lld,",
2777 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2778
2779 /* compression: comp_rsp */
2780 chunk_appendf(&trash, "%lld,",
2781 px->fe_counters.p.http.comp_rsp);
2782
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002783 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2784 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002785
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002786 /* finish with EOL */
2787 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002788 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002789 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002790}
2791
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002792/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2793 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2794 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2795 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002796 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002797static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002798{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002799 struct appctx *appctx = __objt_appctx(si->end);
2800
2801 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002802 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002803 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002804 /* Column sub-heading for Enable or Disable server */
2805 chunk_appendf(&trash, "<td></td>");
2806 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002807 chunk_appendf(&trash,
2808 /* frontend name, listener name */
2809 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2810 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2811 "",
2812 px->id, l->name,
2813 (flags & ST_SHLGNDS)?"<u>":"",
2814 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002815
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002816 if (flags & ST_SHLGNDS) {
2817 char str[INET6_ADDRSTRLEN];
2818 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002819
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002820 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002821
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002822 port = get_host_port(&l->addr);
2823 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2824 case AF_INET:
2825 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2826 break;
2827 case AF_INET6:
2828 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2829 break;
2830 case AF_UNIX:
2831 chunk_appendf(&trash, "unix, ");
2832 break;
2833 case -1:
2834 chunk_appendf(&trash, "(%s), ", strerror(errno));
2835 break;
2836 }
Willy Tarreau91861262007-10-17 17:06:05 +02002837
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002838 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002839 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002840 }
Willy Tarreau91861262007-10-17 17:06:05 +02002841
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002842 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002843 /* queue */
2844 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002845 /* sessions rate: current, max, limit */
2846 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002847 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002848 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002849 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002850 /* bytes: in, out */
2851 "<td>%s</td><td>%s</td>"
2852 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002853 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002854 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2855 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002856
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002857 chunk_appendf(&trash,
2858 /* denied: req, resp */
2859 "<td>%s</td><td>%s</td>"
2860 /* errors: request, connect, response */
2861 "<td>%s</td><td></td><td></td>"
2862 /* warnings: retries, redispatches */
2863 "<td></td><td></td>"
2864 /* server status: reflect listener status */
2865 "<td class=ac>%s</td>"
2866 /* rest of server: nothing */
2867 "<td class=ac colspan=8></td></tr>"
2868 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002869 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2870 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002871 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2872 }
2873 else { /* CSV mode */
2874 chunk_appendf(&trash,
2875 /* pxid, name, queue cur, queue max, */
2876 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002877 /* sessions: current, max, limit, total */
2878 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002879 /* bytes: in, out */
2880 "%lld,%lld,"
2881 /* denied: req, resp */
2882 "%lld,%lld,"
2883 /* errors: request, connect, response */
2884 "%lld,,,"
2885 /* warnings: retries, redispatches */
2886 ",,"
2887 /* server status: reflect listener status */
2888 "%s,"
2889 /* rest of server: nothing */
2890 ",,,,,,,,"
2891 /* pid, iid, sid, throttle, lbtot, tracked, type */
2892 "%d,%d,%d,,,,%d,"
2893 /* rate, rate_lim, rate_max */
2894 ",,,"
2895 /* check_status, check_code, check_duration */
2896 ",,,"
2897 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2898 ",,,,,,"
2899 /* failed health analyses */
2900 ","
2901 /* requests : req_rate, req_rate_max, req_tot, */
2902 ",,,"
2903 /* errors: cli_aborts, srv_aborts */
2904 ",,"
2905 /* compression: in, out, bypassed, comp_rsp */
2906 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002907 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2908 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002909 "\n",
2910 px->id, l->name,
2911 l->nbconn, l->counters->conn_max,
2912 l->maxconn, l->counters->cum_conn,
2913 l->counters->bytes_in, l->counters->bytes_out,
2914 l->counters->denied_req, l->counters->denied_resp,
2915 l->counters->failed_req,
2916 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2917 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2918 }
2919 return 1;
2920}
Willy Tarreau91861262007-10-17 17:06:05 +02002921
Simon Horman4d2eab62015-04-23 14:51:26 +09002922enum srv_stats_state {
2923 SRV_STATS_STATE_DOWN = 0,
2924 SRV_STATS_STATE_DOWN_AGENT,
2925 SRV_STATS_STATE_GOING_UP,
2926 SRV_STATS_STATE_UP_GOING_DOWN,
2927 SRV_STATS_STATE_UP,
2928 SRV_STATS_STATE_NOLB_GOING_DOWN,
2929 SRV_STATS_STATE_NOLB,
2930 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2931 SRV_STATS_STATE_DRAIN,
2932 SRV_STATS_STATE_NO_CHECK,
2933
2934 SRV_STATS_STATE_COUNT, /* Must be last */
2935};
2936
Simon Horman837bfa72015-04-23 14:51:27 +09002937enum srv_stats_colour {
2938 SRV_STATS_COLOUR_DOWN = 0,
2939 SRV_STATS_COLOUR_GOING_UP,
2940 SRV_STATS_COLOUR_GOING_DOWN,
2941 SRV_STATS_COLOUR_UP,
2942 SRV_STATS_COLOUR_NOLB,
2943 SRV_STATS_COLOUR_DRAINING,
2944 SRV_STATS_COLOUR_NO_CHECK,
2945
2946 SRV_STATS_COLOUR_COUNT, /* Must be last */
2947};
2948
2949static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
2950 [SRV_STATS_COLOUR_DOWN] = "down",
2951 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
2952 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
2953 [SRV_STATS_COLOUR_UP] = "up",
2954 [SRV_STATS_COLOUR_NOLB] = "nolb",
2955 [SRV_STATS_COLOUR_DRAINING] = "draining",
2956 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
2957};
2958
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002959/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2960 * from stream interface <si>, stats flags <flags>, and server state <state>.
2961 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09002962 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002963 */
Simon Horman4d2eab62015-04-23 14:51:26 +09002964static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09002965 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002966{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002967 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002968 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002969 char str[INET6_ADDRSTRLEN];
2970 struct chunk src;
2971 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002972
Willy Tarreau32091232014-05-16 13:52:00 +02002973 /* we have "via" which is the tracked server as described in the configuration,
2974 * and "ref" which is the checked server and the end of the chain.
2975 */
2976 via = sv->track ? sv->track : sv;
2977 ref = via;
2978 while (ref->track)
2979 ref = ref->track;
2980
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002981 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09002982 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2983 [SRV_STATS_STATE_DOWN] = "DOWN",
2984 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2985 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
2986 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
2987 [SRV_STATS_STATE_UP] = "UP",
2988 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
2989 [SRV_STATS_STATE_NOLB] = "NOLB",
2990 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
2991 [SRV_STATS_STATE_DRAIN] = "DRAIN",
2992 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002993 };
Willy Tarreau91861262007-10-17 17:06:05 +02002994
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002995 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002996 chunk_appendf(&trash, "<tr class=\"maintain\">");
2997 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002998 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09002999 "<tr class=\"%s_%s\">",
3000 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003001
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003002 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003003 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003004 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3005 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003006
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003007 chunk_appendf(&trash,
3008 "<td class=ac><a name=\"%s/%s\"></a>%s"
3009 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3010 "",
3011 px->id, sv->id,
3012 (flags & ST_SHLGNDS) ? "<u>" : "",
3013 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003014
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003015 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003016 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003017
3018 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3019 case AF_INET:
3020 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3021 break;
3022 case AF_INET6:
3023 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3024 break;
3025 case AF_UNIX:
3026 chunk_appendf(&trash, "unix, ");
3027 break;
3028 case -1:
3029 chunk_appendf(&trash, "(%s), ", strerror(errno));
3030 break;
3031 default: /* address family not supported */
3032 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003033 }
Willy Tarreau91861262007-10-17 17:06:05 +02003034
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003035 /* id */
3036 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003037
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003038 /* cookie */
3039 if (sv->cookie) {
3040 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003041
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003042 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3043 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003044
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003045 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003046 }
3047
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003048 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003049 }
Willy Tarreau91861262007-10-17 17:06:05 +02003050
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003051 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003052 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003053 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003054 /* sessions rate : current, max, limit */
3055 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003056 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003057 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003058 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3059 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003060
Willy Tarreau466c9b52012-12-23 02:25:03 +01003061
3062 chunk_appendf(&trash,
3063 /* sessions: current, max, limit, total */
3064 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003065 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003066 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3067 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003068 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3069 U2H(sv->counters.cum_sess),
3070 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003071
Willy Tarreau466c9b52012-12-23 02:25:03 +01003072 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3073 if (px->mode == PR_MODE_HTTP) {
3074 unsigned long long tot;
3075 for (tot = i = 0; i < 6; i++)
3076 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003077
Willy Tarreau466c9b52012-12-23 02:25:03 +01003078 chunk_appendf(&trash,
3079 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3080 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3081 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3082 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3083 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3084 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3085 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3086 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003087 U2H(tot),
3088 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3089 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3090 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3091 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3092 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3093 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 +02003094 }
Willy Tarreau91861262007-10-17 17:06:05 +02003095
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003096 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3097 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)));
3098 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)));
3099 if (px->mode == PR_MODE_HTTP)
3100 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)));
3101 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)));
3102
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003103 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003104 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003105 /* sessions: lbtot, last */
3106 "<td>%s</td><td>%s</td>",
3107 U2H(sv->counters.cum_lbconn),
3108 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003109
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003110 chunk_appendf(&trash,
3111 /* bytes : in, out */
3112 "<td>%s</td><td>%s</td>"
3113 /* denied: req, resp */
3114 "<td></td><td>%s</td>"
3115 /* errors : request, connect */
3116 "<td></td><td>%s</td>"
3117 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003118 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003119 /* warnings: retries, redispatches */
3120 "<td>%lld</td><td>%lld</td>"
3121 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003122 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3123 U2H(sv->counters.failed_secu),
3124 U2H(sv->counters.failed_conns),
3125 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003126 sv->counters.cli_aborts,
3127 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003128 sv->counters.retries, sv->counters.redispatches);
3129
3130 /* status, lest check */
3131 chunk_appendf(&trash, "<td class=ac>");
3132
Willy Tarreau20125212014-05-13 19:44:56 +02003133 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003134 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3135 chunk_appendf(&trash, "MAINT");
3136 }
Simon Horman0766e442014-11-12 15:55:54 +09003137 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003138 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003139 /* DOWN (agent) */
3140 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 +02003141 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003142 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003143 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3144 chunk_appendf(&trash,
3145 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003146 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3147 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003148 }
Willy Tarreau91861262007-10-17 17:06:05 +02003149
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003150 if ((sv->state == SRV_ST_STOPPED) &&
3151 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3152 chunk_appendf(&trash,
3153 "</td><td class=ac><u> %s%s",
3154 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3155 get_check_status_info(sv->agent.status));
3156
3157 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3158 chunk_appendf(&trash, "/%d", sv->agent.code);
3159
3160 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3161 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3162
3163 chunk_appendf(&trash, "<div class=tips>%s",
3164 get_check_status_description(sv->agent.status));
3165 if (*sv->agent.desc) {
3166 chunk_appendf(&trash, ": ");
3167 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3168 chunk_htmlencode(&trash, &src);
3169 }
3170 chunk_appendf(&trash, "</div></u>");
3171 }
3172 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003173 chunk_appendf(&trash,
3174 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003175 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003176 get_check_status_info(sv->check.status));
3177
3178 if (sv->check.status >= HCHK_STATUS_L57DATA)
3179 chunk_appendf(&trash, "/%d", sv->check.code);
3180
3181 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003182 chunk_appendf(&trash, " in %lums", sv->check.duration);
3183
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003184 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003185 get_check_status_description(sv->check.status));
3186 if (*sv->check.desc) {
3187 chunk_appendf(&trash, ": ");
3188 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3189 chunk_htmlencode(&trash, &src);
3190 }
3191 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003192 }
3193 else
3194 chunk_appendf(&trash, "</td><td>");
3195
3196 chunk_appendf(&trash,
3197 /* weight */
3198 "</td><td class=ac>%d</td>"
3199 /* act, bck */
3200 "<td class=ac>%s</td><td class=ac>%s</td>"
3201 "",
3202 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003203 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3204 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003205
3206 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003207 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003208 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003209
3210 if (ref->observe)
3211 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3212
3213 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003214 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003215 "<td>%lld</td><td>%s</td>"
3216 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003217 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003218 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3219 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003220 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3221 /* tracking a server */
3222 chunk_appendf(&trash,
3223 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003224 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003225 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003226 else
3227 chunk_appendf(&trash, "<td colspan=3></td>");
3228
3229 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003230 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003231 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003232 else
3233 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3234 }
3235 else { /* CSV mode */
Simon Horman4d2eab62015-04-23 14:51:26 +09003236 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3237 [SRV_STATS_STATE_DOWN] = "DOWN,",
3238 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3239 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3240 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3241 [SRV_STATS_STATE_UP] = "UP,",
3242 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3243 [SRV_STATS_STATE_NOLB] = "NOLB,",
3244 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3245 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
3246 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003247 };
3248
3249 chunk_appendf(&trash,
3250 /* pxid, name */
3251 "%s,%s,"
3252 /* queue : current, max */
3253 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003254 /* sessions : current, max, limit, total */
3255 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003256 /* bytes : in, out */
3257 "%lld,%lld,"
3258 /* denied: req, resp */
3259 ",%lld,"
3260 /* errors : request, connect, response */
3261 ",%lld,%lld,"
3262 /* warnings: retries, redispatches */
3263 "%lld,%lld,"
3264 "",
3265 px->id, sv->id,
3266 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003267 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003268 sv->counters.bytes_in, sv->counters.bytes_out,
3269 sv->counters.failed_secu,
3270 sv->counters.failed_conns, sv->counters.failed_resp,
3271 sv->counters.retries, sv->counters.redispatches);
3272
3273 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003274 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003275 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003276 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003277 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003278 else
3279 chunk_appendf(&trash,
3280 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003281 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3282 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003283
3284 chunk_appendf(&trash,
3285 /* weight, active, backup */
3286 "%d,%d,%d,"
3287 "",
3288 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003289 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3290 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003291
3292 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003293 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003294 chunk_appendf(&trash,
3295 "%lld,%lld,%d,%d,",
3296 sv->counters.failed_checks, sv->counters.down_trans,
3297 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3298 else
3299 chunk_appendf(&trash, ",,,,");
3300
3301 /* queue limit, pid, iid, sid, */
3302 chunk_appendf(&trash,
3303 "%s,"
3304 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003305 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003306 relative_pid, px->uuid, sv->puid);
3307
3308 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003309 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003310 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003311
3312 /* sessions: lbtot */
3313 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3314
3315 /* tracked */
3316 if (sv->track)
3317 chunk_appendf(&trash, "%s/%s,",
3318 sv->track->proxy->id, sv->track->id);
3319 else
3320 chunk_appendf(&trash, ",");
3321
3322 /* type */
3323 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3324
3325 /* rate */
3326 chunk_appendf(&trash, "%u,,%u,",
3327 read_freq_ctr(&sv->sess_per_sec),
3328 sv->counters.sps_max);
3329
Willy Tarreauff5ae352013-12-11 20:36:34 +01003330 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003331 /* check_status */
3332 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3333
3334 /* check_code */
3335 if (sv->check.status >= HCHK_STATUS_L57DATA)
3336 chunk_appendf(&trash, "%u,", sv->check.code);
3337 else
3338 chunk_appendf(&trash, ",");
3339
3340 /* check_duration */
3341 if (sv->check.status >= HCHK_STATUS_CHECKED)
3342 chunk_appendf(&trash, "%lu,", sv->check.duration);
3343 else
3344 chunk_appendf(&trash, ",");
3345
3346 }
3347 else
3348 chunk_appendf(&trash, ",,,");
3349
3350 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3351 if (px->mode == PR_MODE_HTTP) {
3352 for (i=1; i<6; i++)
3353 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3354
3355 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3356 }
3357 else
3358 chunk_appendf(&trash, ",,,,,,");
3359
3360 /* failed health analyses */
3361 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3362
3363 /* requests : req_rate, req_rate_max, req_tot, */
3364 chunk_appendf(&trash, ",,,");
3365
3366 /* errors: cli_aborts, srv_aborts */
3367 chunk_appendf(&trash, "%lld,%lld,",
3368 sv->counters.cli_aborts, sv->counters.srv_aborts);
3369
3370 /* compression: in, out, bypassed, comp_rsp */
3371 chunk_appendf(&trash, ",,,,");
3372
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003373 /* lastsess */
3374 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3375
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003376 /* capture of last check and agent statuses */
3377 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->check.desc) : "");
3378 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->agent.desc) : "");
3379
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003380 /* qtime, ctime, rtime, ttime, */
3381 chunk_appendf(&trash, "%u,%u,%u,%u,",
3382 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3383 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3384 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3385 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3386
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003387 /* finish with EOL */
3388 chunk_appendf(&trash, "\n");
3389 }
3390 return 1;
3391}
3392
3393/* Dumps a line for backend <px> to the trash for and uses the state from stream
3394 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3395 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3396 */
3397static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3398{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003399 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003400 struct chunk src;
3401 int i;
3402
3403 if (!(px->cap & PR_CAP_BE))
3404 return 0;
3405
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003406 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003407 return 0;
3408
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003409 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003410 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003411 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003412 /* Column sub-heading for Enable or Disable server */
3413 chunk_appendf(&trash, "<td></td>");
3414 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003415 chunk_appendf(&trash,
3416 "<td class=ac>"
3417 /* name */
3418 "%s<a name=\"%s/Backend\"></a>"
3419 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3420 "",
3421 (flags & ST_SHLGNDS)?"<u>":"",
3422 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003423
3424 if (flags & ST_SHLGNDS) {
3425 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003426 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003427 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3428
3429 /* cookie */
3430 if (px->cookie_name) {
3431 chunk_appendf(&trash, ", cookie: '");
3432 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3433 chunk_htmlencode(&trash, &src);
3434 chunk_appendf(&trash, "'");
3435 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003436 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003437 }
3438
3439 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003440 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003441 /* queue : current, max */
3442 "<td>%s</td><td>%s</td><td></td>"
3443 /* sessions rate : current, max, limit */
3444 "<td>%s</td><td>%s</td><td></td>"
3445 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003446 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003447 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3448 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003449
3450 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003451 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003452 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003453 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003454 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003455 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003456 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3457 U2H(px->be_counters.cum_conn),
3458 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003459
Willy Tarreau466c9b52012-12-23 02:25:03 +01003460 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003461 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003462 chunk_appendf(&trash,
3463 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3464 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3465 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3466 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3467 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3468 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3469 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3470 "<tr><th>- other responses:</th><td>%s</td></tr>"
3471 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003472 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003473 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003474 U2H(px->be_counters.p.http.cum_req),
3475 U2H(px->be_counters.p.http.rsp[1]),
3476 U2H(px->be_counters.p.http.rsp[2]),
3477 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003478 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003479 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003480 U2H(px->be_counters.p.http.rsp[3]),
3481 U2H(px->be_counters.p.http.rsp[4]),
3482 U2H(px->be_counters.p.http.rsp[5]),
3483 U2H(px->be_counters.p.http.rsp[0]),
3484 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003485 }
3486
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003487 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)));
3488 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)));
3489 if (px->mode == PR_MODE_HTTP)
3490 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)));
3491 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)));
3492
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003493 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003494 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003495 /* sessions: lbtot, last */
3496 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003497 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003498 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003499 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003500 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003501 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003502 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003503
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003504 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003505 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003506 "<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 +01003507 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003508 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003509 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3510 px->be_counters.comp_in ?
3511 (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 +01003512 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3513
3514 chunk_appendf(&trash,
3515 /* denied: req, resp */
3516 "<td>%s</td><td>%s</td>"
3517 /* errors : request, connect */
3518 "<td></td><td>%s</td>"
3519 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003520 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003521 /* warnings: retries, redispatches */
3522 "<td>%lld</td><td>%lld</td>"
3523 /* backend status: reflect backend status (up/down): we display UP
3524 * if the backend has known working servers or if it has no server at
3525 * all (eg: for stats). Then we display the total weight, number of
3526 * active and backups. */
3527 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3528 "<td class=ac>%d</td><td class=ac>%d</td>"
3529 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003530 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3531 U2H(px->be_counters.failed_conns),
3532 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003533 px->be_counters.cli_aborts,
3534 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003535 px->be_counters.retries, px->be_counters.redispatches,
3536 human_time(now.tv_sec - px->last_change, 1),
3537 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3538 "<font color=\"red\"><b>DOWN</b></font>",
3539 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3540 px->srv_act, px->srv_bck);
3541
3542 chunk_appendf(&trash,
3543 /* rest of backend: nothing, down transitions, total downtime, throttle */
3544 "<td class=ac>&nbsp;</td><td>%d</td>"
3545 "<td>%s</td>"
3546 "<td></td>"
3547 "</tr>",
3548 px->down_trans,
3549 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3550 }
3551 else { /* CSV mode */
3552 chunk_appendf(&trash,
3553 /* pxid, name */
3554 "%s,BACKEND,"
3555 /* queue : current, max */
3556 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003557 /* sessions : current, max, limit, total */
3558 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003559 /* bytes : in, out */
3560 "%lld,%lld,"
3561 /* denied: req, resp */
3562 "%lld,%lld,"
3563 /* errors : request, connect, response */
3564 ",%lld,%lld,"
3565 /* warnings: retries, redispatches */
3566 "%lld,%lld,"
3567 /* backend status: reflect backend status (up/down): we display UP
3568 * if the backend has known working servers or if it has no server at
3569 * all (eg: for stats). Then we display the total weight, number of
3570 * active and backups. */
3571 "%s,"
3572 "%d,%d,%d,"
3573 /* rest of backend: nothing, down transitions, last change, total downtime */
3574 ",%d,%d,%d,,"
3575 /* pid, iid, sid, throttle, lbtot, tracked, type */
3576 "%d,%d,0,,%lld,,%d,"
3577 /* rate, rate_lim, rate_max, */
3578 "%u,,%u,"
3579 /* check_status, check_code, check_duration */
3580 ",,,",
3581 px->id,
3582 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3583 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3584 px->be_counters.bytes_in, px->be_counters.bytes_out,
3585 px->be_counters.denied_req, px->be_counters.denied_resp,
3586 px->be_counters.failed_conns, px->be_counters.failed_resp,
3587 px->be_counters.retries, px->be_counters.redispatches,
3588 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3589 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3590 px->srv_act, px->srv_bck,
3591 px->down_trans, (int)(now.tv_sec - px->last_change),
3592 px->srv?be_downtime(px):0,
3593 relative_pid, px->uuid,
3594 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3595 read_freq_ctr(&px->be_sess_per_sec),
3596 px->be_counters.sps_max);
3597
3598 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3599 if (px->mode == PR_MODE_HTTP) {
3600 for (i=1; i<6; i++)
3601 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3602 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3603 }
3604 else
3605 chunk_appendf(&trash, ",,,,,,");
3606
3607 /* failed health analyses */
3608 chunk_appendf(&trash, ",");
3609
3610 /* requests : req_rate, req_rate_max, req_tot, */
3611 chunk_appendf(&trash, ",,,");
3612
3613 /* errors: cli_aborts, srv_aborts */
3614 chunk_appendf(&trash, "%lld,%lld,",
3615 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3616
3617 /* compression: in, out, bypassed */
3618 chunk_appendf(&trash, "%lld,%lld,%lld,",
3619 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3620
3621 /* compression: comp_rsp */
3622 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3623
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003624 /* lastsess, last_chk, last_agt, */
3625 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003626
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003627 /* qtime, ctime, rtime, ttime, */
3628 chunk_appendf(&trash, "%u,%u,%u,%u,",
3629 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3630 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3631 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3632 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3633
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003634 /* finish with EOL */
3635 chunk_appendf(&trash, "\n");
3636 }
3637 return 1;
3638}
3639
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003640/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003641 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003642 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003643 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003644static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003645{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003646 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003647 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3648
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003649 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003650 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003651
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003652 /* 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 +02003653 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003654 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003655 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003656 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 +01003657 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003658 }
3659
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003660 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05003661 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003662 }
3663
3664 /* print a new table */
3665 chunk_appendf(&trash,
3666 "<table class=\"tbl\" width=\"100%%\">\n"
3667 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003668 "<th class=\"pxname\" width=\"10%%\">");
3669
3670 chunk_appendf(&trash,
3671 "<a name=\"%s\"></a>%s"
3672 "<a class=px href=\"#%s\">%s</a>",
3673 px->id,
3674 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3675 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003676
3677 if (uri->flags & ST_SHLGNDS) {
3678 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003679 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003680 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3681 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003682 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003683 }
3684
3685 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003686 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003687 "<th class=\"%s\" width=\"90%%\">%s</th>"
3688 "</tr>\n"
3689 "</table>\n"
3690 "<table class=\"tbl\" width=\"100%%\">\n"
3691 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003692 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3693 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3694
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003695 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003696 /* Column heading for Enable or Disable server */
3697 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003698 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003699
3700 chunk_appendf(&trash,
3701 "<th rowspan=2></th>"
3702 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003703 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003704 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3705 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3706 "<th colspan=9>Server</th>"
3707 "</tr>\n"
3708 "<tr class=\"titre\">"
3709 "<th>Cur</th><th>Max</th><th>Limit</th>"
3710 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003711 "<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 +01003712 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3713 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3714 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3715 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3716 "<th>Thrtle</th>\n"
3717 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003718}
3719
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003720/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003721 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003722 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003723static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003724{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003725 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003726 chunk_appendf(&trash, "</table>");
3727
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003728 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003729 /* close the form used to enable/disable this proxy servers */
3730 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003731 "Choose the action to perform on the checked servers : "
3732 "<select name=action>"
3733 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003734 "<option value=\"ready\">Set state to READY</option>"
3735 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02003736 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003737 "<option value=\"dhlth\">Health: disable checks</option>"
3738 "<option value=\"ehlth\">Health: enable checks</option>"
3739 "<option value=\"hrunn\">Health: force UP</option>"
3740 "<option value=\"hnolb\">Health: force NOLB</option>"
3741 "<option value=\"hdown\">Health: force DOWN</option>"
3742 "<option value=\"dagent\">Agent: disable checks</option>"
3743 "<option value=\"eagent\">Agent: enable checks</option>"
3744 "<option value=\"arunn\">Agent: force UP</option>"
3745 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003746 "<option value=\"shutdown\">Kill Sessions</option>"
3747 "</select>"
3748 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3749 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3750 "</form>",
3751 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003752 }
3753
3754 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003755}
Willy Tarreau91861262007-10-17 17:06:05 +02003756
3757/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003758 * Dumps statistics for a proxy. The output is sent to the stream interface's
3759 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3760 * buffer space, or non-zero if everything completed. This function is used
3761 * both by the CLI and the HTTP entry points, and is able to dump the output
3762 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003763 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003764static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003765{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003766 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02003767 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003768 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02003769 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003770 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003771
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003772 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003773
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003774 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003775 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003776 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003777 if (uri && uri->scope) {
3778 /* we have a limited scope, we have to check the proxy name */
3779 struct stat_scope *scope;
3780 int len;
3781
3782 len = strlen(px->id);
3783 scope = uri->scope;
3784
3785 while (scope) {
3786 /* match exact proxy name */
3787 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3788 break;
3789
3790 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003791 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003792 break;
3793 scope = scope->next;
3794 }
3795
3796 /* proxy name not found : don't dump anything */
3797 if (scope == NULL)
3798 return 1;
3799 }
3800
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003801 /* if the user has requested a limited output and the proxy
3802 * name does not match, skip it.
3803 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003804 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003805 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 +02003806 return 1;
3807
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003808 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3809 (appctx->ctx.stats.iid != -1) &&
3810 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003811 return 1;
3812
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003813 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003814 /* fall through */
3815
Willy Tarreau295a8372011-03-10 11:25:07 +01003816 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003817 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003818 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003819 if (bi_putchk(rep, &trash) == -1) {
3820 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003821 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003822 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003823 }
Willy Tarreau91861262007-10-17 17:06:05 +02003824
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003825 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003826 /* fall through */
3827
Willy Tarreau295a8372011-03-10 11:25:07 +01003828 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003829 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003830 if (stats_dump_fe_stats(si, px)) {
3831 if (bi_putchk(rep, &trash) == -1) {
3832 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau91861262007-10-17 17:06:05 +02003833 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003834 }
3835 }
Willy Tarreau91861262007-10-17 17:06:05 +02003836
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003837 appctx->ctx.stats.l = px->conf.listeners.n;
3838 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003839 /* fall through */
3840
Willy Tarreau295a8372011-03-10 11:25:07 +01003841 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003842 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003843 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003844 if (buffer_almost_full(rep->buf)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01003845 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003846 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003847 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003848
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003849 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003850 if (!l->counters)
3851 continue;
3852
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003853 if (appctx->ctx.stats.flags & STAT_BOUND) {
3854 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003855 break;
3856
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003857 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003858 continue;
3859 }
3860
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003861 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003862 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
3863 if (bi_putchk(rep, &trash) == -1) {
3864 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003865 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003866 }
3867 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003868 }
3869
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003870 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3871 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003872 /* fall through */
3873
Willy Tarreau295a8372011-03-10 11:25:07 +01003874 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003875 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003876 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003877 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09003878 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02003879
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003880 if (buffer_almost_full(rep->buf)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01003881 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003882 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003883 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003884
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003885 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003886
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003887 if (appctx->ctx.stats.flags & STAT_BOUND) {
3888 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003889 break;
3890
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003891 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003892 continue;
3893 }
3894
Willy Tarreau32091232014-05-16 13:52:00 +02003895 svs = sv;
3896 while (svs->track)
3897 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003898
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003899 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003900 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09003901 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003902 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09003903 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
3904 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09003905 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09003906 sv_colour = SRV_STATS_COLOUR_UP;
3907 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01003908
Simon Horman4d2eab62015-04-23 14:51:26 +09003909 if (server_is_draining(sv)) {
Simon Horman837bfa72015-04-23 14:51:27 +09003910 if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003911 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09003912 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09003913 sv_state = SRV_STATS_STATE_DRAIN;
Simon Horman837bfa72015-04-23 14:51:27 +09003914 sv_colour = SRV_STATS_COLOUR_DRAINING;
3915 }
Simon Horman4d2eab62015-04-23 14:51:26 +09003916 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003917
Simon Horman837bfa72015-04-23 14:51:27 +09003918 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003919 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09003920 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
3921 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01003922 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003923 else if (sv->state == SRV_ST_STOPPING) {
3924 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09003925 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003926 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09003927 sv_colour = SRV_STATS_COLOUR_NOLB;
3928 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09003929 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09003930 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
3931 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003932 }
3933 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09003934 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003935 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09003936 sv_colour = SRV_STATS_COLOUR_DOWN;
3937 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003938 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09003939 sv_colour = SRV_STATS_COLOUR_DOWN;
3940 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003941 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09003942 sv_colour = SRV_STATS_COLOUR_GOING_UP;
3943 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09003944 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09003945 sv_colour = SRV_STATS_COLOUR_DOWN;
3946 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003947 }
Willy Tarreau91861262007-10-17 17:06:05 +02003948
Willy Tarreau9638efa2014-05-23 11:19:57 +02003949 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003950 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003951 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003952 continue;
3953 }
3954
Simon Horman837bfa72015-04-23 14:51:27 +09003955 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01003956 if (bi_putchk(rep, &trash) == -1) {
3957 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003958 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003959 }
3960 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003961 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003962
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003963 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003964 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003965
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003966 case STAT_PX_ST_BE:
3967 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003968 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
3969 if (bi_putchk(rep, &trash) == -1) {
3970 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003971 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003972 }
3973 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003974
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003975 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003976 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003977
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003978 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003979 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003980 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003981 if (bi_putchk(rep, &trash) == -1) {
3982 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003983 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003984 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003985 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003986
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003987 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003988 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003989
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003990 case STAT_PX_ST_FIN:
3991 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003992
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003993 default:
3994 /* unknown state, we should put an abort() here ! */
3995 return 1;
3996 }
3997}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003998
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003999/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4000 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004001 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004002static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004003{
4004 /* WARNING! This must fit in the first buffer !!! */
4005 chunk_appendf(&trash,
4006 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4007 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4008 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4009 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4010 "<style type=\"text/css\"><!--\n"
4011 "body {"
4012 " font-family: arial, helvetica, sans-serif;"
4013 " font-size: 12px;"
4014 " font-weight: normal;"
4015 " color: black;"
4016 " background: white;"
4017 "}\n"
4018 "th,td {"
4019 " font-size: 10px;"
4020 "}\n"
4021 "h1 {"
4022 " font-size: x-large;"
4023 " margin-bottom: 0.5em;"
4024 "}\n"
4025 "h2 {"
4026 " font-family: helvetica, arial;"
4027 " font-size: x-large;"
4028 " font-weight: bold;"
4029 " font-style: italic;"
4030 " color: #6020a0;"
4031 " margin-top: 0em;"
4032 " margin-bottom: 0em;"
4033 "}\n"
4034 "h3 {"
4035 " font-family: helvetica, arial;"
4036 " font-size: 16px;"
4037 " font-weight: bold;"
4038 " color: #b00040;"
4039 " background: #e8e8d0;"
4040 " margin-top: 0em;"
4041 " margin-bottom: 0em;"
4042 "}\n"
4043 "li {"
4044 " margin-top: 0.25em;"
4045 " margin-right: 2em;"
4046 "}\n"
4047 ".hr {margin-top: 0.25em;"
4048 " border-color: black;"
4049 " border-bottom-style: solid;"
4050 "}\n"
4051 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4052 ".total {background: #20D0D0;color: #ffff80;}\n"
4053 ".frontend {background: #e8e8d0;}\n"
4054 ".socket {background: #d0d0d0;}\n"
4055 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004056 ".active_down {background: #ff9090;}\n"
4057 ".active_going_up {background: #ffd020;}\n"
4058 ".active_going_down {background: #ffffa0;}\n"
4059 ".active_up {background: #c0ffc0;}\n"
4060 ".active_nolb {background: #20a0ff;}\n"
4061 ".active_draining {background: #20a0FF;}\n"
4062 ".active_no_check {background: #e0e0e0;}\n"
4063 ".backup_down {background: #ff9090;}\n"
4064 ".backup_going_up {background: #ff80ff;}\n"
4065 ".backup_going_down {background: #c060ff;}\n"
4066 ".backup_up {background: #b0d0ff;}\n"
4067 ".backup_nolb {background: #90b0e0;}\n"
4068 ".backup_draining {background: #cc9900;}\n"
4069 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004070 ".maintain {background: #c07820;}\n"
4071 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4072 "\n"
4073 "a.px:link {color: #ffff40; text-decoration: none;}"
4074 "a.px:visited {color: #ffff40; text-decoration: none;}"
4075 "a.px:hover {color: #ffffff; text-decoration: none;}"
4076 "a.lfsb:link {color: #000000; text-decoration: none;}"
4077 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4078 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4079 "\n"
4080 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4081 "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"
4082 "table.tbl td.ac { text-align: center;}\n"
4083 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4084 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4085 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4086 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4087 "\n"
4088 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4089 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4090 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004091 "table.det { border-collapse: collapse; border-style: none; }\n"
4092 "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 +01004093 "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 +01004094 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004095 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004096 " display:block;\n"
4097 " visibility:hidden;\n"
4098 " z-index:2147483647;\n"
4099 " position:absolute;\n"
4100 " padding:2px 4px 3px;\n"
4101 " background:#f0f060; color:#000000;\n"
4102 " border:1px solid #7040c0;\n"
4103 " white-space:nowrap;\n"
4104 " font-style:normal;font-size:11px;font-weight:normal;\n"
4105 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4106 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4107 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004108 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004109 "-->\n"
4110 "</style></head>\n",
4111 (uri->flags & ST_SHNODE) ? " on " : "",
4112 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4113 );
4114}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004115
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004116/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004117 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004118 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004119 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004120static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004121{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004122 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004123 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004124 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004125
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004126 /* WARNING! this has to fit the first packet too.
4127 * We are around 3.5 kB, add adding entries will
4128 * become tricky if we want to support 4kB buffers !
4129 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004130 chunk_appendf(&trash,
4131 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4132 PRODUCT_NAME "%s</a></h1>\n"
4133 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4134 "<hr width=\"100%%\" class=\"hr\">\n"
4135 "<h3>&gt; General process information</h3>\n"
4136 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4137 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4138 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4139 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4140 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4141 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4142 "Running tasks: %d/%d; idle = %d %%<br>\n"
4143 "</td><td align=\"center\" nowrap>\n"
4144 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004145 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4146 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004147 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004148 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4149 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004150 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004151 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4152 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004153 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004154 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4155 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004156 "</tr><tr>\n"
4157 "<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 -07004158 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004159 "<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 +01004160 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004161 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004162 "</td>"
4163 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4164 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4165 "",
4166 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4167 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4168 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4169 (uri->flags & ST_SHDESC) ? ": " : "",
4170 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4171 pid, relative_pid, global.nbproc,
4172 up / 86400, (up % 86400) / 3600,
4173 (up % 3600) / 60, (up % 60),
4174 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4175 global.rlimit_memmax ? " MB" : "",
4176 global.rlimit_nofile,
4177 global.maxsock, global.maxconn, global.maxpipes,
4178 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4179 run_queue_cur, nb_tasks_cur, idle_pct
4180 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004181
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004182 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004183 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 +01004184 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004185
4186 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004187 "<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 +01004188 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004189 STAT_SCOPE_TXT_MAXLEN);
4190
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004191 /* 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 +02004192 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004193 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004194 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004195 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 +01004196 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004197 }
4198
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004199 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004200 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004201 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004202 uri->uri_prefix,
4203 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004204 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004205 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004206 else
4207 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004208 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004209 uri->uri_prefix,
4210 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004211 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004212 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004213
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004214 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004215 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004216 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004217 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004218 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004219 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004220 "",
4221 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004222 else
4223 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004224 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004225 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004226 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004227 ";norefresh",
4228 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004229 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004230
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004231 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004232 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004233 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004234 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4235 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004236 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004237
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004238 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004239 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004240 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004241 (uri->refresh > 0) ? ";norefresh" : "",
4242 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004243
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004244 chunk_appendf(&trash,
4245 "</ul></td>"
4246 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4247 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4248 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4249 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4250 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4251 "</ul>"
4252 "</td>"
4253 "</tr></table>\n"
4254 ""
4255 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004256
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004257 if (appctx->ctx.stats.st_code) {
4258 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004259 case STAT_STATUS_DONE:
4260 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004261 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004262 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004263 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004264 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004265 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4266 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004267 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004268 break;
4269 case STAT_STATUS_NONE:
4270 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004271 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004272 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004273 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004274 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004275 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4276 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004277 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004278 break;
4279 case STAT_STATUS_PART:
4280 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004281 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004282 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004283 "Action partially processed.<br>"
4284 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004285 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004286 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4287 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004288 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004289 break;
4290 case STAT_STATUS_ERRP:
4291 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004292 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004293 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004294 "Action not processed because of invalid parameters."
4295 "<ul>"
4296 "<li>The action is maybe unknown.</li>"
4297 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4298 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4299 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004300 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004301 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4302 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004303 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004304 break;
4305 case STAT_STATUS_EXCD:
4306 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004307 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004308 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004309 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4310 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004311 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004312 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4313 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004314 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004315 break;
4316 case STAT_STATUS_DENY:
4317 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004318 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004319 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004320 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004321 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004322 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4323 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004324 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004325 break;
4326 default:
4327 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004328 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004329 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004330 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004331 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004332 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4333 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004334 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004335 }
4336 chunk_appendf(&trash, "<p>\n");
4337 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004338}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004339
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004340/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4341 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004342 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004343static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004344{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004345 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004346}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004347
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004348/* This function dumps statistics onto the stream interface's read buffer in
4349 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004350 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4351 * 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 +02004352 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004353 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004354 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004355static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004356{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004357 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004358 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004359 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004360
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004361 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004362
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004363 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004364 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004365 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004366 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004367
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004368 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004369 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004370 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004371 else
4372 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004373
Willy Tarreaubc18da12015-03-13 14:00:47 +01004374 if (bi_putchk(rep, &trash) == -1) {
4375 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004376 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004377 }
Willy Tarreauae526782010-03-04 20:34:23 +01004378
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004379 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004380 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004381
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004382 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004383 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004384 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004385 if (bi_putchk(rep, &trash) == -1) {
4386 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau91861262007-10-17 17:06:05 +02004387 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004388 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004389 }
Willy Tarreau91861262007-10-17 17:06:05 +02004390
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004391 appctx->ctx.stats.px = proxy;
4392 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4393 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004394 /* fall through */
4395
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004396 case STAT_ST_LIST:
4397 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004398 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004399 if (buffer_almost_full(rep->buf)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004400 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004401 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004402 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004403
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004404 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004405 /* skip the disabled proxies, global frontend and non-networked ones */
4406 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004407 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004408 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004409
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004410 appctx->ctx.stats.px = px->next;
4411 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004412 }
4413 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004414
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004415 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004416 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004417
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004418 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004419 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004420 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01004421 if (bi_putchk(rep, &trash) == -1) {
4422 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004423 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004424 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004425 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004426
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004427 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004428 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004429
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004430 case STAT_ST_FIN:
4431 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004432
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004433 default:
4434 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004435 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004436 return -1;
4437 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004438}
Willy Tarreauae526782010-03-04 20:34:23 +01004439
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004440/* We reached the stats page through a POST request. The appctx is
4441 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004442 * Parse the posted data and enable/disable servers if necessary.
4443 * Returns 1 if request was parsed or zero if it needs more data.
4444 */
4445static int stats_process_http_post(struct stream_interface *si)
4446{
Willy Tarreau87b09662015-04-03 00:22:06 +02004447 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004448 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004449
4450 struct proxy *px = NULL;
4451 struct server *sv = NULL;
4452
4453 char key[LINESIZE];
4454 int action = ST_ADM_ACTION_NONE;
4455 int reprocess = 0;
4456
4457 int total_servers = 0;
4458 int altered_servers = 0;
4459
4460 char *first_param, *cur_param, *next_param, *end_params;
4461 char *st_cur_param = NULL;
4462 char *st_next_param = NULL;
4463
4464 struct chunk *temp;
4465 int reql;
4466
4467 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02004468 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004469 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004470 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004471 goto out;
4472 }
4473
Willy Tarreaueee5b512015-04-03 23:46:31 +02004474 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 +01004475 if (reql <= 0) {
4476 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004477 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004478 return 0;
4479 }
4480
4481 first_param = temp->str;
4482 end_params = temp->str + reql;
4483 cur_param = next_param = end_params;
4484 *end_params = '\0';
4485
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004486 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004487
4488 /*
4489 * Parse the parameters in reverse order to only store the last value.
4490 * From the html form, the backend and the action are at the end.
4491 */
4492 while (cur_param > first_param) {
4493 char *value;
4494 int poffset, plen;
4495
4496 cur_param--;
4497
4498 if ((*cur_param == '&') || (cur_param == first_param)) {
4499 reprocess_servers:
4500 /* Parse the key */
4501 poffset = (cur_param != first_param ? 1 : 0);
4502 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4503 if ((plen > 0) && (plen <= sizeof(key))) {
4504 strncpy(key, cur_param + poffset, plen);
4505 key[plen - 1] = '\0';
4506 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004507 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004508 goto out;
4509 }
4510
4511 /* Parse the value */
4512 value = key;
4513 while (*value != '\0' && *value != '=') {
4514 value++;
4515 }
4516 if (*value == '=') {
4517 /* Ok, a value is found, we can mark the end of the key */
4518 *value++ = '\0';
4519 }
4520 if (url_decode(key) < 0 || url_decode(value) < 0)
4521 break;
4522
4523 /* Now we can check the key to see what to do */
4524 if (!px && (strcmp(key, "b") == 0)) {
4525 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4526 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004527 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004528 goto out;
4529 }
4530 }
4531 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004532 if (strcmp(value, "ready") == 0) {
4533 action = ST_ADM_ACTION_READY;
4534 }
4535 else if (strcmp(value, "drain") == 0) {
4536 action = ST_ADM_ACTION_DRAIN;
4537 }
4538 else if (strcmp(value, "maint") == 0) {
4539 action = ST_ADM_ACTION_MAINT;
4540 }
4541 else if (strcmp(value, "shutdown") == 0) {
4542 action = ST_ADM_ACTION_SHUTDOWN;
4543 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004544 else if (strcmp(value, "dhlth") == 0) {
4545 action = ST_ADM_ACTION_DHLTH;
4546 }
4547 else if (strcmp(value, "ehlth") == 0) {
4548 action = ST_ADM_ACTION_EHLTH;
4549 }
4550 else if (strcmp(value, "hrunn") == 0) {
4551 action = ST_ADM_ACTION_HRUNN;
4552 }
4553 else if (strcmp(value, "hnolb") == 0) {
4554 action = ST_ADM_ACTION_HNOLB;
4555 }
4556 else if (strcmp(value, "hdown") == 0) {
4557 action = ST_ADM_ACTION_HDOWN;
4558 }
4559 else if (strcmp(value, "dagent") == 0) {
4560 action = ST_ADM_ACTION_DAGENT;
4561 }
4562 else if (strcmp(value, "eagent") == 0) {
4563 action = ST_ADM_ACTION_EAGENT;
4564 }
4565 else if (strcmp(value, "arunn") == 0) {
4566 action = ST_ADM_ACTION_ARUNN;
4567 }
4568 else if (strcmp(value, "adown") == 0) {
4569 action = ST_ADM_ACTION_ADOWN;
4570 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004571 /* else these are the old supported methods */
4572 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004573 action = ST_ADM_ACTION_DISABLE;
4574 }
4575 else if (strcmp(value, "enable") == 0) {
4576 action = ST_ADM_ACTION_ENABLE;
4577 }
4578 else if (strcmp(value, "stop") == 0) {
4579 action = ST_ADM_ACTION_STOP;
4580 }
4581 else if (strcmp(value, "start") == 0) {
4582 action = ST_ADM_ACTION_START;
4583 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004584 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004585 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004586 goto out;
4587 }
4588 }
4589 else if (strcmp(key, "s") == 0) {
4590 if (!(px && action)) {
4591 /*
4592 * Indicates that we'll need to reprocess the parameters
4593 * as soon as backend and action are known
4594 */
4595 if (!reprocess) {
4596 st_cur_param = cur_param;
4597 st_next_param = next_param;
4598 }
4599 reprocess = 1;
4600 }
4601 else if ((sv = findserver(px, value)) != NULL) {
4602 switch (action) {
4603 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004604 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004605 altered_servers++;
4606 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004607 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004608 }
4609 break;
4610 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004611 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004612 altered_servers++;
4613 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004614 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004615 }
4616 break;
4617 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004618 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4619 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4620 altered_servers++;
4621 total_servers++;
4622 }
4623 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004624 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004625 if (sv->admin & SRV_ADMF_FDRAIN) {
4626 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4627 altered_servers++;
4628 total_servers++;
4629 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004630 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004631 case ST_ADM_ACTION_DHLTH:
4632 if (sv->check.state & CHK_ST_CONFIGURED) {
4633 sv->check.state &= ~CHK_ST_ENABLED;
4634 altered_servers++;
4635 total_servers++;
4636 }
4637 break;
4638 case ST_ADM_ACTION_EHLTH:
4639 if (sv->check.state & CHK_ST_CONFIGURED) {
4640 sv->check.state |= CHK_ST_ENABLED;
4641 altered_servers++;
4642 total_servers++;
4643 }
4644 break;
4645 case ST_ADM_ACTION_HRUNN:
4646 if (!(sv->track)) {
4647 sv->check.health = sv->check.rise + sv->check.fall - 1;
4648 srv_set_running(sv, "changed from Web interface");
4649 altered_servers++;
4650 total_servers++;
4651 }
4652 break;
4653 case ST_ADM_ACTION_HNOLB:
4654 if (!(sv->track)) {
4655 sv->check.health = sv->check.rise + sv->check.fall - 1;
4656 srv_set_stopping(sv, "changed from Web interface");
4657 altered_servers++;
4658 total_servers++;
4659 }
4660 break;
4661 case ST_ADM_ACTION_HDOWN:
4662 if (!(sv->track)) {
4663 sv->check.health = 0;
4664 srv_set_stopped(sv, "changed from Web interface");
4665 altered_servers++;
4666 total_servers++;
4667 }
4668 break;
4669 case ST_ADM_ACTION_DAGENT:
4670 if (sv->agent.state & CHK_ST_CONFIGURED) {
4671 sv->agent.state &= ~CHK_ST_ENABLED;
4672 altered_servers++;
4673 total_servers++;
4674 }
4675 break;
4676 case ST_ADM_ACTION_EAGENT:
4677 if (sv->agent.state & CHK_ST_CONFIGURED) {
4678 sv->agent.state |= CHK_ST_ENABLED;
4679 altered_servers++;
4680 total_servers++;
4681 }
4682 break;
4683 case ST_ADM_ACTION_ARUNN:
4684 if (sv->agent.state & CHK_ST_ENABLED) {
4685 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4686 srv_set_running(sv, "changed from Web interface");
4687 altered_servers++;
4688 total_servers++;
4689 }
4690 break;
4691 case ST_ADM_ACTION_ADOWN:
4692 if (sv->agent.state & CHK_ST_ENABLED) {
4693 sv->agent.health = 0;
4694 srv_set_stopped(sv, "changed from Web interface");
4695 altered_servers++;
4696 total_servers++;
4697 }
4698 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004699 case ST_ADM_ACTION_READY:
4700 srv_adm_set_ready(sv);
4701 altered_servers++;
4702 total_servers++;
4703 break;
4704 case ST_ADM_ACTION_DRAIN:
4705 srv_adm_set_drain(sv);
4706 altered_servers++;
4707 total_servers++;
4708 break;
4709 case ST_ADM_ACTION_MAINT:
4710 srv_adm_set_maint(sv);
4711 altered_servers++;
4712 total_servers++;
4713 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004714 case ST_ADM_ACTION_SHUTDOWN:
4715 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004716 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004717
4718 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4719 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004720 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004721
4722 altered_servers++;
4723 total_servers++;
4724 }
4725 break;
4726 }
4727 } else {
4728 /* the server name is unknown or ambiguous (duplicate names) */
4729 total_servers++;
4730 }
4731 }
4732 if (reprocess && px && action) {
4733 /* Now, we know the backend and the action chosen by the user.
4734 * We can safely restart from the first server parameter
4735 * to reprocess them
4736 */
4737 cur_param = st_cur_param;
4738 next_param = st_next_param;
4739 reprocess = 0;
4740 goto reprocess_servers;
4741 }
4742
4743 next_param = cur_param;
4744 }
4745 }
4746
4747 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004748 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004749 }
4750 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004751 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004752 }
4753 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004754 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004755 }
4756 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004757 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004758 }
4759 out:
4760 return 1;
4761}
4762
4763
4764static int stats_send_http_headers(struct stream_interface *si)
4765{
Willy Tarreau87b09662015-04-03 00:22:06 +02004766 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004767 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004768 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004769
4770 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004771 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004772 "Cache-Control: no-cache\r\n"
4773 "Connection: close\r\n"
4774 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004775 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004776
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004777 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004778 chunk_appendf(&trash, "Refresh: %d\r\n",
4779 uri->refresh);
4780
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004781 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4782
4783 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4784 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4785 else
4786 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004787
Willy Tarreaueee5b512015-04-03 23:46:31 +02004788 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004789 s->logs.tv_request = now;
4790
Willy Tarreaubc18da12015-03-13 14:00:47 +01004791 if (bi_putchk(si_ic(si), &trash) == -1) {
4792 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004793 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004794 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004795
4796 return 1;
4797}
4798
4799static int stats_send_http_redirect(struct stream_interface *si)
4800{
4801 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02004802 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004803 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004804 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004805
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004806 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004807 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004808 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004809 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004810 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 +01004811 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004812 }
4813
4814 /* We don't want to land on the posted stats page because a refresh will
4815 * repost the data. We don't want this to happen on accident so we redirect
4816 * the browse to the stats page with a GET.
4817 */
4818 chunk_printf(&trash,
4819 "HTTP/1.1 303 See Other\r\n"
4820 "Cache-Control: no-cache\r\n"
4821 "Content-Type: text/plain\r\n"
4822 "Connection: close\r\n"
4823 "Location: %s;st=%s%s%s%s\r\n"
4824 "\r\n",
4825 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004826 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4827 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4828 stat_status_codes[appctx->ctx.stats.st_code]) ?
4829 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004830 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004831 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4832 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004833 scope_txt);
4834
Willy Tarreaueee5b512015-04-03 23:46:31 +02004835 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004836 s->logs.tv_request = now;
4837
Willy Tarreaubc18da12015-03-13 14:00:47 +01004838 if (bi_putchk(si_ic(si), &trash) == -1) {
4839 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004840 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004841 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004842
4843 return 1;
4844}
4845
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004846/* This I/O handler runs as an applet embedded in a stream interface. It is
4847 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004848 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004849 * automatically unregisters itself once transfer is complete.
4850 */
4851static void http_stats_io_handler(struct stream_interface *si)
4852{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004853 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004854 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004855 struct channel *req = si_oc(si);
4856 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01004857
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004858 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4859 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004860
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004861 /* check that the output is not closed */
4862 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004863 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004864
Willy Tarreau347a35d2013-11-22 17:51:09 +01004865 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004866 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004867 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02004868 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004869 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004870 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004871 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004872 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004873 }
4874
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004875 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004876 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004877 unsigned int data_len;
4878 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004879 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004880
4881 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4882 /* One difficulty we're facing is that we must prevent
4883 * the input data from being automatically forwarded to
4884 * the output area. For this, we temporarily disable
4885 * forwarding on the channel.
4886 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004887 last_fwd = si_ic(si)->to_forward;
4888 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004889 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004890 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004891 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004892 si_ic(si)->to_forward = last_fwd;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004893 goto fail;
4894 }
4895 }
4896
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004897 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004898 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004899 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004900
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004901 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004902
4903 /* Now we must either adjust or remove the chunk size. This is
4904 * not easy because the chunk size might wrap at the end of the
4905 * buffer, so we pretend we have nothing in the buffer, we write
4906 * the size, then restore the buffer's contents. Note that we can
4907 * only do that because no forwarding is scheduled on the stats
4908 * applet.
4909 */
4910 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004911 si_ic(si)->total -= (last_len - prev_len);
4912 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004913
4914 if (last_len != data_len) {
4915 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01004916 if (bi_putchk(si_ic(si), &trash) == -1)
4917 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004918
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004919 si_ic(si)->total += (last_len - data_len);
4920 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004921 }
4922 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004923 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004924 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004925 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004926
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004927 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004928 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004929 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004930 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004931 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004932 }
4933
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004934 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004935 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004936 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004937 }
4938
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004939 if (appctx->st0 == STAT_HTTP_DONE) {
4940 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4941 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01004942 if (bi_putchk(si_ic(si), &trash) == -1) {
4943 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004944 goto fail;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004945 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004946 }
4947 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004948 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004949 res->flags |= CF_READ_NULL;
4950 si_shutr(si);
4951 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004952
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004953 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4954 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004955
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004956 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004957 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4958 si_shutr(si);
4959 res->flags |= CF_READ_NULL;
4960 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004961 }
Willy Tarreau91861262007-10-17 17:06:05 +02004962
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004963 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004964 /* update all other flags and resync with the other side */
4965 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004966
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004967 /* we don't want to expire timeouts while we're processing requests */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004968 si_ic(si)->rex = TICK_ETERNITY;
4969 si_oc(si)->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004970
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004971 out:
4972 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4973 /* check that we have released everything then unregister */
4974 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004975 }
4976}
4977
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004978
Willy Tarreau909d5172012-11-26 03:04:41 +01004979static inline const char *get_conn_ctrl_name(const struct connection *conn)
4980{
Willy Tarreau3c728722014-01-23 13:50:42 +01004981 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004982 return "NONE";
4983 return conn->ctrl->name;
4984}
4985
4986static inline const char *get_conn_xprt_name(const struct connection *conn)
4987{
4988 static char ptr[17];
4989
Willy Tarreauaad69382014-01-23 14:21:42 +01004990 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004991 return "NONE";
4992
4993 if (conn->xprt == &raw_sock)
4994 return "RAW";
4995
4996#ifdef USE_OPENSSL
4997 if (conn->xprt == &ssl_sock)
4998 return "SSL";
4999#endif
5000 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5001 return ptr;
5002}
5003
5004static inline const char *get_conn_data_name(const struct connection *conn)
5005{
5006 static char ptr[17];
5007
5008 if (!conn->data)
5009 return "NONE";
5010
5011 if (conn->data == &sess_conn_cb)
5012 return "SESS";
5013
5014 if (conn->data == &si_conn_cb)
5015 return "STRM";
5016
5017 if (conn->data == &check_conn_cb)
5018 return "CHCK";
5019
5020 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5021 return ptr;
5022}
5023
Willy Tarreau87b09662015-04-03 00:22:06 +02005024/* This function dumps a complete stream state onto the stream interface's
5025 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005026 * 0 if the output buffer is full and it needs to be called again, otherwise
5027 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005028 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005029static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005030{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005031 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005032 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005033 extern const char *monthname[12];
5034 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005035 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005036 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005037
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005038 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005039
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005040 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005041 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005042 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005043 if (bi_putchk(si_ic(si), &trash) == -1) {
5044 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005045 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005046 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005047 appctx->ctx.sess.uid = 0;
5048 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005049 return 1;
5050 }
5051
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005052 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005053 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005054 appctx->ctx.sess.uid = sess->uniq_id;
5055 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005056 /* fall through */
5057
5058 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005059 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005060 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005061 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005062 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005063 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5064 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005065 sess->uniq_id,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005066 strm_li(sess) && strm_li(sess)->proto->name ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005067
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005068 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005069 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005070 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005071 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005072 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005073 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005074 break;
5075 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005076 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005077 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005078 default:
5079 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005080 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005081 break;
5082 }
5083
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005084 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005085 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005086 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005087
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005088 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005089 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005090 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5091 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5092 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005093
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005094 if (conn)
5095 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005096
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005097 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005098 case AF_INET:
5099 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005100 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005101 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005102 break;
5103 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005104 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005105 break;
5106 default:
5107 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005108 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005109 break;
5110 }
5111
Willy Tarreau50943332011-09-02 17:33:05 +02005112 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005113 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005114 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005115 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005116 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5117 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005118 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005119
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005120 conn = objt_conn(sess->si[1].end);
5121 if (conn)
5122 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005123
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005124 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005125 case AF_INET:
5126 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005127 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005128 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005129 break;
5130 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005131 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005132 break;
5133 default:
5134 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005135 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005136 break;
5137 }
5138
5139 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005140 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005141 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005142 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5143 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005144 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005145 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005146
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005147 if (conn)
5148 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005149
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005150 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005151 case AF_INET:
5152 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005153 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005154 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005155 break;
5156 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005157 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005158 break;
5159 default:
5160 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005161 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005162 break;
5163 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005164
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005165 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005166 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005167 sess->task,
5168 sess->task->state,
5169 sess->task->nice, sess->task->calls,
5170 sess->task->expire ?
5171 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5172 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5173 TICKS_TO_MS(1000)) : "<NEVER>",
5174 task_in_rq(sess->task) ? ", running" : "");
5175
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005176 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005177 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005178 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5179
Willy Tarreaueee5b512015-04-03 23:46:31 +02005180 if (sess->txn)
5181 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005182 " 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 +02005183 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5184 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 +01005185
5186 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005187 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005188 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005189 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005190 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005191 obj_type_name(sess->si[0].end),
5192 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005193 sess->si[0].exp ?
5194 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5195 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5196 TICKS_TO_MS(1000)) : "<NEVER>",
5197 sess->si[0].err_type);
5198
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005199 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005200 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005201 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005202 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005203 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005204 obj_type_name(sess->si[1].end),
5205 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005206 sess->si[1].exp ?
5207 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5208 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5209 TICKS_TO_MS(1000)) : "<NEVER>",
5210 sess->si[1].err_type);
5211
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005212 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005213 chunk_appendf(&trash,
5214 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005215 conn,
5216 get_conn_ctrl_name(conn),
5217 get_conn_xprt_name(conn),
5218 get_conn_data_name(conn),
5219 obj_type_name(conn->target),
5220 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005221
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005222 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005223 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005224 conn->flags,
5225 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005226 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005227 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005228 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005229 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005230 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5231 chunk_appendf(&trash,
5232 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5233 tmpctx,
5234 tmpctx->st0,
5235 tmpctx->st1,
5236 tmpctx->st2,
5237 tmpctx->applet->name);
5238 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005239
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005240 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005241 chunk_appendf(&trash,
5242 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005243 conn,
5244 get_conn_ctrl_name(conn),
5245 get_conn_xprt_name(conn),
5246 get_conn_data_name(conn),
5247 obj_type_name(conn->target),
5248 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005249
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005250 chunk_appendf(&trash,
5251 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005252 conn->flags,
5253 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005254 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005255 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005256 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005257 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005258 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5259 chunk_appendf(&trash,
5260 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5261 tmpctx,
5262 tmpctx->st0,
5263 tmpctx->st1,
5264 tmpctx->st2,
5265 tmpctx->applet->name);
5266 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005267
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005268 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005269 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005270 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005271 &sess->req,
5272 sess->req.flags, sess->req.analysers,
5273 sess->req.pipe ? sess->req.pipe->data : 0,
5274 sess->req.to_forward, sess->req.total,
5275 sess->req.analyse_exp ?
5276 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005277 TICKS_TO_MS(1000)) : "<NEVER>");
5278
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005279 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005280 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005281 sess->req.rex ?
5282 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005283 TICKS_TO_MS(1000)) : "<NEVER>");
5284
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005285 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005286 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005287 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005288 sess->req.wex ?
5289 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005290 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005291 sess->req.buf,
5292 sess->req.buf->data, sess->req.buf->o,
5293 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005294 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005295 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005296
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005297 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005298 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005299 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005300 &sess->res,
5301 sess->res.flags, sess->res.analysers,
5302 sess->res.pipe ? sess->res.pipe->data : 0,
5303 sess->res.to_forward, sess->res.total,
5304 sess->res.analyse_exp ?
5305 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005306 TICKS_TO_MS(1000)) : "<NEVER>");
5307
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005308 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005309 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005310 sess->res.rex ?
5311 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005312 TICKS_TO_MS(1000)) : "<NEVER>");
5313
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005314 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005315 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005316 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005317 sess->res.wex ?
5318 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005319 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005320 sess->res.buf,
5321 sess->res.buf->data, sess->res.buf->o,
5322 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005323 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005324 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005325
Willy Tarreaubc18da12015-03-13 14:00:47 +01005326 if (bi_putchk(si_ic(si), &trash) == -1) {
5327 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005328 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005329 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005330
5331 /* use other states to dump the contents */
5332 }
5333 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005334 appctx->ctx.sess.uid = 0;
5335 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005336 return 1;
5337}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005338
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005339static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005340{
5341 struct appctx *appctx = __objt_appctx(si->end);
5342
5343 switch (appctx->st2) {
5344 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005345 /* Display the column headers. If the message cannot be sent,
5346 * quit the fucntion with returning 0. The function is called
5347 * later and restart at the state "STAT_ST_INIT".
5348 */
5349 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005350 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005351 if (bi_putchk(si_ic(si), &trash) == -1) {
5352 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005353 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005354 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005355
5356 /* Now, we start the browsing of the references lists.
5357 * Note that the following call to LIST_ELEM return bad pointer. The only
5358 * avalaible field of this pointer is <list>. It is used with the function
5359 * pat_list_get_next() for retruning the first avalaible entry
5360 */
5361 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5362 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5363 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005364 appctx->st2 = STAT_ST_LIST;
5365 /* fall through */
5366
5367 case STAT_ST_LIST:
5368 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005369 chunk_reset(&trash);
5370
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005371 /* Build messages. If the reference is used by another category than
5372 * the listed categorie, display the information in the massage.
5373 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005374 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005375 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5376 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005377
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005378 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005379 /* let's try again later from this stream. We add ourselves into
5380 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005381 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005382 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005383 return 0;
5384 }
5385
5386 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005387 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5388 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005389 }
5390
5391 appctx->st2 = STAT_ST_FIN;
5392 /* fall through */
5393
5394 default:
5395 appctx->st2 = STAT_ST_FIN;
5396 return 1;
5397 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005398 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005399}
5400
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005401static int stats_map_lookup(struct stream_interface *si)
5402{
5403 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005404 struct sample sample;
5405 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005406 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005407
5408 switch (appctx->st2) {
5409 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005410 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005411 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005412 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005413 appctx->st2 = STAT_ST_LIST;
5414 /* fall through */
5415
5416 case STAT_ST_LIST:
5417 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005418 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005419 /* initialise chunk to build new message */
5420 chunk_reset(&trash);
5421
5422 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005423 sample.type = SMP_T_STR;
5424 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005425 sample.data.str.len = appctx->ctx.map.chunk.len;
5426 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005427 if (appctx->ctx.map.expr->pat_head->match &&
5428 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005429 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5430 else
5431 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005432
5433 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005434 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5435 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5436 break;
5437 if (match_method >= PAT_MATCH_NUM)
5438 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5439 else
5440 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005441
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005442 /* case sensitive */
5443 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5444 chunk_appendf(&trash, ", case=insensitive");
5445 else
5446 chunk_appendf(&trash, ", case=sensitive");
5447
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005448 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005449 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005450 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5451 chunk_appendf(&trash, ", found=no");
5452 else
5453 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005454 }
5455
5456 /* Display match and match info */
5457 else {
5458 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005459 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5460 chunk_appendf(&trash, ", found=yes");
5461 else
5462 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005463
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005464 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005465 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005466 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005467 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005468 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005469
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005470 /* display pattern */
5471 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5472 if (pat->ref && pat->ref->pattern)
5473 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5474 else
5475 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005476 }
5477 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005478 if (pat->ref && pat->ref->pattern)
5479 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5480 else
5481 chunk_appendf(&trash, ", pattern=unknown");
5482 }
5483
5484 /* display return value */
5485 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5486 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005487 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005488 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005489 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005490 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005491 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005492 }
5493
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005494 chunk_appendf(&trash, "\n");
5495
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005496 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005497 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005498 /* let's try again later from this stream. We add ourselves into
5499 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005500 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005501 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005502 return 0;
5503 }
5504
5505 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005506 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5507 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005508 }
5509
5510 appctx->st2 = STAT_ST_FIN;
5511 /* fall through */
5512
5513 default:
5514 appctx->st2 = STAT_ST_FIN;
5515 free(appctx->ctx.map.chunk.str);
5516 return 1;
5517 }
5518}
5519
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005520static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005521{
5522 struct appctx *appctx = __objt_appctx(si->end);
5523
5524 switch (appctx->st2) {
5525
5526 case STAT_ST_INIT:
5527 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005528 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5529 struct pat_ref_elt *, list);
5530 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5531 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005532 appctx->st2 = STAT_ST_LIST;
5533 /* fall through */
5534
5535 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005536 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005537 chunk_reset(&trash);
5538
5539 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005540 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005541 chunk_appendf(&trash, "%p %s %s\n",
5542 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5543 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005544 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005545 chunk_appendf(&trash, "%p %s\n",
5546 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005547
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005548 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005549 /* let's try again later from this stream. We add ourselves into
5550 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005551 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005552 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005553 return 0;
5554 }
5555
5556 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005557 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5558 struct pat_ref_elt *, list);
5559 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005560 break;
5561 }
5562
5563 appctx->st2 = STAT_ST_FIN;
5564 /* fall through */
5565
5566 default:
5567 appctx->st2 = STAT_ST_FIN;
5568 return 1;
5569 }
5570}
5571
Willy Tarreau87b09662015-04-03 00:22:06 +02005572/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005573 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005574 * to be called again, otherwise non-zero. It is designed to be called
5575 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005576 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005577static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005578{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005579 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005580 struct connection *conn;
5581
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005582 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005583 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02005584 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005585 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005586 if (appctx->st2 == STAT_ST_LIST) {
5587 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5588 LIST_DEL(&appctx->ctx.sess.bref.users);
5589 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005590 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005591 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005592 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005593 }
5594
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005595 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005596
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005597 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005598 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005599 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02005600 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005601 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02005602 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005603 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02005604 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005605 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005606 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02005607 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005608 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005609 /* fall through */
5610
Willy Tarreau295a8372011-03-10 11:25:07 +01005611 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02005612 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005613 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5614 LIST_DEL(&appctx->ctx.sess.bref.users);
5615 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005616 }
5617
5618 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02005619 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005620 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005621 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005622
Willy Tarreau87b09662015-04-03 00:22:06 +02005623 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005624
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005625 if (appctx->ctx.sess.target) {
5626 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005627 goto next_sess;
5628
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005629 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005630 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005631 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005632 return 0;
5633
Willy Tarreau87b09662015-04-03 00:22:06 +02005634 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005635 LIST_DEL(&appctx->ctx.sess.bref.users);
5636 LIST_INIT(&appctx->ctx.sess.bref.users);
5637 if (appctx->ctx.sess.target != (void *)-1) {
5638 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005639 break;
5640 }
5641 else
5642 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005643 }
5644
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005645 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005646 "%p: proto=%s",
5647 curr_sess,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005648 strm_li(curr_sess)->proto->name);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005649
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005650
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005651 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005652 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005653 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005654 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005655 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005656 " src=%s:%d fe=%s be=%s srv=%s",
5657 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005658 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005659 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005660 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005661 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005662 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005663 break;
5664 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005665 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005666 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005667 strm_li(curr_sess)->luid,
5668 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005669 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005670 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005671 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005672 break;
5673 }
5674
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005675 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005676 " ts=%02x age=%s calls=%d",
5677 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005678 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5679 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005680
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005681 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005682 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005683 curr_sess->req.flags,
5684 curr_sess->req.buf->i,
5685 curr_sess->req.analysers,
5686 curr_sess->req.rex ?
5687 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005688 TICKS_TO_MS(1000)) : "");
5689
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005690 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005691 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005692 curr_sess->req.wex ?
5693 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005694 TICKS_TO_MS(1000)) : "");
5695
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005696 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005697 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005698 curr_sess->req.analyse_exp ?
5699 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005700 TICKS_TO_MS(1000)) : "");
5701
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005702 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005703 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005704 curr_sess->res.flags,
5705 curr_sess->res.buf->i,
5706 curr_sess->res.analysers,
5707 curr_sess->res.rex ?
5708 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005709 TICKS_TO_MS(1000)) : "");
5710
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005711 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005712 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005713 curr_sess->res.wex ?
5714 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005715 TICKS_TO_MS(1000)) : "");
5716
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005717 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005718 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005719 curr_sess->res.analyse_exp ?
5720 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005721 TICKS_TO_MS(1000)) : "");
5722
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005723 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005724 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005725 " s0=[%d,%1xh,fd=%d,ex=%s]",
5726 curr_sess->si[0].state,
5727 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005728 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005729 curr_sess->si[0].exp ?
5730 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5731 TICKS_TO_MS(1000)) : "");
5732
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005733 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005734 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005735 " s1=[%d,%1xh,fd=%d,ex=%s]",
5736 curr_sess->si[1].state,
5737 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005738 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005739 curr_sess->si[1].exp ?
5740 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5741 TICKS_TO_MS(1000)) : "");
5742
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005743 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005744 " exp=%s",
5745 curr_sess->task->expire ?
5746 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5747 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005748 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005749 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005750
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005751 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005752
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005753 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005754 /* let's try again later from this stream. We add ourselves into
5755 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005756 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005757 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005758 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005759 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005760 }
5761
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005762 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005763 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005764 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005765
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005766 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005767 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005768 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005769 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005770 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005771 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005772
Willy Tarreaubc18da12015-03-13 14:00:47 +01005773 if (bi_putchk(si_ic(si), &trash) == -1) {
5774 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005775 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005776 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005777
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005778 appctx->ctx.sess.target = NULL;
5779 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005780 return 1;
5781 }
5782
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005783 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005784 /* fall through */
5785
5786 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005787 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005788 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005789 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005790}
5791
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005792/* This is called when the stream interface is closed. For instance, upon an
5793 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02005794 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005795 */
Simon Horman74d88312013-02-12 10:45:50 +09005796static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005797{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005798 struct appctx *appctx = __objt_appctx(si->end);
5799
5800 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5801 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5802 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005803 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005804 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5805 free(appctx->ctx.cli.err);
5806 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005807 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5808 free(appctx->ctx.map.chunk.str);
5809 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005810}
5811
Willy Tarreau20e99322013-04-13 09:22:25 +02005812/* This function is used to either dump tables states (when action is set
5813 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005814 * It returns 0 if the output buffer is full and it needs to be called
5815 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005816 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005817static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005818{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005819 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02005820 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005821 struct ebmb_node *eb;
5822 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005823 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005824 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005825
5826 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005827 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005828 * - STAT_ST_INIT : the first call
5829 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005830 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005831 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005832 * and the entry pointer points to the next entry to be dumped,
5833 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005834 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005835 * data though.
5836 */
5837
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005838 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005839 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005840 if (appctx->st2 == STAT_ST_LIST) {
5841 appctx->ctx.table.entry->ref_cnt--;
5842 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005843 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005844 return 1;
5845 }
5846
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005847 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005848
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005849 while (appctx->st2 != STAT_ST_FIN) {
5850 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005851 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005852 appctx->ctx.table.proxy = appctx->ctx.table.target;
5853 if (!appctx->ctx.table.proxy)
5854 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005855
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005856 appctx->ctx.table.entry = NULL;
5857 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005858 break;
5859
Willy Tarreau295a8372011-03-10 11:25:07 +01005860 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005861 if (!appctx->ctx.table.proxy ||
5862 (appctx->ctx.table.target &&
5863 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5864 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005865 break;
5866 }
5867
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005868 if (appctx->ctx.table.proxy->table.size) {
5869 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5870 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005871 return 0;
5872
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005873 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005874 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005875 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005876 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005877 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005878 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5879 appctx->ctx.table.entry->ref_cnt++;
5880 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005881 break;
5882 }
5883 }
5884 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005885 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005886 break;
5887
Willy Tarreau295a8372011-03-10 11:25:07 +01005888 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005889 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005890
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005891 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005892 /* we're filtering on some data contents */
5893 void *ptr;
5894 long long data;
5895
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005896 dt = appctx->ctx.table.data_type;
5897 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5898 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005899 dt);
5900
5901 data = 0;
5902 switch (stktable_data_types[dt].std_type) {
5903 case STD_T_SINT:
5904 data = stktable_data_cast(ptr, std_t_sint);
5905 break;
5906 case STD_T_UINT:
5907 data = stktable_data_cast(ptr, std_t_uint);
5908 break;
5909 case STD_T_ULL:
5910 data = stktable_data_cast(ptr, std_t_ull);
5911 break;
5912 case STD_T_FRQP:
5913 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005914 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005915 break;
5916 }
5917
5918 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005919 if ((data < appctx->ctx.table.value &&
5920 (appctx->ctx.table.data_op == STD_OP_EQ ||
5921 appctx->ctx.table.data_op == STD_OP_GT ||
5922 appctx->ctx.table.data_op == STD_OP_GE)) ||
5923 (data == appctx->ctx.table.value &&
5924 (appctx->ctx.table.data_op == STD_OP_NE ||
5925 appctx->ctx.table.data_op == STD_OP_GT ||
5926 appctx->ctx.table.data_op == STD_OP_LT)) ||
5927 (data > appctx->ctx.table.value &&
5928 (appctx->ctx.table.data_op == STD_OP_EQ ||
5929 appctx->ctx.table.data_op == STD_OP_LT ||
5930 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005931 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005932 }
5933
Simon Hormanc88b8872011-06-15 15:18:49 +09005934 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005935 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5936 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005937 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005938
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005939 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005940
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005941 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005942 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005943 struct stksess *old = appctx->ctx.table.entry;
5944 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005945 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005946 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5947 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5948 stksess_kill(&appctx->ctx.table.proxy->table, old);
5949 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005950 break;
5951 }
5952
Simon Hormanc88b8872011-06-15 15:18:49 +09005953
5954 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005955 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5956 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5957 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005958
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005959 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5960 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005961 break;
5962
Willy Tarreau295a8372011-03-10 11:25:07 +01005963 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005964 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005965 break;
5966 }
5967 }
5968 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005969}
5970
Willy Tarreaud426a182010-03-05 14:58:26 +01005971/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005972 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5973 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5974 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5975 * lines are respected within the limit of 70 output chars. Lines that are
5976 * continuation of a previous truncated line begin with "+" instead of " "
5977 * after the offset. The new pointer is returned.
5978 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005979static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5980 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005981{
5982 int end;
5983 unsigned char c;
5984
5985 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005986 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005987 return ptr;
5988
Willy Tarreau77804732012-10-29 16:14:26 +01005989 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005990
Willy Tarreaud426a182010-03-05 14:58:26 +01005991 while (ptr < len && ptr < bsize) {
5992 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005993 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005994 if (out->len > end - 2)
5995 break;
5996 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005997 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005998 if (out->len > end - 3)
5999 break;
6000 out->str[out->len++] = '\\';
6001 switch (c) {
6002 case '\t': c = 't'; break;
6003 case '\n': c = 'n'; break;
6004 case '\r': c = 'r'; break;
6005 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006006 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006007 }
6008 out->str[out->len++] = c;
6009 } else {
6010 if (out->len > end - 5)
6011 break;
6012 out->str[out->len++] = '\\';
6013 out->str[out->len++] = 'x';
6014 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6015 out->str[out->len++] = hextab[c & 0xF];
6016 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006017 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006018 /* we had a line break, let's return now */
6019 out->str[out->len++] = '\n';
6020 *line = ptr;
6021 return ptr;
6022 }
6023 }
6024 /* we have an incomplete line, we return it as-is */
6025 out->str[out->len++] = '\n';
6026 return ptr;
6027}
6028
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006029/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006030 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006031 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006032 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006033static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006034{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006035 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006036 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006037
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006038 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006039 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006040
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006041 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006042
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006043 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006044 /* the function had not been called yet, let's prepare the
6045 * buffer for a response.
6046 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006047 struct tm tm;
6048
6049 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006050 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006051 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6052 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6053 error_snapshot_id);
6054
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006055 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006056 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaubc18da12015-03-13 14:00:47 +01006057 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau10479e42010-12-12 14:00:34 +01006058 return 0;
6059 }
6060
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006061 appctx->ctx.errors.px = proxy;
6062 appctx->ctx.errors.buf = 0;
6063 appctx->ctx.errors.bol = 0;
6064 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006065 }
6066
6067 /* we have two inner loops here, one for the proxy, the other one for
6068 * the buffer.
6069 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006070 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006071 struct error_snapshot *es;
6072
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006073 if (appctx->ctx.errors.buf == 0)
6074 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006075 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006076 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006077
6078 if (!es->when.tv_sec)
6079 goto next;
6080
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006081 if (appctx->ctx.errors.iid >= 0 &&
6082 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6083 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006084 goto next;
6085
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006086 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006087 /* just print headers now */
6088
6089 char pn[INET6_ADDRSTRLEN];
6090 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006091 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006092
6093 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006094 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006095 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006096 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006097
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006098 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6099 case AF_INET:
6100 case AF_INET6:
6101 port = get_host_port(&es->src);
6102 break;
6103 default:
6104 port = 0;
6105 }
6106
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006107 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006108 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006109 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006110 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006111 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006112 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006113 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6114 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006115 break;
6116 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006117 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006118 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006119 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006120 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006121 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006122 break;
6123 }
6124
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006125 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006126 ", server %s (#%d), event #%u\n"
6127 " src %s:%d, session #%d, session flags 0x%08x\n"
6128 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6129 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6130 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6131 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6132 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6133 es->ev_id,
6134 pn, port, es->sid, es->s_flags,
6135 es->state, es->m_flags, es->t_flags,
6136 es->m_clen, es->m_blen,
6137 es->b_flags, es->b_out, es->b_tot,
6138 es->len, es->b_wrap, es->pos);
6139
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006140 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006141 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaubc18da12015-03-13 14:00:47 +01006142 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau61b34732009-10-03 23:49:35 +02006143 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006144 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006145 appctx->ctx.errors.ptr = 0;
6146 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006147 }
6148
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006149 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006150 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006151 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006152 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006153 if (bi_putchk(si_ic(si), &trash) == -1) {
6154 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau61b34732009-10-03 23:49:35 +02006155 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006156 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006157 goto next;
6158 }
6159
6160 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006161 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006162 int newptr;
6163 int newline;
6164
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006165 newline = appctx->ctx.errors.bol;
6166 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6167 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006168 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006169
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006170 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006171 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaubc18da12015-03-13 14:00:47 +01006172 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau61b34732009-10-03 23:49:35 +02006173 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006174 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006175 appctx->ctx.errors.ptr = newptr;
6176 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006177 };
6178 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006179 appctx->ctx.errors.bol = 0;
6180 appctx->ctx.errors.ptr = -1;
6181 appctx->ctx.errors.buf++;
6182 if (appctx->ctx.errors.buf > 1) {
6183 appctx->ctx.errors.buf = 0;
6184 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006185 }
6186 }
6187
6188 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006189 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006190}
6191
Willy Tarreaud5781202012-09-22 19:32:35 +02006192/* parse the "level" argument on the bind lines */
6193static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6194{
6195 if (!*args[cur_arg + 1]) {
6196 memprintf(err, "'%s' : missing level", args[cur_arg]);
6197 return ERR_ALERT | ERR_FATAL;
6198 }
6199
6200 if (!strcmp(args[cur_arg+1], "user"))
6201 conf->level = ACCESS_LVL_USER;
6202 else if (!strcmp(args[cur_arg+1], "operator"))
6203 conf->level = ACCESS_LVL_OPER;
6204 else if (!strcmp(args[cur_arg+1], "admin"))
6205 conf->level = ACCESS_LVL_ADMIN;
6206 else {
6207 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6208 args[cur_arg], args[cur_arg+1]);
6209 return ERR_ALERT | ERR_FATAL;
6210 }
6211
6212 return 0;
6213}
6214
Willy Tarreaub24281b2011-02-13 13:16:36 +01006215struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006216 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006217 .name = "<STATS>", /* used for logging */
6218 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006219 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006220};
6221
Simon Horman9bd2c732011-06-15 15:18:44 +09006222static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006223 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006224 .name = "<CLI>", /* used for logging */
6225 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006226 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006227};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006228
Willy Tarreaudc13c112013-06-21 23:16:39 +02006229static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006230 { CFG_GLOBAL, "stats", stats_parse_global },
6231 { 0, NULL, NULL },
6232}};
6233
Willy Tarreaud5781202012-09-22 19:32:35 +02006234static struct bind_kw_list bind_kws = { "STAT", { }, {
6235 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6236 { NULL, NULL, 0 },
6237}};
6238
Willy Tarreau10522fd2008-07-09 20:12:41 +02006239__attribute__((constructor))
6240static void __dumpstats_module_init(void)
6241{
6242 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006243 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006244}
6245
Willy Tarreau91861262007-10-17 17:06:05 +02006246/*
6247 * Local variables:
6248 * c-indent-level: 8
6249 * c-basic-offset: 8
6250 * End:
6251 */