blob: 4466639269816c35bd711b4f8ff51782913d5d96 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Emeric Brun4147b2e2014-06-16 18:36:30 +020038#include <common/base64.h>
Willy Tarreau91861262007-10-17 17:06:05 +020039
Willy Tarreau91861262007-10-17 17:06:05 +020040#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041
42#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020043#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010045#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
47#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020049#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010050#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010051#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020052#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010053#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010054#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020055#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020056#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010057#include <proto/sample.h>
Willy Tarreau91861262007-10-17 17:06:05 +020058#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020059#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020060#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010061#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010062#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020063
Willy Tarreau7a0169a2012-11-19 17:13:16 +010064#ifdef USE_OPENSSL
65#include <proto/ssl_sock.h>
66#endif
67
Willy Tarreau91b843d2014-01-28 16:27:17 +010068/* stats socket states */
69enum {
70 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
71 STAT_CLI_END, /* final state, let's close */
72 STAT_CLI_GETREQ, /* wait for a request */
73 STAT_CLI_OUTPUT, /* all states after this one are responses */
74 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
75 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010076 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010077 STAT_CLI_O_INFO, /* dump info */
78 STAT_CLI_O_SESS, /* dump sessions */
79 STAT_CLI_O_ERR, /* dump errors */
80 STAT_CLI_O_TAB, /* dump tables */
81 STAT_CLI_O_CLR, /* clear tables */
82 STAT_CLI_O_SET, /* set entries in tables */
83 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010084 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
85 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010086 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010087 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010088};
89
Willy Tarreaued7df902014-05-22 18:04:49 +020090/* Actions available for the stats admin forms */
91enum {
92 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +020093
94 /* enable/disable health checks */
95 ST_ADM_ACTION_DHLTH,
96 ST_ADM_ACTION_EHLTH,
97
98 /* force health check status */
99 ST_ADM_ACTION_HRUNN,
100 ST_ADM_ACTION_HNOLB,
101 ST_ADM_ACTION_HDOWN,
102
103 /* enable/disable agent checks */
104 ST_ADM_ACTION_DAGENT,
105 ST_ADM_ACTION_EAGENT,
106
107 /* force agent check status */
108 ST_ADM_ACTION_ARUNN,
109 ST_ADM_ACTION_ADOWN,
110
111 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200112 ST_ADM_ACTION_READY,
113 ST_ADM_ACTION_DRAIN,
114 ST_ADM_ACTION_MAINT,
115 ST_ADM_ACTION_SHUTDOWN,
116 /* these are the ancient actions, still available for compatibility */
117 ST_ADM_ACTION_DISABLE,
118 ST_ADM_ACTION_ENABLE,
119 ST_ADM_ACTION_STOP,
120 ST_ADM_ACTION_START,
121};
122
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100123static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100124static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +0100125static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900126static int stats_dump_sess_to_buffer(struct stream_interface *si);
127static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100128static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100129static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
130static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100131static int stats_pats_list(struct stream_interface *si);
132static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100133static int stats_map_lookup(struct stream_interface *si);
Willy Tarreaue309ab72014-10-22 19:06:31 +0200134static void cli_release_handler(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +0900135
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100136/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100137 * cli_io_handler()
138 * -> stats_dump_sess_to_buffer() // "show sess"
139 * -> stats_dump_errors_to_buffer() // "show errors"
140 * -> stats_dump_info_to_buffer() // "show info"
141 * -> stats_dump_stat_to_buffer() // "show stat"
142 * -> stats_dump_csv_header()
143 * -> stats_dump_proxy_to_buffer()
144 * -> stats_dump_fe_stats()
145 * -> stats_dump_li_stats()
146 * -> stats_dump_sv_stats()
147 * -> stats_dump_be_stats()
148 *
149 * http_stats_io_handler()
150 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
151 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
152 * -> stats_dump_html_head() // emits the HTML headers
153 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
154 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
155 * -> stats_dump_html_px_hdr()
156 * -> stats_dump_fe_stats()
157 * -> stats_dump_li_stats()
158 * -> stats_dump_sv_stats()
159 * -> stats_dump_be_stats()
160 * -> stats_dump_html_px_end()
161 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100162 */
163
Simon Horman9bd2c732011-06-15 15:18:44 +0900164static struct si_applet cli_applet;
165
166static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200167 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200168 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200169 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200170 " help : this message\n"
171 " prompt : toggle interactive mode with prompt\n"
172 " quit : disconnect\n"
173 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100174 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200175 " show stat : report counters for each proxy and server\n"
176 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100177 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200178 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200179 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200180 " set weight : change a server's weight\n"
Willy Tarreau2a4b70f2014-05-22 18:42:35 +0200181 " set server : change a server's state or weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200182 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100183 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200184 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200185 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200186 " disable : put a server or frontend in maintenance mode\n"
187 " enable : re-enable a server or frontend which is in maintenance mode\n"
188 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100189 " show acl [id] : report avalaible acls or dump an acl's contents\n"
190 " get acl : reports the patterns matching a sample for an ACL\n"
191 " add acl : add acl entry\n"
192 " del acl : delete acl entry\n"
193 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100194 " show map [id] : report avalaible maps or dump a map's contents\n"
195 " get map : reports the keys and values matching a sample for a map\n"
196 " set map : modify map entry\n"
197 " add map : add map entry\n"
198 " del map : delete map entry\n"
199 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200200 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200201 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200202
Simon Horman9bd2c732011-06-15 15:18:44 +0900203static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200204 "Permission denied\n"
205 "";
206
Willy Tarreau295a8372011-03-10 11:25:07 +0100207/* data transmission states for the stats responses */
208enum {
209 STAT_ST_INIT = 0,
210 STAT_ST_HEAD,
211 STAT_ST_INFO,
212 STAT_ST_LIST,
213 STAT_ST_END,
214 STAT_ST_FIN,
215};
216
217/* data transmission states for the stats responses inside a proxy */
218enum {
219 STAT_PX_ST_INIT = 0,
220 STAT_PX_ST_TH,
221 STAT_PX_ST_FE,
222 STAT_PX_ST_LI,
223 STAT_PX_ST_SV,
224 STAT_PX_ST_BE,
225 STAT_PX_ST_END,
226 STAT_PX_ST_FIN,
227};
228
Cyril Bonté19979e12012-04-04 12:57:21 +0200229extern const char *stat_status_codes[];
230
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200231/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200232 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200233 * needs to be closed and ignored, or a negative value upon critical failure.
234 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900235static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200236{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100237 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100238 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200239
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200240 tv_zero(&s->logs.tv_request);
241 s->logs.t_queue = 0;
242 s->logs.t_connect = 0;
243 s->logs.t_data = 0;
244 s->logs.t_close = 0;
245 s->logs.bytes_in = s->logs.bytes_out = 0;
246 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
247 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200248
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200249 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200250
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200251 if (s->listener->timeout) {
252 s->req->rto = *s->listener->timeout;
253 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200254 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200255 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200256}
257
Willy Tarreau07e9e642010-08-17 21:48:17 +0200258/* allocate a new stats frontend named <name>, and return it
259 * (or NULL in case of lack of memory).
260 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200261static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200262{
263 struct proxy *fe;
264
265 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
266 if (!fe)
267 return NULL;
268
Willy Tarreau237250c2011-07-29 01:49:03 +0200269 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200270 fe->next = proxy;
271 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200272 fe->last_change = now.tv_sec;
273 fe->id = strdup("GLOBAL");
274 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200275 fe->maxconn = 10; /* default to 10 concurrent connections */
276 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200277 fe->conf.file = strdup(file);
278 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200279 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200280
281 /* the stats frontend is the only one able to assign ID #0 */
282 fe->conf.id.key = fe->uuid = 0;
283 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200284 return fe;
285}
286
Willy Tarreaufbee7132007-10-18 13:53:22 +0200287/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200288 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
289 * error message into the <err> buffer which will be preallocated. The trailing
290 * '\n' must not be written. The function must be called with <args> pointing to
291 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200292 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200293static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200294 struct proxy *defpx, const char *file, int line,
295 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200296{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200297 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200298 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200299
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200300 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200301 int cur_arg;
302
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200303 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200304 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 +0200305 return -1;
306 }
307
Willy Tarreau89a63132009-08-16 17:41:45 +0200308 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200309 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200310 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200311 return -1;
312 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200313 }
314
Willy Tarreau4348fad2012-09-20 16:48:07 +0200315 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200316 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200317
Willy Tarreauc53d4222012-09-20 20:19:28 +0200318 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
319 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
320 file, line, args[0], args[1], err && *err ? *err : "error");
321 return -1;
322 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200323
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200324 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200325 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200326 static int bind_dumped;
327 struct bind_kw *kw;
328
329 kw = bind_find_kw(args[cur_arg]);
330 if (kw) {
331 if (!kw->parse) {
332 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
333 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200334 return -1;
335 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200336
337 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
338 if (err && *err)
339 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
340 else
341 memprintf(err, "'%s %s' : error encountered while processing '%s'",
342 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200343 return -1;
344 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200345
346 cur_arg += 1 + kw->skip;
347 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200348 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200349
350 if (!bind_dumped) {
351 bind_dump_kws(err);
352 indent_msg(err, 4);
353 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200354 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200355
356 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
357 args[0], args[1], args[cur_arg],
358 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
359 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200360 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100361
Willy Tarreauc53d4222012-09-20 20:19:28 +0200362 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
363 l->maxconn = global.stats_fe->maxconn;
364 l->backlog = global.stats_fe->backlog;
365 l->timeout = &global.stats_fe->timeout.client;
366 l->accept = session_accept;
367 l->handler = process_session;
368 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
369 l->nice = -64; /* we want to boost priority for local stats */
370 global.maxsock += l->maxconn;
371 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200372 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200373 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100374 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200375 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100376
377 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200378 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100379 return -1;
380 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200381
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100382 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200383 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200384 return -1;
385 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200386 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200387 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200388 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200389 return -1;
390 }
391 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200392 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200393 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200394 else if (!strcmp(args[1], "maxconn")) {
395 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200396
397 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200398 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200399 return -1;
400 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200401
402 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200403 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200404 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200405 return -1;
406 }
407 }
408 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200409 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200410 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
411 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100412 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200413
Willy Tarreau91319572013-04-20 09:48:50 +0200414 if (!global.stats_fe) {
415 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
416 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
417 return -1;
418 }
419 }
420
Willy Tarreau35b7b162012-10-22 23:17:18 +0200421 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100422 unsigned int low, high;
423
Willy Tarreau35b7b162012-10-22 23:17:18 +0200424 if (strcmp(args[cur_arg], "all") == 0) {
425 set = 0;
426 break;
427 }
428 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100429 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200430 }
431 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100432 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200433 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100434 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100435 char *dash = strchr(args[cur_arg], '-');
436
437 low = high = str2uic(args[cur_arg]);
438 if (dash)
439 high = str2uic(dash + 1);
440
441 if (high < low) {
442 unsigned int swap = low;
443 low = high;
444 high = swap;
445 }
446
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100447 if (low < 1 || high > LONGBITS) {
448 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
449 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200450 return -1;
451 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100452 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100453 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100454 }
455 else {
456 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100457 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
458 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100459 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200460 }
461 cur_arg++;
462 }
463 global.stats_fe->bind_proc = set;
464 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200465 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200466 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200467 return -1;
468 }
469 return 0;
470}
471
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100472/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
473 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100474 * NOTE: Some tools happen to rely on the field position instead of its name,
475 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100476 */
477static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100478{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100479 chunk_appendf(&trash,
480 "# pxname,svname,"
481 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100482 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100483 "bin,bout,"
484 "dreq,dresp,"
485 "ereq,econ,eresp,"
486 "wretr,wredis,"
487 "status,weight,act,bck,"
488 "chkfail,chkdown,lastchg,downtime,qlimit,"
489 "pid,iid,sid,throttle,lbtot,tracked,type,"
490 "rate,rate_lim,rate_max,"
491 "check_status,check_code,check_duration,"
492 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
493 "req_rate,req_rate_max,req_tot,"
494 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200495 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100496 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100497}
498
Simon Hormand9366582011-06-15 15:18:45 +0900499/* print a string of text buffer to <out>. The format is :
500 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
501 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
502 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
503 */
504static int dump_text(struct chunk *out, const char *buf, int bsize)
505{
506 unsigned char c;
507 int ptr = 0;
508
509 while (buf[ptr] && ptr < bsize) {
510 c = buf[ptr];
511 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
512 if (out->len > out->size - 1)
513 break;
514 out->str[out->len++] = c;
515 }
516 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
517 if (out->len > out->size - 2)
518 break;
519 out->str[out->len++] = '\\';
520 switch (c) {
521 case ' ': c = ' '; break;
522 case '\t': c = 't'; break;
523 case '\n': c = 'n'; break;
524 case '\r': c = 'r'; break;
525 case '\e': c = 'e'; break;
526 case '\\': c = '\\'; break;
527 }
528 out->str[out->len++] = c;
529 }
530 else {
531 if (out->len > out->size - 4)
532 break;
533 out->str[out->len++] = '\\';
534 out->str[out->len++] = 'x';
535 out->str[out->len++] = hextab[(c >> 4) & 0xF];
536 out->str[out->len++] = hextab[c & 0xF];
537 }
538 ptr++;
539 }
540
541 return ptr;
542}
543
544/* print a buffer in hexa.
545 * Print stopped if <bsize> is reached, or if no more place in the chunk.
546 */
547static int dump_binary(struct chunk *out, const char *buf, int bsize)
548{
549 unsigned char c;
550 int ptr = 0;
551
552 while (ptr < bsize) {
553 c = buf[ptr];
554
555 if (out->len > out->size - 2)
556 break;
557 out->str[out->len++] = hextab[(c >> 4) & 0xF];
558 out->str[out->len++] = hextab[c & 0xF];
559
560 ptr++;
561 }
562 return ptr;
563}
564
565/* Dump the status of a table 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_head_to_buffer(struct chunk *msg, struct stream_interface *si,
570 struct proxy *proxy, struct proxy *target)
571{
Willy Tarreau306f8302013-07-08 15:53:06 +0200572 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900573
Willy Tarreau77804732012-10-29 16:14:26 +0100574 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900575 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
576
577 /* any other information should be dumped here */
578
Willy Tarreau290e63a2012-09-20 18:07:14 +0200579 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100580 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900581
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200582 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900583 return 0;
584
585 return 1;
586}
587
588/* Dump the a table entry to a stream interface's
589 * read buffer. It returns 0 if the output buffer is full
590 * and needs to be called again, otherwise non-zero.
591 */
592static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
593 struct proxy *proxy, struct stksess *entry)
594{
595 int dt;
596
Willy Tarreau77804732012-10-29 16:14:26 +0100597 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900598
599 if (proxy->table.type == STKTABLE_TYPE_IP) {
600 char addr[INET_ADDRSTRLEN];
601 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100602 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900603 }
604 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
605 char addr[INET6_ADDRSTRLEN];
606 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100607 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900608 }
609 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100610 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900611 }
612 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100613 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900614 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
615 }
616 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100617 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900618 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
619 }
620
Willy Tarreau77804732012-10-29 16:14:26 +0100621 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900622
623 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
624 void *ptr;
625
626 if (proxy->table.data_ofs[dt] == 0)
627 continue;
628 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100629 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900630 else
Willy Tarreau77804732012-10-29 16:14:26 +0100631 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900632
633 ptr = stktable_data_ptr(&proxy->table, entry, dt);
634 switch (stktable_data_types[dt].std_type) {
635 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100636 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900637 break;
638 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100639 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900640 break;
641 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100642 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900643 break;
644 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100645 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900646 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
647 proxy->table.data_arg[dt].u));
648 break;
649 }
650 }
Willy Tarreau77804732012-10-29 16:14:26 +0100651 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900652
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200653 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900654 return 0;
655
656 return 1;
657}
658
Willy Tarreaudec98142012-06-06 23:37:08 +0200659static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900660{
Willy Tarreau306f8302013-07-08 15:53:06 +0200661 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100662 struct appctx *appctx = __objt_appctx(si->end);
663 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900664 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900665 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900666 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200667 long long value;
668 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200669 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200670 void *ptr;
671 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900672
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100673 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900674
675 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100676 appctx->ctx.cli.msg = "Key value expected\n";
677 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900678 return;
679 }
680
Simon Hormanc5b89f62011-06-15 15:18:50 +0900681 switch (px->table.type) {
682 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900683 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100684 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900685 break;
686 case STKTABLE_TYPE_IPV6:
687 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100688 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900689 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900690 case STKTABLE_TYPE_INTEGER:
691 {
692 char *endptr;
693 unsigned long val;
694 errno = 0;
695 val = strtoul(args[4], &endptr, 10);
696 if ((errno == ERANGE && val == ULONG_MAX) ||
697 (errno != 0 && val == 0) || endptr == args[4] ||
698 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100699 appctx->ctx.cli.msg = "Invalid key\n";
700 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900701 return;
702 }
703 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100704 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900705 break;
706 }
707 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900708 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100709 static_table_key->key = args[4];
710 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900711 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900712 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200713 switch (action) {
714 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100715 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 +0200716 break;
717 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100718 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 +0200719 break;
720 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100721 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200722 break;
723 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100724 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900725 return;
726 }
727
728 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200729 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100730 appctx->ctx.cli.msg = stats_permission_denied_msg;
731 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900732 return;
733 }
734
Willy Tarreau07115412012-10-29 21:56:59 +0100735 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900736
Willy Tarreaudec98142012-06-06 23:37:08 +0200737 switch (action) {
738 case STAT_CLI_O_TAB:
739 if (!ts)
740 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100741 chunk_reset(&trash);
742 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900743 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100744 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900745 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200746
747 case STAT_CLI_O_CLR:
748 if (!ts)
749 return;
750 if (ts->ref_cnt) {
751 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100752 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
753 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200754 return;
755 }
756 stksess_kill(&px->table, ts);
757 break;
Simon Horman17bce342011-06-15 15:18:47 +0900758
Willy Tarreau654694e2012-06-07 01:03:16 +0200759 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200760 if (ts)
761 stktable_touch(&px->table, ts, 1);
762 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100763 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200764 if (!ts) {
765 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100766 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
767 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200768 return;
769 }
770 stktable_store(&px->table, ts, 1);
771 }
772
Willy Tarreau47060b62013-08-01 21:11:42 +0200773 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
774 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100775 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
776 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200777 return;
778 }
779
780 data_type = stktable_get_data_type(args[cur_arg] + 5);
781 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100782 appctx->ctx.cli.msg = "Unknown data type\n";
783 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200784 return;
785 }
786
787 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100788 appctx->ctx.cli.msg = "Data type not stored in this table\n";
789 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200790 return;
791 }
792
793 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100794 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
795 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200796 return;
797 }
798
799 ptr = stktable_data_ptr(&px->table, ts, data_type);
800
801 switch (stktable_data_types[data_type].std_type) {
802 case STD_T_SINT:
803 stktable_data_cast(ptr, std_t_sint) = value;
804 break;
805 case STD_T_UINT:
806 stktable_data_cast(ptr, std_t_uint) = value;
807 break;
808 case STD_T_ULL:
809 stktable_data_cast(ptr, std_t_ull) = value;
810 break;
811 case STD_T_FRQP:
812 /* We set both the current and previous values. That way
813 * the reported frequency is stable during all the period
814 * then slowly fades out. This allows external tools to
815 * push measures without having to update them too often.
816 */
817 frqp = &stktable_data_cast(ptr, std_t_frqp);
818 frqp->curr_tick = now_ms;
819 frqp->prev_ctr = 0;
820 frqp->curr_ctr = value;
821 break;
822 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200823 }
824 break;
825
Willy Tarreaudec98142012-06-06 23:37:08 +0200826 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100827 appctx->ctx.cli.msg = "Unknown action\n";
828 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200829 break;
Simon Horman121f3052011-06-15 15:18:46 +0900830 }
Simon Horman121f3052011-06-15 15:18:46 +0900831}
832
Willy Tarreau654694e2012-06-07 01:03:16 +0200833static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900834{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100835 struct appctx *appctx = __objt_appctx(si->end);
836
Willy Tarreau04b3a192013-04-13 09:41:37 +0200837 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100838 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
839 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200840 return;
841 }
842
Simon Hormand5b9fd92011-06-15 15:18:48 +0900843 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100844 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
845 if (appctx->ctx.table.data_type < 0) {
846 appctx->ctx.cli.msg = "Unknown data type\n";
847 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900848 return;
849 }
850
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100851 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
852 appctx->ctx.cli.msg = "Data type not stored in this table\n";
853 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900854 return;
855 }
856
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100857 appctx->ctx.table.data_op = get_std_op(args[4]);
858 if (appctx->ctx.table.data_op < 0) {
859 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
860 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900861 return;
862 }
863
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100864 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
865 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
866 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900867 return;
868 }
869}
870
Willy Tarreaudec98142012-06-06 23:37:08 +0200871static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900872{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100873 struct appctx *appctx = __objt_appctx(si->end);
874
875 appctx->ctx.table.data_type = -1;
876 appctx->st2 = STAT_ST_INIT;
877 appctx->ctx.table.target = NULL;
878 appctx->ctx.table.proxy = NULL;
879 appctx->ctx.table.entry = NULL;
880 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900881
882 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100883 appctx->ctx.table.target = find_stktable(args[2]);
884 if (!appctx->ctx.table.target) {
885 appctx->ctx.cli.msg = "No such table\n";
886 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900887 return;
888 }
889 }
890 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200891 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900892 goto err_args;
893 return;
894 }
895
896 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200897 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900898 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200899 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900900 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900901 goto err_args;
902
903 return;
904
905err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200906 switch (action) {
907 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100908 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 +0200909 break;
910 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100911 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 +0200912 break;
913 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100914 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200915 break;
916 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100917 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900918}
919
Willy Tarreau532a4502011-09-07 22:37:44 +0200920/* Expects to find a frontend named <arg> and returns it, otherwise displays various
921 * adequate error messages and returns NULL. This function also expects the session
922 * level to be admin.
923 */
924static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
925{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100926 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200927 struct proxy *px;
928
Willy Tarreau290e63a2012-09-20 18:07:14 +0200929 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100930 appctx->ctx.cli.msg = stats_permission_denied_msg;
931 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200932 return NULL;
933 }
934
935 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100936 appctx->ctx.cli.msg = "A frontend name is expected.\n";
937 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200938 return NULL;
939 }
940
941 px = findproxy(arg, PR_CAP_FE);
942 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100943 appctx->ctx.cli.msg = "No such frontend.\n";
944 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200945 return NULL;
946 }
947 return px;
948}
949
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200950/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
951 * and returns the pointer to the server. Otherwise, display adequate error messages
952 * and returns NULL. This function also expects the session level to be admin. Note:
953 * the <arg> is modified to remove the '/'.
954 */
955static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
956{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100957 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200958 struct proxy *px;
959 struct server *sv;
960 char *line;
961
Willy Tarreau290e63a2012-09-20 18:07:14 +0200962 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100963 appctx->ctx.cli.msg = stats_permission_denied_msg;
964 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200965 return NULL;
966 }
967
968 /* split "backend/server" and make <line> point to server */
969 for (line = arg; *line; line++)
970 if (*line == '/') {
971 *line++ = '\0';
972 break;
973 }
974
975 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100976 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
977 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200978 return NULL;
979 }
980
981 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100982 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
983 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200984 return NULL;
985 }
986
987 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100988 appctx->ctx.cli.msg = "Proxy is disabled.\n";
989 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200990 return NULL;
991 }
992
993 return sv;
994}
995
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100996/* This function is used with map and acl management. It permits to browse
997 * each reference. The variable <getnext> must contain the current node,
998 * <end> point to the root node and the <flags> permit to filter required
999 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001000 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001001static inline
1002struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1003 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001004{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001005 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001006
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001007 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001008
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001009 /* Get next list entry. */
1010 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001011
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001012 /* If the entry is the last of the list, return NULL. */
1013 if (&ref->list == end)
1014 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001015
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001016 /* If the entry match the flag, return it. */
1017 if (ref->flags & flags)
1018 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001019 }
1020}
1021
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001022static inline
1023struct pat_ref *pat_ref_lookup_ref(const char *reference)
1024{
1025 int id;
1026 char *error;
1027
1028 /* If the reference starts by a '#', this is numeric id. */
1029 if (reference[0] == '#') {
1030 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1031 id = strtol(reference + 1, &error, 10);
1032 if (*error != '\0')
1033 return NULL;
1034
1035 /* Perform the unique id lookup. */
1036 return pat_ref_lookupid(id);
1037 }
1038
1039 /* Perform the string lookup. */
1040 return pat_ref_lookup(reference);
1041}
1042
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001043/* This function is used with map and acl management. It permits to browse
1044 * each reference.
1045 */
1046static inline
1047struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1048{
1049 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001050 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1051 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001052 return NULL;
1053 return expr;
1054}
1055
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001056/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001057 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001058 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001059 * designating the function which will have to process the request, which can
1060 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001061 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001062static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001063{
Willy Tarreau306f8302013-07-08 15:53:06 +02001064 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001065 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001066 char *args[MAX_STATS_ARGS + 1];
1067 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001068 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001069
1070 while (isspace((unsigned char)*line))
1071 line++;
1072
1073 arg = 0;
1074 args[arg] = line;
1075
1076 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001077 if (*line == '\\') {
1078 line++;
1079 if (*line == '\0')
1080 break;
1081 }
1082 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001083 *line++ = '\0';
1084
1085 while (isspace((unsigned char)*line))
1086 line++;
1087
1088 args[++arg] = line;
1089 continue;
1090 }
1091
1092 line++;
1093 }
1094
1095 while (++arg <= MAX_STATS_ARGS)
1096 args[arg] = line;
1097
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001098 /* remove \ */
1099 arg = 0;
1100 while (*args[arg] != '\0') {
1101 j = 0;
1102 for (i=0; args[arg][i] != '\0'; i++) {
1103 if (args[arg][i] == '\\')
1104 continue;
1105 args[arg][j] = args[arg][i];
1106 j++;
1107 }
1108 args[arg][j] = '\0';
1109 arg++;
1110 }
1111
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001112 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001113 if (strcmp(args[0], "show") == 0) {
1114 if (strcmp(args[1], "stat") == 0) {
1115 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001116 appctx->ctx.stats.flags |= STAT_BOUND;
1117 appctx->ctx.stats.iid = atoi(args[2]);
1118 appctx->ctx.stats.type = atoi(args[3]);
1119 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001120 }
1121
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001122 appctx->st2 = STAT_ST_INIT;
1123 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001124 }
1125 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001126 appctx->st2 = STAT_ST_INIT;
1127 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001128 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001129 else if (strcmp(args[1], "pools") == 0) {
1130 appctx->st2 = STAT_ST_INIT;
1131 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1132 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001133 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001134 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001135 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001136 appctx->ctx.cli.msg = stats_permission_denied_msg;
1137 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001138 return 1;
1139 }
Willy Tarreau76153662012-11-26 01:16:39 +01001140 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001141 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001142 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001143 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001144 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001145 appctx->ctx.sess.target = NULL;
1146 appctx->ctx.sess.section = 0; /* start with session status */
1147 appctx->ctx.sess.pos = 0;
1148 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001149 }
1150 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001151 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001152 appctx->ctx.cli.msg = stats_permission_denied_msg;
1153 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001154 return 1;
1155 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001156 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001157 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001158 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001159 appctx->ctx.errors.iid = -1;
1160 appctx->ctx.errors.px = NULL;
1161 appctx->st2 = STAT_ST_INIT;
1162 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001163 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001164 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001165 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001166 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001167 else if (strcmp(args[1], "map") == 0 ||
1168 strcmp(args[1], "acl") == 0) {
1169
1170 /* Set ACL or MAP flags. */
1171 if (args[1][0] == 'm')
1172 appctx->ctx.map.display_flags = PAT_REF_MAP;
1173 else
1174 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001175
1176 /* no parameter: display all map avalaible */
1177 if (!*args[2]) {
1178 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001179 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001180 return 1;
1181 }
1182
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001183 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001184 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001185 if (!appctx->ctx.map.ref ||
1186 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1187 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001188 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001189 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001190 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001191 appctx->st0 = STAT_CLI_PRINT;
1192 return 1;
1193 }
1194 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001195 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001196 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001197 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001198 return 0;
1199 }
1200 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001201 else if (strcmp(args[0], "clear") == 0) {
1202 if (strcmp(args[1], "counters") == 0) {
1203 struct proxy *px;
1204 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001205 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001206 int clrall = 0;
1207
1208 if (strcmp(args[2], "all") == 0)
1209 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001210
Willy Tarreau6162db22009-10-10 17:13:00 +02001211 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001212 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1213 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001214 appctx->ctx.cli.msg = stats_permission_denied_msg;
1215 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001216 return 1;
1217 }
1218
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001219 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001220 if (clrall) {
1221 memset(&px->be_counters, 0, sizeof(px->be_counters));
1222 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1223 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001224 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001225 px->be_counters.conn_max = 0;
1226 px->be_counters.p.http.rps_max = 0;
1227 px->be_counters.sps_max = 0;
1228 px->be_counters.cps_max = 0;
1229 px->be_counters.nbpend_max = 0;
1230
1231 px->fe_counters.conn_max = 0;
1232 px->fe_counters.p.http.rps_max = 0;
1233 px->fe_counters.sps_max = 0;
1234 px->fe_counters.cps_max = 0;
1235 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001236 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001237
1238 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001239 if (clrall)
1240 memset(&sv->counters, 0, sizeof(sv->counters));
1241 else {
1242 sv->counters.cur_sess_max = 0;
1243 sv->counters.nbpend_max = 0;
1244 sv->counters.sps_max = 0;
1245 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001246
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001247 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001248 if (li->counters) {
1249 if (clrall)
1250 memset(li->counters, 0, sizeof(*li->counters));
1251 else
1252 li->counters->conn_max = 0;
1253 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001254 }
1255
Willy Tarreau81c25d02011-09-07 15:17:21 +02001256 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001257 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001258 return 1;
1259 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001260 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001261 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001262 /* end of processing */
1263 return 1;
1264 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001265 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1266 /* Set ACL or MAP flags. */
1267 if (args[1][0] == 'm')
1268 appctx->ctx.map.display_flags = PAT_REF_MAP;
1269 else
1270 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001271
1272 /* no parameter */
1273 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001274 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1275 appctx->ctx.cli.msg = "Missing map identifier.\n";
1276 else
1277 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001278 appctx->st0 = STAT_CLI_PRINT;
1279 return 1;
1280 }
1281
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001282 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001283 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001284 if (!appctx->ctx.map.ref ||
1285 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1286 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001287 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001288 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001289 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001290 appctx->st0 = STAT_CLI_PRINT;
1291 return 1;
1292 }
1293
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001294 /* Clear all. */
1295 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001296
1297 /* return response */
1298 appctx->ctx.cli.msg = "Done.\n";
1299 appctx->st0 = STAT_CLI_PRINT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001300 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001301 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001302 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001303 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001304 return 0;
1305 }
1306 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001307 else if (strcmp(args[0], "get") == 0) {
1308 if (strcmp(args[1], "weight") == 0) {
1309 struct proxy *px;
1310 struct server *sv;
1311
1312 /* split "backend/server" and make <line> point to server */
1313 for (line = args[2]; *line; line++)
1314 if (*line == '/') {
1315 *line++ = '\0';
1316 break;
1317 }
1318
1319 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001320 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1321 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001322 return 1;
1323 }
1324
1325 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001326 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1327 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001328 return 1;
1329 }
1330
1331 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001332 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1333 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001334 return 1;
1335 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001336 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1337 /* Set flags. */
1338 if (args[1][0] == 'm')
1339 appctx->ctx.map.display_flags = PAT_REF_MAP;
1340 else
1341 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001342
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001343 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001344 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001345 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1346 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1347 else
1348 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001349 appctx->st0 = STAT_CLI_PRINT;
1350 return 1;
1351 }
1352
1353 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001354 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001355 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001356 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001357 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001358 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001359 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001360 appctx->st0 = STAT_CLI_PRINT;
1361 return 1;
1362 }
1363
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001364 /* copy input string. The string must be allocated because
1365 * it may be used over multiple iterations. It's released
1366 * at the end and upon abort anyway.
1367 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001368 appctx->ctx.map.chunk.len = strlen(args[3]);
1369 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1370 appctx->ctx.map.chunk.str = strdup(args[3]);
1371 if (!appctx->ctx.map.chunk.str) {
1372 appctx->ctx.cli.msg = "Out of memory error.\n";
1373 appctx->st0 = STAT_CLI_PRINT;
1374 return 1;
1375 }
1376
1377 /* prepare response */
1378 appctx->st2 = STAT_ST_INIT;
1379 appctx->st0 = STAT_CLI_O_MLOOK;
1380 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001381 else { /* not "get weight" */
1382 return 0;
1383 }
1384 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001385 else if (strcmp(args[0], "set") == 0) {
1386 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001387 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001388 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001389
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001390 sv = expect_server_admin(s, si, args[2]);
1391 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001392 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001393
Simon Horman7d09b9a2013-02-12 10:45:51 +09001394 warning = server_parse_weight_change_request(sv, args[3]);
1395 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001396 appctx->ctx.cli.msg = warning;
1397 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001398 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001399 return 1;
1400 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001401 else if (strcmp(args[1], "server") == 0) {
1402 struct server *sv;
1403 const char *warning;
1404
1405 sv = expect_server_admin(s, si, args[2]);
1406 if (!sv)
1407 return 1;
1408
1409 if (strcmp(args[3], "weight") == 0) {
1410 warning = server_parse_weight_change_request(sv, args[4]);
1411 if (warning) {
1412 appctx->ctx.cli.msg = warning;
1413 appctx->st0 = STAT_CLI_PRINT;
1414 }
1415 }
1416 else if (strcmp(args[3], "state") == 0) {
1417 if (strcmp(args[4], "ready") == 0)
1418 srv_adm_set_ready(sv);
1419 else if (strcmp(args[4], "drain") == 0)
1420 srv_adm_set_drain(sv);
1421 else if (strcmp(args[4], "maint") == 0)
1422 srv_adm_set_maint(sv);
1423 else {
1424 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1425 appctx->st0 = STAT_CLI_PRINT;
1426 }
1427 }
1428 else if (strcmp(args[3], "health") == 0) {
1429 if (sv->track) {
1430 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1431 appctx->st0 = STAT_CLI_PRINT;
1432 }
1433 else if (strcmp(args[4], "up") == 0) {
1434 sv->check.health = sv->check.rise + sv->check.fall - 1;
1435 srv_set_running(sv, "changed from CLI");
1436 }
1437 else if (strcmp(args[4], "stopping") == 0) {
1438 sv->check.health = sv->check.rise + sv->check.fall - 1;
1439 srv_set_stopping(sv, "changed from CLI");
1440 }
1441 else if (strcmp(args[4], "down") == 0) {
1442 sv->check.health = 0;
1443 srv_set_stopped(sv, "changed from CLI");
1444 }
1445 else {
1446 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1447 appctx->st0 = STAT_CLI_PRINT;
1448 }
1449 }
1450 else if (strcmp(args[3], "agent") == 0) {
1451 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1452 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1453 appctx->st0 = STAT_CLI_PRINT;
1454 }
1455 else if (strcmp(args[4], "up") == 0) {
1456 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1457 srv_set_running(sv, "changed from CLI");
1458 }
1459 else if (strcmp(args[4], "down") == 0) {
1460 sv->agent.health = 0;
1461 srv_set_stopped(sv, "changed from CLI");
1462 }
1463 else {
1464 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1465 appctx->st0 = STAT_CLI_PRINT;
1466 }
1467 }
1468 else {
1469 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1470 appctx->st0 = STAT_CLI_PRINT;
1471 }
1472 return 1;
1473 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001474 else if (strcmp(args[1], "timeout") == 0) {
1475 if (strcmp(args[2], "cli") == 0) {
1476 unsigned timeout;
1477 const char *res;
1478
1479 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001480 appctx->ctx.cli.msg = "Expects an integer value.\n";
1481 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001482 return 1;
1483 }
1484
1485 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1486 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001487 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1488 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001489 return 1;
1490 }
1491
1492 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1493 return 1;
1494 }
1495 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001496 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1497 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001498 return 1;
1499 }
1500 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001501 else if (strcmp(args[1], "maxconn") == 0) {
1502 if (strcmp(args[2], "frontend") == 0) {
1503 struct proxy *px;
1504 struct listener *l;
1505 int v;
1506
Willy Tarreau532a4502011-09-07 22:37:44 +02001507 px = expect_frontend_admin(s, si, args[3]);
1508 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001509 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001510
1511 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001512 appctx->ctx.cli.msg = "Integer value expected.\n";
1513 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001514 return 1;
1515 }
1516
1517 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001518 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001519 appctx->ctx.cli.msg = "Value out of range.\n";
1520 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001521 return 1;
1522 }
1523
1524 /* OK, the value is fine, so we assign it to the proxy and to all of
1525 * its listeners. The blocked ones will be dequeued.
1526 */
1527 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001528 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001529 l->maxconn = v;
1530 if (l->state == LI_FULL)
1531 resume_listener(l);
1532 }
1533
1534 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1535 dequeue_all_listeners(&s->fe->listener_queue);
1536
1537 return 1;
1538 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001539 else if (strcmp(args[2], "global") == 0) {
1540 int v;
1541
Willy Tarreau290e63a2012-09-20 18:07:14 +02001542 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001543 appctx->ctx.cli.msg = stats_permission_denied_msg;
1544 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001545 return 1;
1546 }
1547
1548 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001549 appctx->ctx.cli.msg = "Expects an integer value.\n";
1550 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001551 return 1;
1552 }
1553
1554 v = atoi(args[3]);
1555 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001556 appctx->ctx.cli.msg = "Value out of range.\n";
1557 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001558 return 1;
1559 }
1560
1561 /* check for unlimited values */
1562 if (v <= 0)
1563 v = global.hardmaxconn;
1564
1565 global.maxconn = v;
1566
1567 /* Dequeues all of the listeners waiting for a resource */
1568 if (!LIST_ISEMPTY(&global_listener_queue))
1569 dequeue_all_listeners(&global_listener_queue);
1570
1571 return 1;
1572 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001573 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001574 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1575 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001576 return 1;
1577 }
1578 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001579 else if (strcmp(args[1], "rate-limit") == 0) {
1580 if (strcmp(args[2], "connections") == 0) {
1581 if (strcmp(args[3], "global") == 0) {
1582 int v;
1583
Willy Tarreau290e63a2012-09-20 18:07:14 +02001584 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001585 appctx->ctx.cli.msg = stats_permission_denied_msg;
1586 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001587 return 1;
1588 }
1589
1590 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001591 appctx->ctx.cli.msg = "Expects an integer value.\n";
1592 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001593 return 1;
1594 }
1595
1596 v = atoi(args[4]);
1597 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001598 appctx->ctx.cli.msg = "Value out of range.\n";
1599 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001600 return 1;
1601 }
1602
1603 global.cps_lim = v;
1604
1605 /* Dequeues all of the listeners waiting for a resource */
1606 if (!LIST_ISEMPTY(&global_listener_queue))
1607 dequeue_all_listeners(&global_listener_queue);
1608
1609 return 1;
1610 }
1611 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001612 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1613 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001614 return 1;
1615 }
1616 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001617 else if (strcmp(args[2], "sessions") == 0) {
1618 if (strcmp(args[3], "global") == 0) {
1619 int v;
1620
1621 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1622 appctx->ctx.cli.msg = stats_permission_denied_msg;
1623 appctx->st0 = STAT_CLI_PRINT;
1624 return 1;
1625 }
1626
1627 if (!*args[4]) {
1628 appctx->ctx.cli.msg = "Expects an integer value.\n";
1629 appctx->st0 = STAT_CLI_PRINT;
1630 return 1;
1631 }
1632
1633 v = atoi(args[4]);
1634 if (v < 0) {
1635 appctx->ctx.cli.msg = "Value out of range.\n";
1636 appctx->st0 = STAT_CLI_PRINT;
1637 return 1;
1638 }
1639
1640 global.sps_lim = v;
1641
1642 /* Dequeues all of the listeners waiting for a resource */
1643 if (!LIST_ISEMPTY(&global_listener_queue))
1644 dequeue_all_listeners(&global_listener_queue);
1645
1646 return 1;
1647 }
1648 else {
1649 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1650 appctx->st0 = STAT_CLI_PRINT;
1651 return 1;
1652 }
1653 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001654#ifdef USE_OPENSSL
1655 else if (strcmp(args[2], "ssl-sessions") == 0) {
1656 if (strcmp(args[3], "global") == 0) {
1657 int v;
1658
1659 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1660 appctx->ctx.cli.msg = stats_permission_denied_msg;
1661 appctx->st0 = STAT_CLI_PRINT;
1662 return 1;
1663 }
1664
1665 if (!*args[4]) {
1666 appctx->ctx.cli.msg = "Expects an integer value.\n";
1667 appctx->st0 = STAT_CLI_PRINT;
1668 return 1;
1669 }
1670
1671 v = atoi(args[4]);
1672 if (v < 0) {
1673 appctx->ctx.cli.msg = "Value out of range.\n";
1674 appctx->st0 = STAT_CLI_PRINT;
1675 return 1;
1676 }
1677
1678 global.ssl_lim = v;
1679
1680 /* Dequeues all of the listeners waiting for a resource */
1681 if (!LIST_ISEMPTY(&global_listener_queue))
1682 dequeue_all_listeners(&global_listener_queue);
1683
1684 return 1;
1685 }
1686 else {
1687 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1688 appctx->st0 = STAT_CLI_PRINT;
1689 return 1;
1690 }
1691 }
1692#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001693 else if (strcmp(args[2], "http-compression") == 0) {
1694 if (strcmp(args[3], "global") == 0) {
1695 int v;
1696
Willy Tarreau85d47f92012-11-21 00:29:50 +01001697 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001698 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1699 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001700 return 1;
1701 }
1702
William Lallemandd85f9172012-11-09 17:05:39 +01001703 v = atoi(args[4]);
1704 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1705 }
1706 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001707 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1708 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001709 return 1;
1710 }
1711 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001712 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001713 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001714 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001715 return 1;
1716 }
1717 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001718 else if (strcmp(args[1], "table") == 0) {
1719 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1720 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001721 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001722 char *err;
1723
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001724 /* Set flags. */
1725 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001726
1727 /* Expect three parameters: map name, key and new value. */
1728 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001729 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001730 appctx->st0 = STAT_CLI_PRINT;
1731 return 1;
1732 }
1733
1734 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001735 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001736 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001737 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001738 appctx->st0 = STAT_CLI_PRINT;
1739 return 1;
1740 }
1741
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001742 /* If the entry identifier start with a '#', it is considered as
1743 * pointer id
1744 */
1745 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1746 struct pat_ref_elt *ref;
1747 long long int conv;
1748 char *error;
1749
1750 /* Convert argument to integer value. */
1751 conv = strtoll(&args[3][1], &error, 16);
1752 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001753 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001754 appctx->st0 = STAT_CLI_PRINT;
1755 return 1;
1756 }
1757
1758 /* Convert and check integer to pointer. */
1759 ref = (struct pat_ref_elt *)(long)conv;
1760 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001761 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001762 appctx->st0 = STAT_CLI_PRINT;
1763 return 1;
1764 }
1765
1766 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001767 err = NULL;
1768 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1769 if (err)
1770 memprintf(&err, "%s.\n", err);
1771 appctx->ctx.cli.err = err;
1772 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001773 return 1;
1774 }
1775 }
1776 else {
1777 /* Else, use the entry identifier as pattern
1778 * string, and update the value.
1779 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001780 err = NULL;
1781 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1782 if (err)
1783 memprintf(&err, "%s.\n", err);
1784 appctx->ctx.cli.err = err;
1785 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001786 return 1;
1787 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001788 }
1789
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001790 /* The set is done, send message. */
1791 appctx->ctx.cli.msg = "Done.\n";
1792 appctx->st0 = STAT_CLI_PRINT;
1793 return 1;
1794 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001795#ifdef USE_OPENSSL
1796 else if (strcmp(args[1], "ssl") == 0) {
1797 if (strcmp(args[2], "ocsp-response") == 0) {
1798#ifdef SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB
1799 char *err = NULL;
1800
Emeric Brunaf4ef742014-06-19 14:10:45 +02001801 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001802 if (!*args[3]) {
1803 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1804 appctx->st0 = STAT_CLI_PRINT;
1805 return 1;
1806 }
1807
1808 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1809 if (trash.len < 0) {
1810 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1811 appctx->st0 = STAT_CLI_PRINT;
1812 return 1;
1813 }
1814
1815 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1816 if (err) {
1817 memprintf(&err, "%s.\n", err);
1818 appctx->ctx.cli.err = err;
1819 appctx->st0 = STAT_CLI_PRINT_FREE;
1820 }
1821 return 1;
1822 }
1823 appctx->ctx.cli.msg = "OCSP Response updated!";
1824 appctx->st0 = STAT_CLI_PRINT;
1825 return 1;
1826#else
1827 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1828 appctx->st0 = STAT_CLI_PRINT;
1829 return 1;
1830#endif
1831 }
1832 else {
1833 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1834 appctx->st0 = STAT_CLI_PRINT;
1835 return 1;
1836 }
1837 }
1838#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001839 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001840 return 0;
1841 }
1842 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001843 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001844 if (strcmp(args[1], "agent") == 0) {
1845 struct server *sv;
1846
1847 sv = expect_server_admin(s, si, args[2]);
1848 if (!sv)
1849 return 1;
1850
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001851 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1852 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1853 appctx->st0 = STAT_CLI_PRINT;
1854 return 1;
1855 }
1856
1857 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001858 return 1;
1859 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001860 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001861 struct server *sv;
1862
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001863 sv = expect_server_admin(s, si, args[2]);
1864 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001865 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001866
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001867 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1868 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1869 appctx->st0 = STAT_CLI_PRINT;
1870 return 1;
1871 }
1872
1873 sv->check.state |= CHK_ST_ENABLED;
1874 return 1;
1875 }
1876 else if (strcmp(args[1], "server") == 0) {
1877 struct server *sv;
1878
1879 sv = expect_server_admin(s, si, args[2]);
1880 if (!sv)
1881 return 1;
1882
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001883 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001884 return 1;
1885 }
1886 else if (strcmp(args[1], "frontend") == 0) {
1887 struct proxy *px;
1888
1889 px = expect_frontend_admin(s, si, args[2]);
1890 if (!px)
1891 return 1;
1892
1893 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001894 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1895 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001896 return 1;
1897 }
1898
1899 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001900 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1901 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001902 return 1;
1903 }
1904
1905 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001906 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1907 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001908 return 1;
1909 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001910 return 1;
1911 }
1912 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001913 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001914 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001915 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001916 }
1917 }
1918 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001919 if (strcmp(args[1], "agent") == 0) {
1920 struct server *sv;
1921
1922 sv = expect_server_admin(s, si, args[2]);
1923 if (!sv)
1924 return 1;
1925
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001926 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001927 return 1;
1928 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001929 else if (strcmp(args[1], "health") == 0) {
1930 struct server *sv;
1931
1932 sv = expect_server_admin(s, si, args[2]);
1933 if (!sv)
1934 return 1;
1935
1936 sv->check.state &= ~CHK_ST_ENABLED;
1937 return 1;
1938 }
Simon Horman671b6f02013-11-25 10:46:39 +09001939 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001940 struct server *sv;
1941
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001942 sv = expect_server_admin(s, si, args[2]);
1943 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001944 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001945
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001946 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001947 return 1;
1948 }
1949 else if (strcmp(args[1], "frontend") == 0) {
1950 struct proxy *px;
1951
1952 px = expect_frontend_admin(s, si, args[2]);
1953 if (!px)
1954 return 1;
1955
1956 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001957 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1958 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001959 return 1;
1960 }
1961
1962 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001963 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1964 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001965 return 1;
1966 }
1967
1968 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001969 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1970 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001971 return 1;
1972 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001973 return 1;
1974 }
1975 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001976 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001977 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001978 return 1;
1979 }
1980 }
1981 else if (strcmp(args[0], "shutdown") == 0) {
1982 if (strcmp(args[1], "frontend") == 0) {
1983 struct proxy *px;
1984
1985 px = expect_frontend_admin(s, si, args[2]);
1986 if (!px)
1987 return 1;
1988
1989 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001990 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1991 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001992 return 1;
1993 }
1994
1995 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1996 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1997 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1998 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1999 stop_proxy(px);
2000 return 1;
2001 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002002 else if (strcmp(args[1], "session") == 0) {
2003 struct session *sess, *ptr;
2004
Willy Tarreau290e63a2012-09-20 18:07:14 +02002005 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002006 appctx->ctx.cli.msg = stats_permission_denied_msg;
2007 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002008 return 1;
2009 }
2010
2011 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002012 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2013 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002014 return 1;
2015 }
2016
2017 ptr = (void *)strtoul(args[2], NULL, 0);
2018
2019 /* first, look for the requested session in the session table */
2020 list_for_each_entry(sess, &sessions, list) {
2021 if (sess == ptr)
2022 break;
2023 }
2024
2025 /* do we have the session ? */
2026 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002027 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2028 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002029 return 1;
2030 }
2031
2032 session_shutdown(sess, SN_ERR_KILLED);
2033 return 1;
2034 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002035 else if (strcmp(args[1], "sessions") == 0) {
2036 if (strcmp(args[2], "server") == 0) {
2037 struct server *sv;
2038 struct session *sess, *sess_bck;
2039
2040 sv = expect_server_admin(s, si, args[3]);
2041 if (!sv)
2042 return 1;
2043
2044 /* kill all the session that are on this server */
2045 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2046 if (sess->srv_conn == sv)
2047 session_shutdown(sess, SN_ERR_KILLED);
2048
2049 return 1;
2050 }
2051 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002052 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2053 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002054 return 1;
2055 }
2056 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002057 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002058 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2059 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002060 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002061 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002062 }
2063 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002064 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2065 if (args[1][0] == 'm')
2066 appctx->ctx.map.display_flags = PAT_REF_MAP;
2067 else
2068 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002069
2070 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002071 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2072 if (!*args[2] || !*args[3]) {
2073 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2074 appctx->st0 = STAT_CLI_PRINT;
2075 return 1;
2076 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002077 }
2078
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002079 else {
2080 if (!*args[2] || !*args[3]) {
2081 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2082 appctx->st0 = STAT_CLI_PRINT;
2083 return 1;
2084 }
2085 }
2086
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002087 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002088 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002089 if (!appctx->ctx.map.ref ||
2090 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002091 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002092 appctx->st0 = STAT_CLI_PRINT;
2093 return 1;
2094 }
2095
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002096 /* If the entry identifier start with a '#', it is considered as
2097 * pointer id
2098 */
2099 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2100 struct pat_ref_elt *ref;
2101 long long int conv;
2102 char *error;
2103
2104 /* Convert argument to integer value. */
2105 conv = strtoll(&args[3][1], &error, 16);
2106 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002107 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002108 appctx->st0 = STAT_CLI_PRINT;
2109 return 1;
2110 }
2111
2112 /* Convert and check integer to pointer. */
2113 ref = (struct pat_ref_elt *)(long)conv;
2114 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002115 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002116 appctx->st0 = STAT_CLI_PRINT;
2117 return 1;
2118 }
2119
2120 /* Try to delete the entry. */
2121 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2122 /* The entry is not found, send message. */
2123 appctx->ctx.cli.msg = "Key not found.\n";
2124 appctx->st0 = STAT_CLI_PRINT;
2125 return 1;
2126 }
2127 }
2128 else {
2129 /* Else, use the entry identifier as pattern
2130 * string and try to delete the entry.
2131 */
2132 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2133 /* The entry is not found, send message. */
2134 appctx->ctx.cli.msg = "Key not found.\n";
2135 appctx->st0 = STAT_CLI_PRINT;
2136 return 1;
2137 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002138 }
2139
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002140 /* The deletion is done, send message. */
2141 appctx->ctx.cli.msg = "Done.\n";
2142 appctx->st0 = STAT_CLI_PRINT;
2143 return 1;
2144 }
2145 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002146 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002147 appctx->st0 = STAT_CLI_PRINT;
2148 return 1;
2149 }
2150 }
2151 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002152 if (strcmp(args[1], "map") == 0 ||
2153 strcmp(args[1], "acl") == 0) {
2154 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002155 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002156
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002157 /* Set flags. */
2158 if (args[1][0] == 'm')
2159 appctx->ctx.map.display_flags = PAT_REF_MAP;
2160 else
2161 appctx->ctx.map.display_flags = PAT_REF_ACL;
2162
2163 /* If the keywork is "map", we expect three parameters, if it
2164 * is "acl", we expect only two parameters
2165 */
2166 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2167 if (!*args[2] || !*args[3] || !*args[4]) {
2168 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2169 appctx->st0 = STAT_CLI_PRINT;
2170 return 1;
2171 }
2172 }
2173 else {
2174 if (!*args[2] || !*args[3]) {
2175 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2176 appctx->st0 = STAT_CLI_PRINT;
2177 return 1;
2178 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002179 }
2180
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002181 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002182 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002183 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002184 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002185 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002186 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002187 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002188 appctx->st0 = STAT_CLI_PRINT;
2189 return 1;
2190 }
2191
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002192 /* The command "add acl" is prohibited if the reference
2193 * use samples.
2194 */
2195 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2196 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2197 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2198 "You must use the command 'add map' to add values.\n";
2199 appctx->st0 = STAT_CLI_PRINT;
2200 return 1;
2201 }
2202
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002203 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002204 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002205 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002206 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002207 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002208 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002209 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002210 if (err)
2211 memprintf(&err, "%s.\n", err);
2212 appctx->ctx.cli.err = err;
2213 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002214 return 1;
2215 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002216
2217 /* The add is done, send message. */
2218 appctx->ctx.cli.msg = "Done.\n";
2219 appctx->st0 = STAT_CLI_PRINT;
2220 return 1;
2221 }
2222 else { /* unknown "del" parameter */
2223 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2224 appctx->st0 = STAT_CLI_PRINT;
2225 return 1;
2226 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002227 }
2228 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002229 return 0;
2230 }
2231 return 1;
2232}
2233
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002234/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002235 * used to processes I/O from/to the stats unix socket. The system relies on a
2236 * state machine handling requests and various responses. We read a request,
2237 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002238 * Then we can read again. The state is stored in appctx->st0 and is one of the
2239 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002240 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002241 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002242static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002243{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002244 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002245 struct channel *req = si->ob;
2246 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002247 int reql;
2248 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002249
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002250 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2251 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002252
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002253 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002254 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002255 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002256 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2257 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002258 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002259 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002260 /* Let's close for real now. We just close the request
2261 * side, the conditions below will complete if needed.
2262 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002263 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002264 break;
2265 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002266 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002267 /* ensure we have some output room left in the event we
2268 * would want to return some info right after parsing.
2269 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002270 if (buffer_almost_full(si->ib->buf)) {
2271 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002272 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002273 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002274
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002275 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002276 if (reql <= 0) { /* closed or EOL not found */
2277 if (reql == 0)
2278 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002279 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002280 continue;
2281 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002282
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002283 /* seek for a possible semi-colon. If we find one, we
2284 * replace it with an LF and skip only this part.
2285 */
2286 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002287 if (trash.str[len] == ';') {
2288 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002289 reql = len + 1;
2290 break;
2291 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002292
Willy Tarreau816fc222009-10-04 07:36:58 +02002293 /* now it is time to check that we have a full line,
2294 * remove the trailing \n and possibly \r, then cut the
2295 * line.
2296 */
2297 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002298 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002299 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002300 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002301 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002302
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002303 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002304 len--;
2305
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002306 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002307
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002308 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002309 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002310 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002311 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002312 continue;
2313 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002314 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002315 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002316 else if (strcmp(trash.str, "help") == 0 ||
2317 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002318 appctx->ctx.cli.msg = stats_sock_usage_msg;
2319 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002320 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002321 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002322 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002323 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002324 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002325 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002326 /* if prompt is disabled, print help on empty lines,
2327 * so that the user at least knows how to enable
2328 * prompt and find help.
2329 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002330 appctx->ctx.cli.msg = stats_sock_usage_msg;
2331 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002332 }
2333
2334 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002335 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002336 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002337 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002338 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002339 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaue309ab72014-10-22 19:06:31 +02002340 cli_release_handler(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002341 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002342 continue;
2343 }
2344
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002345 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002346 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002347 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2348 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002349 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002350 case STAT_CLI_PRINT_FREE:
2351 if (bi_putstr(si->ib, appctx->ctx.cli.err) != -1) {
2352 free(appctx->ctx.cli.err);
2353 appctx->st0 = STAT_CLI_PROMPT;
2354 }
2355 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002356 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002357 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002358 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002359 break;
2360 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002361 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002362 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002363 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002364 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002365 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002366 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002367 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002368 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002369 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002370 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002371 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002372 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002373 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002374 if (stats_table_request(si, appctx->st0))
2375 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002376 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002377 case STAT_CLI_O_PATS:
2378 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002379 appctx->st0 = STAT_CLI_PROMPT;
2380 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002381 case STAT_CLI_O_PAT:
2382 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002383 appctx->st0 = STAT_CLI_PROMPT;
2384 break;
2385 case STAT_CLI_O_MLOOK:
2386 if (stats_map_lookup(si))
2387 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002388 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002389 case STAT_CLI_O_POOLS:
2390 if (stats_dump_pools_to_buffer(si))
2391 appctx->st0 = STAT_CLI_PROMPT;
2392 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002393 default: /* abnormal state */
Willy Tarreaue309ab72014-10-22 19:06:31 +02002394 cli_release_handler(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002395 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002396 break;
2397 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002398
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002399 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002400 if (appctx->st0 == STAT_CLI_PROMPT) {
2401 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2402 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002403 }
2404
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002405 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002406 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002407 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002408
2409 /* Now we close the output if one of the writers did so,
2410 * or if we're not in interactive mode and the request
2411 * buffer is empty. This still allows pipelined requests
2412 * to be sent in non-interactive mode.
2413 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002414 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2415 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002416 continue;
2417 }
2418
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002419 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002420 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002421 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002422 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002423
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002424 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002425 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2426 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002427 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002428 * and nothing more to consume. This is comparable to a broken pipe, so
2429 * we forward the close to the request side so that it flows upstream to
2430 * the client.
2431 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002432 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002433 }
2434
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002435 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002436 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2437 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2438 /* We have no more processing to do, and nothing more to send, and
2439 * the client side has closed. So we'll forward this state downstream
2440 * on the response buffer.
2441 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002442 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002443 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002444 }
2445
2446 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002447 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002448
2449 /* we don't want to expire timeouts while we're processing requests */
2450 si->ib->rex = TICK_ETERNITY;
2451 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002452
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002453 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002454 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 +02002455 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002456 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 +02002457
2458 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2459 /* check that we have released everything then unregister */
2460 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002461 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002462}
2463
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002464/* This function dumps information onto the stream interface's read buffer.
2465 * It returns 0 as long as it does not complete, non-zero upon completion.
2466 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002467 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002468static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002469{
2470 unsigned int up = (now.tv_sec - start_date.tv_sec);
2471
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002472#ifdef USE_OPENSSL
2473 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2474 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2475 int ssl_reuse = 0;
2476
2477 if (ssl_key_rate < ssl_sess_rate) {
2478 /* count the ssl reuse ratio and avoid overflows in both directions */
2479 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2480 }
2481#endif
2482
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002483 chunk_printf(&trash,
2484 "Name: " PRODUCT_NAME "\n"
2485 "Version: " HAPROXY_VERSION "\n"
2486 "Release_date: " HAPROXY_DATE "\n"
2487 "Nbproc: %d\n"
2488 "Process_num: %d\n"
2489 "Pid: %d\n"
2490 "Uptime: %dd %dh%02dm%02ds\n"
2491 "Uptime_sec: %d\n"
2492 "Memmax_MB: %d\n"
2493 "Ulimit-n: %d\n"
2494 "Maxsock: %d\n"
2495 "Maxconn: %d\n"
2496 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002497 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002498 "CumConns: %d\n"
2499 "CumReq: %d\n"
2500#ifdef USE_OPENSSL
2501 "MaxSslConns: %d\n"
2502 "CurrSslConns: %d\n"
2503 "CumSslConns: %d\n"
2504#endif
2505 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002506 "PipesUsed: %d\n"
2507 "PipesFree: %d\n"
2508 "ConnRate: %d\n"
2509 "ConnRateLimit: %d\n"
2510 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002511 "SessRate: %d\n"
2512 "SessRateLimit: %d\n"
2513 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002514#ifdef USE_OPENSSL
2515 "SslRate: %d\n"
2516 "SslRateLimit: %d\n"
2517 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002518 "SslFrontendKeyRate: %d\n"
2519 "SslFrontendMaxKeyRate: %d\n"
2520 "SslFrontendSessionReuse_pct: %d\n"
2521 "SslBackendKeyRate: %d\n"
2522 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002523 "SslCacheLookups: %u\n"
2524 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002525#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002526 "CompressBpsIn: %u\n"
2527 "CompressBpsOut: %u\n"
2528 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002529#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002530 "ZlibMemUsage: %ld\n"
2531 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002532#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002533 "Tasks: %d\n"
2534 "Run_queue: %d\n"
2535 "Idle_pct: %d\n"
2536 "node: %s\n"
2537 "description: %s\n"
2538 "",
2539 global.nbproc,
2540 relative_pid,
2541 pid,
2542 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2543 up,
2544 global.rlimit_memmax,
2545 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002546 global.maxsock, global.maxconn, global.hardmaxconn,
2547 actconn, totalconn, global.req_count,
2548#ifdef USE_OPENSSL
2549 global.maxsslconn, sslconns, totalsslconns,
2550#endif
2551 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002552 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002553 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002554#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002555 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2556 ssl_key_rate, global.ssl_fe_keys_max,
2557 ssl_reuse,
2558 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002559 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002560#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002561 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2562 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002563#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002564 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002565#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002566 nb_tasks_cur, run_queue_cur, idle_pct,
2567 global.node, global.desc ? global.desc : ""
2568 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002569
2570 if (bi_putchk(si->ib, &trash) == -1)
2571 return 0;
2572
2573 return 1;
2574}
2575
Willy Tarreau12833bb2014-01-28 16:49:56 +01002576/* This function dumps memory usage information onto the stream interface's
2577 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2578 * completion. No state is used.
2579 */
2580static int stats_dump_pools_to_buffer(struct stream_interface *si)
2581{
2582 dump_pools_to_trash();
2583 if (bi_putchk(si->ib, &trash) == -1)
2584 return 0;
2585 return 1;
2586}
2587
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002588/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2589 * the state from stream interface <si>. The caller is responsible for clearing
2590 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002591 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002592static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002593{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002594 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002595 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002596
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002597 if (!(px->cap & PR_CAP_FE))
2598 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002599
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002600 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002601 return 0;
2602
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002603 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002604 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002605 /* name, queue */
2606 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002607
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002608 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002609 /* Column sub-heading for Enable or Disable server */
2610 chunk_appendf(&trash, "<td></td>");
2611 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002612
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002613 chunk_appendf(&trash,
2614 "<td class=ac>"
2615 "<a name=\"%s/Frontend\"></a>"
2616 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2617 "<td colspan=3></td>"
2618 "",
2619 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002620
Willy Tarreau466c9b52012-12-23 02:25:03 +01002621 chunk_appendf(&trash,
2622 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002623 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002624 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2625 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2626 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002627 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2628 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2629 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002630
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002631 if (px->mode == PR_MODE_HTTP)
2632 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002633 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002634 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002635
2636 chunk_appendf(&trash,
2637 "</table></div></u></td>"
2638 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002639 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002640 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2641 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2642 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002643 U2H(px->fe_counters.sps_max),
2644 U2H(px->fe_counters.cps_max),
2645 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002646
2647 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002648 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002649 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002650 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002651
2652 chunk_appendf(&trash,
2653 "</table></div></u></td>"
2654 /* sessions rate : limit */
2655 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002656 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002657
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002658 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002659 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002660 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002661 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002662 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2663 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002664 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002665 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2666 U2H(px->fe_counters.cum_sess),
2667 U2H(px->fe_counters.cum_conn),
2668 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002669
Willy Tarreau466c9b52012-12-23 02:25:03 +01002670 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002671 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002672 chunk_appendf(&trash,
2673 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2674 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2675 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2676 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2677 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2678 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2679 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2680 "<tr><th>- other responses:</th><td>%s</td></tr>"
2681 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2682 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002683 U2H(px->fe_counters.p.http.cum_req),
2684 U2H(px->fe_counters.p.http.rsp[1]),
2685 U2H(px->fe_counters.p.http.rsp[2]),
2686 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002687 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002688 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002689 U2H(px->fe_counters.p.http.rsp[3]),
2690 U2H(px->fe_counters.p.http.rsp[4]),
2691 U2H(px->fe_counters.p.http.rsp[5]),
2692 U2H(px->fe_counters.p.http.rsp[0]),
2693 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002694 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002695
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002696 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002697 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002698 /* sessions: lbtot, lastsess */
2699 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002700 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002701 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002702 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002703 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002704
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002705 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002706 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002707 "<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 +01002708 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002709 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002710 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2711 px->fe_counters.comp_in ?
2712 (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 +01002713 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002714
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002715 chunk_appendf(&trash,
2716 /* denied: req, resp */
2717 "<td>%s</td><td>%s</td>"
2718 /* errors : request, connect, response */
2719 "<td>%s</td><td></td><td></td>"
2720 /* warnings: retries, redispatches */
2721 "<td></td><td></td>"
2722 /* server status : reflect frontend status */
2723 "<td class=ac>%s</td>"
2724 /* rest of server: nothing */
2725 "<td class=ac colspan=8></td></tr>"
2726 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002727 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2728 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002729 px->state == PR_STREADY ? "OPEN" :
2730 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002731 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002732 else { /* CSV mode */
2733 chunk_appendf(&trash,
2734 /* pxid, name, queue cur, queue max, */
2735 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002736 /* sessions : current, max, limit, total */
2737 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002738 /* bytes : in, out */
2739 "%lld,%lld,"
2740 /* denied: req, resp */
2741 "%lld,%lld,"
2742 /* errors : request, connect, response */
2743 "%lld,,,"
2744 /* warnings: retries, redispatches */
2745 ",,"
2746 /* server status : reflect frontend status */
2747 "%s,"
2748 /* rest of server: nothing */
2749 ",,,,,,,,"
2750 /* pid, iid, sid, throttle, lbtot, tracked, type */
2751 "%d,%d,0,,,,%d,"
2752 /* rate, rate_lim, rate_max */
2753 "%u,%u,%u,"
2754 /* check_status, check_code, check_duration */
2755 ",,,",
2756 px->id,
2757 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2758 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2759 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2760 px->fe_counters.failed_req,
2761 px->state == PR_STREADY ? "OPEN" :
2762 px->state == PR_STFULL ? "FULL" : "STOP",
2763 relative_pid, px->uuid, STATS_TYPE_FE,
2764 read_freq_ctr(&px->fe_sess_per_sec),
2765 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002766
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002767 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2768 if (px->mode == PR_MODE_HTTP) {
2769 for (i=1; i<6; i++)
2770 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2771 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2772 }
2773 else
2774 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002775
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002776 /* failed health analyses */
2777 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002778
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002779 /* requests : req_rate, req_rate_max, req_tot, */
2780 chunk_appendf(&trash, "%u,%u,%lld,",
2781 read_freq_ctr(&px->fe_req_per_sec),
2782 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2783
2784 /* errors: cli_aborts, srv_aborts */
2785 chunk_appendf(&trash, ",,");
2786
2787 /* compression: in, out, bypassed */
2788 chunk_appendf(&trash, "%lld,%lld,%lld,",
2789 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2790
2791 /* compression: comp_rsp */
2792 chunk_appendf(&trash, "%lld,",
2793 px->fe_counters.p.http.comp_rsp);
2794
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002795 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2796 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002797
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002798 /* finish with EOL */
2799 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002800 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002801 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002802}
2803
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002804/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2805 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2806 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2807 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002808 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002809static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002810{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002811 struct appctx *appctx = __objt_appctx(si->end);
2812
2813 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002814 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002815 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002816 /* Column sub-heading for Enable or Disable server */
2817 chunk_appendf(&trash, "<td></td>");
2818 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002819 chunk_appendf(&trash,
2820 /* frontend name, listener name */
2821 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2822 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2823 "",
2824 px->id, l->name,
2825 (flags & ST_SHLGNDS)?"<u>":"",
2826 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002827
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002828 if (flags & ST_SHLGNDS) {
2829 char str[INET6_ADDRSTRLEN];
2830 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002831
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002832 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002833
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002834 port = get_host_port(&l->addr);
2835 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2836 case AF_INET:
2837 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2838 break;
2839 case AF_INET6:
2840 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2841 break;
2842 case AF_UNIX:
2843 chunk_appendf(&trash, "unix, ");
2844 break;
2845 case -1:
2846 chunk_appendf(&trash, "(%s), ", strerror(errno));
2847 break;
2848 }
Willy Tarreau91861262007-10-17 17:06:05 +02002849
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002850 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002851 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002852 }
Willy Tarreau91861262007-10-17 17:06:05 +02002853
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002854 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002855 /* queue */
2856 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002857 /* sessions rate: current, max, limit */
2858 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002859 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002860 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002861 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002862 /* bytes: in, out */
2863 "<td>%s</td><td>%s</td>"
2864 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002865 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002866 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2867 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002868
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002869 chunk_appendf(&trash,
2870 /* denied: req, resp */
2871 "<td>%s</td><td>%s</td>"
2872 /* errors: request, connect, response */
2873 "<td>%s</td><td></td><td></td>"
2874 /* warnings: retries, redispatches */
2875 "<td></td><td></td>"
2876 /* server status: reflect listener status */
2877 "<td class=ac>%s</td>"
2878 /* rest of server: nothing */
2879 "<td class=ac colspan=8></td></tr>"
2880 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002881 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2882 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002883 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2884 }
2885 else { /* CSV mode */
2886 chunk_appendf(&trash,
2887 /* pxid, name, queue cur, queue max, */
2888 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002889 /* sessions: current, max, limit, total */
2890 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002891 /* bytes: in, out */
2892 "%lld,%lld,"
2893 /* denied: req, resp */
2894 "%lld,%lld,"
2895 /* errors: request, connect, response */
2896 "%lld,,,"
2897 /* warnings: retries, redispatches */
2898 ",,"
2899 /* server status: reflect listener status */
2900 "%s,"
2901 /* rest of server: nothing */
2902 ",,,,,,,,"
2903 /* pid, iid, sid, throttle, lbtot, tracked, type */
2904 "%d,%d,%d,,,,%d,"
2905 /* rate, rate_lim, rate_max */
2906 ",,,"
2907 /* check_status, check_code, check_duration */
2908 ",,,"
2909 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2910 ",,,,,,"
2911 /* failed health analyses */
2912 ","
2913 /* requests : req_rate, req_rate_max, req_tot, */
2914 ",,,"
2915 /* errors: cli_aborts, srv_aborts */
2916 ",,"
2917 /* compression: in, out, bypassed, comp_rsp */
2918 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002919 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2920 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002921 "\n",
2922 px->id, l->name,
2923 l->nbconn, l->counters->conn_max,
2924 l->maxconn, l->counters->cum_conn,
2925 l->counters->bytes_in, l->counters->bytes_out,
2926 l->counters->denied_req, l->counters->denied_resp,
2927 l->counters->failed_req,
2928 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2929 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2930 }
2931 return 1;
2932}
Willy Tarreau91861262007-10-17 17:06:05 +02002933
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002934/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2935 * from stream interface <si>, stats flags <flags>, and server state <state>.
2936 * The caller is responsible for clearing the trash if needed. Returns non-zero
2937 * if it emits anything, zero otherwise. The <state> parameter can take the
Willy Tarreau9638efa2014-05-23 11:19:57 +02002938 * following values : 0=DOWN, 1=DOWN(agent) 2=going up, 3=going down, 4=UP, 5,6=NOLB,
2939 * 7,8=DRAIN, 9=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002940 */
2941static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2942{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002943 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002944 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002945 char str[INET6_ADDRSTRLEN];
2946 struct chunk src;
2947 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002948
Willy Tarreau32091232014-05-16 13:52:00 +02002949 /* we have "via" which is the tracked server as described in the configuration,
2950 * and "ref" which is the checked server and the end of the chain.
2951 */
2952 via = sv->track ? sv->track : sv;
2953 ref = via;
2954 while (ref->track)
2955 ref = ref->track;
2956
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002957 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02002958 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002959 "DOWN",
Willy Tarreau9638efa2014-05-23 11:19:57 +02002960 "DOWN (agent)",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002961 "DN %d/%d &uarr;",
2962 "UP %d/%d &darr;",
2963 "UP",
2964 "NOLB %d/%d &darr;",
2965 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002966 "DRAIN %d/%d &darr;",
2967 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002968 "<i>no check</i>"
2969 };
Willy Tarreau91861262007-10-17 17:06:05 +02002970
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002971 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002972 chunk_appendf(&trash, "<tr class=\"maintain\">");
2973 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002974 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002975 "<tr class=\"%s%d\">",
Willy Tarreauc93cd162014-05-13 15:54:22 +02002976 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002977
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002978 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002979 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002980 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2981 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002982
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002983 chunk_appendf(&trash,
2984 "<td class=ac><a name=\"%s/%s\"></a>%s"
2985 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2986 "",
2987 px->id, sv->id,
2988 (flags & ST_SHLGNDS) ? "<u>" : "",
2989 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002990
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002991 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002992 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002993
2994 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2995 case AF_INET:
2996 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2997 break;
2998 case AF_INET6:
2999 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3000 break;
3001 case AF_UNIX:
3002 chunk_appendf(&trash, "unix, ");
3003 break;
3004 case -1:
3005 chunk_appendf(&trash, "(%s), ", strerror(errno));
3006 break;
3007 default: /* address family not supported */
3008 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003009 }
Willy Tarreau91861262007-10-17 17:06:05 +02003010
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003011 /* id */
3012 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003013
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003014 /* cookie */
3015 if (sv->cookie) {
3016 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003017
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003018 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3019 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003020
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003021 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003022 }
3023
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003024 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003025 }
Willy Tarreau91861262007-10-17 17:06:05 +02003026
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003027 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003028 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003029 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003030 /* sessions rate : current, max, limit */
3031 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003032 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003033 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003034 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3035 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003036
Willy Tarreau466c9b52012-12-23 02:25:03 +01003037
3038 chunk_appendf(&trash,
3039 /* sessions: current, max, limit, total */
3040 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003041 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003042 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3043 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003044 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3045 U2H(sv->counters.cum_sess),
3046 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003047
Willy Tarreau466c9b52012-12-23 02:25:03 +01003048 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3049 if (px->mode == PR_MODE_HTTP) {
3050 unsigned long long tot;
3051 for (tot = i = 0; i < 6; i++)
3052 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003053
Willy Tarreau466c9b52012-12-23 02:25:03 +01003054 chunk_appendf(&trash,
3055 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3056 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3057 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3058 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3059 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3060 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3061 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3062 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003063 U2H(tot),
3064 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3065 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3066 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3067 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3068 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3069 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 +02003070 }
Willy Tarreau91861262007-10-17 17:06:05 +02003071
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003072 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3073 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES)));
3074 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES)));
3075 if (px->mode == PR_MODE_HTTP)
3076 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES)));
3077 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES)));
3078
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003079 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003080 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003081 /* sessions: lbtot, last */
3082 "<td>%s</td><td>%s</td>",
3083 U2H(sv->counters.cum_lbconn),
3084 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003085
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003086 chunk_appendf(&trash,
3087 /* bytes : in, out */
3088 "<td>%s</td><td>%s</td>"
3089 /* denied: req, resp */
3090 "<td></td><td>%s</td>"
3091 /* errors : request, connect */
3092 "<td></td><td>%s</td>"
3093 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003094 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003095 /* warnings: retries, redispatches */
3096 "<td>%lld</td><td>%lld</td>"
3097 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003098 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3099 U2H(sv->counters.failed_secu),
3100 U2H(sv->counters.failed_conns),
3101 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003102 sv->counters.cli_aborts,
3103 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003104 sv->counters.retries, sv->counters.redispatches);
3105
3106 /* status, lest check */
3107 chunk_appendf(&trash, "<td class=ac>");
3108
Willy Tarreau20125212014-05-13 19:44:56 +02003109 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003110 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3111 chunk_appendf(&trash, "MAINT");
3112 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003113 else if ((ref->agent.state & CHK_ST_ENABLED) && (ref->state == SRV_ST_STOPPED)) {
3114 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003115 /* DOWN (agent) */
3116 chunk_appendf(&trash, srv_hlt_st[1], "GCC: your -Werror=format-security is bogus, annoying, and hides real bugs, I don't thank you, really!");
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003117 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003118 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003119 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3120 chunk_appendf(&trash,
3121 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003122 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3123 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003124 }
Willy Tarreau91861262007-10-17 17:06:05 +02003125
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003126 if ((sv->state == SRV_ST_STOPPED) &&
3127 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3128 chunk_appendf(&trash,
3129 "</td><td class=ac><u> %s%s",
3130 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3131 get_check_status_info(sv->agent.status));
3132
3133 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3134 chunk_appendf(&trash, "/%d", sv->agent.code);
3135
3136 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3137 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3138
3139 chunk_appendf(&trash, "<div class=tips>%s",
3140 get_check_status_description(sv->agent.status));
3141 if (*sv->agent.desc) {
3142 chunk_appendf(&trash, ": ");
3143 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3144 chunk_htmlencode(&trash, &src);
3145 }
3146 chunk_appendf(&trash, "</div></u>");
3147 }
3148 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003149 chunk_appendf(&trash,
3150 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003151 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003152 get_check_status_info(sv->check.status));
3153
3154 if (sv->check.status >= HCHK_STATUS_L57DATA)
3155 chunk_appendf(&trash, "/%d", sv->check.code);
3156
3157 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003158 chunk_appendf(&trash, " in %lums", sv->check.duration);
3159
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003160 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003161 get_check_status_description(sv->check.status));
3162 if (*sv->check.desc) {
3163 chunk_appendf(&trash, ": ");
3164 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3165 chunk_htmlencode(&trash, &src);
3166 }
3167 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003168 }
3169 else
3170 chunk_appendf(&trash, "</td><td>");
3171
3172 chunk_appendf(&trash,
3173 /* weight */
3174 "</td><td class=ac>%d</td>"
3175 /* act, bck */
3176 "<td class=ac>%s</td><td class=ac>%s</td>"
3177 "",
3178 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003179 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3180 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003181
3182 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003183 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003184 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003185
3186 if (ref->observe)
3187 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3188
3189 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003190 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003191 "<td>%lld</td><td>%s</td>"
3192 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003193 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003194 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3195 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003196 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3197 /* tracking a server */
3198 chunk_appendf(&trash,
3199 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003200 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003201 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003202 else
3203 chunk_appendf(&trash, "<td colspan=3></td>");
3204
3205 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003206 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003207 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003208 else
3209 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3210 }
3211 else { /* CSV mode */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003212 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003213 "DOWN,",
Willy Tarreau9638efa2014-05-23 11:19:57 +02003214 "DOWN (agent),",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003215 "DOWN %d/%d,",
3216 "UP %d/%d,",
3217 "UP,",
3218 "NOLB %d/%d,",
3219 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09003220 "DRAIN %d/%d,",
3221 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003222 "no check,"
3223 };
3224
3225 chunk_appendf(&trash,
3226 /* pxid, name */
3227 "%s,%s,"
3228 /* queue : current, max */
3229 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003230 /* sessions : current, max, limit, total */
3231 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003232 /* bytes : in, out */
3233 "%lld,%lld,"
3234 /* denied: req, resp */
3235 ",%lld,"
3236 /* errors : request, connect, response */
3237 ",%lld,%lld,"
3238 /* warnings: retries, redispatches */
3239 "%lld,%lld,"
3240 "",
3241 px->id, sv->id,
3242 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003243 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003244 sv->counters.bytes_in, sv->counters.bytes_out,
3245 sv->counters.failed_secu,
3246 sv->counters.failed_conns, sv->counters.failed_resp,
3247 sv->counters.retries, sv->counters.redispatches);
3248
3249 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003250 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003251 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003252 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003253 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003254 else
3255 chunk_appendf(&trash,
3256 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003257 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3258 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003259
3260 chunk_appendf(&trash,
3261 /* weight, active, backup */
3262 "%d,%d,%d,"
3263 "",
3264 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003265 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3266 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003267
3268 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003269 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003270 chunk_appendf(&trash,
3271 "%lld,%lld,%d,%d,",
3272 sv->counters.failed_checks, sv->counters.down_trans,
3273 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3274 else
3275 chunk_appendf(&trash, ",,,,");
3276
3277 /* queue limit, pid, iid, sid, */
3278 chunk_appendf(&trash,
3279 "%s,"
3280 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003281 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003282 relative_pid, px->uuid, sv->puid);
3283
3284 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003285 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003286 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003287
3288 /* sessions: lbtot */
3289 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3290
3291 /* tracked */
3292 if (sv->track)
3293 chunk_appendf(&trash, "%s/%s,",
3294 sv->track->proxy->id, sv->track->id);
3295 else
3296 chunk_appendf(&trash, ",");
3297
3298 /* type */
3299 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3300
3301 /* rate */
3302 chunk_appendf(&trash, "%u,,%u,",
3303 read_freq_ctr(&sv->sess_per_sec),
3304 sv->counters.sps_max);
3305
Willy Tarreauff5ae352013-12-11 20:36:34 +01003306 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003307 /* check_status */
3308 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3309
3310 /* check_code */
3311 if (sv->check.status >= HCHK_STATUS_L57DATA)
3312 chunk_appendf(&trash, "%u,", sv->check.code);
3313 else
3314 chunk_appendf(&trash, ",");
3315
3316 /* check_duration */
3317 if (sv->check.status >= HCHK_STATUS_CHECKED)
3318 chunk_appendf(&trash, "%lu,", sv->check.duration);
3319 else
3320 chunk_appendf(&trash, ",");
3321
3322 }
3323 else
3324 chunk_appendf(&trash, ",,,");
3325
3326 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3327 if (px->mode == PR_MODE_HTTP) {
3328 for (i=1; i<6; i++)
3329 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3330
3331 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3332 }
3333 else
3334 chunk_appendf(&trash, ",,,,,,");
3335
3336 /* failed health analyses */
3337 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3338
3339 /* requests : req_rate, req_rate_max, req_tot, */
3340 chunk_appendf(&trash, ",,,");
3341
3342 /* errors: cli_aborts, srv_aborts */
3343 chunk_appendf(&trash, "%lld,%lld,",
3344 sv->counters.cli_aborts, sv->counters.srv_aborts);
3345
3346 /* compression: in, out, bypassed, comp_rsp */
3347 chunk_appendf(&trash, ",,,,");
3348
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003349 /* lastsess */
3350 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3351
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003352 /* capture of last check and agent statuses */
3353 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->check.desc) : "");
3354 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->agent.desc) : "");
3355
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003356 /* qtime, ctime, rtime, ttime, */
3357 chunk_appendf(&trash, "%u,%u,%u,%u,",
3358 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3359 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3360 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3361 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3362
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 /* finish with EOL */
3364 chunk_appendf(&trash, "\n");
3365 }
3366 return 1;
3367}
3368
3369/* Dumps a line for backend <px> to the trash for and uses the state from stream
3370 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3371 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3372 */
3373static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3374{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003375 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003376 struct chunk src;
3377 int i;
3378
3379 if (!(px->cap & PR_CAP_BE))
3380 return 0;
3381
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003382 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003383 return 0;
3384
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003385 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003386 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003387 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003388 /* Column sub-heading for Enable or Disable server */
3389 chunk_appendf(&trash, "<td></td>");
3390 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003391 chunk_appendf(&trash,
3392 "<td class=ac>"
3393 /* name */
3394 "%s<a name=\"%s/Backend\"></a>"
3395 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3396 "",
3397 (flags & ST_SHLGNDS)?"<u>":"",
3398 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399
3400 if (flags & ST_SHLGNDS) {
3401 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003402 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3404
3405 /* cookie */
3406 if (px->cookie_name) {
3407 chunk_appendf(&trash, ", cookie: '");
3408 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3409 chunk_htmlencode(&trash, &src);
3410 chunk_appendf(&trash, "'");
3411 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003412 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003413 }
3414
3415 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003416 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003417 /* queue : current, max */
3418 "<td>%s</td><td>%s</td><td></td>"
3419 /* sessions rate : current, max, limit */
3420 "<td>%s</td><td>%s</td><td></td>"
3421 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003423 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3424 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003425
3426 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003427 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003429 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003430 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003431 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003432 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3433 U2H(px->be_counters.cum_conn),
3434 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003435
Willy Tarreau466c9b52012-12-23 02:25:03 +01003436 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003437 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003438 chunk_appendf(&trash,
3439 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3440 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3441 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3442 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3443 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3444 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3445 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3446 "<tr><th>- other responses:</th><td>%s</td></tr>"
3447 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003448 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003449 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003450 U2H(px->be_counters.p.http.cum_req),
3451 U2H(px->be_counters.p.http.rsp[1]),
3452 U2H(px->be_counters.p.http.rsp[2]),
3453 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003454 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003455 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003456 U2H(px->be_counters.p.http.rsp[3]),
3457 U2H(px->be_counters.p.http.rsp[4]),
3458 U2H(px->be_counters.p.http.rsp[5]),
3459 U2H(px->be_counters.p.http.rsp[0]),
3460 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003461 }
3462
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003463 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES)));
3464 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES)));
3465 if (px->mode == PR_MODE_HTTP)
3466 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES)));
3467 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES)));
3468
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003469 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003470 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003471 /* sessions: lbtot, last */
3472 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003473 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003474 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003475 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003476 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003477 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003478 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003479
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003480 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003481 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003482 "<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 +01003483 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003484 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003485 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3486 px->be_counters.comp_in ?
3487 (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 +01003488 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3489
3490 chunk_appendf(&trash,
3491 /* denied: req, resp */
3492 "<td>%s</td><td>%s</td>"
3493 /* errors : request, connect */
3494 "<td></td><td>%s</td>"
3495 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003496 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003497 /* warnings: retries, redispatches */
3498 "<td>%lld</td><td>%lld</td>"
3499 /* backend status: reflect backend status (up/down): we display UP
3500 * if the backend has known working servers or if it has no server at
3501 * all (eg: for stats). Then we display the total weight, number of
3502 * active and backups. */
3503 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3504 "<td class=ac>%d</td><td class=ac>%d</td>"
3505 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003506 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3507 U2H(px->be_counters.failed_conns),
3508 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003509 px->be_counters.cli_aborts,
3510 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003511 px->be_counters.retries, px->be_counters.redispatches,
3512 human_time(now.tv_sec - px->last_change, 1),
3513 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3514 "<font color=\"red\"><b>DOWN</b></font>",
3515 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3516 px->srv_act, px->srv_bck);
3517
3518 chunk_appendf(&trash,
3519 /* rest of backend: nothing, down transitions, total downtime, throttle */
3520 "<td class=ac>&nbsp;</td><td>%d</td>"
3521 "<td>%s</td>"
3522 "<td></td>"
3523 "</tr>",
3524 px->down_trans,
3525 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3526 }
3527 else { /* CSV mode */
3528 chunk_appendf(&trash,
3529 /* pxid, name */
3530 "%s,BACKEND,"
3531 /* queue : current, max */
3532 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003533 /* sessions : current, max, limit, total */
3534 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003535 /* bytes : in, out */
3536 "%lld,%lld,"
3537 /* denied: req, resp */
3538 "%lld,%lld,"
3539 /* errors : request, connect, response */
3540 ",%lld,%lld,"
3541 /* warnings: retries, redispatches */
3542 "%lld,%lld,"
3543 /* backend status: reflect backend status (up/down): we display UP
3544 * if the backend has known working servers or if it has no server at
3545 * all (eg: for stats). Then we display the total weight, number of
3546 * active and backups. */
3547 "%s,"
3548 "%d,%d,%d,"
3549 /* rest of backend: nothing, down transitions, last change, total downtime */
3550 ",%d,%d,%d,,"
3551 /* pid, iid, sid, throttle, lbtot, tracked, type */
3552 "%d,%d,0,,%lld,,%d,"
3553 /* rate, rate_lim, rate_max, */
3554 "%u,,%u,"
3555 /* check_status, check_code, check_duration */
3556 ",,,",
3557 px->id,
3558 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3559 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3560 px->be_counters.bytes_in, px->be_counters.bytes_out,
3561 px->be_counters.denied_req, px->be_counters.denied_resp,
3562 px->be_counters.failed_conns, px->be_counters.failed_resp,
3563 px->be_counters.retries, px->be_counters.redispatches,
3564 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3565 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3566 px->srv_act, px->srv_bck,
3567 px->down_trans, (int)(now.tv_sec - px->last_change),
3568 px->srv?be_downtime(px):0,
3569 relative_pid, px->uuid,
3570 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3571 read_freq_ctr(&px->be_sess_per_sec),
3572 px->be_counters.sps_max);
3573
3574 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3575 if (px->mode == PR_MODE_HTTP) {
3576 for (i=1; i<6; i++)
3577 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3578 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3579 }
3580 else
3581 chunk_appendf(&trash, ",,,,,,");
3582
3583 /* failed health analyses */
3584 chunk_appendf(&trash, ",");
3585
3586 /* requests : req_rate, req_rate_max, req_tot, */
3587 chunk_appendf(&trash, ",,,");
3588
3589 /* errors: cli_aborts, srv_aborts */
3590 chunk_appendf(&trash, "%lld,%lld,",
3591 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3592
3593 /* compression: in, out, bypassed */
3594 chunk_appendf(&trash, "%lld,%lld,%lld,",
3595 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3596
3597 /* compression: comp_rsp */
3598 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3599
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003600 /* lastsess, last_chk, last_agt, */
3601 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003602
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003603 /* qtime, ctime, rtime, ttime, */
3604 chunk_appendf(&trash, "%u,%u,%u,%u,",
3605 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3606 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3607 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3608 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3609
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003610 /* finish with EOL */
3611 chunk_appendf(&trash, "\n");
3612 }
3613 return 1;
3614}
3615
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003616/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003617 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003618 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003619 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003620static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003621{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003622 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003623 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3624
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003625 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003626 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003627
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003628 /* 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 +02003629 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003630 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003631 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003632 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3633 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003634 }
3635
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003636 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003637 "<form action=\"%s%s%s%s\" method=\"post\">",
3638 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003639 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3640 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003641 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003642 }
3643
3644 /* print a new table */
3645 chunk_appendf(&trash,
3646 "<table class=\"tbl\" width=\"100%%\">\n"
3647 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003648 "<th class=\"pxname\" width=\"10%%\">");
3649
3650 chunk_appendf(&trash,
3651 "<a name=\"%s\"></a>%s"
3652 "<a class=px href=\"#%s\">%s</a>",
3653 px->id,
3654 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3655 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003656
3657 if (uri->flags & ST_SHLGNDS) {
3658 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003659 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003660 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3661 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003662 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003663 }
3664
3665 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003666 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003667 "<th class=\"%s\" width=\"90%%\">%s</th>"
3668 "</tr>\n"
3669 "</table>\n"
3670 "<table class=\"tbl\" width=\"100%%\">\n"
3671 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003672 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3673 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3674
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003675 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003676 /* Column heading for Enable or Disable server */
3677 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003678 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003679
3680 chunk_appendf(&trash,
3681 "<th rowspan=2></th>"
3682 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003683 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003684 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3685 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3686 "<th colspan=9>Server</th>"
3687 "</tr>\n"
3688 "<tr class=\"titre\">"
3689 "<th>Cur</th><th>Max</th><th>Limit</th>"
3690 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003691 "<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 +01003692 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3693 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3694 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3695 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3696 "<th>Thrtle</th>\n"
3697 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003698}
3699
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003700/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003701 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003702 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003703static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003704{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003705 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003706 chunk_appendf(&trash, "</table>");
3707
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003708 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003709 /* close the form used to enable/disable this proxy servers */
3710 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003711 "Choose the action to perform on the checked servers : "
3712 "<select name=action>"
3713 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003714 "<option value=\"ready\">Set state to READY</option>"
3715 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corted38f5c02014-07-02 17:49:34 +02003716 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003717 "<option value=\"dhlth\">Health: disable checks</option>"
3718 "<option value=\"ehlth\">Health: enable checks</option>"
3719 "<option value=\"hrunn\">Health: force UP</option>"
3720 "<option value=\"hnolb\">Health: force NOLB</option>"
3721 "<option value=\"hdown\">Health: force DOWN</option>"
3722 "<option value=\"dagent\">Agent: disable checks</option>"
3723 "<option value=\"eagent\">Agent: enable checks</option>"
3724 "<option value=\"arunn\">Agent: force UP</option>"
3725 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003726 "<option value=\"shutdown\">Kill Sessions</option>"
3727 "</select>"
3728 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3729 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3730 "</form>",
3731 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003732 }
3733
3734 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003735}
Willy Tarreau91861262007-10-17 17:06:05 +02003736
3737/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003738 * Dumps statistics for a proxy. The output is sent to the stream interface's
3739 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3740 * buffer space, or non-zero if everything completed. This function is used
3741 * both by the CLI and the HTTP entry points, and is able to dump the output
3742 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003743 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003744static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003745{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003746 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003747 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003748 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003749 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003750 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003751
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003752 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003753
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003754 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003755 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003756 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003757 if (uri && uri->scope) {
3758 /* we have a limited scope, we have to check the proxy name */
3759 struct stat_scope *scope;
3760 int len;
3761
3762 len = strlen(px->id);
3763 scope = uri->scope;
3764
3765 while (scope) {
3766 /* match exact proxy name */
3767 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3768 break;
3769
3770 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003771 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003772 break;
3773 scope = scope->next;
3774 }
3775
3776 /* proxy name not found : don't dump anything */
3777 if (scope == NULL)
3778 return 1;
3779 }
3780
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003781 /* if the user has requested a limited output and the proxy
3782 * name does not match, skip it.
3783 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003784 if (appctx->ctx.stats.scope_len &&
3785 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 +02003786 return 1;
3787
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003788 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3789 (appctx->ctx.stats.iid != -1) &&
3790 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003791 return 1;
3792
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003793 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003794 /* fall through */
3795
Willy Tarreau295a8372011-03-10 11:25:07 +01003796 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003797 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003798 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003799 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003800 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003801 }
Willy Tarreau91861262007-10-17 17:06:05 +02003802
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003803 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003804 /* fall through */
3805
Willy Tarreau295a8372011-03-10 11:25:07 +01003806 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003807 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003808 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003809 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003810 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003811
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003812 appctx->ctx.stats.l = px->conf.listeners.n;
3813 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003814 /* fall through */
3815
Willy Tarreau295a8372011-03-10 11:25:07 +01003816 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003817 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003818 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003819 if (buffer_almost_full(rep->buf)) {
3820 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003821 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003822 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003823
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003824 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003825 if (!l->counters)
3826 continue;
3827
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003828 if (appctx->ctx.stats.flags & STAT_BOUND) {
3829 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003830 break;
3831
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003832 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003833 continue;
3834 }
3835
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003836 /* print the frontend */
3837 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3838 if (bi_putchk(rep, &trash) == -1)
3839 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003840 }
3841
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003842 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3843 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003844 /* fall through */
3845
Willy Tarreau295a8372011-03-10 11:25:07 +01003846 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003847 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003848 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02003849 int sv_state;
Willy Tarreau91861262007-10-17 17:06:05 +02003850
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003851 if (buffer_almost_full(rep->buf)) {
3852 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003853 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003854 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003855
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003856 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003857
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003858 if (appctx->ctx.stats.flags & STAT_BOUND) {
3859 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003860 break;
3861
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003862 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003863 continue;
3864 }
3865
Willy Tarreau32091232014-05-16 13:52:00 +02003866 svs = sv;
3867 while (svs->track)
3868 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003869
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003870 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
3871 /* server is UP. The possibilities are :
Willy Tarreau9638efa2014-05-23 11:19:57 +02003872 * - UP, draining, going down => state = 7
3873 * - UP, going down => state = 3
3874 * - UP, draining => state = 8
3875 * - UP, checked => state = 4
3876 * - UP, not checked nor tracked => state = 9
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003877 */
3878
3879 if ((svs->check.state & CHK_ST_ENABLED) &&
3880 (svs->check.health < svs->check.rise + svs->check.fall - 1))
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003881 sv_state = 3;
Willy Tarreau9638efa2014-05-23 11:19:57 +02003882 else
3883 sv_state = 4;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003884
Willy Tarreauefe28222014-05-21 17:13:13 +02003885 if (server_is_draining(sv))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003886 sv_state += 4;
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003887
Willy Tarreau9638efa2014-05-23 11:19:57 +02003888 if (sv_state == 4 && !(svs->check.state & CHK_ST_ENABLED))
3889 sv_state = 9; /* unchecked UP */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003890 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003891 else if (sv->state == SRV_ST_STOPPING) {
3892 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
3893 (svs->check.health == svs->check.rise + svs->check.fall - 1))
Willy Tarreau9638efa2014-05-23 11:19:57 +02003894 sv_state = 6; /* NOLB */
Willy Tarreau91861262007-10-17 17:06:05 +02003895 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003896 sv_state = 5; /* NOLB going down */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003897 }
3898 else { /* stopped */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003899 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health)
3900 sv_state = 1; /* DOWN (agent) */
3901 else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003902 sv_state = 0; /* DOWN */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003903 else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED))
3904 sv_state = 2; /* going up */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003905 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003906 sv_state = 0; /* DOWN, unchecked */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003907 }
Willy Tarreau91861262007-10-17 17:06:05 +02003908
Willy Tarreau9638efa2014-05-23 11:19:57 +02003909 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003910 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003911 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003912 continue;
3913 }
3914
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003915 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3916 if (bi_putchk(rep, &trash) == -1)
3917 return 0;
3918 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003919
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003920 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003921 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003922
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003923 case STAT_PX_ST_BE:
3924 /* print the backend */
3925 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3926 if (bi_putchk(rep, &trash) == -1)
3927 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003928
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003929 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003930 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003931
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003932 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003933 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003934 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003935 if (bi_putchk(rep, &trash) == -1)
3936 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003937 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003938
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003939 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003940 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003941
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003942 case STAT_PX_ST_FIN:
3943 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003944
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003945 default:
3946 /* unknown state, we should put an abort() here ! */
3947 return 1;
3948 }
3949}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003950
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003951/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3952 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003953 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003954static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003955{
3956 /* WARNING! This must fit in the first buffer !!! */
3957 chunk_appendf(&trash,
3958 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3959 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3960 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3961 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3962 "<style type=\"text/css\"><!--\n"
3963 "body {"
3964 " font-family: arial, helvetica, sans-serif;"
3965 " font-size: 12px;"
3966 " font-weight: normal;"
3967 " color: black;"
3968 " background: white;"
3969 "}\n"
3970 "th,td {"
3971 " font-size: 10px;"
3972 "}\n"
3973 "h1 {"
3974 " font-size: x-large;"
3975 " margin-bottom: 0.5em;"
3976 "}\n"
3977 "h2 {"
3978 " font-family: helvetica, arial;"
3979 " font-size: x-large;"
3980 " font-weight: bold;"
3981 " font-style: italic;"
3982 " color: #6020a0;"
3983 " margin-top: 0em;"
3984 " margin-bottom: 0em;"
3985 "}\n"
3986 "h3 {"
3987 " font-family: helvetica, arial;"
3988 " font-size: 16px;"
3989 " font-weight: bold;"
3990 " color: #b00040;"
3991 " background: #e8e8d0;"
3992 " margin-top: 0em;"
3993 " margin-bottom: 0em;"
3994 "}\n"
3995 "li {"
3996 " margin-top: 0.25em;"
3997 " margin-right: 2em;"
3998 "}\n"
3999 ".hr {margin-top: 0.25em;"
4000 " border-color: black;"
4001 " border-bottom-style: solid;"
4002 "}\n"
4003 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4004 ".total {background: #20D0D0;color: #ffff80;}\n"
4005 ".frontend {background: #e8e8d0;}\n"
4006 ".socket {background: #d0d0d0;}\n"
4007 ".backend {background: #e8e8d0;}\n"
4008 ".active0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004009 ".active1 {background: #ff9090;}\n"
4010 ".active2 {background: #ffd020;}\n"
4011 ".active3 {background: #ffffa0;}\n"
4012 ".active4 {background: #c0ffc0;}\n"
4013 ".active5 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
4014 ".active6 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
4015 ".active7 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
4016 ".active8 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
4017 ".active9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004018 ".backup0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004019 ".backup1 {background: #ff9090;}\n"
4020 ".backup2 {background: #ff80ff;}\n"
4021 ".backup3 {background: #c060ff;}\n"
4022 ".backup4 {background: #b0d0ff;}\n"
4023 ".backup5 {background: #c060ff;}\n" /* NOLB state shows same as going down */
4024 ".backup6 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
4025 ".backup7 {background: #c060ff;}\n"
4026 ".backup8 {background: #cc9900;}\n"
4027 ".backup9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004028 ".maintain {background: #c07820;}\n"
4029 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4030 "\n"
4031 "a.px:link {color: #ffff40; text-decoration: none;}"
4032 "a.px:visited {color: #ffff40; text-decoration: none;}"
4033 "a.px:hover {color: #ffffff; text-decoration: none;}"
4034 "a.lfsb:link {color: #000000; text-decoration: none;}"
4035 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4036 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4037 "\n"
4038 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4039 "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"
4040 "table.tbl td.ac { text-align: center;}\n"
4041 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4042 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4043 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4044 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4045 "\n"
4046 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4047 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4048 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004049 "table.det { border-collapse: collapse; border-style: none; }\n"
4050 "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 +01004051 "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 +01004052 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004053 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004054 " display:block;\n"
4055 " visibility:hidden;\n"
4056 " z-index:2147483647;\n"
4057 " position:absolute;\n"
4058 " padding:2px 4px 3px;\n"
4059 " background:#f0f060; color:#000000;\n"
4060 " border:1px solid #7040c0;\n"
4061 " white-space:nowrap;\n"
4062 " font-style:normal;font-size:11px;font-weight:normal;\n"
4063 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4064 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4065 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004066 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004067 "-->\n"
4068 "</style></head>\n",
4069 (uri->flags & ST_SHNODE) ? " on " : "",
4070 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4071 );
4072}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004073
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004074/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004075 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004076 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004077 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004078static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004079{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004080 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004081 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004082 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004083
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004084 /* WARNING! this has to fit the first packet too.
4085 * We are around 3.5 kB, add adding entries will
4086 * become tricky if we want to support 4kB buffers !
4087 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004088 chunk_appendf(&trash,
4089 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4090 PRODUCT_NAME "%s</a></h1>\n"
4091 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4092 "<hr width=\"100%%\" class=\"hr\">\n"
4093 "<h3>&gt; General process information</h3>\n"
4094 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4095 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4096 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4097 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4098 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4099 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4100 "Running tasks: %d/%d; idle = %d %%<br>\n"
4101 "</td><td align=\"center\" nowrap>\n"
4102 "<table class=\"lgd\"><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004103 "<td class=\"active4\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4104 "<td class=\"backup4\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004105 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004106 "<td class=\"active3\"></td><td class=\"noborder\">active UP, going down </td>"
4107 "<td class=\"backup3\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004108 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004109 "<td class=\"active2\"></td><td class=\"noborder\">active DOWN, going up </td>"
4110 "<td class=\"backup2\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004111 "</tr><tr>\n"
4112 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004113 "<td class=\"active9\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004114 "</tr><tr>\n"
4115 "<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 -07004116 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004117 "<td class=\"active8\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004118 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004119 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004120 "</td>"
4121 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4122 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4123 "",
4124 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4125 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4126 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4127 (uri->flags & ST_SHDESC) ? ": " : "",
4128 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4129 pid, relative_pid, global.nbproc,
4130 up / 86400, (up % 86400) / 3600,
4131 (up % 3600) / 60, (up % 60),
4132 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4133 global.rlimit_memmax ? " MB" : "",
4134 global.rlimit_nofile,
4135 global.maxsock, global.maxconn, global.maxpipes,
4136 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4137 run_queue_cur, nb_tasks_cur, idle_pct
4138 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004139
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004140 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4141 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4142 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004143
4144 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02004145 "<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 +02004146 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004147 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4148 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
4149 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004150 STAT_SCOPE_TXT_MAXLEN);
4151
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004152 /* 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 +02004153 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004154 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004155 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004156 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4157 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004158 }
4159
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004160 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004161 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004162 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004163 uri->uri_prefix,
4164 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004165 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004166 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004167 else
4168 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004169 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004170 uri->uri_prefix,
4171 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004172 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004173 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004174
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004175 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004176 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004177 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004178 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004179 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004180 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004181 "",
4182 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004183 else
4184 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004185 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004186 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004187 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004188 ";norefresh",
4189 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004190 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004191
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004192 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004193 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004194 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004195 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4196 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004197 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004198
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004199 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004200 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004201 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004202 (uri->refresh > 0) ? ";norefresh" : "",
4203 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004204
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004205 chunk_appendf(&trash,
4206 "</ul></td>"
4207 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4208 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4209 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4210 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4211 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4212 "</ul>"
4213 "</td>"
4214 "</tr></table>\n"
4215 ""
4216 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004217
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004218 if (appctx->ctx.stats.st_code) {
4219 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004220 case STAT_STATUS_DONE:
4221 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004222 "<p><div class=active4>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004223 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004224 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004225 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004226 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4227 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004228 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004229 break;
4230 case STAT_STATUS_NONE:
4231 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004232 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004233 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004234 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004235 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004236 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4237 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004238 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004239 break;
4240 case STAT_STATUS_PART:
4241 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004242 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004243 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004244 "Action partially processed.<br>"
4245 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004246 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004247 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4248 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004249 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004250 break;
4251 case STAT_STATUS_ERRP:
4252 chunk_appendf(&trash,
4253 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004254 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004255 "Action not processed because of invalid parameters."
4256 "<ul>"
4257 "<li>The action is maybe unknown.</li>"
4258 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4259 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4260 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004261 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004262 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4263 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004264 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004265 break;
4266 case STAT_STATUS_EXCD:
4267 chunk_appendf(&trash,
4268 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004269 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004270 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4271 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004272 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004273 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4274 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004275 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004276 break;
4277 case STAT_STATUS_DENY:
4278 chunk_appendf(&trash,
4279 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004280 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004281 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004282 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004283 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4284 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004285 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004286 break;
4287 default:
4288 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004289 "<p><div class=active9>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004290 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004291 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004292 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004293 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4294 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004295 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004296 }
4297 chunk_appendf(&trash, "<p>\n");
4298 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004299}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004300
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004301/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4302 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004303 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004304static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004305{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004306 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004307}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004308
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004309/* This function dumps statistics onto the stream interface's read buffer in
4310 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004311 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4312 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
4313 * and the session must be closed, or -1 in case of any error. This function is
4314 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004315 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004316static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004317{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004318 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004319 struct channel *rep = si->ib;
4320 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004321
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004322 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004323
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004324 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004325 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004326 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004327 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004328
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004329 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004330 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004331 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004332 else
4333 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004334
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004335 if (bi_putchk(rep, &trash) == -1)
4336 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004337
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004338 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004339 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004340
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004341 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004342 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004343 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004344 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004345 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004346 }
Willy Tarreau91861262007-10-17 17:06:05 +02004347
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004348 appctx->ctx.stats.px = proxy;
4349 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4350 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004351 /* fall through */
4352
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004353 case STAT_ST_LIST:
4354 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004355 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004356 if (buffer_almost_full(rep->buf)) {
4357 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004358 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004359 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004360
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004361 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004362 /* skip the disabled proxies, global frontend and non-networked ones */
4363 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004364 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004365 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004366
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004367 appctx->ctx.stats.px = px->next;
4368 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004369 }
4370 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004371
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004372 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004373 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004374
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004375 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004376 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004377 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004378 if (bi_putchk(rep, &trash) == -1)
4379 return 0;
4380 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004381
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004382 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004383 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004384
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004385 case STAT_ST_FIN:
4386 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004387
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004388 default:
4389 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004390 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004391 return -1;
4392 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004393}
Willy Tarreauae526782010-03-04 20:34:23 +01004394
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004395/* We reached the stats page through a POST request. The appctx is
4396 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004397 * Parse the posted data and enable/disable servers if necessary.
4398 * Returns 1 if request was parsed or zero if it needs more data.
4399 */
4400static int stats_process_http_post(struct stream_interface *si)
4401{
4402 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004403 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004404
4405 struct proxy *px = NULL;
4406 struct server *sv = NULL;
4407
4408 char key[LINESIZE];
4409 int action = ST_ADM_ACTION_NONE;
4410 int reprocess = 0;
4411
4412 int total_servers = 0;
4413 int altered_servers = 0;
4414
4415 char *first_param, *cur_param, *next_param, *end_params;
4416 char *st_cur_param = NULL;
4417 char *st_next_param = NULL;
4418
4419 struct chunk *temp;
4420 int reql;
4421
4422 temp = get_trash_chunk();
4423 if (temp->size < s->txn.req.body_len) {
4424 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004425 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004426 goto out;
4427 }
4428
4429 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4430 if (reql <= 0) {
4431 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004432 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004433 return 0;
4434 }
4435
4436 first_param = temp->str;
4437 end_params = temp->str + reql;
4438 cur_param = next_param = end_params;
4439 *end_params = '\0';
4440
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004441 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004442
4443 /*
4444 * Parse the parameters in reverse order to only store the last value.
4445 * From the html form, the backend and the action are at the end.
4446 */
4447 while (cur_param > first_param) {
4448 char *value;
4449 int poffset, plen;
4450
4451 cur_param--;
4452
4453 if ((*cur_param == '&') || (cur_param == first_param)) {
4454 reprocess_servers:
4455 /* Parse the key */
4456 poffset = (cur_param != first_param ? 1 : 0);
4457 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4458 if ((plen > 0) && (plen <= sizeof(key))) {
4459 strncpy(key, cur_param + poffset, plen);
4460 key[plen - 1] = '\0';
4461 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004462 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004463 goto out;
4464 }
4465
4466 /* Parse the value */
4467 value = key;
4468 while (*value != '\0' && *value != '=') {
4469 value++;
4470 }
4471 if (*value == '=') {
4472 /* Ok, a value is found, we can mark the end of the key */
4473 *value++ = '\0';
4474 }
4475 if (url_decode(key) < 0 || url_decode(value) < 0)
4476 break;
4477
4478 /* Now we can check the key to see what to do */
4479 if (!px && (strcmp(key, "b") == 0)) {
4480 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4481 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004482 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004483 goto out;
4484 }
4485 }
4486 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004487 if (strcmp(value, "ready") == 0) {
4488 action = ST_ADM_ACTION_READY;
4489 }
4490 else if (strcmp(value, "drain") == 0) {
4491 action = ST_ADM_ACTION_DRAIN;
4492 }
4493 else if (strcmp(value, "maint") == 0) {
4494 action = ST_ADM_ACTION_MAINT;
4495 }
4496 else if (strcmp(value, "shutdown") == 0) {
4497 action = ST_ADM_ACTION_SHUTDOWN;
4498 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004499 else if (strcmp(value, "dhlth") == 0) {
4500 action = ST_ADM_ACTION_DHLTH;
4501 }
4502 else if (strcmp(value, "ehlth") == 0) {
4503 action = ST_ADM_ACTION_EHLTH;
4504 }
4505 else if (strcmp(value, "hrunn") == 0) {
4506 action = ST_ADM_ACTION_HRUNN;
4507 }
4508 else if (strcmp(value, "hnolb") == 0) {
4509 action = ST_ADM_ACTION_HNOLB;
4510 }
4511 else if (strcmp(value, "hdown") == 0) {
4512 action = ST_ADM_ACTION_HDOWN;
4513 }
4514 else if (strcmp(value, "dagent") == 0) {
4515 action = ST_ADM_ACTION_DAGENT;
4516 }
4517 else if (strcmp(value, "eagent") == 0) {
4518 action = ST_ADM_ACTION_EAGENT;
4519 }
4520 else if (strcmp(value, "arunn") == 0) {
4521 action = ST_ADM_ACTION_ARUNN;
4522 }
4523 else if (strcmp(value, "adown") == 0) {
4524 action = ST_ADM_ACTION_ADOWN;
4525 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004526 /* else these are the old supported methods */
4527 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004528 action = ST_ADM_ACTION_DISABLE;
4529 }
4530 else if (strcmp(value, "enable") == 0) {
4531 action = ST_ADM_ACTION_ENABLE;
4532 }
4533 else if (strcmp(value, "stop") == 0) {
4534 action = ST_ADM_ACTION_STOP;
4535 }
4536 else if (strcmp(value, "start") == 0) {
4537 action = ST_ADM_ACTION_START;
4538 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004539 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004540 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004541 goto out;
4542 }
4543 }
4544 else if (strcmp(key, "s") == 0) {
4545 if (!(px && action)) {
4546 /*
4547 * Indicates that we'll need to reprocess the parameters
4548 * as soon as backend and action are known
4549 */
4550 if (!reprocess) {
4551 st_cur_param = cur_param;
4552 st_next_param = next_param;
4553 }
4554 reprocess = 1;
4555 }
4556 else if ((sv = findserver(px, value)) != NULL) {
4557 switch (action) {
4558 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004559 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004560 altered_servers++;
4561 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004562 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004563 }
4564 break;
4565 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004566 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004567 altered_servers++;
4568 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004569 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004570 }
4571 break;
4572 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004573 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4574 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4575 altered_servers++;
4576 total_servers++;
4577 }
4578 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004579 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004580 if (sv->admin & SRV_ADMF_FDRAIN) {
4581 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4582 altered_servers++;
4583 total_servers++;
4584 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004585 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004586 case ST_ADM_ACTION_DHLTH:
4587 if (sv->check.state & CHK_ST_CONFIGURED) {
4588 sv->check.state &= ~CHK_ST_ENABLED;
4589 altered_servers++;
4590 total_servers++;
4591 }
4592 break;
4593 case ST_ADM_ACTION_EHLTH:
4594 if (sv->check.state & CHK_ST_CONFIGURED) {
4595 sv->check.state |= CHK_ST_ENABLED;
4596 altered_servers++;
4597 total_servers++;
4598 }
4599 break;
4600 case ST_ADM_ACTION_HRUNN:
4601 if (!(sv->track)) {
4602 sv->check.health = sv->check.rise + sv->check.fall - 1;
4603 srv_set_running(sv, "changed from Web interface");
4604 altered_servers++;
4605 total_servers++;
4606 }
4607 break;
4608 case ST_ADM_ACTION_HNOLB:
4609 if (!(sv->track)) {
4610 sv->check.health = sv->check.rise + sv->check.fall - 1;
4611 srv_set_stopping(sv, "changed from Web interface");
4612 altered_servers++;
4613 total_servers++;
4614 }
4615 break;
4616 case ST_ADM_ACTION_HDOWN:
4617 if (!(sv->track)) {
4618 sv->check.health = 0;
4619 srv_set_stopped(sv, "changed from Web interface");
4620 altered_servers++;
4621 total_servers++;
4622 }
4623 break;
4624 case ST_ADM_ACTION_DAGENT:
4625 if (sv->agent.state & CHK_ST_CONFIGURED) {
4626 sv->agent.state &= ~CHK_ST_ENABLED;
4627 altered_servers++;
4628 total_servers++;
4629 }
4630 break;
4631 case ST_ADM_ACTION_EAGENT:
4632 if (sv->agent.state & CHK_ST_CONFIGURED) {
4633 sv->agent.state |= CHK_ST_ENABLED;
4634 altered_servers++;
4635 total_servers++;
4636 }
4637 break;
4638 case ST_ADM_ACTION_ARUNN:
4639 if (sv->agent.state & CHK_ST_ENABLED) {
4640 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4641 srv_set_running(sv, "changed from Web interface");
4642 altered_servers++;
4643 total_servers++;
4644 }
4645 break;
4646 case ST_ADM_ACTION_ADOWN:
4647 if (sv->agent.state & CHK_ST_ENABLED) {
4648 sv->agent.health = 0;
4649 srv_set_stopped(sv, "changed from Web interface");
4650 altered_servers++;
4651 total_servers++;
4652 }
4653 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004654 case ST_ADM_ACTION_READY:
4655 srv_adm_set_ready(sv);
4656 altered_servers++;
4657 total_servers++;
4658 break;
4659 case ST_ADM_ACTION_DRAIN:
4660 srv_adm_set_drain(sv);
4661 altered_servers++;
4662 total_servers++;
4663 break;
4664 case ST_ADM_ACTION_MAINT:
4665 srv_adm_set_maint(sv);
4666 altered_servers++;
4667 total_servers++;
4668 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004669 case ST_ADM_ACTION_SHUTDOWN:
4670 if (px->state != PR_STSTOPPED) {
4671 struct session *sess, *sess_bck;
4672
4673 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4674 if (sess->srv_conn == sv)
4675 session_shutdown(sess, SN_ERR_KILLED);
4676
4677 altered_servers++;
4678 total_servers++;
4679 }
4680 break;
4681 }
4682 } else {
4683 /* the server name is unknown or ambiguous (duplicate names) */
4684 total_servers++;
4685 }
4686 }
4687 if (reprocess && px && action) {
4688 /* Now, we know the backend and the action chosen by the user.
4689 * We can safely restart from the first server parameter
4690 * to reprocess them
4691 */
4692 cur_param = st_cur_param;
4693 next_param = st_next_param;
4694 reprocess = 0;
4695 goto reprocess_servers;
4696 }
4697
4698 next_param = cur_param;
4699 }
4700 }
4701
4702 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004703 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004704 }
4705 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004706 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004707 }
4708 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004709 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004710 }
4711 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004712 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004713 }
4714 out:
4715 return 1;
4716}
4717
4718
4719static int stats_send_http_headers(struct stream_interface *si)
4720{
4721 struct session *s = session_from_task(si->owner);
4722 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004723 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004724
4725 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004726 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004727 "Cache-Control: no-cache\r\n"
4728 "Connection: close\r\n"
4729 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004730 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004731
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004732 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004733 chunk_appendf(&trash, "Refresh: %d\r\n",
4734 uri->refresh);
4735
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004736 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4737
4738 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4739 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4740 else
4741 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004742
4743 s->txn.status = 200;
4744 s->logs.tv_request = now;
4745
4746 if (bi_putchk(si->ib, &trash) == -1)
4747 return 0;
4748
4749 return 1;
4750}
4751
4752static int stats_send_http_redirect(struct stream_interface *si)
4753{
4754 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4755 struct session *s = session_from_task(si->owner);
4756 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004757 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004758
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004759 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004760 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004761 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004762 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004763 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4764 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004765 }
4766
4767 /* We don't want to land on the posted stats page because a refresh will
4768 * repost the data. We don't want this to happen on accident so we redirect
4769 * the browse to the stats page with a GET.
4770 */
4771 chunk_printf(&trash,
4772 "HTTP/1.1 303 See Other\r\n"
4773 "Cache-Control: no-cache\r\n"
4774 "Content-Type: text/plain\r\n"
4775 "Connection: close\r\n"
4776 "Location: %s;st=%s%s%s%s\r\n"
4777 "\r\n",
4778 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004779 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4780 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4781 stat_status_codes[appctx->ctx.stats.st_code]) ?
4782 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004783 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004784 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4785 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004786 scope_txt);
4787
4788 s->txn.status = 303;
4789 s->logs.tv_request = now;
4790
4791 if (bi_putchk(si->ib, &trash) == -1)
4792 return 0;
4793
4794 return 1;
4795}
4796
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004797/* This I/O handler runs as an applet embedded in a stream interface. It is
4798 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004799 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004800 * automatically unregisters itself once transfer is complete.
4801 */
4802static void http_stats_io_handler(struct stream_interface *si)
4803{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004804 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004805 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004806 struct channel *req = si->ob;
4807 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004808
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004809 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4810 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004811
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004812 /* check that the output is not closed */
4813 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004814 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004815
Willy Tarreau347a35d2013-11-22 17:51:09 +01004816 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004817 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004818 if (stats_send_http_headers(si)) {
4819 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004820 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004821 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004822 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004823 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004824 }
4825
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004826 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004827 unsigned int prev_len = si->ib->buf->i;
4828 unsigned int data_len;
4829 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004830 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004831
4832 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4833 /* One difficulty we're facing is that we must prevent
4834 * the input data from being automatically forwarded to
4835 * the output area. For this, we temporarily disable
4836 * forwarding on the channel.
4837 */
4838 last_fwd = si->ib->to_forward;
4839 si->ib->to_forward = 0;
4840 chunk_printf(&trash, "\r\n000000\r\n");
4841 if (bi_putchk(si->ib, &trash) == -1) {
4842 si->ib->to_forward = last_fwd;
4843 goto fail;
4844 }
4845 }
4846
4847 data_len = si->ib->buf->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004848 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004849 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004850
4851 last_len = si->ib->buf->i;
4852
4853 /* Now we must either adjust or remove the chunk size. This is
4854 * not easy because the chunk size might wrap at the end of the
4855 * buffer, so we pretend we have nothing in the buffer, we write
4856 * the size, then restore the buffer's contents. Note that we can
4857 * only do that because no forwarding is scheduled on the stats
4858 * applet.
4859 */
4860 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4861 si->ib->total -= (last_len - prev_len);
4862 si->ib->buf->i -= (last_len - prev_len);
4863
4864 if (last_len != data_len) {
4865 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
4866 bi_putchk(si->ib, &trash);
4867
4868 si->ib->total += (last_len - data_len);
4869 si->ib->buf->i += (last_len - data_len);
4870 }
4871 /* now re-enable forwarding */
4872 channel_forward(si->ib, last_fwd);
4873 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004874 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004875
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004876 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004877 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004878 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004879 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004880 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004881 }
4882
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004883 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004884 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004885 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004886 }
4887
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004888 if (appctx->st0 == STAT_HTTP_DONE) {
4889 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4890 chunk_printf(&trash, "\r\n0\r\n\r\n");
4891 if (bi_putchk(si->ib, &trash) == -1)
4892 goto fail;
4893 }
4894 /* eat the whole request */
4895 bo_skip(si->ob, si->ob->buf->o);
4896 res->flags |= CF_READ_NULL;
4897 si_shutr(si);
4898 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004899
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004900 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4901 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004902
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004903 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004904 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4905 si_shutr(si);
4906 res->flags |= CF_READ_NULL;
4907 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004908 }
Willy Tarreau91861262007-10-17 17:06:05 +02004909
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004910 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004911 /* update all other flags and resync with the other side */
4912 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004913
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004914 /* we don't want to expire timeouts while we're processing requests */
4915 si->ib->rex = TICK_ETERNITY;
4916 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004917
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004918 out:
4919 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4920 /* check that we have released everything then unregister */
4921 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004922 }
4923}
4924
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004925
Willy Tarreau909d5172012-11-26 03:04:41 +01004926static inline const char *get_conn_ctrl_name(const struct connection *conn)
4927{
Willy Tarreau3c728722014-01-23 13:50:42 +01004928 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004929 return "NONE";
4930 return conn->ctrl->name;
4931}
4932
4933static inline const char *get_conn_xprt_name(const struct connection *conn)
4934{
4935 static char ptr[17];
4936
Willy Tarreauaad69382014-01-23 14:21:42 +01004937 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004938 return "NONE";
4939
4940 if (conn->xprt == &raw_sock)
4941 return "RAW";
4942
4943#ifdef USE_OPENSSL
4944 if (conn->xprt == &ssl_sock)
4945 return "SSL";
4946#endif
4947 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4948 return ptr;
4949}
4950
4951static inline const char *get_conn_data_name(const struct connection *conn)
4952{
4953 static char ptr[17];
4954
4955 if (!conn->data)
4956 return "NONE";
4957
4958 if (conn->data == &sess_conn_cb)
4959 return "SESS";
4960
4961 if (conn->data == &si_conn_cb)
4962 return "STRM";
4963
4964 if (conn->data == &check_conn_cb)
4965 return "CHCK";
4966
4967 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4968 return ptr;
4969}
4970
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004971/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004972 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004973 * 0 if the output buffer is full and it needs to be called again, otherwise
4974 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004975 */
Willy Tarreau76153662012-11-26 01:16:39 +01004976static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004977{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004978 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004979 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004980 extern const char *monthname[12];
4981 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004982 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004983 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004984
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004985 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004986
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004987 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004988 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004989 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4990 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004991 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004992 appctx->ctx.sess.uid = 0;
4993 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004994 return 1;
4995 }
4996
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004997 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004998 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004999 appctx->ctx.sess.uid = sess->uniq_id;
5000 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005001 /* fall through */
5002
5003 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005004 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005005 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005006 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005007 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005008 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5009 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005010 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01005011 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005012
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005013 conn = objt_conn(sess->si[0].end);
5014 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005015 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005016 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005017 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005018 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005019 break;
5020 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005021 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005022 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005023 default:
5024 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005025 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005026 break;
5027 }
5028
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005029 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005030 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005031 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005032
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005033 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005034 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005035 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
5036 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
5037 sess->listener ? sess->listener->luid : 0);
5038
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005039 if (conn)
5040 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005041
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005042 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005043 case AF_INET:
5044 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005045 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005046 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005047 break;
5048 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005049 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005050 break;
5051 default:
5052 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005053 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005054 break;
5055 }
5056
Willy Tarreau50943332011-09-02 17:33:05 +02005057 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005058 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005059 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005060 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005061 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5062 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005063 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005064
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005065 conn = objt_conn(sess->si[1].end);
5066 if (conn)
5067 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005068
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005069 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005070 case AF_INET:
5071 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005072 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005073 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005074 break;
5075 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005076 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005077 break;
5078 default:
5079 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005080 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005081 break;
5082 }
5083
5084 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005085 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005086 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005087 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5088 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005089 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005090 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005091
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005092 if (conn)
5093 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005094
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005095 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005096 case AF_INET:
5097 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005098 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005099 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005100 break;
5101 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005102 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005103 break;
5104 default:
5105 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005106 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005107 break;
5108 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005109
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005110 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005111 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005112 sess->task,
5113 sess->task->state,
5114 sess->task->nice, sess->task->calls,
5115 sess->task->expire ?
5116 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5117 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5118 TICKS_TO_MS(1000)) : "<NEVER>",
5119 task_in_rq(sess->task) ? ", running" : "");
5120
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005121 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005122 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005123 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5124
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005125 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005126 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
5127 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
5128 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
5129
5130 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005131 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005132 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005133 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005134 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005135 obj_type_name(sess->si[0].end),
5136 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005137 sess->si[0].exp ?
5138 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5139 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5140 TICKS_TO_MS(1000)) : "<NEVER>",
5141 sess->si[0].err_type);
5142
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005143 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005144 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005145 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005146 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005147 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005148 obj_type_name(sess->si[1].end),
5149 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005150 sess->si[1].exp ?
5151 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5152 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5153 TICKS_TO_MS(1000)) : "<NEVER>",
5154 sess->si[1].err_type);
5155
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005156 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005157 chunk_appendf(&trash,
5158 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005159 conn,
5160 get_conn_ctrl_name(conn),
5161 get_conn_xprt_name(conn),
5162 get_conn_data_name(conn),
5163 obj_type_name(conn->target),
5164 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005165
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005166 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005167 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005168 conn->flags,
5169 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005170 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005171 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005172 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005173 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005174 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5175 chunk_appendf(&trash,
5176 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5177 tmpctx,
5178 tmpctx->st0,
5179 tmpctx->st1,
5180 tmpctx->st2,
5181 tmpctx->applet->name);
5182 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005183
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005184 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005185 chunk_appendf(&trash,
5186 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005187 conn,
5188 get_conn_ctrl_name(conn),
5189 get_conn_xprt_name(conn),
5190 get_conn_data_name(conn),
5191 obj_type_name(conn->target),
5192 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005193
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005194 chunk_appendf(&trash,
5195 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005196 conn->flags,
5197 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005198 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005199 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005200 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005201 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005202 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5203 chunk_appendf(&trash,
5204 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5205 tmpctx,
5206 tmpctx->st0,
5207 tmpctx->st1,
5208 tmpctx->st2,
5209 tmpctx->applet->name);
5210 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005211
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005212 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005213 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005214 " an_exp=%s",
5215 sess->req,
5216 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005217 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01005218 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005219 sess->req->analyse_exp ?
5220 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
5221 TICKS_TO_MS(1000)) : "<NEVER>");
5222
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005223 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005224 " rex=%s",
5225 sess->req->rex ?
5226 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
5227 TICKS_TO_MS(1000)) : "<NEVER>");
5228
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005229 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005230 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005231 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005232 sess->req->wex ?
5233 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
5234 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01005235 sess->req->buf,
5236 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005237 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01005238 sess->txn.req.next, sess->req->buf->i,
5239 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005240
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005241 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005242 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005243 " an_exp=%s",
5244 sess->rep,
5245 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005246 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01005247 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005248 sess->rep->analyse_exp ?
5249 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
5250 TICKS_TO_MS(1000)) : "<NEVER>");
5251
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005252 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005253 " rex=%s",
5254 sess->rep->rex ?
5255 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
5256 TICKS_TO_MS(1000)) : "<NEVER>");
5257
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005258 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005259 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005260 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005261 sess->rep->wex ?
5262 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
5263 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01005264 sess->rep->buf,
5265 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005266 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01005267 sess->txn.rsp.next, sess->rep->buf->i,
5268 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005269
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005270 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005271 return 0;
5272
5273 /* use other states to dump the contents */
5274 }
5275 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005276 appctx->ctx.sess.uid = 0;
5277 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005278 return 1;
5279}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005280
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005281static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005282{
5283 struct appctx *appctx = __objt_appctx(si->end);
5284
5285 switch (appctx->st2) {
5286 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005287 /* Display the column headers. If the message cannot be sent,
5288 * quit the fucntion with returning 0. The function is called
5289 * later and restart at the state "STAT_ST_INIT".
5290 */
5291 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005292 chunk_appendf(&trash, "# id (file) description\n");
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005293 if (bi_putchk(si->ib, &trash) == -1)
5294 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005295
5296 /* Now, we start the browsing of the references lists.
5297 * Note that the following call to LIST_ELEM return bad pointer. The only
5298 * avalaible field of this pointer is <list>. It is used with the function
5299 * pat_list_get_next() for retruning the first avalaible entry
5300 */
5301 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5302 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5303 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005304 appctx->st2 = STAT_ST_LIST;
5305 /* fall through */
5306
5307 case STAT_ST_LIST:
5308 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005309 chunk_reset(&trash);
5310
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005311 /* Build messages. If the reference is used by another category than
5312 * the listed categorie, display the information in the massage.
5313 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005314 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005315 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5316 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005317
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005318 if (bi_putchk(si->ib, &trash) == -1) {
5319 /* let's try again later from this session. We add ourselves into
5320 * this session's users so that it can remove us upon termination.
5321 */
5322 return 0;
5323 }
5324
5325 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005326 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5327 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005328 }
5329
5330 appctx->st2 = STAT_ST_FIN;
5331 /* fall through */
5332
5333 default:
5334 appctx->st2 = STAT_ST_FIN;
5335 return 1;
5336 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005337 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005338}
5339
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005340static int stats_map_lookup(struct stream_interface *si)
5341{
5342 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005343 struct sample sample;
5344 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005345 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005346
5347 switch (appctx->st2) {
5348 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005349 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005350 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005351 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005352 appctx->st2 = STAT_ST_LIST;
5353 /* fall through */
5354
5355 case STAT_ST_LIST:
5356 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005357 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005358 /* initialise chunk to build new message */
5359 chunk_reset(&trash);
5360
5361 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005362 sample.type = SMP_T_STR;
5363 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005364 sample.data.str.len = appctx->ctx.map.chunk.len;
5365 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005366 if (appctx->ctx.map.expr->pat_head->match &&
5367 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005368 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5369 else
5370 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005371
5372 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005373 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5374 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5375 break;
5376 if (match_method >= PAT_MATCH_NUM)
5377 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5378 else
5379 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005380
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005381 /* case sensitive */
5382 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5383 chunk_appendf(&trash, ", case=insensitive");
5384 else
5385 chunk_appendf(&trash, ", case=sensitive");
5386
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005387 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005388 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005389 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5390 chunk_appendf(&trash, ", found=no");
5391 else
5392 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005393 }
5394
5395 /* Display match and match info */
5396 else {
5397 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005398 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5399 chunk_appendf(&trash, ", found=yes");
5400 else
5401 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005402
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005403 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005404 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005405 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005406 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005407 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005408
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005409 /* display pattern */
5410 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5411 if (pat->ref && pat->ref->pattern)
5412 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5413 else
5414 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005415 }
5416 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005417 if (pat->ref && pat->ref->pattern)
5418 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5419 else
5420 chunk_appendf(&trash, ", pattern=unknown");
5421 }
5422
5423 /* display return value */
5424 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5425 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005426 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005427 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005428 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005429 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005430 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005431 }
5432
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005433 chunk_appendf(&trash, "\n");
5434
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005435 /* display response */
5436 if (bi_putchk(si->ib, &trash) == -1) {
5437 /* let's try again later from this session. We add ourselves into
5438 * this session's users so that it can remove us upon termination.
5439 */
5440 return 0;
5441 }
5442
5443 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005444 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5445 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005446 }
5447
5448 appctx->st2 = STAT_ST_FIN;
5449 /* fall through */
5450
5451 default:
5452 appctx->st2 = STAT_ST_FIN;
5453 free(appctx->ctx.map.chunk.str);
5454 return 1;
5455 }
5456}
5457
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005458static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005459{
5460 struct appctx *appctx = __objt_appctx(si->end);
5461
5462 switch (appctx->st2) {
5463
5464 case STAT_ST_INIT:
5465 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005466 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5467 struct pat_ref_elt *, list);
5468 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5469 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005470 appctx->st2 = STAT_ST_LIST;
5471 /* fall through */
5472
5473 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005474 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005475 chunk_reset(&trash);
5476
5477 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005478 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005479 chunk_appendf(&trash, "%p %s %s\n",
5480 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5481 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005482 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005483 chunk_appendf(&trash, "%p %s\n",
5484 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005485
5486 if (bi_putchk(si->ib, &trash) == -1) {
5487 /* let's try again later from this session. We add ourselves into
5488 * this session's users so that it can remove us upon termination.
5489 */
5490 return 0;
5491 }
5492
5493 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005494 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5495 struct pat_ref_elt *, list);
5496 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005497 break;
5498 }
5499
5500 appctx->st2 = STAT_ST_FIN;
5501 /* fall through */
5502
5503 default:
5504 appctx->st2 = STAT_ST_FIN;
5505 return 1;
5506 }
5507}
5508
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005509/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005510 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005511 * to be called again, otherwise non-zero. It is designed to be called
5512 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005513 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005514static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005515{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005516 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005517 struct connection *conn;
5518
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005519 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005520 /* If we're forced to shut down, we might have to remove our
5521 * reference to the last session being dumped.
5522 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005523 if (appctx->st2 == STAT_ST_LIST) {
5524 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5525 LIST_DEL(&appctx->ctx.sess.bref.users);
5526 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005527 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005528 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005529 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005530 }
5531
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005532 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005533
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005534 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005535 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005536 /* the function had not been called yet, let's prepare the
5537 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005538 * pointer to the first in the global list. When a target
5539 * session is being destroyed, it is responsible for updating
5540 * this pointer. We know we have reached the end when this
5541 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005542 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005543 LIST_INIT(&appctx->ctx.sess.bref.users);
5544 appctx->ctx.sess.bref.ref = sessions.n;
5545 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005546 /* fall through */
5547
Willy Tarreau295a8372011-03-10 11:25:07 +01005548 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005549 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005550 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5551 LIST_DEL(&appctx->ctx.sess.bref.users);
5552 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005553 }
5554
5555 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005556 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005557 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005558 struct session *curr_sess;
5559
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005560 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005561
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005562 if (appctx->ctx.sess.target) {
5563 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005564 goto next_sess;
5565
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005566 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005567 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005568 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005569 return 0;
5570
5571 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005572 LIST_DEL(&appctx->ctx.sess.bref.users);
5573 LIST_INIT(&appctx->ctx.sess.bref.users);
5574 if (appctx->ctx.sess.target != (void *)-1) {
5575 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005576 break;
5577 }
5578 else
5579 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005580 }
5581
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005582 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005583 "%p: proto=%s",
5584 curr_sess,
5585 curr_sess->listener->proto->name);
5586
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005587
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005588 conn = objt_conn(curr_sess->si[0].end);
5589 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005590 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005591 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005592 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005593 " src=%s:%d fe=%s be=%s srv=%s",
5594 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005595 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005596 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005597 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005598 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005599 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005600 break;
5601 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005602 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005603 " src=unix:%d fe=%s be=%s srv=%s",
5604 curr_sess->listener->luid,
5605 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005606 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005607 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005608 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005609 break;
5610 }
5611
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005612 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005613 " ts=%02x age=%s calls=%d",
5614 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005615 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5616 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005617
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005618 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005619 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005620 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005621 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005622 curr_sess->req->analysers,
5623 curr_sess->req->rex ?
5624 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5625 TICKS_TO_MS(1000)) : "");
5626
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005627 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005628 ",wx=%s",
5629 curr_sess->req->wex ?
5630 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5631 TICKS_TO_MS(1000)) : "");
5632
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005633 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005634 ",ax=%s]",
5635 curr_sess->req->analyse_exp ?
5636 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5637 TICKS_TO_MS(1000)) : "");
5638
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005639 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005640 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005641 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005642 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005643 curr_sess->rep->analysers,
5644 curr_sess->rep->rex ?
5645 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5646 TICKS_TO_MS(1000)) : "");
5647
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005648 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005649 ",wx=%s",
5650 curr_sess->rep->wex ?
5651 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5652 TICKS_TO_MS(1000)) : "");
5653
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005654 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005655 ",ax=%s]",
5656 curr_sess->rep->analyse_exp ?
5657 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5658 TICKS_TO_MS(1000)) : "");
5659
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005660 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005661 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005662 " s0=[%d,%1xh,fd=%d,ex=%s]",
5663 curr_sess->si[0].state,
5664 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005665 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005666 curr_sess->si[0].exp ?
5667 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5668 TICKS_TO_MS(1000)) : "");
5669
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005670 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005671 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005672 " s1=[%d,%1xh,fd=%d,ex=%s]",
5673 curr_sess->si[1].state,
5674 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005675 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005676 curr_sess->si[1].exp ?
5677 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5678 TICKS_TO_MS(1000)) : "");
5679
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005680 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005681 " exp=%s",
5682 curr_sess->task->expire ?
5683 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5684 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005685 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005686 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005687
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005688 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005689
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005690 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005691 /* let's try again later from this session. We add ourselves into
5692 * this session's users so that it can remove us upon termination.
5693 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005694 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005695 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005696 }
5697
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005698 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005699 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005700 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005701
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005702 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005703 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005704 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005705 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005706 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005707 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005708
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005709 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005710 return 0;
5711
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005712 appctx->ctx.sess.target = NULL;
5713 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005714 return 1;
5715 }
5716
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005717 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005718 /* fall through */
5719
5720 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005721 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005722 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005723 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005724}
5725
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005726/* This is called when the stream interface is closed. For instance, upon an
5727 * external abort, we won't call the i/o handler anymore so we may need to
5728 * remove back references to the session currently being dumped.
5729 */
Simon Horman74d88312013-02-12 10:45:50 +09005730static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005731{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005732 struct appctx *appctx = __objt_appctx(si->end);
5733
5734 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5735 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5736 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005737 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005738 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5739 free(appctx->ctx.cli.err);
5740 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005741 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5742 free(appctx->ctx.map.chunk.str);
5743 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005744}
5745
Willy Tarreau20e99322013-04-13 09:22:25 +02005746/* This function is used to either dump tables states (when action is set
5747 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005748 * It returns 0 if the output buffer is full and it needs to be called
5749 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005750 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005751static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005752{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005753 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005754 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005755 struct ebmb_node *eb;
5756 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005757 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005758 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005759
5760 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005761 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005762 * - STAT_ST_INIT : the first call
5763 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005764 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005765 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005766 * and the entry pointer points to the next entry to be dumped,
5767 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005768 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005769 * data though.
5770 */
5771
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005772 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005773 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005774 if (appctx->st2 == STAT_ST_LIST) {
5775 appctx->ctx.table.entry->ref_cnt--;
5776 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005777 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005778 return 1;
5779 }
5780
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005781 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005782
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005783 while (appctx->st2 != STAT_ST_FIN) {
5784 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005785 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005786 appctx->ctx.table.proxy = appctx->ctx.table.target;
5787 if (!appctx->ctx.table.proxy)
5788 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005789
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005790 appctx->ctx.table.entry = NULL;
5791 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005792 break;
5793
Willy Tarreau295a8372011-03-10 11:25:07 +01005794 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005795 if (!appctx->ctx.table.proxy ||
5796 (appctx->ctx.table.target &&
5797 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5798 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005799 break;
5800 }
5801
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005802 if (appctx->ctx.table.proxy->table.size) {
5803 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5804 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005805 return 0;
5806
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005807 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005808 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005809 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005810 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005811 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005812 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5813 appctx->ctx.table.entry->ref_cnt++;
5814 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005815 break;
5816 }
5817 }
5818 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005819 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005820 break;
5821
Willy Tarreau295a8372011-03-10 11:25:07 +01005822 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005823 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005824
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005825 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005826 /* we're filtering on some data contents */
5827 void *ptr;
5828 long long data;
5829
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005830 dt = appctx->ctx.table.data_type;
5831 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5832 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005833 dt);
5834
5835 data = 0;
5836 switch (stktable_data_types[dt].std_type) {
5837 case STD_T_SINT:
5838 data = stktable_data_cast(ptr, std_t_sint);
5839 break;
5840 case STD_T_UINT:
5841 data = stktable_data_cast(ptr, std_t_uint);
5842 break;
5843 case STD_T_ULL:
5844 data = stktable_data_cast(ptr, std_t_ull);
5845 break;
5846 case STD_T_FRQP:
5847 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005848 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005849 break;
5850 }
5851
5852 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005853 if ((data < appctx->ctx.table.value &&
5854 (appctx->ctx.table.data_op == STD_OP_EQ ||
5855 appctx->ctx.table.data_op == STD_OP_GT ||
5856 appctx->ctx.table.data_op == STD_OP_GE)) ||
5857 (data == appctx->ctx.table.value &&
5858 (appctx->ctx.table.data_op == STD_OP_NE ||
5859 appctx->ctx.table.data_op == STD_OP_GT ||
5860 appctx->ctx.table.data_op == STD_OP_LT)) ||
5861 (data > appctx->ctx.table.value &&
5862 (appctx->ctx.table.data_op == STD_OP_EQ ||
5863 appctx->ctx.table.data_op == STD_OP_LT ||
5864 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005865 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005866 }
5867
Simon Hormanc88b8872011-06-15 15:18:49 +09005868 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005869 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5870 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005871 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005872
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005873 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005874
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005875 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005876 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005877 struct stksess *old = appctx->ctx.table.entry;
5878 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005879 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005880 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5881 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5882 stksess_kill(&appctx->ctx.table.proxy->table, old);
5883 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005884 break;
5885 }
5886
Simon Hormanc88b8872011-06-15 15:18:49 +09005887
5888 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005889 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5890 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5891 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005892
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005893 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5894 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005895 break;
5896
Willy Tarreau295a8372011-03-10 11:25:07 +01005897 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005898 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005899 break;
5900 }
5901 }
5902 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005903}
5904
Willy Tarreaud426a182010-03-05 14:58:26 +01005905/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005906 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5907 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5908 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5909 * lines are respected within the limit of 70 output chars. Lines that are
5910 * continuation of a previous truncated line begin with "+" instead of " "
5911 * after the offset. The new pointer is returned.
5912 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005913static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5914 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005915{
5916 int end;
5917 unsigned char c;
5918
5919 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005920 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005921 return ptr;
5922
Willy Tarreau77804732012-10-29 16:14:26 +01005923 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005924
Willy Tarreaud426a182010-03-05 14:58:26 +01005925 while (ptr < len && ptr < bsize) {
5926 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005927 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005928 if (out->len > end - 2)
5929 break;
5930 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005931 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005932 if (out->len > end - 3)
5933 break;
5934 out->str[out->len++] = '\\';
5935 switch (c) {
5936 case '\t': c = 't'; break;
5937 case '\n': c = 'n'; break;
5938 case '\r': c = 'r'; break;
5939 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005940 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005941 }
5942 out->str[out->len++] = c;
5943 } else {
5944 if (out->len > end - 5)
5945 break;
5946 out->str[out->len++] = '\\';
5947 out->str[out->len++] = 'x';
5948 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5949 out->str[out->len++] = hextab[c & 0xF];
5950 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005951 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005952 /* we had a line break, let's return now */
5953 out->str[out->len++] = '\n';
5954 *line = ptr;
5955 return ptr;
5956 }
5957 }
5958 /* we have an incomplete line, we return it as-is */
5959 out->str[out->len++] = '\n';
5960 return ptr;
5961}
5962
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005963/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005964 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005965 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005966 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005967static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005968{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005969 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005970 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005971
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005972 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005973 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005974
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005975 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005976
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005977 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005978 /* the function had not been called yet, let's prepare the
5979 * buffer for a response.
5980 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005981 struct tm tm;
5982
5983 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005984 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005985 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5986 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5987 error_snapshot_id);
5988
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005989 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005990 /* Socket buffer full. Let's try again later from the same point */
5991 return 0;
5992 }
5993
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005994 appctx->ctx.errors.px = proxy;
5995 appctx->ctx.errors.buf = 0;
5996 appctx->ctx.errors.bol = 0;
5997 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005998 }
5999
6000 /* we have two inner loops here, one for the proxy, the other one for
6001 * the buffer.
6002 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006003 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006004 struct error_snapshot *es;
6005
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006006 if (appctx->ctx.errors.buf == 0)
6007 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006008 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006009 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006010
6011 if (!es->when.tv_sec)
6012 goto next;
6013
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006014 if (appctx->ctx.errors.iid >= 0 &&
6015 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6016 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006017 goto next;
6018
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006019 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006020 /* just print headers now */
6021
6022 char pn[INET6_ADDRSTRLEN];
6023 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006024 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006025
6026 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006027 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006028 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006029 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006030
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006031 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6032 case AF_INET:
6033 case AF_INET6:
6034 port = get_host_port(&es->src);
6035 break;
6036 default:
6037 port = 0;
6038 }
6039
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006040 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006041 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006042 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006043 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006044 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006045 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006046 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6047 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006048 break;
6049 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006050 chunk_appendf(&trash,
Olivier Doucet815d7d52014-09-08 11:23:00 +02006051 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006052 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006053 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006054 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006055 break;
6056 }
6057
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006058 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006059 ", server %s (#%d), event #%u\n"
6060 " src %s:%d, session #%d, session flags 0x%08x\n"
6061 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6062 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6063 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6064 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6065 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6066 es->ev_id,
6067 pn, port, es->sid, es->s_flags,
6068 es->state, es->m_flags, es->t_flags,
6069 es->m_clen, es->m_blen,
6070 es->b_flags, es->b_out, es->b_tot,
6071 es->len, es->b_wrap, es->pos);
6072
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006073 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006074 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02006075 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006076 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006077 appctx->ctx.errors.ptr = 0;
6078 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006079 }
6080
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006081 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006082 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006083 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006084 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006085 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02006086 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006087 goto next;
6088 }
6089
6090 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006091 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006092 int newptr;
6093 int newline;
6094
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006095 newline = appctx->ctx.errors.bol;
6096 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6097 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006098 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006099
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006100 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006101 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02006102 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006103 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006104 appctx->ctx.errors.ptr = newptr;
6105 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006106 };
6107 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006108 appctx->ctx.errors.bol = 0;
6109 appctx->ctx.errors.ptr = -1;
6110 appctx->ctx.errors.buf++;
6111 if (appctx->ctx.errors.buf > 1) {
6112 appctx->ctx.errors.buf = 0;
6113 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006114 }
6115 }
6116
6117 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006118 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006119}
6120
Willy Tarreaud5781202012-09-22 19:32:35 +02006121/* parse the "level" argument on the bind lines */
6122static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6123{
6124 if (!*args[cur_arg + 1]) {
6125 memprintf(err, "'%s' : missing level", args[cur_arg]);
6126 return ERR_ALERT | ERR_FATAL;
6127 }
6128
6129 if (!strcmp(args[cur_arg+1], "user"))
6130 conf->level = ACCESS_LVL_USER;
6131 else if (!strcmp(args[cur_arg+1], "operator"))
6132 conf->level = ACCESS_LVL_OPER;
6133 else if (!strcmp(args[cur_arg+1], "admin"))
6134 conf->level = ACCESS_LVL_ADMIN;
6135 else {
6136 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6137 args[cur_arg], args[cur_arg+1]);
6138 return ERR_ALERT | ERR_FATAL;
6139 }
6140
6141 return 0;
6142}
6143
Willy Tarreaub24281b2011-02-13 13:16:36 +01006144struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006145 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006146 .name = "<STATS>", /* used for logging */
6147 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006148 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006149};
6150
Simon Horman9bd2c732011-06-15 15:18:44 +09006151static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006152 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006153 .name = "<CLI>", /* used for logging */
6154 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006155 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006156};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006157
Willy Tarreaudc13c112013-06-21 23:16:39 +02006158static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006159 { CFG_GLOBAL, "stats", stats_parse_global },
6160 { 0, NULL, NULL },
6161}};
6162
Willy Tarreaud5781202012-09-22 19:32:35 +02006163static struct bind_kw_list bind_kws = { "STAT", { }, {
6164 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6165 { NULL, NULL, 0 },
6166}};
6167
Willy Tarreau10522fd2008-07-09 20:12:41 +02006168__attribute__((constructor))
6169static void __dumpstats_module_init(void)
6170{
6171 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006172 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006173}
6174
Willy Tarreau91861262007-10-17 17:06:05 +02006175/*
6176 * Local variables:
6177 * c-indent-level: 8
6178 * c-basic-offset: 8
6179 * End:
6180 */