blob: 073adf2f7f01c46377aa12a53167a57a067ae43b [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 */
Cyril Bontéc4cdeb72016-05-06 12:18:50 +020084 STAT_CLI_O_PATS, /* list all pattern reference available */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010085 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"
Cyril Bontéc4cdeb72016-05-06 12:18:50 +0200189 " show acl [id] : report available acls or dump an acl's contents\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100190 " 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"
Cyril Bontéc4cdeb72016-05-06 12:18:50 +0200194 " show map [id] : report available maps or dump a map's contents\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100195 " 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
Willy Tarreaufdfc46e2015-10-13 15:06:57 +0200337 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200338 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 Tarreau0aa58992015-05-04 18:07:56 +02001112 appctx->ctx.stats.scope_str = 0;
1113 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001114 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001115 if (strcmp(args[0], "show") == 0) {
1116 if (strcmp(args[1], "stat") == 0) {
1117 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001118 appctx->ctx.stats.flags |= STAT_BOUND;
1119 appctx->ctx.stats.iid = atoi(args[2]);
1120 appctx->ctx.stats.type = atoi(args[3]);
1121 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001122 }
1123
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001124 appctx->st2 = STAT_ST_INIT;
1125 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001126 }
1127 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001128 appctx->st2 = STAT_ST_INIT;
1129 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001130 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001131 else if (strcmp(args[1], "pools") == 0) {
1132 appctx->st2 = STAT_ST_INIT;
1133 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1134 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001135 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001136 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001137 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001138 appctx->ctx.cli.msg = stats_permission_denied_msg;
1139 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001140 return 1;
1141 }
Willy Tarreau76153662012-11-26 01:16:39 +01001142 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001143 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001144 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001145 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001146 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001147 appctx->ctx.sess.target = NULL;
1148 appctx->ctx.sess.section = 0; /* start with session status */
1149 appctx->ctx.sess.pos = 0;
1150 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001151 }
1152 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001153 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001154 appctx->ctx.cli.msg = stats_permission_denied_msg;
1155 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001156 return 1;
1157 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001158 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001159 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001160 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001161 appctx->ctx.errors.iid = -1;
1162 appctx->ctx.errors.px = NULL;
1163 appctx->st2 = STAT_ST_INIT;
1164 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001165 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001166 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001167 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001168 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001169 else if (strcmp(args[1], "map") == 0 ||
1170 strcmp(args[1], "acl") == 0) {
1171
1172 /* Set ACL or MAP flags. */
1173 if (args[1][0] == 'm')
1174 appctx->ctx.map.display_flags = PAT_REF_MAP;
1175 else
1176 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001177
Cyril Bontéc4cdeb72016-05-06 12:18:50 +02001178 /* no parameter: display all map available */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001179 if (!*args[2]) {
1180 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001181 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001182 return 1;
1183 }
1184
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001185 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001186 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001187 if (!appctx->ctx.map.ref ||
1188 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1189 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001190 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001191 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001192 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001193 appctx->st0 = STAT_CLI_PRINT;
1194 return 1;
1195 }
1196 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001197 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001198 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001199 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001200 return 0;
1201 }
1202 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001203 else if (strcmp(args[0], "clear") == 0) {
1204 if (strcmp(args[1], "counters") == 0) {
1205 struct proxy *px;
1206 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001207 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001208 int clrall = 0;
1209
1210 if (strcmp(args[2], "all") == 0)
1211 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001212
Willy Tarreau6162db22009-10-10 17:13:00 +02001213 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001214 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1215 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001216 appctx->ctx.cli.msg = stats_permission_denied_msg;
1217 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001218 return 1;
1219 }
1220
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001221 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001222 if (clrall) {
1223 memset(&px->be_counters, 0, sizeof(px->be_counters));
1224 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1225 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001226 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001227 px->be_counters.conn_max = 0;
1228 px->be_counters.p.http.rps_max = 0;
1229 px->be_counters.sps_max = 0;
1230 px->be_counters.cps_max = 0;
1231 px->be_counters.nbpend_max = 0;
1232
1233 px->fe_counters.conn_max = 0;
1234 px->fe_counters.p.http.rps_max = 0;
1235 px->fe_counters.sps_max = 0;
1236 px->fe_counters.cps_max = 0;
1237 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001238 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001239
1240 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001241 if (clrall)
1242 memset(&sv->counters, 0, sizeof(sv->counters));
1243 else {
1244 sv->counters.cur_sess_max = 0;
1245 sv->counters.nbpend_max = 0;
1246 sv->counters.sps_max = 0;
1247 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001248
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001249 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001250 if (li->counters) {
1251 if (clrall)
1252 memset(li->counters, 0, sizeof(*li->counters));
1253 else
1254 li->counters->conn_max = 0;
1255 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001256 }
1257
Willy Tarreau81c25d02011-09-07 15:17:21 +02001258 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001259 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001260 return 1;
1261 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001262 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001263 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001264 /* end of processing */
1265 return 1;
1266 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001267 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1268 /* Set ACL or MAP flags. */
1269 if (args[1][0] == 'm')
1270 appctx->ctx.map.display_flags = PAT_REF_MAP;
1271 else
1272 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001273
1274 /* no parameter */
1275 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001276 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1277 appctx->ctx.cli.msg = "Missing map identifier.\n";
1278 else
1279 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001280 appctx->st0 = STAT_CLI_PRINT;
1281 return 1;
1282 }
1283
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001284 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001285 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001286 if (!appctx->ctx.map.ref ||
1287 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1288 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001289 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001290 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001291 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001292 appctx->st0 = STAT_CLI_PRINT;
1293 return 1;
1294 }
1295
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001296 /* Clear all. */
1297 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001298
1299 /* return response */
Thierry FOURNIER73f1d8f2014-12-18 15:28:01 +01001300 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001301 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001302 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001303 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001304 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001305 return 0;
1306 }
1307 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001308 else if (strcmp(args[0], "get") == 0) {
vd000001f7b633e2021-09-06 21:36:04 +08001309 if (strcmp(args[1], "server") == 0) {
1310 struct proxy *px;
1311 struct server *sv;
1312 char pn[INET6_ADDRSTRLEN];
1313 uint16_t port;
1314 struct sockaddr_storage* addr;
1315
1316 /* split "backend/server" and make <line> point to server */
1317 for (line = args[2]; *line; line++)
1318 if (*line == '/') {
1319 *line++ = '\0';
1320 break;
1321 }
1322
1323 if (!*line) {
1324 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1325 appctx->st0 = STAT_CLI_PRINT;
1326 return 1;
1327 }
1328
1329 if (!test_backend_server(args[2], line, &px, &sv)) {
1330 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1331 appctx->st0 = STAT_CLI_PRINT;
1332 return 1;
1333 }
1334 addr = &(sv->addr);
1335 switch (addr_to_str(addr, pn, sizeof(pn))) {
1336 case AF_INET:
1337 port = ((struct sockaddr_in *)addr)->sin_port;
1338 break;
1339 case AF_INET6:
1340 port = ((struct sockaddr_in6 *)addr)->sin6_port;
1341 break;
1342 default:
1343 port = 0;
1344 break;
1345 }
1346 snprintf(trash.str, trash.size, "%s,%s,%u\n", sv->id, pn, ntohs(port));
1347 bi_putstr(si->ib, trash.str);
1348 return 1;
1349 }
1350 else if (strcmp(args[1], "weight") == 0) {
Willy Tarreau38338fa2009-10-10 18:37:29 +02001351 struct proxy *px;
1352 struct server *sv;
1353
1354 /* split "backend/server" and make <line> point to server */
1355 for (line = args[2]; *line; line++)
1356 if (*line == '/') {
1357 *line++ = '\0';
1358 break;
1359 }
1360
1361 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001362 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1363 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001364 return 1;
1365 }
1366
1367 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001368 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1369 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001370 return 1;
1371 }
1372
1373 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001374 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1375 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001376 return 1;
1377 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001378 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1379 /* Set flags. */
1380 if (args[1][0] == 'm')
1381 appctx->ctx.map.display_flags = PAT_REF_MAP;
1382 else
1383 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001384
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001385 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001386 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001387 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1388 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1389 else
1390 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001391 appctx->st0 = STAT_CLI_PRINT;
1392 return 1;
1393 }
1394
1395 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001396 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001397 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001398 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001399 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001400 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001401 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001402 appctx->st0 = STAT_CLI_PRINT;
1403 return 1;
1404 }
1405
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001406 /* copy input string. The string must be allocated because
1407 * it may be used over multiple iterations. It's released
1408 * at the end and upon abort anyway.
1409 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001410 appctx->ctx.map.chunk.len = strlen(args[3]);
1411 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1412 appctx->ctx.map.chunk.str = strdup(args[3]);
1413 if (!appctx->ctx.map.chunk.str) {
1414 appctx->ctx.cli.msg = "Out of memory error.\n";
1415 appctx->st0 = STAT_CLI_PRINT;
1416 return 1;
1417 }
1418
1419 /* prepare response */
1420 appctx->st2 = STAT_ST_INIT;
1421 appctx->st0 = STAT_CLI_O_MLOOK;
1422 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001423 else { /* not "get weight" */
1424 return 0;
1425 }
1426 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001427 else if (strcmp(args[0], "set") == 0) {
1428 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001429 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001430 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001431
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001432 sv = expect_server_admin(s, si, args[2]);
1433 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001434 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001435
Simon Horman7d09b9a2013-02-12 10:45:51 +09001436 warning = server_parse_weight_change_request(sv, args[3]);
1437 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001438 appctx->ctx.cli.msg = warning;
1439 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001440 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001441 return 1;
1442 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001443 else if (strcmp(args[1], "server") == 0) {
1444 struct server *sv;
1445 const char *warning;
1446
1447 sv = expect_server_admin(s, si, args[2]);
1448 if (!sv)
1449 return 1;
1450
1451 if (strcmp(args[3], "weight") == 0) {
1452 warning = server_parse_weight_change_request(sv, args[4]);
1453 if (warning) {
1454 appctx->ctx.cli.msg = warning;
1455 appctx->st0 = STAT_CLI_PRINT;
1456 }
1457 }
1458 else if (strcmp(args[3], "state") == 0) {
1459 if (strcmp(args[4], "ready") == 0)
1460 srv_adm_set_ready(sv);
1461 else if (strcmp(args[4], "drain") == 0)
1462 srv_adm_set_drain(sv);
1463 else if (strcmp(args[4], "maint") == 0)
1464 srv_adm_set_maint(sv);
1465 else {
1466 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1467 appctx->st0 = STAT_CLI_PRINT;
1468 }
1469 }
1470 else if (strcmp(args[3], "health") == 0) {
1471 if (sv->track) {
1472 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1473 appctx->st0 = STAT_CLI_PRINT;
1474 }
1475 else if (strcmp(args[4], "up") == 0) {
1476 sv->check.health = sv->check.rise + sv->check.fall - 1;
1477 srv_set_running(sv, "changed from CLI");
1478 }
1479 else if (strcmp(args[4], "stopping") == 0) {
1480 sv->check.health = sv->check.rise + sv->check.fall - 1;
1481 srv_set_stopping(sv, "changed from CLI");
1482 }
1483 else if (strcmp(args[4], "down") == 0) {
1484 sv->check.health = 0;
1485 srv_set_stopped(sv, "changed from CLI");
1486 }
1487 else {
1488 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1489 appctx->st0 = STAT_CLI_PRINT;
1490 }
1491 }
1492 else if (strcmp(args[3], "agent") == 0) {
1493 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1494 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1495 appctx->st0 = STAT_CLI_PRINT;
1496 }
1497 else if (strcmp(args[4], "up") == 0) {
1498 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1499 srv_set_running(sv, "changed from CLI");
1500 }
1501 else if (strcmp(args[4], "down") == 0) {
1502 sv->agent.health = 0;
1503 srv_set_stopped(sv, "changed from CLI");
1504 }
1505 else {
1506 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1507 appctx->st0 = STAT_CLI_PRINT;
1508 }
1509 }
1510 else {
1511 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1512 appctx->st0 = STAT_CLI_PRINT;
1513 }
1514 return 1;
1515 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001516 else if (strcmp(args[1], "timeout") == 0) {
1517 if (strcmp(args[2], "cli") == 0) {
1518 unsigned timeout;
1519 const char *res;
1520
1521 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001522 appctx->ctx.cli.msg = "Expects an integer value.\n";
1523 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001524 return 1;
1525 }
1526
1527 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1528 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001529 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1530 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001531 return 1;
1532 }
1533
1534 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1535 return 1;
1536 }
1537 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001538 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1539 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001540 return 1;
1541 }
1542 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001543 else if (strcmp(args[1], "maxconn") == 0) {
1544 if (strcmp(args[2], "frontend") == 0) {
1545 struct proxy *px;
1546 struct listener *l;
1547 int v;
1548
Willy Tarreau532a4502011-09-07 22:37:44 +02001549 px = expect_frontend_admin(s, si, args[3]);
1550 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001551 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001552
1553 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001554 appctx->ctx.cli.msg = "Integer value expected.\n";
1555 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001556 return 1;
1557 }
1558
1559 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001560 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001561 appctx->ctx.cli.msg = "Value out of range.\n";
1562 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001563 return 1;
1564 }
1565
1566 /* OK, the value is fine, so we assign it to the proxy and to all of
1567 * its listeners. The blocked ones will be dequeued.
1568 */
1569 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001570 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001571 l->maxconn = v;
1572 if (l->state == LI_FULL)
1573 resume_listener(l);
1574 }
1575
Willy Tarreau147c1b02016-11-24 15:25:39 +01001576 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&px->listener_queue))
1577 dequeue_all_listeners(&px->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001578
1579 return 1;
1580 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001581 else if (strcmp(args[2], "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 Tarreau91886b62011-09-07 14:38:31 +02001587 return 1;
1588 }
1589
1590 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001591 appctx->ctx.cli.msg = "Expects an integer value.\n";
1592 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001593 return 1;
1594 }
1595
1596 v = atoi(args[3]);
1597 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001598 appctx->ctx.cli.msg = "Value out of range.\n";
1599 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001600 return 1;
1601 }
1602
1603 /* check for unlimited values */
1604 if (v <= 0)
1605 v = global.hardmaxconn;
1606
1607 global.maxconn = v;
1608
1609 /* Dequeues all of the listeners waiting for a resource */
1610 if (!LIST_ISEMPTY(&global_listener_queue))
1611 dequeue_all_listeners(&global_listener_queue);
1612
1613 return 1;
1614 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001615 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001616 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1617 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001618 return 1;
1619 }
1620 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001621 else if (strcmp(args[1], "rate-limit") == 0) {
1622 if (strcmp(args[2], "connections") == 0) {
1623 if (strcmp(args[3], "global") == 0) {
1624 int v;
1625
Willy Tarreau290e63a2012-09-20 18:07:14 +02001626 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001627 appctx->ctx.cli.msg = stats_permission_denied_msg;
1628 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001629 return 1;
1630 }
1631
1632 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001633 appctx->ctx.cli.msg = "Expects an integer value.\n";
1634 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001635 return 1;
1636 }
1637
1638 v = atoi(args[4]);
1639 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001640 appctx->ctx.cli.msg = "Value out of range.\n";
1641 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001642 return 1;
1643 }
1644
1645 global.cps_lim = v;
1646
1647 /* Dequeues all of the listeners waiting for a resource */
1648 if (!LIST_ISEMPTY(&global_listener_queue))
1649 dequeue_all_listeners(&global_listener_queue);
1650
1651 return 1;
1652 }
1653 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001654 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1655 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001656 return 1;
1657 }
1658 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001659 else if (strcmp(args[2], "sessions") == 0) {
1660 if (strcmp(args[3], "global") == 0) {
1661 int v;
1662
1663 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1664 appctx->ctx.cli.msg = stats_permission_denied_msg;
1665 appctx->st0 = STAT_CLI_PRINT;
1666 return 1;
1667 }
1668
1669 if (!*args[4]) {
1670 appctx->ctx.cli.msg = "Expects an integer value.\n";
1671 appctx->st0 = STAT_CLI_PRINT;
1672 return 1;
1673 }
1674
1675 v = atoi(args[4]);
1676 if (v < 0) {
1677 appctx->ctx.cli.msg = "Value out of range.\n";
1678 appctx->st0 = STAT_CLI_PRINT;
1679 return 1;
1680 }
1681
1682 global.sps_lim = v;
1683
1684 /* Dequeues all of the listeners waiting for a resource */
1685 if (!LIST_ISEMPTY(&global_listener_queue))
1686 dequeue_all_listeners(&global_listener_queue);
1687
1688 return 1;
1689 }
1690 else {
1691 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1692 appctx->st0 = STAT_CLI_PRINT;
1693 return 1;
1694 }
1695 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001696#ifdef USE_OPENSSL
1697 else if (strcmp(args[2], "ssl-sessions") == 0) {
1698 if (strcmp(args[3], "global") == 0) {
1699 int v;
1700
1701 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1702 appctx->ctx.cli.msg = stats_permission_denied_msg;
1703 appctx->st0 = STAT_CLI_PRINT;
1704 return 1;
1705 }
1706
1707 if (!*args[4]) {
1708 appctx->ctx.cli.msg = "Expects an integer value.\n";
1709 appctx->st0 = STAT_CLI_PRINT;
1710 return 1;
1711 }
1712
1713 v = atoi(args[4]);
1714 if (v < 0) {
1715 appctx->ctx.cli.msg = "Value out of range.\n";
1716 appctx->st0 = STAT_CLI_PRINT;
1717 return 1;
1718 }
1719
1720 global.ssl_lim = v;
1721
1722 /* Dequeues all of the listeners waiting for a resource */
1723 if (!LIST_ISEMPTY(&global_listener_queue))
1724 dequeue_all_listeners(&global_listener_queue);
1725
1726 return 1;
1727 }
1728 else {
1729 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1730 appctx->st0 = STAT_CLI_PRINT;
1731 return 1;
1732 }
1733 }
1734#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001735 else if (strcmp(args[2], "http-compression") == 0) {
1736 if (strcmp(args[3], "global") == 0) {
1737 int v;
1738
Willy Tarreau07ccb482015-11-26 18:32:39 +01001739 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1740 appctx->ctx.cli.msg = stats_permission_denied_msg;
1741 appctx->st0 = STAT_CLI_PRINT;
1742 return 1;
1743 }
1744
Willy Tarreau85d47f92012-11-21 00:29:50 +01001745 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001746 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1747 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001748 return 1;
1749 }
1750
William Lallemandd85f9172012-11-09 17:05:39 +01001751 v = atoi(args[4]);
1752 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1753 }
1754 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001755 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1756 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001757 return 1;
1758 }
1759 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001760 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001761 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001762 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001763 return 1;
1764 }
1765 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001766 else if (strcmp(args[1], "table") == 0) {
1767 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1768 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001769 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001770 char *err;
1771
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001772 /* Set flags. */
1773 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001774
1775 /* Expect three parameters: map name, key and new value. */
1776 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001777 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001778 appctx->st0 = STAT_CLI_PRINT;
1779 return 1;
1780 }
1781
1782 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001783 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001784 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001785 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001786 appctx->st0 = STAT_CLI_PRINT;
1787 return 1;
1788 }
1789
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001790 /* If the entry identifier start with a '#', it is considered as
1791 * pointer id
1792 */
1793 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1794 struct pat_ref_elt *ref;
1795 long long int conv;
1796 char *error;
1797
1798 /* Convert argument to integer value. */
1799 conv = strtoll(&args[3][1], &error, 16);
1800 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001801 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001802 appctx->st0 = STAT_CLI_PRINT;
1803 return 1;
1804 }
1805
1806 /* Convert and check integer to pointer. */
1807 ref = (struct pat_ref_elt *)(long)conv;
1808 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001809 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001810 appctx->st0 = STAT_CLI_PRINT;
1811 return 1;
1812 }
1813
1814 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001815 err = NULL;
1816 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1817 if (err)
1818 memprintf(&err, "%s.\n", err);
1819 appctx->ctx.cli.err = err;
1820 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001821 return 1;
1822 }
1823 }
1824 else {
1825 /* Else, use the entry identifier as pattern
1826 * string, and update the value.
1827 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001828 err = NULL;
1829 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1830 if (err)
1831 memprintf(&err, "%s.\n", err);
1832 appctx->ctx.cli.err = err;
1833 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001834 return 1;
1835 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001836 }
1837
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001838 /* The set is done, send message. */
Thierry FOURNIER73f1d8f2014-12-18 15:28:01 +01001839 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001840 return 1;
1841 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001842#ifdef USE_OPENSSL
1843 else if (strcmp(args[1], "ssl") == 0) {
1844 if (strcmp(args[2], "ocsp-response") == 0) {
1845#ifdef SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB
1846 char *err = NULL;
1847
Emeric Brunaf4ef742014-06-19 14:10:45 +02001848 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001849 if (!*args[3]) {
1850 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1851 appctx->st0 = STAT_CLI_PRINT;
1852 return 1;
1853 }
1854
1855 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1856 if (trash.len < 0) {
1857 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1858 appctx->st0 = STAT_CLI_PRINT;
1859 return 1;
1860 }
1861
1862 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1863 if (err) {
1864 memprintf(&err, "%s.\n", err);
1865 appctx->ctx.cli.err = err;
1866 appctx->st0 = STAT_CLI_PRINT_FREE;
1867 }
1868 return 1;
1869 }
1870 appctx->ctx.cli.msg = "OCSP Response updated!";
1871 appctx->st0 = STAT_CLI_PRINT;
1872 return 1;
1873#else
1874 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1875 appctx->st0 = STAT_CLI_PRINT;
1876 return 1;
1877#endif
1878 }
1879 else {
1880 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1881 appctx->st0 = STAT_CLI_PRINT;
1882 return 1;
1883 }
1884 }
1885#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001886 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001887 return 0;
1888 }
1889 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001890 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001891 if (strcmp(args[1], "agent") == 0) {
1892 struct server *sv;
1893
1894 sv = expect_server_admin(s, si, args[2]);
1895 if (!sv)
1896 return 1;
1897
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001898 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1899 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1900 appctx->st0 = STAT_CLI_PRINT;
1901 return 1;
1902 }
1903
1904 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001905 return 1;
1906 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001907 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001908 struct server *sv;
1909
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001910 sv = expect_server_admin(s, si, args[2]);
1911 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001912 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001913
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001914 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1915 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1916 appctx->st0 = STAT_CLI_PRINT;
1917 return 1;
1918 }
1919
1920 sv->check.state |= CHK_ST_ENABLED;
1921 return 1;
1922 }
1923 else if (strcmp(args[1], "server") == 0) {
1924 struct server *sv;
1925
1926 sv = expect_server_admin(s, si, args[2]);
1927 if (!sv)
1928 return 1;
1929
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001930 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001931 return 1;
1932 }
1933 else if (strcmp(args[1], "frontend") == 0) {
1934 struct proxy *px;
1935
1936 px = expect_frontend_admin(s, si, args[2]);
1937 if (!px)
1938 return 1;
1939
1940 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001941 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1942 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001943 return 1;
1944 }
1945
1946 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001947 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1948 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001949 return 1;
1950 }
1951
1952 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001953 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1954 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001955 return 1;
1956 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001957 return 1;
1958 }
1959 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001960 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001961 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001962 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001963 }
1964 }
1965 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001966 if (strcmp(args[1], "agent") == 0) {
1967 struct server *sv;
1968
1969 sv = expect_server_admin(s, si, args[2]);
1970 if (!sv)
1971 return 1;
1972
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001973 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001974 return 1;
1975 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001976 else if (strcmp(args[1], "health") == 0) {
1977 struct server *sv;
1978
1979 sv = expect_server_admin(s, si, args[2]);
1980 if (!sv)
1981 return 1;
1982
1983 sv->check.state &= ~CHK_ST_ENABLED;
1984 return 1;
1985 }
Simon Horman671b6f02013-11-25 10:46:39 +09001986 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001987 struct server *sv;
1988
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001989 sv = expect_server_admin(s, si, args[2]);
1990 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001991 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001992
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001993 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001994 return 1;
1995 }
1996 else if (strcmp(args[1], "frontend") == 0) {
1997 struct proxy *px;
1998
1999 px = expect_frontend_admin(s, si, args[2]);
2000 if (!px)
2001 return 1;
2002
2003 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002004 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2005 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002006 return 1;
2007 }
2008
2009 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002010 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2011 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002012 return 1;
2013 }
2014
2015 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002016 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2017 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002018 return 1;
2019 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002020 return 1;
2021 }
2022 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002023 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002024 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002025 return 1;
2026 }
2027 }
2028 else if (strcmp(args[0], "shutdown") == 0) {
2029 if (strcmp(args[1], "frontend") == 0) {
2030 struct proxy *px;
2031
2032 px = expect_frontend_admin(s, si, args[2]);
2033 if (!px)
2034 return 1;
2035
2036 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002037 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2038 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002039 return 1;
2040 }
2041
2042 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2043 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2044 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2045 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2046 stop_proxy(px);
2047 return 1;
2048 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002049 else if (strcmp(args[1], "session") == 0) {
2050 struct session *sess, *ptr;
2051
Willy Tarreau290e63a2012-09-20 18:07:14 +02002052 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002053 appctx->ctx.cli.msg = stats_permission_denied_msg;
2054 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002055 return 1;
2056 }
2057
2058 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002059 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2060 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002061 return 1;
2062 }
2063
2064 ptr = (void *)strtoul(args[2], NULL, 0);
2065
2066 /* first, look for the requested session in the session table */
2067 list_for_each_entry(sess, &sessions, list) {
2068 if (sess == ptr)
2069 break;
2070 }
2071
2072 /* do we have the session ? */
2073 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002074 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2075 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002076 return 1;
2077 }
2078
2079 session_shutdown(sess, SN_ERR_KILLED);
2080 return 1;
2081 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002082 else if (strcmp(args[1], "sessions") == 0) {
2083 if (strcmp(args[2], "server") == 0) {
2084 struct server *sv;
2085 struct session *sess, *sess_bck;
2086
2087 sv = expect_server_admin(s, si, args[3]);
2088 if (!sv)
2089 return 1;
2090
2091 /* kill all the session that are on this server */
2092 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2093 if (sess->srv_conn == sv)
2094 session_shutdown(sess, SN_ERR_KILLED);
2095
2096 return 1;
2097 }
2098 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002099 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2100 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002101 return 1;
2102 }
2103 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002104 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002105 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2106 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002107 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002108 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002109 }
2110 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002111 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2112 if (args[1][0] == 'm')
2113 appctx->ctx.map.display_flags = PAT_REF_MAP;
2114 else
2115 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002116
2117 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002118 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2119 if (!*args[2] || !*args[3]) {
2120 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2121 appctx->st0 = STAT_CLI_PRINT;
2122 return 1;
2123 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002124 }
2125
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002126 else {
2127 if (!*args[2] || !*args[3]) {
2128 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2129 appctx->st0 = STAT_CLI_PRINT;
2130 return 1;
2131 }
2132 }
2133
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002134 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002135 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002136 if (!appctx->ctx.map.ref ||
2137 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002138 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002139 appctx->st0 = STAT_CLI_PRINT;
2140 return 1;
2141 }
2142
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002143 /* If the entry identifier start with a '#', it is considered as
2144 * pointer id
2145 */
2146 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2147 struct pat_ref_elt *ref;
2148 long long int conv;
2149 char *error;
2150
2151 /* Convert argument to integer value. */
2152 conv = strtoll(&args[3][1], &error, 16);
2153 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002154 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002155 appctx->st0 = STAT_CLI_PRINT;
2156 return 1;
2157 }
2158
2159 /* Convert and check integer to pointer. */
2160 ref = (struct pat_ref_elt *)(long)conv;
2161 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002162 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002163 appctx->st0 = STAT_CLI_PRINT;
2164 return 1;
2165 }
2166
2167 /* Try to delete the entry. */
2168 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2169 /* The entry is not found, send message. */
2170 appctx->ctx.cli.msg = "Key not found.\n";
2171 appctx->st0 = STAT_CLI_PRINT;
2172 return 1;
2173 }
2174 }
2175 else {
2176 /* Else, use the entry identifier as pattern
2177 * string and try to delete the entry.
2178 */
2179 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2180 /* The entry is not found, send message. */
2181 appctx->ctx.cli.msg = "Key not found.\n";
2182 appctx->st0 = STAT_CLI_PRINT;
2183 return 1;
2184 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002185 }
2186
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002187 /* The deletion is done, send message. */
Thierry FOURNIER73f1d8f2014-12-18 15:28:01 +01002188 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002189 return 1;
2190 }
2191 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002192 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002193 appctx->st0 = STAT_CLI_PRINT;
2194 return 1;
2195 }
2196 }
2197 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002198 if (strcmp(args[1], "map") == 0 ||
2199 strcmp(args[1], "acl") == 0) {
2200 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002201 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002202
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002203 /* Set flags. */
2204 if (args[1][0] == 'm')
2205 appctx->ctx.map.display_flags = PAT_REF_MAP;
2206 else
2207 appctx->ctx.map.display_flags = PAT_REF_ACL;
2208
2209 /* If the keywork is "map", we expect three parameters, if it
2210 * is "acl", we expect only two parameters
2211 */
2212 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2213 if (!*args[2] || !*args[3] || !*args[4]) {
2214 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2215 appctx->st0 = STAT_CLI_PRINT;
2216 return 1;
2217 }
2218 }
2219 else {
2220 if (!*args[2] || !*args[3]) {
2221 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2222 appctx->st0 = STAT_CLI_PRINT;
2223 return 1;
2224 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002225 }
2226
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002227 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002228 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002229 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002230 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002231 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002232 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002233 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002234 appctx->st0 = STAT_CLI_PRINT;
2235 return 1;
2236 }
2237
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002238 /* The command "add acl" is prohibited if the reference
2239 * use samples.
2240 */
2241 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2242 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2243 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2244 "You must use the command 'add map' to add values.\n";
2245 appctx->st0 = STAT_CLI_PRINT;
2246 return 1;
2247 }
2248
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002249 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002250 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002251 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002252 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002253 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002254 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002255 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002256 if (err)
2257 memprintf(&err, "%s.\n", err);
2258 appctx->ctx.cli.err = err;
2259 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002260 return 1;
2261 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002262
2263 /* The add is done, send message. */
Thierry FOURNIER73f1d8f2014-12-18 15:28:01 +01002264 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002265 return 1;
2266 }
2267 else { /* unknown "del" parameter */
2268 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2269 appctx->st0 = STAT_CLI_PRINT;
2270 return 1;
2271 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002272 }
2273 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002274 return 0;
2275 }
2276 return 1;
2277}
2278
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002279/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002280 * used to processes I/O from/to the stats unix socket. The system relies on a
2281 * state machine handling requests and various responses. We read a request,
2282 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002283 * Then we can read again. The state is stored in appctx->st0 and is one of the
2284 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002285 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002286 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002287static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002288{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002289 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002290 struct channel *req = si->ob;
2291 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002292 int reql;
2293 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002294
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002295 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2296 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002297
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002298 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002299 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002300 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002301 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2302 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002303 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002304 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002305 /* Let's close for real now. We just close the request
2306 * side, the conditions below will complete if needed.
2307 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002308 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002309 break;
2310 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002311 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002312 /* ensure we have some output room left in the event we
2313 * would want to return some info right after parsing.
2314 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002315 if (buffer_almost_full(si->ib->buf)) {
2316 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002317 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002318 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002319
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002320 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002321 if (reql <= 0) { /* closed or EOL not found */
2322 if (reql == 0)
2323 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002324 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002325 continue;
2326 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002327
Chad Lavoie3dac86e2016-05-26 16:42:25 -04002328 /* seek for a possible unescaped semi-colon. If we find
2329 * one, we replace it with an LF and skip only this part.
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002330 */
Chad Lavoie3dac86e2016-05-26 16:42:25 -04002331 for (len = 0; len < reql; len++) {
2332 if (trash.str[len] == '\\') {
2333 len++;
2334 continue;
2335 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002336 if (trash.str[len] == ';') {
2337 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002338 reql = len + 1;
2339 break;
2340 }
Chad Lavoie3dac86e2016-05-26 16:42:25 -04002341 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002342
Willy Tarreau816fc222009-10-04 07:36:58 +02002343 /* now it is time to check that we have a full line,
2344 * remove the trailing \n and possibly \r, then cut the
2345 * line.
2346 */
2347 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002348 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002349 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002350 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002351 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002352
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002353 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002354 len--;
2355
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002356 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002357
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002358 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002359 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002360 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002361 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002362 continue;
2363 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002364 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002365 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002366 else if (strcmp(trash.str, "help") == 0 ||
2367 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002368 appctx->ctx.cli.msg = stats_sock_usage_msg;
2369 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002370 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002371 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002372 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002373 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002374 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002375 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002376 /* if prompt is disabled, print help on empty lines,
2377 * so that the user at least knows how to enable
2378 * prompt and find help.
2379 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002380 appctx->ctx.cli.msg = stats_sock_usage_msg;
2381 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002382 }
2383
2384 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002385 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002386 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002387 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002388 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002389 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreaue309ab72014-10-22 19:06:31 +02002390 cli_release_handler(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002391 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002392 continue;
2393 }
2394
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002395 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002396 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002397 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2398 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002399 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002400 case STAT_CLI_PRINT_FREE:
2401 if (bi_putstr(si->ib, appctx->ctx.cli.err) != -1) {
2402 free(appctx->ctx.cli.err);
2403 appctx->st0 = STAT_CLI_PROMPT;
2404 }
2405 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002406 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002407 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002408 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002409 break;
2410 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002411 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002412 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002413 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002414 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002415 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002416 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002417 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002418 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002419 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002420 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002421 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002422 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002423 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002424 if (stats_table_request(si, appctx->st0))
2425 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002426 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002427 case STAT_CLI_O_PATS:
2428 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002429 appctx->st0 = STAT_CLI_PROMPT;
2430 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002431 case STAT_CLI_O_PAT:
2432 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002433 appctx->st0 = STAT_CLI_PROMPT;
2434 break;
2435 case STAT_CLI_O_MLOOK:
2436 if (stats_map_lookup(si))
2437 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002438 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002439 case STAT_CLI_O_POOLS:
2440 if (stats_dump_pools_to_buffer(si))
2441 appctx->st0 = STAT_CLI_PROMPT;
2442 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002443 default: /* abnormal state */
Willy Tarreaue309ab72014-10-22 19:06:31 +02002444 cli_release_handler(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002445 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002446 break;
2447 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002448
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002449 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002450 if (appctx->st0 == STAT_CLI_PROMPT) {
2451 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2452 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002453 }
2454
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002455 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002456 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002457 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002458
2459 /* Now we close the output if one of the writers did so,
2460 * or if we're not in interactive mode and the request
2461 * buffer is empty. This still allows pipelined requests
2462 * to be sent in non-interactive mode.
2463 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002464 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2465 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002466 continue;
2467 }
2468
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002469 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002470 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002471 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002472 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002473
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002474 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002475 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2476 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002477 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002478 * and nothing more to consume. This is comparable to a broken pipe, so
2479 * we forward the close to the request side so that it flows upstream to
2480 * the client.
2481 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002482 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002483 }
2484
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002485 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002486 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2487 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2488 /* We have no more processing to do, and nothing more to send, and
2489 * the client side has closed. So we'll forward this state downstream
2490 * on the response buffer.
2491 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002492 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002493 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002494 }
2495
2496 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002497 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002498
2499 /* we don't want to expire timeouts while we're processing requests */
2500 si->ib->rex = TICK_ETERNITY;
2501 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002502
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002503 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002504 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 +02002505 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002506 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 +02002507
2508 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2509 /* check that we have released everything then unregister */
2510 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002511 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002512}
2513
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002514/* This function dumps information onto the stream interface's read buffer.
2515 * It returns 0 as long as it does not complete, non-zero upon completion.
2516 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002517 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002518static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002519{
2520 unsigned int up = (now.tv_sec - start_date.tv_sec);
2521
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002522#ifdef USE_OPENSSL
2523 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2524 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2525 int ssl_reuse = 0;
2526
2527 if (ssl_key_rate < ssl_sess_rate) {
2528 /* count the ssl reuse ratio and avoid overflows in both directions */
2529 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2530 }
2531#endif
2532
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002533 chunk_printf(&trash,
2534 "Name: " PRODUCT_NAME "\n"
2535 "Version: " HAPROXY_VERSION "\n"
2536 "Release_date: " HAPROXY_DATE "\n"
2537 "Nbproc: %d\n"
2538 "Process_num: %d\n"
2539 "Pid: %d\n"
2540 "Uptime: %dd %dh%02dm%02ds\n"
2541 "Uptime_sec: %d\n"
2542 "Memmax_MB: %d\n"
2543 "Ulimit-n: %d\n"
2544 "Maxsock: %d\n"
2545 "Maxconn: %d\n"
2546 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002547 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002548 "CumConns: %d\n"
Warren Turkalbc4e62d2015-01-27 15:04:16 -08002549 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002550#ifdef USE_OPENSSL
2551 "MaxSslConns: %d\n"
2552 "CurrSslConns: %d\n"
2553 "CumSslConns: %d\n"
2554#endif
2555 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002556 "PipesUsed: %d\n"
2557 "PipesFree: %d\n"
2558 "ConnRate: %d\n"
2559 "ConnRateLimit: %d\n"
2560 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002561 "SessRate: %d\n"
2562 "SessRateLimit: %d\n"
2563 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002564#ifdef USE_OPENSSL
2565 "SslRate: %d\n"
2566 "SslRateLimit: %d\n"
2567 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002568 "SslFrontendKeyRate: %d\n"
2569 "SslFrontendMaxKeyRate: %d\n"
2570 "SslFrontendSessionReuse_pct: %d\n"
2571 "SslBackendKeyRate: %d\n"
2572 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002573 "SslCacheLookups: %u\n"
2574 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002575#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002576 "CompressBpsIn: %u\n"
2577 "CompressBpsOut: %u\n"
2578 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002579#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002580 "ZlibMemUsage: %ld\n"
2581 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002582#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002583 "Tasks: %d\n"
2584 "Run_queue: %d\n"
2585 "Idle_pct: %d\n"
2586 "node: %s\n"
2587 "description: %s\n"
2588 "",
2589 global.nbproc,
2590 relative_pid,
2591 pid,
2592 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2593 up,
2594 global.rlimit_memmax,
2595 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002596 global.maxsock, global.maxconn, global.hardmaxconn,
2597 actconn, totalconn, global.req_count,
2598#ifdef USE_OPENSSL
2599 global.maxsslconn, sslconns, totalsslconns,
2600#endif
2601 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002602 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002603 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002604#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002605 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2606 ssl_key_rate, global.ssl_fe_keys_max,
2607 ssl_reuse,
2608 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002609 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002610#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002611 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2612 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002613#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002614 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002615#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002616 nb_tasks_cur, run_queue_cur, idle_pct,
2617 global.node, global.desc ? global.desc : ""
2618 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002619
2620 if (bi_putchk(si->ib, &trash) == -1)
2621 return 0;
2622
2623 return 1;
2624}
2625
Willy Tarreau12833bb2014-01-28 16:49:56 +01002626/* This function dumps memory usage information onto the stream interface's
2627 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2628 * completion. No state is used.
2629 */
2630static int stats_dump_pools_to_buffer(struct stream_interface *si)
2631{
2632 dump_pools_to_trash();
2633 if (bi_putchk(si->ib, &trash) == -1)
2634 return 0;
2635 return 1;
2636}
2637
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002638/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2639 * the state from stream interface <si>. The caller is responsible for clearing
2640 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002641 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002642static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002643{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002644 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002645 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002646
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002647 if (!(px->cap & PR_CAP_FE))
2648 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002649
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002650 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002651 return 0;
2652
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002653 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002654 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002655 /* name, queue */
2656 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002657
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002658 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002659 /* Column sub-heading for Enable or Disable server */
2660 chunk_appendf(&trash, "<td></td>");
2661 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002662
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002663 chunk_appendf(&trash,
2664 "<td class=ac>"
2665 "<a name=\"%s/Frontend\"></a>"
2666 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2667 "<td colspan=3></td>"
2668 "",
2669 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002670
Willy Tarreau466c9b52012-12-23 02:25:03 +01002671 chunk_appendf(&trash,
2672 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002673 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002674 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2675 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2676 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002677 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2678 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2679 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002680
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002681 if (px->mode == PR_MODE_HTTP)
2682 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002683 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002684 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002685
2686 chunk_appendf(&trash,
2687 "</table></div></u></td>"
2688 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002689 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002690 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2691 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2692 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002693 U2H(px->fe_counters.sps_max),
2694 U2H(px->fe_counters.cps_max),
2695 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002696
2697 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002698 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002699 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002700 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002701
2702 chunk_appendf(&trash,
2703 "</table></div></u></td>"
2704 /* sessions rate : limit */
2705 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002706 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002707
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002708 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002709 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002710 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002711 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002712 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2713 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002714 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002715 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2716 U2H(px->fe_counters.cum_sess),
2717 U2H(px->fe_counters.cum_conn),
2718 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002719
Willy Tarreau466c9b52012-12-23 02:25:03 +01002720 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002721 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002722 chunk_appendf(&trash,
2723 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2724 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2725 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2726 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2727 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2728 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2729 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2730 "<tr><th>- other responses:</th><td>%s</td></tr>"
2731 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2732 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002733 U2H(px->fe_counters.p.http.cum_req),
2734 U2H(px->fe_counters.p.http.rsp[1]),
2735 U2H(px->fe_counters.p.http.rsp[2]),
2736 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002737 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002738 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002739 U2H(px->fe_counters.p.http.rsp[3]),
2740 U2H(px->fe_counters.p.http.rsp[4]),
2741 U2H(px->fe_counters.p.http.rsp[5]),
2742 U2H(px->fe_counters.p.http.rsp[0]),
2743 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002744 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002745
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002746 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002747 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002748 /* sessions: lbtot, lastsess */
2749 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002750 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002751 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002752 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002753 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002754
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002755 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002756 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002757 "<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 +01002758 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002759 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002760 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2761 px->fe_counters.comp_in ?
2762 (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 +01002763 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002764
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002765 chunk_appendf(&trash,
2766 /* denied: req, resp */
2767 "<td>%s</td><td>%s</td>"
2768 /* errors : request, connect, response */
2769 "<td>%s</td><td></td><td></td>"
2770 /* warnings: retries, redispatches */
2771 "<td></td><td></td>"
2772 /* server status : reflect frontend status */
2773 "<td class=ac>%s</td>"
2774 /* rest of server: nothing */
2775 "<td class=ac colspan=8></td></tr>"
2776 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002777 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2778 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002779 px->state == PR_STREADY ? "OPEN" :
2780 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002781 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002782 else { /* CSV mode */
2783 chunk_appendf(&trash,
2784 /* pxid, name, queue cur, queue max, */
2785 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002786 /* sessions : current, max, limit, total */
2787 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002788 /* bytes : in, out */
2789 "%lld,%lld,"
2790 /* denied: req, resp */
2791 "%lld,%lld,"
2792 /* errors : request, connect, response */
2793 "%lld,,,"
2794 /* warnings: retries, redispatches */
2795 ",,"
2796 /* server status : reflect frontend status */
2797 "%s,"
2798 /* rest of server: nothing */
2799 ",,,,,,,,"
2800 /* pid, iid, sid, throttle, lbtot, tracked, type */
2801 "%d,%d,0,,,,%d,"
2802 /* rate, rate_lim, rate_max */
2803 "%u,%u,%u,"
2804 /* check_status, check_code, check_duration */
2805 ",,,",
2806 px->id,
2807 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2808 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2809 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2810 px->fe_counters.failed_req,
2811 px->state == PR_STREADY ? "OPEN" :
2812 px->state == PR_STFULL ? "FULL" : "STOP",
2813 relative_pid, px->uuid, STATS_TYPE_FE,
2814 read_freq_ctr(&px->fe_sess_per_sec),
2815 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002816
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002817 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2818 if (px->mode == PR_MODE_HTTP) {
2819 for (i=1; i<6; i++)
2820 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2821 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2822 }
2823 else
2824 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002825
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002826 /* failed health analyses */
2827 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002828
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002829 /* requests : req_rate, req_rate_max, req_tot, */
2830 chunk_appendf(&trash, "%u,%u,%lld,",
2831 read_freq_ctr(&px->fe_req_per_sec),
2832 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2833
2834 /* errors: cli_aborts, srv_aborts */
2835 chunk_appendf(&trash, ",,");
2836
2837 /* compression: in, out, bypassed */
2838 chunk_appendf(&trash, "%lld,%lld,%lld,",
2839 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2840
2841 /* compression: comp_rsp */
2842 chunk_appendf(&trash, "%lld,",
2843 px->fe_counters.p.http.comp_rsp);
2844
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002845 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2846 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002847
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002848 /* finish with EOL */
2849 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002850 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002851 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002852}
2853
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002854/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2855 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2856 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2857 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002858 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002859static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002860{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002861 struct appctx *appctx = __objt_appctx(si->end);
2862
2863 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002864 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002865 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002866 /* Column sub-heading for Enable or Disable server */
2867 chunk_appendf(&trash, "<td></td>");
2868 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002869 chunk_appendf(&trash,
2870 /* frontend name, listener name */
2871 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2872 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2873 "",
2874 px->id, l->name,
2875 (flags & ST_SHLGNDS)?"<u>":"",
2876 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002877
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002878 if (flags & ST_SHLGNDS) {
2879 char str[INET6_ADDRSTRLEN];
2880 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002881
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002882 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002883
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002884 port = get_host_port(&l->addr);
2885 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2886 case AF_INET:
2887 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2888 break;
2889 case AF_INET6:
2890 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2891 break;
2892 case AF_UNIX:
2893 chunk_appendf(&trash, "unix, ");
2894 break;
2895 case -1:
2896 chunk_appendf(&trash, "(%s), ", strerror(errno));
2897 break;
2898 }
Willy Tarreau91861262007-10-17 17:06:05 +02002899
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002900 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002901 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002902 }
Willy Tarreau91861262007-10-17 17:06:05 +02002903
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002904 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002905 /* queue */
2906 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002907 /* sessions rate: current, max, limit */
2908 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002909 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002910 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002911 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002912 /* bytes: in, out */
2913 "<td>%s</td><td>%s</td>"
2914 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002915 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002916 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2917 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002918
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002919 chunk_appendf(&trash,
2920 /* denied: req, resp */
2921 "<td>%s</td><td>%s</td>"
2922 /* errors: request, connect, response */
2923 "<td>%s</td><td></td><td></td>"
2924 /* warnings: retries, redispatches */
2925 "<td></td><td></td>"
2926 /* server status: reflect listener status */
2927 "<td class=ac>%s</td>"
2928 /* rest of server: nothing */
2929 "<td class=ac colspan=8></td></tr>"
2930 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002931 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2932 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002933 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2934 }
2935 else { /* CSV mode */
2936 chunk_appendf(&trash,
2937 /* pxid, name, queue cur, queue max, */
2938 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002939 /* sessions: current, max, limit, total */
2940 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002941 /* bytes: in, out */
2942 "%lld,%lld,"
2943 /* denied: req, resp */
2944 "%lld,%lld,"
2945 /* errors: request, connect, response */
2946 "%lld,,,"
2947 /* warnings: retries, redispatches */
2948 ",,"
2949 /* server status: reflect listener status */
2950 "%s,"
2951 /* rest of server: nothing */
2952 ",,,,,,,,"
2953 /* pid, iid, sid, throttle, lbtot, tracked, type */
2954 "%d,%d,%d,,,,%d,"
2955 /* rate, rate_lim, rate_max */
2956 ",,,"
2957 /* check_status, check_code, check_duration */
2958 ",,,"
2959 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2960 ",,,,,,"
2961 /* failed health analyses */
2962 ","
2963 /* requests : req_rate, req_rate_max, req_tot, */
2964 ",,,"
2965 /* errors: cli_aborts, srv_aborts */
2966 ",,"
2967 /* compression: in, out, bypassed, comp_rsp */
2968 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002969 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2970 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002971 "\n",
2972 px->id, l->name,
2973 l->nbconn, l->counters->conn_max,
2974 l->maxconn, l->counters->cum_conn,
2975 l->counters->bytes_in, l->counters->bytes_out,
2976 l->counters->denied_req, l->counters->denied_resp,
2977 l->counters->failed_req,
2978 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2979 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2980 }
2981 return 1;
2982}
Willy Tarreau91861262007-10-17 17:06:05 +02002983
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002984/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2985 * from stream interface <si>, stats flags <flags>, and server state <state>.
2986 * The caller is responsible for clearing the trash if needed. Returns non-zero
2987 * if it emits anything, zero otherwise. The <state> parameter can take the
Willy Tarreau9638efa2014-05-23 11:19:57 +02002988 * following values : 0=DOWN, 1=DOWN(agent) 2=going up, 3=going down, 4=UP, 5,6=NOLB,
2989 * 7,8=DRAIN, 9=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002990 */
2991static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2992{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002993 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002994 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002995 char str[INET6_ADDRSTRLEN];
2996 struct chunk src;
2997 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002998
Willy Tarreau32091232014-05-16 13:52:00 +02002999 /* we have "via" which is the tracked server as described in the configuration,
3000 * and "ref" which is the checked server and the end of the chain.
3001 */
3002 via = sv->track ? sv->track : sv;
3003 ref = via;
3004 while (ref->track)
3005 ref = ref->track;
3006
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003007 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02003008 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003009 "DOWN",
Willy Tarreau9638efa2014-05-23 11:19:57 +02003010 "DOWN (agent)",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003011 "DN %d/%d &uarr;",
3012 "UP %d/%d &darr;",
3013 "UP",
3014 "NOLB %d/%d &darr;",
3015 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09003016 "DRAIN %d/%d &darr;",
3017 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003018 "<i>no check</i>"
3019 };
Willy Tarreau91861262007-10-17 17:06:05 +02003020
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003021 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003022 chunk_appendf(&trash, "<tr class=\"maintain\">");
3023 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003024 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003025 "<tr class=\"%s%d\">",
Willy Tarreauc93cd162014-05-13 15:54:22 +02003026 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02003027
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003028 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003029 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003030 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3031 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003032
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003033 chunk_appendf(&trash,
3034 "<td class=ac><a name=\"%s/%s\"></a>%s"
3035 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3036 "",
3037 px->id, sv->id,
3038 (flags & ST_SHLGNDS) ? "<u>" : "",
3039 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003040
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003041 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003042 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003043
3044 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3045 case AF_INET:
3046 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3047 break;
3048 case AF_INET6:
3049 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3050 break;
3051 case AF_UNIX:
3052 chunk_appendf(&trash, "unix, ");
3053 break;
3054 case -1:
3055 chunk_appendf(&trash, "(%s), ", strerror(errno));
3056 break;
3057 default: /* address family not supported */
3058 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003059 }
Willy Tarreau91861262007-10-17 17:06:05 +02003060
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003061 /* id */
3062 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003063
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003064 /* cookie */
3065 if (sv->cookie) {
3066 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003067
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003068 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3069 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003070
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003071 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003072 }
3073
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003074 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003075 }
Willy Tarreau91861262007-10-17 17:06:05 +02003076
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003077 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003078 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003079 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003080 /* sessions rate : current, max, limit */
3081 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003082 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003083 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003084 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3085 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003086
Willy Tarreau466c9b52012-12-23 02:25:03 +01003087
3088 chunk_appendf(&trash,
3089 /* sessions: current, max, limit, total */
3090 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003091 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003092 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3093 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003094 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3095 U2H(sv->counters.cum_sess),
3096 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003097
Willy Tarreau466c9b52012-12-23 02:25:03 +01003098 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3099 if (px->mode == PR_MODE_HTTP) {
3100 unsigned long long tot;
3101 for (tot = i = 0; i < 6; i++)
3102 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003103
Willy Tarreau466c9b52012-12-23 02:25:03 +01003104 chunk_appendf(&trash,
3105 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3106 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3107 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3108 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3109 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3110 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3111 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3112 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003113 U2H(tot),
3114 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3115 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3116 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3117 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3118 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3119 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 +02003120 }
Willy Tarreau91861262007-10-17 17:06:05 +02003121
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003122 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3123 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)));
3124 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)));
3125 if (px->mode == PR_MODE_HTTP)
3126 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)));
3127 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)));
3128
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003129 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003130 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003131 /* sessions: lbtot, last */
3132 "<td>%s</td><td>%s</td>",
3133 U2H(sv->counters.cum_lbconn),
3134 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003135
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003136 chunk_appendf(&trash,
3137 /* bytes : in, out */
3138 "<td>%s</td><td>%s</td>"
3139 /* denied: req, resp */
3140 "<td></td><td>%s</td>"
3141 /* errors : request, connect */
3142 "<td></td><td>%s</td>"
3143 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003144 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003145 /* warnings: retries, redispatches */
3146 "<td>%lld</td><td>%lld</td>"
3147 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003148 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3149 U2H(sv->counters.failed_secu),
3150 U2H(sv->counters.failed_conns),
3151 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003152 sv->counters.cli_aborts,
3153 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003154 sv->counters.retries, sv->counters.redispatches);
3155
3156 /* status, lest check */
3157 chunk_appendf(&trash, "<td class=ac>");
3158
Willy Tarreau20125212014-05-13 19:44:56 +02003159 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003160 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3161 chunk_appendf(&trash, "MAINT");
3162 }
Simon Horman8eccbf72014-11-12 15:55:54 +09003163 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003164 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003165 /* DOWN (agent) */
3166 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 +02003167 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003168 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003169 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3170 chunk_appendf(&trash,
3171 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003172 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3173 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003174 }
Willy Tarreau91861262007-10-17 17:06:05 +02003175
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003176 if ((sv->state == SRV_ST_STOPPED) &&
3177 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3178 chunk_appendf(&trash,
3179 "</td><td class=ac><u> %s%s",
3180 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3181 get_check_status_info(sv->agent.status));
3182
3183 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3184 chunk_appendf(&trash, "/%d", sv->agent.code);
3185
3186 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3187 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3188
3189 chunk_appendf(&trash, "<div class=tips>%s",
3190 get_check_status_description(sv->agent.status));
3191 if (*sv->agent.desc) {
3192 chunk_appendf(&trash, ": ");
3193 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3194 chunk_htmlencode(&trash, &src);
3195 }
3196 chunk_appendf(&trash, "</div></u>");
3197 }
3198 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003199 chunk_appendf(&trash,
3200 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003201 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003202 get_check_status_info(sv->check.status));
3203
3204 if (sv->check.status >= HCHK_STATUS_L57DATA)
3205 chunk_appendf(&trash, "/%d", sv->check.code);
3206
3207 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003208 chunk_appendf(&trash, " in %lums", sv->check.duration);
3209
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003210 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003211 get_check_status_description(sv->check.status));
3212 if (*sv->check.desc) {
3213 chunk_appendf(&trash, ": ");
3214 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3215 chunk_htmlencode(&trash, &src);
3216 }
3217 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003218 }
3219 else
3220 chunk_appendf(&trash, "</td><td>");
3221
3222 chunk_appendf(&trash,
3223 /* weight */
3224 "</td><td class=ac>%d</td>"
3225 /* act, bck */
3226 "<td class=ac>%s</td><td class=ac>%s</td>"
3227 "",
3228 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003229 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3230 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003231
3232 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003233 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003234 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003235
3236 if (ref->observe)
3237 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3238
3239 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003240 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003241 "<td>%lld</td><td>%s</td>"
3242 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003243 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003244 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3245 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003246 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3247 /* tracking a server */
3248 chunk_appendf(&trash,
3249 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003250 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003251 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003252 else
3253 chunk_appendf(&trash, "<td colspan=3></td>");
3254
3255 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003256 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003257 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003258 else
3259 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3260 }
3261 else { /* CSV mode */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003262 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003263 "DOWN,",
Willy Tarreau9638efa2014-05-23 11:19:57 +02003264 "DOWN (agent),",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003265 "DOWN %d/%d,",
3266 "UP %d/%d,",
3267 "UP,",
3268 "NOLB %d/%d,",
3269 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09003270 "DRAIN %d/%d,",
3271 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003272 "no check,"
3273 };
3274
3275 chunk_appendf(&trash,
3276 /* pxid, name */
3277 "%s,%s,"
3278 /* queue : current, max */
3279 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003280 /* sessions : current, max, limit, total */
3281 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003282 /* bytes : in, out */
3283 "%lld,%lld,"
3284 /* denied: req, resp */
3285 ",%lld,"
3286 /* errors : request, connect, response */
3287 ",%lld,%lld,"
3288 /* warnings: retries, redispatches */
3289 "%lld,%lld,"
3290 "",
3291 px->id, sv->id,
3292 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003293 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003294 sv->counters.bytes_in, sv->counters.bytes_out,
3295 sv->counters.failed_secu,
3296 sv->counters.failed_conns, sv->counters.failed_resp,
3297 sv->counters.retries, sv->counters.redispatches);
3298
3299 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003300 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003301 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003302 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003303 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003304 else
3305 chunk_appendf(&trash,
3306 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003307 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3308 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003309
3310 chunk_appendf(&trash,
3311 /* weight, active, backup */
3312 "%d,%d,%d,"
3313 "",
3314 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003315 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3316 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003317
3318 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003319 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003320 chunk_appendf(&trash,
3321 "%lld,%lld,%d,%d,",
3322 sv->counters.failed_checks, sv->counters.down_trans,
3323 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3324 else
3325 chunk_appendf(&trash, ",,,,");
3326
3327 /* queue limit, pid, iid, sid, */
3328 chunk_appendf(&trash,
3329 "%s,"
3330 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003331 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332 relative_pid, px->uuid, sv->puid);
3333
3334 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003335 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003336 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003337
3338 /* sessions: lbtot */
3339 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3340
3341 /* tracked */
3342 if (sv->track)
3343 chunk_appendf(&trash, "%s/%s,",
3344 sv->track->proxy->id, sv->track->id);
3345 else
3346 chunk_appendf(&trash, ",");
3347
3348 /* type */
3349 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3350
3351 /* rate */
3352 chunk_appendf(&trash, "%u,,%u,",
3353 read_freq_ctr(&sv->sess_per_sec),
3354 sv->counters.sps_max);
3355
Willy Tarreauff5ae352013-12-11 20:36:34 +01003356 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003357 /* check_status */
3358 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3359
3360 /* check_code */
3361 if (sv->check.status >= HCHK_STATUS_L57DATA)
3362 chunk_appendf(&trash, "%u,", sv->check.code);
3363 else
3364 chunk_appendf(&trash, ",");
3365
3366 /* check_duration */
3367 if (sv->check.status >= HCHK_STATUS_CHECKED)
3368 chunk_appendf(&trash, "%lu,", sv->check.duration);
3369 else
3370 chunk_appendf(&trash, ",");
3371
3372 }
3373 else
3374 chunk_appendf(&trash, ",,,");
3375
3376 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3377 if (px->mode == PR_MODE_HTTP) {
3378 for (i=1; i<6; i++)
3379 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3380
3381 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3382 }
3383 else
3384 chunk_appendf(&trash, ",,,,,,");
3385
3386 /* failed health analyses */
3387 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3388
3389 /* requests : req_rate, req_rate_max, req_tot, */
3390 chunk_appendf(&trash, ",,,");
3391
3392 /* errors: cli_aborts, srv_aborts */
3393 chunk_appendf(&trash, "%lld,%lld,",
3394 sv->counters.cli_aborts, sv->counters.srv_aborts);
3395
3396 /* compression: in, out, bypassed, comp_rsp */
3397 chunk_appendf(&trash, ",,,,");
3398
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003399 /* lastsess */
3400 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3401
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003402 /* capture of last check and agent statuses */
3403 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->check.desc) : "");
3404 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->agent.desc) : "");
3405
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003406 /* qtime, ctime, rtime, ttime, */
3407 chunk_appendf(&trash, "%u,%u,%u,%u,",
3408 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3409 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3410 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3411 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3412
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003413 /* finish with EOL */
3414 chunk_appendf(&trash, "\n");
3415 }
3416 return 1;
3417}
3418
3419/* Dumps a line for backend <px> to the trash for and uses the state from stream
3420 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3421 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3422 */
3423static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3424{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003425 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003426 struct chunk src;
3427 int i;
3428
3429 if (!(px->cap & PR_CAP_BE))
3430 return 0;
3431
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003432 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003433 return 0;
3434
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003435 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003436 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003437 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 /* Column sub-heading for Enable or Disable server */
3439 chunk_appendf(&trash, "<td></td>");
3440 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003441 chunk_appendf(&trash,
3442 "<td class=ac>"
3443 /* name */
3444 "%s<a name=\"%s/Backend\"></a>"
3445 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3446 "",
3447 (flags & ST_SHLGNDS)?"<u>":"",
3448 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003449
3450 if (flags & ST_SHLGNDS) {
3451 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003452 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003453 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3454
3455 /* cookie */
3456 if (px->cookie_name) {
3457 chunk_appendf(&trash, ", cookie: '");
3458 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3459 chunk_htmlencode(&trash, &src);
3460 chunk_appendf(&trash, "'");
3461 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003462 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003463 }
3464
3465 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003466 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003467 /* queue : current, max */
3468 "<td>%s</td><td>%s</td><td></td>"
3469 /* sessions rate : current, max, limit */
3470 "<td>%s</td><td>%s</td><td></td>"
3471 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003472 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003473 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3474 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003475
3476 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003477 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003478 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003479 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003480 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003481 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003482 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3483 U2H(px->be_counters.cum_conn),
3484 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003485
Willy Tarreau466c9b52012-12-23 02:25:03 +01003486 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003487 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003488 chunk_appendf(&trash,
3489 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3490 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3491 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3492 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3493 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3494 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3495 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3496 "<tr><th>- other responses:</th><td>%s</td></tr>"
3497 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003498 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003499 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003500 U2H(px->be_counters.p.http.cum_req),
3501 U2H(px->be_counters.p.http.rsp[1]),
3502 U2H(px->be_counters.p.http.rsp[2]),
3503 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003504 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003505 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003506 U2H(px->be_counters.p.http.rsp[3]),
3507 U2H(px->be_counters.p.http.rsp[4]),
3508 U2H(px->be_counters.p.http.rsp[5]),
3509 U2H(px->be_counters.p.http.rsp[0]),
3510 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003511 }
3512
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003513 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)));
3514 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)));
3515 if (px->mode == PR_MODE_HTTP)
3516 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)));
3517 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)));
3518
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003519 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003520 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003521 /* sessions: lbtot, last */
3522 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003523 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003524 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003525 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003526 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003527 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003528 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003529
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003530 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003531 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003532 "<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 +01003533 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003534 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003535 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3536 px->be_counters.comp_in ?
3537 (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 +01003538 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3539
3540 chunk_appendf(&trash,
3541 /* denied: req, resp */
3542 "<td>%s</td><td>%s</td>"
3543 /* errors : request, connect */
3544 "<td></td><td>%s</td>"
3545 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003546 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003547 /* warnings: retries, redispatches */
3548 "<td>%lld</td><td>%lld</td>"
3549 /* backend status: reflect backend status (up/down): we display UP
3550 * if the backend has known working servers or if it has no server at
3551 * all (eg: for stats). Then we display the total weight, number of
3552 * active and backups. */
3553 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3554 "<td class=ac>%d</td><td class=ac>%d</td>"
3555 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003556 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3557 U2H(px->be_counters.failed_conns),
3558 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003559 px->be_counters.cli_aborts,
3560 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003561 px->be_counters.retries, px->be_counters.redispatches,
3562 human_time(now.tv_sec - px->last_change, 1),
3563 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3564 "<font color=\"red\"><b>DOWN</b></font>",
3565 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3566 px->srv_act, px->srv_bck);
3567
3568 chunk_appendf(&trash,
3569 /* rest of backend: nothing, down transitions, total downtime, throttle */
3570 "<td class=ac>&nbsp;</td><td>%d</td>"
3571 "<td>%s</td>"
3572 "<td></td>"
3573 "</tr>",
3574 px->down_trans,
3575 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3576 }
3577 else { /* CSV mode */
3578 chunk_appendf(&trash,
3579 /* pxid, name */
3580 "%s,BACKEND,"
3581 /* queue : current, max */
3582 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003583 /* sessions : current, max, limit, total */
3584 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003585 /* bytes : in, out */
3586 "%lld,%lld,"
3587 /* denied: req, resp */
3588 "%lld,%lld,"
3589 /* errors : request, connect, response */
3590 ",%lld,%lld,"
3591 /* warnings: retries, redispatches */
3592 "%lld,%lld,"
3593 /* backend status: reflect backend status (up/down): we display UP
3594 * if the backend has known working servers or if it has no server at
3595 * all (eg: for stats). Then we display the total weight, number of
3596 * active and backups. */
3597 "%s,"
3598 "%d,%d,%d,"
3599 /* rest of backend: nothing, down transitions, last change, total downtime */
3600 ",%d,%d,%d,,"
3601 /* pid, iid, sid, throttle, lbtot, tracked, type */
3602 "%d,%d,0,,%lld,,%d,"
3603 /* rate, rate_lim, rate_max, */
3604 "%u,,%u,"
3605 /* check_status, check_code, check_duration */
3606 ",,,",
3607 px->id,
3608 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3609 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3610 px->be_counters.bytes_in, px->be_counters.bytes_out,
3611 px->be_counters.denied_req, px->be_counters.denied_resp,
3612 px->be_counters.failed_conns, px->be_counters.failed_resp,
3613 px->be_counters.retries, px->be_counters.redispatches,
3614 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3615 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3616 px->srv_act, px->srv_bck,
3617 px->down_trans, (int)(now.tv_sec - px->last_change),
3618 px->srv?be_downtime(px):0,
3619 relative_pid, px->uuid,
3620 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3621 read_freq_ctr(&px->be_sess_per_sec),
3622 px->be_counters.sps_max);
3623
3624 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3625 if (px->mode == PR_MODE_HTTP) {
3626 for (i=1; i<6; i++)
3627 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3628 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3629 }
3630 else
3631 chunk_appendf(&trash, ",,,,,,");
3632
3633 /* failed health analyses */
3634 chunk_appendf(&trash, ",");
3635
3636 /* requests : req_rate, req_rate_max, req_tot, */
3637 chunk_appendf(&trash, ",,,");
3638
3639 /* errors: cli_aborts, srv_aborts */
3640 chunk_appendf(&trash, "%lld,%lld,",
3641 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3642
3643 /* compression: in, out, bypassed */
3644 chunk_appendf(&trash, "%lld,%lld,%lld,",
3645 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3646
3647 /* compression: comp_rsp */
3648 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3649
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003650 /* lastsess, last_chk, last_agt, */
3651 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003652
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003653 /* qtime, ctime, rtime, ttime, */
3654 chunk_appendf(&trash, "%u,%u,%u,%u,",
3655 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3656 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3657 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3658 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3659
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003660 /* finish with EOL */
3661 chunk_appendf(&trash, "\n");
3662 }
3663 return 1;
3664}
3665
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003666/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003667 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003668 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003669 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003670static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003671{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003672 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003673 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
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 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003677
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003678 /* 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 +02003679 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003680 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003681 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003682 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3683 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003684 }
3685
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003686 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003687 "<form action=\"%s%s%s%s\" method=\"post\">",
3688 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003689 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3690 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003691 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003692 }
3693
3694 /* print a new table */
3695 chunk_appendf(&trash,
3696 "<table class=\"tbl\" width=\"100%%\">\n"
3697 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003698 "<th class=\"pxname\" width=\"10%%\">");
3699
3700 chunk_appendf(&trash,
3701 "<a name=\"%s\"></a>%s"
3702 "<a class=px href=\"#%s\">%s</a>",
3703 px->id,
3704 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3705 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003706
3707 if (uri->flags & ST_SHLGNDS) {
3708 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003709 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003710 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3711 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003712 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003713 }
3714
3715 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003716 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003717 "<th class=\"%s\" width=\"90%%\">%s</th>"
3718 "</tr>\n"
3719 "</table>\n"
3720 "<table class=\"tbl\" width=\"100%%\">\n"
3721 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003722 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3723 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3724
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003725 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003726 /* Column heading for Enable or Disable server */
3727 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003728 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003729
3730 chunk_appendf(&trash,
3731 "<th rowspan=2></th>"
3732 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003733 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003734 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3735 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3736 "<th colspan=9>Server</th>"
3737 "</tr>\n"
3738 "<tr class=\"titre\">"
3739 "<th>Cur</th><th>Max</th><th>Limit</th>"
3740 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003741 "<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 +01003742 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3743 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3744 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3745 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3746 "<th>Thrtle</th>\n"
3747 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003748}
3749
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003750/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003751 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003752 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003753static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003754{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003755 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003756 chunk_appendf(&trash, "</table>");
3757
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003758 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003759 /* close the form used to enable/disable this proxy servers */
3760 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003761 "Choose the action to perform on the checked servers : "
3762 "<select name=action>"
3763 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003764 "<option value=\"ready\">Set state to READY</option>"
3765 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corted38f5c02014-07-02 17:49:34 +02003766 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003767 "<option value=\"dhlth\">Health: disable checks</option>"
3768 "<option value=\"ehlth\">Health: enable checks</option>"
3769 "<option value=\"hrunn\">Health: force UP</option>"
3770 "<option value=\"hnolb\">Health: force NOLB</option>"
3771 "<option value=\"hdown\">Health: force DOWN</option>"
3772 "<option value=\"dagent\">Agent: disable checks</option>"
3773 "<option value=\"eagent\">Agent: enable checks</option>"
3774 "<option value=\"arunn\">Agent: force UP</option>"
3775 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003776 "<option value=\"shutdown\">Kill Sessions</option>"
3777 "</select>"
3778 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3779 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3780 "</form>",
3781 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003782 }
3783
3784 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003785}
Willy Tarreau91861262007-10-17 17:06:05 +02003786
3787/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003788 * Dumps statistics for a proxy. The output is sent to the stream interface's
3789 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3790 * buffer space, or non-zero if everything completed. This function is used
3791 * both by the CLI and the HTTP entry points, and is able to dump the output
3792 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003793 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003794static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003795{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003796 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003797 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003798 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003799 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003800 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003801
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003802 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003803
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003804 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003805 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003806 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003807 if (uri && uri->scope) {
3808 /* we have a limited scope, we have to check the proxy name */
3809 struct stat_scope *scope;
3810 int len;
3811
3812 len = strlen(px->id);
3813 scope = uri->scope;
3814
3815 while (scope) {
3816 /* match exact proxy name */
3817 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3818 break;
3819
3820 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003821 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003822 break;
3823 scope = scope->next;
3824 }
3825
3826 /* proxy name not found : don't dump anything */
3827 if (scope == NULL)
3828 return 1;
3829 }
3830
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003831 /* if the user has requested a limited output and the proxy
3832 * name does not match, skip it.
3833 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003834 if (appctx->ctx.stats.scope_len &&
3835 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 +02003836 return 1;
3837
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003838 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3839 (appctx->ctx.stats.iid != -1) &&
3840 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003841 return 1;
3842
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003843 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003844 /* fall through */
3845
Willy Tarreau295a8372011-03-10 11:25:07 +01003846 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003847 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003848 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003849 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003850 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003851 }
Willy Tarreau91861262007-10-17 17:06:05 +02003852
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003853 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003854 /* fall through */
3855
Willy Tarreau295a8372011-03-10 11:25:07 +01003856 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003857 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003858 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003859 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003860 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003861
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003862 appctx->ctx.stats.l = px->conf.listeners.n;
3863 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003864 /* fall through */
3865
Willy Tarreau295a8372011-03-10 11:25:07 +01003866 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003867 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003868 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003869 if (buffer_almost_full(rep->buf)) {
3870 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003871 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003872 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003873
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003874 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003875 if (!l->counters)
3876 continue;
3877
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003878 if (appctx->ctx.stats.flags & STAT_BOUND) {
3879 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003880 break;
3881
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003882 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003883 continue;
3884 }
3885
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003886 /* print the frontend */
3887 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3888 if (bi_putchk(rep, &trash) == -1)
3889 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003890 }
3891
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003892 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3893 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003894 /* fall through */
3895
Willy Tarreau295a8372011-03-10 11:25:07 +01003896 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003897 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003898 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02003899 int sv_state;
Willy Tarreau91861262007-10-17 17:06:05 +02003900
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003901 if (buffer_almost_full(rep->buf)) {
3902 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003903 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003904 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003905
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003906 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003907
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003908 if (appctx->ctx.stats.flags & STAT_BOUND) {
3909 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003910 break;
3911
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003912 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003913 continue;
3914 }
3915
Willy Tarreau32091232014-05-16 13:52:00 +02003916 svs = sv;
3917 while (svs->track)
3918 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003919
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003920 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
3921 /* server is UP. The possibilities are :
Willy Tarreau9638efa2014-05-23 11:19:57 +02003922 * - UP, draining, going down => state = 7
3923 * - UP, going down => state = 3
3924 * - UP, draining => state = 8
3925 * - UP, checked => state = 4
3926 * - UP, not checked nor tracked => state = 9
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003927 */
3928
3929 if ((svs->check.state & CHK_ST_ENABLED) &&
3930 (svs->check.health < svs->check.rise + svs->check.fall - 1))
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003931 sv_state = 3;
Willy Tarreau9638efa2014-05-23 11:19:57 +02003932 else
3933 sv_state = 4;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003934
Willy Tarreauefe28222014-05-21 17:13:13 +02003935 if (server_is_draining(sv))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003936 sv_state += 4;
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003937
Willy Tarreau9638efa2014-05-23 11:19:57 +02003938 if (sv_state == 4 && !(svs->check.state & CHK_ST_ENABLED))
3939 sv_state = 9; /* unchecked UP */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003940 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003941 else if (sv->state == SRV_ST_STOPPING) {
3942 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
3943 (svs->check.health == svs->check.rise + svs->check.fall - 1))
Willy Tarreau9638efa2014-05-23 11:19:57 +02003944 sv_state = 6; /* NOLB */
Willy Tarreau91861262007-10-17 17:06:05 +02003945 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003946 sv_state = 5; /* NOLB going down */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003947 }
3948 else { /* stopped */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003949 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health)
3950 sv_state = 1; /* DOWN (agent) */
3951 else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003952 sv_state = 0; /* DOWN */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003953 else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED))
3954 sv_state = 2; /* going up */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003955 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003956 sv_state = 0; /* DOWN, unchecked */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003957 }
Willy Tarreau91861262007-10-17 17:06:05 +02003958
Willy Tarreau9638efa2014-05-23 11:19:57 +02003959 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003960 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003961 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003962 continue;
3963 }
3964
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003965 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3966 if (bi_putchk(rep, &trash) == -1)
3967 return 0;
3968 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003969
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003970 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003971 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003972
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003973 case STAT_PX_ST_BE:
3974 /* print the backend */
3975 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3976 if (bi_putchk(rep, &trash) == -1)
3977 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003978
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003979 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003980 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003981
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003982 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003983 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003984 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003985 if (bi_putchk(rep, &trash) == -1)
3986 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003987 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003988
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003989 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003990 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003991
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003992 case STAT_PX_ST_FIN:
3993 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003994
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003995 default:
3996 /* unknown state, we should put an abort() here ! */
3997 return 1;
3998 }
3999}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004000
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004001/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4002 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004003 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004004static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004005{
4006 /* WARNING! This must fit in the first buffer !!! */
4007 chunk_appendf(&trash,
4008 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4009 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4010 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4011 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4012 "<style type=\"text/css\"><!--\n"
4013 "body {"
4014 " font-family: arial, helvetica, sans-serif;"
4015 " font-size: 12px;"
4016 " font-weight: normal;"
4017 " color: black;"
4018 " background: white;"
4019 "}\n"
4020 "th,td {"
4021 " font-size: 10px;"
4022 "}\n"
4023 "h1 {"
4024 " font-size: x-large;"
4025 " margin-bottom: 0.5em;"
4026 "}\n"
4027 "h2 {"
4028 " font-family: helvetica, arial;"
4029 " font-size: x-large;"
4030 " font-weight: bold;"
4031 " font-style: italic;"
4032 " color: #6020a0;"
4033 " margin-top: 0em;"
4034 " margin-bottom: 0em;"
4035 "}\n"
4036 "h3 {"
4037 " font-family: helvetica, arial;"
4038 " font-size: 16px;"
4039 " font-weight: bold;"
4040 " color: #b00040;"
4041 " background: #e8e8d0;"
4042 " margin-top: 0em;"
4043 " margin-bottom: 0em;"
4044 "}\n"
4045 "li {"
4046 " margin-top: 0.25em;"
4047 " margin-right: 2em;"
4048 "}\n"
4049 ".hr {margin-top: 0.25em;"
4050 " border-color: black;"
4051 " border-bottom-style: solid;"
4052 "}\n"
4053 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4054 ".total {background: #20D0D0;color: #ffff80;}\n"
4055 ".frontend {background: #e8e8d0;}\n"
4056 ".socket {background: #d0d0d0;}\n"
4057 ".backend {background: #e8e8d0;}\n"
4058 ".active0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004059 ".active1 {background: #ff9090;}\n"
4060 ".active2 {background: #ffd020;}\n"
4061 ".active3 {background: #ffffa0;}\n"
4062 ".active4 {background: #c0ffc0;}\n"
4063 ".active5 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
4064 ".active6 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
4065 ".active7 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
4066 ".active8 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
4067 ".active9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004068 ".backup0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004069 ".backup1 {background: #ff9090;}\n"
4070 ".backup2 {background: #ff80ff;}\n"
4071 ".backup3 {background: #c060ff;}\n"
4072 ".backup4 {background: #b0d0ff;}\n"
4073 ".backup5 {background: #c060ff;}\n" /* NOLB state shows same as going down */
4074 ".backup6 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
4075 ".backup7 {background: #c060ff;}\n"
4076 ".backup8 {background: #cc9900;}\n"
4077 ".backup9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004078 ".maintain {background: #c07820;}\n"
4079 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4080 "\n"
4081 "a.px:link {color: #ffff40; text-decoration: none;}"
4082 "a.px:visited {color: #ffff40; text-decoration: none;}"
4083 "a.px:hover {color: #ffffff; text-decoration: none;}"
4084 "a.lfsb:link {color: #000000; text-decoration: none;}"
4085 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4086 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4087 "\n"
4088 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4089 "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"
4090 "table.tbl td.ac { text-align: center;}\n"
4091 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4092 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4093 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4094 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4095 "\n"
4096 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4097 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4098 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004099 "table.det { border-collapse: collapse; border-style: none; }\n"
4100 "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 +01004101 "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 +01004102 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004103 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004104 " display:block;\n"
4105 " visibility:hidden;\n"
4106 " z-index:2147483647;\n"
4107 " position:absolute;\n"
4108 " padding:2px 4px 3px;\n"
4109 " background:#f0f060; color:#000000;\n"
4110 " border:1px solid #7040c0;\n"
4111 " white-space:nowrap;\n"
4112 " font-style:normal;font-size:11px;font-weight:normal;\n"
4113 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4114 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4115 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004116 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004117 "-->\n"
4118 "</style></head>\n",
4119 (uri->flags & ST_SHNODE) ? " on " : "",
4120 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4121 );
4122}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004123
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004124/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004125 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004126 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004127 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004128static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004129{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004130 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004131 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004132 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004133
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004134 /* WARNING! this has to fit the first packet too.
4135 * We are around 3.5 kB, add adding entries will
4136 * become tricky if we want to support 4kB buffers !
4137 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004138 chunk_appendf(&trash,
4139 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4140 PRODUCT_NAME "%s</a></h1>\n"
4141 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4142 "<hr width=\"100%%\" class=\"hr\">\n"
4143 "<h3>&gt; General process information</h3>\n"
4144 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4145 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4146 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4147 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4148 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4149 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4150 "Running tasks: %d/%d; idle = %d %%<br>\n"
4151 "</td><td align=\"center\" nowrap>\n"
4152 "<table class=\"lgd\"><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004153 "<td class=\"active4\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4154 "<td class=\"backup4\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004155 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004156 "<td class=\"active3\"></td><td class=\"noborder\">active UP, going down </td>"
4157 "<td class=\"backup3\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004158 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004159 "<td class=\"active2\"></td><td class=\"noborder\">active DOWN, going up </td>"
4160 "<td class=\"backup2\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004161 "</tr><tr>\n"
4162 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004163 "<td class=\"active9\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004164 "</tr><tr>\n"
4165 "<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 -07004166 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004167 "<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 +01004168 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004169 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004170 "</td>"
4171 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4172 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4173 "",
4174 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4175 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4176 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4177 (uri->flags & ST_SHDESC) ? ": " : "",
4178 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4179 pid, relative_pid, global.nbproc,
4180 up / 86400, (up % 86400) / 3600,
4181 (up % 3600) / 60, (up % 60),
4182 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4183 global.rlimit_memmax ? " MB" : "",
4184 global.rlimit_nofile,
4185 global.maxsock, global.maxconn, global.maxpipes,
4186 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4187 run_queue_cur, nb_tasks_cur, idle_pct
4188 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004189
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004190 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4191 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4192 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004193
4194 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02004195 "<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 +02004196 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004197 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4198 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
4199 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004200 STAT_SCOPE_TXT_MAXLEN);
4201
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004202 /* 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 +02004203 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004204 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004205 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004206 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4207 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004208 }
4209
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004210 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004211 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004212 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004213 uri->uri_prefix,
4214 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004215 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004216 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004217 else
4218 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004219 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004220 uri->uri_prefix,
4221 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004222 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004223 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004224
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004225 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004226 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004227 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004228 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004229 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004230 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004231 "",
4232 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004233 else
4234 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004235 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004236 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004237 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004238 ";norefresh",
4239 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004240 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004241
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004242 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004243 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004244 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004245 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4246 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004247 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004248
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004249 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004250 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004251 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004252 (uri->refresh > 0) ? ";norefresh" : "",
4253 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004254
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004255 chunk_appendf(&trash,
4256 "</ul></td>"
4257 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4258 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4259 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4260 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4261 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4262 "</ul>"
4263 "</td>"
4264 "</tr></table>\n"
4265 ""
4266 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004267
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004268 if (appctx->ctx.stats.st_code) {
4269 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004270 case STAT_STATUS_DONE:
4271 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004272 "<p><div class=active4>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004273 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004274 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004275 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004276 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4277 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004278 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004279 break;
4280 case STAT_STATUS_NONE:
4281 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004282 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004283 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004284 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004285 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004286 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4287 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004288 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004289 break;
4290 case STAT_STATUS_PART:
4291 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004292 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004293 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004294 "Action partially processed.<br>"
4295 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004296 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004297 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4298 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004299 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004300 break;
4301 case STAT_STATUS_ERRP:
4302 chunk_appendf(&trash,
4303 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004304 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004305 "Action not processed because of invalid parameters."
4306 "<ul>"
4307 "<li>The action is maybe unknown.</li>"
4308 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4309 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4310 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004311 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004312 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4313 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004314 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004315 break;
4316 case STAT_STATUS_EXCD:
4317 chunk_appendf(&trash,
4318 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004319 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004320 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4321 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004322 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004323 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4324 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004325 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004326 break;
4327 case STAT_STATUS_DENY:
4328 chunk_appendf(&trash,
4329 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004330 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004331 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004332 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004333 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4334 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004335 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004336 break;
4337 default:
4338 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004339 "<p><div class=active9>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004340 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004341 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004342 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004343 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4344 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004345 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004346 }
4347 chunk_appendf(&trash, "<p>\n");
4348 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004349}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004350
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004351/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4352 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004353 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004354static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004355{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004356 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004357}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004358
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004359/* This function dumps statistics onto the stream interface's read buffer in
4360 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004361 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4362 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
4363 * and the session must be closed, or -1 in case of any error. This function is
4364 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004365 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004366static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004367{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004368 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004369 struct channel *rep = si->ib;
4370 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004371
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004372 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004373
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004374 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004375 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004376 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004377 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004378
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004379 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004380 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004381 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004382 else
4383 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004384
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004385 if (bi_putchk(rep, &trash) == -1)
4386 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004387
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004388 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004389 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004390
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004391 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004392 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004393 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004394 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004395 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004396 }
Willy Tarreau91861262007-10-17 17:06:05 +02004397
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004398 appctx->ctx.stats.px = proxy;
4399 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4400 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004401 /* fall through */
4402
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004403 case STAT_ST_LIST:
4404 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004405 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004406 if (buffer_almost_full(rep->buf)) {
4407 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004408 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004409 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004410
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004411 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004412 /* skip the disabled proxies, global frontend and non-networked ones */
4413 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004414 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004415 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004416
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004417 appctx->ctx.stats.px = px->next;
4418 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004419 }
4420 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004421
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004422 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004423 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004424
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004425 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004426 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004427 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004428 if (bi_putchk(rep, &trash) == -1)
4429 return 0;
4430 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004431
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004432 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004433 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004434
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004435 case STAT_ST_FIN:
4436 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004437
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004438 default:
4439 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004440 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004441 return -1;
4442 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004443}
Willy Tarreauae526782010-03-04 20:34:23 +01004444
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004445/* We reached the stats page through a POST request. The appctx is
4446 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004447 * Parse the posted data and enable/disable servers if necessary.
4448 * Returns 1 if request was parsed or zero if it needs more data.
4449 */
4450static int stats_process_http_post(struct stream_interface *si)
4451{
4452 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004453 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004454
4455 struct proxy *px = NULL;
4456 struct server *sv = NULL;
4457
4458 char key[LINESIZE];
4459 int action = ST_ADM_ACTION_NONE;
4460 int reprocess = 0;
4461
4462 int total_servers = 0;
4463 int altered_servers = 0;
4464
4465 char *first_param, *cur_param, *next_param, *end_params;
4466 char *st_cur_param = NULL;
4467 char *st_next_param = NULL;
4468
4469 struct chunk *temp;
4470 int reql;
4471
4472 temp = get_trash_chunk();
4473 if (temp->size < s->txn.req.body_len) {
4474 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004475 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004476 goto out;
4477 }
4478
4479 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4480 if (reql <= 0) {
4481 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004482 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004483 return 0;
4484 }
4485
4486 first_param = temp->str;
4487 end_params = temp->str + reql;
4488 cur_param = next_param = end_params;
4489 *end_params = '\0';
4490
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004491 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004492
4493 /*
4494 * Parse the parameters in reverse order to only store the last value.
4495 * From the html form, the backend and the action are at the end.
4496 */
4497 while (cur_param > first_param) {
4498 char *value;
4499 int poffset, plen;
4500
4501 cur_param--;
4502
4503 if ((*cur_param == '&') || (cur_param == first_param)) {
4504 reprocess_servers:
4505 /* Parse the key */
4506 poffset = (cur_param != first_param ? 1 : 0);
4507 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4508 if ((plen > 0) && (plen <= sizeof(key))) {
4509 strncpy(key, cur_param + poffset, plen);
4510 key[plen - 1] = '\0';
4511 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004512 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004513 goto out;
4514 }
4515
4516 /* Parse the value */
4517 value = key;
4518 while (*value != '\0' && *value != '=') {
4519 value++;
4520 }
4521 if (*value == '=') {
4522 /* Ok, a value is found, we can mark the end of the key */
4523 *value++ = '\0';
4524 }
4525 if (url_decode(key) < 0 || url_decode(value) < 0)
4526 break;
4527
4528 /* Now we can check the key to see what to do */
4529 if (!px && (strcmp(key, "b") == 0)) {
4530 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4531 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004532 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004533 goto out;
4534 }
4535 }
4536 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004537 if (strcmp(value, "ready") == 0) {
4538 action = ST_ADM_ACTION_READY;
4539 }
4540 else if (strcmp(value, "drain") == 0) {
4541 action = ST_ADM_ACTION_DRAIN;
4542 }
4543 else if (strcmp(value, "maint") == 0) {
4544 action = ST_ADM_ACTION_MAINT;
4545 }
4546 else if (strcmp(value, "shutdown") == 0) {
4547 action = ST_ADM_ACTION_SHUTDOWN;
4548 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004549 else if (strcmp(value, "dhlth") == 0) {
4550 action = ST_ADM_ACTION_DHLTH;
4551 }
4552 else if (strcmp(value, "ehlth") == 0) {
4553 action = ST_ADM_ACTION_EHLTH;
4554 }
4555 else if (strcmp(value, "hrunn") == 0) {
4556 action = ST_ADM_ACTION_HRUNN;
4557 }
4558 else if (strcmp(value, "hnolb") == 0) {
4559 action = ST_ADM_ACTION_HNOLB;
4560 }
4561 else if (strcmp(value, "hdown") == 0) {
4562 action = ST_ADM_ACTION_HDOWN;
4563 }
4564 else if (strcmp(value, "dagent") == 0) {
4565 action = ST_ADM_ACTION_DAGENT;
4566 }
4567 else if (strcmp(value, "eagent") == 0) {
4568 action = ST_ADM_ACTION_EAGENT;
4569 }
4570 else if (strcmp(value, "arunn") == 0) {
4571 action = ST_ADM_ACTION_ARUNN;
4572 }
4573 else if (strcmp(value, "adown") == 0) {
4574 action = ST_ADM_ACTION_ADOWN;
4575 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004576 /* else these are the old supported methods */
4577 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004578 action = ST_ADM_ACTION_DISABLE;
4579 }
4580 else if (strcmp(value, "enable") == 0) {
4581 action = ST_ADM_ACTION_ENABLE;
4582 }
4583 else if (strcmp(value, "stop") == 0) {
4584 action = ST_ADM_ACTION_STOP;
4585 }
4586 else if (strcmp(value, "start") == 0) {
4587 action = ST_ADM_ACTION_START;
4588 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004589 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004590 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004591 goto out;
4592 }
4593 }
4594 else if (strcmp(key, "s") == 0) {
4595 if (!(px && action)) {
4596 /*
4597 * Indicates that we'll need to reprocess the parameters
4598 * as soon as backend and action are known
4599 */
4600 if (!reprocess) {
4601 st_cur_param = cur_param;
4602 st_next_param = next_param;
4603 }
4604 reprocess = 1;
4605 }
4606 else if ((sv = findserver(px, value)) != NULL) {
4607 switch (action) {
4608 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004609 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004610 altered_servers++;
4611 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004612 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004613 }
4614 break;
4615 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004616 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004617 altered_servers++;
4618 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004619 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004620 }
4621 break;
4622 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004623 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4624 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4625 altered_servers++;
4626 total_servers++;
4627 }
4628 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004629 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004630 if (sv->admin & SRV_ADMF_FDRAIN) {
4631 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4632 altered_servers++;
4633 total_servers++;
4634 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004635 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004636 case ST_ADM_ACTION_DHLTH:
4637 if (sv->check.state & CHK_ST_CONFIGURED) {
4638 sv->check.state &= ~CHK_ST_ENABLED;
4639 altered_servers++;
4640 total_servers++;
4641 }
4642 break;
4643 case ST_ADM_ACTION_EHLTH:
4644 if (sv->check.state & CHK_ST_CONFIGURED) {
4645 sv->check.state |= CHK_ST_ENABLED;
4646 altered_servers++;
4647 total_servers++;
4648 }
4649 break;
4650 case ST_ADM_ACTION_HRUNN:
4651 if (!(sv->track)) {
4652 sv->check.health = sv->check.rise + sv->check.fall - 1;
4653 srv_set_running(sv, "changed from Web interface");
4654 altered_servers++;
4655 total_servers++;
4656 }
4657 break;
4658 case ST_ADM_ACTION_HNOLB:
4659 if (!(sv->track)) {
4660 sv->check.health = sv->check.rise + sv->check.fall - 1;
4661 srv_set_stopping(sv, "changed from Web interface");
4662 altered_servers++;
4663 total_servers++;
4664 }
4665 break;
4666 case ST_ADM_ACTION_HDOWN:
4667 if (!(sv->track)) {
4668 sv->check.health = 0;
4669 srv_set_stopped(sv, "changed from Web interface");
4670 altered_servers++;
4671 total_servers++;
4672 }
4673 break;
4674 case ST_ADM_ACTION_DAGENT:
4675 if (sv->agent.state & CHK_ST_CONFIGURED) {
4676 sv->agent.state &= ~CHK_ST_ENABLED;
4677 altered_servers++;
4678 total_servers++;
4679 }
4680 break;
4681 case ST_ADM_ACTION_EAGENT:
4682 if (sv->agent.state & CHK_ST_CONFIGURED) {
4683 sv->agent.state |= CHK_ST_ENABLED;
4684 altered_servers++;
4685 total_servers++;
4686 }
4687 break;
4688 case ST_ADM_ACTION_ARUNN:
4689 if (sv->agent.state & CHK_ST_ENABLED) {
4690 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4691 srv_set_running(sv, "changed from Web interface");
4692 altered_servers++;
4693 total_servers++;
4694 }
4695 break;
4696 case ST_ADM_ACTION_ADOWN:
4697 if (sv->agent.state & CHK_ST_ENABLED) {
4698 sv->agent.health = 0;
4699 srv_set_stopped(sv, "changed from Web interface");
4700 altered_servers++;
4701 total_servers++;
4702 }
4703 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004704 case ST_ADM_ACTION_READY:
4705 srv_adm_set_ready(sv);
4706 altered_servers++;
4707 total_servers++;
4708 break;
4709 case ST_ADM_ACTION_DRAIN:
4710 srv_adm_set_drain(sv);
4711 altered_servers++;
4712 total_servers++;
4713 break;
4714 case ST_ADM_ACTION_MAINT:
4715 srv_adm_set_maint(sv);
4716 altered_servers++;
4717 total_servers++;
4718 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004719 case ST_ADM_ACTION_SHUTDOWN:
4720 if (px->state != PR_STSTOPPED) {
4721 struct session *sess, *sess_bck;
4722
4723 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4724 if (sess->srv_conn == sv)
4725 session_shutdown(sess, SN_ERR_KILLED);
4726
4727 altered_servers++;
4728 total_servers++;
4729 }
4730 break;
4731 }
4732 } else {
4733 /* the server name is unknown or ambiguous (duplicate names) */
4734 total_servers++;
4735 }
4736 }
4737 if (reprocess && px && action) {
4738 /* Now, we know the backend and the action chosen by the user.
4739 * We can safely restart from the first server parameter
4740 * to reprocess them
4741 */
4742 cur_param = st_cur_param;
4743 next_param = st_next_param;
4744 reprocess = 0;
4745 goto reprocess_servers;
4746 }
4747
4748 next_param = cur_param;
4749 }
4750 }
4751
4752 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004753 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004754 }
4755 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004756 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004757 }
4758 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004759 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004760 }
4761 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004762 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004763 }
4764 out:
4765 return 1;
4766}
4767
4768
4769static int stats_send_http_headers(struct stream_interface *si)
4770{
4771 struct session *s = session_from_task(si->owner);
4772 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004773 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004774
4775 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004776 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004777 "Cache-Control: no-cache\r\n"
4778 "Connection: close\r\n"
4779 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004780 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004781
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004782 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004783 chunk_appendf(&trash, "Refresh: %d\r\n",
4784 uri->refresh);
4785
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004786 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4787
4788 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4789 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4790 else
4791 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004792
4793 s->txn.status = 200;
4794 s->logs.tv_request = now;
4795
4796 if (bi_putchk(si->ib, &trash) == -1)
4797 return 0;
4798
4799 return 1;
4800}
4801
4802static int stats_send_http_redirect(struct stream_interface *si)
4803{
4804 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4805 struct session *s = session_from_task(si->owner);
4806 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004807 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004808
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004809 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004810 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004811 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004812 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004813 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4814 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004815 }
4816
4817 /* We don't want to land on the posted stats page because a refresh will
4818 * repost the data. We don't want this to happen on accident so we redirect
4819 * the browse to the stats page with a GET.
4820 */
4821 chunk_printf(&trash,
4822 "HTTP/1.1 303 See Other\r\n"
4823 "Cache-Control: no-cache\r\n"
4824 "Content-Type: text/plain\r\n"
4825 "Connection: close\r\n"
4826 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreauc2f2f802016-01-26 13:57:29 +01004827 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004828 "\r\n",
4829 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004830 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4831 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4832 stat_status_codes[appctx->ctx.stats.st_code]) ?
4833 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004834 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004835 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4836 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004837 scope_txt);
4838
4839 s->txn.status = 303;
4840 s->logs.tv_request = now;
4841
4842 if (bi_putchk(si->ib, &trash) == -1)
4843 return 0;
4844
4845 return 1;
4846}
4847
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004848/* This I/O handler runs as an applet embedded in a stream interface. It is
4849 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004850 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004851 * automatically unregisters itself once transfer is complete.
4852 */
4853static void http_stats_io_handler(struct stream_interface *si)
4854{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004855 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004856 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004857 struct channel *req = si->ob;
4858 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004859
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004860 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4861 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004862
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004863 /* check that the output is not closed */
4864 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004865 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004866
Willy Tarreau347a35d2013-11-22 17:51:09 +01004867 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004868 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004869 if (stats_send_http_headers(si)) {
4870 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004871 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004872 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004873 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004874 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004875 }
4876
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004877 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004878 unsigned int prev_len = si->ib->buf->i;
4879 unsigned int data_len;
4880 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004881 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004882
4883 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4884 /* One difficulty we're facing is that we must prevent
4885 * the input data from being automatically forwarded to
4886 * the output area. For this, we temporarily disable
4887 * forwarding on the channel.
4888 */
4889 last_fwd = si->ib->to_forward;
4890 si->ib->to_forward = 0;
4891 chunk_printf(&trash, "\r\n000000\r\n");
4892 if (bi_putchk(si->ib, &trash) == -1) {
4893 si->ib->to_forward = last_fwd;
4894 goto fail;
4895 }
4896 }
4897
4898 data_len = si->ib->buf->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004899 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004900 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004901
4902 last_len = si->ib->buf->i;
4903
4904 /* Now we must either adjust or remove the chunk size. This is
4905 * not easy because the chunk size might wrap at the end of the
4906 * buffer, so we pretend we have nothing in the buffer, we write
4907 * the size, then restore the buffer's contents. Note that we can
4908 * only do that because no forwarding is scheduled on the stats
4909 * applet.
4910 */
4911 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4912 si->ib->total -= (last_len - prev_len);
4913 si->ib->buf->i -= (last_len - prev_len);
4914
4915 if (last_len != data_len) {
4916 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
4917 bi_putchk(si->ib, &trash);
4918
4919 si->ib->total += (last_len - data_len);
4920 si->ib->buf->i += (last_len - data_len);
4921 }
4922 /* now re-enable forwarding */
4923 channel_forward(si->ib, last_fwd);
4924 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004925 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004926
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004927 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004928 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004929 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004930 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004931 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004932 }
4933
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004934 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004935 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004936 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004937 }
4938
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004939 if (appctx->st0 == STAT_HTTP_DONE) {
4940 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4941 chunk_printf(&trash, "\r\n0\r\n\r\n");
4942 if (bi_putchk(si->ib, &trash) == -1)
4943 goto fail;
4944 }
4945 /* eat the whole request */
4946 bo_skip(si->ob, si->ob->buf->o);
4947 res->flags |= CF_READ_NULL;
4948 si_shutr(si);
4949 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004950
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004951 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4952 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004953
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004954 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004955 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4956 si_shutr(si);
4957 res->flags |= CF_READ_NULL;
4958 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004959 }
Willy Tarreau91861262007-10-17 17:06:05 +02004960
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004961 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004962 /* update all other flags and resync with the other side */
4963 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004964
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004965 /* we don't want to expire timeouts while we're processing requests */
4966 si->ib->rex = TICK_ETERNITY;
4967 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004968
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004969 out:
4970 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4971 /* check that we have released everything then unregister */
4972 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004973 }
4974}
4975
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004976
Willy Tarreau909d5172012-11-26 03:04:41 +01004977static inline const char *get_conn_ctrl_name(const struct connection *conn)
4978{
Willy Tarreau3c728722014-01-23 13:50:42 +01004979 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004980 return "NONE";
4981 return conn->ctrl->name;
4982}
4983
4984static inline const char *get_conn_xprt_name(const struct connection *conn)
4985{
Willy Tarreau6a444d02016-11-24 15:21:26 +01004986 static char ptr[19];
Willy Tarreau909d5172012-11-26 03:04:41 +01004987
Willy Tarreauaad69382014-01-23 14:21:42 +01004988 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004989 return "NONE";
4990
4991 if (conn->xprt == &raw_sock)
4992 return "RAW";
4993
4994#ifdef USE_OPENSSL
4995 if (conn->xprt == &ssl_sock)
4996 return "SSL";
4997#endif
4998 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4999 return ptr;
5000}
5001
5002static inline const char *get_conn_data_name(const struct connection *conn)
5003{
Willy Tarreau6a444d02016-11-24 15:21:26 +01005004 static char ptr[19];
Willy Tarreau909d5172012-11-26 03:04:41 +01005005
5006 if (!conn->data)
5007 return "NONE";
5008
5009 if (conn->data == &sess_conn_cb)
5010 return "SESS";
5011
5012 if (conn->data == &si_conn_cb)
5013 return "STRM";
5014
5015 if (conn->data == &check_conn_cb)
5016 return "CHCK";
5017
5018 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5019 return ptr;
5020}
5021
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005022/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005023 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005024 * 0 if the output buffer is full and it needs to be called again, otherwise
5025 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005026 */
Willy Tarreau76153662012-11-26 01:16:39 +01005027static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005028{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005029 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005030 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005031 extern const char *monthname[12];
5032 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005033 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005034 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005035
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005036 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005037
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005038 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005039 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005040 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
5041 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005042 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005043 appctx->ctx.sess.uid = 0;
5044 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005045 return 1;
5046 }
5047
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005048 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005049 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005050 appctx->ctx.sess.uid = sess->uniq_id;
5051 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005052 /* fall through */
5053
5054 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005055 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005056 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005057 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005058 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005059 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5060 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005061 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01005062 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005063
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005064 conn = objt_conn(sess->si[0].end);
5065 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005066 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005067 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005068 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005069 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005070 break;
5071 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005072 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005073 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005074 default:
5075 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005076 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005077 break;
5078 }
5079
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005080 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005081 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005082 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005083
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005084 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005085 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005086 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
5087 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
5088 sess->listener ? sess->listener->luid : 0);
5089
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005090 if (conn)
5091 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005092
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005093 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005094 case AF_INET:
5095 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005096 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005097 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005098 break;
5099 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005100 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005101 break;
5102 default:
5103 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005104 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005105 break;
5106 }
5107
Willy Tarreau50943332011-09-02 17:33:05 +02005108 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005109 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005110 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005111 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005112 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5113 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005114 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005115
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005116 conn = objt_conn(sess->si[1].end);
5117 if (conn)
5118 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005119
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005120 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005121 case AF_INET:
5122 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005123 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005124 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005125 break;
5126 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005127 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005128 break;
5129 default:
5130 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005131 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005132 break;
5133 }
5134
5135 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005136 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005137 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005138 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5139 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005140 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005141 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005142
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005143 if (conn)
5144 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005145
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005146 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005147 case AF_INET:
5148 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005149 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005150 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005151 break;
5152 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005153 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005154 break;
5155 default:
5156 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005157 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005158 break;
5159 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005160
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005161 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005162 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005163 sess->task,
5164 sess->task->state,
5165 sess->task->nice, sess->task->calls,
5166 sess->task->expire ?
5167 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5168 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5169 TICKS_TO_MS(1000)) : "<NEVER>",
5170 task_in_rq(sess->task) ? ", running" : "");
5171
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005172 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005173 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005174 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5175
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005176 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005177 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
5178 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
5179 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
5180
5181 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005182 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005183 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005184 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005185 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005186 obj_type_name(sess->si[0].end),
5187 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005188 sess->si[0].exp ?
5189 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5190 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5191 TICKS_TO_MS(1000)) : "<NEVER>",
5192 sess->si[0].err_type);
5193
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005194 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005195 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005196 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005197 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005198 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005199 obj_type_name(sess->si[1].end),
5200 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005201 sess->si[1].exp ?
5202 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5203 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5204 TICKS_TO_MS(1000)) : "<NEVER>",
5205 sess->si[1].err_type);
5206
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005207 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005208 chunk_appendf(&trash,
5209 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005210 conn,
5211 get_conn_ctrl_name(conn),
5212 get_conn_xprt_name(conn),
5213 get_conn_data_name(conn),
5214 obj_type_name(conn->target),
5215 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005216
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005217 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005218 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005219 conn->flags,
5220 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005221 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005222 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005223 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005224 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005225 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5226 chunk_appendf(&trash,
5227 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5228 tmpctx,
5229 tmpctx->st0,
5230 tmpctx->st1,
5231 tmpctx->st2,
5232 tmpctx->applet->name);
5233 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005234
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005235 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005236 chunk_appendf(&trash,
5237 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005238 conn,
5239 get_conn_ctrl_name(conn),
5240 get_conn_xprt_name(conn),
5241 get_conn_data_name(conn),
5242 obj_type_name(conn->target),
5243 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005244
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005245 chunk_appendf(&trash,
Willy Tarreaubccd8b62016-01-25 15:27:17 +01005246 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005247 conn->flags,
5248 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005249 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005250 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005251 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005252 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005253 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5254 chunk_appendf(&trash,
5255 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5256 tmpctx,
5257 tmpctx->st0,
5258 tmpctx->st1,
5259 tmpctx->st2,
5260 tmpctx->applet->name);
5261 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005262
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005263 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005264 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005265 " an_exp=%s",
5266 sess->req,
5267 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005268 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01005269 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005270 sess->req->analyse_exp ?
5271 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
5272 TICKS_TO_MS(1000)) : "<NEVER>");
5273
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005274 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005275 " rex=%s",
5276 sess->req->rex ?
5277 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
5278 TICKS_TO_MS(1000)) : "<NEVER>");
5279
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005280 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005281 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005282 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005283 sess->req->wex ?
5284 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
5285 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01005286 sess->req->buf,
5287 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005288 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01005289 sess->txn.req.next, sess->req->buf->i,
5290 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005291
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005292 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005293 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005294 " an_exp=%s",
5295 sess->rep,
5296 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005297 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01005298 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005299 sess->rep->analyse_exp ?
5300 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
5301 TICKS_TO_MS(1000)) : "<NEVER>");
5302
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005303 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005304 " rex=%s",
5305 sess->rep->rex ?
5306 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
5307 TICKS_TO_MS(1000)) : "<NEVER>");
5308
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005309 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005310 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005311 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005312 sess->rep->wex ?
5313 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
5314 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01005315 sess->rep->buf,
5316 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005317 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01005318 sess->txn.rsp.next, sess->rep->buf->i,
5319 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005320
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005321 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005322 return 0;
5323
5324 /* use other states to dump the contents */
5325 }
5326 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005327 appctx->ctx.sess.uid = 0;
5328 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005329 return 1;
5330}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005331
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005332static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005333{
5334 struct appctx *appctx = __objt_appctx(si->end);
5335
5336 switch (appctx->st2) {
5337 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005338 /* Display the column headers. If the message cannot be sent,
5339 * quit the fucntion with returning 0. The function is called
5340 * later and restart at the state "STAT_ST_INIT".
5341 */
5342 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005343 chunk_appendf(&trash, "# id (file) description\n");
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005344 if (bi_putchk(si->ib, &trash) == -1)
5345 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005346
5347 /* Now, we start the browsing of the references lists.
5348 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontéc4cdeb72016-05-06 12:18:50 +02005349 * available field of this pointer is <list>. It is used with the function
5350 * pat_list_get_next() for retruning the first available entry
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005351 */
5352 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5353 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5354 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005355 appctx->st2 = STAT_ST_LIST;
5356 /* fall through */
5357
5358 case STAT_ST_LIST:
5359 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005360 chunk_reset(&trash);
5361
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005362 /* Build messages. If the reference is used by another category than
5363 * the listed categorie, display the information in the massage.
5364 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005365 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005366 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5367 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005368
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005369 if (bi_putchk(si->ib, &trash) == -1) {
5370 /* let's try again later from this session. We add ourselves into
5371 * this session's users so that it can remove us upon termination.
5372 */
5373 return 0;
5374 }
5375
5376 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005377 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5378 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005379 }
5380
5381 appctx->st2 = STAT_ST_FIN;
5382 /* fall through */
5383
5384 default:
5385 appctx->st2 = STAT_ST_FIN;
5386 return 1;
5387 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005388 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005389}
5390
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005391static int stats_map_lookup(struct stream_interface *si)
5392{
5393 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005394 struct sample sample;
5395 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005396 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005397
5398 switch (appctx->st2) {
5399 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005400 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005401 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005402 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005403 appctx->st2 = STAT_ST_LIST;
5404 /* fall through */
5405
5406 case STAT_ST_LIST:
5407 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005408 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005409 /* initialise chunk to build new message */
5410 chunk_reset(&trash);
5411
5412 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005413 sample.type = SMP_T_STR;
Andreas Seltenreicha96d28b2016-03-03 19:32:25 +01005414 sample.flags = SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005415 sample.data.str.len = appctx->ctx.map.chunk.len;
5416 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005417 if (appctx->ctx.map.expr->pat_head->match &&
5418 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005419 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5420 else
5421 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005422
5423 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005424 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5425 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5426 break;
5427 if (match_method >= PAT_MATCH_NUM)
5428 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5429 else
5430 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005431
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005432 /* case sensitive */
5433 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5434 chunk_appendf(&trash, ", case=insensitive");
5435 else
5436 chunk_appendf(&trash, ", case=sensitive");
5437
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005438 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005439 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005440 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5441 chunk_appendf(&trash, ", found=no");
5442 else
5443 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005444 }
5445
5446 /* Display match and match info */
5447 else {
5448 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005449 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5450 chunk_appendf(&trash, ", found=yes");
5451 else
5452 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005453
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005454 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005455 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005456 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005457 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005458 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005459
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005460 /* display pattern */
5461 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5462 if (pat->ref && pat->ref->pattern)
5463 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5464 else
5465 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005466 }
5467 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005468 if (pat->ref && pat->ref->pattern)
5469 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5470 else
5471 chunk_appendf(&trash, ", pattern=unknown");
5472 }
5473
5474 /* display return value */
5475 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5476 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005477 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005478 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005479 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005480 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005481 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005482 }
5483
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005484 chunk_appendf(&trash, "\n");
5485
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005486 /* display response */
5487 if (bi_putchk(si->ib, &trash) == -1) {
5488 /* let's try again later from this session. We add ourselves into
5489 * this session's users so that it can remove us upon termination.
5490 */
5491 return 0;
5492 }
5493
5494 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005495 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5496 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005497 }
5498
5499 appctx->st2 = STAT_ST_FIN;
5500 /* fall through */
5501
5502 default:
5503 appctx->st2 = STAT_ST_FIN;
5504 free(appctx->ctx.map.chunk.str);
5505 return 1;
5506 }
5507}
5508
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005509static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005510{
5511 struct appctx *appctx = __objt_appctx(si->end);
5512
5513 switch (appctx->st2) {
5514
5515 case STAT_ST_INIT:
5516 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005517 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5518 struct pat_ref_elt *, list);
5519 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5520 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005521 appctx->st2 = STAT_ST_LIST;
5522 /* fall through */
5523
5524 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005525 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005526 chunk_reset(&trash);
5527
5528 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005529 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005530 chunk_appendf(&trash, "%p %s %s\n",
5531 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5532 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005533 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005534 chunk_appendf(&trash, "%p %s\n",
5535 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005536
5537 if (bi_putchk(si->ib, &trash) == -1) {
5538 /* let's try again later from this session. We add ourselves into
5539 * this session's users so that it can remove us upon termination.
5540 */
5541 return 0;
5542 }
5543
5544 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005545 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5546 struct pat_ref_elt *, list);
5547 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005548 break;
5549 }
5550
5551 appctx->st2 = STAT_ST_FIN;
5552 /* fall through */
5553
5554 default:
5555 appctx->st2 = STAT_ST_FIN;
5556 return 1;
5557 }
5558}
5559
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005560/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005561 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005562 * to be called again, otherwise non-zero. It is designed to be called
5563 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005564 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005565static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005566{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005567 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005568 struct connection *conn;
5569
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005570 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005571 /* If we're forced to shut down, we might have to remove our
5572 * reference to the last session being dumped.
5573 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005574 if (appctx->st2 == STAT_ST_LIST) {
5575 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5576 LIST_DEL(&appctx->ctx.sess.bref.users);
5577 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005578 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005579 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005580 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005581 }
5582
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005583 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005584
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005585 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005586 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005587 /* the function had not been called yet, let's prepare the
5588 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005589 * pointer to the first in the global list. When a target
5590 * session is being destroyed, it is responsible for updating
5591 * this pointer. We know we have reached the end when this
5592 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005593 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005594 LIST_INIT(&appctx->ctx.sess.bref.users);
5595 appctx->ctx.sess.bref.ref = sessions.n;
5596 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005597 /* fall through */
5598
Willy Tarreau295a8372011-03-10 11:25:07 +01005599 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005600 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005601 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5602 LIST_DEL(&appctx->ctx.sess.bref.users);
5603 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005604 }
5605
5606 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005607 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005608 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005609 struct session *curr_sess;
5610
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005611 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005612
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005613 if (appctx->ctx.sess.target) {
5614 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005615 goto next_sess;
5616
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005617 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005618 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005619 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005620 return 0;
5621
5622 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005623 LIST_DEL(&appctx->ctx.sess.bref.users);
5624 LIST_INIT(&appctx->ctx.sess.bref.users);
5625 if (appctx->ctx.sess.target != (void *)-1) {
5626 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005627 break;
5628 }
5629 else
5630 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005631 }
5632
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005633 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005634 "%p: proto=%s",
5635 curr_sess,
5636 curr_sess->listener->proto->name);
5637
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005638
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005639 conn = objt_conn(curr_sess->si[0].end);
5640 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005641 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005642 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005643 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005644 " src=%s:%d fe=%s be=%s srv=%s",
5645 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005646 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005647 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005648 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005649 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005650 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005651 break;
5652 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005653 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005654 " src=unix:%d fe=%s be=%s srv=%s",
5655 curr_sess->listener->luid,
5656 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005657 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005658 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005659 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005660 break;
5661 }
5662
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005663 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005664 " ts=%02x age=%s calls=%d",
5665 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005666 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5667 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005668
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005669 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005670 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005671 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005672 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005673 curr_sess->req->analysers,
5674 curr_sess->req->rex ?
5675 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5676 TICKS_TO_MS(1000)) : "");
5677
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005678 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005679 ",wx=%s",
5680 curr_sess->req->wex ?
5681 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5682 TICKS_TO_MS(1000)) : "");
5683
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005684 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005685 ",ax=%s]",
5686 curr_sess->req->analyse_exp ?
5687 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5688 TICKS_TO_MS(1000)) : "");
5689
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005690 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005691 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005692 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005693 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005694 curr_sess->rep->analysers,
5695 curr_sess->rep->rex ?
5696 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5697 TICKS_TO_MS(1000)) : "");
5698
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005699 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005700 ",wx=%s",
5701 curr_sess->rep->wex ?
5702 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5703 TICKS_TO_MS(1000)) : "");
5704
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005705 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005706 ",ax=%s]",
5707 curr_sess->rep->analyse_exp ?
5708 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5709 TICKS_TO_MS(1000)) : "");
5710
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005711 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005712 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005713 " s0=[%d,%1xh,fd=%d,ex=%s]",
5714 curr_sess->si[0].state,
5715 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005716 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005717 curr_sess->si[0].exp ?
5718 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5719 TICKS_TO_MS(1000)) : "");
5720
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005721 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005722 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005723 " s1=[%d,%1xh,fd=%d,ex=%s]",
5724 curr_sess->si[1].state,
5725 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005726 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005727 curr_sess->si[1].exp ?
5728 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5729 TICKS_TO_MS(1000)) : "");
5730
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005731 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005732 " exp=%s",
5733 curr_sess->task->expire ?
5734 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5735 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005736 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005737 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005738
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005739 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005740
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005741 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005742 /* let's try again later from this session. We add ourselves into
5743 * this session's users so that it can remove us upon termination.
5744 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005745 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005746 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005747 }
5748
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005749 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005750 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005751 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005752
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005753 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005754 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005755 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005756 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005757 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005758 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005759
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005760 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005761 return 0;
5762
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005763 appctx->ctx.sess.target = NULL;
5764 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005765 return 1;
5766 }
5767
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005768 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005769 /* fall through */
5770
5771 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005772 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005773 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005774 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005775}
5776
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005777/* This is called when the stream interface is closed. For instance, upon an
5778 * external abort, we won't call the i/o handler anymore so we may need to
5779 * remove back references to the session currently being dumped.
5780 */
Simon Horman74d88312013-02-12 10:45:50 +09005781static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005782{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005783 struct appctx *appctx = __objt_appctx(si->end);
5784
5785 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5786 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5787 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005788 }
Willy Tarreau4dac86e2016-11-18 19:17:40 +01005789 else if ((appctx->st0 == STAT_CLI_O_TAB || appctx->st0 == STAT_CLI_O_CLR) &&
5790 appctx->st2 == STAT_ST_LIST) {
5791 appctx->ctx.table.entry->ref_cnt--;
5792 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5793 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005794 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5795 free(appctx->ctx.cli.err);
5796 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005797 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5798 free(appctx->ctx.map.chunk.str);
5799 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005800}
5801
Willy Tarreau20e99322013-04-13 09:22:25 +02005802/* This function is used to either dump tables states (when action is set
5803 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005804 * It returns 0 if the output buffer is full and it needs to be called
5805 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005806 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005807static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005808{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005809 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005810 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005811 struct ebmb_node *eb;
5812 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005813 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005814 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005815
5816 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005817 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005818 * - STAT_ST_INIT : the first call
5819 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005820 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005821 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005822 * and the entry pointer points to the next entry to be dumped,
5823 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005824 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005825 * data though.
5826 */
5827
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005828 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005829 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005830 if (appctx->st2 == STAT_ST_LIST) {
5831 appctx->ctx.table.entry->ref_cnt--;
5832 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005833 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005834 return 1;
5835 }
5836
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005837 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005838
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005839 while (appctx->st2 != STAT_ST_FIN) {
5840 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005841 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005842 appctx->ctx.table.proxy = appctx->ctx.table.target;
5843 if (!appctx->ctx.table.proxy)
5844 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005845
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005846 appctx->ctx.table.entry = NULL;
5847 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005848 break;
5849
Willy Tarreau295a8372011-03-10 11:25:07 +01005850 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005851 if (!appctx->ctx.table.proxy ||
5852 (appctx->ctx.table.target &&
5853 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5854 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005855 break;
5856 }
5857
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005858 if (appctx->ctx.table.proxy->table.size) {
5859 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5860 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005861 return 0;
5862
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005863 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005864 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005865 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005866 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005867 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005868 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5869 appctx->ctx.table.entry->ref_cnt++;
5870 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005871 break;
5872 }
5873 }
5874 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005875 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005876 break;
5877
Willy Tarreau295a8372011-03-10 11:25:07 +01005878 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005879 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005880
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005881 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005882 /* we're filtering on some data contents */
5883 void *ptr;
5884 long long data;
5885
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005886 dt = appctx->ctx.table.data_type;
5887 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5888 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005889 dt);
5890
5891 data = 0;
5892 switch (stktable_data_types[dt].std_type) {
5893 case STD_T_SINT:
5894 data = stktable_data_cast(ptr, std_t_sint);
5895 break;
5896 case STD_T_UINT:
5897 data = stktable_data_cast(ptr, std_t_uint);
5898 break;
5899 case STD_T_ULL:
5900 data = stktable_data_cast(ptr, std_t_ull);
5901 break;
5902 case STD_T_FRQP:
5903 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005904 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005905 break;
5906 }
5907
5908 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005909 if ((data < appctx->ctx.table.value &&
5910 (appctx->ctx.table.data_op == STD_OP_EQ ||
5911 appctx->ctx.table.data_op == STD_OP_GT ||
5912 appctx->ctx.table.data_op == STD_OP_GE)) ||
5913 (data == appctx->ctx.table.value &&
5914 (appctx->ctx.table.data_op == STD_OP_NE ||
5915 appctx->ctx.table.data_op == STD_OP_GT ||
5916 appctx->ctx.table.data_op == STD_OP_LT)) ||
5917 (data > appctx->ctx.table.value &&
5918 (appctx->ctx.table.data_op == STD_OP_EQ ||
5919 appctx->ctx.table.data_op == STD_OP_LT ||
5920 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005921 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005922 }
5923
Simon Hormanc88b8872011-06-15 15:18:49 +09005924 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005925 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5926 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005927 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005928
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005929 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005930
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005931 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005932 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005933 struct stksess *old = appctx->ctx.table.entry;
5934 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005935 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005936 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5937 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5938 stksess_kill(&appctx->ctx.table.proxy->table, old);
5939 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005940 break;
5941 }
5942
Simon Hormanc88b8872011-06-15 15:18:49 +09005943
5944 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005945 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5946 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5947 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005948
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005949 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5950 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005951 break;
5952
Willy Tarreau295a8372011-03-10 11:25:07 +01005953 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005954 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005955 break;
5956 }
5957 }
5958 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005959}
5960
Willy Tarreaud426a182010-03-05 14:58:26 +01005961/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005962 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5963 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5964 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5965 * lines are respected within the limit of 70 output chars. Lines that are
5966 * continuation of a previous truncated line begin with "+" instead of " "
5967 * after the offset. The new pointer is returned.
5968 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005969static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5970 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005971{
5972 int end;
5973 unsigned char c;
5974
5975 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005976 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005977 return ptr;
5978
Willy Tarreau77804732012-10-29 16:14:26 +01005979 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005980
Willy Tarreaud426a182010-03-05 14:58:26 +01005981 while (ptr < len && ptr < bsize) {
5982 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005983 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005984 if (out->len > end - 2)
5985 break;
5986 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005987 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005988 if (out->len > end - 3)
5989 break;
5990 out->str[out->len++] = '\\';
5991 switch (c) {
5992 case '\t': c = 't'; break;
5993 case '\n': c = 'n'; break;
5994 case '\r': c = 'r'; break;
5995 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005996 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005997 }
5998 out->str[out->len++] = c;
5999 } else {
6000 if (out->len > end - 5)
6001 break;
6002 out->str[out->len++] = '\\';
6003 out->str[out->len++] = 'x';
6004 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6005 out->str[out->len++] = hextab[c & 0xF];
6006 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006007 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006008 /* we had a line break, let's return now */
6009 out->str[out->len++] = '\n';
6010 *line = ptr;
6011 return ptr;
6012 }
6013 }
6014 /* we have an incomplete line, we return it as-is */
6015 out->str[out->len++] = '\n';
6016 return ptr;
6017}
6018
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006019/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006020 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006021 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006022 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006023static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006024{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006025 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006026 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006027
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006028 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006029 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006030
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006031 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006032
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006033 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006034 /* the function had not been called yet, let's prepare the
6035 * buffer for a response.
6036 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006037 struct tm tm;
6038
6039 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006040 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006041 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6042 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6043 error_snapshot_id);
6044
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006045 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006046 /* Socket buffer full. Let's try again later from the same point */
6047 return 0;
6048 }
6049
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006050 appctx->ctx.errors.px = proxy;
6051 appctx->ctx.errors.buf = 0;
6052 appctx->ctx.errors.bol = 0;
6053 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006054 }
6055
6056 /* we have two inner loops here, one for the proxy, the other one for
6057 * the buffer.
6058 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006059 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006060 struct error_snapshot *es;
6061
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006062 if (appctx->ctx.errors.buf == 0)
6063 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006064 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006065 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006066
6067 if (!es->when.tv_sec)
6068 goto next;
6069
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006070 if (appctx->ctx.errors.iid >= 0 &&
6071 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6072 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006073 goto next;
6074
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006075 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006076 /* just print headers now */
6077
6078 char pn[INET6_ADDRSTRLEN];
6079 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006080 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006081
6082 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006083 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006084 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006085 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006086
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006087 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6088 case AF_INET:
6089 case AF_INET6:
6090 port = get_host_port(&es->src);
6091 break;
6092 default:
6093 port = 0;
6094 }
6095
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006096 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006097 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006098 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006099 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006100 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006101 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006102 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6103 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006104 break;
6105 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006106 chunk_appendf(&trash,
Olivier Doucet815d7d52014-09-08 11:23:00 +02006107 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006108 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006109 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006110 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006111 break;
6112 }
6113
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006114 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006115 ", server %s (#%d), event #%u\n"
6116 " src %s:%d, session #%d, session flags 0x%08x\n"
6117 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6118 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6119 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6120 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6121 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6122 es->ev_id,
6123 pn, port, es->sid, es->s_flags,
6124 es->state, es->m_flags, es->t_flags,
6125 es->m_clen, es->m_blen,
6126 es->b_flags, es->b_out, es->b_tot,
6127 es->len, es->b_wrap, es->pos);
6128
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006129 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006130 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02006131 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006132 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006133 appctx->ctx.errors.ptr = 0;
6134 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006135 }
6136
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006137 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006138 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006139 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006140 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006141 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02006142 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006143 goto next;
6144 }
6145
6146 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006147 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006148 int newptr;
6149 int newline;
6150
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006151 newline = appctx->ctx.errors.bol;
6152 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6153 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006154 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006155
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006156 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006157 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02006158 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006159 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006160 appctx->ctx.errors.ptr = newptr;
6161 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006162 };
6163 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006164 appctx->ctx.errors.bol = 0;
6165 appctx->ctx.errors.ptr = -1;
6166 appctx->ctx.errors.buf++;
6167 if (appctx->ctx.errors.buf > 1) {
6168 appctx->ctx.errors.buf = 0;
6169 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006170 }
6171 }
6172
6173 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006174 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006175}
6176
Willy Tarreaud5781202012-09-22 19:32:35 +02006177/* parse the "level" argument on the bind lines */
6178static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6179{
6180 if (!*args[cur_arg + 1]) {
6181 memprintf(err, "'%s' : missing level", args[cur_arg]);
6182 return ERR_ALERT | ERR_FATAL;
6183 }
6184
6185 if (!strcmp(args[cur_arg+1], "user"))
6186 conf->level = ACCESS_LVL_USER;
6187 else if (!strcmp(args[cur_arg+1], "operator"))
6188 conf->level = ACCESS_LVL_OPER;
6189 else if (!strcmp(args[cur_arg+1], "admin"))
6190 conf->level = ACCESS_LVL_ADMIN;
6191 else {
6192 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6193 args[cur_arg], args[cur_arg+1]);
6194 return ERR_ALERT | ERR_FATAL;
6195 }
6196
6197 return 0;
6198}
6199
Willy Tarreaub24281b2011-02-13 13:16:36 +01006200struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006201 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006202 .name = "<STATS>", /* used for logging */
6203 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006204 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006205};
6206
Simon Horman9bd2c732011-06-15 15:18:44 +09006207static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006208 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006209 .name = "<CLI>", /* used for logging */
6210 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006211 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006212};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006213
Willy Tarreaudc13c112013-06-21 23:16:39 +02006214static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006215 { CFG_GLOBAL, "stats", stats_parse_global },
6216 { 0, NULL, NULL },
6217}};
6218
Willy Tarreaud5781202012-09-22 19:32:35 +02006219static struct bind_kw_list bind_kws = { "STAT", { }, {
6220 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6221 { NULL, NULL, 0 },
6222}};
6223
Willy Tarreau10522fd2008-07-09 20:12:41 +02006224__attribute__((constructor))
6225static void __dumpstats_module_init(void)
6226{
6227 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006228 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006229}
6230
Willy Tarreau91861262007-10-17 17:06:05 +02006231/*
6232 * Local variables:
6233 * c-indent-level: 8
6234 * c-basic-offset: 8
6235 * End:
6236 */