blob: d416ad006ef8791b1bad907b8be11a6f80497732 [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 Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <types/applet.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020042
43#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020044#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020045#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010046#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020047#include <proto/dumpstats.h>
48#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010049#include <proto/freq_ctr.h>
Willy Tarreau32b60d42015-03-13 16:14:57 +010050#include <proto/frontend.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020051#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010052#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010053#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020054#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010055#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010056#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020057#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020058#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010059#include <proto/sample.h>
Willy Tarreau9903f0e2015-04-04 18:50:31 +020060#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020061#include <proto/stream.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020062#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020063#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010064#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010065#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020066
Willy Tarreau7a0169a2012-11-19 17:13:16 +010067#ifdef USE_OPENSSL
68#include <proto/ssl_sock.h>
69#endif
70
Willy Tarreau91b843d2014-01-28 16:27:17 +010071/* stats socket states */
72enum {
73 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
74 STAT_CLI_END, /* final state, let's close */
75 STAT_CLI_GETREQ, /* wait for a request */
76 STAT_CLI_OUTPUT, /* all states after this one are responses */
77 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
78 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010079 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010080 STAT_CLI_O_INFO, /* dump info */
Willy Tarreau87b09662015-04-03 00:22:06 +020081 STAT_CLI_O_SESS, /* dump streams */
Willy Tarreau91b843d2014-01-28 16:27:17 +010082 STAT_CLI_O_ERR, /* dump errors */
83 STAT_CLI_O_TAB, /* dump tables */
84 STAT_CLI_O_CLR, /* clear tables */
85 STAT_CLI_O_SET, /* set entries in tables */
86 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010087 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
88 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010089 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010090 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010091};
92
Willy Tarreaued7df902014-05-22 18:04:49 +020093/* Actions available for the stats admin forms */
94enum {
95 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +020096
97 /* enable/disable health checks */
98 ST_ADM_ACTION_DHLTH,
99 ST_ADM_ACTION_EHLTH,
100
101 /* force health check status */
102 ST_ADM_ACTION_HRUNN,
103 ST_ADM_ACTION_HNOLB,
104 ST_ADM_ACTION_HDOWN,
105
106 /* enable/disable agent checks */
107 ST_ADM_ACTION_DAGENT,
108 ST_ADM_ACTION_EAGENT,
109
110 /* force agent check status */
111 ST_ADM_ACTION_ARUNN,
112 ST_ADM_ACTION_ADOWN,
113
114 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200115 ST_ADM_ACTION_READY,
116 ST_ADM_ACTION_DRAIN,
117 ST_ADM_ACTION_MAINT,
118 ST_ADM_ACTION_SHUTDOWN,
119 /* these are the ancient actions, still available for compatibility */
120 ST_ADM_ACTION_DISABLE,
121 ST_ADM_ACTION_ENABLE,
122 ST_ADM_ACTION_STOP,
123 ST_ADM_ACTION_START,
124};
125
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100126static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100127static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200128static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900129static int stats_dump_sess_to_buffer(struct stream_interface *si);
130static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100131static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100132static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
133static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100134static int stats_pats_list(struct stream_interface *si);
135static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100136static int stats_map_lookup(struct stream_interface *si);
Willy Tarreau00a37f02015-04-13 12:05:19 +0200137static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900138
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100139/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100140 * cli_io_handler()
141 * -> stats_dump_sess_to_buffer() // "show sess"
142 * -> stats_dump_errors_to_buffer() // "show errors"
143 * -> stats_dump_info_to_buffer() // "show info"
144 * -> stats_dump_stat_to_buffer() // "show stat"
145 * -> stats_dump_csv_header()
146 * -> stats_dump_proxy_to_buffer()
147 * -> stats_dump_fe_stats()
148 * -> stats_dump_li_stats()
149 * -> stats_dump_sv_stats()
150 * -> stats_dump_be_stats()
151 *
152 * http_stats_io_handler()
153 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
154 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
155 * -> stats_dump_html_head() // emits the HTML headers
156 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
157 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
158 * -> stats_dump_html_px_hdr()
159 * -> stats_dump_fe_stats()
160 * -> stats_dump_li_stats()
161 * -> stats_dump_sv_stats()
162 * -> stats_dump_be_stats()
163 * -> stats_dump_html_px_end()
164 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100165 */
166
Simon Horman9bd2c732011-06-15 15:18:44 +0900167static struct si_applet cli_applet;
168
169static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200170 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200171 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200172 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200173 " help : this message\n"
174 " prompt : toggle interactive mode with prompt\n"
175 " quit : disconnect\n"
176 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100177 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200178 " show stat : report counters for each proxy and server\n"
179 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100180 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200181 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200182 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200183 " set weight : change a server's weight\n"
Willy Tarreau2a4b70f2014-05-22 18:42:35 +0200184 " set server : change a server's state or weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200185 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100186 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200187 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200188 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200189 " disable : put a server or frontend in maintenance mode\n"
190 " enable : re-enable a server or frontend which is in maintenance mode\n"
191 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100192 " show acl [id] : report avalaible acls or dump an acl's contents\n"
193 " get acl : reports the patterns matching a sample for an ACL\n"
194 " add acl : add acl entry\n"
195 " del acl : delete acl entry\n"
196 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100197 " show map [id] : report avalaible maps or dump a map's contents\n"
198 " get map : reports the keys and values matching a sample for a map\n"
199 " set map : modify map entry\n"
200 " add map : add map entry\n"
201 " del map : delete map entry\n"
202 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200203 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200204 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200205
Simon Horman9bd2c732011-06-15 15:18:44 +0900206static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200207 "Permission denied\n"
208 "";
209
Willy Tarreau295a8372011-03-10 11:25:07 +0100210/* data transmission states for the stats responses */
211enum {
212 STAT_ST_INIT = 0,
213 STAT_ST_HEAD,
214 STAT_ST_INFO,
215 STAT_ST_LIST,
216 STAT_ST_END,
217 STAT_ST_FIN,
218};
219
220/* data transmission states for the stats responses inside a proxy */
221enum {
222 STAT_PX_ST_INIT = 0,
223 STAT_PX_ST_TH,
224 STAT_PX_ST_FE,
225 STAT_PX_ST_LI,
226 STAT_PX_ST_SV,
227 STAT_PX_ST_BE,
228 STAT_PX_ST_END,
229 STAT_PX_ST_FIN,
230};
231
Cyril Bonté19979e12012-04-04 12:57:21 +0200232extern const char *stat_status_codes[];
233
Willy Tarreau07e9e642010-08-17 21:48:17 +0200234/* allocate a new stats frontend named <name>, and return it
235 * (or NULL in case of lack of memory).
236 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200237static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200238{
239 struct proxy *fe;
240
241 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
242 if (!fe)
243 return NULL;
244
Willy Tarreau237250c2011-07-29 01:49:03 +0200245 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200246 fe->next = proxy;
247 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200248 fe->last_change = now.tv_sec;
249 fe->id = strdup("GLOBAL");
250 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200251 fe->maxconn = 10; /* default to 10 concurrent connections */
252 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200253 fe->conf.file = strdup(file);
254 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100255 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100256 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200257
258 /* the stats frontend is the only one able to assign ID #0 */
259 fe->conf.id.key = fe->uuid = 0;
260 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200261 return fe;
262}
263
Willy Tarreaufbee7132007-10-18 13:53:22 +0200264/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200265 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
266 * error message into the <err> buffer which will be preallocated. The trailing
267 * '\n' must not be written. The function must be called with <args> pointing to
268 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200269 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200270static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200271 struct proxy *defpx, const char *file, int line,
272 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200273{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200274 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200275 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200276
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200277 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278 int cur_arg;
279
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200280 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200281 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200282 return -1;
283 }
284
Willy Tarreau89a63132009-08-16 17:41:45 +0200285 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200286 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200288 return -1;
289 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200290 }
291
Willy Tarreau4348fad2012-09-20 16:48:07 +0200292 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200293 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200294
Willy Tarreauc53d4222012-09-20 20:19:28 +0200295 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
296 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
297 file, line, args[0], args[1], err && *err ? *err : "error");
298 return -1;
299 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200300
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200301 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200302 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200303 static int bind_dumped;
304 struct bind_kw *kw;
305
306 kw = bind_find_kw(args[cur_arg]);
307 if (kw) {
308 if (!kw->parse) {
309 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
310 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200311 return -1;
312 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200313
314 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
315 if (err && *err)
316 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
317 else
318 memprintf(err, "'%s %s' : error encountered while processing '%s'",
319 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200320 return -1;
321 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200322
323 cur_arg += 1 + kw->skip;
324 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200325 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200326
327 if (!bind_dumped) {
328 bind_dump_kws(err);
329 indent_msg(err, 4);
330 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200331 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200332
333 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
334 args[0], args[1], args[cur_arg],
335 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
336 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200337 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100338
Willy Tarreauc53d4222012-09-20 20:19:28 +0200339 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
340 l->maxconn = global.stats_fe->maxconn;
341 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200342 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200343 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100344 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200345 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
346 l->nice = -64; /* we want to boost priority for local stats */
347 global.maxsock += l->maxconn;
348 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200349 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200350 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100351 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200352 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100353
354 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200355 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100356 return -1;
357 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200358
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100359 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200360 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200361 return -1;
362 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200363 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200364 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200365 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200366 return -1;
367 }
368 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200369 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200370 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200371 else if (!strcmp(args[1], "maxconn")) {
372 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200373
374 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200375 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200376 return -1;
377 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200378
379 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200380 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200381 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200382 return -1;
383 }
384 }
385 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200386 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200387 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
388 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100389 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200390
Willy Tarreau91319572013-04-20 09:48:50 +0200391 if (!global.stats_fe) {
392 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
393 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
394 return -1;
395 }
396 }
397
Willy Tarreau35b7b162012-10-22 23:17:18 +0200398 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100399 unsigned int low, high;
400
Willy Tarreau35b7b162012-10-22 23:17:18 +0200401 if (strcmp(args[cur_arg], "all") == 0) {
402 set = 0;
403 break;
404 }
405 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100406 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200407 }
408 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100409 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200410 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100411 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100412 char *dash = strchr(args[cur_arg], '-');
413
414 low = high = str2uic(args[cur_arg]);
415 if (dash)
416 high = str2uic(dash + 1);
417
418 if (high < low) {
419 unsigned int swap = low;
420 low = high;
421 high = swap;
422 }
423
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100424 if (low < 1 || high > LONGBITS) {
425 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
426 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200427 return -1;
428 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100429 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100430 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100431 }
432 else {
433 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100434 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
435 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100436 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200437 }
438 cur_arg++;
439 }
440 global.stats_fe->bind_proc = set;
441 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200442 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200443 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200444 return -1;
445 }
446 return 0;
447}
448
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100449/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
450 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100451 * NOTE: Some tools happen to rely on the field position instead of its name,
452 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100453 */
454static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100455{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100456 chunk_appendf(&trash,
457 "# pxname,svname,"
458 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100459 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100460 "bin,bout,"
461 "dreq,dresp,"
462 "ereq,econ,eresp,"
463 "wretr,wredis,"
464 "status,weight,act,bck,"
465 "chkfail,chkdown,lastchg,downtime,qlimit,"
466 "pid,iid,sid,throttle,lbtot,tracked,type,"
467 "rate,rate_lim,rate_max,"
468 "check_status,check_code,check_duration,"
469 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
470 "req_rate,req_rate_max,req_tot,"
471 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200472 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100473 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100474}
475
Simon Hormand9366582011-06-15 15:18:45 +0900476/* print a string of text buffer to <out>. The format is :
477 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
478 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
479 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
480 */
481static int dump_text(struct chunk *out, const char *buf, int bsize)
482{
483 unsigned char c;
484 int ptr = 0;
485
486 while (buf[ptr] && ptr < bsize) {
487 c = buf[ptr];
488 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
489 if (out->len > out->size - 1)
490 break;
491 out->str[out->len++] = c;
492 }
493 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
494 if (out->len > out->size - 2)
495 break;
496 out->str[out->len++] = '\\';
497 switch (c) {
498 case ' ': c = ' '; break;
499 case '\t': c = 't'; break;
500 case '\n': c = 'n'; break;
501 case '\r': c = 'r'; break;
502 case '\e': c = 'e'; break;
503 case '\\': c = '\\'; break;
504 }
505 out->str[out->len++] = c;
506 }
507 else {
508 if (out->len > out->size - 4)
509 break;
510 out->str[out->len++] = '\\';
511 out->str[out->len++] = 'x';
512 out->str[out->len++] = hextab[(c >> 4) & 0xF];
513 out->str[out->len++] = hextab[c & 0xF];
514 }
515 ptr++;
516 }
517
518 return ptr;
519}
520
521/* print a buffer in hexa.
522 * Print stopped if <bsize> is reached, or if no more place in the chunk.
523 */
524static int dump_binary(struct chunk *out, const char *buf, int bsize)
525{
526 unsigned char c;
527 int ptr = 0;
528
529 while (ptr < bsize) {
530 c = buf[ptr];
531
532 if (out->len > out->size - 2)
533 break;
534 out->str[out->len++] = hextab[(c >> 4) & 0xF];
535 out->str[out->len++] = hextab[c & 0xF];
536
537 ptr++;
538 }
539 return ptr;
540}
541
542/* Dump the status of a table to a stream interface's
543 * read buffer. It returns 0 if the output buffer is full
544 * and needs to be called again, otherwise non-zero.
545 */
546static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
547 struct proxy *proxy, struct proxy *target)
548{
Willy Tarreau87b09662015-04-03 00:22:06 +0200549 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900550
Willy Tarreau77804732012-10-29 16:14:26 +0100551 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900552 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
553
554 /* any other information should be dumped here */
555
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200556 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100557 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900558
Willy Tarreaubc18da12015-03-13 14:00:47 +0100559 if (bi_putchk(si_ic(si), msg) == -1) {
560 si->flags |= SI_FL_WAIT_ROOM;
Simon Hormand9366582011-06-15 15:18:45 +0900561 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100562 }
Simon Hormand9366582011-06-15 15:18:45 +0900563
564 return 1;
565}
566
567/* Dump the a table entry to a stream interface's
568 * read buffer. It returns 0 if the output buffer is full
569 * and needs to be called again, otherwise non-zero.
570 */
571static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
572 struct proxy *proxy, struct stksess *entry)
573{
574 int dt;
575
Willy Tarreau77804732012-10-29 16:14:26 +0100576 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900577
578 if (proxy->table.type == STKTABLE_TYPE_IP) {
579 char addr[INET_ADDRSTRLEN];
580 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100581 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900582 }
583 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
584 char addr[INET6_ADDRSTRLEN];
585 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100586 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900587 }
588 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100589 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900590 }
591 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100592 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900593 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
594 }
595 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100596 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900597 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
598 }
599
Willy Tarreau77804732012-10-29 16:14:26 +0100600 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900601
602 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
603 void *ptr;
604
605 if (proxy->table.data_ofs[dt] == 0)
606 continue;
607 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100608 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900609 else
Willy Tarreau77804732012-10-29 16:14:26 +0100610 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900611
612 ptr = stktable_data_ptr(&proxy->table, entry, dt);
613 switch (stktable_data_types[dt].std_type) {
614 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100615 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900616 break;
617 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100618 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900619 break;
620 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100621 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900622 break;
623 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100624 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900625 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
626 proxy->table.data_arg[dt].u));
627 break;
628 }
629 }
Willy Tarreau77804732012-10-29 16:14:26 +0100630 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900631
Willy Tarreaubc18da12015-03-13 14:00:47 +0100632 if (bi_putchk(si_ic(si), msg) == -1) {
633 si->flags |= SI_FL_WAIT_ROOM;
Simon Hormand9366582011-06-15 15:18:45 +0900634 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100635 }
Simon Hormand9366582011-06-15 15:18:45 +0900636
637 return 1;
638}
639
Willy Tarreaudec98142012-06-06 23:37:08 +0200640static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900641{
Willy Tarreau87b09662015-04-03 00:22:06 +0200642 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100643 struct appctx *appctx = __objt_appctx(si->end);
644 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900645 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900646 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900647 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200648 long long value;
649 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200650 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200651 void *ptr;
652 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900653
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100654 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900655
656 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100657 appctx->ctx.cli.msg = "Key value expected\n";
658 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900659 return;
660 }
661
Simon Hormanc5b89f62011-06-15 15:18:50 +0900662 switch (px->table.type) {
663 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900664 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100665 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900666 break;
667 case STKTABLE_TYPE_IPV6:
668 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100669 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900670 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900671 case STKTABLE_TYPE_INTEGER:
672 {
673 char *endptr;
674 unsigned long val;
675 errno = 0;
676 val = strtoul(args[4], &endptr, 10);
677 if ((errno == ERANGE && val == ULONG_MAX) ||
678 (errno != 0 && val == 0) || endptr == args[4] ||
679 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100680 appctx->ctx.cli.msg = "Invalid key\n";
681 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900682 return;
683 }
684 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100685 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900686 break;
687 }
688 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900689 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100690 static_table_key->key = args[4];
691 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900692 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900693 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200694 switch (action) {
695 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100696 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 +0200697 break;
698 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100699 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 +0200700 break;
701 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100702 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200703 break;
704 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100705 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900706 return;
707 }
708
709 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200710 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100711 appctx->ctx.cli.msg = stats_permission_denied_msg;
712 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900713 return;
714 }
715
Willy Tarreau07115412012-10-29 21:56:59 +0100716 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900717
Willy Tarreaudec98142012-06-06 23:37:08 +0200718 switch (action) {
719 case STAT_CLI_O_TAB:
720 if (!ts)
721 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100722 chunk_reset(&trash);
723 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900724 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100725 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900726 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200727
728 case STAT_CLI_O_CLR:
729 if (!ts)
730 return;
731 if (ts->ref_cnt) {
732 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100733 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
734 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200735 return;
736 }
737 stksess_kill(&px->table, ts);
738 break;
Simon Horman17bce342011-06-15 15:18:47 +0900739
Willy Tarreau654694e2012-06-07 01:03:16 +0200740 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200741 if (ts)
742 stktable_touch(&px->table, ts, 1);
743 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100744 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200745 if (!ts) {
746 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100747 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
748 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200749 return;
750 }
751 stktable_store(&px->table, ts, 1);
752 }
753
Willy Tarreau47060b62013-08-01 21:11:42 +0200754 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
755 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100756 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
757 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200758 return;
759 }
760
761 data_type = stktable_get_data_type(args[cur_arg] + 5);
762 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100763 appctx->ctx.cli.msg = "Unknown data type\n";
764 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200765 return;
766 }
767
768 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100769 appctx->ctx.cli.msg = "Data type not stored in this table\n";
770 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200771 return;
772 }
773
774 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100775 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
776 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200777 return;
778 }
779
780 ptr = stktable_data_ptr(&px->table, ts, data_type);
781
782 switch (stktable_data_types[data_type].std_type) {
783 case STD_T_SINT:
784 stktable_data_cast(ptr, std_t_sint) = value;
785 break;
786 case STD_T_UINT:
787 stktable_data_cast(ptr, std_t_uint) = value;
788 break;
789 case STD_T_ULL:
790 stktable_data_cast(ptr, std_t_ull) = value;
791 break;
792 case STD_T_FRQP:
793 /* We set both the current and previous values. That way
794 * the reported frequency is stable during all the period
795 * then slowly fades out. This allows external tools to
796 * push measures without having to update them too often.
797 */
798 frqp = &stktable_data_cast(ptr, std_t_frqp);
799 frqp->curr_tick = now_ms;
800 frqp->prev_ctr = 0;
801 frqp->curr_ctr = value;
802 break;
803 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200804 }
805 break;
806
Willy Tarreaudec98142012-06-06 23:37:08 +0200807 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100808 appctx->ctx.cli.msg = "Unknown action\n";
809 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200810 break;
Simon Horman121f3052011-06-15 15:18:46 +0900811 }
Simon Horman121f3052011-06-15 15:18:46 +0900812}
813
Willy Tarreau654694e2012-06-07 01:03:16 +0200814static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900815{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100816 struct appctx *appctx = __objt_appctx(si->end);
817
Willy Tarreau04b3a192013-04-13 09:41:37 +0200818 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100819 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
820 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200821 return;
822 }
823
Simon Hormand5b9fd92011-06-15 15:18:48 +0900824 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100825 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
826 if (appctx->ctx.table.data_type < 0) {
827 appctx->ctx.cli.msg = "Unknown data type\n";
828 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900829 return;
830 }
831
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100832 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
833 appctx->ctx.cli.msg = "Data type not stored in this table\n";
834 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900835 return;
836 }
837
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100838 appctx->ctx.table.data_op = get_std_op(args[4]);
839 if (appctx->ctx.table.data_op < 0) {
840 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
841 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900842 return;
843 }
844
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100845 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
846 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
847 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900848 return;
849 }
850}
851
Willy Tarreaudec98142012-06-06 23:37:08 +0200852static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900853{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100854 struct appctx *appctx = __objt_appctx(si->end);
855
856 appctx->ctx.table.data_type = -1;
857 appctx->st2 = STAT_ST_INIT;
858 appctx->ctx.table.target = NULL;
859 appctx->ctx.table.proxy = NULL;
860 appctx->ctx.table.entry = NULL;
861 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900862
863 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100864 appctx->ctx.table.target = find_stktable(args[2]);
865 if (!appctx->ctx.table.target) {
866 appctx->ctx.cli.msg = "No such table\n";
867 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900868 return;
869 }
870 }
871 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200872 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900873 goto err_args;
874 return;
875 }
876
877 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200878 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900879 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200880 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900881 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900882 goto err_args;
883
884 return;
885
886err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200887 switch (action) {
888 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100889 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 +0200890 break;
891 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100892 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 +0200893 break;
894 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100895 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200896 break;
897 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100898 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900899}
900
Willy Tarreau532a4502011-09-07 22:37:44 +0200901/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +0200902 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +0200903 * level to be admin.
904 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200905static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +0200906{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100907 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200908 struct proxy *px;
909
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200910 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100911 appctx->ctx.cli.msg = stats_permission_denied_msg;
912 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200913 return NULL;
914 }
915
916 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100917 appctx->ctx.cli.msg = "A frontend name is expected.\n";
918 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200919 return NULL;
920 }
921
922 px = findproxy(arg, PR_CAP_FE);
923 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100924 appctx->ctx.cli.msg = "No such frontend.\n";
925 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200926 return NULL;
927 }
928 return px;
929}
930
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200931/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
932 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +0200933 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200934 * the <arg> is modified to remove the '/'.
935 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200936static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200937{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100938 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200939 struct proxy *px;
940 struct server *sv;
941 char *line;
942
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200943 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100944 appctx->ctx.cli.msg = stats_permission_denied_msg;
945 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200946 return NULL;
947 }
948
949 /* split "backend/server" and make <line> point to server */
950 for (line = arg; *line; line++)
951 if (*line == '/') {
952 *line++ = '\0';
953 break;
954 }
955
956 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100957 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
958 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200959 return NULL;
960 }
961
962 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100963 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
964 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200965 return NULL;
966 }
967
968 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100969 appctx->ctx.cli.msg = "Proxy is disabled.\n";
970 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200971 return NULL;
972 }
973
974 return sv;
975}
976
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100977/* This function is used with map and acl management. It permits to browse
978 * each reference. The variable <getnext> must contain the current node,
979 * <end> point to the root node and the <flags> permit to filter required
980 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100981 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100982static inline
983struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
984 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100985{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100986 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100987
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100988 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100989
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100990 /* Get next list entry. */
991 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100992
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100993 /* If the entry is the last of the list, return NULL. */
994 if (&ref->list == end)
995 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100996
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100997 /* If the entry match the flag, return it. */
998 if (ref->flags & flags)
999 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001000 }
1001}
1002
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001003static inline
1004struct pat_ref *pat_ref_lookup_ref(const char *reference)
1005{
1006 int id;
1007 char *error;
1008
1009 /* If the reference starts by a '#', this is numeric id. */
1010 if (reference[0] == '#') {
1011 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1012 id = strtol(reference + 1, &error, 10);
1013 if (*error != '\0')
1014 return NULL;
1015
1016 /* Perform the unique id lookup. */
1017 return pat_ref_lookupid(id);
1018 }
1019
1020 /* Perform the string lookup. */
1021 return pat_ref_lookup(reference);
1022}
1023
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001024/* This function is used with map and acl management. It permits to browse
1025 * each reference.
1026 */
1027static inline
1028struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1029{
1030 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001031 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1032 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001033 return NULL;
1034 return expr;
1035}
1036
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001037/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001038 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001039 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001040 * designating the function which will have to process the request, which can
1041 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001042 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001043static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001044{
Willy Tarreau87b09662015-04-03 00:22:06 +02001045 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001046 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001047 char *args[MAX_STATS_ARGS + 1];
1048 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001049 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001050
1051 while (isspace((unsigned char)*line))
1052 line++;
1053
1054 arg = 0;
1055 args[arg] = line;
1056
1057 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001058 if (*line == '\\') {
1059 line++;
1060 if (*line == '\0')
1061 break;
1062 }
1063 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001064 *line++ = '\0';
1065
1066 while (isspace((unsigned char)*line))
1067 line++;
1068
1069 args[++arg] = line;
1070 continue;
1071 }
1072
1073 line++;
1074 }
1075
1076 while (++arg <= MAX_STATS_ARGS)
1077 args[arg] = line;
1078
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001079 /* remove \ */
1080 arg = 0;
1081 while (*args[arg] != '\0') {
1082 j = 0;
1083 for (i=0; args[arg][i] != '\0'; i++) {
1084 if (args[arg][i] == '\\')
1085 continue;
1086 args[arg][j] = args[arg][i];
1087 j++;
1088 }
1089 args[arg][j] = '\0';
1090 arg++;
1091 }
1092
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001093 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001094 if (strcmp(args[0], "show") == 0) {
1095 if (strcmp(args[1], "stat") == 0) {
1096 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001097 appctx->ctx.stats.flags |= STAT_BOUND;
1098 appctx->ctx.stats.iid = atoi(args[2]);
1099 appctx->ctx.stats.type = atoi(args[3]);
1100 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001101 }
1102
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001103 appctx->st2 = STAT_ST_INIT;
1104 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001105 }
1106 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001107 appctx->st2 = STAT_ST_INIT;
1108 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001109 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001110 else if (strcmp(args[1], "pools") == 0) {
1111 appctx->st2 = STAT_ST_INIT;
1112 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1113 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001114 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001115 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001116 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001117 appctx->ctx.cli.msg = stats_permission_denied_msg;
1118 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001119 return 1;
1120 }
Willy Tarreau76153662012-11-26 01:16:39 +01001121 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001122 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001123 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001124 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001125 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001126 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001127 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001128 appctx->ctx.sess.pos = 0;
1129 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001130 }
1131 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001132 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001133 appctx->ctx.cli.msg = stats_permission_denied_msg;
1134 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001135 return 1;
1136 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001137 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001138 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001139 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001140 appctx->ctx.errors.iid = -1;
1141 appctx->ctx.errors.px = NULL;
1142 appctx->st2 = STAT_ST_INIT;
1143 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001144 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001145 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001146 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001147 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001148 else if (strcmp(args[1], "map") == 0 ||
1149 strcmp(args[1], "acl") == 0) {
1150
1151 /* Set ACL or MAP flags. */
1152 if (args[1][0] == 'm')
1153 appctx->ctx.map.display_flags = PAT_REF_MAP;
1154 else
1155 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001156
1157 /* no parameter: display all map avalaible */
1158 if (!*args[2]) {
1159 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001160 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001161 return 1;
1162 }
1163
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001164 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001165 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001166 if (!appctx->ctx.map.ref ||
1167 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1168 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001169 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001170 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001171 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001172 appctx->st0 = STAT_CLI_PRINT;
1173 return 1;
1174 }
1175 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001176 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001177 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001178 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001179 return 0;
1180 }
1181 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001182 else if (strcmp(args[0], "clear") == 0) {
1183 if (strcmp(args[1], "counters") == 0) {
1184 struct proxy *px;
1185 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001186 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001187 int clrall = 0;
1188
1189 if (strcmp(args[2], "all") == 0)
1190 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001191
Willy Tarreau6162db22009-10-10 17:13:00 +02001192 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001193 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1194 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001195 appctx->ctx.cli.msg = stats_permission_denied_msg;
1196 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001197 return 1;
1198 }
1199
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001200 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001201 if (clrall) {
1202 memset(&px->be_counters, 0, sizeof(px->be_counters));
1203 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1204 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001205 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001206 px->be_counters.conn_max = 0;
1207 px->be_counters.p.http.rps_max = 0;
1208 px->be_counters.sps_max = 0;
1209 px->be_counters.cps_max = 0;
1210 px->be_counters.nbpend_max = 0;
1211
1212 px->fe_counters.conn_max = 0;
1213 px->fe_counters.p.http.rps_max = 0;
1214 px->fe_counters.sps_max = 0;
1215 px->fe_counters.cps_max = 0;
1216 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001217 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001218
1219 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001220 if (clrall)
1221 memset(&sv->counters, 0, sizeof(sv->counters));
1222 else {
1223 sv->counters.cur_sess_max = 0;
1224 sv->counters.nbpend_max = 0;
1225 sv->counters.sps_max = 0;
1226 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001227
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001228 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001229 if (li->counters) {
1230 if (clrall)
1231 memset(li->counters, 0, sizeof(*li->counters));
1232 else
1233 li->counters->conn_max = 0;
1234 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001235 }
1236
Willy Tarreau81c25d02011-09-07 15:17:21 +02001237 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001238 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001239 return 1;
1240 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001241 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001242 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001243 /* end of processing */
1244 return 1;
1245 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001246 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1247 /* Set ACL or MAP flags. */
1248 if (args[1][0] == 'm')
1249 appctx->ctx.map.display_flags = PAT_REF_MAP;
1250 else
1251 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001252
1253 /* no parameter */
1254 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001255 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1256 appctx->ctx.cli.msg = "Missing map identifier.\n";
1257 else
1258 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001259 appctx->st0 = STAT_CLI_PRINT;
1260 return 1;
1261 }
1262
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001263 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001264 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001265 if (!appctx->ctx.map.ref ||
1266 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1267 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001268 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001269 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001270 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001271 appctx->st0 = STAT_CLI_PRINT;
1272 return 1;
1273 }
1274
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001275 /* Clear all. */
1276 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001277
1278 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001279 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001280 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001281 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001282 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001283 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001284 return 0;
1285 }
1286 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001287 else if (strcmp(args[0], "get") == 0) {
1288 if (strcmp(args[1], "weight") == 0) {
1289 struct proxy *px;
1290 struct server *sv;
1291
1292 /* split "backend/server" and make <line> point to server */
1293 for (line = args[2]; *line; line++)
1294 if (*line == '/') {
1295 *line++ = '\0';
1296 break;
1297 }
1298
1299 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001300 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1301 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001302 return 1;
1303 }
1304
1305 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001306 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1307 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001308 return 1;
1309 }
1310
1311 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001312 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001313 if (bi_putstr(si_ic(si), trash.str) == -1)
1314 si->flags |= SI_FL_WAIT_ROOM;
1315
Willy Tarreau38338fa2009-10-10 18:37:29 +02001316 return 1;
1317 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001318 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1319 /* Set flags. */
1320 if (args[1][0] == 'm')
1321 appctx->ctx.map.display_flags = PAT_REF_MAP;
1322 else
1323 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001324
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001325 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001326 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001327 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1328 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1329 else
1330 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001331 appctx->st0 = STAT_CLI_PRINT;
1332 return 1;
1333 }
1334
1335 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001336 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001337 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001338 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001339 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001340 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001341 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001342 appctx->st0 = STAT_CLI_PRINT;
1343 return 1;
1344 }
1345
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001346 /* copy input string. The string must be allocated because
1347 * it may be used over multiple iterations. It's released
1348 * at the end and upon abort anyway.
1349 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001350 appctx->ctx.map.chunk.len = strlen(args[3]);
1351 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1352 appctx->ctx.map.chunk.str = strdup(args[3]);
1353 if (!appctx->ctx.map.chunk.str) {
1354 appctx->ctx.cli.msg = "Out of memory error.\n";
1355 appctx->st0 = STAT_CLI_PRINT;
1356 return 1;
1357 }
1358
1359 /* prepare response */
1360 appctx->st2 = STAT_ST_INIT;
1361 appctx->st0 = STAT_CLI_O_MLOOK;
1362 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001363 else { /* not "get weight" */
1364 return 0;
1365 }
1366 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001367 else if (strcmp(args[0], "set") == 0) {
1368 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001369 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001370 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001371
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001372 sv = expect_server_admin(s, si, args[2]);
1373 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001374 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001375
Simon Horman7d09b9a2013-02-12 10:45:51 +09001376 warning = server_parse_weight_change_request(sv, args[3]);
1377 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001378 appctx->ctx.cli.msg = warning;
1379 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001380 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001381 return 1;
1382 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001383 else if (strcmp(args[1], "server") == 0) {
1384 struct server *sv;
1385 const char *warning;
1386
1387 sv = expect_server_admin(s, si, args[2]);
1388 if (!sv)
1389 return 1;
1390
1391 if (strcmp(args[3], "weight") == 0) {
1392 warning = server_parse_weight_change_request(sv, args[4]);
1393 if (warning) {
1394 appctx->ctx.cli.msg = warning;
1395 appctx->st0 = STAT_CLI_PRINT;
1396 }
1397 }
1398 else if (strcmp(args[3], "state") == 0) {
1399 if (strcmp(args[4], "ready") == 0)
1400 srv_adm_set_ready(sv);
1401 else if (strcmp(args[4], "drain") == 0)
1402 srv_adm_set_drain(sv);
1403 else if (strcmp(args[4], "maint") == 0)
1404 srv_adm_set_maint(sv);
1405 else {
1406 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1407 appctx->st0 = STAT_CLI_PRINT;
1408 }
1409 }
1410 else if (strcmp(args[3], "health") == 0) {
1411 if (sv->track) {
1412 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1413 appctx->st0 = STAT_CLI_PRINT;
1414 }
1415 else if (strcmp(args[4], "up") == 0) {
1416 sv->check.health = sv->check.rise + sv->check.fall - 1;
1417 srv_set_running(sv, "changed from CLI");
1418 }
1419 else if (strcmp(args[4], "stopping") == 0) {
1420 sv->check.health = sv->check.rise + sv->check.fall - 1;
1421 srv_set_stopping(sv, "changed from CLI");
1422 }
1423 else if (strcmp(args[4], "down") == 0) {
1424 sv->check.health = 0;
1425 srv_set_stopped(sv, "changed from CLI");
1426 }
1427 else {
1428 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1429 appctx->st0 = STAT_CLI_PRINT;
1430 }
1431 }
1432 else if (strcmp(args[3], "agent") == 0) {
1433 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1434 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1435 appctx->st0 = STAT_CLI_PRINT;
1436 }
1437 else if (strcmp(args[4], "up") == 0) {
1438 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1439 srv_set_running(sv, "changed from CLI");
1440 }
1441 else if (strcmp(args[4], "down") == 0) {
1442 sv->agent.health = 0;
1443 srv_set_stopped(sv, "changed from CLI");
1444 }
1445 else {
1446 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1447 appctx->st0 = STAT_CLI_PRINT;
1448 }
1449 }
1450 else {
1451 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1452 appctx->st0 = STAT_CLI_PRINT;
1453 }
1454 return 1;
1455 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001456 else if (strcmp(args[1], "timeout") == 0) {
1457 if (strcmp(args[2], "cli") == 0) {
1458 unsigned timeout;
1459 const char *res;
1460
1461 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001462 appctx->ctx.cli.msg = "Expects an integer value.\n";
1463 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001464 return 1;
1465 }
1466
1467 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1468 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001469 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1470 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001471 return 1;
1472 }
1473
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001474 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001475 return 1;
1476 }
1477 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001478 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1479 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001480 return 1;
1481 }
1482 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001483 else if (strcmp(args[1], "maxconn") == 0) {
1484 if (strcmp(args[2], "frontend") == 0) {
1485 struct proxy *px;
1486 struct listener *l;
1487 int v;
1488
Willy Tarreau532a4502011-09-07 22:37:44 +02001489 px = expect_frontend_admin(s, si, args[3]);
1490 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001491 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001492
1493 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001494 appctx->ctx.cli.msg = "Integer value expected.\n";
1495 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001496 return 1;
1497 }
1498
1499 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001500 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001501 appctx->ctx.cli.msg = "Value out of range.\n";
1502 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001503 return 1;
1504 }
1505
1506 /* OK, the value is fine, so we assign it to the proxy and to all of
1507 * its listeners. The blocked ones will be dequeued.
1508 */
1509 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001510 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001511 l->maxconn = v;
1512 if (l->state == LI_FULL)
1513 resume_listener(l);
1514 }
1515
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001516 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1517 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001518
1519 return 1;
1520 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001521 else if (strcmp(args[2], "global") == 0) {
1522 int v;
1523
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001524 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001525 appctx->ctx.cli.msg = stats_permission_denied_msg;
1526 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001527 return 1;
1528 }
1529
1530 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001531 appctx->ctx.cli.msg = "Expects an integer value.\n";
1532 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001533 return 1;
1534 }
1535
1536 v = atoi(args[3]);
1537 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001538 appctx->ctx.cli.msg = "Value out of range.\n";
1539 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001540 return 1;
1541 }
1542
1543 /* check for unlimited values */
1544 if (v <= 0)
1545 v = global.hardmaxconn;
1546
1547 global.maxconn = v;
1548
1549 /* Dequeues all of the listeners waiting for a resource */
1550 if (!LIST_ISEMPTY(&global_listener_queue))
1551 dequeue_all_listeners(&global_listener_queue);
1552
1553 return 1;
1554 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001555 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001556 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1557 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001558 return 1;
1559 }
1560 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001561 else if (strcmp(args[1], "rate-limit") == 0) {
1562 if (strcmp(args[2], "connections") == 0) {
1563 if (strcmp(args[3], "global") == 0) {
1564 int v;
1565
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001566 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001567 appctx->ctx.cli.msg = stats_permission_denied_msg;
1568 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001569 return 1;
1570 }
1571
1572 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001573 appctx->ctx.cli.msg = "Expects an integer value.\n";
1574 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001575 return 1;
1576 }
1577
1578 v = atoi(args[4]);
1579 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001580 appctx->ctx.cli.msg = "Value out of range.\n";
1581 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001582 return 1;
1583 }
1584
1585 global.cps_lim = v;
1586
1587 /* Dequeues all of the listeners waiting for a resource */
1588 if (!LIST_ISEMPTY(&global_listener_queue))
1589 dequeue_all_listeners(&global_listener_queue);
1590
1591 return 1;
1592 }
1593 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001594 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1595 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001596 return 1;
1597 }
1598 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001599 else if (strcmp(args[2], "sessions") == 0) {
1600 if (strcmp(args[3], "global") == 0) {
1601 int v;
1602
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001603 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001604 appctx->ctx.cli.msg = stats_permission_denied_msg;
1605 appctx->st0 = STAT_CLI_PRINT;
1606 return 1;
1607 }
1608
1609 if (!*args[4]) {
1610 appctx->ctx.cli.msg = "Expects an integer value.\n";
1611 appctx->st0 = STAT_CLI_PRINT;
1612 return 1;
1613 }
1614
1615 v = atoi(args[4]);
1616 if (v < 0) {
1617 appctx->ctx.cli.msg = "Value out of range.\n";
1618 appctx->st0 = STAT_CLI_PRINT;
1619 return 1;
1620 }
1621
1622 global.sps_lim = v;
1623
1624 /* Dequeues all of the listeners waiting for a resource */
1625 if (!LIST_ISEMPTY(&global_listener_queue))
1626 dequeue_all_listeners(&global_listener_queue);
1627
1628 return 1;
1629 }
1630 else {
1631 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1632 appctx->st0 = STAT_CLI_PRINT;
1633 return 1;
1634 }
1635 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001636#ifdef USE_OPENSSL
1637 else if (strcmp(args[2], "ssl-sessions") == 0) {
1638 if (strcmp(args[3], "global") == 0) {
1639 int v;
1640
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001641 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001642 appctx->ctx.cli.msg = stats_permission_denied_msg;
1643 appctx->st0 = STAT_CLI_PRINT;
1644 return 1;
1645 }
1646
1647 if (!*args[4]) {
1648 appctx->ctx.cli.msg = "Expects an integer value.\n";
1649 appctx->st0 = STAT_CLI_PRINT;
1650 return 1;
1651 }
1652
1653 v = atoi(args[4]);
1654 if (v < 0) {
1655 appctx->ctx.cli.msg = "Value out of range.\n";
1656 appctx->st0 = STAT_CLI_PRINT;
1657 return 1;
1658 }
1659
1660 global.ssl_lim = v;
1661
1662 /* Dequeues all of the listeners waiting for a resource */
1663 if (!LIST_ISEMPTY(&global_listener_queue))
1664 dequeue_all_listeners(&global_listener_queue);
1665
1666 return 1;
1667 }
1668 else {
1669 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1670 appctx->st0 = STAT_CLI_PRINT;
1671 return 1;
1672 }
1673 }
1674#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001675 else if (strcmp(args[2], "http-compression") == 0) {
1676 if (strcmp(args[3], "global") == 0) {
1677 int v;
1678
Willy Tarreau85d47f92012-11-21 00:29:50 +01001679 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001680 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1681 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001682 return 1;
1683 }
1684
William Lallemandd85f9172012-11-09 17:05:39 +01001685 v = atoi(args[4]);
1686 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1687 }
1688 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001689 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1690 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001691 return 1;
1692 }
1693 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001694 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001695 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001696 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001697 return 1;
1698 }
1699 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001700 else if (strcmp(args[1], "table") == 0) {
1701 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1702 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001703 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001704 char *err;
1705
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001706 /* Set flags. */
1707 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001708
1709 /* Expect three parameters: map name, key and new value. */
1710 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001711 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001712 appctx->st0 = STAT_CLI_PRINT;
1713 return 1;
1714 }
1715
1716 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001717 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001718 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001719 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001720 appctx->st0 = STAT_CLI_PRINT;
1721 return 1;
1722 }
1723
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001724 /* If the entry identifier start with a '#', it is considered as
1725 * pointer id
1726 */
1727 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1728 struct pat_ref_elt *ref;
1729 long long int conv;
1730 char *error;
1731
1732 /* Convert argument to integer value. */
1733 conv = strtoll(&args[3][1], &error, 16);
1734 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001735 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001736 appctx->st0 = STAT_CLI_PRINT;
1737 return 1;
1738 }
1739
1740 /* Convert and check integer to pointer. */
1741 ref = (struct pat_ref_elt *)(long)conv;
1742 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001743 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001744 appctx->st0 = STAT_CLI_PRINT;
1745 return 1;
1746 }
1747
1748 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001749 err = NULL;
1750 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1751 if (err)
1752 memprintf(&err, "%s.\n", err);
1753 appctx->ctx.cli.err = err;
1754 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001755 return 1;
1756 }
1757 }
1758 else {
1759 /* Else, use the entry identifier as pattern
1760 * string, and update the value.
1761 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001762 err = NULL;
1763 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1764 if (err)
1765 memprintf(&err, "%s.\n", err);
1766 appctx->ctx.cli.err = err;
1767 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001768 return 1;
1769 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001770 }
1771
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001772 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001773 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001774 return 1;
1775 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001776#ifdef USE_OPENSSL
1777 else if (strcmp(args[1], "ssl") == 0) {
1778 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01001779#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001780 char *err = NULL;
1781
Emeric Brunaf4ef742014-06-19 14:10:45 +02001782 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001783 if (!*args[3]) {
1784 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1785 appctx->st0 = STAT_CLI_PRINT;
1786 return 1;
1787 }
1788
1789 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1790 if (trash.len < 0) {
1791 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1792 appctx->st0 = STAT_CLI_PRINT;
1793 return 1;
1794 }
1795
1796 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1797 if (err) {
1798 memprintf(&err, "%s.\n", err);
1799 appctx->ctx.cli.err = err;
1800 appctx->st0 = STAT_CLI_PRINT_FREE;
1801 }
1802 return 1;
1803 }
1804 appctx->ctx.cli.msg = "OCSP Response updated!";
1805 appctx->st0 = STAT_CLI_PRINT;
1806 return 1;
1807#else
1808 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1809 appctx->st0 = STAT_CLI_PRINT;
1810 return 1;
1811#endif
1812 }
1813 else {
1814 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1815 appctx->st0 = STAT_CLI_PRINT;
1816 return 1;
1817 }
1818 }
1819#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001820 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001821 return 0;
1822 }
1823 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001824 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001825 if (strcmp(args[1], "agent") == 0) {
1826 struct server *sv;
1827
1828 sv = expect_server_admin(s, si, args[2]);
1829 if (!sv)
1830 return 1;
1831
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001832 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1833 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1834 appctx->st0 = STAT_CLI_PRINT;
1835 return 1;
1836 }
1837
1838 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001839 return 1;
1840 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001841 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001842 struct server *sv;
1843
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001844 sv = expect_server_admin(s, si, args[2]);
1845 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001846 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001847
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001848 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1849 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1850 appctx->st0 = STAT_CLI_PRINT;
1851 return 1;
1852 }
1853
1854 sv->check.state |= CHK_ST_ENABLED;
1855 return 1;
1856 }
1857 else if (strcmp(args[1], "server") == 0) {
1858 struct server *sv;
1859
1860 sv = expect_server_admin(s, si, args[2]);
1861 if (!sv)
1862 return 1;
1863
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001864 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001865 return 1;
1866 }
1867 else if (strcmp(args[1], "frontend") == 0) {
1868 struct proxy *px;
1869
1870 px = expect_frontend_admin(s, si, args[2]);
1871 if (!px)
1872 return 1;
1873
1874 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001875 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1876 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001877 return 1;
1878 }
1879
1880 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001881 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1882 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001883 return 1;
1884 }
1885
1886 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001887 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1888 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001889 return 1;
1890 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001891 return 1;
1892 }
1893 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001894 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001895 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001896 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001897 }
1898 }
1899 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001900 if (strcmp(args[1], "agent") == 0) {
1901 struct server *sv;
1902
1903 sv = expect_server_admin(s, si, args[2]);
1904 if (!sv)
1905 return 1;
1906
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001907 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001908 return 1;
1909 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001910 else if (strcmp(args[1], "health") == 0) {
1911 struct server *sv;
1912
1913 sv = expect_server_admin(s, si, args[2]);
1914 if (!sv)
1915 return 1;
1916
1917 sv->check.state &= ~CHK_ST_ENABLED;
1918 return 1;
1919 }
Simon Horman671b6f02013-11-25 10:46:39 +09001920 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001921 struct server *sv;
1922
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001923 sv = expect_server_admin(s, si, args[2]);
1924 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001925 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001926
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001927 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001928 return 1;
1929 }
1930 else if (strcmp(args[1], "frontend") == 0) {
1931 struct proxy *px;
1932
1933 px = expect_frontend_admin(s, si, args[2]);
1934 if (!px)
1935 return 1;
1936
1937 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001938 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1939 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001940 return 1;
1941 }
1942
1943 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001944 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1945 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001946 return 1;
1947 }
1948
1949 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001950 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1951 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001952 return 1;
1953 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001954 return 1;
1955 }
1956 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001957 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001958 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001959 return 1;
1960 }
1961 }
1962 else if (strcmp(args[0], "shutdown") == 0) {
1963 if (strcmp(args[1], "frontend") == 0) {
1964 struct proxy *px;
1965
1966 px = expect_frontend_admin(s, si, args[2]);
1967 if (!px)
1968 return 1;
1969
1970 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001971 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1972 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001973 return 1;
1974 }
1975
1976 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1977 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1978 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1979 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1980 stop_proxy(px);
1981 return 1;
1982 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001983 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02001984 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001985
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001986 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001987 appctx->ctx.cli.msg = stats_permission_denied_msg;
1988 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001989 return 1;
1990 }
1991
1992 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001993 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1994 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001995 return 1;
1996 }
1997
1998 ptr = (void *)strtoul(args[2], NULL, 0);
1999
Willy Tarreau87b09662015-04-03 00:22:06 +02002000 /* first, look for the requested stream in the stream table */
2001 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002002 if (sess == ptr)
2003 break;
2004 }
2005
Willy Tarreau87b09662015-04-03 00:22:06 +02002006 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002007 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002008 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2009 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002010 return 1;
2011 }
2012
Willy Tarreaue7dff022015-04-03 01:14:29 +02002013 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002014 return 1;
2015 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002016 else if (strcmp(args[1], "sessions") == 0) {
2017 if (strcmp(args[2], "server") == 0) {
2018 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002019 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002020
2021 sv = expect_server_admin(s, si, args[3]);
2022 if (!sv)
2023 return 1;
2024
Willy Tarreau87b09662015-04-03 00:22:06 +02002025 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002026 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2027 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002028 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002029
2030 return 1;
2031 }
2032 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002033 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2034 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002035 return 1;
2036 }
2037 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002038 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002039 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2040 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002041 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002042 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002043 }
2044 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002045 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2046 if (args[1][0] == 'm')
2047 appctx->ctx.map.display_flags = PAT_REF_MAP;
2048 else
2049 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002050
2051 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002052 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2053 if (!*args[2] || !*args[3]) {
2054 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2055 appctx->st0 = STAT_CLI_PRINT;
2056 return 1;
2057 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002058 }
2059
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002060 else {
2061 if (!*args[2] || !*args[3]) {
2062 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2063 appctx->st0 = STAT_CLI_PRINT;
2064 return 1;
2065 }
2066 }
2067
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002068 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002069 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002070 if (!appctx->ctx.map.ref ||
2071 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002072 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002073 appctx->st0 = STAT_CLI_PRINT;
2074 return 1;
2075 }
2076
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002077 /* If the entry identifier start with a '#', it is considered as
2078 * pointer id
2079 */
2080 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2081 struct pat_ref_elt *ref;
2082 long long int conv;
2083 char *error;
2084
2085 /* Convert argument to integer value. */
2086 conv = strtoll(&args[3][1], &error, 16);
2087 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002088 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002089 appctx->st0 = STAT_CLI_PRINT;
2090 return 1;
2091 }
2092
2093 /* Convert and check integer to pointer. */
2094 ref = (struct pat_ref_elt *)(long)conv;
2095 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002096 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002097 appctx->st0 = STAT_CLI_PRINT;
2098 return 1;
2099 }
2100
2101 /* Try to delete the entry. */
2102 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2103 /* The entry is not found, send message. */
2104 appctx->ctx.cli.msg = "Key not found.\n";
2105 appctx->st0 = STAT_CLI_PRINT;
2106 return 1;
2107 }
2108 }
2109 else {
2110 /* Else, use the entry identifier as pattern
2111 * string and try to delete the entry.
2112 */
2113 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2114 /* The entry is not found, send message. */
2115 appctx->ctx.cli.msg = "Key not found.\n";
2116 appctx->st0 = STAT_CLI_PRINT;
2117 return 1;
2118 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002119 }
2120
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002121 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002122 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002123 return 1;
2124 }
2125 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002126 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002127 appctx->st0 = STAT_CLI_PRINT;
2128 return 1;
2129 }
2130 }
2131 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002132 if (strcmp(args[1], "map") == 0 ||
2133 strcmp(args[1], "acl") == 0) {
2134 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002135 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002136
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002137 /* Set flags. */
2138 if (args[1][0] == 'm')
2139 appctx->ctx.map.display_flags = PAT_REF_MAP;
2140 else
2141 appctx->ctx.map.display_flags = PAT_REF_ACL;
2142
2143 /* If the keywork is "map", we expect three parameters, if it
2144 * is "acl", we expect only two parameters
2145 */
2146 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2147 if (!*args[2] || !*args[3] || !*args[4]) {
2148 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2149 appctx->st0 = STAT_CLI_PRINT;
2150 return 1;
2151 }
2152 }
2153 else {
2154 if (!*args[2] || !*args[3]) {
2155 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2156 appctx->st0 = STAT_CLI_PRINT;
2157 return 1;
2158 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002159 }
2160
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002161 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002162 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002163 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002164 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002165 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002166 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002167 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002168 appctx->st0 = STAT_CLI_PRINT;
2169 return 1;
2170 }
2171
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002172 /* The command "add acl" is prohibited if the reference
2173 * use samples.
2174 */
2175 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2176 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2177 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2178 "You must use the command 'add map' to add values.\n";
2179 appctx->st0 = STAT_CLI_PRINT;
2180 return 1;
2181 }
2182
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002183 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002184 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002185 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002186 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002187 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002188 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002189 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002190 if (err)
2191 memprintf(&err, "%s.\n", err);
2192 appctx->ctx.cli.err = err;
2193 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002194 return 1;
2195 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002196
2197 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002198 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002199 return 1;
2200 }
2201 else { /* unknown "del" parameter */
2202 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2203 appctx->st0 = STAT_CLI_PRINT;
2204 return 1;
2205 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002206 }
2207 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002208 return 0;
2209 }
2210 return 1;
2211}
2212
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002213/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002214 * used to processes I/O from/to the stats unix socket. The system relies on a
2215 * state machine handling requests and various responses. We read a request,
2216 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002217 * Then we can read again. The state is stored in appctx->st0 and is one of the
2218 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002219 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002220 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002221static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002222{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002223 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002224 struct channel *req = si_oc(si);
2225 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002226 int reql;
2227 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002228
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002229 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2230 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002231
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002232 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002233 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002234 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002235 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2236 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002237 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002238 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002239 /* Let's close for real now. We just close the request
2240 * side, the conditions below will complete if needed.
2241 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002242 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002243 break;
2244 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002245 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002246 /* ensure we have some output room left in the event we
2247 * would want to return some info right after parsing.
2248 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002249 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002250 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002251 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002252 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002253
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002254 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002255 if (reql <= 0) { /* closed or EOL not found */
2256 if (reql == 0)
2257 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002258 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002259 continue;
2260 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002261
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002262 /* seek for a possible semi-colon. If we find one, we
2263 * replace it with an LF and skip only this part.
2264 */
2265 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002266 if (trash.str[len] == ';') {
2267 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002268 reql = len + 1;
2269 break;
2270 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002271
Willy Tarreau816fc222009-10-04 07:36:58 +02002272 /* now it is time to check that we have a full line,
2273 * remove the trailing \n and possibly \r, then cut the
2274 * line.
2275 */
2276 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002277 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002278 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002279 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002280 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002281
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002282 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002283 len--;
2284
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002285 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002286
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002287 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002288 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002289 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002290 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002291 continue;
2292 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002293 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002294 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002295 else if (strcmp(trash.str, "help") == 0 ||
2296 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002297 appctx->ctx.cli.msg = stats_sock_usage_msg;
2298 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002299 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002300 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002301 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002302 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002303 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002304 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002305 /* if prompt is disabled, print help on empty lines,
2306 * so that the user at least knows how to enable
2307 * prompt and find help.
2308 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002309 appctx->ctx.cli.msg = stats_sock_usage_msg;
2310 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002311 }
2312
2313 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002314 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002315 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002316 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002317 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002318 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau00a37f02015-04-13 12:05:19 +02002319 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002320 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002321 continue;
2322 }
2323
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002324 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002325 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002326 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002327 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002328 else
2329 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002330 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002331 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002332 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002333 free(appctx->ctx.cli.err);
2334 appctx->st0 = STAT_CLI_PROMPT;
2335 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002336 else
2337 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002338 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002339 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002340 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002341 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002342 break;
2343 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002344 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002345 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002346 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002347 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002348 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002349 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002350 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002351 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002352 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002353 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002354 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002355 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002356 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002357 if (stats_table_request(si, appctx->st0))
2358 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002359 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002360 case STAT_CLI_O_PATS:
2361 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002362 appctx->st0 = STAT_CLI_PROMPT;
2363 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002364 case STAT_CLI_O_PAT:
2365 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002366 appctx->st0 = STAT_CLI_PROMPT;
2367 break;
2368 case STAT_CLI_O_MLOOK:
2369 if (stats_map_lookup(si))
2370 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002371 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002372 case STAT_CLI_O_POOLS:
2373 if (stats_dump_pools_to_buffer(si))
2374 appctx->st0 = STAT_CLI_PROMPT;
2375 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002376 default: /* abnormal state */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002377 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002378 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002379 break;
2380 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002381
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002382 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002383 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002384 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002385 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002386 else
2387 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002388 }
2389
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002390 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002391 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002392 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002393
2394 /* Now we close the output if one of the writers did so,
2395 * or if we're not in interactive mode and the request
2396 * buffer is empty. This still allows pipelined requests
2397 * to be sent in non-interactive mode.
2398 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002399 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2400 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002401 continue;
2402 }
2403
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002404 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002405 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002406 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002407 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002408
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002409 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002410 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2411 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002412 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002413 * and nothing more to consume. This is comparable to a broken pipe, so
2414 * we forward the close to the request side so that it flows upstream to
2415 * the client.
2416 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002417 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002418 }
2419
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002420 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002421 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2422 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2423 /* We have no more processing to do, and nothing more to send, and
2424 * the client side has closed. So we'll forward this state downstream
2425 * on the response buffer.
2426 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002427 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002428 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002429 }
2430
2431 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002432 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002433
2434 /* we don't want to expire timeouts while we're processing requests */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002435 si_ic(si)->rex = TICK_ETERNITY;
2436 si_oc(si)->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002437
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002438 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002439 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 +02002440 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002441 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 +02002442
2443 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2444 /* check that we have released everything then unregister */
2445 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002446 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002447}
2448
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002449/* This function dumps information onto the stream interface's read buffer.
2450 * It returns 0 as long as it does not complete, non-zero upon completion.
2451 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002452 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002453static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002454{
2455 unsigned int up = (now.tv_sec - start_date.tv_sec);
2456
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002457#ifdef USE_OPENSSL
2458 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2459 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2460 int ssl_reuse = 0;
2461
2462 if (ssl_key_rate < ssl_sess_rate) {
2463 /* count the ssl reuse ratio and avoid overflows in both directions */
2464 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2465 }
2466#endif
2467
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002468 chunk_printf(&trash,
2469 "Name: " PRODUCT_NAME "\n"
2470 "Version: " HAPROXY_VERSION "\n"
2471 "Release_date: " HAPROXY_DATE "\n"
2472 "Nbproc: %d\n"
2473 "Process_num: %d\n"
2474 "Pid: %d\n"
2475 "Uptime: %dd %dh%02dm%02ds\n"
2476 "Uptime_sec: %d\n"
2477 "Memmax_MB: %d\n"
2478 "Ulimit-n: %d\n"
2479 "Maxsock: %d\n"
2480 "Maxconn: %d\n"
2481 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002482 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002483 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002484 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002485#ifdef USE_OPENSSL
2486 "MaxSslConns: %d\n"
2487 "CurrSslConns: %d\n"
2488 "CumSslConns: %d\n"
2489#endif
2490 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002491 "PipesUsed: %d\n"
2492 "PipesFree: %d\n"
2493 "ConnRate: %d\n"
2494 "ConnRateLimit: %d\n"
2495 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002496 "SessRate: %d\n"
2497 "SessRateLimit: %d\n"
2498 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002499#ifdef USE_OPENSSL
2500 "SslRate: %d\n"
2501 "SslRateLimit: %d\n"
2502 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002503 "SslFrontendKeyRate: %d\n"
2504 "SslFrontendMaxKeyRate: %d\n"
2505 "SslFrontendSessionReuse_pct: %d\n"
2506 "SslBackendKeyRate: %d\n"
2507 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002508 "SslCacheLookups: %u\n"
2509 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002510#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002511 "CompressBpsIn: %u\n"
2512 "CompressBpsOut: %u\n"
2513 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002514#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002515 "ZlibMemUsage: %ld\n"
2516 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002517#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002518 "Tasks: %d\n"
2519 "Run_queue: %d\n"
2520 "Idle_pct: %d\n"
2521 "node: %s\n"
2522 "description: %s\n"
2523 "",
2524 global.nbproc,
2525 relative_pid,
2526 pid,
2527 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2528 up,
2529 global.rlimit_memmax,
2530 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002531 global.maxsock, global.maxconn, global.hardmaxconn,
2532 actconn, totalconn, global.req_count,
2533#ifdef USE_OPENSSL
2534 global.maxsslconn, sslconns, totalsslconns,
2535#endif
2536 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002537 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002538 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002539#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002540 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2541 ssl_key_rate, global.ssl_fe_keys_max,
2542 ssl_reuse,
2543 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002544 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002545#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002546 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2547 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002548#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002549 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002550#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002551 nb_tasks_cur, run_queue_cur, idle_pct,
2552 global.node, global.desc ? global.desc : ""
2553 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002554
Willy Tarreaubc18da12015-03-13 14:00:47 +01002555 if (bi_putchk(si_ic(si), &trash) == -1) {
2556 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002557 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002558 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002559
2560 return 1;
2561}
2562
Willy Tarreau12833bb2014-01-28 16:49:56 +01002563/* This function dumps memory usage information onto the stream interface's
2564 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2565 * completion. No state is used.
2566 */
2567static int stats_dump_pools_to_buffer(struct stream_interface *si)
2568{
2569 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01002570 if (bi_putchk(si_ic(si), &trash) == -1) {
2571 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002572 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002573 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01002574 return 1;
2575}
2576
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002577/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2578 * the state from stream interface <si>. The caller is responsible for clearing
2579 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002580 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002581static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002582{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002583 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002584 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002585
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002586 if (!(px->cap & PR_CAP_FE))
2587 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002588
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002589 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002590 return 0;
2591
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002592 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002593 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002594 /* name, queue */
2595 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002596
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002597 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002598 /* Column sub-heading for Enable or Disable server */
2599 chunk_appendf(&trash, "<td></td>");
2600 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002601
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002602 chunk_appendf(&trash,
2603 "<td class=ac>"
2604 "<a name=\"%s/Frontend\"></a>"
2605 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2606 "<td colspan=3></td>"
2607 "",
2608 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002609
Willy Tarreau466c9b52012-12-23 02:25:03 +01002610 chunk_appendf(&trash,
2611 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002612 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002613 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2614 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2615 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002616 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2617 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2618 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002619
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002620 if (px->mode == PR_MODE_HTTP)
2621 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002622 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002623 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002624
2625 chunk_appendf(&trash,
2626 "</table></div></u></td>"
2627 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002628 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002629 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2630 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2631 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002632 U2H(px->fe_counters.sps_max),
2633 U2H(px->fe_counters.cps_max),
2634 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002635
2636 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002637 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002638 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002639 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002640
2641 chunk_appendf(&trash,
2642 "</table></div></u></td>"
2643 /* sessions rate : limit */
2644 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002645 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002646
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002647 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002648 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002649 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002650 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002651 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2652 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002653 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002654 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2655 U2H(px->fe_counters.cum_sess),
2656 U2H(px->fe_counters.cum_conn),
2657 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002658
Willy Tarreau466c9b52012-12-23 02:25:03 +01002659 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002660 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002661 chunk_appendf(&trash,
2662 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2663 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2664 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2665 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2666 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2667 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2668 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2669 "<tr><th>- other responses:</th><td>%s</td></tr>"
2670 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2671 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002672 U2H(px->fe_counters.p.http.cum_req),
2673 U2H(px->fe_counters.p.http.rsp[1]),
2674 U2H(px->fe_counters.p.http.rsp[2]),
2675 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002676 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002677 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002678 U2H(px->fe_counters.p.http.rsp[3]),
2679 U2H(px->fe_counters.p.http.rsp[4]),
2680 U2H(px->fe_counters.p.http.rsp[5]),
2681 U2H(px->fe_counters.p.http.rsp[0]),
2682 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002683 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002684
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002685 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002686 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002687 /* sessions: lbtot, lastsess */
2688 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002689 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002690 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002691 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002692 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002693
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002694 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002695 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002696 "<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 +01002697 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002698 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002699 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2700 px->fe_counters.comp_in ?
2701 (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 +01002702 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002703
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002704 chunk_appendf(&trash,
2705 /* denied: req, resp */
2706 "<td>%s</td><td>%s</td>"
2707 /* errors : request, connect, response */
2708 "<td>%s</td><td></td><td></td>"
2709 /* warnings: retries, redispatches */
2710 "<td></td><td></td>"
2711 /* server status : reflect frontend status */
2712 "<td class=ac>%s</td>"
2713 /* rest of server: nothing */
2714 "<td class=ac colspan=8></td></tr>"
2715 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002716 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2717 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002718 px->state == PR_STREADY ? "OPEN" :
2719 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002720 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002721 else { /* CSV mode */
2722 chunk_appendf(&trash,
2723 /* pxid, name, queue cur, queue max, */
2724 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002725 /* sessions : current, max, limit, total */
2726 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002727 /* bytes : in, out */
2728 "%lld,%lld,"
2729 /* denied: req, resp */
2730 "%lld,%lld,"
2731 /* errors : request, connect, response */
2732 "%lld,,,"
2733 /* warnings: retries, redispatches */
2734 ",,"
2735 /* server status : reflect frontend status */
2736 "%s,"
2737 /* rest of server: nothing */
2738 ",,,,,,,,"
2739 /* pid, iid, sid, throttle, lbtot, tracked, type */
2740 "%d,%d,0,,,,%d,"
2741 /* rate, rate_lim, rate_max */
2742 "%u,%u,%u,"
2743 /* check_status, check_code, check_duration */
2744 ",,,",
2745 px->id,
2746 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2747 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2748 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2749 px->fe_counters.failed_req,
2750 px->state == PR_STREADY ? "OPEN" :
2751 px->state == PR_STFULL ? "FULL" : "STOP",
2752 relative_pid, px->uuid, STATS_TYPE_FE,
2753 read_freq_ctr(&px->fe_sess_per_sec),
2754 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002755
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002756 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2757 if (px->mode == PR_MODE_HTTP) {
2758 for (i=1; i<6; i++)
2759 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2760 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2761 }
2762 else
2763 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002764
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002765 /* failed health analyses */
2766 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002767
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002768 /* requests : req_rate, req_rate_max, req_tot, */
2769 chunk_appendf(&trash, "%u,%u,%lld,",
2770 read_freq_ctr(&px->fe_req_per_sec),
2771 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2772
2773 /* errors: cli_aborts, srv_aborts */
2774 chunk_appendf(&trash, ",,");
2775
2776 /* compression: in, out, bypassed */
2777 chunk_appendf(&trash, "%lld,%lld,%lld,",
2778 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2779
2780 /* compression: comp_rsp */
2781 chunk_appendf(&trash, "%lld,",
2782 px->fe_counters.p.http.comp_rsp);
2783
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002784 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2785 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002786
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002787 /* finish with EOL */
2788 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002789 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002790 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002791}
2792
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002793/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2794 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2795 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2796 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002797 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002798static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002799{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002800 struct appctx *appctx = __objt_appctx(si->end);
2801
2802 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002803 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002804 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002805 /* Column sub-heading for Enable or Disable server */
2806 chunk_appendf(&trash, "<td></td>");
2807 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002808 chunk_appendf(&trash,
2809 /* frontend name, listener name */
2810 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2811 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2812 "",
2813 px->id, l->name,
2814 (flags & ST_SHLGNDS)?"<u>":"",
2815 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002816
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002817 if (flags & ST_SHLGNDS) {
2818 char str[INET6_ADDRSTRLEN];
2819 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002820
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002821 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002822
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002823 port = get_host_port(&l->addr);
2824 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2825 case AF_INET:
2826 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2827 break;
2828 case AF_INET6:
2829 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2830 break;
2831 case AF_UNIX:
2832 chunk_appendf(&trash, "unix, ");
2833 break;
2834 case -1:
2835 chunk_appendf(&trash, "(%s), ", strerror(errno));
2836 break;
2837 }
Willy Tarreau91861262007-10-17 17:06:05 +02002838
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002839 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002840 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002841 }
Willy Tarreau91861262007-10-17 17:06:05 +02002842
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002843 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002844 /* queue */
2845 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002846 /* sessions rate: current, max, limit */
2847 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002848 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002849 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002850 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002851 /* bytes: in, out */
2852 "<td>%s</td><td>%s</td>"
2853 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002854 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002855 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2856 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002857
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002858 chunk_appendf(&trash,
2859 /* denied: req, resp */
2860 "<td>%s</td><td>%s</td>"
2861 /* errors: request, connect, response */
2862 "<td>%s</td><td></td><td></td>"
2863 /* warnings: retries, redispatches */
2864 "<td></td><td></td>"
2865 /* server status: reflect listener status */
2866 "<td class=ac>%s</td>"
2867 /* rest of server: nothing */
2868 "<td class=ac colspan=8></td></tr>"
2869 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002870 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2871 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002872 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2873 }
2874 else { /* CSV mode */
2875 chunk_appendf(&trash,
2876 /* pxid, name, queue cur, queue max, */
2877 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002878 /* sessions: current, max, limit, total */
2879 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002880 /* bytes: in, out */
2881 "%lld,%lld,"
2882 /* denied: req, resp */
2883 "%lld,%lld,"
2884 /* errors: request, connect, response */
2885 "%lld,,,"
2886 /* warnings: retries, redispatches */
2887 ",,"
2888 /* server status: reflect listener status */
2889 "%s,"
2890 /* rest of server: nothing */
2891 ",,,,,,,,"
2892 /* pid, iid, sid, throttle, lbtot, tracked, type */
2893 "%d,%d,%d,,,,%d,"
2894 /* rate, rate_lim, rate_max */
2895 ",,,"
2896 /* check_status, check_code, check_duration */
2897 ",,,"
2898 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2899 ",,,,,,"
2900 /* failed health analyses */
2901 ","
2902 /* requests : req_rate, req_rate_max, req_tot, */
2903 ",,,"
2904 /* errors: cli_aborts, srv_aborts */
2905 ",,"
2906 /* compression: in, out, bypassed, comp_rsp */
2907 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002908 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2909 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002910 "\n",
2911 px->id, l->name,
2912 l->nbconn, l->counters->conn_max,
2913 l->maxconn, l->counters->cum_conn,
2914 l->counters->bytes_in, l->counters->bytes_out,
2915 l->counters->denied_req, l->counters->denied_resp,
2916 l->counters->failed_req,
2917 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2918 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2919 }
2920 return 1;
2921}
Willy Tarreau91861262007-10-17 17:06:05 +02002922
Simon Horman4d2eab62015-04-23 14:51:26 +09002923enum srv_stats_state {
2924 SRV_STATS_STATE_DOWN = 0,
2925 SRV_STATS_STATE_DOWN_AGENT,
2926 SRV_STATS_STATE_GOING_UP,
2927 SRV_STATS_STATE_UP_GOING_DOWN,
2928 SRV_STATS_STATE_UP,
2929 SRV_STATS_STATE_NOLB_GOING_DOWN,
2930 SRV_STATS_STATE_NOLB,
2931 SRV_STATS_STATE_DRAIN_GOING_DOWN,
2932 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09002933 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09002934 SRV_STATS_STATE_NO_CHECK,
2935
2936 SRV_STATS_STATE_COUNT, /* Must be last */
2937};
2938
Simon Horman837bfa72015-04-23 14:51:27 +09002939enum srv_stats_colour {
2940 SRV_STATS_COLOUR_DOWN = 0,
2941 SRV_STATS_COLOUR_GOING_UP,
2942 SRV_STATS_COLOUR_GOING_DOWN,
2943 SRV_STATS_COLOUR_UP,
2944 SRV_STATS_COLOUR_NOLB,
2945 SRV_STATS_COLOUR_DRAINING,
2946 SRV_STATS_COLOUR_NO_CHECK,
2947
2948 SRV_STATS_COLOUR_COUNT, /* Must be last */
2949};
2950
2951static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
2952 [SRV_STATS_COLOUR_DOWN] = "down",
2953 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
2954 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
2955 [SRV_STATS_COLOUR_UP] = "up",
2956 [SRV_STATS_COLOUR_NOLB] = "nolb",
2957 [SRV_STATS_COLOUR_DRAINING] = "draining",
2958 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
2959};
2960
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002961/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2962 * from stream interface <si>, stats flags <flags>, and server state <state>.
2963 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09002964 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002965 */
Simon Horman4d2eab62015-04-23 14:51:26 +09002966static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09002967 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002968{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002969 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002970 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002971 char str[INET6_ADDRSTRLEN];
2972 struct chunk src;
2973 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002974
Willy Tarreau32091232014-05-16 13:52:00 +02002975 /* we have "via" which is the tracked server as described in the configuration,
2976 * and "ref" which is the checked server and the end of the chain.
2977 */
2978 via = sv->track ? sv->track : sv;
2979 ref = via;
2980 while (ref->track)
2981 ref = ref->track;
2982
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002983 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09002984 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
2985 [SRV_STATS_STATE_DOWN] = "DOWN",
2986 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
2987 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
2988 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
2989 [SRV_STATS_STATE_UP] = "UP",
2990 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
2991 [SRV_STATS_STATE_NOLB] = "NOLB",
2992 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
2993 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09002994 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09002995 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002996 };
Willy Tarreau91861262007-10-17 17:06:05 +02002997
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002998 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002999 chunk_appendf(&trash, "<tr class=\"maintain\">");
3000 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003001 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003002 "<tr class=\"%s_%s\">",
3003 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003004
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003005 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003006 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003007 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3008 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003009
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003010 chunk_appendf(&trash,
3011 "<td class=ac><a name=\"%s/%s\"></a>%s"
3012 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3013 "",
3014 px->id, sv->id,
3015 (flags & ST_SHLGNDS) ? "<u>" : "",
3016 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003017
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003018 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003019 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003020
3021 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3022 case AF_INET:
3023 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3024 break;
3025 case AF_INET6:
3026 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3027 break;
3028 case AF_UNIX:
3029 chunk_appendf(&trash, "unix, ");
3030 break;
3031 case -1:
3032 chunk_appendf(&trash, "(%s), ", strerror(errno));
3033 break;
3034 default: /* address family not supported */
3035 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003036 }
Willy Tarreau91861262007-10-17 17:06:05 +02003037
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003038 /* id */
3039 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003040
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003041 /* cookie */
3042 if (sv->cookie) {
3043 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003044
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003045 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3046 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003047
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003048 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003049 }
3050
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003051 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003052 }
Willy Tarreau91861262007-10-17 17:06:05 +02003053
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003054 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003055 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003056 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003057 /* sessions rate : current, max, limit */
3058 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003059 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003060 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003061 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3062 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003063
Willy Tarreau466c9b52012-12-23 02:25:03 +01003064
3065 chunk_appendf(&trash,
3066 /* sessions: current, max, limit, total */
3067 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003068 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003069 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3070 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003071 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3072 U2H(sv->counters.cum_sess),
3073 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003074
Willy Tarreau466c9b52012-12-23 02:25:03 +01003075 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3076 if (px->mode == PR_MODE_HTTP) {
3077 unsigned long long tot;
3078 for (tot = i = 0; i < 6; i++)
3079 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003080
Willy Tarreau466c9b52012-12-23 02:25:03 +01003081 chunk_appendf(&trash,
3082 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3083 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3084 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3085 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3086 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3087 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3088 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3089 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003090 U2H(tot),
3091 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3092 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3093 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3094 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3095 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3096 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 +02003097 }
Willy Tarreau91861262007-10-17 17:06:05 +02003098
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003099 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3100 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)));
3101 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)));
3102 if (px->mode == PR_MODE_HTTP)
3103 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)));
3104 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)));
3105
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003106 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003107 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003108 /* sessions: lbtot, last */
3109 "<td>%s</td><td>%s</td>",
3110 U2H(sv->counters.cum_lbconn),
3111 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003112
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003113 chunk_appendf(&trash,
3114 /* bytes : in, out */
3115 "<td>%s</td><td>%s</td>"
3116 /* denied: req, resp */
3117 "<td></td><td>%s</td>"
3118 /* errors : request, connect */
3119 "<td></td><td>%s</td>"
3120 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003121 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003122 /* warnings: retries, redispatches */
3123 "<td>%lld</td><td>%lld</td>"
3124 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003125 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3126 U2H(sv->counters.failed_secu),
3127 U2H(sv->counters.failed_conns),
3128 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003129 sv->counters.cli_aborts,
3130 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003131 sv->counters.retries, sv->counters.redispatches);
3132
3133 /* status, lest check */
3134 chunk_appendf(&trash, "<td class=ac>");
3135
Willy Tarreau20125212014-05-13 19:44:56 +02003136 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003137 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3138 chunk_appendf(&trash, "MAINT");
3139 }
Simon Horman0766e442014-11-12 15:55:54 +09003140 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003141 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003142 /* DOWN (agent) */
3143 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 +02003144 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003145 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003146 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3147 chunk_appendf(&trash,
3148 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003149 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3150 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003151 }
Willy Tarreau91861262007-10-17 17:06:05 +02003152
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003153 if ((sv->state == SRV_ST_STOPPED) &&
3154 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3155 chunk_appendf(&trash,
3156 "</td><td class=ac><u> %s%s",
3157 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3158 get_check_status_info(sv->agent.status));
3159
3160 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3161 chunk_appendf(&trash, "/%d", sv->agent.code);
3162
3163 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3164 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3165
3166 chunk_appendf(&trash, "<div class=tips>%s",
3167 get_check_status_description(sv->agent.status));
3168 if (*sv->agent.desc) {
3169 chunk_appendf(&trash, ": ");
3170 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3171 chunk_htmlencode(&trash, &src);
3172 }
3173 chunk_appendf(&trash, "</div></u>");
3174 }
3175 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003176 chunk_appendf(&trash,
3177 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003178 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003179 get_check_status_info(sv->check.status));
3180
3181 if (sv->check.status >= HCHK_STATUS_L57DATA)
3182 chunk_appendf(&trash, "/%d", sv->check.code);
3183
3184 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003185 chunk_appendf(&trash, " in %lums", sv->check.duration);
3186
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003187 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003188 get_check_status_description(sv->check.status));
3189 if (*sv->check.desc) {
3190 chunk_appendf(&trash, ": ");
3191 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3192 chunk_htmlencode(&trash, &src);
3193 }
3194 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003195 }
3196 else
3197 chunk_appendf(&trash, "</td><td>");
3198
3199 chunk_appendf(&trash,
3200 /* weight */
3201 "</td><td class=ac>%d</td>"
3202 /* act, bck */
3203 "<td class=ac>%s</td><td class=ac>%s</td>"
3204 "",
3205 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003206 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3207 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003208
3209 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003210 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003211 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003212
3213 if (ref->observe)
3214 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3215
3216 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003217 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003218 "<td>%lld</td><td>%s</td>"
3219 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003220 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003221 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3222 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003223 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3224 /* tracking a server */
3225 chunk_appendf(&trash,
3226 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003227 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003228 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003229 else
3230 chunk_appendf(&trash, "<td colspan=3></td>");
3231
3232 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003233 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003234 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003235 else
3236 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3237 }
3238 else { /* CSV mode */
Simon Horman4d2eab62015-04-23 14:51:26 +09003239 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3240 [SRV_STATS_STATE_DOWN] = "DOWN,",
3241 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3242 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3243 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3244 [SRV_STATS_STATE_UP] = "UP,",
3245 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3246 [SRV_STATS_STATE_NOLB] = "NOLB,",
3247 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3248 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003249 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003250 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003251 };
3252
3253 chunk_appendf(&trash,
3254 /* pxid, name */
3255 "%s,%s,"
3256 /* queue : current, max */
3257 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003258 /* sessions : current, max, limit, total */
3259 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003260 /* bytes : in, out */
3261 "%lld,%lld,"
3262 /* denied: req, resp */
3263 ",%lld,"
3264 /* errors : request, connect, response */
3265 ",%lld,%lld,"
3266 /* warnings: retries, redispatches */
3267 "%lld,%lld,"
3268 "",
3269 px->id, sv->id,
3270 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003271 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003272 sv->counters.bytes_in, sv->counters.bytes_out,
3273 sv->counters.failed_secu,
3274 sv->counters.failed_conns, sv->counters.failed_resp,
3275 sv->counters.retries, sv->counters.redispatches);
3276
3277 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003278 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003279 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003280 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003281 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003282 else
3283 chunk_appendf(&trash,
3284 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003285 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3286 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003287
3288 chunk_appendf(&trash,
3289 /* weight, active, backup */
3290 "%d,%d,%d,"
3291 "",
3292 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003293 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3294 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003295
3296 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003297 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003298 chunk_appendf(&trash,
3299 "%lld,%lld,%d,%d,",
3300 sv->counters.failed_checks, sv->counters.down_trans,
3301 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3302 else
3303 chunk_appendf(&trash, ",,,,");
3304
3305 /* queue limit, pid, iid, sid, */
3306 chunk_appendf(&trash,
3307 "%s,"
3308 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003309 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003310 relative_pid, px->uuid, sv->puid);
3311
3312 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003313 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003314 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003315
3316 /* sessions: lbtot */
3317 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3318
3319 /* tracked */
3320 if (sv->track)
3321 chunk_appendf(&trash, "%s/%s,",
3322 sv->track->proxy->id, sv->track->id);
3323 else
3324 chunk_appendf(&trash, ",");
3325
3326 /* type */
3327 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3328
3329 /* rate */
3330 chunk_appendf(&trash, "%u,,%u,",
3331 read_freq_ctr(&sv->sess_per_sec),
3332 sv->counters.sps_max);
3333
Willy Tarreauff5ae352013-12-11 20:36:34 +01003334 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003335 /* check_status */
3336 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3337
3338 /* check_code */
3339 if (sv->check.status >= HCHK_STATUS_L57DATA)
3340 chunk_appendf(&trash, "%u,", sv->check.code);
3341 else
3342 chunk_appendf(&trash, ",");
3343
3344 /* check_duration */
3345 if (sv->check.status >= HCHK_STATUS_CHECKED)
3346 chunk_appendf(&trash, "%lu,", sv->check.duration);
3347 else
3348 chunk_appendf(&trash, ",");
3349
3350 }
3351 else
3352 chunk_appendf(&trash, ",,,");
3353
3354 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3355 if (px->mode == PR_MODE_HTTP) {
3356 for (i=1; i<6; i++)
3357 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3358
3359 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3360 }
3361 else
3362 chunk_appendf(&trash, ",,,,,,");
3363
3364 /* failed health analyses */
3365 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3366
3367 /* requests : req_rate, req_rate_max, req_tot, */
3368 chunk_appendf(&trash, ",,,");
3369
3370 /* errors: cli_aborts, srv_aborts */
3371 chunk_appendf(&trash, "%lld,%lld,",
3372 sv->counters.cli_aborts, sv->counters.srv_aborts);
3373
3374 /* compression: in, out, bypassed, comp_rsp */
3375 chunk_appendf(&trash, ",,,,");
3376
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003377 /* lastsess */
3378 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3379
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003380 /* capture of last check and agent statuses */
3381 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->check.desc) : "");
3382 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->agent.desc) : "");
3383
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003384 /* qtime, ctime, rtime, ttime, */
3385 chunk_appendf(&trash, "%u,%u,%u,%u,",
3386 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3387 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3388 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3389 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3390
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003391 /* finish with EOL */
3392 chunk_appendf(&trash, "\n");
3393 }
3394 return 1;
3395}
3396
3397/* Dumps a line for backend <px> to the trash for and uses the state from stream
3398 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3399 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3400 */
3401static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3402{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003403 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003404 struct chunk src;
3405 int i;
3406
3407 if (!(px->cap & PR_CAP_BE))
3408 return 0;
3409
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003410 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003411 return 0;
3412
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003413 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003414 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003415 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003416 /* Column sub-heading for Enable or Disable server */
3417 chunk_appendf(&trash, "<td></td>");
3418 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003419 chunk_appendf(&trash,
3420 "<td class=ac>"
3421 /* name */
3422 "%s<a name=\"%s/Backend\"></a>"
3423 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3424 "",
3425 (flags & ST_SHLGNDS)?"<u>":"",
3426 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003427
3428 if (flags & ST_SHLGNDS) {
3429 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003430 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003431 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3432
3433 /* cookie */
3434 if (px->cookie_name) {
3435 chunk_appendf(&trash, ", cookie: '");
3436 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3437 chunk_htmlencode(&trash, &src);
3438 chunk_appendf(&trash, "'");
3439 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003440 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003441 }
3442
3443 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003444 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003445 /* queue : current, max */
3446 "<td>%s</td><td>%s</td><td></td>"
3447 /* sessions rate : current, max, limit */
3448 "<td>%s</td><td>%s</td><td></td>"
3449 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003450 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003451 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3452 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003453
3454 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003455 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003456 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003457 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003458 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003459 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003460 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3461 U2H(px->be_counters.cum_conn),
3462 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003463
Willy Tarreau466c9b52012-12-23 02:25:03 +01003464 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003465 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003466 chunk_appendf(&trash,
3467 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3468 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3469 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3470 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3471 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3472 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3473 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3474 "<tr><th>- other responses:</th><td>%s</td></tr>"
3475 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003476 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003477 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003478 U2H(px->be_counters.p.http.cum_req),
3479 U2H(px->be_counters.p.http.rsp[1]),
3480 U2H(px->be_counters.p.http.rsp[2]),
3481 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003482 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003483 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003484 U2H(px->be_counters.p.http.rsp[3]),
3485 U2H(px->be_counters.p.http.rsp[4]),
3486 U2H(px->be_counters.p.http.rsp[5]),
3487 U2H(px->be_counters.p.http.rsp[0]),
3488 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003489 }
3490
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003491 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)));
3492 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)));
3493 if (px->mode == PR_MODE_HTTP)
3494 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)));
3495 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)));
3496
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003497 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003498 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003499 /* sessions: lbtot, last */
3500 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003501 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003502 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003503 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003504 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003505 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003506 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003507
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003508 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003509 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003510 "<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 +01003511 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003512 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003513 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3514 px->be_counters.comp_in ?
3515 (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 +01003516 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3517
3518 chunk_appendf(&trash,
3519 /* denied: req, resp */
3520 "<td>%s</td><td>%s</td>"
3521 /* errors : request, connect */
3522 "<td></td><td>%s</td>"
3523 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003524 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003525 /* warnings: retries, redispatches */
3526 "<td>%lld</td><td>%lld</td>"
3527 /* backend status: reflect backend status (up/down): we display UP
3528 * if the backend has known working servers or if it has no server at
3529 * all (eg: for stats). Then we display the total weight, number of
3530 * active and backups. */
3531 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3532 "<td class=ac>%d</td><td class=ac>%d</td>"
3533 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003534 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3535 U2H(px->be_counters.failed_conns),
3536 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003537 px->be_counters.cli_aborts,
3538 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003539 px->be_counters.retries, px->be_counters.redispatches,
3540 human_time(now.tv_sec - px->last_change, 1),
3541 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3542 "<font color=\"red\"><b>DOWN</b></font>",
3543 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3544 px->srv_act, px->srv_bck);
3545
3546 chunk_appendf(&trash,
3547 /* rest of backend: nothing, down transitions, total downtime, throttle */
3548 "<td class=ac>&nbsp;</td><td>%d</td>"
3549 "<td>%s</td>"
3550 "<td></td>"
3551 "</tr>",
3552 px->down_trans,
3553 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3554 }
3555 else { /* CSV mode */
3556 chunk_appendf(&trash,
3557 /* pxid, name */
3558 "%s,BACKEND,"
3559 /* queue : current, max */
3560 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003561 /* sessions : current, max, limit, total */
3562 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003563 /* bytes : in, out */
3564 "%lld,%lld,"
3565 /* denied: req, resp */
3566 "%lld,%lld,"
3567 /* errors : request, connect, response */
3568 ",%lld,%lld,"
3569 /* warnings: retries, redispatches */
3570 "%lld,%lld,"
3571 /* backend status: reflect backend status (up/down): we display UP
3572 * if the backend has known working servers or if it has no server at
3573 * all (eg: for stats). Then we display the total weight, number of
3574 * active and backups. */
3575 "%s,"
3576 "%d,%d,%d,"
3577 /* rest of backend: nothing, down transitions, last change, total downtime */
3578 ",%d,%d,%d,,"
3579 /* pid, iid, sid, throttle, lbtot, tracked, type */
3580 "%d,%d,0,,%lld,,%d,"
3581 /* rate, rate_lim, rate_max, */
3582 "%u,,%u,"
3583 /* check_status, check_code, check_duration */
3584 ",,,",
3585 px->id,
3586 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3587 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3588 px->be_counters.bytes_in, px->be_counters.bytes_out,
3589 px->be_counters.denied_req, px->be_counters.denied_resp,
3590 px->be_counters.failed_conns, px->be_counters.failed_resp,
3591 px->be_counters.retries, px->be_counters.redispatches,
3592 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3593 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3594 px->srv_act, px->srv_bck,
3595 px->down_trans, (int)(now.tv_sec - px->last_change),
3596 px->srv?be_downtime(px):0,
3597 relative_pid, px->uuid,
3598 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3599 read_freq_ctr(&px->be_sess_per_sec),
3600 px->be_counters.sps_max);
3601
3602 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3603 if (px->mode == PR_MODE_HTTP) {
3604 for (i=1; i<6; i++)
3605 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3606 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3607 }
3608 else
3609 chunk_appendf(&trash, ",,,,,,");
3610
3611 /* failed health analyses */
3612 chunk_appendf(&trash, ",");
3613
3614 /* requests : req_rate, req_rate_max, req_tot, */
3615 chunk_appendf(&trash, ",,,");
3616
3617 /* errors: cli_aborts, srv_aborts */
3618 chunk_appendf(&trash, "%lld,%lld,",
3619 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3620
3621 /* compression: in, out, bypassed */
3622 chunk_appendf(&trash, "%lld,%lld,%lld,",
3623 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3624
3625 /* compression: comp_rsp */
3626 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3627
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003628 /* lastsess, last_chk, last_agt, */
3629 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003630
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003631 /* qtime, ctime, rtime, ttime, */
3632 chunk_appendf(&trash, "%u,%u,%u,%u,",
3633 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3634 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3635 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3636 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3637
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003638 /* finish with EOL */
3639 chunk_appendf(&trash, "\n");
3640 }
3641 return 1;
3642}
3643
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003644/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003645 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003646 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003647 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003648static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003649{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003650 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003651 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3652
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003653 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003654 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003655
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003656 /* 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 +02003657 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003658 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003659 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003660 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003661 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003662 }
3663
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003664 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05003665 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003666 }
3667
3668 /* print a new table */
3669 chunk_appendf(&trash,
3670 "<table class=\"tbl\" width=\"100%%\">\n"
3671 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003672 "<th class=\"pxname\" width=\"10%%\">");
3673
3674 chunk_appendf(&trash,
3675 "<a name=\"%s\"></a>%s"
3676 "<a class=px href=\"#%s\">%s</a>",
3677 px->id,
3678 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3679 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003680
3681 if (uri->flags & ST_SHLGNDS) {
3682 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003683 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003684 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3685 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003686 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003687 }
3688
3689 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003690 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003691 "<th class=\"%s\" width=\"90%%\">%s</th>"
3692 "</tr>\n"
3693 "</table>\n"
3694 "<table class=\"tbl\" width=\"100%%\">\n"
3695 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003696 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3697 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3698
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003699 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003700 /* Column heading for Enable or Disable server */
3701 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003702 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003703
3704 chunk_appendf(&trash,
3705 "<th rowspan=2></th>"
3706 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003707 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003708 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3709 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3710 "<th colspan=9>Server</th>"
3711 "</tr>\n"
3712 "<tr class=\"titre\">"
3713 "<th>Cur</th><th>Max</th><th>Limit</th>"
3714 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003715 "<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 +01003716 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3717 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3718 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3719 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3720 "<th>Thrtle</th>\n"
3721 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003722}
3723
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003724/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003725 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003726 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003727static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003728{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003729 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003730 chunk_appendf(&trash, "</table>");
3731
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003732 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003733 /* close the form used to enable/disable this proxy servers */
3734 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003735 "Choose the action to perform on the checked servers : "
3736 "<select name=action>"
3737 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003738 "<option value=\"ready\">Set state to READY</option>"
3739 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02003740 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003741 "<option value=\"dhlth\">Health: disable checks</option>"
3742 "<option value=\"ehlth\">Health: enable checks</option>"
3743 "<option value=\"hrunn\">Health: force UP</option>"
3744 "<option value=\"hnolb\">Health: force NOLB</option>"
3745 "<option value=\"hdown\">Health: force DOWN</option>"
3746 "<option value=\"dagent\">Agent: disable checks</option>"
3747 "<option value=\"eagent\">Agent: enable checks</option>"
3748 "<option value=\"arunn\">Agent: force UP</option>"
3749 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003750 "<option value=\"shutdown\">Kill Sessions</option>"
3751 "</select>"
3752 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3753 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3754 "</form>",
3755 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003756 }
3757
3758 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003759}
Willy Tarreau91861262007-10-17 17:06:05 +02003760
3761/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003762 * Dumps statistics for a proxy. The output is sent to the stream interface's
3763 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3764 * buffer space, or non-zero if everything completed. This function is used
3765 * both by the CLI and the HTTP entry points, and is able to dump the output
3766 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003767 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003768static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003769{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003770 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02003771 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003772 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02003773 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003774 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003775
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003776 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003777
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003778 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003779 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003780 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003781 if (uri && uri->scope) {
3782 /* we have a limited scope, we have to check the proxy name */
3783 struct stat_scope *scope;
3784 int len;
3785
3786 len = strlen(px->id);
3787 scope = uri->scope;
3788
3789 while (scope) {
3790 /* match exact proxy name */
3791 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3792 break;
3793
3794 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003795 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003796 break;
3797 scope = scope->next;
3798 }
3799
3800 /* proxy name not found : don't dump anything */
3801 if (scope == NULL)
3802 return 1;
3803 }
3804
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003805 /* if the user has requested a limited output and the proxy
3806 * name does not match, skip it.
3807 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003808 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003809 strnistr(px->id, strlen(px->id), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003810 return 1;
3811
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003812 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3813 (appctx->ctx.stats.iid != -1) &&
3814 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003815 return 1;
3816
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003817 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003818 /* fall through */
3819
Willy Tarreau295a8372011-03-10 11:25:07 +01003820 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003821 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003822 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003823 if (bi_putchk(rep, &trash) == -1) {
3824 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003825 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003826 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003827 }
Willy Tarreau91861262007-10-17 17:06:05 +02003828
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003829 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003830 /* fall through */
3831
Willy Tarreau295a8372011-03-10 11:25:07 +01003832 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003833 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003834 if (stats_dump_fe_stats(si, px)) {
3835 if (bi_putchk(rep, &trash) == -1) {
3836 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau91861262007-10-17 17:06:05 +02003837 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003838 }
3839 }
Willy Tarreau91861262007-10-17 17:06:05 +02003840
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003841 appctx->ctx.stats.l = px->conf.listeners.n;
3842 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003843 /* fall through */
3844
Willy Tarreau295a8372011-03-10 11:25:07 +01003845 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003846 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003847 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003848 if (buffer_almost_full(rep->buf)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01003849 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003850 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003851 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003852
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003853 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003854 if (!l->counters)
3855 continue;
3856
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003857 if (appctx->ctx.stats.flags & STAT_BOUND) {
3858 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003859 break;
3860
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003861 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003862 continue;
3863 }
3864
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003865 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003866 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
3867 if (bi_putchk(rep, &trash) == -1) {
3868 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003869 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003870 }
3871 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003872 }
3873
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003874 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3875 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003876 /* fall through */
3877
Willy Tarreau295a8372011-03-10 11:25:07 +01003878 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003879 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003880 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003881 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09003882 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02003883
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003884 if (buffer_almost_full(rep->buf)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01003885 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003886 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003887 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003888
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003889 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003890
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003891 if (appctx->ctx.stats.flags & STAT_BOUND) {
3892 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003893 break;
3894
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003895 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003896 continue;
3897 }
3898
Willy Tarreau32091232014-05-16 13:52:00 +02003899 svs = sv;
3900 while (svs->track)
3901 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003902
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003903 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003904 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09003905 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003906 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09003907 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
3908 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09003909 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09003910 sv_colour = SRV_STATS_COLOUR_UP;
3911 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01003912
Simon Horman4463d192015-04-23 14:51:28 +09003913 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
3914 sv_colour = SRV_STATS_COLOUR_DRAINING;
3915
3916 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09003917 if (svs->agent.state & CHK_ST_ENABLED)
3918 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
3919 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09003920 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09003921 else
Simon Horman4d2eab62015-04-23 14:51:26 +09003922 sv_state = SRV_STATS_STATE_DRAIN;
3923 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003924
Simon Horman837bfa72015-04-23 14:51:27 +09003925 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003926 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09003927 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
3928 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01003929 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003930 else if (sv->state == SRV_ST_STOPPING) {
3931 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09003932 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003933 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09003934 sv_colour = SRV_STATS_COLOUR_NOLB;
3935 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09003936 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09003937 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
3938 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003939 }
3940 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09003941 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003942 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09003943 sv_colour = SRV_STATS_COLOUR_DOWN;
3944 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003945 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09003946 sv_colour = SRV_STATS_COLOUR_DOWN;
3947 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003948 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09003949 sv_colour = SRV_STATS_COLOUR_GOING_UP;
3950 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09003951 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09003952 sv_colour = SRV_STATS_COLOUR_DOWN;
3953 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003954 }
Willy Tarreau91861262007-10-17 17:06:05 +02003955
Willy Tarreau9638efa2014-05-23 11:19:57 +02003956 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003957 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003958 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003959 continue;
3960 }
3961
Simon Horman837bfa72015-04-23 14:51:27 +09003962 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01003963 if (bi_putchk(rep, &trash) == -1) {
3964 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003965 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003966 }
3967 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003968 } /* 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 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003975 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
3976 if (bi_putchk(rep, &trash) == -1) {
3977 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003978 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003979 }
3980 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003981
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003982 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003983 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003984
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003985 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003986 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003987 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003988 if (bi_putchk(rep, &trash) == -1) {
3989 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003990 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003991 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003992 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003993
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003994 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003995 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003996
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003997 case STAT_PX_ST_FIN:
3998 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003999
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004000 default:
4001 /* unknown state, we should put an abort() here ! */
4002 return 1;
4003 }
4004}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004005
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004006/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4007 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004008 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004009static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004010{
4011 /* WARNING! This must fit in the first buffer !!! */
4012 chunk_appendf(&trash,
4013 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4014 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4015 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4016 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4017 "<style type=\"text/css\"><!--\n"
4018 "body {"
4019 " font-family: arial, helvetica, sans-serif;"
4020 " font-size: 12px;"
4021 " font-weight: normal;"
4022 " color: black;"
4023 " background: white;"
4024 "}\n"
4025 "th,td {"
4026 " font-size: 10px;"
4027 "}\n"
4028 "h1 {"
4029 " font-size: x-large;"
4030 " margin-bottom: 0.5em;"
4031 "}\n"
4032 "h2 {"
4033 " font-family: helvetica, arial;"
4034 " font-size: x-large;"
4035 " font-weight: bold;"
4036 " font-style: italic;"
4037 " color: #6020a0;"
4038 " margin-top: 0em;"
4039 " margin-bottom: 0em;"
4040 "}\n"
4041 "h3 {"
4042 " font-family: helvetica, arial;"
4043 " font-size: 16px;"
4044 " font-weight: bold;"
4045 " color: #b00040;"
4046 " background: #e8e8d0;"
4047 " margin-top: 0em;"
4048 " margin-bottom: 0em;"
4049 "}\n"
4050 "li {"
4051 " margin-top: 0.25em;"
4052 " margin-right: 2em;"
4053 "}\n"
4054 ".hr {margin-top: 0.25em;"
4055 " border-color: black;"
4056 " border-bottom-style: solid;"
4057 "}\n"
4058 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4059 ".total {background: #20D0D0;color: #ffff80;}\n"
4060 ".frontend {background: #e8e8d0;}\n"
4061 ".socket {background: #d0d0d0;}\n"
4062 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004063 ".active_down {background: #ff9090;}\n"
4064 ".active_going_up {background: #ffd020;}\n"
4065 ".active_going_down {background: #ffffa0;}\n"
4066 ".active_up {background: #c0ffc0;}\n"
4067 ".active_nolb {background: #20a0ff;}\n"
4068 ".active_draining {background: #20a0FF;}\n"
4069 ".active_no_check {background: #e0e0e0;}\n"
4070 ".backup_down {background: #ff9090;}\n"
4071 ".backup_going_up {background: #ff80ff;}\n"
4072 ".backup_going_down {background: #c060ff;}\n"
4073 ".backup_up {background: #b0d0ff;}\n"
4074 ".backup_nolb {background: #90b0e0;}\n"
4075 ".backup_draining {background: #cc9900;}\n"
4076 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004077 ".maintain {background: #c07820;}\n"
4078 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4079 "\n"
4080 "a.px:link {color: #ffff40; text-decoration: none;}"
4081 "a.px:visited {color: #ffff40; text-decoration: none;}"
4082 "a.px:hover {color: #ffffff; text-decoration: none;}"
4083 "a.lfsb:link {color: #000000; text-decoration: none;}"
4084 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4085 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4086 "\n"
4087 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4088 "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"
4089 "table.tbl td.ac { text-align: center;}\n"
4090 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4091 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4092 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4093 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4094 "\n"
4095 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4096 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4097 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004098 "table.det { border-collapse: collapse; border-style: none; }\n"
4099 "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 +01004100 "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 +01004101 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004102 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004103 " display:block;\n"
4104 " visibility:hidden;\n"
4105 " z-index:2147483647;\n"
4106 " position:absolute;\n"
4107 " padding:2px 4px 3px;\n"
4108 " background:#f0f060; color:#000000;\n"
4109 " border:1px solid #7040c0;\n"
4110 " white-space:nowrap;\n"
4111 " font-style:normal;font-size:11px;font-weight:normal;\n"
4112 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4113 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4114 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004115 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004116 "-->\n"
4117 "</style></head>\n",
4118 (uri->flags & ST_SHNODE) ? " on " : "",
4119 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4120 );
4121}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004122
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004123/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004124 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004125 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004126 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004127static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004128{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004129 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004130 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004131 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004132
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004133 /* WARNING! this has to fit the first packet too.
4134 * We are around 3.5 kB, add adding entries will
4135 * become tricky if we want to support 4kB buffers !
4136 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004137 chunk_appendf(&trash,
4138 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4139 PRODUCT_NAME "%s</a></h1>\n"
4140 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4141 "<hr width=\"100%%\" class=\"hr\">\n"
4142 "<h3>&gt; General process information</h3>\n"
4143 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4144 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4145 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4146 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4147 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4148 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4149 "Running tasks: %d/%d; idle = %d %%<br>\n"
4150 "</td><td align=\"center\" nowrap>\n"
4151 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004152 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4153 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004154 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004155 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4156 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004157 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004158 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4159 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004160 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004161 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4162 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004163 "</tr><tr>\n"
4164 "<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 -07004165 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004166 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004167 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004168 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004169 "</td>"
4170 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4171 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4172 "",
4173 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4174 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4175 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4176 (uri->flags & ST_SHDESC) ? ": " : "",
4177 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4178 pid, relative_pid, global.nbproc,
4179 up / 86400, (up % 86400) / 3600,
4180 (up % 3600) / 60, (up % 60),
4181 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4182 global.rlimit_memmax ? " MB" : "",
4183 global.rlimit_nofile,
4184 global.maxsock, global.maxconn, global.maxpipes,
4185 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4186 run_queue_cur, nb_tasks_cur, idle_pct
4187 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004188
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004189 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004190 memcpy(scope_txt, bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004191 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004192
4193 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004194 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004195 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004196 STAT_SCOPE_TXT_MAXLEN);
4197
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004198 /* 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 +02004199 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004200 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004201 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004202 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004203 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004204 }
4205
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004206 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004207 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004208 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004209 uri->uri_prefix,
4210 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004211 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004212 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004213 else
4214 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004215 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004216 uri->uri_prefix,
4217 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004218 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004219 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004220
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004221 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004222 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004223 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004224 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004225 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004226 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004227 "",
4228 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004229 else
4230 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004231 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004232 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004233 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004234 ";norefresh",
4235 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004236 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004237
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004238 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004239 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004240 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004241 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4242 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004243 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004244
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004245 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004246 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004247 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004248 (uri->refresh > 0) ? ";norefresh" : "",
4249 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004250
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004251 chunk_appendf(&trash,
4252 "</ul></td>"
4253 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4254 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4255 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4256 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4257 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4258 "</ul>"
4259 "</td>"
4260 "</tr></table>\n"
4261 ""
4262 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004263
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004264 if (appctx->ctx.stats.st_code) {
4265 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004266 case STAT_STATUS_DONE:
4267 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004268 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004269 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004270 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004271 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004272 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4273 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004274 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004275 break;
4276 case STAT_STATUS_NONE:
4277 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004278 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004279 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004280 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004281 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004282 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4283 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004284 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004285 break;
4286 case STAT_STATUS_PART:
4287 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004288 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004289 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004290 "Action partially processed.<br>"
4291 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004292 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004293 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4294 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004295 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004296 break;
4297 case STAT_STATUS_ERRP:
4298 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004299 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004300 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004301 "Action not processed because of invalid parameters."
4302 "<ul>"
4303 "<li>The action is maybe unknown.</li>"
4304 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4305 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4306 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004307 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004308 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4309 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004310 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004311 break;
4312 case STAT_STATUS_EXCD:
4313 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004314 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004315 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004316 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4317 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004318 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004319 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4320 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004321 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004322 break;
4323 case STAT_STATUS_DENY:
4324 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004325 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004326 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004327 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004328 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004329 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4330 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004331 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004332 break;
4333 default:
4334 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004335 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004336 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004337 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004338 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004339 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4340 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004341 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004342 }
4343 chunk_appendf(&trash, "<p>\n");
4344 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004345}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004346
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004347/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4348 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004349 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004350static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004351{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004352 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004353}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004354
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004355/* This function dumps statistics onto the stream interface's read buffer in
4356 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004357 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4358 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
Willy Tarreau87b09662015-04-03 00:22:06 +02004359 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004360 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004361 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004362static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004363{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004364 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004365 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004366 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004367
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004368 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004369
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004370 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004371 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004372 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004373 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004374
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004375 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004376 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004377 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004378 else
4379 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004380
Willy Tarreaubc18da12015-03-13 14:00:47 +01004381 if (bi_putchk(rep, &trash) == -1) {
4382 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004383 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004384 }
Willy Tarreauae526782010-03-04 20:34:23 +01004385
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004386 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004387 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004388
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004389 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004390 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004391 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004392 if (bi_putchk(rep, &trash) == -1) {
4393 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau91861262007-10-17 17:06:05 +02004394 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004395 }
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)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004407 si->flags |= SI_FL_WAIT_ROOM;
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 Tarreaubc18da12015-03-13 14:00:47 +01004428 if (bi_putchk(rep, &trash) == -1) {
4429 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004430 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004431 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004432 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004433
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004434 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004435 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004436
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004437 case STAT_ST_FIN:
4438 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004439
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004440 default:
4441 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004442 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004443 return -1;
4444 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004445}
Willy Tarreauae526782010-03-04 20:34:23 +01004446
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004447/* We reached the stats page through a POST request. The appctx is
4448 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004449 * Parse the posted data and enable/disable servers if necessary.
4450 * Returns 1 if request was parsed or zero if it needs more data.
4451 */
4452static int stats_process_http_post(struct stream_interface *si)
4453{
Willy Tarreau87b09662015-04-03 00:22:06 +02004454 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004455 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004456
4457 struct proxy *px = NULL;
4458 struct server *sv = NULL;
4459
4460 char key[LINESIZE];
4461 int action = ST_ADM_ACTION_NONE;
4462 int reprocess = 0;
4463
4464 int total_servers = 0;
4465 int altered_servers = 0;
4466
4467 char *first_param, *cur_param, *next_param, *end_params;
4468 char *st_cur_param = NULL;
4469 char *st_next_param = NULL;
4470
4471 struct chunk *temp;
4472 int reql;
4473
4474 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02004475 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004476 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004477 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004478 goto out;
4479 }
4480
Willy Tarreaueee5b512015-04-03 23:46:31 +02004481 reql = bo_getblk(si_oc(si), temp->str, s->txn->req.body_len, s->txn->req.eoh + 2);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004482 if (reql <= 0) {
4483 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004484 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004485 return 0;
4486 }
4487
4488 first_param = temp->str;
4489 end_params = temp->str + reql;
4490 cur_param = next_param = end_params;
4491 *end_params = '\0';
4492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004493 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004494
4495 /*
4496 * Parse the parameters in reverse order to only store the last value.
4497 * From the html form, the backend and the action are at the end.
4498 */
4499 while (cur_param > first_param) {
4500 char *value;
4501 int poffset, plen;
4502
4503 cur_param--;
4504
4505 if ((*cur_param == '&') || (cur_param == first_param)) {
4506 reprocess_servers:
4507 /* Parse the key */
4508 poffset = (cur_param != first_param ? 1 : 0);
4509 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4510 if ((plen > 0) && (plen <= sizeof(key))) {
4511 strncpy(key, cur_param + poffset, plen);
4512 key[plen - 1] = '\0';
4513 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004514 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004515 goto out;
4516 }
4517
4518 /* Parse the value */
4519 value = key;
4520 while (*value != '\0' && *value != '=') {
4521 value++;
4522 }
4523 if (*value == '=') {
4524 /* Ok, a value is found, we can mark the end of the key */
4525 *value++ = '\0';
4526 }
4527 if (url_decode(key) < 0 || url_decode(value) < 0)
4528 break;
4529
4530 /* Now we can check the key to see what to do */
4531 if (!px && (strcmp(key, "b") == 0)) {
4532 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4533 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004534 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004535 goto out;
4536 }
4537 }
4538 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004539 if (strcmp(value, "ready") == 0) {
4540 action = ST_ADM_ACTION_READY;
4541 }
4542 else if (strcmp(value, "drain") == 0) {
4543 action = ST_ADM_ACTION_DRAIN;
4544 }
4545 else if (strcmp(value, "maint") == 0) {
4546 action = ST_ADM_ACTION_MAINT;
4547 }
4548 else if (strcmp(value, "shutdown") == 0) {
4549 action = ST_ADM_ACTION_SHUTDOWN;
4550 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004551 else if (strcmp(value, "dhlth") == 0) {
4552 action = ST_ADM_ACTION_DHLTH;
4553 }
4554 else if (strcmp(value, "ehlth") == 0) {
4555 action = ST_ADM_ACTION_EHLTH;
4556 }
4557 else if (strcmp(value, "hrunn") == 0) {
4558 action = ST_ADM_ACTION_HRUNN;
4559 }
4560 else if (strcmp(value, "hnolb") == 0) {
4561 action = ST_ADM_ACTION_HNOLB;
4562 }
4563 else if (strcmp(value, "hdown") == 0) {
4564 action = ST_ADM_ACTION_HDOWN;
4565 }
4566 else if (strcmp(value, "dagent") == 0) {
4567 action = ST_ADM_ACTION_DAGENT;
4568 }
4569 else if (strcmp(value, "eagent") == 0) {
4570 action = ST_ADM_ACTION_EAGENT;
4571 }
4572 else if (strcmp(value, "arunn") == 0) {
4573 action = ST_ADM_ACTION_ARUNN;
4574 }
4575 else if (strcmp(value, "adown") == 0) {
4576 action = ST_ADM_ACTION_ADOWN;
4577 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004578 /* else these are the old supported methods */
4579 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004580 action = ST_ADM_ACTION_DISABLE;
4581 }
4582 else if (strcmp(value, "enable") == 0) {
4583 action = ST_ADM_ACTION_ENABLE;
4584 }
4585 else if (strcmp(value, "stop") == 0) {
4586 action = ST_ADM_ACTION_STOP;
4587 }
4588 else if (strcmp(value, "start") == 0) {
4589 action = ST_ADM_ACTION_START;
4590 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004591 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004592 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004593 goto out;
4594 }
4595 }
4596 else if (strcmp(key, "s") == 0) {
4597 if (!(px && action)) {
4598 /*
4599 * Indicates that we'll need to reprocess the parameters
4600 * as soon as backend and action are known
4601 */
4602 if (!reprocess) {
4603 st_cur_param = cur_param;
4604 st_next_param = next_param;
4605 }
4606 reprocess = 1;
4607 }
4608 else if ((sv = findserver(px, value)) != NULL) {
4609 switch (action) {
4610 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004611 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004612 altered_servers++;
4613 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004614 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004615 }
4616 break;
4617 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004618 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004619 altered_servers++;
4620 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004621 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004622 }
4623 break;
4624 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004625 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4626 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4627 altered_servers++;
4628 total_servers++;
4629 }
4630 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004631 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004632 if (sv->admin & SRV_ADMF_FDRAIN) {
4633 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4634 altered_servers++;
4635 total_servers++;
4636 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004637 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004638 case ST_ADM_ACTION_DHLTH:
4639 if (sv->check.state & CHK_ST_CONFIGURED) {
4640 sv->check.state &= ~CHK_ST_ENABLED;
4641 altered_servers++;
4642 total_servers++;
4643 }
4644 break;
4645 case ST_ADM_ACTION_EHLTH:
4646 if (sv->check.state & CHK_ST_CONFIGURED) {
4647 sv->check.state |= CHK_ST_ENABLED;
4648 altered_servers++;
4649 total_servers++;
4650 }
4651 break;
4652 case ST_ADM_ACTION_HRUNN:
4653 if (!(sv->track)) {
4654 sv->check.health = sv->check.rise + sv->check.fall - 1;
4655 srv_set_running(sv, "changed from Web interface");
4656 altered_servers++;
4657 total_servers++;
4658 }
4659 break;
4660 case ST_ADM_ACTION_HNOLB:
4661 if (!(sv->track)) {
4662 sv->check.health = sv->check.rise + sv->check.fall - 1;
4663 srv_set_stopping(sv, "changed from Web interface");
4664 altered_servers++;
4665 total_servers++;
4666 }
4667 break;
4668 case ST_ADM_ACTION_HDOWN:
4669 if (!(sv->track)) {
4670 sv->check.health = 0;
4671 srv_set_stopped(sv, "changed from Web interface");
4672 altered_servers++;
4673 total_servers++;
4674 }
4675 break;
4676 case ST_ADM_ACTION_DAGENT:
4677 if (sv->agent.state & CHK_ST_CONFIGURED) {
4678 sv->agent.state &= ~CHK_ST_ENABLED;
4679 altered_servers++;
4680 total_servers++;
4681 }
4682 break;
4683 case ST_ADM_ACTION_EAGENT:
4684 if (sv->agent.state & CHK_ST_CONFIGURED) {
4685 sv->agent.state |= CHK_ST_ENABLED;
4686 altered_servers++;
4687 total_servers++;
4688 }
4689 break;
4690 case ST_ADM_ACTION_ARUNN:
4691 if (sv->agent.state & CHK_ST_ENABLED) {
4692 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4693 srv_set_running(sv, "changed from Web interface");
4694 altered_servers++;
4695 total_servers++;
4696 }
4697 break;
4698 case ST_ADM_ACTION_ADOWN:
4699 if (sv->agent.state & CHK_ST_ENABLED) {
4700 sv->agent.health = 0;
4701 srv_set_stopped(sv, "changed from Web interface");
4702 altered_servers++;
4703 total_servers++;
4704 }
4705 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004706 case ST_ADM_ACTION_READY:
4707 srv_adm_set_ready(sv);
4708 altered_servers++;
4709 total_servers++;
4710 break;
4711 case ST_ADM_ACTION_DRAIN:
4712 srv_adm_set_drain(sv);
4713 altered_servers++;
4714 total_servers++;
4715 break;
4716 case ST_ADM_ACTION_MAINT:
4717 srv_adm_set_maint(sv);
4718 altered_servers++;
4719 total_servers++;
4720 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004721 case ST_ADM_ACTION_SHUTDOWN:
4722 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004723 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004724
4725 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4726 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004727 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004728
4729 altered_servers++;
4730 total_servers++;
4731 }
4732 break;
4733 }
4734 } else {
4735 /* the server name is unknown or ambiguous (duplicate names) */
4736 total_servers++;
4737 }
4738 }
4739 if (reprocess && px && action) {
4740 /* Now, we know the backend and the action chosen by the user.
4741 * We can safely restart from the first server parameter
4742 * to reprocess them
4743 */
4744 cur_param = st_cur_param;
4745 next_param = st_next_param;
4746 reprocess = 0;
4747 goto reprocess_servers;
4748 }
4749
4750 next_param = cur_param;
4751 }
4752 }
4753
4754 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004755 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004756 }
4757 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004758 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004759 }
4760 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004761 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004762 }
4763 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004764 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004765 }
4766 out:
4767 return 1;
4768}
4769
4770
4771static int stats_send_http_headers(struct stream_interface *si)
4772{
Willy Tarreau87b09662015-04-03 00:22:06 +02004773 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004774 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004775 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004776
4777 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004778 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004779 "Cache-Control: no-cache\r\n"
4780 "Connection: close\r\n"
4781 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004782 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004783
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004784 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004785 chunk_appendf(&trash, "Refresh: %d\r\n",
4786 uri->refresh);
4787
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004788 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4789
4790 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4791 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4792 else
4793 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004794
Willy Tarreaueee5b512015-04-03 23:46:31 +02004795 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004796 s->logs.tv_request = now;
4797
Willy Tarreaubc18da12015-03-13 14:00:47 +01004798 if (bi_putchk(si_ic(si), &trash) == -1) {
4799 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004800 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004801 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004802
4803 return 1;
4804}
4805
4806static int stats_send_http_redirect(struct stream_interface *si)
4807{
4808 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02004809 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004810 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004811 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004812
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004813 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004814 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004815 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004816 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004817 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si_ob(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004818 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004819 }
4820
4821 /* We don't want to land on the posted stats page because a refresh will
4822 * repost the data. We don't want this to happen on accident so we redirect
4823 * the browse to the stats page with a GET.
4824 */
4825 chunk_printf(&trash,
4826 "HTTP/1.1 303 See Other\r\n"
4827 "Cache-Control: no-cache\r\n"
4828 "Content-Type: text/plain\r\n"
4829 "Connection: close\r\n"
4830 "Location: %s;st=%s%s%s%s\r\n"
4831 "\r\n",
4832 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004833 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4834 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4835 stat_status_codes[appctx->ctx.stats.st_code]) ?
4836 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004837 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004838 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4839 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004840 scope_txt);
4841
Willy Tarreaueee5b512015-04-03 23:46:31 +02004842 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004843 s->logs.tv_request = now;
4844
Willy Tarreaubc18da12015-03-13 14:00:47 +01004845 if (bi_putchk(si_ic(si), &trash) == -1) {
4846 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004847 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004848 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004849
4850 return 1;
4851}
4852
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004853/* This I/O handler runs as an applet embedded in a stream interface. It is
4854 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004855 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004856 * automatically unregisters itself once transfer is complete.
4857 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02004858static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004859{
Willy Tarreau00a37f02015-04-13 12:05:19 +02004860 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02004861 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004862 struct channel *req = si_oc(si);
4863 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01004864
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004865 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4866 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004867
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004868 /* check that the output is not closed */
4869 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004870 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004871
Willy Tarreau347a35d2013-11-22 17:51:09 +01004872 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004873 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004874 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02004875 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004876 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004877 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004878 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004879 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004880 }
4881
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004882 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004883 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004884 unsigned int data_len;
4885 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004886 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004887
4888 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4889 /* One difficulty we're facing is that we must prevent
4890 * the input data from being automatically forwarded to
4891 * the output area. For this, we temporarily disable
4892 * forwarding on the channel.
4893 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004894 last_fwd = si_ic(si)->to_forward;
4895 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004896 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004897 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004898 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004899 si_ic(si)->to_forward = last_fwd;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004900 goto fail;
4901 }
4902 }
4903
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004904 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004905 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004906 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004907
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004908 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004909
4910 /* Now we must either adjust or remove the chunk size. This is
4911 * not easy because the chunk size might wrap at the end of the
4912 * buffer, so we pretend we have nothing in the buffer, we write
4913 * the size, then restore the buffer's contents. Note that we can
4914 * only do that because no forwarding is scheduled on the stats
4915 * applet.
4916 */
4917 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004918 si_ic(si)->total -= (last_len - prev_len);
4919 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004920
4921 if (last_len != data_len) {
4922 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01004923 if (bi_putchk(si_ic(si), &trash) == -1)
4924 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004925
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004926 si_ic(si)->total += (last_len - data_len);
4927 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004928 }
4929 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004930 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004931 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004932 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004933
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004934 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004935 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004936 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004937 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004938 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004939 }
4940
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004941 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004942 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004943 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004944 }
4945
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004946 if (appctx->st0 == STAT_HTTP_DONE) {
4947 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4948 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01004949 if (bi_putchk(si_ic(si), &trash) == -1) {
4950 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004951 goto fail;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004952 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004953 }
4954 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004955 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004956 res->flags |= CF_READ_NULL;
4957 si_shutr(si);
4958 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004959
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004960 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4961 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004962
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004963 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004964 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4965 si_shutr(si);
4966 res->flags |= CF_READ_NULL;
4967 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004968 }
Willy Tarreau91861262007-10-17 17:06:05 +02004969
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004970 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004971 /* update all other flags and resync with the other side */
4972 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004973
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004974 /* we don't want to expire timeouts while we're processing requests */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004975 si_ic(si)->rex = TICK_ETERNITY;
4976 si_oc(si)->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004977
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004978 out:
4979 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4980 /* check that we have released everything then unregister */
4981 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004982 }
4983}
4984
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004985
Willy Tarreau909d5172012-11-26 03:04:41 +01004986static inline const char *get_conn_ctrl_name(const struct connection *conn)
4987{
Willy Tarreau3c728722014-01-23 13:50:42 +01004988 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004989 return "NONE";
4990 return conn->ctrl->name;
4991}
4992
4993static inline const char *get_conn_xprt_name(const struct connection *conn)
4994{
4995 static char ptr[17];
4996
Willy Tarreauaad69382014-01-23 14:21:42 +01004997 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004998 return "NONE";
4999
5000 if (conn->xprt == &raw_sock)
5001 return "RAW";
5002
5003#ifdef USE_OPENSSL
5004 if (conn->xprt == &ssl_sock)
5005 return "SSL";
5006#endif
5007 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5008 return ptr;
5009}
5010
5011static inline const char *get_conn_data_name(const struct connection *conn)
5012{
5013 static char ptr[17];
5014
5015 if (!conn->data)
5016 return "NONE";
5017
5018 if (conn->data == &sess_conn_cb)
5019 return "SESS";
5020
5021 if (conn->data == &si_conn_cb)
5022 return "STRM";
5023
5024 if (conn->data == &check_conn_cb)
5025 return "CHCK";
5026
5027 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5028 return ptr;
5029}
5030
Willy Tarreau87b09662015-04-03 00:22:06 +02005031/* This function dumps a complete stream state onto the stream interface's
5032 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005033 * 0 if the output buffer is full and it needs to be called again, otherwise
5034 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005035 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005036static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005037{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005038 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005039 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005040 extern const char *monthname[12];
5041 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005042 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005043 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005044
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005045 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005046
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005047 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005048 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005049 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005050 if (bi_putchk(si_ic(si), &trash) == -1) {
5051 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005052 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005053 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005054 appctx->ctx.sess.uid = 0;
5055 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005056 return 1;
5057 }
5058
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005059 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005060 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005061 appctx->ctx.sess.uid = sess->uniq_id;
5062 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005063 /* fall through */
5064
5065 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005066 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005067 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005068 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005069 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005070 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5071 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005072 sess->uniq_id,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005073 strm_li(sess) && strm_li(sess)->proto->name ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005074
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005075 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005076 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005077 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005078 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005079 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005080 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005081 break;
5082 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005083 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005084 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005085 default:
5086 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005087 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005088 break;
5089 }
5090
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005091 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005092 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005093 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005094
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005095 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005096 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005097 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5098 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5099 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005100
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005101 if (conn)
5102 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005103
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005104 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005105 case AF_INET:
5106 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005107 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005108 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005109 break;
5110 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005111 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005112 break;
5113 default:
5114 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005115 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005116 break;
5117 }
5118
Willy Tarreau50943332011-09-02 17:33:05 +02005119 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005120 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005121 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005122 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005123 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5124 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005125 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005126
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005127 conn = objt_conn(sess->si[1].end);
5128 if (conn)
5129 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005130
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005131 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005132 case AF_INET:
5133 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005134 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005135 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005136 break;
5137 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005138 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005139 break;
5140 default:
5141 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005142 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005143 break;
5144 }
5145
5146 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005147 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005148 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005149 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5150 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005151 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005152 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005153
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005154 if (conn)
5155 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005156
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005157 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005158 case AF_INET:
5159 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005160 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005161 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005162 break;
5163 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005164 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005165 break;
5166 default:
5167 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005168 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005169 break;
5170 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005171
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005172 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005173 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005174 sess->task,
5175 sess->task->state,
5176 sess->task->nice, sess->task->calls,
5177 sess->task->expire ?
5178 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5179 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5180 TICKS_TO_MS(1000)) : "<NEVER>",
5181 task_in_rq(sess->task) ? ", running" : "");
5182
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005183 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005184 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005185 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5186
Willy Tarreaueee5b512015-04-03 23:46:31 +02005187 if (sess->txn)
5188 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005189 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s waiting=%d\n",
Willy Tarreaueee5b512015-04-03 23:46:31 +02005190 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5191 http_msg_state_str(sess->txn->req.msg_state), http_msg_state_str(sess->txn->rsp.msg_state), !LIST_ISEMPTY(&sess->buffer_wait));
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005192
5193 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005194 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005195 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005196 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005197 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005198 obj_type_name(sess->si[0].end),
5199 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005200 sess->si[0].exp ?
5201 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5202 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5203 TICKS_TO_MS(1000)) : "<NEVER>",
5204 sess->si[0].err_type);
5205
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005206 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005207 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005208 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005209 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005210 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005211 obj_type_name(sess->si[1].end),
5212 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005213 sess->si[1].exp ?
5214 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5215 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5216 TICKS_TO_MS(1000)) : "<NEVER>",
5217 sess->si[1].err_type);
5218
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005219 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005220 chunk_appendf(&trash,
5221 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005222 conn,
5223 get_conn_ctrl_name(conn),
5224 get_conn_xprt_name(conn),
5225 get_conn_data_name(conn),
5226 obj_type_name(conn->target),
5227 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005228
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005229 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005230 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005231 conn->flags,
5232 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005233 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005234 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005235 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005236 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005237 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5238 chunk_appendf(&trash,
5239 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5240 tmpctx,
5241 tmpctx->st0,
5242 tmpctx->st1,
5243 tmpctx->st2,
5244 tmpctx->applet->name);
5245 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005246
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005247 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005248 chunk_appendf(&trash,
5249 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005250 conn,
5251 get_conn_ctrl_name(conn),
5252 get_conn_xprt_name(conn),
5253 get_conn_data_name(conn),
5254 obj_type_name(conn->target),
5255 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005256
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005257 chunk_appendf(&trash,
5258 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005259 conn->flags,
5260 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005261 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005262 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005263 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005264 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005265 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5266 chunk_appendf(&trash,
5267 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5268 tmpctx,
5269 tmpctx->st0,
5270 tmpctx->st1,
5271 tmpctx->st2,
5272 tmpctx->applet->name);
5273 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005274
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005275 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005276 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005277 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005278 &sess->req,
5279 sess->req.flags, sess->req.analysers,
5280 sess->req.pipe ? sess->req.pipe->data : 0,
5281 sess->req.to_forward, sess->req.total,
5282 sess->req.analyse_exp ?
5283 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005284 TICKS_TO_MS(1000)) : "<NEVER>");
5285
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005286 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005287 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005288 sess->req.rex ?
5289 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005290 TICKS_TO_MS(1000)) : "<NEVER>");
5291
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005292 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005293 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005294 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005295 sess->req.wex ?
5296 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005297 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005298 sess->req.buf,
5299 sess->req.buf->data, sess->req.buf->o,
5300 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005301 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005302 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005303
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005304 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005305 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005306 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005307 &sess->res,
5308 sess->res.flags, sess->res.analysers,
5309 sess->res.pipe ? sess->res.pipe->data : 0,
5310 sess->res.to_forward, sess->res.total,
5311 sess->res.analyse_exp ?
5312 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005313 TICKS_TO_MS(1000)) : "<NEVER>");
5314
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005315 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005316 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005317 sess->res.rex ?
5318 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005319 TICKS_TO_MS(1000)) : "<NEVER>");
5320
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005321 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005322 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005323 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005324 sess->res.wex ?
5325 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005326 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005327 sess->res.buf,
5328 sess->res.buf->data, sess->res.buf->o,
5329 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005330 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005331 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005332
Willy Tarreaubc18da12015-03-13 14:00:47 +01005333 if (bi_putchk(si_ic(si), &trash) == -1) {
5334 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005335 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005336 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005337
5338 /* use other states to dump the contents */
5339 }
5340 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005341 appctx->ctx.sess.uid = 0;
5342 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005343 return 1;
5344}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005345
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005346static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005347{
5348 struct appctx *appctx = __objt_appctx(si->end);
5349
5350 switch (appctx->st2) {
5351 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005352 /* Display the column headers. If the message cannot be sent,
5353 * quit the fucntion with returning 0. The function is called
5354 * later and restart at the state "STAT_ST_INIT".
5355 */
5356 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005357 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005358 if (bi_putchk(si_ic(si), &trash) == -1) {
5359 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005360 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005361 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005362
5363 /* Now, we start the browsing of the references lists.
5364 * Note that the following call to LIST_ELEM return bad pointer. The only
5365 * avalaible field of this pointer is <list>. It is used with the function
5366 * pat_list_get_next() for retruning the first avalaible entry
5367 */
5368 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5369 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5370 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005371 appctx->st2 = STAT_ST_LIST;
5372 /* fall through */
5373
5374 case STAT_ST_LIST:
5375 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005376 chunk_reset(&trash);
5377
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005378 /* Build messages. If the reference is used by another category than
5379 * the listed categorie, display the information in the massage.
5380 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005381 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005382 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5383 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005384
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005385 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005386 /* let's try again later from this stream. We add ourselves into
5387 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005388 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005389 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005390 return 0;
5391 }
5392
5393 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005394 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5395 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005396 }
5397
5398 appctx->st2 = STAT_ST_FIN;
5399 /* fall through */
5400
5401 default:
5402 appctx->st2 = STAT_ST_FIN;
5403 return 1;
5404 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005405 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005406}
5407
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005408static int stats_map_lookup(struct stream_interface *si)
5409{
5410 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005411 struct sample sample;
5412 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005413 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005414
5415 switch (appctx->st2) {
5416 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005417 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005418 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005419 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005420 appctx->st2 = STAT_ST_LIST;
5421 /* fall through */
5422
5423 case STAT_ST_LIST:
5424 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005425 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005426 /* initialise chunk to build new message */
5427 chunk_reset(&trash);
5428
5429 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005430 sample.type = SMP_T_STR;
5431 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005432 sample.data.str.len = appctx->ctx.map.chunk.len;
5433 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005434 if (appctx->ctx.map.expr->pat_head->match &&
5435 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005436 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5437 else
5438 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005439
5440 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005441 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5442 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5443 break;
5444 if (match_method >= PAT_MATCH_NUM)
5445 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5446 else
5447 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005448
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005449 /* case sensitive */
5450 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5451 chunk_appendf(&trash, ", case=insensitive");
5452 else
5453 chunk_appendf(&trash, ", case=sensitive");
5454
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005455 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005456 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005457 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5458 chunk_appendf(&trash, ", found=no");
5459 else
5460 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005461 }
5462
5463 /* Display match and match info */
5464 else {
5465 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005466 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5467 chunk_appendf(&trash, ", found=yes");
5468 else
5469 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005470
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005471 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005472 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005473 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005474 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005475 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005476
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005477 /* display pattern */
5478 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5479 if (pat->ref && pat->ref->pattern)
5480 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5481 else
5482 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005483 }
5484 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005485 if (pat->ref && pat->ref->pattern)
5486 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5487 else
5488 chunk_appendf(&trash, ", pattern=unknown");
5489 }
5490
5491 /* display return value */
5492 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5493 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005494 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005495 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005496 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005497 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005498 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005499 }
5500
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005501 chunk_appendf(&trash, "\n");
5502
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005503 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005504 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005505 /* let's try again later from this stream. We add ourselves into
5506 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005507 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005508 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005509 return 0;
5510 }
5511
5512 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005513 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5514 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005515 }
5516
5517 appctx->st2 = STAT_ST_FIN;
5518 /* fall through */
5519
5520 default:
5521 appctx->st2 = STAT_ST_FIN;
5522 free(appctx->ctx.map.chunk.str);
5523 return 1;
5524 }
5525}
5526
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005527static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005528{
5529 struct appctx *appctx = __objt_appctx(si->end);
5530
5531 switch (appctx->st2) {
5532
5533 case STAT_ST_INIT:
5534 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005535 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5536 struct pat_ref_elt *, list);
5537 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5538 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005539 appctx->st2 = STAT_ST_LIST;
5540 /* fall through */
5541
5542 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005543 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005544 chunk_reset(&trash);
5545
5546 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005547 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005548 chunk_appendf(&trash, "%p %s %s\n",
5549 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5550 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005551 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005552 chunk_appendf(&trash, "%p %s\n",
5553 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005554
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005555 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005556 /* let's try again later from this stream. We add ourselves into
5557 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005558 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005559 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005560 return 0;
5561 }
5562
5563 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005564 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5565 struct pat_ref_elt *, list);
5566 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005567 break;
5568 }
5569
5570 appctx->st2 = STAT_ST_FIN;
5571 /* fall through */
5572
5573 default:
5574 appctx->st2 = STAT_ST_FIN;
5575 return 1;
5576 }
5577}
5578
Willy Tarreau87b09662015-04-03 00:22:06 +02005579/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005580 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005581 * to be called again, otherwise non-zero. It is designed to be called
5582 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005583 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005584static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005585{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005586 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005587 struct connection *conn;
5588
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005589 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005590 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02005591 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005592 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005593 if (appctx->st2 == STAT_ST_LIST) {
5594 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5595 LIST_DEL(&appctx->ctx.sess.bref.users);
5596 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005597 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005598 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005599 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005600 }
5601
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005602 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005603
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005604 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005605 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005606 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02005607 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005608 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02005609 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005610 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02005611 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005612 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005613 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02005614 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005615 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005616 /* fall through */
5617
Willy Tarreau295a8372011-03-10 11:25:07 +01005618 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02005619 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005620 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5621 LIST_DEL(&appctx->ctx.sess.bref.users);
5622 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005623 }
5624
5625 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02005626 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005627 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005628 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005629
Willy Tarreau87b09662015-04-03 00:22:06 +02005630 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005631
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005632 if (appctx->ctx.sess.target) {
5633 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005634 goto next_sess;
5635
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005636 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005637 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005638 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005639 return 0;
5640
Willy Tarreau87b09662015-04-03 00:22:06 +02005641 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005642 LIST_DEL(&appctx->ctx.sess.bref.users);
5643 LIST_INIT(&appctx->ctx.sess.bref.users);
5644 if (appctx->ctx.sess.target != (void *)-1) {
5645 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005646 break;
5647 }
5648 else
5649 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005650 }
5651
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005652 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005653 "%p: proto=%s",
5654 curr_sess,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005655 strm_li(curr_sess)->proto->name);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005656
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005657
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005658 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005659 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005660 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005661 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005662 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005663 " src=%s:%d fe=%s be=%s srv=%s",
5664 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005665 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005666 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005667 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005668 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005669 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005670 break;
5671 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005672 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005673 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005674 strm_li(curr_sess)->luid,
5675 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005676 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005677 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005678 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005679 break;
5680 }
5681
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005682 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005683 " ts=%02x age=%s calls=%d",
5684 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005685 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5686 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005687
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005688 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005689 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005690 curr_sess->req.flags,
5691 curr_sess->req.buf->i,
5692 curr_sess->req.analysers,
5693 curr_sess->req.rex ?
5694 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005695 TICKS_TO_MS(1000)) : "");
5696
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005697 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005698 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005699 curr_sess->req.wex ?
5700 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005701 TICKS_TO_MS(1000)) : "");
5702
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005703 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005704 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005705 curr_sess->req.analyse_exp ?
5706 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005707 TICKS_TO_MS(1000)) : "");
5708
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005709 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005710 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005711 curr_sess->res.flags,
5712 curr_sess->res.buf->i,
5713 curr_sess->res.analysers,
5714 curr_sess->res.rex ?
5715 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005716 TICKS_TO_MS(1000)) : "");
5717
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005718 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005719 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005720 curr_sess->res.wex ?
5721 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005722 TICKS_TO_MS(1000)) : "");
5723
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005724 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005725 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005726 curr_sess->res.analyse_exp ?
5727 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005728 TICKS_TO_MS(1000)) : "");
5729
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005730 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005731 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005732 " s0=[%d,%1xh,fd=%d,ex=%s]",
5733 curr_sess->si[0].state,
5734 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005735 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005736 curr_sess->si[0].exp ?
5737 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5738 TICKS_TO_MS(1000)) : "");
5739
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005740 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005741 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005742 " s1=[%d,%1xh,fd=%d,ex=%s]",
5743 curr_sess->si[1].state,
5744 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005745 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005746 curr_sess->si[1].exp ?
5747 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5748 TICKS_TO_MS(1000)) : "");
5749
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005750 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005751 " exp=%s",
5752 curr_sess->task->expire ?
5753 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5754 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005755 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005756 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005757
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005758 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005759
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005760 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005761 /* let's try again later from this stream. We add ourselves into
5762 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005763 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005764 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005765 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005766 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005767 }
5768
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005769 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005770 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005771 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005772
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005773 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005774 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005775 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005776 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005777 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005778 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005779
Willy Tarreaubc18da12015-03-13 14:00:47 +01005780 if (bi_putchk(si_ic(si), &trash) == -1) {
5781 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005782 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005783 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005784
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005785 appctx->ctx.sess.target = NULL;
5786 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005787 return 1;
5788 }
5789
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005790 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005791 /* fall through */
5792
5793 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005794 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005795 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005796 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005797}
5798
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005799/* This is called when the stream interface is closed. For instance, upon an
5800 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02005801 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005802 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005803static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005804{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005805 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5806 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5807 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005808 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005809 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5810 free(appctx->ctx.cli.err);
5811 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005812 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5813 free(appctx->ctx.map.chunk.str);
5814 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005815}
5816
Willy Tarreau20e99322013-04-13 09:22:25 +02005817/* This function is used to either dump tables states (when action is set
5818 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005819 * It returns 0 if the output buffer is full and it needs to be called
5820 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005821 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005822static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005823{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005824 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02005825 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005826 struct ebmb_node *eb;
5827 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005828 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005829 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005830
5831 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005832 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005833 * - STAT_ST_INIT : the first call
5834 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005835 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005836 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005837 * and the entry pointer points to the next entry to be dumped,
5838 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005839 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005840 * data though.
5841 */
5842
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005843 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005844 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005845 if (appctx->st2 == STAT_ST_LIST) {
5846 appctx->ctx.table.entry->ref_cnt--;
5847 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005848 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005849 return 1;
5850 }
5851
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005852 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005853
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005854 while (appctx->st2 != STAT_ST_FIN) {
5855 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005856 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005857 appctx->ctx.table.proxy = appctx->ctx.table.target;
5858 if (!appctx->ctx.table.proxy)
5859 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005860
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005861 appctx->ctx.table.entry = NULL;
5862 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005863 break;
5864
Willy Tarreau295a8372011-03-10 11:25:07 +01005865 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005866 if (!appctx->ctx.table.proxy ||
5867 (appctx->ctx.table.target &&
5868 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5869 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005870 break;
5871 }
5872
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005873 if (appctx->ctx.table.proxy->table.size) {
5874 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5875 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005876 return 0;
5877
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005878 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005879 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005880 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005881 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005882 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005883 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5884 appctx->ctx.table.entry->ref_cnt++;
5885 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005886 break;
5887 }
5888 }
5889 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005890 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005891 break;
5892
Willy Tarreau295a8372011-03-10 11:25:07 +01005893 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005894 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005895
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005896 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005897 /* we're filtering on some data contents */
5898 void *ptr;
5899 long long data;
5900
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005901 dt = appctx->ctx.table.data_type;
5902 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5903 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005904 dt);
5905
5906 data = 0;
5907 switch (stktable_data_types[dt].std_type) {
5908 case STD_T_SINT:
5909 data = stktable_data_cast(ptr, std_t_sint);
5910 break;
5911 case STD_T_UINT:
5912 data = stktable_data_cast(ptr, std_t_uint);
5913 break;
5914 case STD_T_ULL:
5915 data = stktable_data_cast(ptr, std_t_ull);
5916 break;
5917 case STD_T_FRQP:
5918 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005919 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005920 break;
5921 }
5922
5923 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005924 if ((data < appctx->ctx.table.value &&
5925 (appctx->ctx.table.data_op == STD_OP_EQ ||
5926 appctx->ctx.table.data_op == STD_OP_GT ||
5927 appctx->ctx.table.data_op == STD_OP_GE)) ||
5928 (data == appctx->ctx.table.value &&
5929 (appctx->ctx.table.data_op == STD_OP_NE ||
5930 appctx->ctx.table.data_op == STD_OP_GT ||
5931 appctx->ctx.table.data_op == STD_OP_LT)) ||
5932 (data > appctx->ctx.table.value &&
5933 (appctx->ctx.table.data_op == STD_OP_EQ ||
5934 appctx->ctx.table.data_op == STD_OP_LT ||
5935 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005936 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005937 }
5938
Simon Hormanc88b8872011-06-15 15:18:49 +09005939 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005940 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5941 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005942 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005943
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005944 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005945
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005946 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005947 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005948 struct stksess *old = appctx->ctx.table.entry;
5949 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005950 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005951 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5952 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5953 stksess_kill(&appctx->ctx.table.proxy->table, old);
5954 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005955 break;
5956 }
5957
Simon Hormanc88b8872011-06-15 15:18:49 +09005958
5959 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005960 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5961 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5962 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005963
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005964 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5965 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005966 break;
5967
Willy Tarreau295a8372011-03-10 11:25:07 +01005968 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005969 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005970 break;
5971 }
5972 }
5973 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005974}
5975
Willy Tarreaud426a182010-03-05 14:58:26 +01005976/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005977 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5978 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5979 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5980 * lines are respected within the limit of 70 output chars. Lines that are
5981 * continuation of a previous truncated line begin with "+" instead of " "
5982 * after the offset. The new pointer is returned.
5983 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005984static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5985 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005986{
5987 int end;
5988 unsigned char c;
5989
5990 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005991 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005992 return ptr;
5993
Willy Tarreau77804732012-10-29 16:14:26 +01005994 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005995
Willy Tarreaud426a182010-03-05 14:58:26 +01005996 while (ptr < len && ptr < bsize) {
5997 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005998 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005999 if (out->len > end - 2)
6000 break;
6001 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006002 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006003 if (out->len > end - 3)
6004 break;
6005 out->str[out->len++] = '\\';
6006 switch (c) {
6007 case '\t': c = 't'; break;
6008 case '\n': c = 'n'; break;
6009 case '\r': c = 'r'; break;
6010 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006011 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006012 }
6013 out->str[out->len++] = c;
6014 } else {
6015 if (out->len > end - 5)
6016 break;
6017 out->str[out->len++] = '\\';
6018 out->str[out->len++] = 'x';
6019 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6020 out->str[out->len++] = hextab[c & 0xF];
6021 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006022 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006023 /* we had a line break, let's return now */
6024 out->str[out->len++] = '\n';
6025 *line = ptr;
6026 return ptr;
6027 }
6028 }
6029 /* we have an incomplete line, we return it as-is */
6030 out->str[out->len++] = '\n';
6031 return ptr;
6032}
6033
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006034/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006035 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006036 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006037 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006038static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006039{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006040 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006041 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006042
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006043 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006044 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006045
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006046 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006047
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006048 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006049 /* the function had not been called yet, let's prepare the
6050 * buffer for a response.
6051 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006052 struct tm tm;
6053
6054 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006055 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006056 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6057 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6058 error_snapshot_id);
6059
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006060 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006061 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaubc18da12015-03-13 14:00:47 +01006062 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau10479e42010-12-12 14:00:34 +01006063 return 0;
6064 }
6065
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006066 appctx->ctx.errors.px = proxy;
6067 appctx->ctx.errors.buf = 0;
6068 appctx->ctx.errors.bol = 0;
6069 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006070 }
6071
6072 /* we have two inner loops here, one for the proxy, the other one for
6073 * the buffer.
6074 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006075 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006076 struct error_snapshot *es;
6077
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006078 if (appctx->ctx.errors.buf == 0)
6079 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006080 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006081 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006082
6083 if (!es->when.tv_sec)
6084 goto next;
6085
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006086 if (appctx->ctx.errors.iid >= 0 &&
6087 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6088 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006089 goto next;
6090
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006091 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006092 /* just print headers now */
6093
6094 char pn[INET6_ADDRSTRLEN];
6095 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006096 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006097
6098 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006099 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006100 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006101 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006102
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006103 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6104 case AF_INET:
6105 case AF_INET6:
6106 port = get_host_port(&es->src);
6107 break;
6108 default:
6109 port = 0;
6110 }
6111
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006112 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006113 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006114 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006115 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006116 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006117 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006118 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6119 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006120 break;
6121 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006122 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006123 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006124 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006125 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006126 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006127 break;
6128 }
6129
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006130 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006131 ", server %s (#%d), event #%u\n"
6132 " src %s:%d, session #%d, session flags 0x%08x\n"
6133 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6134 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6135 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6136 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6137 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6138 es->ev_id,
6139 pn, port, es->sid, es->s_flags,
6140 es->state, es->m_flags, es->t_flags,
6141 es->m_clen, es->m_blen,
6142 es->b_flags, es->b_out, es->b_tot,
6143 es->len, es->b_wrap, es->pos);
6144
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006145 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006146 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaubc18da12015-03-13 14:00:47 +01006147 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau61b34732009-10-03 23:49:35 +02006148 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006149 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006150 appctx->ctx.errors.ptr = 0;
6151 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006152 }
6153
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006154 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006155 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006156 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006157 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006158 if (bi_putchk(si_ic(si), &trash) == -1) {
6159 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau61b34732009-10-03 23:49:35 +02006160 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006161 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006162 goto next;
6163 }
6164
6165 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006166 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006167 int newptr;
6168 int newline;
6169
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006170 newline = appctx->ctx.errors.bol;
6171 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6172 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006173 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006174
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006175 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006176 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaubc18da12015-03-13 14:00:47 +01006177 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau61b34732009-10-03 23:49:35 +02006178 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006179 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006180 appctx->ctx.errors.ptr = newptr;
6181 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006182 };
6183 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006184 appctx->ctx.errors.bol = 0;
6185 appctx->ctx.errors.ptr = -1;
6186 appctx->ctx.errors.buf++;
6187 if (appctx->ctx.errors.buf > 1) {
6188 appctx->ctx.errors.buf = 0;
6189 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006190 }
6191 }
6192
6193 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006194 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006195}
6196
Willy Tarreaud5781202012-09-22 19:32:35 +02006197/* parse the "level" argument on the bind lines */
6198static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6199{
6200 if (!*args[cur_arg + 1]) {
6201 memprintf(err, "'%s' : missing level", args[cur_arg]);
6202 return ERR_ALERT | ERR_FATAL;
6203 }
6204
6205 if (!strcmp(args[cur_arg+1], "user"))
6206 conf->level = ACCESS_LVL_USER;
6207 else if (!strcmp(args[cur_arg+1], "operator"))
6208 conf->level = ACCESS_LVL_OPER;
6209 else if (!strcmp(args[cur_arg+1], "admin"))
6210 conf->level = ACCESS_LVL_ADMIN;
6211 else {
6212 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6213 args[cur_arg], args[cur_arg+1]);
6214 return ERR_ALERT | ERR_FATAL;
6215 }
6216
6217 return 0;
6218}
6219
Willy Tarreaub24281b2011-02-13 13:16:36 +01006220struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006221 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006222 .name = "<STATS>", /* used for logging */
6223 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006224 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006225};
6226
Simon Horman9bd2c732011-06-15 15:18:44 +09006227static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006228 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006229 .name = "<CLI>", /* used for logging */
6230 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006231 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006232};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006233
Willy Tarreaudc13c112013-06-21 23:16:39 +02006234static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006235 { CFG_GLOBAL, "stats", stats_parse_global },
6236 { 0, NULL, NULL },
6237}};
6238
Willy Tarreaud5781202012-09-22 19:32:35 +02006239static struct bind_kw_list bind_kws = { "STAT", { }, {
6240 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6241 { NULL, NULL, 0 },
6242}};
6243
Willy Tarreau10522fd2008-07-09 20:12:41 +02006244__attribute__((constructor))
6245static void __dumpstats_module_init(void)
6246{
6247 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006248 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006249}
6250
Willy Tarreau91861262007-10-17 17:06:05 +02006251/*
6252 * Local variables:
6253 * c-indent-level: 8
6254 * c-basic-offset: 8
6255 * End:
6256 */