blob: 4e4b657c9a5ddc3018b5c774af8443bc5a4c42b9 [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 Tarreau654694e2012-06-07 01:03:16 +0200160 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100161 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200162 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200163 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200164 " disable : put a server or frontend in maintenance mode\n"
165 " enable : re-enable a server or frontend which is in maintenance mode\n"
166 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100167 " show acl [id] : report avalaible acls or dump an acl's contents\n"
168 " get acl : reports the patterns matching a sample for an ACL\n"
169 " add acl : add acl entry\n"
170 " del acl : delete acl entry\n"
171 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100172 " show map [id] : report avalaible maps or dump a map's contents\n"
173 " get map : reports the keys and values matching a sample for a map\n"
174 " set map : modify map entry\n"
175 " add map : add map entry\n"
176 " del map : delete map entry\n"
177 " clear map <id> : clear the content of this map\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200178 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200179
Simon Horman9bd2c732011-06-15 15:18:44 +0900180static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200181 "Permission denied\n"
182 "";
183
Willy Tarreau295a8372011-03-10 11:25:07 +0100184/* data transmission states for the stats responses */
185enum {
186 STAT_ST_INIT = 0,
187 STAT_ST_HEAD,
188 STAT_ST_INFO,
189 STAT_ST_LIST,
190 STAT_ST_END,
191 STAT_ST_FIN,
192};
193
194/* data transmission states for the stats responses inside a proxy */
195enum {
196 STAT_PX_ST_INIT = 0,
197 STAT_PX_ST_TH,
198 STAT_PX_ST_FE,
199 STAT_PX_ST_LI,
200 STAT_PX_ST_SV,
201 STAT_PX_ST_BE,
202 STAT_PX_ST_END,
203 STAT_PX_ST_FIN,
204};
205
Cyril Bonté19979e12012-04-04 12:57:21 +0200206extern const char *stat_status_codes[];
207
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200208/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200209 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200210 * needs to be closed and ignored, or a negative value upon critical failure.
211 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900212static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200213{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100214 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100215 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200216
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200217 tv_zero(&s->logs.tv_request);
218 s->logs.t_queue = 0;
219 s->logs.t_connect = 0;
220 s->logs.t_data = 0;
221 s->logs.t_close = 0;
222 s->logs.bytes_in = s->logs.bytes_out = 0;
223 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
224 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200225
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200226 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200227
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200228 if (s->listener->timeout) {
229 s->req->rto = *s->listener->timeout;
230 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200231 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200232 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200233}
234
Willy Tarreau07e9e642010-08-17 21:48:17 +0200235/* allocate a new stats frontend named <name>, and return it
236 * (or NULL in case of lack of memory).
237 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200238static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200239{
240 struct proxy *fe;
241
242 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
243 if (!fe)
244 return NULL;
245
Willy Tarreau237250c2011-07-29 01:49:03 +0200246 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200247 fe->next = proxy;
248 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200249 fe->last_change = now.tv_sec;
250 fe->id = strdup("GLOBAL");
251 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200252 fe->maxconn = 10; /* default to 10 concurrent connections */
253 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200254 fe->conf.file = strdup(file);
255 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200256 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200257
258 /* the stats frontend is the only one able to assign ID #0 */
259 fe->conf.id.key = fe->uuid = 0;
260 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200261 return fe;
262}
263
Willy Tarreaufbee7132007-10-18 13:53:22 +0200264/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200265 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
266 * error message into the <err> buffer which will be preallocated. The trailing
267 * '\n' must not be written. The function must be called with <args> pointing to
268 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200269 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200270static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200271 struct proxy *defpx, const char *file, int line,
272 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200273{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200274 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200275 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200276
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200277 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278 int cur_arg;
279
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200280 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200281 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 +0200282 return -1;
283 }
284
Willy Tarreau89a63132009-08-16 17:41:45 +0200285 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200286 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200288 return -1;
289 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200290 }
291
Willy Tarreau4348fad2012-09-20 16:48:07 +0200292 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200293 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200294
Willy Tarreauc53d4222012-09-20 20:19:28 +0200295 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
296 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
297 file, line, args[0], args[1], err && *err ? *err : "error");
298 return -1;
299 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200300
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200301 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200302 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200303 static int bind_dumped;
304 struct bind_kw *kw;
305
306 kw = bind_find_kw(args[cur_arg]);
307 if (kw) {
308 if (!kw->parse) {
309 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
310 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200311 return -1;
312 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200313
314 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
315 if (err && *err)
316 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
317 else
318 memprintf(err, "'%s %s' : error encountered while processing '%s'",
319 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200320 return -1;
321 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200322
323 cur_arg += 1 + kw->skip;
324 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200325 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200326
327 if (!bind_dumped) {
328 bind_dump_kws(err);
329 indent_msg(err, 4);
330 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200331 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200332
333 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
334 args[0], args[1], args[cur_arg],
335 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
336 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200337 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100338
Willy Tarreauc53d4222012-09-20 20:19:28 +0200339 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
340 l->maxconn = global.stats_fe->maxconn;
341 l->backlog = global.stats_fe->backlog;
342 l->timeout = &global.stats_fe->timeout.client;
343 l->accept = session_accept;
344 l->handler = process_session;
345 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
346 l->nice = -64; /* we want to boost priority for local stats */
347 global.maxsock += l->maxconn;
348 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200349 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200350 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100351 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200352 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100353
354 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200355 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100356 return -1;
357 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200358
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100359 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200360 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200361 return -1;
362 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200363 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200364 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200365 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200366 return -1;
367 }
368 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200369 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200370 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200371 else if (!strcmp(args[1], "maxconn")) {
372 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200373
374 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200375 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200376 return -1;
377 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200378
379 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200380 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200381 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200382 return -1;
383 }
384 }
385 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200386 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200387 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
388 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100389 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200390
Willy Tarreau91319572013-04-20 09:48:50 +0200391 if (!global.stats_fe) {
392 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
393 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
394 return -1;
395 }
396 }
397
Willy Tarreau35b7b162012-10-22 23:17:18 +0200398 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100399 unsigned int low, high;
400
Willy Tarreau35b7b162012-10-22 23:17:18 +0200401 if (strcmp(args[cur_arg], "all") == 0) {
402 set = 0;
403 break;
404 }
405 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100406 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200407 }
408 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100409 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200410 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100411 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100412 char *dash = strchr(args[cur_arg], '-');
413
414 low = high = str2uic(args[cur_arg]);
415 if (dash)
416 high = str2uic(dash + 1);
417
418 if (high < low) {
419 unsigned int swap = low;
420 low = high;
421 high = swap;
422 }
423
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100424 if (low < 1 || high > LONGBITS) {
425 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
426 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200427 return -1;
428 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100429 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100430 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100431 }
432 else {
433 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100434 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
435 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100436 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200437 }
438 cur_arg++;
439 }
440 global.stats_fe->bind_proc = set;
441 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200442 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200443 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200444 return -1;
445 }
446 return 0;
447}
448
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100449/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
450 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100451 * NOTE: Some tools happen to rely on the field position instead of its name,
452 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100453 */
454static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100455{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100456 chunk_appendf(&trash,
457 "# pxname,svname,"
458 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100459 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100460 "bin,bout,"
461 "dreq,dresp,"
462 "ereq,econ,eresp,"
463 "wretr,wredis,"
464 "status,weight,act,bck,"
465 "chkfail,chkdown,lastchg,downtime,qlimit,"
466 "pid,iid,sid,throttle,lbtot,tracked,type,"
467 "rate,rate_lim,rate_max,"
468 "check_status,check_code,check_duration,"
469 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
470 "req_rate,req_rate_max,req_tot,"
471 "cli_abrt,srv_abrt,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100472 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100473 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100474}
475
Simon Hormand9366582011-06-15 15:18:45 +0900476/* print a string of text buffer to <out>. The format is :
477 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
478 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
479 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
480 */
481static int dump_text(struct chunk *out, const char *buf, int bsize)
482{
483 unsigned char c;
484 int ptr = 0;
485
486 while (buf[ptr] && ptr < bsize) {
487 c = buf[ptr];
488 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
489 if (out->len > out->size - 1)
490 break;
491 out->str[out->len++] = c;
492 }
493 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
494 if (out->len > out->size - 2)
495 break;
496 out->str[out->len++] = '\\';
497 switch (c) {
498 case ' ': c = ' '; break;
499 case '\t': c = 't'; break;
500 case '\n': c = 'n'; break;
501 case '\r': c = 'r'; break;
502 case '\e': c = 'e'; break;
503 case '\\': c = '\\'; break;
504 }
505 out->str[out->len++] = c;
506 }
507 else {
508 if (out->len > out->size - 4)
509 break;
510 out->str[out->len++] = '\\';
511 out->str[out->len++] = 'x';
512 out->str[out->len++] = hextab[(c >> 4) & 0xF];
513 out->str[out->len++] = hextab[c & 0xF];
514 }
515 ptr++;
516 }
517
518 return ptr;
519}
520
521/* print a buffer in hexa.
522 * Print stopped if <bsize> is reached, or if no more place in the chunk.
523 */
524static int dump_binary(struct chunk *out, const char *buf, int bsize)
525{
526 unsigned char c;
527 int ptr = 0;
528
529 while (ptr < bsize) {
530 c = buf[ptr];
531
532 if (out->len > out->size - 2)
533 break;
534 out->str[out->len++] = hextab[(c >> 4) & 0xF];
535 out->str[out->len++] = hextab[c & 0xF];
536
537 ptr++;
538 }
539 return ptr;
540}
541
542/* Dump the status of a table to a stream interface's
543 * read buffer. It returns 0 if the output buffer is full
544 * and needs to be called again, otherwise non-zero.
545 */
546static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
547 struct proxy *proxy, struct proxy *target)
548{
Willy Tarreau306f8302013-07-08 15:53:06 +0200549 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900550
Willy Tarreau77804732012-10-29 16:14:26 +0100551 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900552 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
553
554 /* any other information should be dumped here */
555
Willy Tarreau290e63a2012-09-20 18:07:14 +0200556 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100557 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900558
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200559 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900560 return 0;
561
562 return 1;
563}
564
565/* Dump the a table entry to a stream interface's
566 * read buffer. It returns 0 if the output buffer is full
567 * and needs to be called again, otherwise non-zero.
568 */
569static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
570 struct proxy *proxy, struct stksess *entry)
571{
572 int dt;
573
Willy Tarreau77804732012-10-29 16:14:26 +0100574 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900575
576 if (proxy->table.type == STKTABLE_TYPE_IP) {
577 char addr[INET_ADDRSTRLEN];
578 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100579 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900580 }
581 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
582 char addr[INET6_ADDRSTRLEN];
583 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100584 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900585 }
586 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100587 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900588 }
589 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100590 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900591 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
592 }
593 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100594 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900595 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
596 }
597
Willy Tarreau77804732012-10-29 16:14:26 +0100598 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900599
600 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
601 void *ptr;
602
603 if (proxy->table.data_ofs[dt] == 0)
604 continue;
605 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100606 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900607 else
Willy Tarreau77804732012-10-29 16:14:26 +0100608 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900609
610 ptr = stktable_data_ptr(&proxy->table, entry, dt);
611 switch (stktable_data_types[dt].std_type) {
612 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100613 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900614 break;
615 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100616 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900617 break;
618 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100619 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900620 break;
621 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100622 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900623 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
624 proxy->table.data_arg[dt].u));
625 break;
626 }
627 }
Willy Tarreau77804732012-10-29 16:14:26 +0100628 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900629
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200630 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900631 return 0;
632
633 return 1;
634}
635
Willy Tarreaudec98142012-06-06 23:37:08 +0200636static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900637{
Willy Tarreau306f8302013-07-08 15:53:06 +0200638 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100639 struct appctx *appctx = __objt_appctx(si->end);
640 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900641 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900642 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900643 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200644 long long value;
645 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200646 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200647 void *ptr;
648 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900649
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100650 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900651
652 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100653 appctx->ctx.cli.msg = "Key value expected\n";
654 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900655 return;
656 }
657
Simon Hormanc5b89f62011-06-15 15:18:50 +0900658 switch (px->table.type) {
659 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900660 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100661 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900662 break;
663 case STKTABLE_TYPE_IPV6:
664 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100665 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900666 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900667 case STKTABLE_TYPE_INTEGER:
668 {
669 char *endptr;
670 unsigned long val;
671 errno = 0;
672 val = strtoul(args[4], &endptr, 10);
673 if ((errno == ERANGE && val == ULONG_MAX) ||
674 (errno != 0 && val == 0) || endptr == args[4] ||
675 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100676 appctx->ctx.cli.msg = "Invalid key\n";
677 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900678 return;
679 }
680 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100681 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900682 break;
683 }
684 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900685 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100686 static_table_key->key = args[4];
687 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900688 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900689 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200690 switch (action) {
691 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100692 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 +0200693 break;
694 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100695 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 +0200696 break;
697 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100698 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200699 break;
700 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100701 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900702 return;
703 }
704
705 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200706 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100707 appctx->ctx.cli.msg = stats_permission_denied_msg;
708 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900709 return;
710 }
711
Willy Tarreau07115412012-10-29 21:56:59 +0100712 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900713
Willy Tarreaudec98142012-06-06 23:37:08 +0200714 switch (action) {
715 case STAT_CLI_O_TAB:
716 if (!ts)
717 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100718 chunk_reset(&trash);
719 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900720 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100721 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900722 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200723
724 case STAT_CLI_O_CLR:
725 if (!ts)
726 return;
727 if (ts->ref_cnt) {
728 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100729 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
730 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200731 return;
732 }
733 stksess_kill(&px->table, ts);
734 break;
Simon Horman17bce342011-06-15 15:18:47 +0900735
Willy Tarreau654694e2012-06-07 01:03:16 +0200736 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200737 if (ts)
738 stktable_touch(&px->table, ts, 1);
739 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100740 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200741 if (!ts) {
742 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100743 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
744 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200745 return;
746 }
747 stktable_store(&px->table, ts, 1);
748 }
749
Willy Tarreau47060b62013-08-01 21:11:42 +0200750 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
751 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100752 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
753 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200754 return;
755 }
756
757 data_type = stktable_get_data_type(args[cur_arg] + 5);
758 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100759 appctx->ctx.cli.msg = "Unknown data type\n";
760 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200761 return;
762 }
763
764 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100765 appctx->ctx.cli.msg = "Data type not stored in this table\n";
766 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200767 return;
768 }
769
770 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100771 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
772 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200773 return;
774 }
775
776 ptr = stktable_data_ptr(&px->table, ts, data_type);
777
778 switch (stktable_data_types[data_type].std_type) {
779 case STD_T_SINT:
780 stktable_data_cast(ptr, std_t_sint) = value;
781 break;
782 case STD_T_UINT:
783 stktable_data_cast(ptr, std_t_uint) = value;
784 break;
785 case STD_T_ULL:
786 stktable_data_cast(ptr, std_t_ull) = value;
787 break;
788 case STD_T_FRQP:
789 /* We set both the current and previous values. That way
790 * the reported frequency is stable during all the period
791 * then slowly fades out. This allows external tools to
792 * push measures without having to update them too often.
793 */
794 frqp = &stktable_data_cast(ptr, std_t_frqp);
795 frqp->curr_tick = now_ms;
796 frqp->prev_ctr = 0;
797 frqp->curr_ctr = value;
798 break;
799 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200800 }
801 break;
802
Willy Tarreaudec98142012-06-06 23:37:08 +0200803 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100804 appctx->ctx.cli.msg = "Unknown action\n";
805 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200806 break;
Simon Horman121f3052011-06-15 15:18:46 +0900807 }
Simon Horman121f3052011-06-15 15:18:46 +0900808}
809
Willy Tarreau654694e2012-06-07 01:03:16 +0200810static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900811{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100812 struct appctx *appctx = __objt_appctx(si->end);
813
Willy Tarreau04b3a192013-04-13 09:41:37 +0200814 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100815 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
816 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200817 return;
818 }
819
Simon Hormand5b9fd92011-06-15 15:18:48 +0900820 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100821 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
822 if (appctx->ctx.table.data_type < 0) {
823 appctx->ctx.cli.msg = "Unknown data type\n";
824 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900825 return;
826 }
827
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100828 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
829 appctx->ctx.cli.msg = "Data type not stored in this table\n";
830 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900831 return;
832 }
833
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100834 appctx->ctx.table.data_op = get_std_op(args[4]);
835 if (appctx->ctx.table.data_op < 0) {
836 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
837 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900838 return;
839 }
840
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100841 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
842 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
843 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900844 return;
845 }
846}
847
Willy Tarreaudec98142012-06-06 23:37:08 +0200848static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900849{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100850 struct appctx *appctx = __objt_appctx(si->end);
851
852 appctx->ctx.table.data_type = -1;
853 appctx->st2 = STAT_ST_INIT;
854 appctx->ctx.table.target = NULL;
855 appctx->ctx.table.proxy = NULL;
856 appctx->ctx.table.entry = NULL;
857 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900858
859 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100860 appctx->ctx.table.target = find_stktable(args[2]);
861 if (!appctx->ctx.table.target) {
862 appctx->ctx.cli.msg = "No such table\n";
863 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900864 return;
865 }
866 }
867 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200868 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900869 goto err_args;
870 return;
871 }
872
873 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200874 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900875 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200876 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900877 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900878 goto err_args;
879
880 return;
881
882err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200883 switch (action) {
884 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100885 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 +0200886 break;
887 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100888 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 +0200889 break;
890 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100891 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200892 break;
893 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100894 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900895}
896
Willy Tarreau532a4502011-09-07 22:37:44 +0200897/* Expects to find a frontend named <arg> and returns it, otherwise displays various
898 * adequate error messages and returns NULL. This function also expects the session
899 * level to be admin.
900 */
901static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
902{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100903 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200904 struct proxy *px;
905
Willy Tarreau290e63a2012-09-20 18:07:14 +0200906 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100907 appctx->ctx.cli.msg = stats_permission_denied_msg;
908 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200909 return NULL;
910 }
911
912 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100913 appctx->ctx.cli.msg = "A frontend name is expected.\n";
914 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200915 return NULL;
916 }
917
918 px = findproxy(arg, PR_CAP_FE);
919 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100920 appctx->ctx.cli.msg = "No such frontend.\n";
921 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200922 return NULL;
923 }
924 return px;
925}
926
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200927/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
928 * and returns the pointer to the server. Otherwise, display adequate error messages
929 * and returns NULL. This function also expects the session level to be admin. Note:
930 * the <arg> is modified to remove the '/'.
931 */
932static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
933{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100934 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200935 struct proxy *px;
936 struct server *sv;
937 char *line;
938
Willy Tarreau290e63a2012-09-20 18:07:14 +0200939 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100940 appctx->ctx.cli.msg = stats_permission_denied_msg;
941 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200942 return NULL;
943 }
944
945 /* split "backend/server" and make <line> point to server */
946 for (line = arg; *line; line++)
947 if (*line == '/') {
948 *line++ = '\0';
949 break;
950 }
951
952 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100953 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
954 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200955 return NULL;
956 }
957
958 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100959 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
960 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200961 return NULL;
962 }
963
964 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100965 appctx->ctx.cli.msg = "Proxy is disabled.\n";
966 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200967 return NULL;
968 }
969
970 return sv;
971}
972
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100973/* This function is used with map and acl management. It permits to browse
974 * each reference. The variable <getnext> must contain the current node,
975 * <end> point to the root node and the <flags> permit to filter required
976 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100977 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100978static inline
979struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
980 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100981{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100982 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100983
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100984 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100985
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100986 /* Get next list entry. */
987 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100988
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100989 /* If the entry is the last of the list, return NULL. */
990 if (&ref->list == end)
991 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100992
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100993 /* If the entry match the flag, return it. */
994 if (ref->flags & flags)
995 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100996 }
997}
998
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +0100999static inline
1000struct pat_ref *pat_ref_lookup_ref(const char *reference)
1001{
1002 int id;
1003 char *error;
1004
1005 /* If the reference starts by a '#', this is numeric id. */
1006 if (reference[0] == '#') {
1007 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1008 id = strtol(reference + 1, &error, 10);
1009 if (*error != '\0')
1010 return NULL;
1011
1012 /* Perform the unique id lookup. */
1013 return pat_ref_lookupid(id);
1014 }
1015
1016 /* Perform the string lookup. */
1017 return pat_ref_lookup(reference);
1018}
1019
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001020/* This function is used with map and acl management. It permits to browse
1021 * each reference.
1022 */
1023static inline
1024struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1025{
1026 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001027 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1028 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001029 return NULL;
1030 return expr;
1031}
1032
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001033/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001034 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001035 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001036 * designating the function which will have to process the request, which can
1037 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001038 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001039static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001040{
Willy Tarreau306f8302013-07-08 15:53:06 +02001041 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001042 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001043 char *args[MAX_STATS_ARGS + 1];
1044 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001045 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001046
1047 while (isspace((unsigned char)*line))
1048 line++;
1049
1050 arg = 0;
1051 args[arg] = line;
1052
1053 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001054 if (*line == '\\') {
1055 line++;
1056 if (*line == '\0')
1057 break;
1058 }
1059 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001060 *line++ = '\0';
1061
1062 while (isspace((unsigned char)*line))
1063 line++;
1064
1065 args[++arg] = line;
1066 continue;
1067 }
1068
1069 line++;
1070 }
1071
1072 while (++arg <= MAX_STATS_ARGS)
1073 args[arg] = line;
1074
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001075 /* remove \ */
1076 arg = 0;
1077 while (*args[arg] != '\0') {
1078 j = 0;
1079 for (i=0; args[arg][i] != '\0'; i++) {
1080 if (args[arg][i] == '\\')
1081 continue;
1082 args[arg][j] = args[arg][i];
1083 j++;
1084 }
1085 args[arg][j] = '\0';
1086 arg++;
1087 }
1088
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001089 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001090 if (strcmp(args[0], "show") == 0) {
1091 if (strcmp(args[1], "stat") == 0) {
1092 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001093 appctx->ctx.stats.flags |= STAT_BOUND;
1094 appctx->ctx.stats.iid = atoi(args[2]);
1095 appctx->ctx.stats.type = atoi(args[3]);
1096 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001097 }
1098
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001099 appctx->st2 = STAT_ST_INIT;
1100 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001101 }
1102 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001103 appctx->st2 = STAT_ST_INIT;
1104 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001105 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001106 else if (strcmp(args[1], "pools") == 0) {
1107 appctx->st2 = STAT_ST_INIT;
1108 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1109 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001110 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001111 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001112 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001113 appctx->ctx.cli.msg = stats_permission_denied_msg;
1114 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001115 return 1;
1116 }
Willy Tarreau76153662012-11-26 01:16:39 +01001117 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001118 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001119 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001120 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001121 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001122 appctx->ctx.sess.target = NULL;
1123 appctx->ctx.sess.section = 0; /* start with session status */
1124 appctx->ctx.sess.pos = 0;
1125 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001126 }
1127 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001128 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001129 appctx->ctx.cli.msg = stats_permission_denied_msg;
1130 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001131 return 1;
1132 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001133 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001134 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001135 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001136 appctx->ctx.errors.iid = -1;
1137 appctx->ctx.errors.px = NULL;
1138 appctx->st2 = STAT_ST_INIT;
1139 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001140 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001141 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001142 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001143 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001144 else if (strcmp(args[1], "map") == 0 ||
1145 strcmp(args[1], "acl") == 0) {
1146
1147 /* Set ACL or MAP flags. */
1148 if (args[1][0] == 'm')
1149 appctx->ctx.map.display_flags = PAT_REF_MAP;
1150 else
1151 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001152
1153 /* no parameter: display all map avalaible */
1154 if (!*args[2]) {
1155 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001156 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001157 return 1;
1158 }
1159
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001160 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001161 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001162 if (!appctx->ctx.map.ref ||
1163 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1164 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001165 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001166 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001167 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001168 appctx->st0 = STAT_CLI_PRINT;
1169 return 1;
1170 }
1171 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001172 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001173 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001174 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001175 return 0;
1176 }
1177 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001178 else if (strcmp(args[0], "clear") == 0) {
1179 if (strcmp(args[1], "counters") == 0) {
1180 struct proxy *px;
1181 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001182 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001183 int clrall = 0;
1184
1185 if (strcmp(args[2], "all") == 0)
1186 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001187
Willy Tarreau6162db22009-10-10 17:13:00 +02001188 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001189 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1190 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001191 appctx->ctx.cli.msg = stats_permission_denied_msg;
1192 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001193 return 1;
1194 }
1195
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001196 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001197 if (clrall) {
1198 memset(&px->be_counters, 0, sizeof(px->be_counters));
1199 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1200 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001201 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001202 px->be_counters.conn_max = 0;
1203 px->be_counters.p.http.rps_max = 0;
1204 px->be_counters.sps_max = 0;
1205 px->be_counters.cps_max = 0;
1206 px->be_counters.nbpend_max = 0;
1207
1208 px->fe_counters.conn_max = 0;
1209 px->fe_counters.p.http.rps_max = 0;
1210 px->fe_counters.sps_max = 0;
1211 px->fe_counters.cps_max = 0;
1212 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001213 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001214
1215 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001216 if (clrall)
1217 memset(&sv->counters, 0, sizeof(sv->counters));
1218 else {
1219 sv->counters.cur_sess_max = 0;
1220 sv->counters.nbpend_max = 0;
1221 sv->counters.sps_max = 0;
1222 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001223
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001224 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001225 if (li->counters) {
1226 if (clrall)
1227 memset(li->counters, 0, sizeof(*li->counters));
1228 else
1229 li->counters->conn_max = 0;
1230 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001231 }
1232
Willy Tarreau81c25d02011-09-07 15:17:21 +02001233 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001234 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001235 return 1;
1236 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001237 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001238 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001239 /* end of processing */
1240 return 1;
1241 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001242 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1243 /* Set ACL or MAP flags. */
1244 if (args[1][0] == 'm')
1245 appctx->ctx.map.display_flags = PAT_REF_MAP;
1246 else
1247 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001248
1249 /* no parameter */
1250 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001251 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1252 appctx->ctx.cli.msg = "Missing map identifier.\n";
1253 else
1254 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001255 appctx->st0 = STAT_CLI_PRINT;
1256 return 1;
1257 }
1258
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001259 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001260 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001261 if (!appctx->ctx.map.ref ||
1262 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1263 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001264 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001265 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001266 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001267 appctx->st0 = STAT_CLI_PRINT;
1268 return 1;
1269 }
1270
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001271 /* Clear all. */
1272 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001273
1274 /* return response */
1275 appctx->ctx.cli.msg = "Done.\n";
1276 appctx->st0 = STAT_CLI_PRINT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001277 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001278 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001279 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001280 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001281 return 0;
1282 }
1283 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001284 else if (strcmp(args[0], "get") == 0) {
1285 if (strcmp(args[1], "weight") == 0) {
1286 struct proxy *px;
1287 struct server *sv;
1288
1289 /* split "backend/server" and make <line> point to server */
1290 for (line = args[2]; *line; line++)
1291 if (*line == '/') {
1292 *line++ = '\0';
1293 break;
1294 }
1295
1296 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001297 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1298 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001299 return 1;
1300 }
1301
1302 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001303 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1304 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001305 return 1;
1306 }
1307
1308 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001309 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1310 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001311 return 1;
1312 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001313 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1314 /* Set flags. */
1315 if (args[1][0] == 'm')
1316 appctx->ctx.map.display_flags = PAT_REF_MAP;
1317 else
1318 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001319
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001320 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001321 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001322 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1323 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1324 else
1325 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001326 appctx->st0 = STAT_CLI_PRINT;
1327 return 1;
1328 }
1329
1330 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001331 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001332 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001333 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001334 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001335 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001336 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001337 appctx->st0 = STAT_CLI_PRINT;
1338 return 1;
1339 }
1340
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001341 /* copy input string. The string must be allocated because
1342 * it may be used over multiple iterations. It's released
1343 * at the end and upon abort anyway.
1344 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001345 appctx->ctx.map.chunk.len = strlen(args[3]);
1346 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1347 appctx->ctx.map.chunk.str = strdup(args[3]);
1348 if (!appctx->ctx.map.chunk.str) {
1349 appctx->ctx.cli.msg = "Out of memory error.\n";
1350 appctx->st0 = STAT_CLI_PRINT;
1351 return 1;
1352 }
1353
1354 /* prepare response */
1355 appctx->st2 = STAT_ST_INIT;
1356 appctx->st0 = STAT_CLI_O_MLOOK;
1357 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001358 else { /* not "get weight" */
1359 return 0;
1360 }
1361 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001362 else if (strcmp(args[0], "set") == 0) {
1363 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001364 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001365 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001366
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001367 sv = expect_server_admin(s, si, args[2]);
1368 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001369 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001370
Simon Horman7d09b9a2013-02-12 10:45:51 +09001371 warning = server_parse_weight_change_request(sv, args[3]);
1372 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001373 appctx->ctx.cli.msg = warning;
1374 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001375 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001376 return 1;
1377 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001378 else if (strcmp(args[1], "timeout") == 0) {
1379 if (strcmp(args[2], "cli") == 0) {
1380 unsigned timeout;
1381 const char *res;
1382
1383 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001384 appctx->ctx.cli.msg = "Expects an integer value.\n";
1385 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001386 return 1;
1387 }
1388
1389 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1390 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001391 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1392 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001393 return 1;
1394 }
1395
1396 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1397 return 1;
1398 }
1399 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001400 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1401 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001402 return 1;
1403 }
1404 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001405 else if (strcmp(args[1], "maxconn") == 0) {
1406 if (strcmp(args[2], "frontend") == 0) {
1407 struct proxy *px;
1408 struct listener *l;
1409 int v;
1410
Willy Tarreau532a4502011-09-07 22:37:44 +02001411 px = expect_frontend_admin(s, si, args[3]);
1412 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001413 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001414
1415 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001416 appctx->ctx.cli.msg = "Integer value expected.\n";
1417 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001418 return 1;
1419 }
1420
1421 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001422 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001423 appctx->ctx.cli.msg = "Value out of range.\n";
1424 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001425 return 1;
1426 }
1427
1428 /* OK, the value is fine, so we assign it to the proxy and to all of
1429 * its listeners. The blocked ones will be dequeued.
1430 */
1431 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001432 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001433 l->maxconn = v;
1434 if (l->state == LI_FULL)
1435 resume_listener(l);
1436 }
1437
1438 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1439 dequeue_all_listeners(&s->fe->listener_queue);
1440
1441 return 1;
1442 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001443 else if (strcmp(args[2], "global") == 0) {
1444 int v;
1445
Willy Tarreau290e63a2012-09-20 18:07:14 +02001446 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001447 appctx->ctx.cli.msg = stats_permission_denied_msg;
1448 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001449 return 1;
1450 }
1451
1452 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001453 appctx->ctx.cli.msg = "Expects an integer value.\n";
1454 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001455 return 1;
1456 }
1457
1458 v = atoi(args[3]);
1459 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001460 appctx->ctx.cli.msg = "Value out of range.\n";
1461 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001462 return 1;
1463 }
1464
1465 /* check for unlimited values */
1466 if (v <= 0)
1467 v = global.hardmaxconn;
1468
1469 global.maxconn = v;
1470
1471 /* Dequeues all of the listeners waiting for a resource */
1472 if (!LIST_ISEMPTY(&global_listener_queue))
1473 dequeue_all_listeners(&global_listener_queue);
1474
1475 return 1;
1476 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001477 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001478 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1479 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001480 return 1;
1481 }
1482 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001483 else if (strcmp(args[1], "rate-limit") == 0) {
1484 if (strcmp(args[2], "connections") == 0) {
1485 if (strcmp(args[3], "global") == 0) {
1486 int v;
1487
Willy Tarreau290e63a2012-09-20 18:07:14 +02001488 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001489 appctx->ctx.cli.msg = stats_permission_denied_msg;
1490 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001491 return 1;
1492 }
1493
1494 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001495 appctx->ctx.cli.msg = "Expects an integer value.\n";
1496 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001497 return 1;
1498 }
1499
1500 v = atoi(args[4]);
1501 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001502 appctx->ctx.cli.msg = "Value out of range.\n";
1503 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001504 return 1;
1505 }
1506
1507 global.cps_lim = v;
1508
1509 /* Dequeues all of the listeners waiting for a resource */
1510 if (!LIST_ISEMPTY(&global_listener_queue))
1511 dequeue_all_listeners(&global_listener_queue);
1512
1513 return 1;
1514 }
1515 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001516 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1517 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001518 return 1;
1519 }
1520 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001521 else if (strcmp(args[2], "sessions") == 0) {
1522 if (strcmp(args[3], "global") == 0) {
1523 int v;
1524
1525 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1526 appctx->ctx.cli.msg = stats_permission_denied_msg;
1527 appctx->st0 = STAT_CLI_PRINT;
1528 return 1;
1529 }
1530
1531 if (!*args[4]) {
1532 appctx->ctx.cli.msg = "Expects an integer value.\n";
1533 appctx->st0 = STAT_CLI_PRINT;
1534 return 1;
1535 }
1536
1537 v = atoi(args[4]);
1538 if (v < 0) {
1539 appctx->ctx.cli.msg = "Value out of range.\n";
1540 appctx->st0 = STAT_CLI_PRINT;
1541 return 1;
1542 }
1543
1544 global.sps_lim = v;
1545
1546 /* Dequeues all of the listeners waiting for a resource */
1547 if (!LIST_ISEMPTY(&global_listener_queue))
1548 dequeue_all_listeners(&global_listener_queue);
1549
1550 return 1;
1551 }
1552 else {
1553 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1554 appctx->st0 = STAT_CLI_PRINT;
1555 return 1;
1556 }
1557 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001558#ifdef USE_OPENSSL
1559 else if (strcmp(args[2], "ssl-sessions") == 0) {
1560 if (strcmp(args[3], "global") == 0) {
1561 int v;
1562
1563 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1564 appctx->ctx.cli.msg = stats_permission_denied_msg;
1565 appctx->st0 = STAT_CLI_PRINT;
1566 return 1;
1567 }
1568
1569 if (!*args[4]) {
1570 appctx->ctx.cli.msg = "Expects an integer value.\n";
1571 appctx->st0 = STAT_CLI_PRINT;
1572 return 1;
1573 }
1574
1575 v = atoi(args[4]);
1576 if (v < 0) {
1577 appctx->ctx.cli.msg = "Value out of range.\n";
1578 appctx->st0 = STAT_CLI_PRINT;
1579 return 1;
1580 }
1581
1582 global.ssl_lim = v;
1583
1584 /* Dequeues all of the listeners waiting for a resource */
1585 if (!LIST_ISEMPTY(&global_listener_queue))
1586 dequeue_all_listeners(&global_listener_queue);
1587
1588 return 1;
1589 }
1590 else {
1591 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1592 appctx->st0 = STAT_CLI_PRINT;
1593 return 1;
1594 }
1595 }
1596#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001597 else if (strcmp(args[2], "http-compression") == 0) {
1598 if (strcmp(args[3], "global") == 0) {
1599 int v;
1600
Willy Tarreau85d47f92012-11-21 00:29:50 +01001601 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001602 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1603 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001604 return 1;
1605 }
1606
William Lallemandd85f9172012-11-09 17:05:39 +01001607 v = atoi(args[4]);
1608 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1609 }
1610 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001611 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1612 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001613 return 1;
1614 }
1615 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001616 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001617 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001618 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001619 return 1;
1620 }
1621 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001622 else if (strcmp(args[1], "table") == 0) {
1623 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1624 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001625 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001626 char *err;
1627
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001628 /* Set flags. */
1629 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001630
1631 /* Expect three parameters: map name, key and new value. */
1632 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001633 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001634 appctx->st0 = STAT_CLI_PRINT;
1635 return 1;
1636 }
1637
1638 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001639 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001640 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001641 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001642 appctx->st0 = STAT_CLI_PRINT;
1643 return 1;
1644 }
1645
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001646 /* If the entry identifier start with a '#', it is considered as
1647 * pointer id
1648 */
1649 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1650 struct pat_ref_elt *ref;
1651 long long int conv;
1652 char *error;
1653
1654 /* Convert argument to integer value. */
1655 conv = strtoll(&args[3][1], &error, 16);
1656 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001657 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001658 appctx->st0 = STAT_CLI_PRINT;
1659 return 1;
1660 }
1661
1662 /* Convert and check integer to pointer. */
1663 ref = (struct pat_ref_elt *)(long)conv;
1664 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001665 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001666 appctx->st0 = STAT_CLI_PRINT;
1667 return 1;
1668 }
1669
1670 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001671 err = NULL;
1672 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1673 if (err)
1674 memprintf(&err, "%s.\n", err);
1675 appctx->ctx.cli.err = err;
1676 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001677 return 1;
1678 }
1679 }
1680 else {
1681 /* Else, use the entry identifier as pattern
1682 * string, and update the value.
1683 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001684 err = NULL;
1685 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1686 if (err)
1687 memprintf(&err, "%s.\n", err);
1688 appctx->ctx.cli.err = err;
1689 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001690 return 1;
1691 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001692 }
1693
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001694 /* The set is done, send message. */
1695 appctx->ctx.cli.msg = "Done.\n";
1696 appctx->st0 = STAT_CLI_PRINT;
1697 return 1;
1698 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001699 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001700 return 0;
1701 }
1702 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001703 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001704 if (strcmp(args[1], "agent") == 0) {
1705 struct server *sv;
1706
1707 sv = expect_server_admin(s, si, args[2]);
1708 if (!sv)
1709 return 1;
1710
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001711 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1712 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1713 appctx->st0 = STAT_CLI_PRINT;
1714 return 1;
1715 }
1716
1717 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001718 return 1;
1719 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001720 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001721 struct server *sv;
1722
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001723 sv = expect_server_admin(s, si, args[2]);
1724 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001725 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001726
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001727 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001728 return 1;
1729 }
1730 else if (strcmp(args[1], "frontend") == 0) {
1731 struct proxy *px;
1732
1733 px = expect_frontend_admin(s, si, args[2]);
1734 if (!px)
1735 return 1;
1736
1737 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001738 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1739 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001740 return 1;
1741 }
1742
1743 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001744 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1745 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001746 return 1;
1747 }
1748
1749 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001750 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1751 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001752 return 1;
1753 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001754 return 1;
1755 }
1756 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001757 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1758 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001759 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001760 }
1761 }
1762 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001763 if (strcmp(args[1], "agent") == 0) {
1764 struct server *sv;
1765
1766 sv = expect_server_admin(s, si, args[2]);
1767 if (!sv)
1768 return 1;
1769
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001770 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001771 return 1;
1772 }
1773 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001774 struct server *sv;
1775
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001776 sv = expect_server_admin(s, si, args[2]);
1777 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001778 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001779
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001780 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001781 return 1;
1782 }
1783 else if (strcmp(args[1], "frontend") == 0) {
1784 struct proxy *px;
1785
1786 px = expect_frontend_admin(s, si, args[2]);
1787 if (!px)
1788 return 1;
1789
1790 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001791 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1792 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001793 return 1;
1794 }
1795
1796 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001797 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1798 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001799 return 1;
1800 }
1801
1802 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001803 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1804 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001805 return 1;
1806 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001807 return 1;
1808 }
1809 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001810 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1811 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001812 return 1;
1813 }
1814 }
1815 else if (strcmp(args[0], "shutdown") == 0) {
1816 if (strcmp(args[1], "frontend") == 0) {
1817 struct proxy *px;
1818
1819 px = expect_frontend_admin(s, si, args[2]);
1820 if (!px)
1821 return 1;
1822
1823 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001824 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1825 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001826 return 1;
1827 }
1828
1829 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1830 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1831 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1832 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1833 stop_proxy(px);
1834 return 1;
1835 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001836 else if (strcmp(args[1], "session") == 0) {
1837 struct session *sess, *ptr;
1838
Willy Tarreau290e63a2012-09-20 18:07:14 +02001839 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001840 appctx->ctx.cli.msg = stats_permission_denied_msg;
1841 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001842 return 1;
1843 }
1844
1845 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001846 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1847 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001848 return 1;
1849 }
1850
1851 ptr = (void *)strtoul(args[2], NULL, 0);
1852
1853 /* first, look for the requested session in the session table */
1854 list_for_each_entry(sess, &sessions, list) {
1855 if (sess == ptr)
1856 break;
1857 }
1858
1859 /* do we have the session ? */
1860 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001861 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1862 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001863 return 1;
1864 }
1865
1866 session_shutdown(sess, SN_ERR_KILLED);
1867 return 1;
1868 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001869 else if (strcmp(args[1], "sessions") == 0) {
1870 if (strcmp(args[2], "server") == 0) {
1871 struct server *sv;
1872 struct session *sess, *sess_bck;
1873
1874 sv = expect_server_admin(s, si, args[3]);
1875 if (!sv)
1876 return 1;
1877
1878 /* kill all the session that are on this server */
1879 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1880 if (sess->srv_conn == sv)
1881 session_shutdown(sess, SN_ERR_KILLED);
1882
1883 return 1;
1884 }
1885 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001886 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1887 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001888 return 1;
1889 }
1890 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001891 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001892 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1893 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001894 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001895 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001896 }
1897 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001898 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1899 if (args[1][0] == 'm')
1900 appctx->ctx.map.display_flags = PAT_REF_MAP;
1901 else
1902 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001903
1904 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001905 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1906 if (!*args[2] || !*args[3]) {
1907 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
1908 appctx->st0 = STAT_CLI_PRINT;
1909 return 1;
1910 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001911 }
1912
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001913 else {
1914 if (!*args[2] || !*args[3]) {
1915 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
1916 appctx->st0 = STAT_CLI_PRINT;
1917 return 1;
1918 }
1919 }
1920
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001921 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001922 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001923 if (!appctx->ctx.map.ref ||
1924 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001925 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001926 appctx->st0 = STAT_CLI_PRINT;
1927 return 1;
1928 }
1929
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001930 /* If the entry identifier start with a '#', it is considered as
1931 * pointer id
1932 */
1933 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1934 struct pat_ref_elt *ref;
1935 long long int conv;
1936 char *error;
1937
1938 /* Convert argument to integer value. */
1939 conv = strtoll(&args[3][1], &error, 16);
1940 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001941 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001942 appctx->st0 = STAT_CLI_PRINT;
1943 return 1;
1944 }
1945
1946 /* Convert and check integer to pointer. */
1947 ref = (struct pat_ref_elt *)(long)conv;
1948 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001949 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001950 appctx->st0 = STAT_CLI_PRINT;
1951 return 1;
1952 }
1953
1954 /* Try to delete the entry. */
1955 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
1956 /* The entry is not found, send message. */
1957 appctx->ctx.cli.msg = "Key not found.\n";
1958 appctx->st0 = STAT_CLI_PRINT;
1959 return 1;
1960 }
1961 }
1962 else {
1963 /* Else, use the entry identifier as pattern
1964 * string and try to delete the entry.
1965 */
1966 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
1967 /* The entry is not found, send message. */
1968 appctx->ctx.cli.msg = "Key not found.\n";
1969 appctx->st0 = STAT_CLI_PRINT;
1970 return 1;
1971 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001972 }
1973
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001974 /* The deletion is done, send message. */
1975 appctx->ctx.cli.msg = "Done.\n";
1976 appctx->st0 = STAT_CLI_PRINT;
1977 return 1;
1978 }
1979 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001980 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001981 appctx->st0 = STAT_CLI_PRINT;
1982 return 1;
1983 }
1984 }
1985 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001986 if (strcmp(args[1], "map") == 0 ||
1987 strcmp(args[1], "acl") == 0) {
1988 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001989 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001990
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001991 /* Set flags. */
1992 if (args[1][0] == 'm')
1993 appctx->ctx.map.display_flags = PAT_REF_MAP;
1994 else
1995 appctx->ctx.map.display_flags = PAT_REF_ACL;
1996
1997 /* If the keywork is "map", we expect three parameters, if it
1998 * is "acl", we expect only two parameters
1999 */
2000 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2001 if (!*args[2] || !*args[3] || !*args[4]) {
2002 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2003 appctx->st0 = STAT_CLI_PRINT;
2004 return 1;
2005 }
2006 }
2007 else {
2008 if (!*args[2] || !*args[3]) {
2009 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2010 appctx->st0 = STAT_CLI_PRINT;
2011 return 1;
2012 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002013 }
2014
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002015 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002016 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002017 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002018 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002019 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002020 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002021 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002022 appctx->st0 = STAT_CLI_PRINT;
2023 return 1;
2024 }
2025
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002026 /* The command "add acl" is prohibited if the reference
2027 * use samples.
2028 */
2029 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2030 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2031 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2032 "You must use the command 'add map' to add values.\n";
2033 appctx->st0 = STAT_CLI_PRINT;
2034 return 1;
2035 }
2036
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002037 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002038 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002039 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002040 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002041 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002042 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002043 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002044 if (err)
2045 memprintf(&err, "%s.\n", err);
2046 appctx->ctx.cli.err = err;
2047 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002048 return 1;
2049 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002050
2051 /* The add is done, send message. */
2052 appctx->ctx.cli.msg = "Done.\n";
2053 appctx->st0 = STAT_CLI_PRINT;
2054 return 1;
2055 }
2056 else { /* unknown "del" parameter */
2057 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2058 appctx->st0 = STAT_CLI_PRINT;
2059 return 1;
2060 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002061 }
2062 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002063 return 0;
2064 }
2065 return 1;
2066}
2067
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002068/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002069 * used to processes I/O from/to the stats unix socket. The system relies on a
2070 * state machine handling requests and various responses. We read a request,
2071 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002072 * Then we can read again. The state is stored in appctx->st0 and is one of the
2073 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002074 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002075 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002076static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002077{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002078 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002079 struct channel *req = si->ob;
2080 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002081 int reql;
2082 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002083
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002084 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2085 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002086
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002087 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002088 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002089 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002090 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2091 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002092 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002093 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002094 /* Let's close for real now. We just close the request
2095 * side, the conditions below will complete if needed.
2096 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002097 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002098 break;
2099 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002100 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002101 /* ensure we have some output room left in the event we
2102 * would want to return some info right after parsing.
2103 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002104 if (buffer_almost_full(si->ib->buf)) {
2105 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002106 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002107 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002108
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002109 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002110 if (reql <= 0) { /* closed or EOL not found */
2111 if (reql == 0)
2112 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002113 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002114 continue;
2115 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002116
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002117 /* seek for a possible semi-colon. If we find one, we
2118 * replace it with an LF and skip only this part.
2119 */
2120 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002121 if (trash.str[len] == ';') {
2122 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002123 reql = len + 1;
2124 break;
2125 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002126
Willy Tarreau816fc222009-10-04 07:36:58 +02002127 /* now it is time to check that we have a full line,
2128 * remove the trailing \n and possibly \r, then cut the
2129 * line.
2130 */
2131 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002132 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002133 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002134 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002135 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002136
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002137 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002138 len--;
2139
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002140 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002141
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002142 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002143 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002144 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002145 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002146 continue;
2147 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002148 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002149 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002150 else if (strcmp(trash.str, "help") == 0 ||
2151 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002152 appctx->ctx.cli.msg = stats_sock_usage_msg;
2153 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002154 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002155 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002156 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002157 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002158 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002159 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002160 /* if prompt is disabled, print help on empty lines,
2161 * so that the user at least knows how to enable
2162 * prompt and find help.
2163 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002164 appctx->ctx.cli.msg = stats_sock_usage_msg;
2165 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002166 }
2167
2168 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002169 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002170 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002171 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002172 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002173 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002174 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002175 continue;
2176 }
2177
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002178 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002179 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002180 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2181 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002182 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002183 case STAT_CLI_PRINT_FREE:
2184 if (bi_putstr(si->ib, appctx->ctx.cli.err) != -1) {
2185 free(appctx->ctx.cli.err);
2186 appctx->st0 = STAT_CLI_PROMPT;
2187 }
2188 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002189 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002190 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002191 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002192 break;
2193 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002194 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002195 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002196 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002197 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002198 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002199 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002200 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002201 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002202 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002203 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002204 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002205 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002206 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002207 if (stats_table_request(si, appctx->st0))
2208 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002209 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002210 case STAT_CLI_O_PATS:
2211 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002212 appctx->st0 = STAT_CLI_PROMPT;
2213 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002214 case STAT_CLI_O_PAT:
2215 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002216 appctx->st0 = STAT_CLI_PROMPT;
2217 break;
2218 case STAT_CLI_O_MLOOK:
2219 if (stats_map_lookup(si))
2220 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002221 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002222 case STAT_CLI_O_POOLS:
2223 if (stats_dump_pools_to_buffer(si))
2224 appctx->st0 = STAT_CLI_PROMPT;
2225 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002226 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002227 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002228 break;
2229 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002230
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002231 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002232 if (appctx->st0 == STAT_CLI_PROMPT) {
2233 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2234 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002235 }
2236
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002237 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002238 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002239 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002240
2241 /* Now we close the output if one of the writers did so,
2242 * or if we're not in interactive mode and the request
2243 * buffer is empty. This still allows pipelined requests
2244 * to be sent in non-interactive mode.
2245 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002246 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2247 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002248 continue;
2249 }
2250
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002251 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002252 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002253 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002254 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002255
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002256 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002257 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2258 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002259 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002260 * and nothing more to consume. This is comparable to a broken pipe, so
2261 * we forward the close to the request side so that it flows upstream to
2262 * the client.
2263 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002264 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002265 }
2266
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002267 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002268 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2269 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2270 /* We have no more processing to do, and nothing more to send, and
2271 * the client side has closed. So we'll forward this state downstream
2272 * on the response buffer.
2273 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002274 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002275 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002276 }
2277
2278 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002279 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002280
2281 /* we don't want to expire timeouts while we're processing requests */
2282 si->ib->rex = TICK_ETERNITY;
2283 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002284
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002285 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002286 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 +02002287 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002288 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 +02002289
2290 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2291 /* check that we have released everything then unregister */
2292 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002293 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002294}
2295
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002296/* This function dumps information onto the stream interface's read buffer.
2297 * It returns 0 as long as it does not complete, non-zero upon completion.
2298 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002299 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002300static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002301{
2302 unsigned int up = (now.tv_sec - start_date.tv_sec);
2303
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002304 chunk_printf(&trash,
2305 "Name: " PRODUCT_NAME "\n"
2306 "Version: " HAPROXY_VERSION "\n"
2307 "Release_date: " HAPROXY_DATE "\n"
2308 "Nbproc: %d\n"
2309 "Process_num: %d\n"
2310 "Pid: %d\n"
2311 "Uptime: %dd %dh%02dm%02ds\n"
2312 "Uptime_sec: %d\n"
2313 "Memmax_MB: %d\n"
2314 "Ulimit-n: %d\n"
2315 "Maxsock: %d\n"
2316 "Maxconn: %d\n"
2317 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002318 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002319 "CumConns: %d\n"
2320 "CumReq: %d\n"
2321#ifdef USE_OPENSSL
2322 "MaxSslConns: %d\n"
2323 "CurrSslConns: %d\n"
2324 "CumSslConns: %d\n"
2325#endif
2326 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002327 "PipesUsed: %d\n"
2328 "PipesFree: %d\n"
2329 "ConnRate: %d\n"
2330 "ConnRateLimit: %d\n"
2331 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002332 "SessRate: %d\n"
2333 "SessRateLimit: %d\n"
2334 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002335#ifdef USE_OPENSSL
2336 "SslRate: %d\n"
2337 "SslRateLimit: %d\n"
2338 "MaxSslRate: %d\n"
2339#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002340 "CompressBpsIn: %u\n"
2341 "CompressBpsOut: %u\n"
2342 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002343#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002344 "ZlibMemUsage: %ld\n"
2345 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002346#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002347 "Tasks: %d\n"
2348 "Run_queue: %d\n"
2349 "Idle_pct: %d\n"
2350 "node: %s\n"
2351 "description: %s\n"
2352 "",
2353 global.nbproc,
2354 relative_pid,
2355 pid,
2356 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2357 up,
2358 global.rlimit_memmax,
2359 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002360 global.maxsock, global.maxconn, global.hardmaxconn,
2361 actconn, totalconn, global.req_count,
2362#ifdef USE_OPENSSL
2363 global.maxsslconn, sslconns, totalsslconns,
2364#endif
2365 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002366 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002367 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002368#ifdef USE_OPENSSL
2369 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2370#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002371 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2372 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002373#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002374 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002375#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002376 nb_tasks_cur, run_queue_cur, idle_pct,
2377 global.node, global.desc ? global.desc : ""
2378 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002379
2380 if (bi_putchk(si->ib, &trash) == -1)
2381 return 0;
2382
2383 return 1;
2384}
2385
Willy Tarreau12833bb2014-01-28 16:49:56 +01002386/* This function dumps memory usage information onto the stream interface's
2387 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2388 * completion. No state is used.
2389 */
2390static int stats_dump_pools_to_buffer(struct stream_interface *si)
2391{
2392 dump_pools_to_trash();
2393 if (bi_putchk(si->ib, &trash) == -1)
2394 return 0;
2395 return 1;
2396}
2397
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002398/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2399 * the state from stream interface <si>. The caller is responsible for clearing
2400 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002401 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002402static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002403{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002404 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002405 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002406
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002407 if (!(px->cap & PR_CAP_FE))
2408 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002409
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002410 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002411 return 0;
2412
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002413 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002414 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002415 /* name, queue */
2416 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002417
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002418 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002419 /* Column sub-heading for Enable or Disable server */
2420 chunk_appendf(&trash, "<td></td>");
2421 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002422
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002423 chunk_appendf(&trash,
2424 "<td class=ac>"
2425 "<a name=\"%s/Frontend\"></a>"
2426 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2427 "<td colspan=3></td>"
2428 "",
2429 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002430
Willy Tarreau466c9b52012-12-23 02:25:03 +01002431 chunk_appendf(&trash,
2432 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002433 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002434 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2435 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2436 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002437 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2438 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2439 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002440
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002441 if (px->mode == PR_MODE_HTTP)
2442 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002443 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002444 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002445
2446 chunk_appendf(&trash,
2447 "</table></div></u></td>"
2448 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002449 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002450 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2451 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2452 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002453 U2H(px->fe_counters.sps_max),
2454 U2H(px->fe_counters.cps_max),
2455 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002456
2457 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002458 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002459 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002460 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002461
2462 chunk_appendf(&trash,
2463 "</table></div></u></td>"
2464 /* sessions rate : limit */
2465 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002466 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002467
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002468 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002469 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002470 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002471 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002472 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2473 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002474 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002475 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2476 U2H(px->fe_counters.cum_sess),
2477 U2H(px->fe_counters.cum_conn),
2478 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002479
Willy Tarreau466c9b52012-12-23 02:25:03 +01002480 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002481 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002482 chunk_appendf(&trash,
2483 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2484 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2485 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2486 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2487 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2488 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2489 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2490 "<tr><th>- other responses:</th><td>%s</td></tr>"
2491 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2492 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002493 U2H(px->fe_counters.p.http.cum_req),
2494 U2H(px->fe_counters.p.http.rsp[1]),
2495 U2H(px->fe_counters.p.http.rsp[2]),
2496 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002497 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002498 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002499 U2H(px->fe_counters.p.http.rsp[3]),
2500 U2H(px->fe_counters.p.http.rsp[4]),
2501 U2H(px->fe_counters.p.http.rsp[5]),
2502 U2H(px->fe_counters.p.http.rsp[0]),
2503 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002504 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002505
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002506 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002507 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002508 /* sessions: lbtot, lastsess */
2509 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002510 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002511 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002512 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002513 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002514
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002515 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002516 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002517 "<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 +01002518 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002519 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002520 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2521 px->fe_counters.comp_in ?
2522 (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 +01002523 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002524
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002525 chunk_appendf(&trash,
2526 /* denied: req, resp */
2527 "<td>%s</td><td>%s</td>"
2528 /* errors : request, connect, response */
2529 "<td>%s</td><td></td><td></td>"
2530 /* warnings: retries, redispatches */
2531 "<td></td><td></td>"
2532 /* server status : reflect frontend status */
2533 "<td class=ac>%s</td>"
2534 /* rest of server: nothing */
2535 "<td class=ac colspan=8></td></tr>"
2536 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002537 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2538 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002539 px->state == PR_STREADY ? "OPEN" :
2540 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002541 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002542 else { /* CSV mode */
2543 chunk_appendf(&trash,
2544 /* pxid, name, queue cur, queue max, */
2545 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002546 /* sessions : current, max, limit, total */
2547 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002548 /* bytes : in, out */
2549 "%lld,%lld,"
2550 /* denied: req, resp */
2551 "%lld,%lld,"
2552 /* errors : request, connect, response */
2553 "%lld,,,"
2554 /* warnings: retries, redispatches */
2555 ",,"
2556 /* server status : reflect frontend status */
2557 "%s,"
2558 /* rest of server: nothing */
2559 ",,,,,,,,"
2560 /* pid, iid, sid, throttle, lbtot, tracked, type */
2561 "%d,%d,0,,,,%d,"
2562 /* rate, rate_lim, rate_max */
2563 "%u,%u,%u,"
2564 /* check_status, check_code, check_duration */
2565 ",,,",
2566 px->id,
2567 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2568 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2569 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2570 px->fe_counters.failed_req,
2571 px->state == PR_STREADY ? "OPEN" :
2572 px->state == PR_STFULL ? "FULL" : "STOP",
2573 relative_pid, px->uuid, STATS_TYPE_FE,
2574 read_freq_ctr(&px->fe_sess_per_sec),
2575 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002576
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002577 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2578 if (px->mode == PR_MODE_HTTP) {
2579 for (i=1; i<6; i++)
2580 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2581 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2582 }
2583 else
2584 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002585
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002586 /* failed health analyses */
2587 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002588
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002589 /* requests : req_rate, req_rate_max, req_tot, */
2590 chunk_appendf(&trash, "%u,%u,%lld,",
2591 read_freq_ctr(&px->fe_req_per_sec),
2592 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2593
2594 /* errors: cli_aborts, srv_aborts */
2595 chunk_appendf(&trash, ",,");
2596
2597 /* compression: in, out, bypassed */
2598 chunk_appendf(&trash, "%lld,%lld,%lld,",
2599 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2600
2601 /* compression: comp_rsp */
2602 chunk_appendf(&trash, "%lld,",
2603 px->fe_counters.p.http.comp_rsp);
2604
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002605 /* lastsess */
2606 chunk_appendf(&trash, ",");
2607
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002608 /* finish with EOL */
2609 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002610 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002611 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002612}
2613
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002614/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2615 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2616 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2617 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002618 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002619static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002620{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002621 struct appctx *appctx = __objt_appctx(si->end);
2622
2623 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002624 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002625 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002626 /* Column sub-heading for Enable or Disable server */
2627 chunk_appendf(&trash, "<td></td>");
2628 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002629 chunk_appendf(&trash,
2630 /* frontend name, listener name */
2631 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2632 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2633 "",
2634 px->id, l->name,
2635 (flags & ST_SHLGNDS)?"<u>":"",
2636 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002637
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002638 if (flags & ST_SHLGNDS) {
2639 char str[INET6_ADDRSTRLEN];
2640 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002641
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002642 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002643
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002644 port = get_host_port(&l->addr);
2645 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2646 case AF_INET:
2647 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2648 break;
2649 case AF_INET6:
2650 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2651 break;
2652 case AF_UNIX:
2653 chunk_appendf(&trash, "unix, ");
2654 break;
2655 case -1:
2656 chunk_appendf(&trash, "(%s), ", strerror(errno));
2657 break;
2658 }
Willy Tarreau91861262007-10-17 17:06:05 +02002659
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002660 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002661 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002662 }
Willy Tarreau91861262007-10-17 17:06:05 +02002663
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002664 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002665 /* queue */
2666 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002667 /* sessions rate: current, max, limit */
2668 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002669 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002670 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002671 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002672 /* bytes: in, out */
2673 "<td>%s</td><td>%s</td>"
2674 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002675 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002676 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2677 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002678
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002679 chunk_appendf(&trash,
2680 /* denied: req, resp */
2681 "<td>%s</td><td>%s</td>"
2682 /* errors: request, connect, response */
2683 "<td>%s</td><td></td><td></td>"
2684 /* warnings: retries, redispatches */
2685 "<td></td><td></td>"
2686 /* server status: reflect listener status */
2687 "<td class=ac>%s</td>"
2688 /* rest of server: nothing */
2689 "<td class=ac colspan=8></td></tr>"
2690 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002691 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2692 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002693 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2694 }
2695 else { /* CSV mode */
2696 chunk_appendf(&trash,
2697 /* pxid, name, queue cur, queue max, */
2698 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002699 /* sessions: current, max, limit, total */
2700 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002701 /* bytes: in, out */
2702 "%lld,%lld,"
2703 /* denied: req, resp */
2704 "%lld,%lld,"
2705 /* errors: request, connect, response */
2706 "%lld,,,"
2707 /* warnings: retries, redispatches */
2708 ",,"
2709 /* server status: reflect listener status */
2710 "%s,"
2711 /* rest of server: nothing */
2712 ",,,,,,,,"
2713 /* pid, iid, sid, throttle, lbtot, tracked, type */
2714 "%d,%d,%d,,,,%d,"
2715 /* rate, rate_lim, rate_max */
2716 ",,,"
2717 /* check_status, check_code, check_duration */
2718 ",,,"
2719 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2720 ",,,,,,"
2721 /* failed health analyses */
2722 ","
2723 /* requests : req_rate, req_rate_max, req_tot, */
2724 ",,,"
2725 /* errors: cli_aborts, srv_aborts */
2726 ",,"
2727 /* compression: in, out, bypassed, comp_rsp */
2728 ",,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002729 /* lastsess */
2730 ","
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002731 "\n",
2732 px->id, l->name,
2733 l->nbconn, l->counters->conn_max,
2734 l->maxconn, l->counters->cum_conn,
2735 l->counters->bytes_in, l->counters->bytes_out,
2736 l->counters->denied_req, l->counters->denied_resp,
2737 l->counters->failed_req,
2738 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2739 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2740 }
2741 return 1;
2742}
Willy Tarreau91861262007-10-17 17:06:05 +02002743
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002744/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2745 * from stream interface <si>, stats flags <flags>, and server state <state>.
2746 * The caller is responsible for clearing the trash if needed. Returns non-zero
2747 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002748 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2749 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002750 */
2751static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2752{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002753 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002754 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002755 char str[INET6_ADDRSTRLEN];
2756 struct chunk src;
2757 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002758
Willy Tarreau32091232014-05-16 13:52:00 +02002759 /* we have "via" which is the tracked server as described in the configuration,
2760 * and "ref" which is the checked server and the end of the chain.
2761 */
2762 via = sv->track ? sv->track : sv;
2763 ref = via;
2764 while (ref->track)
2765 ref = ref->track;
2766
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002767 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002768 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002769 "DOWN",
2770 "DN %d/%d &uarr;",
2771 "UP %d/%d &darr;",
2772 "UP",
2773 "NOLB %d/%d &darr;",
2774 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002775 "DRAIN %d/%d &darr;",
2776 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002777 "<i>no check</i>"
2778 };
Willy Tarreau91861262007-10-17 17:06:05 +02002779
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002780 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002781 chunk_appendf(&trash, "<tr class=\"maintain\">");
2782 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002783 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002784 "<tr class=\"%s%d\">",
Willy Tarreauc93cd162014-05-13 15:54:22 +02002785 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002786
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002787 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002788 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002789 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2790 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002791
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002792 chunk_appendf(&trash,
2793 "<td class=ac><a name=\"%s/%s\"></a>%s"
2794 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2795 "",
2796 px->id, sv->id,
2797 (flags & ST_SHLGNDS) ? "<u>" : "",
2798 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002799
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002800 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002801 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002802
2803 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2804 case AF_INET:
2805 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2806 break;
2807 case AF_INET6:
2808 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2809 break;
2810 case AF_UNIX:
2811 chunk_appendf(&trash, "unix, ");
2812 break;
2813 case -1:
2814 chunk_appendf(&trash, "(%s), ", strerror(errno));
2815 break;
2816 default: /* address family not supported */
2817 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002818 }
Willy Tarreau91861262007-10-17 17:06:05 +02002819
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002820 /* id */
2821 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002822
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002823 /* cookie */
2824 if (sv->cookie) {
2825 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002826
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002827 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2828 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002829
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002830 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002831 }
2832
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002833 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002834 }
Willy Tarreau91861262007-10-17 17:06:05 +02002835
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002836 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002837 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002838 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002839 /* sessions rate : current, max, limit */
2840 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002841 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002842 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002843 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2844 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002845
Willy Tarreau466c9b52012-12-23 02:25:03 +01002846
2847 chunk_appendf(&trash,
2848 /* sessions: current, max, limit, total */
2849 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002850 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002851 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2852 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002853 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2854 U2H(sv->counters.cum_sess),
2855 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002856
Willy Tarreau466c9b52012-12-23 02:25:03 +01002857 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2858 if (px->mode == PR_MODE_HTTP) {
2859 unsigned long long tot;
2860 for (tot = i = 0; i < 6; i++)
2861 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002862
Willy Tarreau466c9b52012-12-23 02:25:03 +01002863 chunk_appendf(&trash,
2864 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2865 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2866 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2867 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2868 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2869 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2870 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2871 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002872 U2H(tot),
2873 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2874 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2875 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2876 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2877 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2878 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 +02002879 }
Willy Tarreau91861262007-10-17 17:06:05 +02002880
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002881 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002882 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002883 /* sessions: lbtot, last */
2884 "<td>%s</td><td>%s</td>",
2885 U2H(sv->counters.cum_lbconn),
2886 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02002887
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002888 chunk_appendf(&trash,
2889 /* bytes : in, out */
2890 "<td>%s</td><td>%s</td>"
2891 /* denied: req, resp */
2892 "<td></td><td>%s</td>"
2893 /* errors : request, connect */
2894 "<td></td><td>%s</td>"
2895 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002896 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002897 /* warnings: retries, redispatches */
2898 "<td>%lld</td><td>%lld</td>"
2899 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002900 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2901 U2H(sv->counters.failed_secu),
2902 U2H(sv->counters.failed_conns),
2903 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002904 sv->counters.cli_aborts,
2905 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002906 sv->counters.retries, sv->counters.redispatches);
2907
2908 /* status, lest check */
2909 chunk_appendf(&trash, "<td class=ac>");
2910
Willy Tarreau20125212014-05-13 19:44:56 +02002911 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002912 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2913 chunk_appendf(&trash, "MAINT");
2914 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002915 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002916 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2917 chunk_appendf(&trash,
2918 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02002919 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2920 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002921 }
Willy Tarreau91861262007-10-17 17:06:05 +02002922
Willy Tarreauff5ae352013-12-11 20:36:34 +01002923 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002924 chunk_appendf(&trash,
2925 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002926 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002927 get_check_status_info(sv->check.status));
2928
2929 if (sv->check.status >= HCHK_STATUS_L57DATA)
2930 chunk_appendf(&trash, "/%d", sv->check.code);
2931
2932 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002933 chunk_appendf(&trash, " in %lums", sv->check.duration);
2934
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002935 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002936 get_check_status_description(sv->check.status));
2937 if (*sv->check.desc) {
2938 chunk_appendf(&trash, ": ");
2939 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2940 chunk_htmlencode(&trash, &src);
2941 }
2942 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002943 }
2944 else
2945 chunk_appendf(&trash, "</td><td>");
2946
2947 chunk_appendf(&trash,
2948 /* weight */
2949 "</td><td class=ac>%d</td>"
2950 /* act, bck */
2951 "<td class=ac>%s</td><td class=ac>%s</td>"
2952 "",
2953 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02002954 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
2955 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002956
2957 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002958 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002959 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002960
2961 if (ref->observe)
2962 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2963
2964 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002965 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002966 "<td>%lld</td><td>%s</td>"
2967 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002968 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002969 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2970 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002971 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
2972 /* tracking a server */
2973 chunk_appendf(&trash,
2974 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02002975 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01002976 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002977 else
2978 chunk_appendf(&trash, "<td colspan=3></td>");
2979
2980 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02002981 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01002982 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002983 else
2984 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2985 }
2986 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002987 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002988 "DOWN,",
2989 "DOWN %d/%d,",
2990 "UP %d/%d,",
2991 "UP,",
2992 "NOLB %d/%d,",
2993 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002994 "DRAIN %d/%d,",
2995 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002996 "no check,"
2997 };
2998
2999 chunk_appendf(&trash,
3000 /* pxid, name */
3001 "%s,%s,"
3002 /* queue : current, max */
3003 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003004 /* sessions : current, max, limit, total */
3005 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003006 /* bytes : in, out */
3007 "%lld,%lld,"
3008 /* denied: req, resp */
3009 ",%lld,"
3010 /* errors : request, connect, response */
3011 ",%lld,%lld,"
3012 /* warnings: retries, redispatches */
3013 "%lld,%lld,"
3014 "",
3015 px->id, sv->id,
3016 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003017 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003018 sv->counters.bytes_in, sv->counters.bytes_out,
3019 sv->counters.failed_secu,
3020 sv->counters.failed_conns, sv->counters.failed_resp,
3021 sv->counters.retries, sv->counters.redispatches);
3022
3023 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003024 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003025 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003026 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003027 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003028 else
3029 chunk_appendf(&trash,
3030 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003031 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3032 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003033
3034 chunk_appendf(&trash,
3035 /* weight, active, backup */
3036 "%d,%d,%d,"
3037 "",
3038 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003039 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3040 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003041
3042 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003043 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003044 chunk_appendf(&trash,
3045 "%lld,%lld,%d,%d,",
3046 sv->counters.failed_checks, sv->counters.down_trans,
3047 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3048 else
3049 chunk_appendf(&trash, ",,,,");
3050
3051 /* queue limit, pid, iid, sid, */
3052 chunk_appendf(&trash,
3053 "%s,"
3054 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003055 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003056 relative_pid, px->uuid, sv->puid);
3057
3058 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003059 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003060 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003061
3062 /* sessions: lbtot */
3063 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3064
3065 /* tracked */
3066 if (sv->track)
3067 chunk_appendf(&trash, "%s/%s,",
3068 sv->track->proxy->id, sv->track->id);
3069 else
3070 chunk_appendf(&trash, ",");
3071
3072 /* type */
3073 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3074
3075 /* rate */
3076 chunk_appendf(&trash, "%u,,%u,",
3077 read_freq_ctr(&sv->sess_per_sec),
3078 sv->counters.sps_max);
3079
Willy Tarreauff5ae352013-12-11 20:36:34 +01003080 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003081 /* check_status */
3082 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3083
3084 /* check_code */
3085 if (sv->check.status >= HCHK_STATUS_L57DATA)
3086 chunk_appendf(&trash, "%u,", sv->check.code);
3087 else
3088 chunk_appendf(&trash, ",");
3089
3090 /* check_duration */
3091 if (sv->check.status >= HCHK_STATUS_CHECKED)
3092 chunk_appendf(&trash, "%lu,", sv->check.duration);
3093 else
3094 chunk_appendf(&trash, ",");
3095
3096 }
3097 else
3098 chunk_appendf(&trash, ",,,");
3099
3100 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3101 if (px->mode == PR_MODE_HTTP) {
3102 for (i=1; i<6; i++)
3103 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3104
3105 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3106 }
3107 else
3108 chunk_appendf(&trash, ",,,,,,");
3109
3110 /* failed health analyses */
3111 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3112
3113 /* requests : req_rate, req_rate_max, req_tot, */
3114 chunk_appendf(&trash, ",,,");
3115
3116 /* errors: cli_aborts, srv_aborts */
3117 chunk_appendf(&trash, "%lld,%lld,",
3118 sv->counters.cli_aborts, sv->counters.srv_aborts);
3119
3120 /* compression: in, out, bypassed, comp_rsp */
3121 chunk_appendf(&trash, ",,,,");
3122
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003123 /* lastsess */
3124 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3125
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003126 /* finish with EOL */
3127 chunk_appendf(&trash, "\n");
3128 }
3129 return 1;
3130}
3131
3132/* Dumps a line for backend <px> to the trash for and uses the state from stream
3133 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3134 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3135 */
3136static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3137{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003138 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003139 struct chunk src;
3140 int i;
3141
3142 if (!(px->cap & PR_CAP_BE))
3143 return 0;
3144
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003145 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003146 return 0;
3147
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003148 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003149 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003150 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003151 /* Column sub-heading for Enable or Disable server */
3152 chunk_appendf(&trash, "<td></td>");
3153 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003154 chunk_appendf(&trash,
3155 "<td class=ac>"
3156 /* name */
3157 "%s<a name=\"%s/Backend\"></a>"
3158 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3159 "",
3160 (flags & ST_SHLGNDS)?"<u>":"",
3161 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003162
3163 if (flags & ST_SHLGNDS) {
3164 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003165 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003166 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3167
3168 /* cookie */
3169 if (px->cookie_name) {
3170 chunk_appendf(&trash, ", cookie: '");
3171 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3172 chunk_htmlencode(&trash, &src);
3173 chunk_appendf(&trash, "'");
3174 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003175 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003176 }
3177
3178 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003179 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003180 /* queue : current, max */
3181 "<td>%s</td><td>%s</td><td></td>"
3182 /* sessions rate : current, max, limit */
3183 "<td>%s</td><td>%s</td><td></td>"
3184 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003185 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003186 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3187 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003188
3189 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003190 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003191 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003192 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003193 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003194 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003195 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3196 U2H(px->be_counters.cum_conn),
3197 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003198
Willy Tarreau466c9b52012-12-23 02:25:03 +01003199 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003200 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003201 chunk_appendf(&trash,
3202 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3203 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3204 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3205 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3206 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3207 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3208 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3209 "<tr><th>- other responses:</th><td>%s</td></tr>"
3210 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3211 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003212 U2H(px->be_counters.p.http.cum_req),
3213 U2H(px->be_counters.p.http.rsp[1]),
3214 U2H(px->be_counters.p.http.rsp[2]),
3215 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003216 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003217 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003218 U2H(px->be_counters.p.http.rsp[3]),
3219 U2H(px->be_counters.p.http.rsp[4]),
3220 U2H(px->be_counters.p.http.rsp[5]),
3221 U2H(px->be_counters.p.http.rsp[0]),
3222 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003223 }
3224
3225 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003226 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003227 /* sessions: lbtot, last */
3228 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003229 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003230 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003231 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003232 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003233 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003234 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003235
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003236 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003237 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003238 "<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 +01003239 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003240 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003241 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3242 px->be_counters.comp_in ?
3243 (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 +01003244 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3245
3246 chunk_appendf(&trash,
3247 /* denied: req, resp */
3248 "<td>%s</td><td>%s</td>"
3249 /* errors : request, connect */
3250 "<td></td><td>%s</td>"
3251 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003252 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003253 /* warnings: retries, redispatches */
3254 "<td>%lld</td><td>%lld</td>"
3255 /* backend status: reflect backend status (up/down): we display UP
3256 * if the backend has known working servers or if it has no server at
3257 * all (eg: for stats). Then we display the total weight, number of
3258 * active and backups. */
3259 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3260 "<td class=ac>%d</td><td class=ac>%d</td>"
3261 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003262 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3263 U2H(px->be_counters.failed_conns),
3264 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003265 px->be_counters.cli_aborts,
3266 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003267 px->be_counters.retries, px->be_counters.redispatches,
3268 human_time(now.tv_sec - px->last_change, 1),
3269 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3270 "<font color=\"red\"><b>DOWN</b></font>",
3271 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3272 px->srv_act, px->srv_bck);
3273
3274 chunk_appendf(&trash,
3275 /* rest of backend: nothing, down transitions, total downtime, throttle */
3276 "<td class=ac>&nbsp;</td><td>%d</td>"
3277 "<td>%s</td>"
3278 "<td></td>"
3279 "</tr>",
3280 px->down_trans,
3281 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3282 }
3283 else { /* CSV mode */
3284 chunk_appendf(&trash,
3285 /* pxid, name */
3286 "%s,BACKEND,"
3287 /* queue : current, max */
3288 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003289 /* sessions : current, max, limit, total */
3290 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003291 /* bytes : in, out */
3292 "%lld,%lld,"
3293 /* denied: req, resp */
3294 "%lld,%lld,"
3295 /* errors : request, connect, response */
3296 ",%lld,%lld,"
3297 /* warnings: retries, redispatches */
3298 "%lld,%lld,"
3299 /* backend status: reflect backend status (up/down): we display UP
3300 * if the backend has known working servers or if it has no server at
3301 * all (eg: for stats). Then we display the total weight, number of
3302 * active and backups. */
3303 "%s,"
3304 "%d,%d,%d,"
3305 /* rest of backend: nothing, down transitions, last change, total downtime */
3306 ",%d,%d,%d,,"
3307 /* pid, iid, sid, throttle, lbtot, tracked, type */
3308 "%d,%d,0,,%lld,,%d,"
3309 /* rate, rate_lim, rate_max, */
3310 "%u,,%u,"
3311 /* check_status, check_code, check_duration */
3312 ",,,",
3313 px->id,
3314 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3315 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3316 px->be_counters.bytes_in, px->be_counters.bytes_out,
3317 px->be_counters.denied_req, px->be_counters.denied_resp,
3318 px->be_counters.failed_conns, px->be_counters.failed_resp,
3319 px->be_counters.retries, px->be_counters.redispatches,
3320 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3321 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3322 px->srv_act, px->srv_bck,
3323 px->down_trans, (int)(now.tv_sec - px->last_change),
3324 px->srv?be_downtime(px):0,
3325 relative_pid, px->uuid,
3326 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3327 read_freq_ctr(&px->be_sess_per_sec),
3328 px->be_counters.sps_max);
3329
3330 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3331 if (px->mode == PR_MODE_HTTP) {
3332 for (i=1; i<6; i++)
3333 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3334 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3335 }
3336 else
3337 chunk_appendf(&trash, ",,,,,,");
3338
3339 /* failed health analyses */
3340 chunk_appendf(&trash, ",");
3341
3342 /* requests : req_rate, req_rate_max, req_tot, */
3343 chunk_appendf(&trash, ",,,");
3344
3345 /* errors: cli_aborts, srv_aborts */
3346 chunk_appendf(&trash, "%lld,%lld,",
3347 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3348
3349 /* compression: in, out, bypassed */
3350 chunk_appendf(&trash, "%lld,%lld,%lld,",
3351 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3352
3353 /* compression: comp_rsp */
3354 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3355
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003356 /* lastsess */
3357 chunk_appendf(&trash, "%d,", be_lastsession(px));
3358
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003359 /* finish with EOL */
3360 chunk_appendf(&trash, "\n");
3361 }
3362 return 1;
3363}
3364
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003365/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003366 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003367 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003368 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003369static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003370{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003371 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003372 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3373
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003374 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003375 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003376
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003377 /* 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 +02003378 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003379 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003380 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003381 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3382 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003383 }
3384
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003385 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003386 "<form action=\"%s%s%s%s\" method=\"post\">",
3387 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003388 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3389 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003390 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003391 }
3392
3393 /* print a new table */
3394 chunk_appendf(&trash,
3395 "<table class=\"tbl\" width=\"100%%\">\n"
3396 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003397 "<th class=\"pxname\" width=\"10%%\">");
3398
3399 chunk_appendf(&trash,
3400 "<a name=\"%s\"></a>%s"
3401 "<a class=px href=\"#%s\">%s</a>",
3402 px->id,
3403 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3404 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405
3406 if (uri->flags & ST_SHLGNDS) {
3407 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003408 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003409 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3410 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003411 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003412 }
3413
3414 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003415 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003416 "<th class=\"%s\" width=\"90%%\">%s</th>"
3417 "</tr>\n"
3418 "</table>\n"
3419 "<table class=\"tbl\" width=\"100%%\">\n"
3420 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003421 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3422 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3423
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003424 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003425 /* Column heading for Enable or Disable server */
3426 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003427 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428
3429 chunk_appendf(&trash,
3430 "<th rowspan=2></th>"
3431 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003432 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003433 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3434 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3435 "<th colspan=9>Server</th>"
3436 "</tr>\n"
3437 "<tr class=\"titre\">"
3438 "<th>Cur</th><th>Max</th><th>Limit</th>"
3439 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003440 "<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 +01003441 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3442 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3443 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3444 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3445 "<th>Thrtle</th>\n"
3446 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003447}
3448
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003449/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003450 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003451 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003452static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003453{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003454 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003455 chunk_appendf(&trash, "</table>");
3456
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003457 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003458 /* close the form used to enable/disable this proxy servers */
3459 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003460 "Choose the action to perform on the checked servers : "
3461 "<select name=action>"
3462 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003463 "<option value=\"ready\">Set state to READY</option>"
3464 "<option value=\"drain\">Set state to DRAIN</option>"
3465 "<option value=\"maint\">set state to MAINT</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003466 "<option value=\"shutdown\">Kill Sessions</option>"
3467 "</select>"
3468 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3469 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3470 "</form>",
3471 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003472 }
3473
3474 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003475}
Willy Tarreau91861262007-10-17 17:06:05 +02003476
3477/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003478 * Dumps statistics for a proxy. The output is sent to the stream interface's
3479 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3480 * buffer space, or non-zero if everything completed. This function is used
3481 * both by the CLI and the HTTP entry points, and is able to dump the output
3482 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003483 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003484static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003485{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003486 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003487 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003488 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003489 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003490 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003491
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003492 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003493
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003494 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003495 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003496 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003497 if (uri && uri->scope) {
3498 /* we have a limited scope, we have to check the proxy name */
3499 struct stat_scope *scope;
3500 int len;
3501
3502 len = strlen(px->id);
3503 scope = uri->scope;
3504
3505 while (scope) {
3506 /* match exact proxy name */
3507 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3508 break;
3509
3510 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003511 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003512 break;
3513 scope = scope->next;
3514 }
3515
3516 /* proxy name not found : don't dump anything */
3517 if (scope == NULL)
3518 return 1;
3519 }
3520
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003521 /* if the user has requested a limited output and the proxy
3522 * name does not match, skip it.
3523 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003524 if (appctx->ctx.stats.scope_len &&
3525 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 +02003526 return 1;
3527
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003528 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3529 (appctx->ctx.stats.iid != -1) &&
3530 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003531 return 1;
3532
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003533 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003534 /* fall through */
3535
Willy Tarreau295a8372011-03-10 11:25:07 +01003536 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003537 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003538 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003539 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003540 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003541 }
Willy Tarreau91861262007-10-17 17:06:05 +02003542
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003543 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003544 /* fall through */
3545
Willy Tarreau295a8372011-03-10 11:25:07 +01003546 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003547 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003548 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003549 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003550 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003551
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003552 appctx->ctx.stats.l = px->conf.listeners.n;
3553 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003554 /* fall through */
3555
Willy Tarreau295a8372011-03-10 11:25:07 +01003556 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003557 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003558 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003559 if (buffer_almost_full(rep->buf)) {
3560 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003561 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003562 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003563
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003564 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003565 if (!l->counters)
3566 continue;
3567
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003568 if (appctx->ctx.stats.flags & STAT_BOUND) {
3569 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003570 break;
3571
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003572 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003573 continue;
3574 }
3575
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003576 /* print the frontend */
3577 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3578 if (bi_putchk(rep, &trash) == -1)
3579 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003580 }
3581
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003582 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3583 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003584 /* fall through */
3585
Willy Tarreau295a8372011-03-10 11:25:07 +01003586 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003587 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003588 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003589 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 +02003590
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003591 if (buffer_almost_full(rep->buf)) {
3592 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003593 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003594 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003595
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003596 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003597
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003598 if (appctx->ctx.stats.flags & STAT_BOUND) {
3599 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003600 break;
3601
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003602 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003603 continue;
3604 }
3605
Willy Tarreau32091232014-05-16 13:52:00 +02003606 svs = sv;
3607 while (svs->track)
3608 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003609
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003610 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
3611 /* server is UP. The possibilities are :
3612 * - UP, draining, going down => state = 6
3613 * - UP, going down => state = 2
3614 * - UP, draining => state = 7
3615 * - UP, checked => state = 3
3616 * - UP, not checked nor tracked => state = 8
3617 */
3618
3619 if ((svs->check.state & CHK_ST_ENABLED) &&
3620 (svs->check.health < svs->check.rise + svs->check.fall - 1))
3621 sv_state = 2;
Willy Tarreau91861262007-10-17 17:06:05 +02003622 else
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003623 sv_state = 3;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003624
Willy Tarreauefe28222014-05-21 17:13:13 +02003625 if (server_is_draining(sv))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003626 sv_state += 4;
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003627
3628 if (sv_state == 3 && !(svs->check.state & CHK_ST_ENABLED))
3629 sv_state = 8; /* unchecked UP */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003630 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003631 else if (sv->state == SRV_ST_STOPPING) {
3632 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
3633 (svs->check.health == svs->check.rise + svs->check.fall - 1))
3634 sv_state = 5; /* NOLB */
Willy Tarreau91861262007-10-17 17:06:05 +02003635 else
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003636 sv_state = 4; /* NOLB going down */
3637 }
3638 else { /* stopped */
3639 if ((svs->check.state & CHK_ST_ENABLED && !svs->check.health) ||
3640 (svs->agent.state & CHK_ST_ENABLED && !svs->agent.health))
Willy Tarreau91861262007-10-17 17:06:05 +02003641 sv_state = 0; /* DOWN */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003642 else
3643 sv_state = 1; /* going up */
3644 }
Willy Tarreau91861262007-10-17 17:06:05 +02003645
Willy Tarreau20125212014-05-13 19:44:56 +02003646 if (((sv_state == 0) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003647 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003648 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003649 continue;
3650 }
3651
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003652 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3653 if (bi_putchk(rep, &trash) == -1)
3654 return 0;
3655 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003656
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003657 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003658 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003659
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003660 case STAT_PX_ST_BE:
3661 /* print the backend */
3662 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3663 if (bi_putchk(rep, &trash) == -1)
3664 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003665
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003666 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003667 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003668
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003669 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003670 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003671 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003672 if (bi_putchk(rep, &trash) == -1)
3673 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003674 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003675
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003676 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003677 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003678
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003679 case STAT_PX_ST_FIN:
3680 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003681
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003682 default:
3683 /* unknown state, we should put an abort() here ! */
3684 return 1;
3685 }
3686}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003687
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003688/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3689 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003690 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003691static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003692{
3693 /* WARNING! This must fit in the first buffer !!! */
3694 chunk_appendf(&trash,
3695 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3696 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3697 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3698 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3699 "<style type=\"text/css\"><!--\n"
3700 "body {"
3701 " font-family: arial, helvetica, sans-serif;"
3702 " font-size: 12px;"
3703 " font-weight: normal;"
3704 " color: black;"
3705 " background: white;"
3706 "}\n"
3707 "th,td {"
3708 " font-size: 10px;"
3709 "}\n"
3710 "h1 {"
3711 " font-size: x-large;"
3712 " margin-bottom: 0.5em;"
3713 "}\n"
3714 "h2 {"
3715 " font-family: helvetica, arial;"
3716 " font-size: x-large;"
3717 " font-weight: bold;"
3718 " font-style: italic;"
3719 " color: #6020a0;"
3720 " margin-top: 0em;"
3721 " margin-bottom: 0em;"
3722 "}\n"
3723 "h3 {"
3724 " font-family: helvetica, arial;"
3725 " font-size: 16px;"
3726 " font-weight: bold;"
3727 " color: #b00040;"
3728 " background: #e8e8d0;"
3729 " margin-top: 0em;"
3730 " margin-bottom: 0em;"
3731 "}\n"
3732 "li {"
3733 " margin-top: 0.25em;"
3734 " margin-right: 2em;"
3735 "}\n"
3736 ".hr {margin-top: 0.25em;"
3737 " border-color: black;"
3738 " border-bottom-style: solid;"
3739 "}\n"
3740 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3741 ".total {background: #20D0D0;color: #ffff80;}\n"
3742 ".frontend {background: #e8e8d0;}\n"
3743 ".socket {background: #d0d0d0;}\n"
3744 ".backend {background: #e8e8d0;}\n"
3745 ".active0 {background: #ff9090;}\n"
3746 ".active1 {background: #ffd020;}\n"
3747 ".active2 {background: #ffffa0;}\n"
3748 ".active3 {background: #c0ffc0;}\n"
3749 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003750 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3751 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3752 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003753 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003754 ".backup0 {background: #ff9090;}\n"
3755 ".backup1 {background: #ff80ff;}\n"
3756 ".backup2 {background: #c060ff;}\n"
3757 ".backup3 {background: #b0d0ff;}\n"
3758 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3759 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003760 ".backup6 {background: #c060ff;}\n"
3761 ".backup7 {background: #cc9900;}\n"
3762 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003763 ".maintain {background: #c07820;}\n"
3764 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3765 "\n"
3766 "a.px:link {color: #ffff40; text-decoration: none;}"
3767 "a.px:visited {color: #ffff40; text-decoration: none;}"
3768 "a.px:hover {color: #ffffff; text-decoration: none;}"
3769 "a.lfsb:link {color: #000000; text-decoration: none;}"
3770 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3771 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3772 "\n"
3773 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3774 "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"
3775 "table.tbl td.ac { text-align: center;}\n"
3776 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3777 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3778 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3779 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3780 "\n"
3781 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3782 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3783 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003784 "table.det { border-collapse: collapse; border-style: none; }\n"
3785 "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 +01003786 "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 +01003787 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003788 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003789 " display:block;\n"
3790 " visibility:hidden;\n"
3791 " z-index:2147483647;\n"
3792 " position:absolute;\n"
3793 " padding:2px 4px 3px;\n"
3794 " background:#f0f060; color:#000000;\n"
3795 " border:1px solid #7040c0;\n"
3796 " white-space:nowrap;\n"
3797 " font-style:normal;font-size:11px;font-weight:normal;\n"
3798 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3799 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3800 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003801 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003802 "-->\n"
3803 "</style></head>\n",
3804 (uri->flags & ST_SHNODE) ? " on " : "",
3805 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3806 );
3807}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003808
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003809/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003810 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003811 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003812 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003813static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003814{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003815 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003816 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003817 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003818
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003819 /* WARNING! this has to fit the first packet too.
3820 * We are around 3.5 kB, add adding entries will
3821 * become tricky if we want to support 4kB buffers !
3822 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003823 chunk_appendf(&trash,
3824 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3825 PRODUCT_NAME "%s</a></h1>\n"
3826 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3827 "<hr width=\"100%%\" class=\"hr\">\n"
3828 "<h3>&gt; General process information</h3>\n"
3829 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3830 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3831 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3832 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3833 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3834 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3835 "Running tasks: %d/%d; idle = %d %%<br>\n"
3836 "</td><td align=\"center\" nowrap>\n"
3837 "<table class=\"lgd\"><tr>\n"
3838 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3839 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3840 "</tr><tr>\n"
3841 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3842 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3843 "</tr><tr>\n"
3844 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3845 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3846 "</tr><tr>\n"
3847 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003848 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003849 "</tr><tr>\n"
3850 "<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 -07003851 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003852 "<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 +01003853 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003854 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003855 "</td>"
3856 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3857 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3858 "",
3859 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3860 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3861 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3862 (uri->flags & ST_SHDESC) ? ": " : "",
3863 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3864 pid, relative_pid, global.nbproc,
3865 up / 86400, (up % 86400) / 3600,
3866 (up % 3600) / 60, (up % 60),
3867 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3868 global.rlimit_memmax ? " MB" : "",
3869 global.rlimit_nofile,
3870 global.maxsock, global.maxconn, global.maxpipes,
3871 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3872 run_queue_cur, nb_tasks_cur, idle_pct
3873 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003874
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003875 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3876 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3877 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003878
3879 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003880 "<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 +02003881 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003882 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3883 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3884 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003885 STAT_SCOPE_TXT_MAXLEN);
3886
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003887 /* 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 +02003888 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003889 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003890 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003891 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3892 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003893 }
3894
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003895 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003896 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003897 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003898 uri->uri_prefix,
3899 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003900 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003901 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003902 else
3903 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003904 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003905 uri->uri_prefix,
3906 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003907 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003908 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003909
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003910 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003911 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003912 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003913 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003914 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003915 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003916 "",
3917 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003918 else
3919 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003920 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003921 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003922 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003923 ";norefresh",
3924 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003925 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003926
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003927 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003928 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003929 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003930 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3931 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003932 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003933
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003934 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003935 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003936 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003937 (uri->refresh > 0) ? ";norefresh" : "",
3938 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003939
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003940 chunk_appendf(&trash,
3941 "</ul></td>"
3942 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3943 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3944 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3945 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3946 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3947 "</ul>"
3948 "</td>"
3949 "</tr></table>\n"
3950 ""
3951 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003952
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003953 if (appctx->ctx.stats.st_code) {
3954 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003955 case STAT_STATUS_DONE:
3956 chunk_appendf(&trash,
3957 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003958 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003959 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003960 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003961 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3962 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003963 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003964 break;
3965 case STAT_STATUS_NONE:
3966 chunk_appendf(&trash,
3967 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003968 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003969 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003970 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003971 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3972 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003973 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003974 break;
3975 case STAT_STATUS_PART:
3976 chunk_appendf(&trash,
3977 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003978 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003979 "Action partially processed.<br>"
3980 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003981 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003982 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3983 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003984 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003985 break;
3986 case STAT_STATUS_ERRP:
3987 chunk_appendf(&trash,
3988 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003989 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003990 "Action not processed because of invalid parameters."
3991 "<ul>"
3992 "<li>The action is maybe unknown.</li>"
3993 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3994 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3995 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003996 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003997 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3998 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003999 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004000 break;
4001 case STAT_STATUS_EXCD:
4002 chunk_appendf(&trash,
4003 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004004 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004005 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4006 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004007 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004008 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4009 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004010 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004011 break;
4012 case STAT_STATUS_DENY:
4013 chunk_appendf(&trash,
4014 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004015 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004016 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004017 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004018 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4019 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004020 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004021 break;
4022 default:
4023 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09004024 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004025 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004026 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004027 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004028 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4029 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004030 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004031 }
4032 chunk_appendf(&trash, "<p>\n");
4033 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004034}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004035
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004036/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4037 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004038 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004039static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004040{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004041 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004042}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004043
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004044/* This function dumps statistics onto the stream interface's read buffer in
4045 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004046 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4047 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
4048 * and the session must be closed, or -1 in case of any error. This function is
4049 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004050 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004051static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004052{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004053 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004054 struct channel *rep = si->ib;
4055 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004056
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004057 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004058
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004059 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004060 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004061 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004062 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004063
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004064 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004065 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004066 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004067 else
4068 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004069
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004070 if (bi_putchk(rep, &trash) == -1)
4071 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004072
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004073 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004074 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004075
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004076 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004077 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004078 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004079 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004080 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004081 }
Willy Tarreau91861262007-10-17 17:06:05 +02004082
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004083 appctx->ctx.stats.px = proxy;
4084 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4085 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004086 /* fall through */
4087
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004088 case STAT_ST_LIST:
4089 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004090 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004091 if (buffer_almost_full(rep->buf)) {
4092 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004093 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004094 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004095
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004096 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004097 /* skip the disabled proxies, global frontend and non-networked ones */
4098 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004099 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004100 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004101
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004102 appctx->ctx.stats.px = px->next;
4103 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004104 }
4105 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004106
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004107 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004108 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004109
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004110 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004111 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004112 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004113 if (bi_putchk(rep, &trash) == -1)
4114 return 0;
4115 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004116
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004117 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004118 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004119
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004120 case STAT_ST_FIN:
4121 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004122
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004123 default:
4124 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004125 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004126 return -1;
4127 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004128}
Willy Tarreauae526782010-03-04 20:34:23 +01004129
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004130/* We reached the stats page through a POST request. The appctx is
4131 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004132 * Parse the posted data and enable/disable servers if necessary.
4133 * Returns 1 if request was parsed or zero if it needs more data.
4134 */
4135static int stats_process_http_post(struct stream_interface *si)
4136{
4137 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004138 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004139
4140 struct proxy *px = NULL;
4141 struct server *sv = NULL;
4142
4143 char key[LINESIZE];
4144 int action = ST_ADM_ACTION_NONE;
4145 int reprocess = 0;
4146
4147 int total_servers = 0;
4148 int altered_servers = 0;
4149
4150 char *first_param, *cur_param, *next_param, *end_params;
4151 char *st_cur_param = NULL;
4152 char *st_next_param = NULL;
4153
4154 struct chunk *temp;
4155 int reql;
4156
4157 temp = get_trash_chunk();
4158 if (temp->size < s->txn.req.body_len) {
4159 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004160 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004161 goto out;
4162 }
4163
4164 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4165 if (reql <= 0) {
4166 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004167 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004168 return 0;
4169 }
4170
4171 first_param = temp->str;
4172 end_params = temp->str + reql;
4173 cur_param = next_param = end_params;
4174 *end_params = '\0';
4175
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004176 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004177
4178 /*
4179 * Parse the parameters in reverse order to only store the last value.
4180 * From the html form, the backend and the action are at the end.
4181 */
4182 while (cur_param > first_param) {
4183 char *value;
4184 int poffset, plen;
4185
4186 cur_param--;
4187
4188 if ((*cur_param == '&') || (cur_param == first_param)) {
4189 reprocess_servers:
4190 /* Parse the key */
4191 poffset = (cur_param != first_param ? 1 : 0);
4192 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4193 if ((plen > 0) && (plen <= sizeof(key))) {
4194 strncpy(key, cur_param + poffset, plen);
4195 key[plen - 1] = '\0';
4196 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004197 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004198 goto out;
4199 }
4200
4201 /* Parse the value */
4202 value = key;
4203 while (*value != '\0' && *value != '=') {
4204 value++;
4205 }
4206 if (*value == '=') {
4207 /* Ok, a value is found, we can mark the end of the key */
4208 *value++ = '\0';
4209 }
4210 if (url_decode(key) < 0 || url_decode(value) < 0)
4211 break;
4212
4213 /* Now we can check the key to see what to do */
4214 if (!px && (strcmp(key, "b") == 0)) {
4215 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4216 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004217 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004218 goto out;
4219 }
4220 }
4221 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004222 if (strcmp(value, "ready") == 0) {
4223 action = ST_ADM_ACTION_READY;
4224 }
4225 else if (strcmp(value, "drain") == 0) {
4226 action = ST_ADM_ACTION_DRAIN;
4227 }
4228 else if (strcmp(value, "maint") == 0) {
4229 action = ST_ADM_ACTION_MAINT;
4230 }
4231 else if (strcmp(value, "shutdown") == 0) {
4232 action = ST_ADM_ACTION_SHUTDOWN;
4233 }
4234 /* else these are the old supported methods */
4235 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004236 action = ST_ADM_ACTION_DISABLE;
4237 }
4238 else if (strcmp(value, "enable") == 0) {
4239 action = ST_ADM_ACTION_ENABLE;
4240 }
4241 else if (strcmp(value, "stop") == 0) {
4242 action = ST_ADM_ACTION_STOP;
4243 }
4244 else if (strcmp(value, "start") == 0) {
4245 action = ST_ADM_ACTION_START;
4246 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004247 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004248 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004249 goto out;
4250 }
4251 }
4252 else if (strcmp(key, "s") == 0) {
4253 if (!(px && action)) {
4254 /*
4255 * Indicates that we'll need to reprocess the parameters
4256 * as soon as backend and action are known
4257 */
4258 if (!reprocess) {
4259 st_cur_param = cur_param;
4260 st_next_param = next_param;
4261 }
4262 reprocess = 1;
4263 }
4264 else if ((sv = findserver(px, value)) != NULL) {
4265 switch (action) {
4266 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004267 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004268 altered_servers++;
4269 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004270 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004271 }
4272 break;
4273 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004274 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004275 altered_servers++;
4276 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004277 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004278 }
4279 break;
4280 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004281 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4282 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4283 altered_servers++;
4284 total_servers++;
4285 }
4286 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004287 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004288 if (sv->admin & SRV_ADMF_FDRAIN) {
4289 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4290 altered_servers++;
4291 total_servers++;
4292 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004293 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004294 case ST_ADM_ACTION_READY:
4295 srv_adm_set_ready(sv);
4296 altered_servers++;
4297 total_servers++;
4298 break;
4299 case ST_ADM_ACTION_DRAIN:
4300 srv_adm_set_drain(sv);
4301 altered_servers++;
4302 total_servers++;
4303 break;
4304 case ST_ADM_ACTION_MAINT:
4305 srv_adm_set_maint(sv);
4306 altered_servers++;
4307 total_servers++;
4308 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004309 case ST_ADM_ACTION_SHUTDOWN:
4310 if (px->state != PR_STSTOPPED) {
4311 struct session *sess, *sess_bck;
4312
4313 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4314 if (sess->srv_conn == sv)
4315 session_shutdown(sess, SN_ERR_KILLED);
4316
4317 altered_servers++;
4318 total_servers++;
4319 }
4320 break;
4321 }
4322 } else {
4323 /* the server name is unknown or ambiguous (duplicate names) */
4324 total_servers++;
4325 }
4326 }
4327 if (reprocess && px && action) {
4328 /* Now, we know the backend and the action chosen by the user.
4329 * We can safely restart from the first server parameter
4330 * to reprocess them
4331 */
4332 cur_param = st_cur_param;
4333 next_param = st_next_param;
4334 reprocess = 0;
4335 goto reprocess_servers;
4336 }
4337
4338 next_param = cur_param;
4339 }
4340 }
4341
4342 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004343 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004344 }
4345 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004346 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004347 }
4348 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004349 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004350 }
4351 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004352 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004353 }
4354 out:
4355 return 1;
4356}
4357
4358
4359static int stats_send_http_headers(struct stream_interface *si)
4360{
4361 struct session *s = session_from_task(si->owner);
4362 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004363 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004364
4365 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004366 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004367 "Cache-Control: no-cache\r\n"
4368 "Connection: close\r\n"
4369 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004370 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004371
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004372 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004373 chunk_appendf(&trash, "Refresh: %d\r\n",
4374 uri->refresh);
4375
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004376 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4377
4378 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4379 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4380 else
4381 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004382
4383 s->txn.status = 200;
4384 s->logs.tv_request = now;
4385
4386 if (bi_putchk(si->ib, &trash) == -1)
4387 return 0;
4388
4389 return 1;
4390}
4391
4392static int stats_send_http_redirect(struct stream_interface *si)
4393{
4394 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4395 struct session *s = session_from_task(si->owner);
4396 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004397 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004398
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004399 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004400 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004401 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004402 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004403 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4404 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004405 }
4406
4407 /* We don't want to land on the posted stats page because a refresh will
4408 * repost the data. We don't want this to happen on accident so we redirect
4409 * the browse to the stats page with a GET.
4410 */
4411 chunk_printf(&trash,
4412 "HTTP/1.1 303 See Other\r\n"
4413 "Cache-Control: no-cache\r\n"
4414 "Content-Type: text/plain\r\n"
4415 "Connection: close\r\n"
4416 "Location: %s;st=%s%s%s%s\r\n"
4417 "\r\n",
4418 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004419 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4420 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4421 stat_status_codes[appctx->ctx.stats.st_code]) ?
4422 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004423 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004424 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4425 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004426 scope_txt);
4427
4428 s->txn.status = 303;
4429 s->logs.tv_request = now;
4430
4431 if (bi_putchk(si->ib, &trash) == -1)
4432 return 0;
4433
4434 return 1;
4435}
4436
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004437/* This I/O handler runs as an applet embedded in a stream interface. It is
4438 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004439 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004440 * automatically unregisters itself once transfer is complete.
4441 */
4442static void http_stats_io_handler(struct stream_interface *si)
4443{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004444 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004445 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004446 struct channel *req = si->ob;
4447 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004448
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004449 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4450 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004451
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004452 /* check that the output is not closed */
4453 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004454 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004455
Willy Tarreau347a35d2013-11-22 17:51:09 +01004456 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004457 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004458 if (stats_send_http_headers(si)) {
4459 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004460 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004461 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004462 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004463 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004464 }
4465
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004466 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004467 unsigned int prev_len = si->ib->buf->i;
4468 unsigned int data_len;
4469 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004470 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004471
4472 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4473 /* One difficulty we're facing is that we must prevent
4474 * the input data from being automatically forwarded to
4475 * the output area. For this, we temporarily disable
4476 * forwarding on the channel.
4477 */
4478 last_fwd = si->ib->to_forward;
4479 si->ib->to_forward = 0;
4480 chunk_printf(&trash, "\r\n000000\r\n");
4481 if (bi_putchk(si->ib, &trash) == -1) {
4482 si->ib->to_forward = last_fwd;
4483 goto fail;
4484 }
4485 }
4486
4487 data_len = si->ib->buf->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004488 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004489 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004490
4491 last_len = si->ib->buf->i;
4492
4493 /* Now we must either adjust or remove the chunk size. This is
4494 * not easy because the chunk size might wrap at the end of the
4495 * buffer, so we pretend we have nothing in the buffer, we write
4496 * the size, then restore the buffer's contents. Note that we can
4497 * only do that because no forwarding is scheduled on the stats
4498 * applet.
4499 */
4500 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4501 si->ib->total -= (last_len - prev_len);
4502 si->ib->buf->i -= (last_len - prev_len);
4503
4504 if (last_len != data_len) {
4505 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
4506 bi_putchk(si->ib, &trash);
4507
4508 si->ib->total += (last_len - data_len);
4509 si->ib->buf->i += (last_len - data_len);
4510 }
4511 /* now re-enable forwarding */
4512 channel_forward(si->ib, last_fwd);
4513 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004514 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004515
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004516 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004517 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004518 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004519 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004520 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004521 }
4522
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004523 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004524 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004525 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004526 }
4527
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004528 if (appctx->st0 == STAT_HTTP_DONE) {
4529 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4530 chunk_printf(&trash, "\r\n0\r\n\r\n");
4531 if (bi_putchk(si->ib, &trash) == -1)
4532 goto fail;
4533 }
4534 /* eat the whole request */
4535 bo_skip(si->ob, si->ob->buf->o);
4536 res->flags |= CF_READ_NULL;
4537 si_shutr(si);
4538 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004539
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004540 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4541 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004542
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004543 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004544 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4545 si_shutr(si);
4546 res->flags |= CF_READ_NULL;
4547 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004548 }
Willy Tarreau91861262007-10-17 17:06:05 +02004549
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004550 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004551 /* update all other flags and resync with the other side */
4552 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004553
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004554 /* we don't want to expire timeouts while we're processing requests */
4555 si->ib->rex = TICK_ETERNITY;
4556 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004557
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004558 out:
4559 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4560 /* check that we have released everything then unregister */
4561 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004562 }
4563}
4564
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004565
Willy Tarreau909d5172012-11-26 03:04:41 +01004566static inline const char *get_conn_ctrl_name(const struct connection *conn)
4567{
Willy Tarreau3c728722014-01-23 13:50:42 +01004568 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004569 return "NONE";
4570 return conn->ctrl->name;
4571}
4572
4573static inline const char *get_conn_xprt_name(const struct connection *conn)
4574{
4575 static char ptr[17];
4576
Willy Tarreauaad69382014-01-23 14:21:42 +01004577 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004578 return "NONE";
4579
4580 if (conn->xprt == &raw_sock)
4581 return "RAW";
4582
4583#ifdef USE_OPENSSL
4584 if (conn->xprt == &ssl_sock)
4585 return "SSL";
4586#endif
4587 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4588 return ptr;
4589}
4590
4591static inline const char *get_conn_data_name(const struct connection *conn)
4592{
4593 static char ptr[17];
4594
4595 if (!conn->data)
4596 return "NONE";
4597
4598 if (conn->data == &sess_conn_cb)
4599 return "SESS";
4600
4601 if (conn->data == &si_conn_cb)
4602 return "STRM";
4603
4604 if (conn->data == &check_conn_cb)
4605 return "CHCK";
4606
4607 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4608 return ptr;
4609}
4610
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004611/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004612 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004613 * 0 if the output buffer is full and it needs to be called again, otherwise
4614 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004615 */
Willy Tarreau76153662012-11-26 01:16:39 +01004616static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004617{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004618 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004619 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004620 extern const char *monthname[12];
4621 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004622 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004623 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004624
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004625 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004626
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004627 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004628 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004629 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4630 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004631 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004632 appctx->ctx.sess.uid = 0;
4633 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004634 return 1;
4635 }
4636
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004637 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004638 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004639 appctx->ctx.sess.uid = sess->uniq_id;
4640 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004641 /* fall through */
4642
4643 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004644 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004645 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004646 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004647 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004648 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4649 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004650 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004651 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004652
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004653 conn = objt_conn(sess->si[0].end);
4654 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004655 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004656 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004657 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004658 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004659 break;
4660 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004661 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004662 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004663 default:
4664 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004665 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004666 break;
4667 }
4668
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004669 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004670 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004671 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004672
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004673 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004674 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004675 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4676 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4677 sess->listener ? sess->listener->luid : 0);
4678
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004679 if (conn)
4680 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004681
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004682 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004683 case AF_INET:
4684 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004685 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004686 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004687 break;
4688 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004689 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004690 break;
4691 default:
4692 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004693 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004694 break;
4695 }
4696
Willy Tarreau50943332011-09-02 17:33:05 +02004697 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004698 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004699 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004700 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004701 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4702 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004703 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004704
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004705 conn = objt_conn(sess->si[1].end);
4706 if (conn)
4707 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004708
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004709 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004710 case AF_INET:
4711 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004712 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004713 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004714 break;
4715 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004716 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004717 break;
4718 default:
4719 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004720 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004721 break;
4722 }
4723
4724 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004725 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004726 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004727 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4728 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004729 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004730 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004731
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004732 if (conn)
4733 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004734
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004735 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004736 case AF_INET:
4737 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004738 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004739 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004740 break;
4741 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004742 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004743 break;
4744 default:
4745 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004746 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004747 break;
4748 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004749
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004750 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004751 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004752 sess->task,
4753 sess->task->state,
4754 sess->task->nice, sess->task->calls,
4755 sess->task->expire ?
4756 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4757 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4758 TICKS_TO_MS(1000)) : "<NEVER>",
4759 task_in_rq(sess->task) ? ", running" : "");
4760
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004761 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004762 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004763 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4764
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004765 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004766 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4767 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4768 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4769
4770 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004771 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004772 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004773 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004774 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004775 obj_type_name(sess->si[0].end),
4776 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004777 sess->si[0].exp ?
4778 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4779 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4780 TICKS_TO_MS(1000)) : "<NEVER>",
4781 sess->si[0].err_type);
4782
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004783 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004784 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004785 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004786 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004787 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004788 obj_type_name(sess->si[1].end),
4789 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004790 sess->si[1].exp ?
4791 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4792 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4793 TICKS_TO_MS(1000)) : "<NEVER>",
4794 sess->si[1].err_type);
4795
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004796 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004797 chunk_appendf(&trash,
4798 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004799 conn,
4800 get_conn_ctrl_name(conn),
4801 get_conn_xprt_name(conn),
4802 get_conn_data_name(conn),
4803 obj_type_name(conn->target),
4804 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004805
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004806 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004807 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004808 conn->flags,
4809 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004810 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004811 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004812 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004813 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004814 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4815 chunk_appendf(&trash,
4816 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4817 tmpctx,
4818 tmpctx->st0,
4819 tmpctx->st1,
4820 tmpctx->st2,
4821 tmpctx->applet->name);
4822 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004823
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004824 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004825 chunk_appendf(&trash,
4826 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004827 conn,
4828 get_conn_ctrl_name(conn),
4829 get_conn_xprt_name(conn),
4830 get_conn_data_name(conn),
4831 obj_type_name(conn->target),
4832 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004833
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004834 chunk_appendf(&trash,
4835 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004836 conn->flags,
4837 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004838 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004839 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004840 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004841 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004842 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4843 chunk_appendf(&trash,
4844 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4845 tmpctx,
4846 tmpctx->st0,
4847 tmpctx->st1,
4848 tmpctx->st2,
4849 tmpctx->applet->name);
4850 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004851
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004852 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004853 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004854 " an_exp=%s",
4855 sess->req,
4856 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004857 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004858 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004859 sess->req->analyse_exp ?
4860 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4861 TICKS_TO_MS(1000)) : "<NEVER>");
4862
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004863 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004864 " rex=%s",
4865 sess->req->rex ?
4866 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4867 TICKS_TO_MS(1000)) : "<NEVER>");
4868
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004869 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004870 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004871 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004872 sess->req->wex ?
4873 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4874 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004875 sess->req->buf,
4876 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004877 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004878 sess->txn.req.next, sess->req->buf->i,
4879 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004880
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004881 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004882 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004883 " an_exp=%s",
4884 sess->rep,
4885 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004886 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004887 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004888 sess->rep->analyse_exp ?
4889 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4890 TICKS_TO_MS(1000)) : "<NEVER>");
4891
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004892 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004893 " rex=%s",
4894 sess->rep->rex ?
4895 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4896 TICKS_TO_MS(1000)) : "<NEVER>");
4897
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004898 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004899 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004900 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004901 sess->rep->wex ?
4902 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4903 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004904 sess->rep->buf,
4905 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004906 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004907 sess->txn.rsp.next, sess->rep->buf->i,
4908 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004909
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004910 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004911 return 0;
4912
4913 /* use other states to dump the contents */
4914 }
4915 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004916 appctx->ctx.sess.uid = 0;
4917 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004918 return 1;
4919}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004920
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004921static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004922{
4923 struct appctx *appctx = __objt_appctx(si->end);
4924
4925 switch (appctx->st2) {
4926 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004927 /* Display the column headers. If the message cannot be sent,
4928 * quit the fucntion with returning 0. The function is called
4929 * later and restart at the state "STAT_ST_INIT".
4930 */
4931 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01004932 chunk_appendf(&trash, "# id (file) description\n");
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004933 if (bi_putchk(si->ib, &trash) == -1)
4934 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004935
4936 /* Now, we start the browsing of the references lists.
4937 * Note that the following call to LIST_ELEM return bad pointer. The only
4938 * avalaible field of this pointer is <list>. It is used with the function
4939 * pat_list_get_next() for retruning the first avalaible entry
4940 */
4941 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
4942 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
4943 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004944 appctx->st2 = STAT_ST_LIST;
4945 /* fall through */
4946
4947 case STAT_ST_LIST:
4948 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004949 chunk_reset(&trash);
4950
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004951 /* Build messages. If the reference is used by another category than
4952 * the listed categorie, display the information in the massage.
4953 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01004954 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01004955 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
4956 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004957
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004958 if (bi_putchk(si->ib, &trash) == -1) {
4959 /* let's try again later from this session. We add ourselves into
4960 * this session's users so that it can remove us upon termination.
4961 */
4962 return 0;
4963 }
4964
4965 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004966 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
4967 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004968 }
4969
4970 appctx->st2 = STAT_ST_FIN;
4971 /* fall through */
4972
4973 default:
4974 appctx->st2 = STAT_ST_FIN;
4975 return 1;
4976 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004977 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004978}
4979
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004980static int stats_map_lookup(struct stream_interface *si)
4981{
4982 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004983 struct sample sample;
4984 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004985 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004986
4987 switch (appctx->st2) {
4988 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004989 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01004990 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004991 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004992 appctx->st2 = STAT_ST_LIST;
4993 /* fall through */
4994
4995 case STAT_ST_LIST:
4996 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004997 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004998 /* initialise chunk to build new message */
4999 chunk_reset(&trash);
5000
5001 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005002 sample.type = SMP_T_STR;
5003 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005004 sample.data.str.len = appctx->ctx.map.chunk.len;
5005 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005006 if (appctx->ctx.map.expr->pat_head->match &&
5007 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005008 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5009 else
5010 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005011
5012 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005013 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5014 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5015 break;
5016 if (match_method >= PAT_MATCH_NUM)
5017 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5018 else
5019 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005020
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005021 /* case sensitive */
5022 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5023 chunk_appendf(&trash, ", case=insensitive");
5024 else
5025 chunk_appendf(&trash, ", case=sensitive");
5026
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005027 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005028 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005029 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5030 chunk_appendf(&trash, ", found=no");
5031 else
5032 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005033 }
5034
5035 /* Display match and match info */
5036 else {
5037 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005038 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5039 chunk_appendf(&trash, ", found=yes");
5040 else
5041 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005042
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005043 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005044 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005045 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005046 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005047 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005048
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005049 /* display pattern */
5050 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5051 if (pat->ref && pat->ref->pattern)
5052 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5053 else
5054 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005055 }
5056 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005057 if (pat->ref && pat->ref->pattern)
5058 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5059 else
5060 chunk_appendf(&trash, ", pattern=unknown");
5061 }
5062
5063 /* display return value */
5064 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5065 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005066 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005067 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005068 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005069 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005070 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005071 }
5072
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005073 chunk_appendf(&trash, "\n");
5074
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005075 /* display response */
5076 if (bi_putchk(si->ib, &trash) == -1) {
5077 /* let's try again later from this session. We add ourselves into
5078 * this session's users so that it can remove us upon termination.
5079 */
5080 return 0;
5081 }
5082
5083 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005084 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5085 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005086 }
5087
5088 appctx->st2 = STAT_ST_FIN;
5089 /* fall through */
5090
5091 default:
5092 appctx->st2 = STAT_ST_FIN;
5093 free(appctx->ctx.map.chunk.str);
5094 return 1;
5095 }
5096}
5097
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005098static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005099{
5100 struct appctx *appctx = __objt_appctx(si->end);
5101
5102 switch (appctx->st2) {
5103
5104 case STAT_ST_INIT:
5105 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005106 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5107 struct pat_ref_elt *, list);
5108 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5109 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005110 appctx->st2 = STAT_ST_LIST;
5111 /* fall through */
5112
5113 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005114 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005115 chunk_reset(&trash);
5116
5117 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005118 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005119 chunk_appendf(&trash, "%p %s %s\n",
5120 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5121 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005122 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005123 chunk_appendf(&trash, "%p %s\n",
5124 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005125
5126 if (bi_putchk(si->ib, &trash) == -1) {
5127 /* let's try again later from this session. We add ourselves into
5128 * this session's users so that it can remove us upon termination.
5129 */
5130 return 0;
5131 }
5132
5133 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005134 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5135 struct pat_ref_elt *, list);
5136 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005137 break;
5138 }
5139
5140 appctx->st2 = STAT_ST_FIN;
5141 /* fall through */
5142
5143 default:
5144 appctx->st2 = STAT_ST_FIN;
5145 return 1;
5146 }
5147}
5148
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005149/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005150 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005151 * to be called again, otherwise non-zero. It is designed to be called
5152 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005153 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005154static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005155{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005156 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005157 struct connection *conn;
5158
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005159 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005160 /* If we're forced to shut down, we might have to remove our
5161 * reference to the last session being dumped.
5162 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005163 if (appctx->st2 == STAT_ST_LIST) {
5164 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5165 LIST_DEL(&appctx->ctx.sess.bref.users);
5166 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005167 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005168 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005169 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005170 }
5171
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005172 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005173
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005174 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005175 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005176 /* the function had not been called yet, let's prepare the
5177 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005178 * pointer to the first in the global list. When a target
5179 * session is being destroyed, it is responsible for updating
5180 * this pointer. We know we have reached the end when this
5181 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005182 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005183 LIST_INIT(&appctx->ctx.sess.bref.users);
5184 appctx->ctx.sess.bref.ref = sessions.n;
5185 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005186 /* fall through */
5187
Willy Tarreau295a8372011-03-10 11:25:07 +01005188 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005189 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005190 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5191 LIST_DEL(&appctx->ctx.sess.bref.users);
5192 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005193 }
5194
5195 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005196 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005197 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005198 struct session *curr_sess;
5199
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005200 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005201
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005202 if (appctx->ctx.sess.target) {
5203 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005204 goto next_sess;
5205
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005206 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005207 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005208 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005209 return 0;
5210
5211 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005212 LIST_DEL(&appctx->ctx.sess.bref.users);
5213 LIST_INIT(&appctx->ctx.sess.bref.users);
5214 if (appctx->ctx.sess.target != (void *)-1) {
5215 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005216 break;
5217 }
5218 else
5219 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005220 }
5221
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005222 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005223 "%p: proto=%s",
5224 curr_sess,
5225 curr_sess->listener->proto->name);
5226
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005227
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005228 conn = objt_conn(curr_sess->si[0].end);
5229 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005230 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005231 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005232 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005233 " src=%s:%d fe=%s be=%s srv=%s",
5234 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005235 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005236 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005237 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005238 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005239 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005240 break;
5241 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005242 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005243 " src=unix:%d fe=%s be=%s srv=%s",
5244 curr_sess->listener->luid,
5245 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005246 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005247 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005248 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005249 break;
5250 }
5251
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005252 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005253 " ts=%02x age=%s calls=%d",
5254 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005255 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5256 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005257
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005258 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005259 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005260 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005261 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005262 curr_sess->req->analysers,
5263 curr_sess->req->rex ?
5264 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5265 TICKS_TO_MS(1000)) : "");
5266
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005267 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005268 ",wx=%s",
5269 curr_sess->req->wex ?
5270 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5271 TICKS_TO_MS(1000)) : "");
5272
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005273 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005274 ",ax=%s]",
5275 curr_sess->req->analyse_exp ?
5276 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5277 TICKS_TO_MS(1000)) : "");
5278
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005279 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005280 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005281 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005282 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005283 curr_sess->rep->analysers,
5284 curr_sess->rep->rex ?
5285 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5286 TICKS_TO_MS(1000)) : "");
5287
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005288 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005289 ",wx=%s",
5290 curr_sess->rep->wex ?
5291 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5292 TICKS_TO_MS(1000)) : "");
5293
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005294 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005295 ",ax=%s]",
5296 curr_sess->rep->analyse_exp ?
5297 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5298 TICKS_TO_MS(1000)) : "");
5299
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005300 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005301 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005302 " s0=[%d,%1xh,fd=%d,ex=%s]",
5303 curr_sess->si[0].state,
5304 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005305 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005306 curr_sess->si[0].exp ?
5307 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5308 TICKS_TO_MS(1000)) : "");
5309
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005310 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005311 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005312 " s1=[%d,%1xh,fd=%d,ex=%s]",
5313 curr_sess->si[1].state,
5314 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005315 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005316 curr_sess->si[1].exp ?
5317 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5318 TICKS_TO_MS(1000)) : "");
5319
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005320 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005321 " exp=%s",
5322 curr_sess->task->expire ?
5323 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5324 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005325 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005326 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005327
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005328 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005329
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005330 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005331 /* let's try again later from this session. We add ourselves into
5332 * this session's users so that it can remove us upon termination.
5333 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005334 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005335 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005336 }
5337
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005338 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005339 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005340 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005341
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005342 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005343 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005344 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005345 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005346 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005347 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005348
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005349 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005350 return 0;
5351
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005352 appctx->ctx.sess.target = NULL;
5353 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005354 return 1;
5355 }
5356
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005357 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005358 /* fall through */
5359
5360 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005361 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005362 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005363 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005364}
5365
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005366/* This is called when the stream interface is closed. For instance, upon an
5367 * external abort, we won't call the i/o handler anymore so we may need to
5368 * remove back references to the session currently being dumped.
5369 */
Simon Horman74d88312013-02-12 10:45:50 +09005370static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005371{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005372 struct appctx *appctx = __objt_appctx(si->end);
5373
5374 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5375 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5376 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005377 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005378 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5379 free(appctx->ctx.cli.err);
5380 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005381 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5382 free(appctx->ctx.map.chunk.str);
5383 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005384}
5385
Willy Tarreau20e99322013-04-13 09:22:25 +02005386/* This function is used to either dump tables states (when action is set
5387 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005388 * It returns 0 if the output buffer is full and it needs to be called
5389 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005390 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005391static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005392{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005393 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005394 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005395 struct ebmb_node *eb;
5396 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005397 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005398 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005399
5400 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005401 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005402 * - STAT_ST_INIT : the first call
5403 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005404 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005405 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005406 * and the entry pointer points to the next entry to be dumped,
5407 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005408 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005409 * data though.
5410 */
5411
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005412 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005413 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005414 if (appctx->st2 == STAT_ST_LIST) {
5415 appctx->ctx.table.entry->ref_cnt--;
5416 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005417 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005418 return 1;
5419 }
5420
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005421 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005422
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005423 while (appctx->st2 != STAT_ST_FIN) {
5424 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005425 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005426 appctx->ctx.table.proxy = appctx->ctx.table.target;
5427 if (!appctx->ctx.table.proxy)
5428 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005429
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005430 appctx->ctx.table.entry = NULL;
5431 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005432 break;
5433
Willy Tarreau295a8372011-03-10 11:25:07 +01005434 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005435 if (!appctx->ctx.table.proxy ||
5436 (appctx->ctx.table.target &&
5437 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5438 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005439 break;
5440 }
5441
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005442 if (appctx->ctx.table.proxy->table.size) {
5443 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5444 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005445 return 0;
5446
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005447 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005448 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005449 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005450 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005451 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005452 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5453 appctx->ctx.table.entry->ref_cnt++;
5454 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005455 break;
5456 }
5457 }
5458 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005459 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005460 break;
5461
Willy Tarreau295a8372011-03-10 11:25:07 +01005462 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005463 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005464
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005465 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005466 /* we're filtering on some data contents */
5467 void *ptr;
5468 long long data;
5469
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005470 dt = appctx->ctx.table.data_type;
5471 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5472 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005473 dt);
5474
5475 data = 0;
5476 switch (stktable_data_types[dt].std_type) {
5477 case STD_T_SINT:
5478 data = stktable_data_cast(ptr, std_t_sint);
5479 break;
5480 case STD_T_UINT:
5481 data = stktable_data_cast(ptr, std_t_uint);
5482 break;
5483 case STD_T_ULL:
5484 data = stktable_data_cast(ptr, std_t_ull);
5485 break;
5486 case STD_T_FRQP:
5487 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005488 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005489 break;
5490 }
5491
5492 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005493 if ((data < appctx->ctx.table.value &&
5494 (appctx->ctx.table.data_op == STD_OP_EQ ||
5495 appctx->ctx.table.data_op == STD_OP_GT ||
5496 appctx->ctx.table.data_op == STD_OP_GE)) ||
5497 (data == appctx->ctx.table.value &&
5498 (appctx->ctx.table.data_op == STD_OP_NE ||
5499 appctx->ctx.table.data_op == STD_OP_GT ||
5500 appctx->ctx.table.data_op == STD_OP_LT)) ||
5501 (data > appctx->ctx.table.value &&
5502 (appctx->ctx.table.data_op == STD_OP_EQ ||
5503 appctx->ctx.table.data_op == STD_OP_LT ||
5504 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005505 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005506 }
5507
Simon Hormanc88b8872011-06-15 15:18:49 +09005508 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005509 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5510 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005511 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005512
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005513 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005514
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005515 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005516 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005517 struct stksess *old = appctx->ctx.table.entry;
5518 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005519 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005520 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5521 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5522 stksess_kill(&appctx->ctx.table.proxy->table, old);
5523 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005524 break;
5525 }
5526
Simon Hormanc88b8872011-06-15 15:18:49 +09005527
5528 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005529 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5530 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5531 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005532
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005533 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5534 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005535 break;
5536
Willy Tarreau295a8372011-03-10 11:25:07 +01005537 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005538 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005539 break;
5540 }
5541 }
5542 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005543}
5544
Willy Tarreaud426a182010-03-05 14:58:26 +01005545/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005546 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5547 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5548 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5549 * lines are respected within the limit of 70 output chars. Lines that are
5550 * continuation of a previous truncated line begin with "+" instead of " "
5551 * after the offset. The new pointer is returned.
5552 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005553static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5554 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005555{
5556 int end;
5557 unsigned char c;
5558
5559 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005560 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005561 return ptr;
5562
Willy Tarreau77804732012-10-29 16:14:26 +01005563 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005564
Willy Tarreaud426a182010-03-05 14:58:26 +01005565 while (ptr < len && ptr < bsize) {
5566 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005567 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005568 if (out->len > end - 2)
5569 break;
5570 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005571 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005572 if (out->len > end - 3)
5573 break;
5574 out->str[out->len++] = '\\';
5575 switch (c) {
5576 case '\t': c = 't'; break;
5577 case '\n': c = 'n'; break;
5578 case '\r': c = 'r'; break;
5579 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005580 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005581 }
5582 out->str[out->len++] = c;
5583 } else {
5584 if (out->len > end - 5)
5585 break;
5586 out->str[out->len++] = '\\';
5587 out->str[out->len++] = 'x';
5588 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5589 out->str[out->len++] = hextab[c & 0xF];
5590 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005591 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005592 /* we had a line break, let's return now */
5593 out->str[out->len++] = '\n';
5594 *line = ptr;
5595 return ptr;
5596 }
5597 }
5598 /* we have an incomplete line, we return it as-is */
5599 out->str[out->len++] = '\n';
5600 return ptr;
5601}
5602
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005603/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005604 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005605 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005606 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005607static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005608{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005609 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005610 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005611
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005612 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005613 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005614
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005615 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005616
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005617 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005618 /* the function had not been called yet, let's prepare the
5619 * buffer for a response.
5620 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005621 struct tm tm;
5622
5623 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005624 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005625 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5626 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5627 error_snapshot_id);
5628
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005629 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005630 /* Socket buffer full. Let's try again later from the same point */
5631 return 0;
5632 }
5633
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005634 appctx->ctx.errors.px = proxy;
5635 appctx->ctx.errors.buf = 0;
5636 appctx->ctx.errors.bol = 0;
5637 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005638 }
5639
5640 /* we have two inner loops here, one for the proxy, the other one for
5641 * the buffer.
5642 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005643 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005644 struct error_snapshot *es;
5645
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005646 if (appctx->ctx.errors.buf == 0)
5647 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005648 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005649 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005650
5651 if (!es->when.tv_sec)
5652 goto next;
5653
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005654 if (appctx->ctx.errors.iid >= 0 &&
5655 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5656 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005657 goto next;
5658
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005659 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005660 /* just print headers now */
5661
5662 char pn[INET6_ADDRSTRLEN];
5663 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005664 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005665
5666 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005667 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005668 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005669 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005670
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005671 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5672 case AF_INET:
5673 case AF_INET6:
5674 port = get_host_port(&es->src);
5675 break;
5676 default:
5677 port = 0;
5678 }
5679
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005680 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005681 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005682 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005683 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005684 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005685 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005686 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5687 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005688 break;
5689 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005690 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005691 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005692 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005693 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005694 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005695 break;
5696 }
5697
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005698 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005699 ", server %s (#%d), event #%u\n"
5700 " src %s:%d, session #%d, session flags 0x%08x\n"
5701 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5702 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5703 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5704 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5705 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5706 es->ev_id,
5707 pn, port, es->sid, es->s_flags,
5708 es->state, es->m_flags, es->t_flags,
5709 es->m_clen, es->m_blen,
5710 es->b_flags, es->b_out, es->b_tot,
5711 es->len, es->b_wrap, es->pos);
5712
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005713 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005714 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005715 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005716 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005717 appctx->ctx.errors.ptr = 0;
5718 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005719 }
5720
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005721 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005722 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005723 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005724 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005725 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005726 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005727 goto next;
5728 }
5729
5730 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005731 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005732 int newptr;
5733 int newline;
5734
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005735 newline = appctx->ctx.errors.bol;
5736 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5737 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005738 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005739
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005740 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005741 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005742 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005743 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005744 appctx->ctx.errors.ptr = newptr;
5745 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005746 };
5747 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005748 appctx->ctx.errors.bol = 0;
5749 appctx->ctx.errors.ptr = -1;
5750 appctx->ctx.errors.buf++;
5751 if (appctx->ctx.errors.buf > 1) {
5752 appctx->ctx.errors.buf = 0;
5753 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005754 }
5755 }
5756
5757 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005758 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005759}
5760
Willy Tarreaud5781202012-09-22 19:32:35 +02005761/* parse the "level" argument on the bind lines */
5762static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5763{
5764 if (!*args[cur_arg + 1]) {
5765 memprintf(err, "'%s' : missing level", args[cur_arg]);
5766 return ERR_ALERT | ERR_FATAL;
5767 }
5768
5769 if (!strcmp(args[cur_arg+1], "user"))
5770 conf->level = ACCESS_LVL_USER;
5771 else if (!strcmp(args[cur_arg+1], "operator"))
5772 conf->level = ACCESS_LVL_OPER;
5773 else if (!strcmp(args[cur_arg+1], "admin"))
5774 conf->level = ACCESS_LVL_ADMIN;
5775 else {
5776 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5777 args[cur_arg], args[cur_arg+1]);
5778 return ERR_ALERT | ERR_FATAL;
5779 }
5780
5781 return 0;
5782}
5783
Willy Tarreaub24281b2011-02-13 13:16:36 +01005784struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005785 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005786 .name = "<STATS>", /* used for logging */
5787 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005788 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005789};
5790
Simon Horman9bd2c732011-06-15 15:18:44 +09005791static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005792 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005793 .name = "<CLI>", /* used for logging */
5794 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005795 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005796};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005797
Willy Tarreaudc13c112013-06-21 23:16:39 +02005798static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005799 { CFG_GLOBAL, "stats", stats_parse_global },
5800 { 0, NULL, NULL },
5801}};
5802
Willy Tarreaud5781202012-09-22 19:32:35 +02005803static struct bind_kw_list bind_kws = { "STAT", { }, {
5804 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5805 { NULL, NULL, 0 },
5806}};
5807
Willy Tarreau10522fd2008-07-09 20:12:41 +02005808__attribute__((constructor))
5809static void __dumpstats_module_init(void)
5810{
5811 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005812 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005813}
5814
Willy Tarreau91861262007-10-17 17:06:05 +02005815/*
5816 * Local variables:
5817 * c-indent-level: 8
5818 * c-basic-offset: 8
5819 * End:
5820 */