blob: 21eff521830330278150d00aabc795ed1ab00e53 [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>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010044#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010049#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020051#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010052#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010053#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020054#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020055#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010056#include <proto/sample.h>
Willy Tarreau91861262007-10-17 17:06:05 +020057#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020058#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020059#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010060#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010061#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020062
Willy Tarreau7a0169a2012-11-19 17:13:16 +010063#ifdef USE_OPENSSL
64#include <proto/ssl_sock.h>
65#endif
66
Willy Tarreau91b843d2014-01-28 16:27:17 +010067/* stats socket states */
68enum {
69 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
70 STAT_CLI_END, /* final state, let's close */
71 STAT_CLI_GETREQ, /* wait for a request */
72 STAT_CLI_OUTPUT, /* all states after this one are responses */
73 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
74 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010075 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010076 STAT_CLI_O_INFO, /* dump info */
77 STAT_CLI_O_SESS, /* dump sessions */
78 STAT_CLI_O_ERR, /* dump errors */
79 STAT_CLI_O_TAB, /* dump tables */
80 STAT_CLI_O_CLR, /* clear tables */
81 STAT_CLI_O_SET, /* set entries in tables */
82 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010083 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
84 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010085 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010086 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010087};
88
Willy Tarreaued7df902014-05-22 18:04:49 +020089/* Actions available for the stats admin forms */
90enum {
91 ST_ADM_ACTION_NONE = 0,
92 ST_ADM_ACTION_READY,
93 ST_ADM_ACTION_DRAIN,
94 ST_ADM_ACTION_MAINT,
95 ST_ADM_ACTION_SHUTDOWN,
96 /* these are the ancient actions, still available for compatibility */
97 ST_ADM_ACTION_DISABLE,
98 ST_ADM_ACTION_ENABLE,
99 ST_ADM_ACTION_STOP,
100 ST_ADM_ACTION_START,
101};
102
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100103static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100104static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +0100105static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900106static int stats_dump_sess_to_buffer(struct stream_interface *si);
107static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100108static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100109static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
110static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100111static int stats_pats_list(struct stream_interface *si);
112static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100113static int stats_map_lookup(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +0900114
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100115/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100116 * cli_io_handler()
117 * -> stats_dump_sess_to_buffer() // "show sess"
118 * -> stats_dump_errors_to_buffer() // "show errors"
119 * -> stats_dump_info_to_buffer() // "show info"
120 * -> stats_dump_stat_to_buffer() // "show stat"
121 * -> stats_dump_csv_header()
122 * -> stats_dump_proxy_to_buffer()
123 * -> stats_dump_fe_stats()
124 * -> stats_dump_li_stats()
125 * -> stats_dump_sv_stats()
126 * -> stats_dump_be_stats()
127 *
128 * http_stats_io_handler()
129 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
130 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
131 * -> stats_dump_html_head() // emits the HTML headers
132 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
133 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
134 * -> stats_dump_html_px_hdr()
135 * -> stats_dump_fe_stats()
136 * -> stats_dump_li_stats()
137 * -> stats_dump_sv_stats()
138 * -> stats_dump_be_stats()
139 * -> stats_dump_html_px_end()
140 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100141 */
142
Simon Horman9bd2c732011-06-15 15:18:44 +0900143static struct si_applet cli_applet;
144
145static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200146 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200147 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200148 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200149 " help : this message\n"
150 " prompt : toggle interactive mode with prompt\n"
151 " quit : disconnect\n"
152 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100153 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200154 " show stat : report counters for each proxy and server\n"
155 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100156 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200157 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200158 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200159 " set weight : change a server's weight\n"
Willy Tarreau2a4b70f2014-05-22 18:42:35 +0200160 " set server : change a server's state or weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200161 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100162 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200163 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200164 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200165 " disable : put a server or frontend in maintenance mode\n"
166 " enable : re-enable a server or frontend which is in maintenance mode\n"
167 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100168 " show acl [id] : report avalaible acls or dump an acl's contents\n"
169 " get acl : reports the patterns matching a sample for an ACL\n"
170 " add acl : add acl entry\n"
171 " del acl : delete acl entry\n"
172 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100173 " show map [id] : report avalaible maps or dump a map's contents\n"
174 " get map : reports the keys and values matching a sample for a map\n"
175 " set map : modify map entry\n"
176 " add map : add map entry\n"
177 " del map : delete map entry\n"
178 " clear map <id> : clear the content of this map\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200179 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200180
Simon Horman9bd2c732011-06-15 15:18:44 +0900181static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200182 "Permission denied\n"
183 "";
184
Willy Tarreau295a8372011-03-10 11:25:07 +0100185/* data transmission states for the stats responses */
186enum {
187 STAT_ST_INIT = 0,
188 STAT_ST_HEAD,
189 STAT_ST_INFO,
190 STAT_ST_LIST,
191 STAT_ST_END,
192 STAT_ST_FIN,
193};
194
195/* data transmission states for the stats responses inside a proxy */
196enum {
197 STAT_PX_ST_INIT = 0,
198 STAT_PX_ST_TH,
199 STAT_PX_ST_FE,
200 STAT_PX_ST_LI,
201 STAT_PX_ST_SV,
202 STAT_PX_ST_BE,
203 STAT_PX_ST_END,
204 STAT_PX_ST_FIN,
205};
206
Cyril Bonté19979e12012-04-04 12:57:21 +0200207extern const char *stat_status_codes[];
208
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200209/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200210 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200211 * needs to be closed and ignored, or a negative value upon critical failure.
212 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900213static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200214{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100215 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100216 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200217
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200218 tv_zero(&s->logs.tv_request);
219 s->logs.t_queue = 0;
220 s->logs.t_connect = 0;
221 s->logs.t_data = 0;
222 s->logs.t_close = 0;
223 s->logs.bytes_in = s->logs.bytes_out = 0;
224 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
225 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200226
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200227 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200228
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200229 if (s->listener->timeout) {
230 s->req->rto = *s->listener->timeout;
231 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200232 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200233 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200234}
235
Willy Tarreau07e9e642010-08-17 21:48:17 +0200236/* allocate a new stats frontend named <name>, and return it
237 * (or NULL in case of lack of memory).
238 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200239static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200240{
241 struct proxy *fe;
242
243 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
244 if (!fe)
245 return NULL;
246
Willy Tarreau237250c2011-07-29 01:49:03 +0200247 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200248 fe->next = proxy;
249 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200250 fe->last_change = now.tv_sec;
251 fe->id = strdup("GLOBAL");
252 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200253 fe->maxconn = 10; /* default to 10 concurrent connections */
254 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200255 fe->conf.file = strdup(file);
256 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200257 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200258
259 /* the stats frontend is the only one able to assign ID #0 */
260 fe->conf.id.key = fe->uuid = 0;
261 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200262 return fe;
263}
264
Willy Tarreaufbee7132007-10-18 13:53:22 +0200265/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200266 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
267 * error message into the <err> buffer which will be preallocated. The trailing
268 * '\n' must not be written. The function must be called with <args> pointing to
269 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200270 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200271static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200272 struct proxy *defpx, const char *file, int line,
273 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200274{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200275 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200276 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200277
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200278 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200279 int cur_arg;
280
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200281 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200282 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 +0200283 return -1;
284 }
285
Willy Tarreau89a63132009-08-16 17:41:45 +0200286 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200287 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200288 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200289 return -1;
290 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200291 }
292
Willy Tarreau4348fad2012-09-20 16:48:07 +0200293 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200294 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200295
Willy Tarreauc53d4222012-09-20 20:19:28 +0200296 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
297 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
298 file, line, args[0], args[1], err && *err ? *err : "error");
299 return -1;
300 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200301
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200302 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200303 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200304 static int bind_dumped;
305 struct bind_kw *kw;
306
307 kw = bind_find_kw(args[cur_arg]);
308 if (kw) {
309 if (!kw->parse) {
310 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
311 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200312 return -1;
313 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200314
315 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
316 if (err && *err)
317 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
318 else
319 memprintf(err, "'%s %s' : error encountered while processing '%s'",
320 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200321 return -1;
322 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200323
324 cur_arg += 1 + kw->skip;
325 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200326 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200327
328 if (!bind_dumped) {
329 bind_dump_kws(err);
330 indent_msg(err, 4);
331 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200332 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200333
334 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
335 args[0], args[1], args[cur_arg],
336 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
337 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200338 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100339
Willy Tarreauc53d4222012-09-20 20:19:28 +0200340 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
341 l->maxconn = global.stats_fe->maxconn;
342 l->backlog = global.stats_fe->backlog;
343 l->timeout = &global.stats_fe->timeout.client;
344 l->accept = session_accept;
345 l->handler = process_session;
346 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
347 l->nice = -64; /* we want to boost priority for local stats */
348 global.maxsock += l->maxconn;
349 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200350 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200351 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100352 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200353 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100354
355 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200356 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100357 return -1;
358 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200359
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100360 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200361 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200362 return -1;
363 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200364 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200365 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200366 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200367 return -1;
368 }
369 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200370 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200371 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200372 else if (!strcmp(args[1], "maxconn")) {
373 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200374
375 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200376 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200377 return -1;
378 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200379
380 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200381 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200382 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200383 return -1;
384 }
385 }
386 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200387 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200388 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
389 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100390 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200391
Willy Tarreau91319572013-04-20 09:48:50 +0200392 if (!global.stats_fe) {
393 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
394 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
395 return -1;
396 }
397 }
398
Willy Tarreau35b7b162012-10-22 23:17:18 +0200399 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100400 unsigned int low, high;
401
Willy Tarreau35b7b162012-10-22 23:17:18 +0200402 if (strcmp(args[cur_arg], "all") == 0) {
403 set = 0;
404 break;
405 }
406 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100407 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200408 }
409 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100410 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200411 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100412 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100413 char *dash = strchr(args[cur_arg], '-');
414
415 low = high = str2uic(args[cur_arg]);
416 if (dash)
417 high = str2uic(dash + 1);
418
419 if (high < low) {
420 unsigned int swap = low;
421 low = high;
422 high = swap;
423 }
424
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100425 if (low < 1 || high > LONGBITS) {
426 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
427 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200428 return -1;
429 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100430 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100431 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100432 }
433 else {
434 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100435 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
436 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100437 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200438 }
439 cur_arg++;
440 }
441 global.stats_fe->bind_proc = set;
442 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200443 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200444 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200445 return -1;
446 }
447 return 0;
448}
449
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100450/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
451 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100452 * NOTE: Some tools happen to rely on the field position instead of its name,
453 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100454 */
455static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100456{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100457 chunk_appendf(&trash,
458 "# pxname,svname,"
459 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100460 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100461 "bin,bout,"
462 "dreq,dresp,"
463 "ereq,econ,eresp,"
464 "wretr,wredis,"
465 "status,weight,act,bck,"
466 "chkfail,chkdown,lastchg,downtime,qlimit,"
467 "pid,iid,sid,throttle,lbtot,tracked,type,"
468 "rate,rate_lim,rate_max,"
469 "check_status,check_code,check_duration,"
470 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
471 "req_rate,req_rate_max,req_tot,"
472 "cli_abrt,srv_abrt,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100473 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100474 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100475}
476
Simon Hormand9366582011-06-15 15:18:45 +0900477/* print a string of text buffer to <out>. The format is :
478 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
479 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
480 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
481 */
482static int dump_text(struct chunk *out, const char *buf, int bsize)
483{
484 unsigned char c;
485 int ptr = 0;
486
487 while (buf[ptr] && ptr < bsize) {
488 c = buf[ptr];
489 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
490 if (out->len > out->size - 1)
491 break;
492 out->str[out->len++] = c;
493 }
494 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
495 if (out->len > out->size - 2)
496 break;
497 out->str[out->len++] = '\\';
498 switch (c) {
499 case ' ': c = ' '; break;
500 case '\t': c = 't'; break;
501 case '\n': c = 'n'; break;
502 case '\r': c = 'r'; break;
503 case '\e': c = 'e'; break;
504 case '\\': c = '\\'; break;
505 }
506 out->str[out->len++] = c;
507 }
508 else {
509 if (out->len > out->size - 4)
510 break;
511 out->str[out->len++] = '\\';
512 out->str[out->len++] = 'x';
513 out->str[out->len++] = hextab[(c >> 4) & 0xF];
514 out->str[out->len++] = hextab[c & 0xF];
515 }
516 ptr++;
517 }
518
519 return ptr;
520}
521
522/* print a buffer in hexa.
523 * Print stopped if <bsize> is reached, or if no more place in the chunk.
524 */
525static int dump_binary(struct chunk *out, const char *buf, int bsize)
526{
527 unsigned char c;
528 int ptr = 0;
529
530 while (ptr < bsize) {
531 c = buf[ptr];
532
533 if (out->len > out->size - 2)
534 break;
535 out->str[out->len++] = hextab[(c >> 4) & 0xF];
536 out->str[out->len++] = hextab[c & 0xF];
537
538 ptr++;
539 }
540 return ptr;
541}
542
543/* Dump the status of a table to a stream interface's
544 * read buffer. It returns 0 if the output buffer is full
545 * and needs to be called again, otherwise non-zero.
546 */
547static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
548 struct proxy *proxy, struct proxy *target)
549{
Willy Tarreau306f8302013-07-08 15:53:06 +0200550 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900551
Willy Tarreau77804732012-10-29 16:14:26 +0100552 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900553 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
554
555 /* any other information should be dumped here */
556
Willy Tarreau290e63a2012-09-20 18:07:14 +0200557 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100558 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900559
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200560 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900561 return 0;
562
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 Tarreau9dab5fc2012-05-07 11:56:55 +0200631 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900632 return 0;
633
634 return 1;
635}
636
Willy Tarreaudec98142012-06-06 23:37:08 +0200637static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900638{
Willy Tarreau306f8302013-07-08 15:53:06 +0200639 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100640 struct appctx *appctx = __objt_appctx(si->end);
641 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900642 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900643 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900644 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200645 long long value;
646 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200647 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200648 void *ptr;
649 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900650
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100651 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900652
653 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100654 appctx->ctx.cli.msg = "Key value expected\n";
655 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900656 return;
657 }
658
Simon Hormanc5b89f62011-06-15 15:18:50 +0900659 switch (px->table.type) {
660 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900661 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100662 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900663 break;
664 case STKTABLE_TYPE_IPV6:
665 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100666 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900667 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900668 case STKTABLE_TYPE_INTEGER:
669 {
670 char *endptr;
671 unsigned long val;
672 errno = 0;
673 val = strtoul(args[4], &endptr, 10);
674 if ((errno == ERANGE && val == ULONG_MAX) ||
675 (errno != 0 && val == 0) || endptr == args[4] ||
676 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100677 appctx->ctx.cli.msg = "Invalid key\n";
678 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900679 return;
680 }
681 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100682 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900683 break;
684 }
685 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900686 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100687 static_table_key->key = args[4];
688 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900689 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900690 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200691 switch (action) {
692 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100693 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 +0200694 break;
695 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100696 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 +0200697 break;
698 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100699 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200700 break;
701 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100702 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900703 return;
704 }
705
706 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200707 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100708 appctx->ctx.cli.msg = stats_permission_denied_msg;
709 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900710 return;
711 }
712
Willy Tarreau07115412012-10-29 21:56:59 +0100713 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900714
Willy Tarreaudec98142012-06-06 23:37:08 +0200715 switch (action) {
716 case STAT_CLI_O_TAB:
717 if (!ts)
718 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100719 chunk_reset(&trash);
720 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900721 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100722 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900723 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200724
725 case STAT_CLI_O_CLR:
726 if (!ts)
727 return;
728 if (ts->ref_cnt) {
729 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100730 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
731 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200732 return;
733 }
734 stksess_kill(&px->table, ts);
735 break;
Simon Horman17bce342011-06-15 15:18:47 +0900736
Willy Tarreau654694e2012-06-07 01:03:16 +0200737 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200738 if (ts)
739 stktable_touch(&px->table, ts, 1);
740 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100741 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200742 if (!ts) {
743 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100744 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
745 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200746 return;
747 }
748 stktable_store(&px->table, ts, 1);
749 }
750
Willy Tarreau47060b62013-08-01 21:11:42 +0200751 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
752 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100753 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
754 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200755 return;
756 }
757
758 data_type = stktable_get_data_type(args[cur_arg] + 5);
759 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100760 appctx->ctx.cli.msg = "Unknown data type\n";
761 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200762 return;
763 }
764
765 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100766 appctx->ctx.cli.msg = "Data type not stored in this table\n";
767 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200768 return;
769 }
770
771 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100772 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
773 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200774 return;
775 }
776
777 ptr = stktable_data_ptr(&px->table, ts, data_type);
778
779 switch (stktable_data_types[data_type].std_type) {
780 case STD_T_SINT:
781 stktable_data_cast(ptr, std_t_sint) = value;
782 break;
783 case STD_T_UINT:
784 stktable_data_cast(ptr, std_t_uint) = value;
785 break;
786 case STD_T_ULL:
787 stktable_data_cast(ptr, std_t_ull) = value;
788 break;
789 case STD_T_FRQP:
790 /* We set both the current and previous values. That way
791 * the reported frequency is stable during all the period
792 * then slowly fades out. This allows external tools to
793 * push measures without having to update them too often.
794 */
795 frqp = &stktable_data_cast(ptr, std_t_frqp);
796 frqp->curr_tick = now_ms;
797 frqp->prev_ctr = 0;
798 frqp->curr_ctr = value;
799 break;
800 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200801 }
802 break;
803
Willy Tarreaudec98142012-06-06 23:37:08 +0200804 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100805 appctx->ctx.cli.msg = "Unknown action\n";
806 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200807 break;
Simon Horman121f3052011-06-15 15:18:46 +0900808 }
Simon Horman121f3052011-06-15 15:18:46 +0900809}
810
Willy Tarreau654694e2012-06-07 01:03:16 +0200811static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900812{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100813 struct appctx *appctx = __objt_appctx(si->end);
814
Willy Tarreau04b3a192013-04-13 09:41:37 +0200815 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100816 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
817 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200818 return;
819 }
820
Simon Hormand5b9fd92011-06-15 15:18:48 +0900821 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100822 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
823 if (appctx->ctx.table.data_type < 0) {
824 appctx->ctx.cli.msg = "Unknown data type\n";
825 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900826 return;
827 }
828
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100829 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
830 appctx->ctx.cli.msg = "Data type not stored in this table\n";
831 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900832 return;
833 }
834
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100835 appctx->ctx.table.data_op = get_std_op(args[4]);
836 if (appctx->ctx.table.data_op < 0) {
837 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
838 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900839 return;
840 }
841
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100842 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
843 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
844 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900845 return;
846 }
847}
848
Willy Tarreaudec98142012-06-06 23:37:08 +0200849static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900850{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100851 struct appctx *appctx = __objt_appctx(si->end);
852
853 appctx->ctx.table.data_type = -1;
854 appctx->st2 = STAT_ST_INIT;
855 appctx->ctx.table.target = NULL;
856 appctx->ctx.table.proxy = NULL;
857 appctx->ctx.table.entry = NULL;
858 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900859
860 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100861 appctx->ctx.table.target = find_stktable(args[2]);
862 if (!appctx->ctx.table.target) {
863 appctx->ctx.cli.msg = "No such table\n";
864 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900865 return;
866 }
867 }
868 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200869 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900870 goto err_args;
871 return;
872 }
873
874 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200875 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900876 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200877 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900878 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900879 goto err_args;
880
881 return;
882
883err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200884 switch (action) {
885 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100886 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 +0200887 break;
888 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100889 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 +0200890 break;
891 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100892 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200893 break;
894 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100895 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900896}
897
Willy Tarreau532a4502011-09-07 22:37:44 +0200898/* Expects to find a frontend named <arg> and returns it, otherwise displays various
899 * adequate error messages and returns NULL. This function also expects the session
900 * level to be admin.
901 */
902static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
903{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100904 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200905 struct proxy *px;
906
Willy Tarreau290e63a2012-09-20 18:07:14 +0200907 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100908 appctx->ctx.cli.msg = stats_permission_denied_msg;
909 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200910 return NULL;
911 }
912
913 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100914 appctx->ctx.cli.msg = "A frontend name is expected.\n";
915 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200916 return NULL;
917 }
918
919 px = findproxy(arg, PR_CAP_FE);
920 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100921 appctx->ctx.cli.msg = "No such frontend.\n";
922 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200923 return NULL;
924 }
925 return px;
926}
927
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200928/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
929 * and returns the pointer to the server. Otherwise, display adequate error messages
930 * and returns NULL. This function also expects the session level to be admin. Note:
931 * the <arg> is modified to remove the '/'.
932 */
933static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
934{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100935 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200936 struct proxy *px;
937 struct server *sv;
938 char *line;
939
Willy Tarreau290e63a2012-09-20 18:07:14 +0200940 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100941 appctx->ctx.cli.msg = stats_permission_denied_msg;
942 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200943 return NULL;
944 }
945
946 /* split "backend/server" and make <line> point to server */
947 for (line = arg; *line; line++)
948 if (*line == '/') {
949 *line++ = '\0';
950 break;
951 }
952
953 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100954 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
955 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200956 return NULL;
957 }
958
959 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100960 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
961 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200962 return NULL;
963 }
964
965 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100966 appctx->ctx.cli.msg = "Proxy is disabled.\n";
967 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200968 return NULL;
969 }
970
971 return sv;
972}
973
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100974/* This function is used with map and acl management. It permits to browse
975 * each reference. The variable <getnext> must contain the current node,
976 * <end> point to the root node and the <flags> permit to filter required
977 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100978 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100979static inline
980struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
981 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100982{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100983 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100984
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100985 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100986
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100987 /* Get next list entry. */
988 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100989
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100990 /* If the entry is the last of the list, return NULL. */
991 if (&ref->list == end)
992 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100993
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100994 /* If the entry match the flag, return it. */
995 if (ref->flags & flags)
996 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100997 }
998}
999
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001000static inline
1001struct pat_ref *pat_ref_lookup_ref(const char *reference)
1002{
1003 int id;
1004 char *error;
1005
1006 /* If the reference starts by a '#', this is numeric id. */
1007 if (reference[0] == '#') {
1008 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1009 id = strtol(reference + 1, &error, 10);
1010 if (*error != '\0')
1011 return NULL;
1012
1013 /* Perform the unique id lookup. */
1014 return pat_ref_lookupid(id);
1015 }
1016
1017 /* Perform the string lookup. */
1018 return pat_ref_lookup(reference);
1019}
1020
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001021/* This function is used with map and acl management. It permits to browse
1022 * each reference.
1023 */
1024static inline
1025struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1026{
1027 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001028 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1029 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001030 return NULL;
1031 return expr;
1032}
1033
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001034/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001035 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001036 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001037 * designating the function which will have to process the request, which can
1038 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001039 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001040static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001041{
Willy Tarreau306f8302013-07-08 15:53:06 +02001042 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001043 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001044 char *args[MAX_STATS_ARGS + 1];
1045 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001046 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001047
1048 while (isspace((unsigned char)*line))
1049 line++;
1050
1051 arg = 0;
1052 args[arg] = line;
1053
1054 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001055 if (*line == '\\') {
1056 line++;
1057 if (*line == '\0')
1058 break;
1059 }
1060 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001061 *line++ = '\0';
1062
1063 while (isspace((unsigned char)*line))
1064 line++;
1065
1066 args[++arg] = line;
1067 continue;
1068 }
1069
1070 line++;
1071 }
1072
1073 while (++arg <= MAX_STATS_ARGS)
1074 args[arg] = line;
1075
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001076 /* remove \ */
1077 arg = 0;
1078 while (*args[arg] != '\0') {
1079 j = 0;
1080 for (i=0; args[arg][i] != '\0'; i++) {
1081 if (args[arg][i] == '\\')
1082 continue;
1083 args[arg][j] = args[arg][i];
1084 j++;
1085 }
1086 args[arg][j] = '\0';
1087 arg++;
1088 }
1089
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001090 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001091 if (strcmp(args[0], "show") == 0) {
1092 if (strcmp(args[1], "stat") == 0) {
1093 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001094 appctx->ctx.stats.flags |= STAT_BOUND;
1095 appctx->ctx.stats.iid = atoi(args[2]);
1096 appctx->ctx.stats.type = atoi(args[3]);
1097 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001098 }
1099
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001100 appctx->st2 = STAT_ST_INIT;
1101 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001102 }
1103 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001104 appctx->st2 = STAT_ST_INIT;
1105 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001106 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001107 else if (strcmp(args[1], "pools") == 0) {
1108 appctx->st2 = STAT_ST_INIT;
1109 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1110 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001111 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001112 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001113 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001114 appctx->ctx.cli.msg = stats_permission_denied_msg;
1115 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001116 return 1;
1117 }
Willy Tarreau76153662012-11-26 01:16:39 +01001118 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001119 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001120 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001121 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001122 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001123 appctx->ctx.sess.target = NULL;
1124 appctx->ctx.sess.section = 0; /* start with session status */
1125 appctx->ctx.sess.pos = 0;
1126 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001127 }
1128 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001129 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001130 appctx->ctx.cli.msg = stats_permission_denied_msg;
1131 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001132 return 1;
1133 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001134 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001135 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001136 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001137 appctx->ctx.errors.iid = -1;
1138 appctx->ctx.errors.px = NULL;
1139 appctx->st2 = STAT_ST_INIT;
1140 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001141 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001142 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001143 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001144 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001145 else if (strcmp(args[1], "map") == 0 ||
1146 strcmp(args[1], "acl") == 0) {
1147
1148 /* Set ACL or MAP flags. */
1149 if (args[1][0] == 'm')
1150 appctx->ctx.map.display_flags = PAT_REF_MAP;
1151 else
1152 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001153
1154 /* no parameter: display all map avalaible */
1155 if (!*args[2]) {
1156 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001157 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001158 return 1;
1159 }
1160
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001161 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001162 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001163 if (!appctx->ctx.map.ref ||
1164 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1165 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001166 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001167 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001168 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001169 appctx->st0 = STAT_CLI_PRINT;
1170 return 1;
1171 }
1172 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001173 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001174 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001175 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001176 return 0;
1177 }
1178 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001179 else if (strcmp(args[0], "clear") == 0) {
1180 if (strcmp(args[1], "counters") == 0) {
1181 struct proxy *px;
1182 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001183 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001184 int clrall = 0;
1185
1186 if (strcmp(args[2], "all") == 0)
1187 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001188
Willy Tarreau6162db22009-10-10 17:13:00 +02001189 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001190 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1191 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001192 appctx->ctx.cli.msg = stats_permission_denied_msg;
1193 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001194 return 1;
1195 }
1196
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001197 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001198 if (clrall) {
1199 memset(&px->be_counters, 0, sizeof(px->be_counters));
1200 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1201 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001202 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001203 px->be_counters.conn_max = 0;
1204 px->be_counters.p.http.rps_max = 0;
1205 px->be_counters.sps_max = 0;
1206 px->be_counters.cps_max = 0;
1207 px->be_counters.nbpend_max = 0;
1208
1209 px->fe_counters.conn_max = 0;
1210 px->fe_counters.p.http.rps_max = 0;
1211 px->fe_counters.sps_max = 0;
1212 px->fe_counters.cps_max = 0;
1213 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001214 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001215
1216 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001217 if (clrall)
1218 memset(&sv->counters, 0, sizeof(sv->counters));
1219 else {
1220 sv->counters.cur_sess_max = 0;
1221 sv->counters.nbpend_max = 0;
1222 sv->counters.sps_max = 0;
1223 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001224
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001225 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001226 if (li->counters) {
1227 if (clrall)
1228 memset(li->counters, 0, sizeof(*li->counters));
1229 else
1230 li->counters->conn_max = 0;
1231 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001232 }
1233
Willy Tarreau81c25d02011-09-07 15:17:21 +02001234 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001235 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001236 return 1;
1237 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001238 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001239 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001240 /* end of processing */
1241 return 1;
1242 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001243 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1244 /* Set ACL or MAP flags. */
1245 if (args[1][0] == 'm')
1246 appctx->ctx.map.display_flags = PAT_REF_MAP;
1247 else
1248 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001249
1250 /* no parameter */
1251 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001252 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1253 appctx->ctx.cli.msg = "Missing map identifier.\n";
1254 else
1255 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001256 appctx->st0 = STAT_CLI_PRINT;
1257 return 1;
1258 }
1259
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001260 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001261 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001262 if (!appctx->ctx.map.ref ||
1263 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1264 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001265 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001266 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001267 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001268 appctx->st0 = STAT_CLI_PRINT;
1269 return 1;
1270 }
1271
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001272 /* Clear all. */
1273 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001274
1275 /* return response */
1276 appctx->ctx.cli.msg = "Done.\n";
1277 appctx->st0 = STAT_CLI_PRINT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001278 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001279 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001280 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001281 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001282 return 0;
1283 }
1284 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001285 else if (strcmp(args[0], "get") == 0) {
1286 if (strcmp(args[1], "weight") == 0) {
1287 struct proxy *px;
1288 struct server *sv;
1289
1290 /* split "backend/server" and make <line> point to server */
1291 for (line = args[2]; *line; line++)
1292 if (*line == '/') {
1293 *line++ = '\0';
1294 break;
1295 }
1296
1297 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001298 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1299 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001300 return 1;
1301 }
1302
1303 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001304 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1305 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001306 return 1;
1307 }
1308
1309 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001310 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1311 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001312 return 1;
1313 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001314 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1315 /* Set flags. */
1316 if (args[1][0] == 'm')
1317 appctx->ctx.map.display_flags = PAT_REF_MAP;
1318 else
1319 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001320
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001321 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001322 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001323 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1324 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1325 else
1326 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001327 appctx->st0 = STAT_CLI_PRINT;
1328 return 1;
1329 }
1330
1331 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001332 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001333 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001334 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001335 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001336 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001337 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001338 appctx->st0 = STAT_CLI_PRINT;
1339 return 1;
1340 }
1341
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001342 /* copy input string. The string must be allocated because
1343 * it may be used over multiple iterations. It's released
1344 * at the end and upon abort anyway.
1345 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001346 appctx->ctx.map.chunk.len = strlen(args[3]);
1347 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1348 appctx->ctx.map.chunk.str = strdup(args[3]);
1349 if (!appctx->ctx.map.chunk.str) {
1350 appctx->ctx.cli.msg = "Out of memory error.\n";
1351 appctx->st0 = STAT_CLI_PRINT;
1352 return 1;
1353 }
1354
1355 /* prepare response */
1356 appctx->st2 = STAT_ST_INIT;
1357 appctx->st0 = STAT_CLI_O_MLOOK;
1358 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001359 else { /* not "get weight" */
1360 return 0;
1361 }
1362 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001363 else if (strcmp(args[0], "set") == 0) {
1364 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001365 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001366 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001367
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001368 sv = expect_server_admin(s, si, args[2]);
1369 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001370 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001371
Simon Horman7d09b9a2013-02-12 10:45:51 +09001372 warning = server_parse_weight_change_request(sv, args[3]);
1373 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001374 appctx->ctx.cli.msg = warning;
1375 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001376 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001377 return 1;
1378 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001379 else if (strcmp(args[1], "server") == 0) {
1380 struct server *sv;
1381 const char *warning;
1382
1383 sv = expect_server_admin(s, si, args[2]);
1384 if (!sv)
1385 return 1;
1386
1387 if (strcmp(args[3], "weight") == 0) {
1388 warning = server_parse_weight_change_request(sv, args[4]);
1389 if (warning) {
1390 appctx->ctx.cli.msg = warning;
1391 appctx->st0 = STAT_CLI_PRINT;
1392 }
1393 }
1394 else if (strcmp(args[3], "state") == 0) {
1395 if (strcmp(args[4], "ready") == 0)
1396 srv_adm_set_ready(sv);
1397 else if (strcmp(args[4], "drain") == 0)
1398 srv_adm_set_drain(sv);
1399 else if (strcmp(args[4], "maint") == 0)
1400 srv_adm_set_maint(sv);
1401 else {
1402 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1403 appctx->st0 = STAT_CLI_PRINT;
1404 }
1405 }
1406 else if (strcmp(args[3], "health") == 0) {
1407 if (sv->track) {
1408 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1409 appctx->st0 = STAT_CLI_PRINT;
1410 }
1411 else if (strcmp(args[4], "up") == 0) {
1412 sv->check.health = sv->check.rise + sv->check.fall - 1;
1413 srv_set_running(sv, "changed from CLI");
1414 }
1415 else if (strcmp(args[4], "stopping") == 0) {
1416 sv->check.health = sv->check.rise + sv->check.fall - 1;
1417 srv_set_stopping(sv, "changed from CLI");
1418 }
1419 else if (strcmp(args[4], "down") == 0) {
1420 sv->check.health = 0;
1421 srv_set_stopped(sv, "changed from CLI");
1422 }
1423 else {
1424 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1425 appctx->st0 = STAT_CLI_PRINT;
1426 }
1427 }
1428 else if (strcmp(args[3], "agent") == 0) {
1429 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1430 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1431 appctx->st0 = STAT_CLI_PRINT;
1432 }
1433 else if (strcmp(args[4], "up") == 0) {
1434 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1435 srv_set_running(sv, "changed from CLI");
1436 }
1437 else if (strcmp(args[4], "down") == 0) {
1438 sv->agent.health = 0;
1439 srv_set_stopped(sv, "changed from CLI");
1440 }
1441 else {
1442 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1443 appctx->st0 = STAT_CLI_PRINT;
1444 }
1445 }
1446 else {
1447 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1448 appctx->st0 = STAT_CLI_PRINT;
1449 }
1450 return 1;
1451 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001452 else if (strcmp(args[1], "timeout") == 0) {
1453 if (strcmp(args[2], "cli") == 0) {
1454 unsigned timeout;
1455 const char *res;
1456
1457 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001458 appctx->ctx.cli.msg = "Expects an integer value.\n";
1459 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001460 return 1;
1461 }
1462
1463 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1464 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001465 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1466 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001467 return 1;
1468 }
1469
1470 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1471 return 1;
1472 }
1473 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001474 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1475 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001476 return 1;
1477 }
1478 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001479 else if (strcmp(args[1], "maxconn") == 0) {
1480 if (strcmp(args[2], "frontend") == 0) {
1481 struct proxy *px;
1482 struct listener *l;
1483 int v;
1484
Willy Tarreau532a4502011-09-07 22:37:44 +02001485 px = expect_frontend_admin(s, si, args[3]);
1486 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001487 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001488
1489 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001490 appctx->ctx.cli.msg = "Integer value expected.\n";
1491 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001492 return 1;
1493 }
1494
1495 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001496 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001497 appctx->ctx.cli.msg = "Value out of range.\n";
1498 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001499 return 1;
1500 }
1501
1502 /* OK, the value is fine, so we assign it to the proxy and to all of
1503 * its listeners. The blocked ones will be dequeued.
1504 */
1505 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001506 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001507 l->maxconn = v;
1508 if (l->state == LI_FULL)
1509 resume_listener(l);
1510 }
1511
1512 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1513 dequeue_all_listeners(&s->fe->listener_queue);
1514
1515 return 1;
1516 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001517 else if (strcmp(args[2], "global") == 0) {
1518 int v;
1519
Willy Tarreau290e63a2012-09-20 18:07:14 +02001520 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001521 appctx->ctx.cli.msg = stats_permission_denied_msg;
1522 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001523 return 1;
1524 }
1525
1526 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001527 appctx->ctx.cli.msg = "Expects an integer value.\n";
1528 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001529 return 1;
1530 }
1531
1532 v = atoi(args[3]);
1533 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001534 appctx->ctx.cli.msg = "Value out of range.\n";
1535 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001536 return 1;
1537 }
1538
1539 /* check for unlimited values */
1540 if (v <= 0)
1541 v = global.hardmaxconn;
1542
1543 global.maxconn = v;
1544
1545 /* Dequeues all of the listeners waiting for a resource */
1546 if (!LIST_ISEMPTY(&global_listener_queue))
1547 dequeue_all_listeners(&global_listener_queue);
1548
1549 return 1;
1550 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001551 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001552 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1553 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001554 return 1;
1555 }
1556 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001557 else if (strcmp(args[1], "rate-limit") == 0) {
1558 if (strcmp(args[2], "connections") == 0) {
1559 if (strcmp(args[3], "global") == 0) {
1560 int v;
1561
Willy Tarreau290e63a2012-09-20 18:07:14 +02001562 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001563 appctx->ctx.cli.msg = stats_permission_denied_msg;
1564 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001565 return 1;
1566 }
1567
1568 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001569 appctx->ctx.cli.msg = "Expects an integer value.\n";
1570 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001571 return 1;
1572 }
1573
1574 v = atoi(args[4]);
1575 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001576 appctx->ctx.cli.msg = "Value out of range.\n";
1577 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001578 return 1;
1579 }
1580
1581 global.cps_lim = v;
1582
1583 /* Dequeues all of the listeners waiting for a resource */
1584 if (!LIST_ISEMPTY(&global_listener_queue))
1585 dequeue_all_listeners(&global_listener_queue);
1586
1587 return 1;
1588 }
1589 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001590 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1591 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001592 return 1;
1593 }
1594 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001595 else if (strcmp(args[2], "sessions") == 0) {
1596 if (strcmp(args[3], "global") == 0) {
1597 int v;
1598
1599 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1600 appctx->ctx.cli.msg = stats_permission_denied_msg;
1601 appctx->st0 = STAT_CLI_PRINT;
1602 return 1;
1603 }
1604
1605 if (!*args[4]) {
1606 appctx->ctx.cli.msg = "Expects an integer value.\n";
1607 appctx->st0 = STAT_CLI_PRINT;
1608 return 1;
1609 }
1610
1611 v = atoi(args[4]);
1612 if (v < 0) {
1613 appctx->ctx.cli.msg = "Value out of range.\n";
1614 appctx->st0 = STAT_CLI_PRINT;
1615 return 1;
1616 }
1617
1618 global.sps_lim = v;
1619
1620 /* Dequeues all of the listeners waiting for a resource */
1621 if (!LIST_ISEMPTY(&global_listener_queue))
1622 dequeue_all_listeners(&global_listener_queue);
1623
1624 return 1;
1625 }
1626 else {
1627 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1628 appctx->st0 = STAT_CLI_PRINT;
1629 return 1;
1630 }
1631 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001632#ifdef USE_OPENSSL
1633 else if (strcmp(args[2], "ssl-sessions") == 0) {
1634 if (strcmp(args[3], "global") == 0) {
1635 int v;
1636
1637 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1638 appctx->ctx.cli.msg = stats_permission_denied_msg;
1639 appctx->st0 = STAT_CLI_PRINT;
1640 return 1;
1641 }
1642
1643 if (!*args[4]) {
1644 appctx->ctx.cli.msg = "Expects an integer value.\n";
1645 appctx->st0 = STAT_CLI_PRINT;
1646 return 1;
1647 }
1648
1649 v = atoi(args[4]);
1650 if (v < 0) {
1651 appctx->ctx.cli.msg = "Value out of range.\n";
1652 appctx->st0 = STAT_CLI_PRINT;
1653 return 1;
1654 }
1655
1656 global.ssl_lim = v;
1657
1658 /* Dequeues all of the listeners waiting for a resource */
1659 if (!LIST_ISEMPTY(&global_listener_queue))
1660 dequeue_all_listeners(&global_listener_queue);
1661
1662 return 1;
1663 }
1664 else {
1665 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1666 appctx->st0 = STAT_CLI_PRINT;
1667 return 1;
1668 }
1669 }
1670#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001671 else if (strcmp(args[2], "http-compression") == 0) {
1672 if (strcmp(args[3], "global") == 0) {
1673 int v;
1674
Willy Tarreau85d47f92012-11-21 00:29:50 +01001675 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001676 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1677 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001678 return 1;
1679 }
1680
William Lallemandd85f9172012-11-09 17:05:39 +01001681 v = atoi(args[4]);
1682 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1683 }
1684 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001685 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1686 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001687 return 1;
1688 }
1689 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001690 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001691 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001692 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001693 return 1;
1694 }
1695 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001696 else if (strcmp(args[1], "table") == 0) {
1697 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1698 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001699 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001700 char *err;
1701
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001702 /* Set flags. */
1703 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001704
1705 /* Expect three parameters: map name, key and new value. */
1706 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001707 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001708 appctx->st0 = STAT_CLI_PRINT;
1709 return 1;
1710 }
1711
1712 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001713 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001714 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001715 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001716 appctx->st0 = STAT_CLI_PRINT;
1717 return 1;
1718 }
1719
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001720 /* If the entry identifier start with a '#', it is considered as
1721 * pointer id
1722 */
1723 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1724 struct pat_ref_elt *ref;
1725 long long int conv;
1726 char *error;
1727
1728 /* Convert argument to integer value. */
1729 conv = strtoll(&args[3][1], &error, 16);
1730 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001731 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001732 appctx->st0 = STAT_CLI_PRINT;
1733 return 1;
1734 }
1735
1736 /* Convert and check integer to pointer. */
1737 ref = (struct pat_ref_elt *)(long)conv;
1738 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001739 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001740 appctx->st0 = STAT_CLI_PRINT;
1741 return 1;
1742 }
1743
1744 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001745 err = NULL;
1746 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1747 if (err)
1748 memprintf(&err, "%s.\n", err);
1749 appctx->ctx.cli.err = err;
1750 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001751 return 1;
1752 }
1753 }
1754 else {
1755 /* Else, use the entry identifier as pattern
1756 * string, and update the value.
1757 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001758 err = NULL;
1759 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1760 if (err)
1761 memprintf(&err, "%s.\n", err);
1762 appctx->ctx.cli.err = err;
1763 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001764 return 1;
1765 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001766 }
1767
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001768 /* The set is done, send message. */
1769 appctx->ctx.cli.msg = "Done.\n";
1770 appctx->st0 = STAT_CLI_PRINT;
1771 return 1;
1772 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001773 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001774 return 0;
1775 }
1776 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001777 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001778 if (strcmp(args[1], "agent") == 0) {
1779 struct server *sv;
1780
1781 sv = expect_server_admin(s, si, args[2]);
1782 if (!sv)
1783 return 1;
1784
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001785 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1786 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1787 appctx->st0 = STAT_CLI_PRINT;
1788 return 1;
1789 }
1790
1791 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001792 return 1;
1793 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001794 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001795 struct server *sv;
1796
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001797 sv = expect_server_admin(s, si, args[2]);
1798 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001799 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001800
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001801 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001802 return 1;
1803 }
1804 else if (strcmp(args[1], "frontend") == 0) {
1805 struct proxy *px;
1806
1807 px = expect_frontend_admin(s, si, args[2]);
1808 if (!px)
1809 return 1;
1810
1811 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001812 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1813 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001814 return 1;
1815 }
1816
1817 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001818 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1819 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001820 return 1;
1821 }
1822
1823 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001824 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1825 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001826 return 1;
1827 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001828 return 1;
1829 }
1830 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001831 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1832 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001833 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001834 }
1835 }
1836 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001837 if (strcmp(args[1], "agent") == 0) {
1838 struct server *sv;
1839
1840 sv = expect_server_admin(s, si, args[2]);
1841 if (!sv)
1842 return 1;
1843
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001844 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001845 return 1;
1846 }
1847 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001848 struct server *sv;
1849
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001850 sv = expect_server_admin(s, si, args[2]);
1851 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001852 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001853
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001854 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001855 return 1;
1856 }
1857 else if (strcmp(args[1], "frontend") == 0) {
1858 struct proxy *px;
1859
1860 px = expect_frontend_admin(s, si, args[2]);
1861 if (!px)
1862 return 1;
1863
1864 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001865 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1866 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001867 return 1;
1868 }
1869
1870 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001871 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1872 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001873 return 1;
1874 }
1875
1876 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001877 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1878 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001879 return 1;
1880 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001881 return 1;
1882 }
1883 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001884 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1885 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001886 return 1;
1887 }
1888 }
1889 else if (strcmp(args[0], "shutdown") == 0) {
1890 if (strcmp(args[1], "frontend") == 0) {
1891 struct proxy *px;
1892
1893 px = expect_frontend_admin(s, si, args[2]);
1894 if (!px)
1895 return 1;
1896
1897 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001898 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1899 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001900 return 1;
1901 }
1902
1903 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1904 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1905 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1906 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1907 stop_proxy(px);
1908 return 1;
1909 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001910 else if (strcmp(args[1], "session") == 0) {
1911 struct session *sess, *ptr;
1912
Willy Tarreau290e63a2012-09-20 18:07:14 +02001913 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001914 appctx->ctx.cli.msg = stats_permission_denied_msg;
1915 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001916 return 1;
1917 }
1918
1919 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001920 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1921 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001922 return 1;
1923 }
1924
1925 ptr = (void *)strtoul(args[2], NULL, 0);
1926
1927 /* first, look for the requested session in the session table */
1928 list_for_each_entry(sess, &sessions, list) {
1929 if (sess == ptr)
1930 break;
1931 }
1932
1933 /* do we have the session ? */
1934 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001935 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1936 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001937 return 1;
1938 }
1939
1940 session_shutdown(sess, SN_ERR_KILLED);
1941 return 1;
1942 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001943 else if (strcmp(args[1], "sessions") == 0) {
1944 if (strcmp(args[2], "server") == 0) {
1945 struct server *sv;
1946 struct session *sess, *sess_bck;
1947
1948 sv = expect_server_admin(s, si, args[3]);
1949 if (!sv)
1950 return 1;
1951
1952 /* kill all the session that are on this server */
1953 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1954 if (sess->srv_conn == sv)
1955 session_shutdown(sess, SN_ERR_KILLED);
1956
1957 return 1;
1958 }
1959 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001960 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1961 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001962 return 1;
1963 }
1964 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001965 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001966 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1967 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001968 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001969 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001970 }
1971 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001972 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1973 if (args[1][0] == 'm')
1974 appctx->ctx.map.display_flags = PAT_REF_MAP;
1975 else
1976 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001977
1978 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001979 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1980 if (!*args[2] || !*args[3]) {
1981 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
1982 appctx->st0 = STAT_CLI_PRINT;
1983 return 1;
1984 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001985 }
1986
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001987 else {
1988 if (!*args[2] || !*args[3]) {
1989 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
1990 appctx->st0 = STAT_CLI_PRINT;
1991 return 1;
1992 }
1993 }
1994
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001995 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001996 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001997 if (!appctx->ctx.map.ref ||
1998 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001999 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002000 appctx->st0 = STAT_CLI_PRINT;
2001 return 1;
2002 }
2003
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002004 /* If the entry identifier start with a '#', it is considered as
2005 * pointer id
2006 */
2007 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2008 struct pat_ref_elt *ref;
2009 long long int conv;
2010 char *error;
2011
2012 /* Convert argument to integer value. */
2013 conv = strtoll(&args[3][1], &error, 16);
2014 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002015 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002016 appctx->st0 = STAT_CLI_PRINT;
2017 return 1;
2018 }
2019
2020 /* Convert and check integer to pointer. */
2021 ref = (struct pat_ref_elt *)(long)conv;
2022 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002023 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002024 appctx->st0 = STAT_CLI_PRINT;
2025 return 1;
2026 }
2027
2028 /* Try to delete the entry. */
2029 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2030 /* The entry is not found, send message. */
2031 appctx->ctx.cli.msg = "Key not found.\n";
2032 appctx->st0 = STAT_CLI_PRINT;
2033 return 1;
2034 }
2035 }
2036 else {
2037 /* Else, use the entry identifier as pattern
2038 * string and try to delete the entry.
2039 */
2040 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2041 /* The entry is not found, send message. */
2042 appctx->ctx.cli.msg = "Key not found.\n";
2043 appctx->st0 = STAT_CLI_PRINT;
2044 return 1;
2045 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002046 }
2047
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002048 /* The deletion is done, send message. */
2049 appctx->ctx.cli.msg = "Done.\n";
2050 appctx->st0 = STAT_CLI_PRINT;
2051 return 1;
2052 }
2053 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002054 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002055 appctx->st0 = STAT_CLI_PRINT;
2056 return 1;
2057 }
2058 }
2059 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002060 if (strcmp(args[1], "map") == 0 ||
2061 strcmp(args[1], "acl") == 0) {
2062 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002063 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002064
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002065 /* Set flags. */
2066 if (args[1][0] == 'm')
2067 appctx->ctx.map.display_flags = PAT_REF_MAP;
2068 else
2069 appctx->ctx.map.display_flags = PAT_REF_ACL;
2070
2071 /* If the keywork is "map", we expect three parameters, if it
2072 * is "acl", we expect only two parameters
2073 */
2074 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2075 if (!*args[2] || !*args[3] || !*args[4]) {
2076 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2077 appctx->st0 = STAT_CLI_PRINT;
2078 return 1;
2079 }
2080 }
2081 else {
2082 if (!*args[2] || !*args[3]) {
2083 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2084 appctx->st0 = STAT_CLI_PRINT;
2085 return 1;
2086 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002087 }
2088
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002089 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002090 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002091 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002092 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002093 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002094 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002095 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002096 appctx->st0 = STAT_CLI_PRINT;
2097 return 1;
2098 }
2099
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002100 /* The command "add acl" is prohibited if the reference
2101 * use samples.
2102 */
2103 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2104 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2105 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2106 "You must use the command 'add map' to add values.\n";
2107 appctx->st0 = STAT_CLI_PRINT;
2108 return 1;
2109 }
2110
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002111 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002112 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002113 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002114 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002115 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002116 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002117 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002118 if (err)
2119 memprintf(&err, "%s.\n", err);
2120 appctx->ctx.cli.err = err;
2121 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002122 return 1;
2123 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002124
2125 /* The add is done, send message. */
2126 appctx->ctx.cli.msg = "Done.\n";
2127 appctx->st0 = STAT_CLI_PRINT;
2128 return 1;
2129 }
2130 else { /* unknown "del" parameter */
2131 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2132 appctx->st0 = STAT_CLI_PRINT;
2133 return 1;
2134 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002135 }
2136 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002137 return 0;
2138 }
2139 return 1;
2140}
2141
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002142/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002143 * used to processes I/O from/to the stats unix socket. The system relies on a
2144 * state machine handling requests and various responses. We read a request,
2145 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002146 * Then we can read again. The state is stored in appctx->st0 and is one of the
2147 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002148 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002149 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002150static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002151{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002152 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002153 struct channel *req = si->ob;
2154 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002155 int reql;
2156 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002157
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002158 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2159 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002160
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002161 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002162 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002163 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002164 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2165 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002166 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002167 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002168 /* Let's close for real now. We just close the request
2169 * side, the conditions below will complete if needed.
2170 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002171 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002172 break;
2173 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002174 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002175 /* ensure we have some output room left in the event we
2176 * would want to return some info right after parsing.
2177 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002178 if (buffer_almost_full(si->ib->buf)) {
2179 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002180 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002181 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002182
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002183 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002184 if (reql <= 0) { /* closed or EOL not found */
2185 if (reql == 0)
2186 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002187 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002188 continue;
2189 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002190
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002191 /* seek for a possible semi-colon. If we find one, we
2192 * replace it with an LF and skip only this part.
2193 */
2194 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002195 if (trash.str[len] == ';') {
2196 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002197 reql = len + 1;
2198 break;
2199 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002200
Willy Tarreau816fc222009-10-04 07:36:58 +02002201 /* now it is time to check that we have a full line,
2202 * remove the trailing \n and possibly \r, then cut the
2203 * line.
2204 */
2205 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002206 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002207 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002208 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002209 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002210
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002211 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002212 len--;
2213
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002214 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002215
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002216 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002217 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002218 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002219 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002220 continue;
2221 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002222 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002223 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002224 else if (strcmp(trash.str, "help") == 0 ||
2225 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002226 appctx->ctx.cli.msg = stats_sock_usage_msg;
2227 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002228 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002229 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002230 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002231 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002232 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002233 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002234 /* if prompt is disabled, print help on empty lines,
2235 * so that the user at least knows how to enable
2236 * prompt and find help.
2237 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002238 appctx->ctx.cli.msg = stats_sock_usage_msg;
2239 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002240 }
2241
2242 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002243 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002244 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002245 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002246 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002247 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002248 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002249 continue;
2250 }
2251
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002252 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002253 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002254 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2255 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002256 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002257 case STAT_CLI_PRINT_FREE:
2258 if (bi_putstr(si->ib, appctx->ctx.cli.err) != -1) {
2259 free(appctx->ctx.cli.err);
2260 appctx->st0 = STAT_CLI_PROMPT;
2261 }
2262 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002263 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002264 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002265 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002266 break;
2267 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002268 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002269 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002270 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002271 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002272 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002273 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002274 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002275 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002276 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002277 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002278 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002279 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002280 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002281 if (stats_table_request(si, appctx->st0))
2282 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002283 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002284 case STAT_CLI_O_PATS:
2285 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002286 appctx->st0 = STAT_CLI_PROMPT;
2287 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002288 case STAT_CLI_O_PAT:
2289 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002290 appctx->st0 = STAT_CLI_PROMPT;
2291 break;
2292 case STAT_CLI_O_MLOOK:
2293 if (stats_map_lookup(si))
2294 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002295 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002296 case STAT_CLI_O_POOLS:
2297 if (stats_dump_pools_to_buffer(si))
2298 appctx->st0 = STAT_CLI_PROMPT;
2299 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002300 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002301 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002302 break;
2303 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002304
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002305 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002306 if (appctx->st0 == STAT_CLI_PROMPT) {
2307 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2308 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002309 }
2310
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002311 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002312 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002313 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002314
2315 /* Now we close the output if one of the writers did so,
2316 * or if we're not in interactive mode and the request
2317 * buffer is empty. This still allows pipelined requests
2318 * to be sent in non-interactive mode.
2319 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002320 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2321 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002322 continue;
2323 }
2324
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002325 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002326 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002327 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002328 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002329
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002330 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002331 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2332 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002333 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002334 * and nothing more to consume. This is comparable to a broken pipe, so
2335 * we forward the close to the request side so that it flows upstream to
2336 * the client.
2337 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002338 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002339 }
2340
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002341 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002342 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2343 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2344 /* We have no more processing to do, and nothing more to send, and
2345 * the client side has closed. So we'll forward this state downstream
2346 * on the response buffer.
2347 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002348 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002349 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002350 }
2351
2352 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002353 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002354
2355 /* we don't want to expire timeouts while we're processing requests */
2356 si->ib->rex = TICK_ETERNITY;
2357 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002358
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002359 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002360 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 +02002361 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002362 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 +02002363
2364 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2365 /* check that we have released everything then unregister */
2366 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002367 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002368}
2369
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002370/* This function dumps information onto the stream interface's read buffer.
2371 * It returns 0 as long as it does not complete, non-zero upon completion.
2372 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002373 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002374static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002375{
2376 unsigned int up = (now.tv_sec - start_date.tv_sec);
2377
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002378 chunk_printf(&trash,
2379 "Name: " PRODUCT_NAME "\n"
2380 "Version: " HAPROXY_VERSION "\n"
2381 "Release_date: " HAPROXY_DATE "\n"
2382 "Nbproc: %d\n"
2383 "Process_num: %d\n"
2384 "Pid: %d\n"
2385 "Uptime: %dd %dh%02dm%02ds\n"
2386 "Uptime_sec: %d\n"
2387 "Memmax_MB: %d\n"
2388 "Ulimit-n: %d\n"
2389 "Maxsock: %d\n"
2390 "Maxconn: %d\n"
2391 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002392 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002393 "CumConns: %d\n"
2394 "CumReq: %d\n"
2395#ifdef USE_OPENSSL
2396 "MaxSslConns: %d\n"
2397 "CurrSslConns: %d\n"
2398 "CumSslConns: %d\n"
2399#endif
2400 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002401 "PipesUsed: %d\n"
2402 "PipesFree: %d\n"
2403 "ConnRate: %d\n"
2404 "ConnRateLimit: %d\n"
2405 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002406 "SessRate: %d\n"
2407 "SessRateLimit: %d\n"
2408 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002409#ifdef USE_OPENSSL
2410 "SslRate: %d\n"
2411 "SslRateLimit: %d\n"
2412 "MaxSslRate: %d\n"
2413#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002414 "CompressBpsIn: %u\n"
2415 "CompressBpsOut: %u\n"
2416 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002417#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002418 "ZlibMemUsage: %ld\n"
2419 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002420#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002421 "Tasks: %d\n"
2422 "Run_queue: %d\n"
2423 "Idle_pct: %d\n"
2424 "node: %s\n"
2425 "description: %s\n"
2426 "",
2427 global.nbproc,
2428 relative_pid,
2429 pid,
2430 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2431 up,
2432 global.rlimit_memmax,
2433 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002434 global.maxsock, global.maxconn, global.hardmaxconn,
2435 actconn, totalconn, global.req_count,
2436#ifdef USE_OPENSSL
2437 global.maxsslconn, sslconns, totalsslconns,
2438#endif
2439 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002440 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002441 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002442#ifdef USE_OPENSSL
2443 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2444#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002445 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2446 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002447#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002448 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002449#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002450 nb_tasks_cur, run_queue_cur, idle_pct,
2451 global.node, global.desc ? global.desc : ""
2452 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002453
2454 if (bi_putchk(si->ib, &trash) == -1)
2455 return 0;
2456
2457 return 1;
2458}
2459
Willy Tarreau12833bb2014-01-28 16:49:56 +01002460/* This function dumps memory usage information onto the stream interface's
2461 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2462 * completion. No state is used.
2463 */
2464static int stats_dump_pools_to_buffer(struct stream_interface *si)
2465{
2466 dump_pools_to_trash();
2467 if (bi_putchk(si->ib, &trash) == -1)
2468 return 0;
2469 return 1;
2470}
2471
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002472/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2473 * the state from stream interface <si>. The caller is responsible for clearing
2474 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002475 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002476static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002477{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002478 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002479 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002480
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002481 if (!(px->cap & PR_CAP_FE))
2482 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002483
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002484 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002485 return 0;
2486
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002487 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002488 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002489 /* name, queue */
2490 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002491
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002492 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002493 /* Column sub-heading for Enable or Disable server */
2494 chunk_appendf(&trash, "<td></td>");
2495 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002496
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002497 chunk_appendf(&trash,
2498 "<td class=ac>"
2499 "<a name=\"%s/Frontend\"></a>"
2500 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2501 "<td colspan=3></td>"
2502 "",
2503 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002504
Willy Tarreau466c9b52012-12-23 02:25:03 +01002505 chunk_appendf(&trash,
2506 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002507 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002508 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2509 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2510 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002511 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2512 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2513 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002514
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002515 if (px->mode == PR_MODE_HTTP)
2516 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002517 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002518 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002519
2520 chunk_appendf(&trash,
2521 "</table></div></u></td>"
2522 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002523 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002524 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2525 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2526 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002527 U2H(px->fe_counters.sps_max),
2528 U2H(px->fe_counters.cps_max),
2529 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002530
2531 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002532 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002533 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002534 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002535
2536 chunk_appendf(&trash,
2537 "</table></div></u></td>"
2538 /* sessions rate : limit */
2539 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002540 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002541
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002542 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002543 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002544 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002545 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002546 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2547 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002548 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002549 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2550 U2H(px->fe_counters.cum_sess),
2551 U2H(px->fe_counters.cum_conn),
2552 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002553
Willy Tarreau466c9b52012-12-23 02:25:03 +01002554 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002555 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002556 chunk_appendf(&trash,
2557 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2558 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2559 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2560 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2561 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2562 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2563 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2564 "<tr><th>- other responses:</th><td>%s</td></tr>"
2565 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2566 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002567 U2H(px->fe_counters.p.http.cum_req),
2568 U2H(px->fe_counters.p.http.rsp[1]),
2569 U2H(px->fe_counters.p.http.rsp[2]),
2570 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002571 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002572 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002573 U2H(px->fe_counters.p.http.rsp[3]),
2574 U2H(px->fe_counters.p.http.rsp[4]),
2575 U2H(px->fe_counters.p.http.rsp[5]),
2576 U2H(px->fe_counters.p.http.rsp[0]),
2577 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002578 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002579
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002580 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002581 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002582 /* sessions: lbtot, lastsess */
2583 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002584 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002585 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002586 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002587 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002588
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002589 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002590 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002591 "<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 +01002592 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002593 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002594 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2595 px->fe_counters.comp_in ?
2596 (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 +01002597 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002598
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002599 chunk_appendf(&trash,
2600 /* denied: req, resp */
2601 "<td>%s</td><td>%s</td>"
2602 /* errors : request, connect, response */
2603 "<td>%s</td><td></td><td></td>"
2604 /* warnings: retries, redispatches */
2605 "<td></td><td></td>"
2606 /* server status : reflect frontend status */
2607 "<td class=ac>%s</td>"
2608 /* rest of server: nothing */
2609 "<td class=ac colspan=8></td></tr>"
2610 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002611 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2612 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002613 px->state == PR_STREADY ? "OPEN" :
2614 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002615 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002616 else { /* CSV mode */
2617 chunk_appendf(&trash,
2618 /* pxid, name, queue cur, queue max, */
2619 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002620 /* sessions : current, max, limit, total */
2621 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002622 /* bytes : in, out */
2623 "%lld,%lld,"
2624 /* denied: req, resp */
2625 "%lld,%lld,"
2626 /* errors : request, connect, response */
2627 "%lld,,,"
2628 /* warnings: retries, redispatches */
2629 ",,"
2630 /* server status : reflect frontend status */
2631 "%s,"
2632 /* rest of server: nothing */
2633 ",,,,,,,,"
2634 /* pid, iid, sid, throttle, lbtot, tracked, type */
2635 "%d,%d,0,,,,%d,"
2636 /* rate, rate_lim, rate_max */
2637 "%u,%u,%u,"
2638 /* check_status, check_code, check_duration */
2639 ",,,",
2640 px->id,
2641 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2642 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2643 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2644 px->fe_counters.failed_req,
2645 px->state == PR_STREADY ? "OPEN" :
2646 px->state == PR_STFULL ? "FULL" : "STOP",
2647 relative_pid, px->uuid, STATS_TYPE_FE,
2648 read_freq_ctr(&px->fe_sess_per_sec),
2649 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002650
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002651 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2652 if (px->mode == PR_MODE_HTTP) {
2653 for (i=1; i<6; i++)
2654 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2655 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2656 }
2657 else
2658 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002659
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002660 /* failed health analyses */
2661 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002662
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002663 /* requests : req_rate, req_rate_max, req_tot, */
2664 chunk_appendf(&trash, "%u,%u,%lld,",
2665 read_freq_ctr(&px->fe_req_per_sec),
2666 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2667
2668 /* errors: cli_aborts, srv_aborts */
2669 chunk_appendf(&trash, ",,");
2670
2671 /* compression: in, out, bypassed */
2672 chunk_appendf(&trash, "%lld,%lld,%lld,",
2673 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2674
2675 /* compression: comp_rsp */
2676 chunk_appendf(&trash, "%lld,",
2677 px->fe_counters.p.http.comp_rsp);
2678
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002679 /* lastsess */
2680 chunk_appendf(&trash, ",");
2681
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002682 /* finish with EOL */
2683 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002684 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002685 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002686}
2687
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002688/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2689 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2690 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2691 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002692 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002693static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002694{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002695 struct appctx *appctx = __objt_appctx(si->end);
2696
2697 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002698 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002699 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002700 /* Column sub-heading for Enable or Disable server */
2701 chunk_appendf(&trash, "<td></td>");
2702 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002703 chunk_appendf(&trash,
2704 /* frontend name, listener name */
2705 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2706 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2707 "",
2708 px->id, l->name,
2709 (flags & ST_SHLGNDS)?"<u>":"",
2710 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002711
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002712 if (flags & ST_SHLGNDS) {
2713 char str[INET6_ADDRSTRLEN];
2714 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002715
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002716 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002717
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002718 port = get_host_port(&l->addr);
2719 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2720 case AF_INET:
2721 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2722 break;
2723 case AF_INET6:
2724 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2725 break;
2726 case AF_UNIX:
2727 chunk_appendf(&trash, "unix, ");
2728 break;
2729 case -1:
2730 chunk_appendf(&trash, "(%s), ", strerror(errno));
2731 break;
2732 }
Willy Tarreau91861262007-10-17 17:06:05 +02002733
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002734 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002735 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002736 }
Willy Tarreau91861262007-10-17 17:06:05 +02002737
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002738 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002739 /* queue */
2740 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002741 /* sessions rate: current, max, limit */
2742 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002743 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002744 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002745 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002746 /* bytes: in, out */
2747 "<td>%s</td><td>%s</td>"
2748 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002749 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002750 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2751 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002752
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002753 chunk_appendf(&trash,
2754 /* denied: req, resp */
2755 "<td>%s</td><td>%s</td>"
2756 /* errors: request, connect, response */
2757 "<td>%s</td><td></td><td></td>"
2758 /* warnings: retries, redispatches */
2759 "<td></td><td></td>"
2760 /* server status: reflect listener status */
2761 "<td class=ac>%s</td>"
2762 /* rest of server: nothing */
2763 "<td class=ac colspan=8></td></tr>"
2764 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002765 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2766 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002767 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2768 }
2769 else { /* CSV mode */
2770 chunk_appendf(&trash,
2771 /* pxid, name, queue cur, queue max, */
2772 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002773 /* sessions: current, max, limit, total */
2774 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002775 /* bytes: in, out */
2776 "%lld,%lld,"
2777 /* denied: req, resp */
2778 "%lld,%lld,"
2779 /* errors: request, connect, response */
2780 "%lld,,,"
2781 /* warnings: retries, redispatches */
2782 ",,"
2783 /* server status: reflect listener status */
2784 "%s,"
2785 /* rest of server: nothing */
2786 ",,,,,,,,"
2787 /* pid, iid, sid, throttle, lbtot, tracked, type */
2788 "%d,%d,%d,,,,%d,"
2789 /* rate, rate_lim, rate_max */
2790 ",,,"
2791 /* check_status, check_code, check_duration */
2792 ",,,"
2793 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2794 ",,,,,,"
2795 /* failed health analyses */
2796 ","
2797 /* requests : req_rate, req_rate_max, req_tot, */
2798 ",,,"
2799 /* errors: cli_aborts, srv_aborts */
2800 ",,"
2801 /* compression: in, out, bypassed, comp_rsp */
2802 ",,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002803 /* lastsess */
2804 ","
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002805 "\n",
2806 px->id, l->name,
2807 l->nbconn, l->counters->conn_max,
2808 l->maxconn, l->counters->cum_conn,
2809 l->counters->bytes_in, l->counters->bytes_out,
2810 l->counters->denied_req, l->counters->denied_resp,
2811 l->counters->failed_req,
2812 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2813 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2814 }
2815 return 1;
2816}
Willy Tarreau91861262007-10-17 17:06:05 +02002817
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002818/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2819 * from stream interface <si>, stats flags <flags>, and server state <state>.
2820 * The caller is responsible for clearing the trash if needed. Returns non-zero
2821 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002822 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2823 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002824 */
2825static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2826{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002827 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002828 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002829 char str[INET6_ADDRSTRLEN];
2830 struct chunk src;
2831 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002832
Willy Tarreau32091232014-05-16 13:52:00 +02002833 /* we have "via" which is the tracked server as described in the configuration,
2834 * and "ref" which is the checked server and the end of the chain.
2835 */
2836 via = sv->track ? sv->track : sv;
2837 ref = via;
2838 while (ref->track)
2839 ref = ref->track;
2840
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002841 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002842 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002843 "DOWN",
2844 "DN %d/%d &uarr;",
2845 "UP %d/%d &darr;",
2846 "UP",
2847 "NOLB %d/%d &darr;",
2848 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002849 "DRAIN %d/%d &darr;",
2850 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002851 "<i>no check</i>"
2852 };
Willy Tarreau91861262007-10-17 17:06:05 +02002853
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002854 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002855 chunk_appendf(&trash, "<tr class=\"maintain\">");
2856 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002857 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002858 "<tr class=\"%s%d\">",
Willy Tarreauc93cd162014-05-13 15:54:22 +02002859 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002860
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002861 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002862 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002863 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2864 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002865
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002866 chunk_appendf(&trash,
2867 "<td class=ac><a name=\"%s/%s\"></a>%s"
2868 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2869 "",
2870 px->id, sv->id,
2871 (flags & ST_SHLGNDS) ? "<u>" : "",
2872 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002873
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002874 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002875 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002876
2877 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2878 case AF_INET:
2879 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2880 break;
2881 case AF_INET6:
2882 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2883 break;
2884 case AF_UNIX:
2885 chunk_appendf(&trash, "unix, ");
2886 break;
2887 case -1:
2888 chunk_appendf(&trash, "(%s), ", strerror(errno));
2889 break;
2890 default: /* address family not supported */
2891 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002892 }
Willy Tarreau91861262007-10-17 17:06:05 +02002893
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002894 /* id */
2895 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002896
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002897 /* cookie */
2898 if (sv->cookie) {
2899 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002900
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002901 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2902 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002903
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002904 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002905 }
2906
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002907 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002908 }
Willy Tarreau91861262007-10-17 17:06:05 +02002909
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002910 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002911 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002912 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002913 /* sessions rate : current, max, limit */
2914 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002915 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002916 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002917 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2918 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002919
Willy Tarreau466c9b52012-12-23 02:25:03 +01002920
2921 chunk_appendf(&trash,
2922 /* sessions: current, max, limit, total */
2923 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002924 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002925 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2926 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002927 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2928 U2H(sv->counters.cum_sess),
2929 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002930
Willy Tarreau466c9b52012-12-23 02:25:03 +01002931 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2932 if (px->mode == PR_MODE_HTTP) {
2933 unsigned long long tot;
2934 for (tot = i = 0; i < 6; i++)
2935 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002936
Willy Tarreau466c9b52012-12-23 02:25:03 +01002937 chunk_appendf(&trash,
2938 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2939 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2940 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2941 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2942 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2943 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2944 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2945 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002946 U2H(tot),
2947 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2948 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2949 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2950 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2951 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2952 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 +02002953 }
Willy Tarreau91861262007-10-17 17:06:05 +02002954
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002955 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002956 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002957 /* sessions: lbtot, last */
2958 "<td>%s</td><td>%s</td>",
2959 U2H(sv->counters.cum_lbconn),
2960 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02002961
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002962 chunk_appendf(&trash,
2963 /* bytes : in, out */
2964 "<td>%s</td><td>%s</td>"
2965 /* denied: req, resp */
2966 "<td></td><td>%s</td>"
2967 /* errors : request, connect */
2968 "<td></td><td>%s</td>"
2969 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002970 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002971 /* warnings: retries, redispatches */
2972 "<td>%lld</td><td>%lld</td>"
2973 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002974 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2975 U2H(sv->counters.failed_secu),
2976 U2H(sv->counters.failed_conns),
2977 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002978 sv->counters.cli_aborts,
2979 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002980 sv->counters.retries, sv->counters.redispatches);
2981
2982 /* status, lest check */
2983 chunk_appendf(&trash, "<td class=ac>");
2984
Willy Tarreau20125212014-05-13 19:44:56 +02002985 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002986 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2987 chunk_appendf(&trash, "MAINT");
2988 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002989 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002990 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2991 chunk_appendf(&trash,
2992 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02002993 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2994 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002995 }
Willy Tarreau91861262007-10-17 17:06:05 +02002996
Willy Tarreauff5ae352013-12-11 20:36:34 +01002997 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002998 chunk_appendf(&trash,
2999 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003000 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003001 get_check_status_info(sv->check.status));
3002
3003 if (sv->check.status >= HCHK_STATUS_L57DATA)
3004 chunk_appendf(&trash, "/%d", sv->check.code);
3005
3006 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003007 chunk_appendf(&trash, " in %lums", sv->check.duration);
3008
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003009 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003010 get_check_status_description(sv->check.status));
3011 if (*sv->check.desc) {
3012 chunk_appendf(&trash, ": ");
3013 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3014 chunk_htmlencode(&trash, &src);
3015 }
3016 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003017 }
3018 else
3019 chunk_appendf(&trash, "</td><td>");
3020
3021 chunk_appendf(&trash,
3022 /* weight */
3023 "</td><td class=ac>%d</td>"
3024 /* act, bck */
3025 "<td class=ac>%s</td><td class=ac>%s</td>"
3026 "",
3027 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003028 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3029 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003030
3031 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003032 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003033 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003034
3035 if (ref->observe)
3036 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3037
3038 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003039 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003040 "<td>%lld</td><td>%s</td>"
3041 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003042 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003043 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3044 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003045 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3046 /* tracking a server */
3047 chunk_appendf(&trash,
3048 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003049 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003050 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003051 else
3052 chunk_appendf(&trash, "<td colspan=3></td>");
3053
3054 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003055 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003056 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003057 else
3058 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3059 }
3060 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003061 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003062 "DOWN,",
3063 "DOWN %d/%d,",
3064 "UP %d/%d,",
3065 "UP,",
3066 "NOLB %d/%d,",
3067 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09003068 "DRAIN %d/%d,",
3069 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003070 "no check,"
3071 };
3072
3073 chunk_appendf(&trash,
3074 /* pxid, name */
3075 "%s,%s,"
3076 /* queue : current, max */
3077 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003078 /* sessions : current, max, limit, total */
3079 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003080 /* bytes : in, out */
3081 "%lld,%lld,"
3082 /* denied: req, resp */
3083 ",%lld,"
3084 /* errors : request, connect, response */
3085 ",%lld,%lld,"
3086 /* warnings: retries, redispatches */
3087 "%lld,%lld,"
3088 "",
3089 px->id, sv->id,
3090 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003091 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003092 sv->counters.bytes_in, sv->counters.bytes_out,
3093 sv->counters.failed_secu,
3094 sv->counters.failed_conns, sv->counters.failed_resp,
3095 sv->counters.retries, sv->counters.redispatches);
3096
3097 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003098 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003099 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003100 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003101 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003102 else
3103 chunk_appendf(&trash,
3104 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003105 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3106 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003107
3108 chunk_appendf(&trash,
3109 /* weight, active, backup */
3110 "%d,%d,%d,"
3111 "",
3112 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003113 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3114 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003115
3116 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003117 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003118 chunk_appendf(&trash,
3119 "%lld,%lld,%d,%d,",
3120 sv->counters.failed_checks, sv->counters.down_trans,
3121 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3122 else
3123 chunk_appendf(&trash, ",,,,");
3124
3125 /* queue limit, pid, iid, sid, */
3126 chunk_appendf(&trash,
3127 "%s,"
3128 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003129 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003130 relative_pid, px->uuid, sv->puid);
3131
3132 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003133 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003134 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003135
3136 /* sessions: lbtot */
3137 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3138
3139 /* tracked */
3140 if (sv->track)
3141 chunk_appendf(&trash, "%s/%s,",
3142 sv->track->proxy->id, sv->track->id);
3143 else
3144 chunk_appendf(&trash, ",");
3145
3146 /* type */
3147 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3148
3149 /* rate */
3150 chunk_appendf(&trash, "%u,,%u,",
3151 read_freq_ctr(&sv->sess_per_sec),
3152 sv->counters.sps_max);
3153
Willy Tarreauff5ae352013-12-11 20:36:34 +01003154 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003155 /* check_status */
3156 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3157
3158 /* check_code */
3159 if (sv->check.status >= HCHK_STATUS_L57DATA)
3160 chunk_appendf(&trash, "%u,", sv->check.code);
3161 else
3162 chunk_appendf(&trash, ",");
3163
3164 /* check_duration */
3165 if (sv->check.status >= HCHK_STATUS_CHECKED)
3166 chunk_appendf(&trash, "%lu,", sv->check.duration);
3167 else
3168 chunk_appendf(&trash, ",");
3169
3170 }
3171 else
3172 chunk_appendf(&trash, ",,,");
3173
3174 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3175 if (px->mode == PR_MODE_HTTP) {
3176 for (i=1; i<6; i++)
3177 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3178
3179 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3180 }
3181 else
3182 chunk_appendf(&trash, ",,,,,,");
3183
3184 /* failed health analyses */
3185 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3186
3187 /* requests : req_rate, req_rate_max, req_tot, */
3188 chunk_appendf(&trash, ",,,");
3189
3190 /* errors: cli_aborts, srv_aborts */
3191 chunk_appendf(&trash, "%lld,%lld,",
3192 sv->counters.cli_aborts, sv->counters.srv_aborts);
3193
3194 /* compression: in, out, bypassed, comp_rsp */
3195 chunk_appendf(&trash, ",,,,");
3196
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003197 /* lastsess */
3198 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3199
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003200 /* finish with EOL */
3201 chunk_appendf(&trash, "\n");
3202 }
3203 return 1;
3204}
3205
3206/* Dumps a line for backend <px> to the trash for and uses the state from stream
3207 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3208 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3209 */
3210static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3211{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003212 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003213 struct chunk src;
3214 int i;
3215
3216 if (!(px->cap & PR_CAP_BE))
3217 return 0;
3218
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003219 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003220 return 0;
3221
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003222 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003223 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003224 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003225 /* Column sub-heading for Enable or Disable server */
3226 chunk_appendf(&trash, "<td></td>");
3227 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003228 chunk_appendf(&trash,
3229 "<td class=ac>"
3230 /* name */
3231 "%s<a name=\"%s/Backend\"></a>"
3232 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3233 "",
3234 (flags & ST_SHLGNDS)?"<u>":"",
3235 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003236
3237 if (flags & ST_SHLGNDS) {
3238 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003239 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003240 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3241
3242 /* cookie */
3243 if (px->cookie_name) {
3244 chunk_appendf(&trash, ", cookie: '");
3245 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3246 chunk_htmlencode(&trash, &src);
3247 chunk_appendf(&trash, "'");
3248 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003249 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003250 }
3251
3252 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003253 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003254 /* queue : current, max */
3255 "<td>%s</td><td>%s</td><td></td>"
3256 /* sessions rate : current, max, limit */
3257 "<td>%s</td><td>%s</td><td></td>"
3258 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003259 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003260 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3261 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003262
3263 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003264 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003265 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003266 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003267 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003268 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003269 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3270 U2H(px->be_counters.cum_conn),
3271 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003272
Willy Tarreau466c9b52012-12-23 02:25:03 +01003273 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003274 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003275 chunk_appendf(&trash,
3276 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3277 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3278 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3279 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3280 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3281 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3282 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3283 "<tr><th>- other responses:</th><td>%s</td></tr>"
3284 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3285 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003286 U2H(px->be_counters.p.http.cum_req),
3287 U2H(px->be_counters.p.http.rsp[1]),
3288 U2H(px->be_counters.p.http.rsp[2]),
3289 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003290 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003291 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003292 U2H(px->be_counters.p.http.rsp[3]),
3293 U2H(px->be_counters.p.http.rsp[4]),
3294 U2H(px->be_counters.p.http.rsp[5]),
3295 U2H(px->be_counters.p.http.rsp[0]),
3296 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003297 }
3298
3299 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003300 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003301 /* sessions: lbtot, last */
3302 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003303 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003304 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003305 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003306 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003307 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003308 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003309
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003310 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003311 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003312 "<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 +01003313 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003314 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003315 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3316 px->be_counters.comp_in ?
3317 (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 +01003318 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3319
3320 chunk_appendf(&trash,
3321 /* denied: req, resp */
3322 "<td>%s</td><td>%s</td>"
3323 /* errors : request, connect */
3324 "<td></td><td>%s</td>"
3325 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003326 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003327 /* warnings: retries, redispatches */
3328 "<td>%lld</td><td>%lld</td>"
3329 /* backend status: reflect backend status (up/down): we display UP
3330 * if the backend has known working servers or if it has no server at
3331 * all (eg: for stats). Then we display the total weight, number of
3332 * active and backups. */
3333 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3334 "<td class=ac>%d</td><td class=ac>%d</td>"
3335 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003336 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3337 U2H(px->be_counters.failed_conns),
3338 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003339 px->be_counters.cli_aborts,
3340 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003341 px->be_counters.retries, px->be_counters.redispatches,
3342 human_time(now.tv_sec - px->last_change, 1),
3343 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3344 "<font color=\"red\"><b>DOWN</b></font>",
3345 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3346 px->srv_act, px->srv_bck);
3347
3348 chunk_appendf(&trash,
3349 /* rest of backend: nothing, down transitions, total downtime, throttle */
3350 "<td class=ac>&nbsp;</td><td>%d</td>"
3351 "<td>%s</td>"
3352 "<td></td>"
3353 "</tr>",
3354 px->down_trans,
3355 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3356 }
3357 else { /* CSV mode */
3358 chunk_appendf(&trash,
3359 /* pxid, name */
3360 "%s,BACKEND,"
3361 /* queue : current, max */
3362 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003363 /* sessions : current, max, limit, total */
3364 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003365 /* bytes : in, out */
3366 "%lld,%lld,"
3367 /* denied: req, resp */
3368 "%lld,%lld,"
3369 /* errors : request, connect, response */
3370 ",%lld,%lld,"
3371 /* warnings: retries, redispatches */
3372 "%lld,%lld,"
3373 /* backend status: reflect backend status (up/down): we display UP
3374 * if the backend has known working servers or if it has no server at
3375 * all (eg: for stats). Then we display the total weight, number of
3376 * active and backups. */
3377 "%s,"
3378 "%d,%d,%d,"
3379 /* rest of backend: nothing, down transitions, last change, total downtime */
3380 ",%d,%d,%d,,"
3381 /* pid, iid, sid, throttle, lbtot, tracked, type */
3382 "%d,%d,0,,%lld,,%d,"
3383 /* rate, rate_lim, rate_max, */
3384 "%u,,%u,"
3385 /* check_status, check_code, check_duration */
3386 ",,,",
3387 px->id,
3388 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3389 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3390 px->be_counters.bytes_in, px->be_counters.bytes_out,
3391 px->be_counters.denied_req, px->be_counters.denied_resp,
3392 px->be_counters.failed_conns, px->be_counters.failed_resp,
3393 px->be_counters.retries, px->be_counters.redispatches,
3394 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3395 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3396 px->srv_act, px->srv_bck,
3397 px->down_trans, (int)(now.tv_sec - px->last_change),
3398 px->srv?be_downtime(px):0,
3399 relative_pid, px->uuid,
3400 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3401 read_freq_ctr(&px->be_sess_per_sec),
3402 px->be_counters.sps_max);
3403
3404 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3405 if (px->mode == PR_MODE_HTTP) {
3406 for (i=1; i<6; i++)
3407 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3408 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3409 }
3410 else
3411 chunk_appendf(&trash, ",,,,,,");
3412
3413 /* failed health analyses */
3414 chunk_appendf(&trash, ",");
3415
3416 /* requests : req_rate, req_rate_max, req_tot, */
3417 chunk_appendf(&trash, ",,,");
3418
3419 /* errors: cli_aborts, srv_aborts */
3420 chunk_appendf(&trash, "%lld,%lld,",
3421 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3422
3423 /* compression: in, out, bypassed */
3424 chunk_appendf(&trash, "%lld,%lld,%lld,",
3425 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3426
3427 /* compression: comp_rsp */
3428 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3429
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003430 /* lastsess */
3431 chunk_appendf(&trash, "%d,", be_lastsession(px));
3432
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003433 /* finish with EOL */
3434 chunk_appendf(&trash, "\n");
3435 }
3436 return 1;
3437}
3438
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003439/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003440 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003441 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003442 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003443static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003444{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003445 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003446 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3447
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003448 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003449 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003450
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003451 /* 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 +02003452 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003453 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003454 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003455 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3456 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003457 }
3458
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003459 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003460 "<form action=\"%s%s%s%s\" method=\"post\">",
3461 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003462 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3463 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003464 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003465 }
3466
3467 /* print a new table */
3468 chunk_appendf(&trash,
3469 "<table class=\"tbl\" width=\"100%%\">\n"
3470 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003471 "<th class=\"pxname\" width=\"10%%\">");
3472
3473 chunk_appendf(&trash,
3474 "<a name=\"%s\"></a>%s"
3475 "<a class=px href=\"#%s\">%s</a>",
3476 px->id,
3477 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3478 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003479
3480 if (uri->flags & ST_SHLGNDS) {
3481 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003482 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003483 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3484 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003485 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003486 }
3487
3488 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003489 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003490 "<th class=\"%s\" width=\"90%%\">%s</th>"
3491 "</tr>\n"
3492 "</table>\n"
3493 "<table class=\"tbl\" width=\"100%%\">\n"
3494 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003495 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3496 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3497
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003498 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003499 /* Column heading for Enable or Disable server */
3500 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003501 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003502
3503 chunk_appendf(&trash,
3504 "<th rowspan=2></th>"
3505 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003506 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003507 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3508 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3509 "<th colspan=9>Server</th>"
3510 "</tr>\n"
3511 "<tr class=\"titre\">"
3512 "<th>Cur</th><th>Max</th><th>Limit</th>"
3513 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003514 "<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 +01003515 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3516 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3517 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3518 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3519 "<th>Thrtle</th>\n"
3520 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003521}
3522
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003523/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003524 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003525 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003526static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003527{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003528 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003529 chunk_appendf(&trash, "</table>");
3530
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003531 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003532 /* close the form used to enable/disable this proxy servers */
3533 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003534 "Choose the action to perform on the checked servers : "
3535 "<select name=action>"
3536 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003537 "<option value=\"ready\">Set state to READY</option>"
3538 "<option value=\"drain\">Set state to DRAIN</option>"
3539 "<option value=\"maint\">set state to MAINT</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003540 "<option value=\"shutdown\">Kill Sessions</option>"
3541 "</select>"
3542 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3543 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3544 "</form>",
3545 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003546 }
3547
3548 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003549}
Willy Tarreau91861262007-10-17 17:06:05 +02003550
3551/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003552 * Dumps statistics for a proxy. The output is sent to the stream interface's
3553 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3554 * buffer space, or non-zero if everything completed. This function is used
3555 * both by the CLI and the HTTP entry points, and is able to dump the output
3556 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003557 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003558static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003559{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003560 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003561 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003562 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003563 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003564 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003565
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003566 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003567
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003568 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003569 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003570 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003571 if (uri && uri->scope) {
3572 /* we have a limited scope, we have to check the proxy name */
3573 struct stat_scope *scope;
3574 int len;
3575
3576 len = strlen(px->id);
3577 scope = uri->scope;
3578
3579 while (scope) {
3580 /* match exact proxy name */
3581 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3582 break;
3583
3584 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003585 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003586 break;
3587 scope = scope->next;
3588 }
3589
3590 /* proxy name not found : don't dump anything */
3591 if (scope == NULL)
3592 return 1;
3593 }
3594
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003595 /* if the user has requested a limited output and the proxy
3596 * name does not match, skip it.
3597 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003598 if (appctx->ctx.stats.scope_len &&
3599 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003600 return 1;
3601
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003602 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3603 (appctx->ctx.stats.iid != -1) &&
3604 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003605 return 1;
3606
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003607 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003608 /* fall through */
3609
Willy Tarreau295a8372011-03-10 11:25:07 +01003610 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003611 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003612 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003613 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003614 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003615 }
Willy Tarreau91861262007-10-17 17:06:05 +02003616
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003617 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003618 /* fall through */
3619
Willy Tarreau295a8372011-03-10 11:25:07 +01003620 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003621 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003622 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003623 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003624 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003625
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003626 appctx->ctx.stats.l = px->conf.listeners.n;
3627 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003628 /* fall through */
3629
Willy Tarreau295a8372011-03-10 11:25:07 +01003630 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003631 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003632 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003633 if (buffer_almost_full(rep->buf)) {
3634 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003635 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003636 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003637
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003638 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003639 if (!l->counters)
3640 continue;
3641
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003642 if (appctx->ctx.stats.flags & STAT_BOUND) {
3643 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003644 break;
3645
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003646 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003647 continue;
3648 }
3649
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003650 /* print the frontend */
3651 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3652 if (bi_putchk(rep, &trash) == -1)
3653 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003654 }
3655
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003656 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3657 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003658 /* fall through */
3659
Willy Tarreau295a8372011-03-10 11:25:07 +01003660 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003661 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003662 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003663 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB, 6=unchecked */
Willy Tarreau91861262007-10-17 17:06:05 +02003664
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003665 if (buffer_almost_full(rep->buf)) {
3666 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003667 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003668 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003669
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003670 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003671
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003672 if (appctx->ctx.stats.flags & STAT_BOUND) {
3673 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003674 break;
3675
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003676 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003677 continue;
3678 }
3679
Willy Tarreau32091232014-05-16 13:52:00 +02003680 svs = sv;
3681 while (svs->track)
3682 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003683
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003684 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
3685 /* server is UP. The possibilities are :
3686 * - UP, draining, going down => state = 6
3687 * - UP, going down => state = 2
3688 * - UP, draining => state = 7
3689 * - UP, checked => state = 3
3690 * - UP, not checked nor tracked => state = 8
3691 */
3692
3693 if ((svs->check.state & CHK_ST_ENABLED) &&
3694 (svs->check.health < svs->check.rise + svs->check.fall - 1))
3695 sv_state = 2;
Willy Tarreau91861262007-10-17 17:06:05 +02003696 else
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003697 sv_state = 3;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003698
Willy Tarreauefe28222014-05-21 17:13:13 +02003699 if (server_is_draining(sv))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003700 sv_state += 4;
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003701
3702 if (sv_state == 3 && !(svs->check.state & CHK_ST_ENABLED))
3703 sv_state = 8; /* unchecked UP */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003704 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003705 else if (sv->state == SRV_ST_STOPPING) {
3706 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
3707 (svs->check.health == svs->check.rise + svs->check.fall - 1))
3708 sv_state = 5; /* NOLB */
Willy Tarreau91861262007-10-17 17:06:05 +02003709 else
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003710 sv_state = 4; /* NOLB going down */
3711 }
3712 else { /* stopped */
3713 if ((svs->check.state & CHK_ST_ENABLED && !svs->check.health) ||
3714 (svs->agent.state & CHK_ST_ENABLED && !svs->agent.health))
Willy Tarreau91861262007-10-17 17:06:05 +02003715 sv_state = 0; /* DOWN */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003716 else
3717 sv_state = 1; /* going up */
3718 }
Willy Tarreau91861262007-10-17 17:06:05 +02003719
Willy Tarreau20125212014-05-13 19:44:56 +02003720 if (((sv_state == 0) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003721 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003722 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003723 continue;
3724 }
3725
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003726 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3727 if (bi_putchk(rep, &trash) == -1)
3728 return 0;
3729 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003730
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003731 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003732 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003733
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003734 case STAT_PX_ST_BE:
3735 /* print the backend */
3736 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3737 if (bi_putchk(rep, &trash) == -1)
3738 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003739
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003740 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003741 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003742
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003743 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003744 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003745 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003746 if (bi_putchk(rep, &trash) == -1)
3747 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003748 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003749
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003750 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003751 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003752
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003753 case STAT_PX_ST_FIN:
3754 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003755
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003756 default:
3757 /* unknown state, we should put an abort() here ! */
3758 return 1;
3759 }
3760}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003761
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003762/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3763 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003764 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003765static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003766{
3767 /* WARNING! This must fit in the first buffer !!! */
3768 chunk_appendf(&trash,
3769 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3770 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3771 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3772 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3773 "<style type=\"text/css\"><!--\n"
3774 "body {"
3775 " font-family: arial, helvetica, sans-serif;"
3776 " font-size: 12px;"
3777 " font-weight: normal;"
3778 " color: black;"
3779 " background: white;"
3780 "}\n"
3781 "th,td {"
3782 " font-size: 10px;"
3783 "}\n"
3784 "h1 {"
3785 " font-size: x-large;"
3786 " margin-bottom: 0.5em;"
3787 "}\n"
3788 "h2 {"
3789 " font-family: helvetica, arial;"
3790 " font-size: x-large;"
3791 " font-weight: bold;"
3792 " font-style: italic;"
3793 " color: #6020a0;"
3794 " margin-top: 0em;"
3795 " margin-bottom: 0em;"
3796 "}\n"
3797 "h3 {"
3798 " font-family: helvetica, arial;"
3799 " font-size: 16px;"
3800 " font-weight: bold;"
3801 " color: #b00040;"
3802 " background: #e8e8d0;"
3803 " margin-top: 0em;"
3804 " margin-bottom: 0em;"
3805 "}\n"
3806 "li {"
3807 " margin-top: 0.25em;"
3808 " margin-right: 2em;"
3809 "}\n"
3810 ".hr {margin-top: 0.25em;"
3811 " border-color: black;"
3812 " border-bottom-style: solid;"
3813 "}\n"
3814 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3815 ".total {background: #20D0D0;color: #ffff80;}\n"
3816 ".frontend {background: #e8e8d0;}\n"
3817 ".socket {background: #d0d0d0;}\n"
3818 ".backend {background: #e8e8d0;}\n"
3819 ".active0 {background: #ff9090;}\n"
3820 ".active1 {background: #ffd020;}\n"
3821 ".active2 {background: #ffffa0;}\n"
3822 ".active3 {background: #c0ffc0;}\n"
3823 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003824 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3825 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3826 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003827 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003828 ".backup0 {background: #ff9090;}\n"
3829 ".backup1 {background: #ff80ff;}\n"
3830 ".backup2 {background: #c060ff;}\n"
3831 ".backup3 {background: #b0d0ff;}\n"
3832 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3833 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003834 ".backup6 {background: #c060ff;}\n"
3835 ".backup7 {background: #cc9900;}\n"
3836 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003837 ".maintain {background: #c07820;}\n"
3838 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3839 "\n"
3840 "a.px:link {color: #ffff40; text-decoration: none;}"
3841 "a.px:visited {color: #ffff40; text-decoration: none;}"
3842 "a.px:hover {color: #ffffff; text-decoration: none;}"
3843 "a.lfsb:link {color: #000000; text-decoration: none;}"
3844 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3845 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3846 "\n"
3847 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3848 "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"
3849 "table.tbl td.ac { text-align: center;}\n"
3850 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3851 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3852 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3853 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3854 "\n"
3855 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3856 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3857 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003858 "table.det { border-collapse: collapse; border-style: none; }\n"
3859 "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 +01003860 "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 +01003861 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003862 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003863 " display:block;\n"
3864 " visibility:hidden;\n"
3865 " z-index:2147483647;\n"
3866 " position:absolute;\n"
3867 " padding:2px 4px 3px;\n"
3868 " background:#f0f060; color:#000000;\n"
3869 " border:1px solid #7040c0;\n"
3870 " white-space:nowrap;\n"
3871 " font-style:normal;font-size:11px;font-weight:normal;\n"
3872 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3873 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3874 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003875 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003876 "-->\n"
3877 "</style></head>\n",
3878 (uri->flags & ST_SHNODE) ? " on " : "",
3879 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3880 );
3881}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003882
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003883/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003884 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003885 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003886 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003887static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003888{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003889 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003890 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003891 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003892
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003893 /* WARNING! this has to fit the first packet too.
3894 * We are around 3.5 kB, add adding entries will
3895 * become tricky if we want to support 4kB buffers !
3896 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003897 chunk_appendf(&trash,
3898 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3899 PRODUCT_NAME "%s</a></h1>\n"
3900 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3901 "<hr width=\"100%%\" class=\"hr\">\n"
3902 "<h3>&gt; General process information</h3>\n"
3903 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3904 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3905 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3906 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3907 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3908 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3909 "Running tasks: %d/%d; idle = %d %%<br>\n"
3910 "</td><td align=\"center\" nowrap>\n"
3911 "<table class=\"lgd\"><tr>\n"
3912 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3913 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3914 "</tr><tr>\n"
3915 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3916 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3917 "</tr><tr>\n"
3918 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3919 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3920 "</tr><tr>\n"
3921 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003922 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003923 "</tr><tr>\n"
3924 "<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 -07003925 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003926 "<td class=\"active7\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003927 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003928 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003929 "</td>"
3930 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3931 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3932 "",
3933 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3934 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3935 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3936 (uri->flags & ST_SHDESC) ? ": " : "",
3937 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3938 pid, relative_pid, global.nbproc,
3939 up / 86400, (up % 86400) / 3600,
3940 (up % 3600) / 60, (up % 60),
3941 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3942 global.rlimit_memmax ? " MB" : "",
3943 global.rlimit_nofile,
3944 global.maxsock, global.maxconn, global.maxpipes,
3945 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3946 run_queue_cur, nb_tasks_cur, idle_pct
3947 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003948
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003949 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3950 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3951 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003952
3953 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003954 "<li><form method=\"GET\" action=\"%s%s%s\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003955 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003956 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3957 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3958 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003959 STAT_SCOPE_TXT_MAXLEN);
3960
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003961 /* 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 +02003962 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003963 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003964 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003965 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3966 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003967 }
3968
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003969 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003970 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003971 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003972 uri->uri_prefix,
3973 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003974 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003975 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003976 else
3977 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003978 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003979 uri->uri_prefix,
3980 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003981 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003982 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003983
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003984 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003985 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003986 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003987 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003988 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003989 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003990 "",
3991 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003992 else
3993 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003994 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003995 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003996 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003997 ";norefresh",
3998 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003999 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004000
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004001 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004002 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004003 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004004 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4005 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004006 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004007
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004008 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004009 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004010 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004011 (uri->refresh > 0) ? ";norefresh" : "",
4012 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004013
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004014 chunk_appendf(&trash,
4015 "</ul></td>"
4016 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4017 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4018 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4019 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4020 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4021 "</ul>"
4022 "</td>"
4023 "</tr></table>\n"
4024 ""
4025 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004026
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004027 if (appctx->ctx.stats.st_code) {
4028 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004029 case STAT_STATUS_DONE:
4030 chunk_appendf(&trash,
4031 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004032 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004033 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004034 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004035 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4036 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004037 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004038 break;
4039 case STAT_STATUS_NONE:
4040 chunk_appendf(&trash,
4041 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004042 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004043 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004044 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004045 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4046 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004047 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004048 break;
4049 case STAT_STATUS_PART:
4050 chunk_appendf(&trash,
4051 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004052 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004053 "Action partially processed.<br>"
4054 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004055 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004056 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4057 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004058 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004059 break;
4060 case STAT_STATUS_ERRP:
4061 chunk_appendf(&trash,
4062 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004063 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004064 "Action not processed because of invalid parameters."
4065 "<ul>"
4066 "<li>The action is maybe unknown.</li>"
4067 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4068 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4069 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004070 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004071 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4072 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004073 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004074 break;
4075 case STAT_STATUS_EXCD:
4076 chunk_appendf(&trash,
4077 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004078 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004079 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4080 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004081 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004082 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4083 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004084 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004085 break;
4086 case STAT_STATUS_DENY:
4087 chunk_appendf(&trash,
4088 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004089 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004090 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004091 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004092 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4093 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004094 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004095 break;
4096 default:
4097 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09004098 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004099 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004100 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004101 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004102 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4103 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004104 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004105 }
4106 chunk_appendf(&trash, "<p>\n");
4107 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004108}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004109
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004110/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4111 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004112 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004113static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004114{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004115 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004116}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004117
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004118/* This function dumps statistics onto the stream interface's read buffer in
4119 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004120 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4121 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
4122 * and the session must be closed, or -1 in case of any error. This function is
4123 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004124 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004125static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004126{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004127 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004128 struct channel *rep = si->ib;
4129 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004130
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004131 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004132
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004133 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004134 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004135 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004136 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004137
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004138 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004139 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004140 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004141 else
4142 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004143
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004144 if (bi_putchk(rep, &trash) == -1)
4145 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004146
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004147 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004148 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004149
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004150 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004151 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004152 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004153 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004154 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004155 }
Willy Tarreau91861262007-10-17 17:06:05 +02004156
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004157 appctx->ctx.stats.px = proxy;
4158 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4159 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004160 /* fall through */
4161
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004162 case STAT_ST_LIST:
4163 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004164 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004165 if (buffer_almost_full(rep->buf)) {
4166 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004167 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004168 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004169
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004170 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004171 /* skip the disabled proxies, global frontend and non-networked ones */
4172 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004173 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004174 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004175
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004176 appctx->ctx.stats.px = px->next;
4177 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004178 }
4179 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004180
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004181 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004182 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004183
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004184 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004185 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004186 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004187 if (bi_putchk(rep, &trash) == -1)
4188 return 0;
4189 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004190
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004191 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004192 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004193
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004194 case STAT_ST_FIN:
4195 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004196
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004197 default:
4198 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004199 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004200 return -1;
4201 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004202}
Willy Tarreauae526782010-03-04 20:34:23 +01004203
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004204/* We reached the stats page through a POST request. The appctx is
4205 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004206 * Parse the posted data and enable/disable servers if necessary.
4207 * Returns 1 if request was parsed or zero if it needs more data.
4208 */
4209static int stats_process_http_post(struct stream_interface *si)
4210{
4211 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004212 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004213
4214 struct proxy *px = NULL;
4215 struct server *sv = NULL;
4216
4217 char key[LINESIZE];
4218 int action = ST_ADM_ACTION_NONE;
4219 int reprocess = 0;
4220
4221 int total_servers = 0;
4222 int altered_servers = 0;
4223
4224 char *first_param, *cur_param, *next_param, *end_params;
4225 char *st_cur_param = NULL;
4226 char *st_next_param = NULL;
4227
4228 struct chunk *temp;
4229 int reql;
4230
4231 temp = get_trash_chunk();
4232 if (temp->size < s->txn.req.body_len) {
4233 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004234 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004235 goto out;
4236 }
4237
4238 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4239 if (reql <= 0) {
4240 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004241 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004242 return 0;
4243 }
4244
4245 first_param = temp->str;
4246 end_params = temp->str + reql;
4247 cur_param = next_param = end_params;
4248 *end_params = '\0';
4249
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004250 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004251
4252 /*
4253 * Parse the parameters in reverse order to only store the last value.
4254 * From the html form, the backend and the action are at the end.
4255 */
4256 while (cur_param > first_param) {
4257 char *value;
4258 int poffset, plen;
4259
4260 cur_param--;
4261
4262 if ((*cur_param == '&') || (cur_param == first_param)) {
4263 reprocess_servers:
4264 /* Parse the key */
4265 poffset = (cur_param != first_param ? 1 : 0);
4266 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4267 if ((plen > 0) && (plen <= sizeof(key))) {
4268 strncpy(key, cur_param + poffset, plen);
4269 key[plen - 1] = '\0';
4270 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004271 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004272 goto out;
4273 }
4274
4275 /* Parse the value */
4276 value = key;
4277 while (*value != '\0' && *value != '=') {
4278 value++;
4279 }
4280 if (*value == '=') {
4281 /* Ok, a value is found, we can mark the end of the key */
4282 *value++ = '\0';
4283 }
4284 if (url_decode(key) < 0 || url_decode(value) < 0)
4285 break;
4286
4287 /* Now we can check the key to see what to do */
4288 if (!px && (strcmp(key, "b") == 0)) {
4289 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4290 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004291 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004292 goto out;
4293 }
4294 }
4295 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004296 if (strcmp(value, "ready") == 0) {
4297 action = ST_ADM_ACTION_READY;
4298 }
4299 else if (strcmp(value, "drain") == 0) {
4300 action = ST_ADM_ACTION_DRAIN;
4301 }
4302 else if (strcmp(value, "maint") == 0) {
4303 action = ST_ADM_ACTION_MAINT;
4304 }
4305 else if (strcmp(value, "shutdown") == 0) {
4306 action = ST_ADM_ACTION_SHUTDOWN;
4307 }
4308 /* else these are the old supported methods */
4309 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004310 action = ST_ADM_ACTION_DISABLE;
4311 }
4312 else if (strcmp(value, "enable") == 0) {
4313 action = ST_ADM_ACTION_ENABLE;
4314 }
4315 else if (strcmp(value, "stop") == 0) {
4316 action = ST_ADM_ACTION_STOP;
4317 }
4318 else if (strcmp(value, "start") == 0) {
4319 action = ST_ADM_ACTION_START;
4320 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004321 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004322 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004323 goto out;
4324 }
4325 }
4326 else if (strcmp(key, "s") == 0) {
4327 if (!(px && action)) {
4328 /*
4329 * Indicates that we'll need to reprocess the parameters
4330 * as soon as backend and action are known
4331 */
4332 if (!reprocess) {
4333 st_cur_param = cur_param;
4334 st_next_param = next_param;
4335 }
4336 reprocess = 1;
4337 }
4338 else if ((sv = findserver(px, value)) != NULL) {
4339 switch (action) {
4340 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004341 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004342 altered_servers++;
4343 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004344 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004345 }
4346 break;
4347 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004348 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004349 altered_servers++;
4350 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004351 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004352 }
4353 break;
4354 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004355 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4356 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4357 altered_servers++;
4358 total_servers++;
4359 }
4360 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004361 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004362 if (sv->admin & SRV_ADMF_FDRAIN) {
4363 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4364 altered_servers++;
4365 total_servers++;
4366 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004367 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004368 case ST_ADM_ACTION_READY:
4369 srv_adm_set_ready(sv);
4370 altered_servers++;
4371 total_servers++;
4372 break;
4373 case ST_ADM_ACTION_DRAIN:
4374 srv_adm_set_drain(sv);
4375 altered_servers++;
4376 total_servers++;
4377 break;
4378 case ST_ADM_ACTION_MAINT:
4379 srv_adm_set_maint(sv);
4380 altered_servers++;
4381 total_servers++;
4382 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004383 case ST_ADM_ACTION_SHUTDOWN:
4384 if (px->state != PR_STSTOPPED) {
4385 struct session *sess, *sess_bck;
4386
4387 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4388 if (sess->srv_conn == sv)
4389 session_shutdown(sess, SN_ERR_KILLED);
4390
4391 altered_servers++;
4392 total_servers++;
4393 }
4394 break;
4395 }
4396 } else {
4397 /* the server name is unknown or ambiguous (duplicate names) */
4398 total_servers++;
4399 }
4400 }
4401 if (reprocess && px && action) {
4402 /* Now, we know the backend and the action chosen by the user.
4403 * We can safely restart from the first server parameter
4404 * to reprocess them
4405 */
4406 cur_param = st_cur_param;
4407 next_param = st_next_param;
4408 reprocess = 0;
4409 goto reprocess_servers;
4410 }
4411
4412 next_param = cur_param;
4413 }
4414 }
4415
4416 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004417 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004418 }
4419 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004420 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004421 }
4422 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004423 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004424 }
4425 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004426 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004427 }
4428 out:
4429 return 1;
4430}
4431
4432
4433static int stats_send_http_headers(struct stream_interface *si)
4434{
4435 struct session *s = session_from_task(si->owner);
4436 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004437 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004438
4439 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004440 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004441 "Cache-Control: no-cache\r\n"
4442 "Connection: close\r\n"
4443 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004444 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004445
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004446 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004447 chunk_appendf(&trash, "Refresh: %d\r\n",
4448 uri->refresh);
4449
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004450 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4451
4452 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4453 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4454 else
4455 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004456
4457 s->txn.status = 200;
4458 s->logs.tv_request = now;
4459
4460 if (bi_putchk(si->ib, &trash) == -1)
4461 return 0;
4462
4463 return 1;
4464}
4465
4466static int stats_send_http_redirect(struct stream_interface *si)
4467{
4468 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4469 struct session *s = session_from_task(si->owner);
4470 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004471 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004472
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004473 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004474 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004475 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004476 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004477 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4478 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004479 }
4480
4481 /* We don't want to land on the posted stats page because a refresh will
4482 * repost the data. We don't want this to happen on accident so we redirect
4483 * the browse to the stats page with a GET.
4484 */
4485 chunk_printf(&trash,
4486 "HTTP/1.1 303 See Other\r\n"
4487 "Cache-Control: no-cache\r\n"
4488 "Content-Type: text/plain\r\n"
4489 "Connection: close\r\n"
4490 "Location: %s;st=%s%s%s%s\r\n"
4491 "\r\n",
4492 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004493 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4494 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4495 stat_status_codes[appctx->ctx.stats.st_code]) ?
4496 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004497 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004498 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4499 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004500 scope_txt);
4501
4502 s->txn.status = 303;
4503 s->logs.tv_request = now;
4504
4505 if (bi_putchk(si->ib, &trash) == -1)
4506 return 0;
4507
4508 return 1;
4509}
4510
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004511/* This I/O handler runs as an applet embedded in a stream interface. It is
4512 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004513 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004514 * automatically unregisters itself once transfer is complete.
4515 */
4516static void http_stats_io_handler(struct stream_interface *si)
4517{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004518 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004519 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004520 struct channel *req = si->ob;
4521 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004522
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004523 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4524 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004525
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004526 /* check that the output is not closed */
4527 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004528 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004529
Willy Tarreau347a35d2013-11-22 17:51:09 +01004530 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004531 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004532 if (stats_send_http_headers(si)) {
4533 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004534 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004535 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004536 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004537 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004538 }
4539
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004540 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004541 unsigned int prev_len = si->ib->buf->i;
4542 unsigned int data_len;
4543 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004544 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004545
4546 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4547 /* One difficulty we're facing is that we must prevent
4548 * the input data from being automatically forwarded to
4549 * the output area. For this, we temporarily disable
4550 * forwarding on the channel.
4551 */
4552 last_fwd = si->ib->to_forward;
4553 si->ib->to_forward = 0;
4554 chunk_printf(&trash, "\r\n000000\r\n");
4555 if (bi_putchk(si->ib, &trash) == -1) {
4556 si->ib->to_forward = last_fwd;
4557 goto fail;
4558 }
4559 }
4560
4561 data_len = si->ib->buf->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004562 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004563 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004564
4565 last_len = si->ib->buf->i;
4566
4567 /* Now we must either adjust or remove the chunk size. This is
4568 * not easy because the chunk size might wrap at the end of the
4569 * buffer, so we pretend we have nothing in the buffer, we write
4570 * the size, then restore the buffer's contents. Note that we can
4571 * only do that because no forwarding is scheduled on the stats
4572 * applet.
4573 */
4574 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4575 si->ib->total -= (last_len - prev_len);
4576 si->ib->buf->i -= (last_len - prev_len);
4577
4578 if (last_len != data_len) {
4579 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
4580 bi_putchk(si->ib, &trash);
4581
4582 si->ib->total += (last_len - data_len);
4583 si->ib->buf->i += (last_len - data_len);
4584 }
4585 /* now re-enable forwarding */
4586 channel_forward(si->ib, last_fwd);
4587 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004588 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004589
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004590 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004591 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004592 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004593 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004594 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004595 }
4596
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004597 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004598 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004599 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004600 }
4601
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004602 if (appctx->st0 == STAT_HTTP_DONE) {
4603 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4604 chunk_printf(&trash, "\r\n0\r\n\r\n");
4605 if (bi_putchk(si->ib, &trash) == -1)
4606 goto fail;
4607 }
4608 /* eat the whole request */
4609 bo_skip(si->ob, si->ob->buf->o);
4610 res->flags |= CF_READ_NULL;
4611 si_shutr(si);
4612 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004613
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004614 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4615 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004616
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004617 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004618 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4619 si_shutr(si);
4620 res->flags |= CF_READ_NULL;
4621 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004622 }
Willy Tarreau91861262007-10-17 17:06:05 +02004623
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004624 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004625 /* update all other flags and resync with the other side */
4626 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004627
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004628 /* we don't want to expire timeouts while we're processing requests */
4629 si->ib->rex = TICK_ETERNITY;
4630 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004631
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004632 out:
4633 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4634 /* check that we have released everything then unregister */
4635 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004636 }
4637}
4638
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004639
Willy Tarreau909d5172012-11-26 03:04:41 +01004640static inline const char *get_conn_ctrl_name(const struct connection *conn)
4641{
Willy Tarreau3c728722014-01-23 13:50:42 +01004642 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004643 return "NONE";
4644 return conn->ctrl->name;
4645}
4646
4647static inline const char *get_conn_xprt_name(const struct connection *conn)
4648{
4649 static char ptr[17];
4650
Willy Tarreauaad69382014-01-23 14:21:42 +01004651 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004652 return "NONE";
4653
4654 if (conn->xprt == &raw_sock)
4655 return "RAW";
4656
4657#ifdef USE_OPENSSL
4658 if (conn->xprt == &ssl_sock)
4659 return "SSL";
4660#endif
4661 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4662 return ptr;
4663}
4664
4665static inline const char *get_conn_data_name(const struct connection *conn)
4666{
4667 static char ptr[17];
4668
4669 if (!conn->data)
4670 return "NONE";
4671
4672 if (conn->data == &sess_conn_cb)
4673 return "SESS";
4674
4675 if (conn->data == &si_conn_cb)
4676 return "STRM";
4677
4678 if (conn->data == &check_conn_cb)
4679 return "CHCK";
4680
4681 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4682 return ptr;
4683}
4684
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004685/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004686 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004687 * 0 if the output buffer is full and it needs to be called again, otherwise
4688 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004689 */
Willy Tarreau76153662012-11-26 01:16:39 +01004690static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004691{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004692 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004693 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004694 extern const char *monthname[12];
4695 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004696 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004697 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004698
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004699 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004700
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004701 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004702 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004703 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4704 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004705 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004706 appctx->ctx.sess.uid = 0;
4707 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004708 return 1;
4709 }
4710
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004711 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004712 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004713 appctx->ctx.sess.uid = sess->uniq_id;
4714 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004715 /* fall through */
4716
4717 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004718 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004719 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004720 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004721 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004722 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4723 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004724 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004725 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004726
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004727 conn = objt_conn(sess->si[0].end);
4728 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004729 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004730 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004731 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004732 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004733 break;
4734 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004735 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004736 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004737 default:
4738 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004739 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004740 break;
4741 }
4742
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004743 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004744 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004745 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004746
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004747 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004748 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004749 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4750 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4751 sess->listener ? sess->listener->luid : 0);
4752
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004753 if (conn)
4754 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004755
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004756 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004757 case AF_INET:
4758 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004759 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004760 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004761 break;
4762 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004763 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004764 break;
4765 default:
4766 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004767 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004768 break;
4769 }
4770
Willy Tarreau50943332011-09-02 17:33:05 +02004771 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004772 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004773 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004774 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004775 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4776 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004777 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004778
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004779 conn = objt_conn(sess->si[1].end);
4780 if (conn)
4781 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004782
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004783 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004784 case AF_INET:
4785 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004786 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004787 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004788 break;
4789 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004790 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004791 break;
4792 default:
4793 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004794 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004795 break;
4796 }
4797
4798 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004799 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004800 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004801 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4802 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004803 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004804 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004805
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004806 if (conn)
4807 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004808
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004809 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004810 case AF_INET:
4811 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004812 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004813 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004814 break;
4815 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004816 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004817 break;
4818 default:
4819 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004820 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004821 break;
4822 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004823
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004824 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004825 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004826 sess->task,
4827 sess->task->state,
4828 sess->task->nice, sess->task->calls,
4829 sess->task->expire ?
4830 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4831 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4832 TICKS_TO_MS(1000)) : "<NEVER>",
4833 task_in_rq(sess->task) ? ", running" : "");
4834
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004835 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004836 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004837 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4838
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004839 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004840 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4841 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4842 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4843
4844 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004845 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004846 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004847 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004848 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004849 obj_type_name(sess->si[0].end),
4850 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004851 sess->si[0].exp ?
4852 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4853 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4854 TICKS_TO_MS(1000)) : "<NEVER>",
4855 sess->si[0].err_type);
4856
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004857 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004858 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004859 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004860 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004861 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004862 obj_type_name(sess->si[1].end),
4863 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004864 sess->si[1].exp ?
4865 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4866 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4867 TICKS_TO_MS(1000)) : "<NEVER>",
4868 sess->si[1].err_type);
4869
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004870 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004871 chunk_appendf(&trash,
4872 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004873 conn,
4874 get_conn_ctrl_name(conn),
4875 get_conn_xprt_name(conn),
4876 get_conn_data_name(conn),
4877 obj_type_name(conn->target),
4878 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004879
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004880 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004881 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004882 conn->flags,
4883 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004884 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004885 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004886 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004887 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004888 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4889 chunk_appendf(&trash,
4890 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4891 tmpctx,
4892 tmpctx->st0,
4893 tmpctx->st1,
4894 tmpctx->st2,
4895 tmpctx->applet->name);
4896 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004897
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004898 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004899 chunk_appendf(&trash,
4900 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004901 conn,
4902 get_conn_ctrl_name(conn),
4903 get_conn_xprt_name(conn),
4904 get_conn_data_name(conn),
4905 obj_type_name(conn->target),
4906 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004907
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004908 chunk_appendf(&trash,
4909 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004910 conn->flags,
4911 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004912 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004913 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004914 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004915 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004916 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4917 chunk_appendf(&trash,
4918 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4919 tmpctx,
4920 tmpctx->st0,
4921 tmpctx->st1,
4922 tmpctx->st2,
4923 tmpctx->applet->name);
4924 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004925
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004926 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004927 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004928 " an_exp=%s",
4929 sess->req,
4930 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004931 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004932 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004933 sess->req->analyse_exp ?
4934 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4935 TICKS_TO_MS(1000)) : "<NEVER>");
4936
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004937 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004938 " rex=%s",
4939 sess->req->rex ?
4940 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4941 TICKS_TO_MS(1000)) : "<NEVER>");
4942
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004943 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004944 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004945 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004946 sess->req->wex ?
4947 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4948 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004949 sess->req->buf,
4950 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004951 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004952 sess->txn.req.next, sess->req->buf->i,
4953 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004954
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004955 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004956 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004957 " an_exp=%s",
4958 sess->rep,
4959 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004960 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004961 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004962 sess->rep->analyse_exp ?
4963 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4964 TICKS_TO_MS(1000)) : "<NEVER>");
4965
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004966 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004967 " rex=%s",
4968 sess->rep->rex ?
4969 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4970 TICKS_TO_MS(1000)) : "<NEVER>");
4971
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004972 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004973 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004974 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004975 sess->rep->wex ?
4976 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4977 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004978 sess->rep->buf,
4979 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004980 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004981 sess->txn.rsp.next, sess->rep->buf->i,
4982 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004983
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004984 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004985 return 0;
4986
4987 /* use other states to dump the contents */
4988 }
4989 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004990 appctx->ctx.sess.uid = 0;
4991 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004992 return 1;
4993}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004994
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004995static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004996{
4997 struct appctx *appctx = __objt_appctx(si->end);
4998
4999 switch (appctx->st2) {
5000 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005001 /* Display the column headers. If the message cannot be sent,
5002 * quit the fucntion with returning 0. The function is called
5003 * later and restart at the state "STAT_ST_INIT".
5004 */
5005 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005006 chunk_appendf(&trash, "# id (file) description\n");
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005007 if (bi_putchk(si->ib, &trash) == -1)
5008 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005009
5010 /* Now, we start the browsing of the references lists.
5011 * Note that the following call to LIST_ELEM return bad pointer. The only
5012 * avalaible field of this pointer is <list>. It is used with the function
5013 * pat_list_get_next() for retruning the first avalaible entry
5014 */
5015 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5016 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5017 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005018 appctx->st2 = STAT_ST_LIST;
5019 /* fall through */
5020
5021 case STAT_ST_LIST:
5022 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005023 chunk_reset(&trash);
5024
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005025 /* Build messages. If the reference is used by another category than
5026 * the listed categorie, display the information in the massage.
5027 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005028 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005029 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5030 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005031
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005032 if (bi_putchk(si->ib, &trash) == -1) {
5033 /* let's try again later from this session. We add ourselves into
5034 * this session's users so that it can remove us upon termination.
5035 */
5036 return 0;
5037 }
5038
5039 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005040 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5041 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005042 }
5043
5044 appctx->st2 = STAT_ST_FIN;
5045 /* fall through */
5046
5047 default:
5048 appctx->st2 = STAT_ST_FIN;
5049 return 1;
5050 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005051 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005052}
5053
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005054static int stats_map_lookup(struct stream_interface *si)
5055{
5056 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005057 struct sample sample;
5058 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005059 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005060
5061 switch (appctx->st2) {
5062 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005063 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005064 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005065 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005066 appctx->st2 = STAT_ST_LIST;
5067 /* fall through */
5068
5069 case STAT_ST_LIST:
5070 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005071 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005072 /* initialise chunk to build new message */
5073 chunk_reset(&trash);
5074
5075 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005076 sample.type = SMP_T_STR;
5077 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005078 sample.data.str.len = appctx->ctx.map.chunk.len;
5079 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005080 if (appctx->ctx.map.expr->pat_head->match &&
5081 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005082 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5083 else
5084 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005085
5086 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005087 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5088 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5089 break;
5090 if (match_method >= PAT_MATCH_NUM)
5091 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5092 else
5093 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005094
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005095 /* case sensitive */
5096 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5097 chunk_appendf(&trash, ", case=insensitive");
5098 else
5099 chunk_appendf(&trash, ", case=sensitive");
5100
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005101 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005102 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005103 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5104 chunk_appendf(&trash, ", found=no");
5105 else
5106 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005107 }
5108
5109 /* Display match and match info */
5110 else {
5111 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005112 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5113 chunk_appendf(&trash, ", found=yes");
5114 else
5115 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005116
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005117 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005118 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005119 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005120 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005121 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005122
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005123 /* display pattern */
5124 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5125 if (pat->ref && pat->ref->pattern)
5126 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5127 else
5128 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005129 }
5130 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005131 if (pat->ref && pat->ref->pattern)
5132 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5133 else
5134 chunk_appendf(&trash, ", pattern=unknown");
5135 }
5136
5137 /* display return value */
5138 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5139 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005140 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005141 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005142 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005143 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005144 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005145 }
5146
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005147 chunk_appendf(&trash, "\n");
5148
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005149 /* display response */
5150 if (bi_putchk(si->ib, &trash) == -1) {
5151 /* let's try again later from this session. We add ourselves into
5152 * this session's users so that it can remove us upon termination.
5153 */
5154 return 0;
5155 }
5156
5157 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005158 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5159 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005160 }
5161
5162 appctx->st2 = STAT_ST_FIN;
5163 /* fall through */
5164
5165 default:
5166 appctx->st2 = STAT_ST_FIN;
5167 free(appctx->ctx.map.chunk.str);
5168 return 1;
5169 }
5170}
5171
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005172static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005173{
5174 struct appctx *appctx = __objt_appctx(si->end);
5175
5176 switch (appctx->st2) {
5177
5178 case STAT_ST_INIT:
5179 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005180 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5181 struct pat_ref_elt *, list);
5182 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5183 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005184 appctx->st2 = STAT_ST_LIST;
5185 /* fall through */
5186
5187 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005188 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005189 chunk_reset(&trash);
5190
5191 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005192 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005193 chunk_appendf(&trash, "%p %s %s\n",
5194 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5195 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005196 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005197 chunk_appendf(&trash, "%p %s\n",
5198 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005199
5200 if (bi_putchk(si->ib, &trash) == -1) {
5201 /* let's try again later from this session. We add ourselves into
5202 * this session's users so that it can remove us upon termination.
5203 */
5204 return 0;
5205 }
5206
5207 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005208 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5209 struct pat_ref_elt *, list);
5210 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005211 break;
5212 }
5213
5214 appctx->st2 = STAT_ST_FIN;
5215 /* fall through */
5216
5217 default:
5218 appctx->st2 = STAT_ST_FIN;
5219 return 1;
5220 }
5221}
5222
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005223/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005224 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005225 * to be called again, otherwise non-zero. It is designed to be called
5226 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005227 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005228static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005229{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005230 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005231 struct connection *conn;
5232
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005233 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005234 /* If we're forced to shut down, we might have to remove our
5235 * reference to the last session being dumped.
5236 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005237 if (appctx->st2 == STAT_ST_LIST) {
5238 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5239 LIST_DEL(&appctx->ctx.sess.bref.users);
5240 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005241 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005242 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005243 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005244 }
5245
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005246 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005247
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005248 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005249 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005250 /* the function had not been called yet, let's prepare the
5251 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005252 * pointer to the first in the global list. When a target
5253 * session is being destroyed, it is responsible for updating
5254 * this pointer. We know we have reached the end when this
5255 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005256 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005257 LIST_INIT(&appctx->ctx.sess.bref.users);
5258 appctx->ctx.sess.bref.ref = sessions.n;
5259 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005260 /* fall through */
5261
Willy Tarreau295a8372011-03-10 11:25:07 +01005262 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005263 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005264 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5265 LIST_DEL(&appctx->ctx.sess.bref.users);
5266 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005267 }
5268
5269 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005270 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005271 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005272 struct session *curr_sess;
5273
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005274 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005275
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005276 if (appctx->ctx.sess.target) {
5277 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005278 goto next_sess;
5279
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005280 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005281 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005282 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005283 return 0;
5284
5285 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005286 LIST_DEL(&appctx->ctx.sess.bref.users);
5287 LIST_INIT(&appctx->ctx.sess.bref.users);
5288 if (appctx->ctx.sess.target != (void *)-1) {
5289 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005290 break;
5291 }
5292 else
5293 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005294 }
5295
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005296 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005297 "%p: proto=%s",
5298 curr_sess,
5299 curr_sess->listener->proto->name);
5300
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005301
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005302 conn = objt_conn(curr_sess->si[0].end);
5303 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005304 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005305 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005306 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005307 " src=%s:%d fe=%s be=%s srv=%s",
5308 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005309 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005310 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005311 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005312 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005313 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005314 break;
5315 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005316 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005317 " src=unix:%d fe=%s be=%s srv=%s",
5318 curr_sess->listener->luid,
5319 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005320 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005321 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005322 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005323 break;
5324 }
5325
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005326 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005327 " ts=%02x age=%s calls=%d",
5328 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005329 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5330 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005331
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005332 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005333 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005334 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005335 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005336 curr_sess->req->analysers,
5337 curr_sess->req->rex ?
5338 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5339 TICKS_TO_MS(1000)) : "");
5340
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005341 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005342 ",wx=%s",
5343 curr_sess->req->wex ?
5344 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5345 TICKS_TO_MS(1000)) : "");
5346
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005347 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005348 ",ax=%s]",
5349 curr_sess->req->analyse_exp ?
5350 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5351 TICKS_TO_MS(1000)) : "");
5352
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005353 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005354 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005355 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005356 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005357 curr_sess->rep->analysers,
5358 curr_sess->rep->rex ?
5359 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5360 TICKS_TO_MS(1000)) : "");
5361
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005362 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005363 ",wx=%s",
5364 curr_sess->rep->wex ?
5365 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5366 TICKS_TO_MS(1000)) : "");
5367
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005368 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005369 ",ax=%s]",
5370 curr_sess->rep->analyse_exp ?
5371 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5372 TICKS_TO_MS(1000)) : "");
5373
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005374 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005375 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005376 " s0=[%d,%1xh,fd=%d,ex=%s]",
5377 curr_sess->si[0].state,
5378 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005379 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005380 curr_sess->si[0].exp ?
5381 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5382 TICKS_TO_MS(1000)) : "");
5383
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005384 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005385 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005386 " s1=[%d,%1xh,fd=%d,ex=%s]",
5387 curr_sess->si[1].state,
5388 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005389 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005390 curr_sess->si[1].exp ?
5391 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5392 TICKS_TO_MS(1000)) : "");
5393
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005394 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005395 " exp=%s",
5396 curr_sess->task->expire ?
5397 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5398 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005399 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005400 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005401
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005402 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005403
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005404 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005405 /* let's try again later from this session. We add ourselves into
5406 * this session's users so that it can remove us upon termination.
5407 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005408 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005409 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005410 }
5411
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005412 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005413 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005414 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005415
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005416 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005417 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005418 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005419 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005420 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005421 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005422
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005423 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005424 return 0;
5425
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005426 appctx->ctx.sess.target = NULL;
5427 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005428 return 1;
5429 }
5430
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005431 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005432 /* fall through */
5433
5434 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005435 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005436 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005437 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005438}
5439
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005440/* This is called when the stream interface is closed. For instance, upon an
5441 * external abort, we won't call the i/o handler anymore so we may need to
5442 * remove back references to the session currently being dumped.
5443 */
Simon Horman74d88312013-02-12 10:45:50 +09005444static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005445{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005446 struct appctx *appctx = __objt_appctx(si->end);
5447
5448 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5449 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5450 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005451 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005452 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5453 free(appctx->ctx.cli.err);
5454 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005455 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5456 free(appctx->ctx.map.chunk.str);
5457 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005458}
5459
Willy Tarreau20e99322013-04-13 09:22:25 +02005460/* This function is used to either dump tables states (when action is set
5461 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005462 * It returns 0 if the output buffer is full and it needs to be called
5463 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005464 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005465static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005466{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005467 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005468 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005469 struct ebmb_node *eb;
5470 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005471 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005472 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005473
5474 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005475 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005476 * - STAT_ST_INIT : the first call
5477 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005478 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005479 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005480 * and the entry pointer points to the next entry to be dumped,
5481 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005482 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005483 * data though.
5484 */
5485
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005486 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005487 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005488 if (appctx->st2 == STAT_ST_LIST) {
5489 appctx->ctx.table.entry->ref_cnt--;
5490 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005491 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005492 return 1;
5493 }
5494
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005495 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005496
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005497 while (appctx->st2 != STAT_ST_FIN) {
5498 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005499 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005500 appctx->ctx.table.proxy = appctx->ctx.table.target;
5501 if (!appctx->ctx.table.proxy)
5502 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005503
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005504 appctx->ctx.table.entry = NULL;
5505 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005506 break;
5507
Willy Tarreau295a8372011-03-10 11:25:07 +01005508 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005509 if (!appctx->ctx.table.proxy ||
5510 (appctx->ctx.table.target &&
5511 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5512 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005513 break;
5514 }
5515
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005516 if (appctx->ctx.table.proxy->table.size) {
5517 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5518 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005519 return 0;
5520
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005521 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005522 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005523 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005524 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005525 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005526 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5527 appctx->ctx.table.entry->ref_cnt++;
5528 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005529 break;
5530 }
5531 }
5532 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005533 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005534 break;
5535
Willy Tarreau295a8372011-03-10 11:25:07 +01005536 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005537 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005538
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005539 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005540 /* we're filtering on some data contents */
5541 void *ptr;
5542 long long data;
5543
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005544 dt = appctx->ctx.table.data_type;
5545 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5546 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005547 dt);
5548
5549 data = 0;
5550 switch (stktable_data_types[dt].std_type) {
5551 case STD_T_SINT:
5552 data = stktable_data_cast(ptr, std_t_sint);
5553 break;
5554 case STD_T_UINT:
5555 data = stktable_data_cast(ptr, std_t_uint);
5556 break;
5557 case STD_T_ULL:
5558 data = stktable_data_cast(ptr, std_t_ull);
5559 break;
5560 case STD_T_FRQP:
5561 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005562 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005563 break;
5564 }
5565
5566 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005567 if ((data < appctx->ctx.table.value &&
5568 (appctx->ctx.table.data_op == STD_OP_EQ ||
5569 appctx->ctx.table.data_op == STD_OP_GT ||
5570 appctx->ctx.table.data_op == STD_OP_GE)) ||
5571 (data == appctx->ctx.table.value &&
5572 (appctx->ctx.table.data_op == STD_OP_NE ||
5573 appctx->ctx.table.data_op == STD_OP_GT ||
5574 appctx->ctx.table.data_op == STD_OP_LT)) ||
5575 (data > appctx->ctx.table.value &&
5576 (appctx->ctx.table.data_op == STD_OP_EQ ||
5577 appctx->ctx.table.data_op == STD_OP_LT ||
5578 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005579 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005580 }
5581
Simon Hormanc88b8872011-06-15 15:18:49 +09005582 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005583 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5584 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005585 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005587 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005588
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005589 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005590 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005591 struct stksess *old = appctx->ctx.table.entry;
5592 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005593 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005594 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5595 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5596 stksess_kill(&appctx->ctx.table.proxy->table, old);
5597 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005598 break;
5599 }
5600
Simon Hormanc88b8872011-06-15 15:18:49 +09005601
5602 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005603 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5604 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5605 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005606
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005607 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5608 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005609 break;
5610
Willy Tarreau295a8372011-03-10 11:25:07 +01005611 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005612 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005613 break;
5614 }
5615 }
5616 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005617}
5618
Willy Tarreaud426a182010-03-05 14:58:26 +01005619/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005620 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5621 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5622 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5623 * lines are respected within the limit of 70 output chars. Lines that are
5624 * continuation of a previous truncated line begin with "+" instead of " "
5625 * after the offset. The new pointer is returned.
5626 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005627static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5628 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005629{
5630 int end;
5631 unsigned char c;
5632
5633 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005634 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005635 return ptr;
5636
Willy Tarreau77804732012-10-29 16:14:26 +01005637 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005638
Willy Tarreaud426a182010-03-05 14:58:26 +01005639 while (ptr < len && ptr < bsize) {
5640 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005641 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005642 if (out->len > end - 2)
5643 break;
5644 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005645 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005646 if (out->len > end - 3)
5647 break;
5648 out->str[out->len++] = '\\';
5649 switch (c) {
5650 case '\t': c = 't'; break;
5651 case '\n': c = 'n'; break;
5652 case '\r': c = 'r'; break;
5653 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005654 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005655 }
5656 out->str[out->len++] = c;
5657 } else {
5658 if (out->len > end - 5)
5659 break;
5660 out->str[out->len++] = '\\';
5661 out->str[out->len++] = 'x';
5662 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5663 out->str[out->len++] = hextab[c & 0xF];
5664 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005665 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005666 /* we had a line break, let's return now */
5667 out->str[out->len++] = '\n';
5668 *line = ptr;
5669 return ptr;
5670 }
5671 }
5672 /* we have an incomplete line, we return it as-is */
5673 out->str[out->len++] = '\n';
5674 return ptr;
5675}
5676
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005677/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005678 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005679 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005680 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005681static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005682{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005683 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005684 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005685
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005686 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005687 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005688
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005689 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005690
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005691 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005692 /* the function had not been called yet, let's prepare the
5693 * buffer for a response.
5694 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005695 struct tm tm;
5696
5697 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005698 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005699 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5700 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5701 error_snapshot_id);
5702
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005703 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005704 /* Socket buffer full. Let's try again later from the same point */
5705 return 0;
5706 }
5707
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005708 appctx->ctx.errors.px = proxy;
5709 appctx->ctx.errors.buf = 0;
5710 appctx->ctx.errors.bol = 0;
5711 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005712 }
5713
5714 /* we have two inner loops here, one for the proxy, the other one for
5715 * the buffer.
5716 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005717 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005718 struct error_snapshot *es;
5719
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005720 if (appctx->ctx.errors.buf == 0)
5721 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005722 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005723 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005724
5725 if (!es->when.tv_sec)
5726 goto next;
5727
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005728 if (appctx->ctx.errors.iid >= 0 &&
5729 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5730 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005731 goto next;
5732
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005733 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005734 /* just print headers now */
5735
5736 char pn[INET6_ADDRSTRLEN];
5737 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005738 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005739
5740 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005741 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005742 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005743 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005744
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005745 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5746 case AF_INET:
5747 case AF_INET6:
5748 port = get_host_port(&es->src);
5749 break;
5750 default:
5751 port = 0;
5752 }
5753
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005754 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005755 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005756 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005757 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005758 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005759 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005760 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5761 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005762 break;
5763 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005764 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005765 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005766 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005767 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005768 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005769 break;
5770 }
5771
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005772 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005773 ", server %s (#%d), event #%u\n"
5774 " src %s:%d, session #%d, session flags 0x%08x\n"
5775 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5776 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5777 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5778 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5779 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5780 es->ev_id,
5781 pn, port, es->sid, es->s_flags,
5782 es->state, es->m_flags, es->t_flags,
5783 es->m_clen, es->m_blen,
5784 es->b_flags, es->b_out, es->b_tot,
5785 es->len, es->b_wrap, es->pos);
5786
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005787 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005788 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005789 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005790 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005791 appctx->ctx.errors.ptr = 0;
5792 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005793 }
5794
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005795 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005796 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005797 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005798 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005799 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005800 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005801 goto next;
5802 }
5803
5804 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005805 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005806 int newptr;
5807 int newline;
5808
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005809 newline = appctx->ctx.errors.bol;
5810 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5811 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005812 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005813
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005814 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005815 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005816 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005817 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005818 appctx->ctx.errors.ptr = newptr;
5819 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005820 };
5821 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005822 appctx->ctx.errors.bol = 0;
5823 appctx->ctx.errors.ptr = -1;
5824 appctx->ctx.errors.buf++;
5825 if (appctx->ctx.errors.buf > 1) {
5826 appctx->ctx.errors.buf = 0;
5827 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005828 }
5829 }
5830
5831 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005832 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005833}
5834
Willy Tarreaud5781202012-09-22 19:32:35 +02005835/* parse the "level" argument on the bind lines */
5836static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5837{
5838 if (!*args[cur_arg + 1]) {
5839 memprintf(err, "'%s' : missing level", args[cur_arg]);
5840 return ERR_ALERT | ERR_FATAL;
5841 }
5842
5843 if (!strcmp(args[cur_arg+1], "user"))
5844 conf->level = ACCESS_LVL_USER;
5845 else if (!strcmp(args[cur_arg+1], "operator"))
5846 conf->level = ACCESS_LVL_OPER;
5847 else if (!strcmp(args[cur_arg+1], "admin"))
5848 conf->level = ACCESS_LVL_ADMIN;
5849 else {
5850 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5851 args[cur_arg], args[cur_arg+1]);
5852 return ERR_ALERT | ERR_FATAL;
5853 }
5854
5855 return 0;
5856}
5857
Willy Tarreaub24281b2011-02-13 13:16:36 +01005858struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005859 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005860 .name = "<STATS>", /* used for logging */
5861 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005862 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005863};
5864
Simon Horman9bd2c732011-06-15 15:18:44 +09005865static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005866 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005867 .name = "<CLI>", /* used for logging */
5868 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005869 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005870};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005871
Willy Tarreaudc13c112013-06-21 23:16:39 +02005872static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005873 { CFG_GLOBAL, "stats", stats_parse_global },
5874 { 0, NULL, NULL },
5875}};
5876
Willy Tarreaud5781202012-09-22 19:32:35 +02005877static struct bind_kw_list bind_kws = { "STAT", { }, {
5878 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5879 { NULL, NULL, 0 },
5880}};
5881
Willy Tarreau10522fd2008-07-09 20:12:41 +02005882__attribute__((constructor))
5883static void __dumpstats_module_init(void)
5884{
5885 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005886 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005887}
5888
Willy Tarreau91861262007-10-17 17:06:05 +02005889/*
5890 * Local variables:
5891 * c-indent-level: 8
5892 * c-basic-offset: 8
5893 * End:
5894 */