blob: 96180fe710378fdb2626f36420fd1c79dcb3b404 [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>
Baptiste Assmann3863f972015-05-17 00:33:24 +020042#include <types/dns.h>
Willy Tarreau91861262007-10-17 17:06:05 +020043
44#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020045#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020046#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010047#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020048#include <proto/dumpstats.h>
49#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010050#include <proto/freq_ctr.h>
Willy Tarreau32b60d42015-03-13 16:14:57 +010051#include <proto/frontend.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020052#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010053#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010054#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020055#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010056#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010057#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020058#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020059#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010060#include <proto/sample.h>
Willy Tarreau9903f0e2015-04-04 18:50:31 +020061#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020062#include <proto/stream.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020064#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010065#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010066#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020067
Willy Tarreau7a0169a2012-11-19 17:13:16 +010068#ifdef USE_OPENSSL
69#include <proto/ssl_sock.h>
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020070#include <types/ssl_sock.h>
Willy Tarreau7a0169a2012-11-19 17:13:16 +010071#endif
72
Willy Tarreau91b843d2014-01-28 16:27:17 +010073/* stats socket states */
74enum {
75 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
76 STAT_CLI_END, /* final state, let's close */
77 STAT_CLI_GETREQ, /* wait for a request */
78 STAT_CLI_OUTPUT, /* all states after this one are responses */
79 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
80 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010081 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010082 STAT_CLI_O_INFO, /* dump info */
Willy Tarreau87b09662015-04-03 00:22:06 +020083 STAT_CLI_O_SESS, /* dump streams */
Willy Tarreau91b843d2014-01-28 16:27:17 +010084 STAT_CLI_O_ERR, /* dump errors */
85 STAT_CLI_O_TAB, /* dump tables */
86 STAT_CLI_O_CLR, /* clear tables */
87 STAT_CLI_O_SET, /* set entries in tables */
88 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010089 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
90 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010091 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010092 STAT_CLI_O_POOLS, /* dump memory pools */
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020093 STAT_CLI_O_TLSK, /* list all TLS ticket keys references */
Baptiste Assmann3863f972015-05-17 00:33:24 +020094 STAT_CLI_O_RESOLVERS,/* dump a resolver's section nameservers counters */
Willy Tarreau91b843d2014-01-28 16:27:17 +010095};
96
Willy Tarreaued7df902014-05-22 18:04:49 +020097/* Actions available for the stats admin forms */
98enum {
99 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +0200100
101 /* enable/disable health checks */
102 ST_ADM_ACTION_DHLTH,
103 ST_ADM_ACTION_EHLTH,
104
105 /* force health check status */
106 ST_ADM_ACTION_HRUNN,
107 ST_ADM_ACTION_HNOLB,
108 ST_ADM_ACTION_HDOWN,
109
110 /* enable/disable agent checks */
111 ST_ADM_ACTION_DAGENT,
112 ST_ADM_ACTION_EAGENT,
113
114 /* force agent check status */
115 ST_ADM_ACTION_ARUNN,
116 ST_ADM_ACTION_ADOWN,
117
118 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200119 ST_ADM_ACTION_READY,
120 ST_ADM_ACTION_DRAIN,
121 ST_ADM_ACTION_MAINT,
122 ST_ADM_ACTION_SHUTDOWN,
123 /* these are the ancient actions, still available for compatibility */
124 ST_ADM_ACTION_DISABLE,
125 ST_ADM_ACTION_ENABLE,
126 ST_ADM_ACTION_STOP,
127 ST_ADM_ACTION_START,
128};
129
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100130static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100131static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200132static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900133static int stats_dump_sess_to_buffer(struct stream_interface *si);
134static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100135static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100136static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
137static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200138static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100139static int stats_pats_list(struct stream_interface *si);
140static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100141static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200142#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
143static int stats_tlskeys_list(struct stream_interface *si);
144#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200145static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900146
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100147/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100148 * cli_io_handler()
149 * -> stats_dump_sess_to_buffer() // "show sess"
150 * -> stats_dump_errors_to_buffer() // "show errors"
151 * -> stats_dump_info_to_buffer() // "show info"
152 * -> stats_dump_stat_to_buffer() // "show stat"
Baptiste Assmann3863f972015-05-17 00:33:24 +0200153 * -> stats_dump_resolvers_to_buffer() // "show stat resolver <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100154 * -> stats_dump_csv_header()
155 * -> stats_dump_proxy_to_buffer()
156 * -> stats_dump_fe_stats()
157 * -> stats_dump_li_stats()
158 * -> stats_dump_sv_stats()
159 * -> stats_dump_be_stats()
160 *
161 * http_stats_io_handler()
162 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
163 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
164 * -> stats_dump_html_head() // emits the HTML headers
165 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
166 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
167 * -> stats_dump_html_px_hdr()
168 * -> stats_dump_fe_stats()
169 * -> stats_dump_li_stats()
170 * -> stats_dump_sv_stats()
171 * -> stats_dump_be_stats()
172 * -> stats_dump_html_px_end()
173 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100174 */
175
Willy Tarreau30576452015-04-13 13:50:30 +0200176static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900177
178static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200179 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200180 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200181 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200182 " help : this message\n"
183 " prompt : toggle interactive mode with prompt\n"
184 " quit : disconnect\n"
185 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100186 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200187 " show stat : report counters for each proxy and server\n"
188 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100189 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200190 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200191 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200192 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200193 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200194 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100195 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200196 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200197 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200198 " disable : put a server or frontend in maintenance mode\n"
199 " enable : re-enable a server or frontend which is in maintenance mode\n"
200 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100201 " show acl [id] : report avalaible acls or dump an acl's contents\n"
202 " get acl : reports the patterns matching a sample for an ACL\n"
203 " add acl : add acl entry\n"
204 " del acl : delete acl entry\n"
205 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100206 " show map [id] : report avalaible maps or dump a map's contents\n"
207 " get map : reports the keys and values matching a sample for a map\n"
208 " set map : modify map entry\n"
209 " add map : add map entry\n"
210 " del map : delete map entry\n"
211 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200212 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200213 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200214
Simon Horman9bd2c732011-06-15 15:18:44 +0900215static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200216 "Permission denied\n"
217 "";
218
Willy Tarreau295a8372011-03-10 11:25:07 +0100219/* data transmission states for the stats responses */
220enum {
221 STAT_ST_INIT = 0,
222 STAT_ST_HEAD,
223 STAT_ST_INFO,
224 STAT_ST_LIST,
225 STAT_ST_END,
226 STAT_ST_FIN,
227};
228
229/* data transmission states for the stats responses inside a proxy */
230enum {
231 STAT_PX_ST_INIT = 0,
232 STAT_PX_ST_TH,
233 STAT_PX_ST_FE,
234 STAT_PX_ST_LI,
235 STAT_PX_ST_SV,
236 STAT_PX_ST_BE,
237 STAT_PX_ST_END,
238 STAT_PX_ST_FIN,
239};
240
Cyril Bonté19979e12012-04-04 12:57:21 +0200241extern const char *stat_status_codes[];
242
Willy Tarreau07e9e642010-08-17 21:48:17 +0200243/* allocate a new stats frontend named <name>, and return it
244 * (or NULL in case of lack of memory).
245 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200246static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200247{
248 struct proxy *fe;
249
250 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
251 if (!fe)
252 return NULL;
253
Willy Tarreau237250c2011-07-29 01:49:03 +0200254 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200255 fe->next = proxy;
256 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200257 fe->last_change = now.tv_sec;
258 fe->id = strdup("GLOBAL");
259 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200260 fe->maxconn = 10; /* default to 10 concurrent connections */
261 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200262 fe->conf.file = strdup(file);
263 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100264 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100265 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200266
267 /* the stats frontend is the only one able to assign ID #0 */
268 fe->conf.id.key = fe->uuid = 0;
269 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200270 return fe;
271}
272
Willy Tarreaufbee7132007-10-18 13:53:22 +0200273/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200274 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
275 * error message into the <err> buffer which will be preallocated. The trailing
276 * '\n' must not be written. The function must be called with <args> pointing to
277 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200278 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200279static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200280 struct proxy *defpx, const char *file, int line,
281 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200282{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200283 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200284 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200285
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200286 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200287 int cur_arg;
288
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200289 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200290 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 +0200291 return -1;
292 }
293
Willy Tarreau89a63132009-08-16 17:41:45 +0200294 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200295 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200296 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200297 return -1;
298 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200299 }
300
Willy Tarreau4348fad2012-09-20 16:48:07 +0200301 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200302 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200303
Willy Tarreauc53d4222012-09-20 20:19:28 +0200304 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
305 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
306 file, line, args[0], args[1], err && *err ? *err : "error");
307 return -1;
308 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200309
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200310 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200311 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200312 static int bind_dumped;
313 struct bind_kw *kw;
314
315 kw = bind_find_kw(args[cur_arg]);
316 if (kw) {
317 if (!kw->parse) {
318 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
319 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200320 return -1;
321 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200322
323 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
324 if (err && *err)
325 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
326 else
327 memprintf(err, "'%s %s' : error encountered while processing '%s'",
328 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200329 return -1;
330 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200331
332 cur_arg += 1 + kw->skip;
333 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200334 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200335
336 if (!bind_dumped) {
337 bind_dump_kws(err);
338 indent_msg(err, 4);
339 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200340 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200341
342 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
343 args[0], args[1], args[cur_arg],
344 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
345 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200346 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100347
Willy Tarreauc53d4222012-09-20 20:19:28 +0200348 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
349 l->maxconn = global.stats_fe->maxconn;
350 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200351 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200352 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100353 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200354 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
355 l->nice = -64; /* we want to boost priority for local stats */
356 global.maxsock += l->maxconn;
357 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200358 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200359 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100360 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200361 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100362
363 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200364 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100365 return -1;
366 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200367
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100368 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200369 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200370 return -1;
371 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200372 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200373 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200374 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200375 return -1;
376 }
377 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200378 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200379 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200380 else if (!strcmp(args[1], "maxconn")) {
381 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200382
383 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200384 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200385 return -1;
386 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200387
388 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200389 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200390 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200391 return -1;
392 }
393 }
394 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200395 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200396 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
397 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100398 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200399
Willy Tarreau91319572013-04-20 09:48:50 +0200400 if (!global.stats_fe) {
401 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
402 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
403 return -1;
404 }
405 }
406
Willy Tarreau35b7b162012-10-22 23:17:18 +0200407 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100408 unsigned int low, high;
409
Willy Tarreau35b7b162012-10-22 23:17:18 +0200410 if (strcmp(args[cur_arg], "all") == 0) {
411 set = 0;
412 break;
413 }
414 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100415 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200416 }
417 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100418 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200419 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100420 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100421 char *dash = strchr(args[cur_arg], '-');
422
423 low = high = str2uic(args[cur_arg]);
424 if (dash)
425 high = str2uic(dash + 1);
426
427 if (high < low) {
428 unsigned int swap = low;
429 low = high;
430 high = swap;
431 }
432
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100433 if (low < 1 || high > LONGBITS) {
434 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
435 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200436 return -1;
437 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100438 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100439 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100440 }
441 else {
442 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100443 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
444 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100445 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200446 }
447 cur_arg++;
448 }
449 global.stats_fe->bind_proc = set;
450 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200451 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200452 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200453 return -1;
454 }
455 return 0;
456}
457
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100458/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
459 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100460 * NOTE: Some tools happen to rely on the field position instead of its name,
461 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100462 */
463static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100464{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100465 chunk_appendf(&trash,
466 "# pxname,svname,"
467 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100468 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100469 "bin,bout,"
470 "dreq,dresp,"
471 "ereq,econ,eresp,"
472 "wretr,wredis,"
473 "status,weight,act,bck,"
474 "chkfail,chkdown,lastchg,downtime,qlimit,"
475 "pid,iid,sid,throttle,lbtot,tracked,type,"
476 "rate,rate_lim,rate_max,"
477 "check_status,check_code,check_duration,"
478 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
479 "req_rate,req_rate_max,req_tot,"
480 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200481 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100482 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100483}
484
Simon Hormand9366582011-06-15 15:18:45 +0900485/* print a string of text buffer to <out>. The format is :
486 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
487 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
488 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
489 */
490static int dump_text(struct chunk *out, const char *buf, int bsize)
491{
492 unsigned char c;
493 int ptr = 0;
494
495 while (buf[ptr] && ptr < bsize) {
496 c = buf[ptr];
497 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
498 if (out->len > out->size - 1)
499 break;
500 out->str[out->len++] = c;
501 }
502 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
503 if (out->len > out->size - 2)
504 break;
505 out->str[out->len++] = '\\';
506 switch (c) {
507 case ' ': c = ' '; break;
508 case '\t': c = 't'; break;
509 case '\n': c = 'n'; break;
510 case '\r': c = 'r'; break;
511 case '\e': c = 'e'; break;
512 case '\\': c = '\\'; break;
513 }
514 out->str[out->len++] = c;
515 }
516 else {
517 if (out->len > out->size - 4)
518 break;
519 out->str[out->len++] = '\\';
520 out->str[out->len++] = 'x';
521 out->str[out->len++] = hextab[(c >> 4) & 0xF];
522 out->str[out->len++] = hextab[c & 0xF];
523 }
524 ptr++;
525 }
526
527 return ptr;
528}
529
530/* print a buffer in hexa.
531 * Print stopped if <bsize> is reached, or if no more place in the chunk.
532 */
533static int dump_binary(struct chunk *out, const char *buf, int bsize)
534{
535 unsigned char c;
536 int ptr = 0;
537
538 while (ptr < bsize) {
539 c = buf[ptr];
540
541 if (out->len > out->size - 2)
542 break;
543 out->str[out->len++] = hextab[(c >> 4) & 0xF];
544 out->str[out->len++] = hextab[c & 0xF];
545
546 ptr++;
547 }
548 return ptr;
549}
550
551/* Dump the status of a table to a stream interface's
552 * read buffer. It returns 0 if the output buffer is full
553 * and needs to be called again, otherwise non-zero.
554 */
555static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
556 struct proxy *proxy, struct proxy *target)
557{
Willy Tarreau87b09662015-04-03 00:22:06 +0200558 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900559
Willy Tarreau77804732012-10-29 16:14:26 +0100560 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900561 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
562
563 /* any other information should be dumped here */
564
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200565 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100566 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900567
Willy Tarreaubc18da12015-03-13 14:00:47 +0100568 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200569 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900570 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100571 }
Simon Hormand9366582011-06-15 15:18:45 +0900572
573 return 1;
574}
575
576/* Dump the a table entry to a stream interface's
577 * read buffer. It returns 0 if the output buffer is full
578 * and needs to be called again, otherwise non-zero.
579 */
580static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
581 struct proxy *proxy, struct stksess *entry)
582{
583 int dt;
584
Willy Tarreau77804732012-10-29 16:14:26 +0100585 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900586
587 if (proxy->table.type == STKTABLE_TYPE_IP) {
588 char addr[INET_ADDRSTRLEN];
589 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100590 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900591 }
592 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
593 char addr[INET6_ADDRSTRLEN];
594 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100595 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900596 }
597 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100598 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900599 }
600 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100601 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900602 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
603 }
604 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100605 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900606 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
607 }
608
Willy Tarreau77804732012-10-29 16:14:26 +0100609 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900610
611 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
612 void *ptr;
613
614 if (proxy->table.data_ofs[dt] == 0)
615 continue;
616 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100617 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900618 else
Willy Tarreau77804732012-10-29 16:14:26 +0100619 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900620
621 ptr = stktable_data_ptr(&proxy->table, entry, dt);
622 switch (stktable_data_types[dt].std_type) {
623 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100624 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900625 break;
626 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100627 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900628 break;
629 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100630 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900631 break;
632 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100633 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900634 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
635 proxy->table.data_arg[dt].u));
636 break;
637 }
638 }
Willy Tarreau77804732012-10-29 16:14:26 +0100639 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900640
Willy Tarreaubc18da12015-03-13 14:00:47 +0100641 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200642 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900643 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100644 }
Simon Hormand9366582011-06-15 15:18:45 +0900645
646 return 1;
647}
648
Willy Tarreaudec98142012-06-06 23:37:08 +0200649static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900650{
Willy Tarreau87b09662015-04-03 00:22:06 +0200651 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100652 struct appctx *appctx = __objt_appctx(si->end);
653 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900654 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900655 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900656 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200657 long long value;
658 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200659 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200660 void *ptr;
661 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900662
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100663 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900664
665 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100666 appctx->ctx.cli.msg = "Key value expected\n";
667 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900668 return;
669 }
670
Simon Hormanc5b89f62011-06-15 15:18:50 +0900671 switch (px->table.type) {
672 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900673 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100674 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900675 break;
676 case STKTABLE_TYPE_IPV6:
677 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100678 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900679 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900680 case STKTABLE_TYPE_INTEGER:
681 {
682 char *endptr;
683 unsigned long val;
684 errno = 0;
685 val = strtoul(args[4], &endptr, 10);
686 if ((errno == ERANGE && val == ULONG_MAX) ||
687 (errno != 0 && val == 0) || endptr == args[4] ||
688 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100689 appctx->ctx.cli.msg = "Invalid key\n";
690 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900691 return;
692 }
693 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100694 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900695 break;
696 }
697 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900698 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100699 static_table_key->key = args[4];
700 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900701 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900702 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200703 switch (action) {
704 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100705 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 +0200706 break;
707 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100708 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 +0200709 break;
710 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100711 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200712 break;
713 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100714 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900715 return;
716 }
717
718 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200719 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100720 appctx->ctx.cli.msg = stats_permission_denied_msg;
721 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900722 return;
723 }
724
Willy Tarreau07115412012-10-29 21:56:59 +0100725 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900726
Willy Tarreaudec98142012-06-06 23:37:08 +0200727 switch (action) {
728 case STAT_CLI_O_TAB:
729 if (!ts)
730 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100731 chunk_reset(&trash);
732 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900733 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100734 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900735 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200736
737 case STAT_CLI_O_CLR:
738 if (!ts)
739 return;
740 if (ts->ref_cnt) {
741 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100742 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
743 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200744 return;
745 }
746 stksess_kill(&px->table, ts);
747 break;
Simon Horman17bce342011-06-15 15:18:47 +0900748
Willy Tarreau654694e2012-06-07 01:03:16 +0200749 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200750 if (ts)
751 stktable_touch(&px->table, ts, 1);
752 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100753 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200754 if (!ts) {
755 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100756 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
757 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200758 return;
759 }
760 stktable_store(&px->table, ts, 1);
761 }
762
Willy Tarreau47060b62013-08-01 21:11:42 +0200763 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
764 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100765 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
766 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200767 return;
768 }
769
770 data_type = stktable_get_data_type(args[cur_arg] + 5);
771 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100772 appctx->ctx.cli.msg = "Unknown data type\n";
773 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200774 return;
775 }
776
777 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100778 appctx->ctx.cli.msg = "Data type not stored in this table\n";
779 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200780 return;
781 }
782
783 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100784 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
785 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200786 return;
787 }
788
789 ptr = stktable_data_ptr(&px->table, ts, data_type);
790
791 switch (stktable_data_types[data_type].std_type) {
792 case STD_T_SINT:
793 stktable_data_cast(ptr, std_t_sint) = value;
794 break;
795 case STD_T_UINT:
796 stktable_data_cast(ptr, std_t_uint) = value;
797 break;
798 case STD_T_ULL:
799 stktable_data_cast(ptr, std_t_ull) = value;
800 break;
801 case STD_T_FRQP:
802 /* We set both the current and previous values. That way
803 * the reported frequency is stable during all the period
804 * then slowly fades out. This allows external tools to
805 * push measures without having to update them too often.
806 */
807 frqp = &stktable_data_cast(ptr, std_t_frqp);
808 frqp->curr_tick = now_ms;
809 frqp->prev_ctr = 0;
810 frqp->curr_ctr = value;
811 break;
812 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200813 }
814 break;
815
Willy Tarreaudec98142012-06-06 23:37:08 +0200816 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100817 appctx->ctx.cli.msg = "Unknown action\n";
818 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200819 break;
Simon Horman121f3052011-06-15 15:18:46 +0900820 }
Simon Horman121f3052011-06-15 15:18:46 +0900821}
822
Willy Tarreau654694e2012-06-07 01:03:16 +0200823static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900824{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100825 struct appctx *appctx = __objt_appctx(si->end);
826
Willy Tarreau04b3a192013-04-13 09:41:37 +0200827 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100828 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
829 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200830 return;
831 }
832
Simon Hormand5b9fd92011-06-15 15:18:48 +0900833 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100834 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
835 if (appctx->ctx.table.data_type < 0) {
836 appctx->ctx.cli.msg = "Unknown data type\n";
837 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900838 return;
839 }
840
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100841 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
842 appctx->ctx.cli.msg = "Data type not stored in this table\n";
843 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900844 return;
845 }
846
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100847 appctx->ctx.table.data_op = get_std_op(args[4]);
848 if (appctx->ctx.table.data_op < 0) {
849 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
850 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900851 return;
852 }
853
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100854 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
855 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
856 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900857 return;
858 }
859}
860
Willy Tarreaudec98142012-06-06 23:37:08 +0200861static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900862{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100863 struct appctx *appctx = __objt_appctx(si->end);
864
865 appctx->ctx.table.data_type = -1;
866 appctx->st2 = STAT_ST_INIT;
867 appctx->ctx.table.target = NULL;
868 appctx->ctx.table.proxy = NULL;
869 appctx->ctx.table.entry = NULL;
870 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900871
872 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200873 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100874 if (!appctx->ctx.table.target) {
875 appctx->ctx.cli.msg = "No such table\n";
876 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900877 return;
878 }
879 }
880 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200881 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900882 goto err_args;
883 return;
884 }
885
886 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200887 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900888 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200889 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900890 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900891 goto err_args;
892
893 return;
894
895err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200896 switch (action) {
897 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100898 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 +0200899 break;
900 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100901 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 +0200902 break;
903 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100904 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200905 break;
906 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100907 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900908}
909
Willy Tarreau532a4502011-09-07 22:37:44 +0200910/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +0200911 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +0200912 * level to be admin.
913 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200914static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +0200915{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100916 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200917 struct proxy *px;
918
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200919 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100920 appctx->ctx.cli.msg = stats_permission_denied_msg;
921 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200922 return NULL;
923 }
924
925 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100926 appctx->ctx.cli.msg = "A frontend name is expected.\n";
927 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200928 return NULL;
929 }
930
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200931 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +0200932 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100933 appctx->ctx.cli.msg = "No such frontend.\n";
934 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200935 return NULL;
936 }
937 return px;
938}
939
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200940/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
941 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +0200942 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200943 * the <arg> is modified to remove the '/'.
944 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200945static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200946{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100947 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200948 struct proxy *px;
949 struct server *sv;
950 char *line;
951
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200952 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100953 appctx->ctx.cli.msg = stats_permission_denied_msg;
954 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200955 return NULL;
956 }
957
958 /* split "backend/server" and make <line> point to server */
959 for (line = arg; *line; line++)
960 if (*line == '/') {
961 *line++ = '\0';
962 break;
963 }
964
965 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100966 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
967 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200968 return NULL;
969 }
970
971 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100972 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
973 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200974 return NULL;
975 }
976
977 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100978 appctx->ctx.cli.msg = "Proxy is disabled.\n";
979 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200980 return NULL;
981 }
982
983 return sv;
984}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200985
986/* This function is used with TLS ticket keys management. It permits to browse
987 * each reference. The variable <getnext> must contain the current node,
988 * <end> point to the root node.
989 */
990#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
991static inline
992struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
993{
994 struct tls_keys_ref *ref = getnext;
995
996 while (1) {
997
998 /* Get next list entry. */
999 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1000
1001 /* If the entry is the last of the list, return NULL. */
1002 if (&ref->list == end)
1003 return NULL;
1004
1005 return ref;
1006 }
1007}
1008
1009static inline
1010struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1011{
1012 int id;
1013 char *error;
1014
1015 /* If the reference starts by a '#', this is numeric id. */
1016 if (reference[0] == '#') {
1017 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1018 id = strtol(reference + 1, &error, 10);
1019 if (*error != '\0')
1020 return NULL;
1021
1022 /* Perform the unique id lookup. */
1023 return tlskeys_ref_lookupid(id);
1024 }
1025
1026 /* Perform the string lookup. */
1027 return tlskeys_ref_lookup(reference);
1028}
1029#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001030
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001031/* This function is used with map and acl management. It permits to browse
1032 * each reference. The variable <getnext> must contain the current node,
1033 * <end> point to the root node and the <flags> permit to filter required
1034 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001035 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001036static inline
1037struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1038 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001039{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001040 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001041
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001042 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001043
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001044 /* Get next list entry. */
1045 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001046
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001047 /* If the entry is the last of the list, return NULL. */
1048 if (&ref->list == end)
1049 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001050
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001051 /* If the entry match the flag, return it. */
1052 if (ref->flags & flags)
1053 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001054 }
1055}
1056
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001057static inline
1058struct pat_ref *pat_ref_lookup_ref(const char *reference)
1059{
1060 int id;
1061 char *error;
1062
1063 /* If the reference starts by a '#', this is numeric id. */
1064 if (reference[0] == '#') {
1065 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1066 id = strtol(reference + 1, &error, 10);
1067 if (*error != '\0')
1068 return NULL;
1069
1070 /* Perform the unique id lookup. */
1071 return pat_ref_lookupid(id);
1072 }
1073
1074 /* Perform the string lookup. */
1075 return pat_ref_lookup(reference);
1076}
1077
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001078/* This function is used with map and acl management. It permits to browse
1079 * each reference.
1080 */
1081static inline
1082struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1083{
1084 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001085 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1086 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001087 return NULL;
1088 return expr;
1089}
1090
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001091/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001092 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001093 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001094 * designating the function which will have to process the request, which can
1095 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001096 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001097static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001098{
Willy Tarreau87b09662015-04-03 00:22:06 +02001099 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001100 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001101 char *args[MAX_STATS_ARGS + 1];
1102 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001103 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001104
1105 while (isspace((unsigned char)*line))
1106 line++;
1107
1108 arg = 0;
1109 args[arg] = line;
1110
1111 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001112 if (*line == '\\') {
1113 line++;
1114 if (*line == '\0')
1115 break;
1116 }
1117 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001118 *line++ = '\0';
1119
1120 while (isspace((unsigned char)*line))
1121 line++;
1122
1123 args[++arg] = line;
1124 continue;
1125 }
1126
1127 line++;
1128 }
1129
1130 while (++arg <= MAX_STATS_ARGS)
1131 args[arg] = line;
1132
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001133 /* remove \ */
1134 arg = 0;
1135 while (*args[arg] != '\0') {
1136 j = 0;
1137 for (i=0; args[arg][i] != '\0'; i++) {
1138 if (args[arg][i] == '\\')
1139 continue;
1140 args[arg][j] = args[arg][i];
1141 j++;
1142 }
1143 args[arg][j] = '\0';
1144 arg++;
1145 }
1146
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001147 appctx->ctx.stats.scope_str = 0;
1148 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001149 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001150 if (strcmp(args[0], "show") == 0) {
1151 if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001152 if (strcmp(args[2], "resolvers") == 0) {
1153 struct dns_resolvers *presolvers;
1154
1155 if (!*args[3]) {
1156 appctx->ctx.cli.msg = "Missing resolver section identifier.\n";
1157 appctx->st0 = STAT_CLI_PRINT;
1158 return 1;
1159 }
1160
1161 appctx->ctx.resolvers.ptr = NULL;
1162 list_for_each_entry(presolvers, &dns_resolvers, list) {
1163 if (strcmp(presolvers->id, args[3]) == 0) {
1164 appctx->ctx.resolvers.ptr = presolvers;
1165 break;
1166 }
1167 }
1168 if (appctx->ctx.resolvers.ptr == NULL) {
1169 appctx->ctx.cli.msg = "Can't find resolvers section.\n";
1170 appctx->st0 = STAT_CLI_PRINT;
1171 return 1;
1172 }
1173
1174 appctx->st2 = STAT_ST_INIT;
1175 appctx->st0 = STAT_CLI_O_RESOLVERS;
1176 return 1;
1177 }
1178 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001179 appctx->ctx.stats.flags |= STAT_BOUND;
1180 appctx->ctx.stats.iid = atoi(args[2]);
1181 appctx->ctx.stats.type = atoi(args[3]);
1182 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001183 }
1184
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001185 appctx->st2 = STAT_ST_INIT;
1186 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001187 }
1188 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001189 appctx->st2 = STAT_ST_INIT;
1190 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001191 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001192 else if (strcmp(args[1], "pools") == 0) {
1193 appctx->st2 = STAT_ST_INIT;
1194 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1195 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001196 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001197 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001198 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001199 appctx->ctx.cli.msg = stats_permission_denied_msg;
1200 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001201 return 1;
1202 }
Willy Tarreau76153662012-11-26 01:16:39 +01001203 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001204 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001205 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001206 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001207 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001208 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001209 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001210 appctx->ctx.sess.pos = 0;
1211 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001212 }
1213 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001214 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001215 appctx->ctx.cli.msg = stats_permission_denied_msg;
1216 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001217 return 1;
1218 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001219 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001220 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001221 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001222 appctx->ctx.errors.iid = -1;
1223 appctx->ctx.errors.px = NULL;
1224 appctx->st2 = STAT_ST_INIT;
1225 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001226 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001227 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001228 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001229 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001230 else if (strcmp(args[1], "tls-keys") == 0) {
1231#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1232 appctx->st2 = STAT_ST_INIT;
1233 appctx->st0 = STAT_CLI_O_TLSK;
1234#else
1235 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1236 "that doesn't support specifying TLS ticket keys\n";
1237 appctx->st0 = STAT_CLI_PRINT;
1238#endif
1239 return 1;
1240 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001241 else if (strcmp(args[1], "map") == 0 ||
1242 strcmp(args[1], "acl") == 0) {
1243
1244 /* Set ACL or MAP flags. */
1245 if (args[1][0] == 'm')
1246 appctx->ctx.map.display_flags = PAT_REF_MAP;
1247 else
1248 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001249
1250 /* no parameter: display all map avalaible */
1251 if (!*args[2]) {
1252 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001253 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001254 return 1;
1255 }
1256
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001257 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001258 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001259 if (!appctx->ctx.map.ref ||
1260 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1261 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001262 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001263 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001264 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001265 appctx->st0 = STAT_CLI_PRINT;
1266 return 1;
1267 }
1268 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001269 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001270 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001271 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001272 return 0;
1273 }
1274 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001275 else if (strcmp(args[0], "clear") == 0) {
1276 if (strcmp(args[1], "counters") == 0) {
1277 struct proxy *px;
1278 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001279 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001280 int clrall = 0;
1281
1282 if (strcmp(args[2], "all") == 0)
1283 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001284
Willy Tarreau6162db22009-10-10 17:13:00 +02001285 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001286 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1287 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001288 appctx->ctx.cli.msg = stats_permission_denied_msg;
1289 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001290 return 1;
1291 }
1292
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001293 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001294 if (clrall) {
1295 memset(&px->be_counters, 0, sizeof(px->be_counters));
1296 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1297 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001298 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001299 px->be_counters.conn_max = 0;
1300 px->be_counters.p.http.rps_max = 0;
1301 px->be_counters.sps_max = 0;
1302 px->be_counters.cps_max = 0;
1303 px->be_counters.nbpend_max = 0;
1304
1305 px->fe_counters.conn_max = 0;
1306 px->fe_counters.p.http.rps_max = 0;
1307 px->fe_counters.sps_max = 0;
1308 px->fe_counters.cps_max = 0;
1309 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001310 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001311
1312 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001313 if (clrall)
1314 memset(&sv->counters, 0, sizeof(sv->counters));
1315 else {
1316 sv->counters.cur_sess_max = 0;
1317 sv->counters.nbpend_max = 0;
1318 sv->counters.sps_max = 0;
1319 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001320
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001321 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001322 if (li->counters) {
1323 if (clrall)
1324 memset(li->counters, 0, sizeof(*li->counters));
1325 else
1326 li->counters->conn_max = 0;
1327 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001328 }
1329
Willy Tarreau81c25d02011-09-07 15:17:21 +02001330 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001331 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001332 return 1;
1333 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001334 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001335 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001336 /* end of processing */
1337 return 1;
1338 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001339 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1340 /* Set ACL or MAP flags. */
1341 if (args[1][0] == 'm')
1342 appctx->ctx.map.display_flags = PAT_REF_MAP;
1343 else
1344 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001345
1346 /* no parameter */
1347 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001348 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1349 appctx->ctx.cli.msg = "Missing map identifier.\n";
1350 else
1351 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001352 appctx->st0 = STAT_CLI_PRINT;
1353 return 1;
1354 }
1355
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001356 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001357 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001358 if (!appctx->ctx.map.ref ||
1359 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1360 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001361 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001362 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001363 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001364 appctx->st0 = STAT_CLI_PRINT;
1365 return 1;
1366 }
1367
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001368 /* Clear all. */
1369 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001370
1371 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001372 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001373 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001374 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001375 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001376 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001377 return 0;
1378 }
1379 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001380 else if (strcmp(args[0], "get") == 0) {
1381 if (strcmp(args[1], "weight") == 0) {
1382 struct proxy *px;
1383 struct server *sv;
1384
1385 /* split "backend/server" and make <line> point to server */
1386 for (line = args[2]; *line; line++)
1387 if (*line == '/') {
1388 *line++ = '\0';
1389 break;
1390 }
1391
1392 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001393 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1394 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001395 return 1;
1396 }
1397
1398 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001399 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1400 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001401 return 1;
1402 }
1403
1404 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001405 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001406 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001407 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001408
Willy Tarreau38338fa2009-10-10 18:37:29 +02001409 return 1;
1410 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001411 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1412 /* Set flags. */
1413 if (args[1][0] == 'm')
1414 appctx->ctx.map.display_flags = PAT_REF_MAP;
1415 else
1416 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001417
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001418 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001419 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001420 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1421 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1422 else
1423 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001424 appctx->st0 = STAT_CLI_PRINT;
1425 return 1;
1426 }
1427
1428 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001429 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001430 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001431 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001432 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001433 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001434 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001435 appctx->st0 = STAT_CLI_PRINT;
1436 return 1;
1437 }
1438
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001439 /* copy input string. The string must be allocated because
1440 * it may be used over multiple iterations. It's released
1441 * at the end and upon abort anyway.
1442 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001443 appctx->ctx.map.chunk.len = strlen(args[3]);
1444 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1445 appctx->ctx.map.chunk.str = strdup(args[3]);
1446 if (!appctx->ctx.map.chunk.str) {
1447 appctx->ctx.cli.msg = "Out of memory error.\n";
1448 appctx->st0 = STAT_CLI_PRINT;
1449 return 1;
1450 }
1451
1452 /* prepare response */
1453 appctx->st2 = STAT_ST_INIT;
1454 appctx->st0 = STAT_CLI_O_MLOOK;
1455 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001456 else { /* not "get weight" */
1457 return 0;
1458 }
1459 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001460 else if (strcmp(args[0], "set") == 0) {
1461 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001462 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001463 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001464
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001465 sv = expect_server_admin(s, si, args[2]);
1466 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001467 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001468
Simon Horman7d09b9a2013-02-12 10:45:51 +09001469 warning = server_parse_weight_change_request(sv, args[3]);
1470 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001471 appctx->ctx.cli.msg = warning;
1472 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001473 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001474 return 1;
1475 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001476 else if (strcmp(args[1], "server") == 0) {
1477 struct server *sv;
1478 const char *warning;
1479
1480 sv = expect_server_admin(s, si, args[2]);
1481 if (!sv)
1482 return 1;
1483
1484 if (strcmp(args[3], "weight") == 0) {
1485 warning = server_parse_weight_change_request(sv, args[4]);
1486 if (warning) {
1487 appctx->ctx.cli.msg = warning;
1488 appctx->st0 = STAT_CLI_PRINT;
1489 }
1490 }
1491 else if (strcmp(args[3], "state") == 0) {
1492 if (strcmp(args[4], "ready") == 0)
1493 srv_adm_set_ready(sv);
1494 else if (strcmp(args[4], "drain") == 0)
1495 srv_adm_set_drain(sv);
1496 else if (strcmp(args[4], "maint") == 0)
1497 srv_adm_set_maint(sv);
1498 else {
1499 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1500 appctx->st0 = STAT_CLI_PRINT;
1501 }
1502 }
1503 else if (strcmp(args[3], "health") == 0) {
1504 if (sv->track) {
1505 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1506 appctx->st0 = STAT_CLI_PRINT;
1507 }
1508 else if (strcmp(args[4], "up") == 0) {
1509 sv->check.health = sv->check.rise + sv->check.fall - 1;
1510 srv_set_running(sv, "changed from CLI");
1511 }
1512 else if (strcmp(args[4], "stopping") == 0) {
1513 sv->check.health = sv->check.rise + sv->check.fall - 1;
1514 srv_set_stopping(sv, "changed from CLI");
1515 }
1516 else if (strcmp(args[4], "down") == 0) {
1517 sv->check.health = 0;
1518 srv_set_stopped(sv, "changed from CLI");
1519 }
1520 else {
1521 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1522 appctx->st0 = STAT_CLI_PRINT;
1523 }
1524 }
1525 else if (strcmp(args[3], "agent") == 0) {
1526 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1527 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1528 appctx->st0 = STAT_CLI_PRINT;
1529 }
1530 else if (strcmp(args[4], "up") == 0) {
1531 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1532 srv_set_running(sv, "changed from CLI");
1533 }
1534 else if (strcmp(args[4], "down") == 0) {
1535 sv->agent.health = 0;
1536 srv_set_stopped(sv, "changed from CLI");
1537 }
1538 else {
1539 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1540 appctx->st0 = STAT_CLI_PRINT;
1541 }
1542 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001543 else if (strcmp(args[3], "addr") == 0) {
1544 warning = server_parse_addr_change_request(sv, args[4]);
1545 if (warning) {
1546 appctx->ctx.cli.msg = warning;
1547 appctx->st0 = STAT_CLI_PRINT;
1548 }
1549 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001550 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001551 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001552 appctx->st0 = STAT_CLI_PRINT;
1553 }
1554 return 1;
1555 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001556 else if (strcmp(args[1], "timeout") == 0) {
1557 if (strcmp(args[2], "cli") == 0) {
1558 unsigned timeout;
1559 const char *res;
1560
1561 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001562 appctx->ctx.cli.msg = "Expects an integer value.\n";
1563 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001564 return 1;
1565 }
1566
1567 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1568 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001569 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1570 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001571 return 1;
1572 }
1573
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001574 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001575 return 1;
1576 }
1577 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001578 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1579 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001580 return 1;
1581 }
1582 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001583 else if (strcmp(args[1], "maxconn") == 0) {
1584 if (strcmp(args[2], "frontend") == 0) {
1585 struct proxy *px;
1586 struct listener *l;
1587 int v;
1588
Willy Tarreau532a4502011-09-07 22:37:44 +02001589 px = expect_frontend_admin(s, si, args[3]);
1590 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001591 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001592
1593 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001594 appctx->ctx.cli.msg = "Integer value expected.\n";
1595 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001596 return 1;
1597 }
1598
1599 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001600 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001601 appctx->ctx.cli.msg = "Value out of range.\n";
1602 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001603 return 1;
1604 }
1605
1606 /* OK, the value is fine, so we assign it to the proxy and to all of
1607 * its listeners. The blocked ones will be dequeued.
1608 */
1609 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001610 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001611 l->maxconn = v;
1612 if (l->state == LI_FULL)
1613 resume_listener(l);
1614 }
1615
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001616 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1617 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001618
1619 return 1;
1620 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001621 else if (strcmp(args[2], "global") == 0) {
1622 int v;
1623
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001624 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001625 appctx->ctx.cli.msg = stats_permission_denied_msg;
1626 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001627 return 1;
1628 }
1629
1630 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001631 appctx->ctx.cli.msg = "Expects an integer value.\n";
1632 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001633 return 1;
1634 }
1635
1636 v = atoi(args[3]);
1637 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001638 appctx->ctx.cli.msg = "Value out of range.\n";
1639 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001640 return 1;
1641 }
1642
1643 /* check for unlimited values */
1644 if (v <= 0)
1645 v = global.hardmaxconn;
1646
1647 global.maxconn = v;
1648
1649 /* Dequeues all of the listeners waiting for a resource */
1650 if (!LIST_ISEMPTY(&global_listener_queue))
1651 dequeue_all_listeners(&global_listener_queue);
1652
1653 return 1;
1654 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001655 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001656 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1657 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001658 return 1;
1659 }
1660 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001661 else if (strcmp(args[1], "rate-limit") == 0) {
1662 if (strcmp(args[2], "connections") == 0) {
1663 if (strcmp(args[3], "global") == 0) {
1664 int v;
1665
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001666 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001667 appctx->ctx.cli.msg = stats_permission_denied_msg;
1668 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001669 return 1;
1670 }
1671
1672 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001673 appctx->ctx.cli.msg = "Expects an integer value.\n";
1674 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001675 return 1;
1676 }
1677
1678 v = atoi(args[4]);
1679 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001680 appctx->ctx.cli.msg = "Value out of range.\n";
1681 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001682 return 1;
1683 }
1684
1685 global.cps_lim = v;
1686
1687 /* Dequeues all of the listeners waiting for a resource */
1688 if (!LIST_ISEMPTY(&global_listener_queue))
1689 dequeue_all_listeners(&global_listener_queue);
1690
1691 return 1;
1692 }
1693 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001694 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1695 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001696 return 1;
1697 }
1698 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001699 else if (strcmp(args[2], "sessions") == 0) {
1700 if (strcmp(args[3], "global") == 0) {
1701 int v;
1702
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001703 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001704 appctx->ctx.cli.msg = stats_permission_denied_msg;
1705 appctx->st0 = STAT_CLI_PRINT;
1706 return 1;
1707 }
1708
1709 if (!*args[4]) {
1710 appctx->ctx.cli.msg = "Expects an integer value.\n";
1711 appctx->st0 = STAT_CLI_PRINT;
1712 return 1;
1713 }
1714
1715 v = atoi(args[4]);
1716 if (v < 0) {
1717 appctx->ctx.cli.msg = "Value out of range.\n";
1718 appctx->st0 = STAT_CLI_PRINT;
1719 return 1;
1720 }
1721
1722 global.sps_lim = v;
1723
1724 /* Dequeues all of the listeners waiting for a resource */
1725 if (!LIST_ISEMPTY(&global_listener_queue))
1726 dequeue_all_listeners(&global_listener_queue);
1727
1728 return 1;
1729 }
1730 else {
1731 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1732 appctx->st0 = STAT_CLI_PRINT;
1733 return 1;
1734 }
1735 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001736#ifdef USE_OPENSSL
1737 else if (strcmp(args[2], "ssl-sessions") == 0) {
1738 if (strcmp(args[3], "global") == 0) {
1739 int v;
1740
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001741 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001742 appctx->ctx.cli.msg = stats_permission_denied_msg;
1743 appctx->st0 = STAT_CLI_PRINT;
1744 return 1;
1745 }
1746
1747 if (!*args[4]) {
1748 appctx->ctx.cli.msg = "Expects an integer value.\n";
1749 appctx->st0 = STAT_CLI_PRINT;
1750 return 1;
1751 }
1752
1753 v = atoi(args[4]);
1754 if (v < 0) {
1755 appctx->ctx.cli.msg = "Value out of range.\n";
1756 appctx->st0 = STAT_CLI_PRINT;
1757 return 1;
1758 }
1759
1760 global.ssl_lim = v;
1761
1762 /* Dequeues all of the listeners waiting for a resource */
1763 if (!LIST_ISEMPTY(&global_listener_queue))
1764 dequeue_all_listeners(&global_listener_queue);
1765
1766 return 1;
1767 }
1768 else {
1769 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1770 appctx->st0 = STAT_CLI_PRINT;
1771 return 1;
1772 }
1773 }
1774#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001775 else if (strcmp(args[2], "http-compression") == 0) {
1776 if (strcmp(args[3], "global") == 0) {
1777 int v;
1778
Willy Tarreau85d47f92012-11-21 00:29:50 +01001779 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001780 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1781 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001782 return 1;
1783 }
1784
William Lallemandd85f9172012-11-09 17:05:39 +01001785 v = atoi(args[4]);
1786 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1787 }
1788 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001789 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1790 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001791 return 1;
1792 }
1793 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001794 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001795 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001796 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001797 return 1;
1798 }
1799 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001800 else if (strcmp(args[1], "table") == 0) {
1801 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1802 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001803 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001804 char *err;
1805
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001806 /* Set flags. */
1807 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001808
1809 /* Expect three parameters: map name, key and new value. */
1810 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001811 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001812 appctx->st0 = STAT_CLI_PRINT;
1813 return 1;
1814 }
1815
1816 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001817 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001818 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001819 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001820 appctx->st0 = STAT_CLI_PRINT;
1821 return 1;
1822 }
1823
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001824 /* If the entry identifier start with a '#', it is considered as
1825 * pointer id
1826 */
1827 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1828 struct pat_ref_elt *ref;
1829 long long int conv;
1830 char *error;
1831
1832 /* Convert argument to integer value. */
1833 conv = strtoll(&args[3][1], &error, 16);
1834 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001835 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001836 appctx->st0 = STAT_CLI_PRINT;
1837 return 1;
1838 }
1839
1840 /* Convert and check integer to pointer. */
1841 ref = (struct pat_ref_elt *)(long)conv;
1842 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001843 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001844 appctx->st0 = STAT_CLI_PRINT;
1845 return 1;
1846 }
1847
1848 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001849 err = NULL;
1850 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1851 if (err)
1852 memprintf(&err, "%s.\n", err);
1853 appctx->ctx.cli.err = err;
1854 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001855 return 1;
1856 }
1857 }
1858 else {
1859 /* Else, use the entry identifier as pattern
1860 * string, and update the value.
1861 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001862 err = NULL;
1863 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1864 if (err)
1865 memprintf(&err, "%s.\n", err);
1866 appctx->ctx.cli.err = err;
1867 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001868 return 1;
1869 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001870 }
1871
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001872 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001873 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001874 return 1;
1875 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001876#ifdef USE_OPENSSL
1877 else if (strcmp(args[1], "ssl") == 0) {
1878 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01001879#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001880 char *err = NULL;
1881
Emeric Brunaf4ef742014-06-19 14:10:45 +02001882 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001883 if (!*args[3]) {
1884 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1885 appctx->st0 = STAT_CLI_PRINT;
1886 return 1;
1887 }
1888
1889 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1890 if (trash.len < 0) {
1891 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1892 appctx->st0 = STAT_CLI_PRINT;
1893 return 1;
1894 }
1895
1896 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1897 if (err) {
1898 memprintf(&err, "%s.\n", err);
1899 appctx->ctx.cli.err = err;
1900 appctx->st0 = STAT_CLI_PRINT_FREE;
1901 }
1902 return 1;
1903 }
1904 appctx->ctx.cli.msg = "OCSP Response updated!";
1905 appctx->st0 = STAT_CLI_PRINT;
1906 return 1;
1907#else
1908 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1909 appctx->st0 = STAT_CLI_PRINT;
1910 return 1;
1911#endif
1912 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001913 else if (strcmp(args[2], "tls-key") == 0) {
1914#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1915 /* Expect two parameters: the filename and the new new TLS key in encoding */
1916 if (!*args[3] || !*args[4]) {
1917 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
1918 appctx->st0 = STAT_CLI_PRINT;
1919 return 1;
1920 }
1921
1922 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
1923 if(!appctx->ctx.tlskeys.ref) {
1924 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
1925 appctx->st0 = STAT_CLI_PRINT;
1926 return 1;
1927 }
1928
1929 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
1930 if (trash.len != sizeof(struct tls_sess_key)) {
1931 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
1932 appctx->st0 = STAT_CLI_PRINT;
1933 return 1;
1934 }
1935
1936 memcpy(appctx->ctx.tlskeys.ref->tlskeys + 2 % TLS_TICKETS_NO, trash.str, trash.len);
1937 appctx->ctx.tlskeys.ref->tls_ticket_enc_index = appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 1 % TLS_TICKETS_NO;
1938
1939 appctx->ctx.cli.msg = "TLS ticket key updated!";
1940 appctx->st0 = STAT_CLI_PRINT;
1941 return 1;
1942#else
1943 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1944 "that doesn't support specifying TLS ticket keys\n";
1945 appctx->st0 = STAT_CLI_PRINT;
1946 return 1;
1947#endif
1948 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001949 else {
1950 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1951 appctx->st0 = STAT_CLI_PRINT;
1952 return 1;
1953 }
1954 }
1955#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001956 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001957 return 0;
1958 }
1959 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001960 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001961 if (strcmp(args[1], "agent") == 0) {
1962 struct server *sv;
1963
1964 sv = expect_server_admin(s, si, args[2]);
1965 if (!sv)
1966 return 1;
1967
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001968 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1969 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1970 appctx->st0 = STAT_CLI_PRINT;
1971 return 1;
1972 }
1973
1974 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001975 return 1;
1976 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001977 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001978 struct server *sv;
1979
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001980 sv = expect_server_admin(s, si, args[2]);
1981 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001982 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001983
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001984 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1985 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1986 appctx->st0 = STAT_CLI_PRINT;
1987 return 1;
1988 }
1989
1990 sv->check.state |= CHK_ST_ENABLED;
1991 return 1;
1992 }
1993 else if (strcmp(args[1], "server") == 0) {
1994 struct server *sv;
1995
1996 sv = expect_server_admin(s, si, args[2]);
1997 if (!sv)
1998 return 1;
1999
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002000 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002001 return 1;
2002 }
2003 else if (strcmp(args[1], "frontend") == 0) {
2004 struct proxy *px;
2005
2006 px = expect_frontend_admin(s, si, args[2]);
2007 if (!px)
2008 return 1;
2009
2010 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002011 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2012 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002013 return 1;
2014 }
2015
2016 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002017 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2018 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002019 return 1;
2020 }
2021
2022 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002023 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2024 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002025 return 1;
2026 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002027 return 1;
2028 }
2029 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002030 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002031 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002032 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002033 }
2034 }
2035 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002036 if (strcmp(args[1], "agent") == 0) {
2037 struct server *sv;
2038
2039 sv = expect_server_admin(s, si, args[2]);
2040 if (!sv)
2041 return 1;
2042
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002043 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002044 return 1;
2045 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002046 else if (strcmp(args[1], "health") == 0) {
2047 struct server *sv;
2048
2049 sv = expect_server_admin(s, si, args[2]);
2050 if (!sv)
2051 return 1;
2052
2053 sv->check.state &= ~CHK_ST_ENABLED;
2054 return 1;
2055 }
Simon Horman671b6f02013-11-25 10:46:39 +09002056 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002057 struct server *sv;
2058
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002059 sv = expect_server_admin(s, si, args[2]);
2060 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002061 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002062
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002063 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002064 return 1;
2065 }
2066 else if (strcmp(args[1], "frontend") == 0) {
2067 struct proxy *px;
2068
2069 px = expect_frontend_admin(s, si, args[2]);
2070 if (!px)
2071 return 1;
2072
2073 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002074 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2075 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002076 return 1;
2077 }
2078
2079 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002080 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2081 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002082 return 1;
2083 }
2084
2085 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002086 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2087 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002088 return 1;
2089 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002090 return 1;
2091 }
2092 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002093 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002094 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002095 return 1;
2096 }
2097 }
2098 else if (strcmp(args[0], "shutdown") == 0) {
2099 if (strcmp(args[1], "frontend") == 0) {
2100 struct proxy *px;
2101
2102 px = expect_frontend_admin(s, si, args[2]);
2103 if (!px)
2104 return 1;
2105
2106 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002107 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2108 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002109 return 1;
2110 }
2111
2112 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2113 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2114 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2115 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2116 stop_proxy(px);
2117 return 1;
2118 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002119 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002120 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002121
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002122 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002123 appctx->ctx.cli.msg = stats_permission_denied_msg;
2124 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002125 return 1;
2126 }
2127
2128 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002129 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2130 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002131 return 1;
2132 }
2133
2134 ptr = (void *)strtoul(args[2], NULL, 0);
2135
Willy Tarreau87b09662015-04-03 00:22:06 +02002136 /* first, look for the requested stream in the stream table */
2137 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002138 if (sess == ptr)
2139 break;
2140 }
2141
Willy Tarreau87b09662015-04-03 00:22:06 +02002142 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002143 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002144 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2145 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002146 return 1;
2147 }
2148
Willy Tarreaue7dff022015-04-03 01:14:29 +02002149 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002150 return 1;
2151 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002152 else if (strcmp(args[1], "sessions") == 0) {
2153 if (strcmp(args[2], "server") == 0) {
2154 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002155 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002156
2157 sv = expect_server_admin(s, si, args[3]);
2158 if (!sv)
2159 return 1;
2160
Willy Tarreau87b09662015-04-03 00:22:06 +02002161 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002162 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2163 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002164 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002165
2166 return 1;
2167 }
2168 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002169 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2170 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002171 return 1;
2172 }
2173 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002174 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002175 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2176 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002177 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002178 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002179 }
2180 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002181 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2182 if (args[1][0] == 'm')
2183 appctx->ctx.map.display_flags = PAT_REF_MAP;
2184 else
2185 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002186
2187 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002188 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2189 if (!*args[2] || !*args[3]) {
2190 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2191 appctx->st0 = STAT_CLI_PRINT;
2192 return 1;
2193 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002194 }
2195
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002196 else {
2197 if (!*args[2] || !*args[3]) {
2198 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2199 appctx->st0 = STAT_CLI_PRINT;
2200 return 1;
2201 }
2202 }
2203
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002204 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002205 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002206 if (!appctx->ctx.map.ref ||
2207 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002208 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002209 appctx->st0 = STAT_CLI_PRINT;
2210 return 1;
2211 }
2212
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002213 /* If the entry identifier start with a '#', it is considered as
2214 * pointer id
2215 */
2216 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2217 struct pat_ref_elt *ref;
2218 long long int conv;
2219 char *error;
2220
2221 /* Convert argument to integer value. */
2222 conv = strtoll(&args[3][1], &error, 16);
2223 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002224 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002225 appctx->st0 = STAT_CLI_PRINT;
2226 return 1;
2227 }
2228
2229 /* Convert and check integer to pointer. */
2230 ref = (struct pat_ref_elt *)(long)conv;
2231 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002232 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002233 appctx->st0 = STAT_CLI_PRINT;
2234 return 1;
2235 }
2236
2237 /* Try to delete the entry. */
2238 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2239 /* The entry is not found, send message. */
2240 appctx->ctx.cli.msg = "Key not found.\n";
2241 appctx->st0 = STAT_CLI_PRINT;
2242 return 1;
2243 }
2244 }
2245 else {
2246 /* Else, use the entry identifier as pattern
2247 * string and try to delete the entry.
2248 */
2249 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2250 /* The entry is not found, send message. */
2251 appctx->ctx.cli.msg = "Key not found.\n";
2252 appctx->st0 = STAT_CLI_PRINT;
2253 return 1;
2254 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002255 }
2256
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002257 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002258 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002259 return 1;
2260 }
2261 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002262 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002263 appctx->st0 = STAT_CLI_PRINT;
2264 return 1;
2265 }
2266 }
2267 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002268 if (strcmp(args[1], "map") == 0 ||
2269 strcmp(args[1], "acl") == 0) {
2270 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002271 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002272
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002273 /* Set flags. */
2274 if (args[1][0] == 'm')
2275 appctx->ctx.map.display_flags = PAT_REF_MAP;
2276 else
2277 appctx->ctx.map.display_flags = PAT_REF_ACL;
2278
2279 /* If the keywork is "map", we expect three parameters, if it
2280 * is "acl", we expect only two parameters
2281 */
2282 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2283 if (!*args[2] || !*args[3] || !*args[4]) {
2284 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2285 appctx->st0 = STAT_CLI_PRINT;
2286 return 1;
2287 }
2288 }
2289 else {
2290 if (!*args[2] || !*args[3]) {
2291 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2292 appctx->st0 = STAT_CLI_PRINT;
2293 return 1;
2294 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002295 }
2296
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002297 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002298 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002299 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002300 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002301 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002302 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002303 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002304 appctx->st0 = STAT_CLI_PRINT;
2305 return 1;
2306 }
2307
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002308 /* The command "add acl" is prohibited if the reference
2309 * use samples.
2310 */
2311 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2312 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2313 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2314 "You must use the command 'add map' to add values.\n";
2315 appctx->st0 = STAT_CLI_PRINT;
2316 return 1;
2317 }
2318
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002319 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002320 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002321 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002322 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002323 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002324 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002325 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002326 if (err)
2327 memprintf(&err, "%s.\n", err);
2328 appctx->ctx.cli.err = err;
2329 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002330 return 1;
2331 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002332
2333 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002334 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002335 return 1;
2336 }
2337 else { /* unknown "del" parameter */
2338 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2339 appctx->st0 = STAT_CLI_PRINT;
2340 return 1;
2341 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002342 }
2343 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002344 return 0;
2345 }
2346 return 1;
2347}
2348
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002349/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002350 * used to processes I/O from/to the stats unix socket. The system relies on a
2351 * state machine handling requests and various responses. We read a request,
2352 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002353 * Then we can read again. The state is stored in appctx->st0 and is one of the
2354 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002355 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002356 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002357static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002358{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002359 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002360 struct channel *req = si_oc(si);
2361 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002362 int reql;
2363 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002364
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002365 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2366 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002367
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002368 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002369 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002370 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002371 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2372 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002373 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002374 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002375 /* Let's close for real now. We just close the request
2376 * side, the conditions below will complete if needed.
2377 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002378 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002379 break;
2380 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002381 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002382 /* ensure we have some output room left in the event we
2383 * would want to return some info right after parsing.
2384 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002385 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002386 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002387 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002388 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002389
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002390 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002391 if (reql <= 0) { /* closed or EOL not found */
2392 if (reql == 0)
2393 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002394 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002395 continue;
2396 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002397
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002398 /* seek for a possible semi-colon. If we find one, we
2399 * replace it with an LF and skip only this part.
2400 */
2401 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002402 if (trash.str[len] == ';') {
2403 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002404 reql = len + 1;
2405 break;
2406 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002407
Willy Tarreau816fc222009-10-04 07:36:58 +02002408 /* now it is time to check that we have a full line,
2409 * remove the trailing \n and possibly \r, then cut the
2410 * line.
2411 */
2412 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002413 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002414 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002415 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002416 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002417
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002418 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002419 len--;
2420
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002421 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002422
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002423 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002424 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002425 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002426 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002427 continue;
2428 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002429 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002430 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002431 else if (strcmp(trash.str, "help") == 0 ||
2432 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002433 appctx->ctx.cli.msg = stats_sock_usage_msg;
2434 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002435 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002436 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002437 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002438 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002439 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002440 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002441 /* if prompt is disabled, print help on empty lines,
2442 * so that the user at least knows how to enable
2443 * prompt and find help.
2444 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002445 appctx->ctx.cli.msg = stats_sock_usage_msg;
2446 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002447 }
2448
2449 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002450 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002451 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002452 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002453 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002454 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau00a37f02015-04-13 12:05:19 +02002455 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002456 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002457 continue;
2458 }
2459
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002460 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002461 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002462 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002463 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002464 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002465 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002466 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002467 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002468 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002469 free(appctx->ctx.cli.err);
2470 appctx->st0 = STAT_CLI_PROMPT;
2471 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002472 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002473 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002474 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002475 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002476 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002477 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002478 break;
2479 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002480 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002481 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002482 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002483 case STAT_CLI_O_RESOLVERS:
2484 if (stats_dump_resolvers_to_buffer(si))
2485 appctx->st0 = STAT_CLI_PROMPT;
2486 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002487 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002488 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002489 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002490 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002491 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002492 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002493 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002494 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002495 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002496 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002497 if (stats_table_request(si, appctx->st0))
2498 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002499 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002500 case STAT_CLI_O_PATS:
2501 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002502 appctx->st0 = STAT_CLI_PROMPT;
2503 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002504 case STAT_CLI_O_PAT:
2505 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002506 appctx->st0 = STAT_CLI_PROMPT;
2507 break;
2508 case STAT_CLI_O_MLOOK:
2509 if (stats_map_lookup(si))
2510 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002511 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002512 case STAT_CLI_O_POOLS:
2513 if (stats_dump_pools_to_buffer(si))
2514 appctx->st0 = STAT_CLI_PROMPT;
2515 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002516#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2517 case STAT_CLI_O_TLSK:
2518 if (stats_tlskeys_list(si))
2519 appctx->st0 = STAT_CLI_PROMPT;
2520 break;
2521#endif
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002522 default: /* abnormal state */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002523 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002524 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002525 break;
2526 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002527
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002528 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002529 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002530 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002531 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002532 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002533 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002534 }
2535
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002536 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002537 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002538 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002539
2540 /* Now we close the output if one of the writers did so,
2541 * or if we're not in interactive mode and the request
2542 * buffer is empty. This still allows pipelined requests
2543 * to be sent in non-interactive mode.
2544 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002545 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2546 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002547 continue;
2548 }
2549
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002550 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002551 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002552 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002553 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002554
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002555 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002556 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2557 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002558 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002559 * and nothing more to consume. This is comparable to a broken pipe, so
2560 * we forward the close to the request side so that it flows upstream to
2561 * the client.
2562 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002563 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002564 }
2565
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002566 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002567 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2568 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2569 /* We have no more processing to do, and nothing more to send, and
2570 * the client side has closed. So we'll forward this state downstream
2571 * on the response buffer.
2572 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002573 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002574 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002575 }
2576
Willy Tarreau828824a2015-04-19 17:20:03 +02002577 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002578 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 +02002579 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002580 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002581}
2582
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002583/* This function dumps information onto the stream interface's read buffer.
2584 * It returns 0 as long as it does not complete, non-zero upon completion.
2585 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002586 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002587static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002588{
2589 unsigned int up = (now.tv_sec - start_date.tv_sec);
2590
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002591#ifdef USE_OPENSSL
2592 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2593 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2594 int ssl_reuse = 0;
2595
2596 if (ssl_key_rate < ssl_sess_rate) {
2597 /* count the ssl reuse ratio and avoid overflows in both directions */
2598 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2599 }
2600#endif
2601
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002602 chunk_printf(&trash,
2603 "Name: " PRODUCT_NAME "\n"
2604 "Version: " HAPROXY_VERSION "\n"
2605 "Release_date: " HAPROXY_DATE "\n"
2606 "Nbproc: %d\n"
2607 "Process_num: %d\n"
2608 "Pid: %d\n"
2609 "Uptime: %dd %dh%02dm%02ds\n"
2610 "Uptime_sec: %d\n"
2611 "Memmax_MB: %d\n"
2612 "Ulimit-n: %d\n"
2613 "Maxsock: %d\n"
2614 "Maxconn: %d\n"
2615 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002616 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002617 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002618 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002619#ifdef USE_OPENSSL
2620 "MaxSslConns: %d\n"
2621 "CurrSslConns: %d\n"
2622 "CumSslConns: %d\n"
2623#endif
2624 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002625 "PipesUsed: %d\n"
2626 "PipesFree: %d\n"
2627 "ConnRate: %d\n"
2628 "ConnRateLimit: %d\n"
2629 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002630 "SessRate: %d\n"
2631 "SessRateLimit: %d\n"
2632 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002633#ifdef USE_OPENSSL
2634 "SslRate: %d\n"
2635 "SslRateLimit: %d\n"
2636 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002637 "SslFrontendKeyRate: %d\n"
2638 "SslFrontendMaxKeyRate: %d\n"
2639 "SslFrontendSessionReuse_pct: %d\n"
2640 "SslBackendKeyRate: %d\n"
2641 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002642 "SslCacheLookups: %u\n"
2643 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002644#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002645 "CompressBpsIn: %u\n"
2646 "CompressBpsOut: %u\n"
2647 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002648#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002649 "ZlibMemUsage: %ld\n"
2650 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002651#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002652 "Tasks: %d\n"
2653 "Run_queue: %d\n"
2654 "Idle_pct: %d\n"
2655 "node: %s\n"
2656 "description: %s\n"
2657 "",
2658 global.nbproc,
2659 relative_pid,
2660 pid,
2661 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2662 up,
2663 global.rlimit_memmax,
2664 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002665 global.maxsock, global.maxconn, global.hardmaxconn,
2666 actconn, totalconn, global.req_count,
2667#ifdef USE_OPENSSL
2668 global.maxsslconn, sslconns, totalsslconns,
2669#endif
2670 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002671 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002672 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002673#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002674 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2675 ssl_key_rate, global.ssl_fe_keys_max,
2676 ssl_reuse,
2677 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002678 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002679#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002680 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2681 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002682#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002683 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002684#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002685 nb_tasks_cur, run_queue_cur, idle_pct,
2686 global.node, global.desc ? global.desc : ""
2687 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002688
Willy Tarreaubc18da12015-03-13 14:00:47 +01002689 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002690 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002691 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002692 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002693
2694 return 1;
2695}
2696
Willy Tarreau12833bb2014-01-28 16:49:56 +01002697/* This function dumps memory usage information onto the stream interface's
2698 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2699 * completion. No state is used.
2700 */
2701static int stats_dump_pools_to_buffer(struct stream_interface *si)
2702{
2703 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01002704 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002705 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01002706 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002707 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01002708 return 1;
2709}
2710
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002711/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2712 * the state from stream interface <si>. The caller is responsible for clearing
2713 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002714 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002715static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002716{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002717 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002718 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002719
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002720 if (!(px->cap & PR_CAP_FE))
2721 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002722
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002723 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002724 return 0;
2725
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002726 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002727 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002728 /* name, queue */
2729 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002730
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002731 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002732 /* Column sub-heading for Enable or Disable server */
2733 chunk_appendf(&trash, "<td></td>");
2734 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002735
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002736 chunk_appendf(&trash,
2737 "<td class=ac>"
2738 "<a name=\"%s/Frontend\"></a>"
2739 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2740 "<td colspan=3></td>"
2741 "",
2742 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002743
Willy Tarreau466c9b52012-12-23 02:25:03 +01002744 chunk_appendf(&trash,
2745 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002746 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002747 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2748 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2749 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002750 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2751 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2752 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002753
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002754 if (px->mode == PR_MODE_HTTP)
2755 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002756 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002757 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002758
2759 chunk_appendf(&trash,
2760 "</table></div></u></td>"
2761 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002762 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002763 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2764 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2765 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002766 U2H(px->fe_counters.sps_max),
2767 U2H(px->fe_counters.cps_max),
2768 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002769
2770 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002771 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002772 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002773 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002774
2775 chunk_appendf(&trash,
2776 "</table></div></u></td>"
2777 /* sessions rate : limit */
2778 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002779 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002780
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002781 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002782 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002783 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002784 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002785 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2786 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002787 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002788 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2789 U2H(px->fe_counters.cum_sess),
2790 U2H(px->fe_counters.cum_conn),
2791 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002792
Willy Tarreau466c9b52012-12-23 02:25:03 +01002793 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002794 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002795 chunk_appendf(&trash,
2796 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2797 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2798 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2799 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2800 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2801 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2802 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2803 "<tr><th>- other responses:</th><td>%s</td></tr>"
2804 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2805 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002806 U2H(px->fe_counters.p.http.cum_req),
2807 U2H(px->fe_counters.p.http.rsp[1]),
2808 U2H(px->fe_counters.p.http.rsp[2]),
2809 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002810 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002811 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002812 U2H(px->fe_counters.p.http.rsp[3]),
2813 U2H(px->fe_counters.p.http.rsp[4]),
2814 U2H(px->fe_counters.p.http.rsp[5]),
2815 U2H(px->fe_counters.p.http.rsp[0]),
2816 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002817 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002818
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002819 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002820 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002821 /* sessions: lbtot, lastsess */
2822 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002823 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002824 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002825 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002826 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002827
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002828 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002829 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002830 "<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 +01002831 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002832 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002833 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2834 px->fe_counters.comp_in ?
2835 (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 +01002836 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002837
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002838 chunk_appendf(&trash,
2839 /* denied: req, resp */
2840 "<td>%s</td><td>%s</td>"
2841 /* errors : request, connect, response */
2842 "<td>%s</td><td></td><td></td>"
2843 /* warnings: retries, redispatches */
2844 "<td></td><td></td>"
2845 /* server status : reflect frontend status */
2846 "<td class=ac>%s</td>"
2847 /* rest of server: nothing */
2848 "<td class=ac colspan=8></td></tr>"
2849 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002850 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2851 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002852 px->state == PR_STREADY ? "OPEN" :
2853 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002854 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002855 else { /* CSV mode */
2856 chunk_appendf(&trash,
2857 /* pxid, name, queue cur, queue max, */
2858 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002859 /* sessions : current, max, limit, total */
2860 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002861 /* bytes : in, out */
2862 "%lld,%lld,"
2863 /* denied: req, resp */
2864 "%lld,%lld,"
2865 /* errors : request, connect, response */
2866 "%lld,,,"
2867 /* warnings: retries, redispatches */
2868 ",,"
2869 /* server status : reflect frontend status */
2870 "%s,"
2871 /* rest of server: nothing */
2872 ",,,,,,,,"
2873 /* pid, iid, sid, throttle, lbtot, tracked, type */
2874 "%d,%d,0,,,,%d,"
2875 /* rate, rate_lim, rate_max */
2876 "%u,%u,%u,"
2877 /* check_status, check_code, check_duration */
2878 ",,,",
2879 px->id,
2880 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2881 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2882 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2883 px->fe_counters.failed_req,
2884 px->state == PR_STREADY ? "OPEN" :
2885 px->state == PR_STFULL ? "FULL" : "STOP",
2886 relative_pid, px->uuid, STATS_TYPE_FE,
2887 read_freq_ctr(&px->fe_sess_per_sec),
2888 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002889
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002890 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2891 if (px->mode == PR_MODE_HTTP) {
2892 for (i=1; i<6; i++)
2893 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2894 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2895 }
2896 else
2897 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002898
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002899 /* failed health analyses */
2900 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002901
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002902 /* requests : req_rate, req_rate_max, req_tot, */
2903 chunk_appendf(&trash, "%u,%u,%lld,",
2904 read_freq_ctr(&px->fe_req_per_sec),
2905 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2906
2907 /* errors: cli_aborts, srv_aborts */
2908 chunk_appendf(&trash, ",,");
2909
2910 /* compression: in, out, bypassed */
2911 chunk_appendf(&trash, "%lld,%lld,%lld,",
2912 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2913
2914 /* compression: comp_rsp */
2915 chunk_appendf(&trash, "%lld,",
2916 px->fe_counters.p.http.comp_rsp);
2917
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002918 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2919 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002920
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002921 /* finish with EOL */
2922 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002923 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002924 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002925}
2926
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002927/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2928 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2929 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2930 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002931 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002932static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002933{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002934 struct appctx *appctx = __objt_appctx(si->end);
2935
2936 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002937 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002938 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002939 /* Column sub-heading for Enable or Disable server */
2940 chunk_appendf(&trash, "<td></td>");
2941 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002942 chunk_appendf(&trash,
2943 /* frontend name, listener name */
2944 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2945 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2946 "",
2947 px->id, l->name,
2948 (flags & ST_SHLGNDS)?"<u>":"",
2949 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002950
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002951 if (flags & ST_SHLGNDS) {
2952 char str[INET6_ADDRSTRLEN];
2953 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002954
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002955 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002956
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002957 port = get_host_port(&l->addr);
2958 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2959 case AF_INET:
2960 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2961 break;
2962 case AF_INET6:
2963 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2964 break;
2965 case AF_UNIX:
2966 chunk_appendf(&trash, "unix, ");
2967 break;
2968 case -1:
2969 chunk_appendf(&trash, "(%s), ", strerror(errno));
2970 break;
2971 }
Willy Tarreau91861262007-10-17 17:06:05 +02002972
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002973 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002974 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002975 }
Willy Tarreau91861262007-10-17 17:06:05 +02002976
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002977 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002978 /* queue */
2979 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002980 /* sessions rate: current, max, limit */
2981 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002982 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002983 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002984 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002985 /* bytes: in, out */
2986 "<td>%s</td><td>%s</td>"
2987 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002988 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002989 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2990 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002991
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002992 chunk_appendf(&trash,
2993 /* denied: req, resp */
2994 "<td>%s</td><td>%s</td>"
2995 /* errors: request, connect, response */
2996 "<td>%s</td><td></td><td></td>"
2997 /* warnings: retries, redispatches */
2998 "<td></td><td></td>"
2999 /* server status: reflect listener status */
3000 "<td class=ac>%s</td>"
3001 /* rest of server: nothing */
3002 "<td class=ac colspan=8></td></tr>"
3003 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003004 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
3005 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003006 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
3007 }
3008 else { /* CSV mode */
3009 chunk_appendf(&trash,
3010 /* pxid, name, queue cur, queue max, */
3011 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003012 /* sessions: current, max, limit, total */
3013 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003014 /* bytes: in, out */
3015 "%lld,%lld,"
3016 /* denied: req, resp */
3017 "%lld,%lld,"
3018 /* errors: request, connect, response */
3019 "%lld,,,"
3020 /* warnings: retries, redispatches */
3021 ",,"
3022 /* server status: reflect listener status */
3023 "%s,"
3024 /* rest of server: nothing */
3025 ",,,,,,,,"
3026 /* pid, iid, sid, throttle, lbtot, tracked, type */
3027 "%d,%d,%d,,,,%d,"
3028 /* rate, rate_lim, rate_max */
3029 ",,,"
3030 /* check_status, check_code, check_duration */
3031 ",,,"
3032 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3033 ",,,,,,"
3034 /* failed health analyses */
3035 ","
3036 /* requests : req_rate, req_rate_max, req_tot, */
3037 ",,,"
3038 /* errors: cli_aborts, srv_aborts */
3039 ",,"
3040 /* compression: in, out, bypassed, comp_rsp */
3041 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003042 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3043 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003044 "\n",
3045 px->id, l->name,
3046 l->nbconn, l->counters->conn_max,
3047 l->maxconn, l->counters->cum_conn,
3048 l->counters->bytes_in, l->counters->bytes_out,
3049 l->counters->denied_req, l->counters->denied_resp,
3050 l->counters->failed_req,
3051 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
3052 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
3053 }
3054 return 1;
3055}
Willy Tarreau91861262007-10-17 17:06:05 +02003056
Simon Horman4d2eab62015-04-23 14:51:26 +09003057enum srv_stats_state {
3058 SRV_STATS_STATE_DOWN = 0,
3059 SRV_STATS_STATE_DOWN_AGENT,
3060 SRV_STATS_STATE_GOING_UP,
3061 SRV_STATS_STATE_UP_GOING_DOWN,
3062 SRV_STATS_STATE_UP,
3063 SRV_STATS_STATE_NOLB_GOING_DOWN,
3064 SRV_STATS_STATE_NOLB,
3065 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3066 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003067 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003068 SRV_STATS_STATE_NO_CHECK,
3069
3070 SRV_STATS_STATE_COUNT, /* Must be last */
3071};
3072
Simon Horman837bfa72015-04-23 14:51:27 +09003073enum srv_stats_colour {
3074 SRV_STATS_COLOUR_DOWN = 0,
3075 SRV_STATS_COLOUR_GOING_UP,
3076 SRV_STATS_COLOUR_GOING_DOWN,
3077 SRV_STATS_COLOUR_UP,
3078 SRV_STATS_COLOUR_NOLB,
3079 SRV_STATS_COLOUR_DRAINING,
3080 SRV_STATS_COLOUR_NO_CHECK,
3081
3082 SRV_STATS_COLOUR_COUNT, /* Must be last */
3083};
3084
3085static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3086 [SRV_STATS_COLOUR_DOWN] = "down",
3087 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3088 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3089 [SRV_STATS_COLOUR_UP] = "up",
3090 [SRV_STATS_COLOUR_NOLB] = "nolb",
3091 [SRV_STATS_COLOUR_DRAINING] = "draining",
3092 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3093};
3094
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003095/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3096 * from stream interface <si>, stats flags <flags>, and server state <state>.
3097 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003098 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003099 */
Simon Horman4d2eab62015-04-23 14:51:26 +09003100static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09003101 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003102{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003103 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003104 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003105 char str[INET6_ADDRSTRLEN];
3106 struct chunk src;
3107 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02003108
Willy Tarreau32091232014-05-16 13:52:00 +02003109 /* we have "via" which is the tracked server as described in the configuration,
3110 * and "ref" which is the checked server and the end of the chain.
3111 */
3112 via = sv->track ? sv->track : sv;
3113 ref = via;
3114 while (ref->track)
3115 ref = ref->track;
3116
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003117 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003118 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3119 [SRV_STATS_STATE_DOWN] = "DOWN",
3120 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3121 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
3122 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
3123 [SRV_STATS_STATE_UP] = "UP",
3124 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
3125 [SRV_STATS_STATE_NOLB] = "NOLB",
3126 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
3127 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003128 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003129 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003130 };
Willy Tarreau91861262007-10-17 17:06:05 +02003131
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003132 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003133 chunk_appendf(&trash, "<tr class=\"maintain\">");
3134 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003135 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003136 "<tr class=\"%s_%s\">",
3137 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003138
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003139 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003140 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003141 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3142 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003143
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003144 chunk_appendf(&trash,
3145 "<td class=ac><a name=\"%s/%s\"></a>%s"
3146 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3147 "",
3148 px->id, sv->id,
3149 (flags & ST_SHLGNDS) ? "<u>" : "",
3150 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003151
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003152 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003153 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003154
3155 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3156 case AF_INET:
3157 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3158 break;
3159 case AF_INET6:
3160 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3161 break;
3162 case AF_UNIX:
3163 chunk_appendf(&trash, "unix, ");
3164 break;
3165 case -1:
3166 chunk_appendf(&trash, "(%s), ", strerror(errno));
3167 break;
3168 default: /* address family not supported */
3169 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003170 }
Willy Tarreau91861262007-10-17 17:06:05 +02003171
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003172 /* id */
3173 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003174
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003175 /* cookie */
3176 if (sv->cookie) {
3177 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003178
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003179 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3180 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003181
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003182 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003183 }
3184
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003185 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003186 }
Willy Tarreau91861262007-10-17 17:06:05 +02003187
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003188 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003189 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003190 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003191 /* sessions rate : current, max, limit */
3192 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003193 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003194 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003195 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3196 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003197
Willy Tarreau466c9b52012-12-23 02:25:03 +01003198
3199 chunk_appendf(&trash,
3200 /* sessions: current, max, limit, total */
3201 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003202 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003203 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3204 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003205 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3206 U2H(sv->counters.cum_sess),
3207 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003208
Willy Tarreau466c9b52012-12-23 02:25:03 +01003209 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3210 if (px->mode == PR_MODE_HTTP) {
3211 unsigned long long tot;
3212 for (tot = i = 0; i < 6; i++)
3213 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003214
Willy Tarreau466c9b52012-12-23 02:25:03 +01003215 chunk_appendf(&trash,
3216 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3217 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3218 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3219 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3220 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3221 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3222 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3223 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003224 U2H(tot),
3225 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3226 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3227 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3228 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3229 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3230 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 +02003231 }
Willy Tarreau91861262007-10-17 17:06:05 +02003232
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003233 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3234 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)));
3235 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)));
3236 if (px->mode == PR_MODE_HTTP)
3237 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)));
3238 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)));
3239
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003240 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003241 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003242 /* sessions: lbtot, last */
3243 "<td>%s</td><td>%s</td>",
3244 U2H(sv->counters.cum_lbconn),
3245 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003246
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003247 chunk_appendf(&trash,
3248 /* bytes : in, out */
3249 "<td>%s</td><td>%s</td>"
3250 /* denied: req, resp */
3251 "<td></td><td>%s</td>"
3252 /* errors : request, connect */
3253 "<td></td><td>%s</td>"
3254 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003255 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003256 /* warnings: retries, redispatches */
3257 "<td>%lld</td><td>%lld</td>"
3258 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003259 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3260 U2H(sv->counters.failed_secu),
3261 U2H(sv->counters.failed_conns),
3262 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003263 sv->counters.cli_aborts,
3264 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003265 sv->counters.retries, sv->counters.redispatches);
3266
3267 /* status, lest check */
3268 chunk_appendf(&trash, "<td class=ac>");
3269
Willy Tarreau20125212014-05-13 19:44:56 +02003270 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003271 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3272 chunk_appendf(&trash, "MAINT");
3273 }
Simon Horman0766e442014-11-12 15:55:54 +09003274 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003275 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003276 /* DOWN (agent) */
3277 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 +02003278 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003279 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003280 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3281 chunk_appendf(&trash,
3282 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003283 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3284 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003285 }
Willy Tarreau91861262007-10-17 17:06:05 +02003286
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003287 if ((sv->state == SRV_ST_STOPPED) &&
3288 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3289 chunk_appendf(&trash,
3290 "</td><td class=ac><u> %s%s",
3291 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3292 get_check_status_info(sv->agent.status));
3293
3294 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3295 chunk_appendf(&trash, "/%d", sv->agent.code);
3296
3297 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3298 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3299
3300 chunk_appendf(&trash, "<div class=tips>%s",
3301 get_check_status_description(sv->agent.status));
3302 if (*sv->agent.desc) {
3303 chunk_appendf(&trash, ": ");
3304 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3305 chunk_htmlencode(&trash, &src);
3306 }
3307 chunk_appendf(&trash, "</div></u>");
3308 }
3309 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003310 chunk_appendf(&trash,
3311 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003312 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003313 get_check_status_info(sv->check.status));
3314
3315 if (sv->check.status >= HCHK_STATUS_L57DATA)
3316 chunk_appendf(&trash, "/%d", sv->check.code);
3317
3318 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003319 chunk_appendf(&trash, " in %lums", sv->check.duration);
3320
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003321 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003322 get_check_status_description(sv->check.status));
3323 if (*sv->check.desc) {
3324 chunk_appendf(&trash, ": ");
3325 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3326 chunk_htmlencode(&trash, &src);
3327 }
3328 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003329 }
3330 else
3331 chunk_appendf(&trash, "</td><td>");
3332
3333 chunk_appendf(&trash,
3334 /* weight */
3335 "</td><td class=ac>%d</td>"
3336 /* act, bck */
3337 "<td class=ac>%s</td><td class=ac>%s</td>"
3338 "",
3339 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003340 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3341 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003342
3343 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003344 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003345 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003346
3347 if (ref->observe)
3348 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3349
3350 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003351 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003352 "<td>%lld</td><td>%s</td>"
3353 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003354 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003355 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3356 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003357 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3358 /* tracking a server */
3359 chunk_appendf(&trash,
3360 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003361 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003362 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 else
3364 chunk_appendf(&trash, "<td colspan=3></td>");
3365
3366 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003367 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003368 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369 else
3370 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3371 }
3372 else { /* CSV mode */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003373 struct chunk *out = get_trash_chunk();
Simon Horman4d2eab62015-04-23 14:51:26 +09003374 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3375 [SRV_STATS_STATE_DOWN] = "DOWN,",
3376 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3377 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3378 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3379 [SRV_STATS_STATE_UP] = "UP,",
3380 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3381 [SRV_STATS_STATE_NOLB] = "NOLB,",
3382 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3383 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003384 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003385 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003386 };
3387
3388 chunk_appendf(&trash,
3389 /* pxid, name */
3390 "%s,%s,"
3391 /* queue : current, max */
3392 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003393 /* sessions : current, max, limit, total */
3394 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003395 /* bytes : in, out */
3396 "%lld,%lld,"
3397 /* denied: req, resp */
3398 ",%lld,"
3399 /* errors : request, connect, response */
3400 ",%lld,%lld,"
3401 /* warnings: retries, redispatches */
3402 "%lld,%lld,"
3403 "",
3404 px->id, sv->id,
3405 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003406 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003407 sv->counters.bytes_in, sv->counters.bytes_out,
3408 sv->counters.failed_secu,
3409 sv->counters.failed_conns, sv->counters.failed_resp,
3410 sv->counters.retries, sv->counters.redispatches);
3411
3412 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003413 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003414 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003415 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003416 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003417 else
3418 chunk_appendf(&trash,
3419 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003420 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3421 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422
3423 chunk_appendf(&trash,
3424 /* weight, active, backup */
3425 "%d,%d,%d,"
3426 "",
3427 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003428 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3429 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003430
3431 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003432 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003433 chunk_appendf(&trash,
3434 "%lld,%lld,%d,%d,",
3435 sv->counters.failed_checks, sv->counters.down_trans,
3436 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3437 else
3438 chunk_appendf(&trash, ",,,,");
3439
3440 /* queue limit, pid, iid, sid, */
3441 chunk_appendf(&trash,
3442 "%s,"
3443 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003444 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003445 relative_pid, px->uuid, sv->puid);
3446
3447 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003448 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003449 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003450
3451 /* sessions: lbtot */
3452 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3453
3454 /* tracked */
3455 if (sv->track)
3456 chunk_appendf(&trash, "%s/%s,",
3457 sv->track->proxy->id, sv->track->id);
3458 else
3459 chunk_appendf(&trash, ",");
3460
3461 /* type */
3462 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3463
3464 /* rate */
3465 chunk_appendf(&trash, "%u,,%u,",
3466 read_freq_ctr(&sv->sess_per_sec),
3467 sv->counters.sps_max);
3468
Willy Tarreauff5ae352013-12-11 20:36:34 +01003469 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003470 /* check_status */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003471 chunk_appendf(&trash, "%s,", csv_enc(get_check_status_info(sv->check.status), 1, out));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003472
3473 /* check_code */
3474 if (sv->check.status >= HCHK_STATUS_L57DATA)
3475 chunk_appendf(&trash, "%u,", sv->check.code);
3476 else
3477 chunk_appendf(&trash, ",");
3478
3479 /* check_duration */
3480 if (sv->check.status >= HCHK_STATUS_CHECKED)
3481 chunk_appendf(&trash, "%lu,", sv->check.duration);
3482 else
3483 chunk_appendf(&trash, ",");
3484
3485 }
3486 else
3487 chunk_appendf(&trash, ",,,");
3488
3489 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3490 if (px->mode == PR_MODE_HTTP) {
3491 for (i=1; i<6; i++)
3492 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3493
3494 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3495 }
3496 else
3497 chunk_appendf(&trash, ",,,,,,");
3498
3499 /* failed health analyses */
3500 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3501
3502 /* requests : req_rate, req_rate_max, req_tot, */
3503 chunk_appendf(&trash, ",,,");
3504
3505 /* errors: cli_aborts, srv_aborts */
3506 chunk_appendf(&trash, "%lld,%lld,",
3507 sv->counters.cli_aborts, sv->counters.srv_aborts);
3508
3509 /* compression: in, out, bypassed, comp_rsp */
3510 chunk_appendf(&trash, ",,,,");
3511
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003512 /* lastsess */
3513 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3514
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003515 /* capture of last check and agent statuses */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003516 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->check.desc), 1, out) : "");
3517 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->agent.desc), 1, out) : "");
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003518
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003519 /* qtime, ctime, rtime, ttime, */
3520 chunk_appendf(&trash, "%u,%u,%u,%u,",
3521 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3522 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3523 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3524 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3525
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003526 /* finish with EOL */
3527 chunk_appendf(&trash, "\n");
3528 }
3529 return 1;
3530}
3531
3532/* Dumps a line for backend <px> to the trash for and uses the state from stream
3533 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3534 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3535 */
3536static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3537{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003538 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003539 struct chunk src;
3540 int i;
3541
3542 if (!(px->cap & PR_CAP_BE))
3543 return 0;
3544
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003545 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003546 return 0;
3547
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003548 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003549 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003550 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003551 /* Column sub-heading for Enable or Disable server */
3552 chunk_appendf(&trash, "<td></td>");
3553 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003554 chunk_appendf(&trash,
3555 "<td class=ac>"
3556 /* name */
3557 "%s<a name=\"%s/Backend\"></a>"
3558 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3559 "",
3560 (flags & ST_SHLGNDS)?"<u>":"",
3561 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003562
3563 if (flags & ST_SHLGNDS) {
3564 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003565 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003566 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3567
3568 /* cookie */
3569 if (px->cookie_name) {
3570 chunk_appendf(&trash, ", cookie: '");
3571 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3572 chunk_htmlencode(&trash, &src);
3573 chunk_appendf(&trash, "'");
3574 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003575 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003576 }
3577
3578 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003579 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003580 /* queue : current, max */
3581 "<td>%s</td><td>%s</td><td></td>"
3582 /* sessions rate : current, max, limit */
3583 "<td>%s</td><td>%s</td><td></td>"
3584 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003585 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003586 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3587 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003588
3589 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003590 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003591 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003592 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003593 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003594 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003595 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3596 U2H(px->be_counters.cum_conn),
3597 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003598
Willy Tarreau466c9b52012-12-23 02:25:03 +01003599 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003600 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003601 chunk_appendf(&trash,
3602 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3603 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3604 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3605 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3606 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3607 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3608 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3609 "<tr><th>- other responses:</th><td>%s</td></tr>"
3610 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003611 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003612 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003613 U2H(px->be_counters.p.http.cum_req),
3614 U2H(px->be_counters.p.http.rsp[1]),
3615 U2H(px->be_counters.p.http.rsp[2]),
3616 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003617 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003618 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003619 U2H(px->be_counters.p.http.rsp[3]),
3620 U2H(px->be_counters.p.http.rsp[4]),
3621 U2H(px->be_counters.p.http.rsp[5]),
3622 U2H(px->be_counters.p.http.rsp[0]),
3623 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003624 }
3625
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003626 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)));
3627 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)));
3628 if (px->mode == PR_MODE_HTTP)
3629 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)));
3630 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)));
3631
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003632 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003633 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003634 /* sessions: lbtot, last */
3635 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003636 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003637 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003638 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003639 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003640 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003641 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003642
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003643 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003644 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003645 "<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 +01003646 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003647 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003648 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3649 px->be_counters.comp_in ?
3650 (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 +01003651 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3652
3653 chunk_appendf(&trash,
3654 /* denied: req, resp */
3655 "<td>%s</td><td>%s</td>"
3656 /* errors : request, connect */
3657 "<td></td><td>%s</td>"
3658 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003659 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003660 /* warnings: retries, redispatches */
3661 "<td>%lld</td><td>%lld</td>"
3662 /* backend status: reflect backend status (up/down): we display UP
3663 * if the backend has known working servers or if it has no server at
3664 * all (eg: for stats). Then we display the total weight, number of
3665 * active and backups. */
3666 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3667 "<td class=ac>%d</td><td class=ac>%d</td>"
3668 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003669 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3670 U2H(px->be_counters.failed_conns),
3671 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003672 px->be_counters.cli_aborts,
3673 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003674 px->be_counters.retries, px->be_counters.redispatches,
3675 human_time(now.tv_sec - px->last_change, 1),
3676 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3677 "<font color=\"red\"><b>DOWN</b></font>",
3678 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3679 px->srv_act, px->srv_bck);
3680
3681 chunk_appendf(&trash,
3682 /* rest of backend: nothing, down transitions, total downtime, throttle */
3683 "<td class=ac>&nbsp;</td><td>%d</td>"
3684 "<td>%s</td>"
3685 "<td></td>"
3686 "</tr>",
3687 px->down_trans,
3688 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3689 }
3690 else { /* CSV mode */
3691 chunk_appendf(&trash,
3692 /* pxid, name */
3693 "%s,BACKEND,"
3694 /* queue : current, max */
3695 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003696 /* sessions : current, max, limit, total */
3697 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003698 /* bytes : in, out */
3699 "%lld,%lld,"
3700 /* denied: req, resp */
3701 "%lld,%lld,"
3702 /* errors : request, connect, response */
3703 ",%lld,%lld,"
3704 /* warnings: retries, redispatches */
3705 "%lld,%lld,"
3706 /* backend status: reflect backend status (up/down): we display UP
3707 * if the backend has known working servers or if it has no server at
3708 * all (eg: for stats). Then we display the total weight, number of
3709 * active and backups. */
3710 "%s,"
3711 "%d,%d,%d,"
3712 /* rest of backend: nothing, down transitions, last change, total downtime */
3713 ",%d,%d,%d,,"
3714 /* pid, iid, sid, throttle, lbtot, tracked, type */
3715 "%d,%d,0,,%lld,,%d,"
3716 /* rate, rate_lim, rate_max, */
3717 "%u,,%u,"
3718 /* check_status, check_code, check_duration */
3719 ",,,",
3720 px->id,
3721 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3722 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3723 px->be_counters.bytes_in, px->be_counters.bytes_out,
3724 px->be_counters.denied_req, px->be_counters.denied_resp,
3725 px->be_counters.failed_conns, px->be_counters.failed_resp,
3726 px->be_counters.retries, px->be_counters.redispatches,
3727 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3728 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3729 px->srv_act, px->srv_bck,
3730 px->down_trans, (int)(now.tv_sec - px->last_change),
3731 px->srv?be_downtime(px):0,
3732 relative_pid, px->uuid,
3733 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3734 read_freq_ctr(&px->be_sess_per_sec),
3735 px->be_counters.sps_max);
3736
3737 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3738 if (px->mode == PR_MODE_HTTP) {
3739 for (i=1; i<6; i++)
3740 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3741 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3742 }
3743 else
3744 chunk_appendf(&trash, ",,,,,,");
3745
3746 /* failed health analyses */
3747 chunk_appendf(&trash, ",");
3748
3749 /* requests : req_rate, req_rate_max, req_tot, */
3750 chunk_appendf(&trash, ",,,");
3751
3752 /* errors: cli_aborts, srv_aborts */
3753 chunk_appendf(&trash, "%lld,%lld,",
3754 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3755
3756 /* compression: in, out, bypassed */
3757 chunk_appendf(&trash, "%lld,%lld,%lld,",
3758 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3759
3760 /* compression: comp_rsp */
3761 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3762
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003763 /* lastsess, last_chk, last_agt, */
3764 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003765
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003766 /* qtime, ctime, rtime, ttime, */
3767 chunk_appendf(&trash, "%u,%u,%u,%u,",
3768 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3769 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3770 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3771 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3772
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003773 /* finish with EOL */
3774 chunk_appendf(&trash, "\n");
3775 }
3776 return 1;
3777}
3778
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003779/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003780 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003781 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003782 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003783static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003784{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003785 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003786 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3787
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003788 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003789 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003790
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003791 /* 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 +02003792 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003793 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003794 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003795 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 +01003796 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003797 }
3798
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003799 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05003800 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003801 }
3802
3803 /* print a new table */
3804 chunk_appendf(&trash,
3805 "<table class=\"tbl\" width=\"100%%\">\n"
3806 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003807 "<th class=\"pxname\" width=\"10%%\">");
3808
3809 chunk_appendf(&trash,
3810 "<a name=\"%s\"></a>%s"
3811 "<a class=px href=\"#%s\">%s</a>",
3812 px->id,
3813 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3814 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003815
3816 if (uri->flags & ST_SHLGNDS) {
3817 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003818 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003819 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3820 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003821 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003822 }
3823
3824 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003825 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003826 "<th class=\"%s\" width=\"90%%\">%s</th>"
3827 "</tr>\n"
3828 "</table>\n"
3829 "<table class=\"tbl\" width=\"100%%\">\n"
3830 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003831 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3832 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3833
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003834 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003835 /* Column heading for Enable or Disable server */
3836 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003837 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003838
3839 chunk_appendf(&trash,
3840 "<th rowspan=2></th>"
3841 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003842 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003843 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3844 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3845 "<th colspan=9>Server</th>"
3846 "</tr>\n"
3847 "<tr class=\"titre\">"
3848 "<th>Cur</th><th>Max</th><th>Limit</th>"
3849 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003850 "<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 +01003851 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3852 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3853 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3854 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3855 "<th>Thrtle</th>\n"
3856 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003857}
3858
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003859/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003860 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003861 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003862static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003863{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003864 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003865 chunk_appendf(&trash, "</table>");
3866
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003867 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003868 /* close the form used to enable/disable this proxy servers */
3869 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003870 "Choose the action to perform on the checked servers : "
3871 "<select name=action>"
3872 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003873 "<option value=\"ready\">Set state to READY</option>"
3874 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02003875 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003876 "<option value=\"dhlth\">Health: disable checks</option>"
3877 "<option value=\"ehlth\">Health: enable checks</option>"
3878 "<option value=\"hrunn\">Health: force UP</option>"
3879 "<option value=\"hnolb\">Health: force NOLB</option>"
3880 "<option value=\"hdown\">Health: force DOWN</option>"
3881 "<option value=\"dagent\">Agent: disable checks</option>"
3882 "<option value=\"eagent\">Agent: enable checks</option>"
3883 "<option value=\"arunn\">Agent: force UP</option>"
3884 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003885 "<option value=\"shutdown\">Kill Sessions</option>"
3886 "</select>"
3887 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3888 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3889 "</form>",
3890 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003891 }
3892
3893 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003894}
Willy Tarreau91861262007-10-17 17:06:05 +02003895
3896/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003897 * Dumps statistics for a proxy. The output is sent to the stream interface's
3898 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3899 * buffer space, or non-zero if everything completed. This function is used
3900 * both by the CLI and the HTTP entry points, and is able to dump the output
3901 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003902 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003903static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003904{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003905 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02003906 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003907 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02003908 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003909 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003910
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003911 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003912
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003913 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003914 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003915 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003916 if (uri && uri->scope) {
3917 /* we have a limited scope, we have to check the proxy name */
3918 struct stat_scope *scope;
3919 int len;
3920
3921 len = strlen(px->id);
3922 scope = uri->scope;
3923
3924 while (scope) {
3925 /* match exact proxy name */
3926 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3927 break;
3928
3929 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003930 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003931 break;
3932 scope = scope->next;
3933 }
3934
3935 /* proxy name not found : don't dump anything */
3936 if (scope == NULL)
3937 return 1;
3938 }
3939
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003940 /* if the user has requested a limited output and the proxy
3941 * name does not match, skip it.
3942 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003943 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003944 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 +02003945 return 1;
3946
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003947 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3948 (appctx->ctx.stats.iid != -1) &&
3949 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003950 return 1;
3951
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003952 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003953 /* fall through */
3954
Willy Tarreau295a8372011-03-10 11:25:07 +01003955 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003956 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003957 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003958 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003959 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02003960 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003961 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003962 }
Willy Tarreau91861262007-10-17 17:06:05 +02003963
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003964 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003965 /* fall through */
3966
Willy Tarreau295a8372011-03-10 11:25:07 +01003967 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003968 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003969 if (stats_dump_fe_stats(si, px)) {
3970 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003971 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003972 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003973 }
3974 }
Willy Tarreau91861262007-10-17 17:06:05 +02003975
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003976 appctx->ctx.stats.l = px->conf.listeners.n;
3977 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003978 /* fall through */
3979
Willy Tarreau295a8372011-03-10 11:25:07 +01003980 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003981 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003982 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003983 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003984 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02003985 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003986 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003987
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003988 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003989 if (!l->counters)
3990 continue;
3991
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003992 if (appctx->ctx.stats.flags & STAT_BOUND) {
3993 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003994 break;
3995
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003996 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003997 continue;
3998 }
3999
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004000 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004001 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
4002 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004003 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004004 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004005 }
4006 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004007 }
4008
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004009 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4010 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004011 /* fall through */
4012
Willy Tarreau295a8372011-03-10 11:25:07 +01004013 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004014 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004015 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004016 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09004017 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02004018
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004019 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004020 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004021 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004022 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004023
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004024 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004025
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004026 if (appctx->ctx.stats.flags & STAT_BOUND) {
4027 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004028 break;
4029
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004030 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004031 continue;
4032 }
4033
Willy Tarreau32091232014-05-16 13:52:00 +02004034 svs = sv;
4035 while (svs->track)
4036 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004037
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004038 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004039 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09004040 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004041 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004042 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4043 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004044 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004045 sv_colour = SRV_STATS_COLOUR_UP;
4046 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004047
Simon Horman4463d192015-04-23 14:51:28 +09004048 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
4049 sv_colour = SRV_STATS_COLOUR_DRAINING;
4050
4051 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09004052 if (svs->agent.state & CHK_ST_ENABLED)
4053 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
4054 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09004055 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09004056 else
Simon Horman4d2eab62015-04-23 14:51:26 +09004057 sv_state = SRV_STATS_STATE_DRAIN;
4058 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004059
Simon Horman837bfa72015-04-23 14:51:27 +09004060 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004061 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09004062 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
4063 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004064 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004065 else if (sv->state == SRV_ST_STOPPING) {
4066 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09004067 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004068 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09004069 sv_colour = SRV_STATS_COLOUR_NOLB;
4070 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004071 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004072 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4073 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004074 }
4075 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09004076 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004077 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09004078 sv_colour = SRV_STATS_COLOUR_DOWN;
4079 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004080 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09004081 sv_colour = SRV_STATS_COLOUR_DOWN;
4082 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004083 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004084 sv_colour = SRV_STATS_COLOUR_GOING_UP;
4085 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004086 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09004087 sv_colour = SRV_STATS_COLOUR_DOWN;
4088 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004089 }
Willy Tarreau91861262007-10-17 17:06:05 +02004090
Willy Tarreau9638efa2014-05-23 11:19:57 +02004091 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02004092 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004093 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02004094 continue;
4095 }
4096
Simon Horman837bfa72015-04-23 14:51:27 +09004097 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004098 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004099 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004100 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004101 }
4102 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004103 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004104
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004105 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004106 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004107
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004108 case STAT_PX_ST_BE:
4109 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004110 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
4111 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004112 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004113 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004114 }
4115 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004116
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004117 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004118 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004119
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004120 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004121 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004122 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004123 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004124 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004125 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004126 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004127 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004128
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004129 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004130 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004131
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004132 case STAT_PX_ST_FIN:
4133 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004134
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004135 default:
4136 /* unknown state, we should put an abort() here ! */
4137 return 1;
4138 }
4139}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004140
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004141/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4142 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004143 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004144static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004145{
4146 /* WARNING! This must fit in the first buffer !!! */
4147 chunk_appendf(&trash,
4148 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4149 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4150 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4151 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4152 "<style type=\"text/css\"><!--\n"
4153 "body {"
4154 " font-family: arial, helvetica, sans-serif;"
4155 " font-size: 12px;"
4156 " font-weight: normal;"
4157 " color: black;"
4158 " background: white;"
4159 "}\n"
4160 "th,td {"
4161 " font-size: 10px;"
4162 "}\n"
4163 "h1 {"
4164 " font-size: x-large;"
4165 " margin-bottom: 0.5em;"
4166 "}\n"
4167 "h2 {"
4168 " font-family: helvetica, arial;"
4169 " font-size: x-large;"
4170 " font-weight: bold;"
4171 " font-style: italic;"
4172 " color: #6020a0;"
4173 " margin-top: 0em;"
4174 " margin-bottom: 0em;"
4175 "}\n"
4176 "h3 {"
4177 " font-family: helvetica, arial;"
4178 " font-size: 16px;"
4179 " font-weight: bold;"
4180 " color: #b00040;"
4181 " background: #e8e8d0;"
4182 " margin-top: 0em;"
4183 " margin-bottom: 0em;"
4184 "}\n"
4185 "li {"
4186 " margin-top: 0.25em;"
4187 " margin-right: 2em;"
4188 "}\n"
4189 ".hr {margin-top: 0.25em;"
4190 " border-color: black;"
4191 " border-bottom-style: solid;"
4192 "}\n"
4193 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4194 ".total {background: #20D0D0;color: #ffff80;}\n"
4195 ".frontend {background: #e8e8d0;}\n"
4196 ".socket {background: #d0d0d0;}\n"
4197 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004198 ".active_down {background: #ff9090;}\n"
4199 ".active_going_up {background: #ffd020;}\n"
4200 ".active_going_down {background: #ffffa0;}\n"
4201 ".active_up {background: #c0ffc0;}\n"
4202 ".active_nolb {background: #20a0ff;}\n"
4203 ".active_draining {background: #20a0FF;}\n"
4204 ".active_no_check {background: #e0e0e0;}\n"
4205 ".backup_down {background: #ff9090;}\n"
4206 ".backup_going_up {background: #ff80ff;}\n"
4207 ".backup_going_down {background: #c060ff;}\n"
4208 ".backup_up {background: #b0d0ff;}\n"
4209 ".backup_nolb {background: #90b0e0;}\n"
4210 ".backup_draining {background: #cc9900;}\n"
4211 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004212 ".maintain {background: #c07820;}\n"
4213 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4214 "\n"
4215 "a.px:link {color: #ffff40; text-decoration: none;}"
4216 "a.px:visited {color: #ffff40; text-decoration: none;}"
4217 "a.px:hover {color: #ffffff; text-decoration: none;}"
4218 "a.lfsb:link {color: #000000; text-decoration: none;}"
4219 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4220 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4221 "\n"
4222 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4223 "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"
4224 "table.tbl td.ac { text-align: center;}\n"
4225 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4226 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4227 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4228 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4229 "\n"
4230 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4231 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4232 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004233 "table.det { border-collapse: collapse; border-style: none; }\n"
4234 "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 +01004235 "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 +01004236 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004237 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004238 " display:block;\n"
4239 " visibility:hidden;\n"
4240 " z-index:2147483647;\n"
4241 " position:absolute;\n"
4242 " padding:2px 4px 3px;\n"
4243 " background:#f0f060; color:#000000;\n"
4244 " border:1px solid #7040c0;\n"
4245 " white-space:nowrap;\n"
4246 " font-style:normal;font-size:11px;font-weight:normal;\n"
4247 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4248 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4249 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004250 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004251 "-->\n"
4252 "</style></head>\n",
4253 (uri->flags & ST_SHNODE) ? " on " : "",
4254 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4255 );
4256}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004257
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004258/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004259 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004260 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004261 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004262static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004263{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004264 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004265 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004266 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004267
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004268 /* WARNING! this has to fit the first packet too.
4269 * We are around 3.5 kB, add adding entries will
4270 * become tricky if we want to support 4kB buffers !
4271 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004272 chunk_appendf(&trash,
4273 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4274 PRODUCT_NAME "%s</a></h1>\n"
4275 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4276 "<hr width=\"100%%\" class=\"hr\">\n"
4277 "<h3>&gt; General process information</h3>\n"
4278 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4279 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4280 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4281 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4282 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4283 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4284 "Running tasks: %d/%d; idle = %d %%<br>\n"
4285 "</td><td align=\"center\" nowrap>\n"
4286 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004287 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4288 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004289 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004290 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4291 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004292 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004293 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4294 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004295 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004296 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4297 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004298 "</tr><tr>\n"
4299 "<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 -07004300 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004301 "<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 +01004302 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004303 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004304 "</td>"
4305 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4306 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4307 "",
4308 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4309 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4310 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4311 (uri->flags & ST_SHDESC) ? ": " : "",
4312 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4313 pid, relative_pid, global.nbproc,
4314 up / 86400, (up % 86400) / 3600,
4315 (up % 3600) / 60, (up % 60),
4316 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4317 global.rlimit_memmax ? " MB" : "",
4318 global.rlimit_nofile,
4319 global.maxsock, global.maxconn, global.maxpipes,
4320 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4321 run_queue_cur, nb_tasks_cur, idle_pct
4322 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004323
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004324 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004325 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 +01004326 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004327
4328 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004329 "<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 +01004330 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004331 STAT_SCOPE_TXT_MAXLEN);
4332
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004333 /* 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 +02004334 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004335 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004336 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004337 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 +01004338 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004339 }
4340
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004341 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004342 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004343 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004344 uri->uri_prefix,
4345 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004346 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004347 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004348 else
4349 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004350 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004351 uri->uri_prefix,
4352 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004353 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004354 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004355
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004356 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004357 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004358 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004359 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004360 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004361 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004362 "",
4363 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004364 else
4365 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004366 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004367 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004368 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004369 ";norefresh",
4370 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004371 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004372
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004373 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004374 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004375 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004376 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4377 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004378 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004379
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004380 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004381 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004382 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004383 (uri->refresh > 0) ? ";norefresh" : "",
4384 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004385
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004386 chunk_appendf(&trash,
4387 "</ul></td>"
4388 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4389 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4390 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4391 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4392 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4393 "</ul>"
4394 "</td>"
4395 "</tr></table>\n"
4396 ""
4397 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004398
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004399 if (appctx->ctx.stats.st_code) {
4400 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004401 case STAT_STATUS_DONE:
4402 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004403 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004404 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004405 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004406 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004407 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4408 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004409 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004410 break;
4411 case STAT_STATUS_NONE:
4412 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004413 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004414 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004415 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004416 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004417 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4418 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004419 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004420 break;
4421 case STAT_STATUS_PART:
4422 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004423 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004424 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004425 "Action partially processed.<br>"
4426 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004427 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004428 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4429 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004430 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004431 break;
4432 case STAT_STATUS_ERRP:
4433 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004434 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004435 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004436 "Action not processed because of invalid parameters."
4437 "<ul>"
4438 "<li>The action is maybe unknown.</li>"
4439 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4440 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4441 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004442 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004443 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4444 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004445 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004446 break;
4447 case STAT_STATUS_EXCD:
4448 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004449 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004450 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004451 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4452 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004453 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004454 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4455 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004456 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004457 break;
4458 case STAT_STATUS_DENY:
4459 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004460 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004461 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004462 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004463 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004464 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4465 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004466 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004467 break;
4468 default:
4469 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004470 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004471 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004472 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004473 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004474 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4475 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004476 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004477 }
4478 chunk_appendf(&trash, "<p>\n");
4479 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004480}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004481
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004482/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4483 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004484 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004485static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004486{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004487 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004488}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004489
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004490/* This function dumps statistics onto the stream interface's read buffer in
4491 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004492 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4493 * 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 +02004494 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004495 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004496 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004497static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004498{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004499 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004500 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004501 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004502
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004503 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004504
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004505 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004506 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004507 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004508 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004509
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004510 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004511 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004512 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004513 else
4514 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004515
Willy Tarreaubc18da12015-03-13 14:00:47 +01004516 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004517 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004518 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004519 }
Willy Tarreauae526782010-03-04 20:34:23 +01004520
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004521 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004522 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004523
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004524 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004525 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004526 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004527 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004528 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004529 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004530 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004531 }
Willy Tarreau91861262007-10-17 17:06:05 +02004532
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004533 appctx->ctx.stats.px = proxy;
4534 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4535 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004536 /* fall through */
4537
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004538 case STAT_ST_LIST:
4539 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004540 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004541 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004542 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004543 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004544 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004545
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004546 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004547 /* skip the disabled proxies, global frontend and non-networked ones */
4548 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004549 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004550 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004551
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004552 appctx->ctx.stats.px = px->next;
4553 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004554 }
4555 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004556
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004557 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004558 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004559
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004560 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004561 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004562 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01004563 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004564 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004565 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004566 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004567 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004568
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004569 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004570 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004571
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004572 case STAT_ST_FIN:
4573 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004574
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004575 default:
4576 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004577 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004578 return -1;
4579 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004580}
Willy Tarreauae526782010-03-04 20:34:23 +01004581
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004582/* We reached the stats page through a POST request. The appctx is
4583 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004584 * Parse the posted data and enable/disable servers if necessary.
4585 * Returns 1 if request was parsed or zero if it needs more data.
4586 */
4587static int stats_process_http_post(struct stream_interface *si)
4588{
Willy Tarreau87b09662015-04-03 00:22:06 +02004589 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004590 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004591
4592 struct proxy *px = NULL;
4593 struct server *sv = NULL;
4594
4595 char key[LINESIZE];
4596 int action = ST_ADM_ACTION_NONE;
4597 int reprocess = 0;
4598
4599 int total_servers = 0;
4600 int altered_servers = 0;
4601
4602 char *first_param, *cur_param, *next_param, *end_params;
4603 char *st_cur_param = NULL;
4604 char *st_next_param = NULL;
4605
4606 struct chunk *temp;
4607 int reql;
4608
4609 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02004610 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004611 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004612 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004613 goto out;
4614 }
4615
Willy Tarreaueee5b512015-04-03 23:46:31 +02004616 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 +01004617 if (reql <= 0) {
4618 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004619 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004620 return 0;
4621 }
4622
4623 first_param = temp->str;
4624 end_params = temp->str + reql;
4625 cur_param = next_param = end_params;
4626 *end_params = '\0';
4627
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004628 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004629
4630 /*
4631 * Parse the parameters in reverse order to only store the last value.
4632 * From the html form, the backend and the action are at the end.
4633 */
4634 while (cur_param > first_param) {
4635 char *value;
4636 int poffset, plen;
4637
4638 cur_param--;
4639
4640 if ((*cur_param == '&') || (cur_param == first_param)) {
4641 reprocess_servers:
4642 /* Parse the key */
4643 poffset = (cur_param != first_param ? 1 : 0);
4644 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4645 if ((plen > 0) && (plen <= sizeof(key))) {
4646 strncpy(key, cur_param + poffset, plen);
4647 key[plen - 1] = '\0';
4648 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004649 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004650 goto out;
4651 }
4652
4653 /* Parse the value */
4654 value = key;
4655 while (*value != '\0' && *value != '=') {
4656 value++;
4657 }
4658 if (*value == '=') {
4659 /* Ok, a value is found, we can mark the end of the key */
4660 *value++ = '\0';
4661 }
4662 if (url_decode(key) < 0 || url_decode(value) < 0)
4663 break;
4664
4665 /* Now we can check the key to see what to do */
4666 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02004667 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004668 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004669 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004670 goto out;
4671 }
4672 }
4673 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004674 if (strcmp(value, "ready") == 0) {
4675 action = ST_ADM_ACTION_READY;
4676 }
4677 else if (strcmp(value, "drain") == 0) {
4678 action = ST_ADM_ACTION_DRAIN;
4679 }
4680 else if (strcmp(value, "maint") == 0) {
4681 action = ST_ADM_ACTION_MAINT;
4682 }
4683 else if (strcmp(value, "shutdown") == 0) {
4684 action = ST_ADM_ACTION_SHUTDOWN;
4685 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004686 else if (strcmp(value, "dhlth") == 0) {
4687 action = ST_ADM_ACTION_DHLTH;
4688 }
4689 else if (strcmp(value, "ehlth") == 0) {
4690 action = ST_ADM_ACTION_EHLTH;
4691 }
4692 else if (strcmp(value, "hrunn") == 0) {
4693 action = ST_ADM_ACTION_HRUNN;
4694 }
4695 else if (strcmp(value, "hnolb") == 0) {
4696 action = ST_ADM_ACTION_HNOLB;
4697 }
4698 else if (strcmp(value, "hdown") == 0) {
4699 action = ST_ADM_ACTION_HDOWN;
4700 }
4701 else if (strcmp(value, "dagent") == 0) {
4702 action = ST_ADM_ACTION_DAGENT;
4703 }
4704 else if (strcmp(value, "eagent") == 0) {
4705 action = ST_ADM_ACTION_EAGENT;
4706 }
4707 else if (strcmp(value, "arunn") == 0) {
4708 action = ST_ADM_ACTION_ARUNN;
4709 }
4710 else if (strcmp(value, "adown") == 0) {
4711 action = ST_ADM_ACTION_ADOWN;
4712 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004713 /* else these are the old supported methods */
4714 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004715 action = ST_ADM_ACTION_DISABLE;
4716 }
4717 else if (strcmp(value, "enable") == 0) {
4718 action = ST_ADM_ACTION_ENABLE;
4719 }
4720 else if (strcmp(value, "stop") == 0) {
4721 action = ST_ADM_ACTION_STOP;
4722 }
4723 else if (strcmp(value, "start") == 0) {
4724 action = ST_ADM_ACTION_START;
4725 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004726 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004727 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004728 goto out;
4729 }
4730 }
4731 else if (strcmp(key, "s") == 0) {
4732 if (!(px && action)) {
4733 /*
4734 * Indicates that we'll need to reprocess the parameters
4735 * as soon as backend and action are known
4736 */
4737 if (!reprocess) {
4738 st_cur_param = cur_param;
4739 st_next_param = next_param;
4740 }
4741 reprocess = 1;
4742 }
4743 else if ((sv = findserver(px, value)) != NULL) {
4744 switch (action) {
4745 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004746 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004747 altered_servers++;
4748 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004749 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004750 }
4751 break;
4752 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004753 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004754 altered_servers++;
4755 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004756 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004757 }
4758 break;
4759 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004760 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4761 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4762 altered_servers++;
4763 total_servers++;
4764 }
4765 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004766 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004767 if (sv->admin & SRV_ADMF_FDRAIN) {
4768 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4769 altered_servers++;
4770 total_servers++;
4771 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004772 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004773 case ST_ADM_ACTION_DHLTH:
4774 if (sv->check.state & CHK_ST_CONFIGURED) {
4775 sv->check.state &= ~CHK_ST_ENABLED;
4776 altered_servers++;
4777 total_servers++;
4778 }
4779 break;
4780 case ST_ADM_ACTION_EHLTH:
4781 if (sv->check.state & CHK_ST_CONFIGURED) {
4782 sv->check.state |= CHK_ST_ENABLED;
4783 altered_servers++;
4784 total_servers++;
4785 }
4786 break;
4787 case ST_ADM_ACTION_HRUNN:
4788 if (!(sv->track)) {
4789 sv->check.health = sv->check.rise + sv->check.fall - 1;
4790 srv_set_running(sv, "changed from Web interface");
4791 altered_servers++;
4792 total_servers++;
4793 }
4794 break;
4795 case ST_ADM_ACTION_HNOLB:
4796 if (!(sv->track)) {
4797 sv->check.health = sv->check.rise + sv->check.fall - 1;
4798 srv_set_stopping(sv, "changed from Web interface");
4799 altered_servers++;
4800 total_servers++;
4801 }
4802 break;
4803 case ST_ADM_ACTION_HDOWN:
4804 if (!(sv->track)) {
4805 sv->check.health = 0;
4806 srv_set_stopped(sv, "changed from Web interface");
4807 altered_servers++;
4808 total_servers++;
4809 }
4810 break;
4811 case ST_ADM_ACTION_DAGENT:
4812 if (sv->agent.state & CHK_ST_CONFIGURED) {
4813 sv->agent.state &= ~CHK_ST_ENABLED;
4814 altered_servers++;
4815 total_servers++;
4816 }
4817 break;
4818 case ST_ADM_ACTION_EAGENT:
4819 if (sv->agent.state & CHK_ST_CONFIGURED) {
4820 sv->agent.state |= CHK_ST_ENABLED;
4821 altered_servers++;
4822 total_servers++;
4823 }
4824 break;
4825 case ST_ADM_ACTION_ARUNN:
4826 if (sv->agent.state & CHK_ST_ENABLED) {
4827 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4828 srv_set_running(sv, "changed from Web interface");
4829 altered_servers++;
4830 total_servers++;
4831 }
4832 break;
4833 case ST_ADM_ACTION_ADOWN:
4834 if (sv->agent.state & CHK_ST_ENABLED) {
4835 sv->agent.health = 0;
4836 srv_set_stopped(sv, "changed from Web interface");
4837 altered_servers++;
4838 total_servers++;
4839 }
4840 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004841 case ST_ADM_ACTION_READY:
4842 srv_adm_set_ready(sv);
4843 altered_servers++;
4844 total_servers++;
4845 break;
4846 case ST_ADM_ACTION_DRAIN:
4847 srv_adm_set_drain(sv);
4848 altered_servers++;
4849 total_servers++;
4850 break;
4851 case ST_ADM_ACTION_MAINT:
4852 srv_adm_set_maint(sv);
4853 altered_servers++;
4854 total_servers++;
4855 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004856 case ST_ADM_ACTION_SHUTDOWN:
4857 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004858 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004859
4860 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4861 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004862 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004863
4864 altered_servers++;
4865 total_servers++;
4866 }
4867 break;
4868 }
4869 } else {
4870 /* the server name is unknown or ambiguous (duplicate names) */
4871 total_servers++;
4872 }
4873 }
4874 if (reprocess && px && action) {
4875 /* Now, we know the backend and the action chosen by the user.
4876 * We can safely restart from the first server parameter
4877 * to reprocess them
4878 */
4879 cur_param = st_cur_param;
4880 next_param = st_next_param;
4881 reprocess = 0;
4882 goto reprocess_servers;
4883 }
4884
4885 next_param = cur_param;
4886 }
4887 }
4888
4889 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004890 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004891 }
4892 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004893 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004894 }
4895 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004896 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004897 }
4898 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004899 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004900 }
4901 out:
4902 return 1;
4903}
4904
4905
4906static int stats_send_http_headers(struct stream_interface *si)
4907{
Willy Tarreau87b09662015-04-03 00:22:06 +02004908 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004909 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004910 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004911
4912 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004913 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004914 "Cache-Control: no-cache\r\n"
4915 "Connection: close\r\n"
4916 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004917 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004918
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004919 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004920 chunk_appendf(&trash, "Refresh: %d\r\n",
4921 uri->refresh);
4922
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004923 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4924
4925 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4926 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4927 else
4928 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004929
Willy Tarreaueee5b512015-04-03 23:46:31 +02004930 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004931 s->logs.tv_request = now;
4932
Willy Tarreaubc18da12015-03-13 14:00:47 +01004933 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004934 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004935 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004936 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004937
4938 return 1;
4939}
4940
4941static int stats_send_http_redirect(struct stream_interface *si)
4942{
4943 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02004944 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004945 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004946 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004947
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004948 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004949 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004950 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004951 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004952 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 +01004953 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004954 }
4955
4956 /* We don't want to land on the posted stats page because a refresh will
4957 * repost the data. We don't want this to happen on accident so we redirect
4958 * the browse to the stats page with a GET.
4959 */
4960 chunk_printf(&trash,
4961 "HTTP/1.1 303 See Other\r\n"
4962 "Cache-Control: no-cache\r\n"
4963 "Content-Type: text/plain\r\n"
4964 "Connection: close\r\n"
4965 "Location: %s;st=%s%s%s%s\r\n"
4966 "\r\n",
4967 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004968 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4969 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4970 stat_status_codes[appctx->ctx.stats.st_code]) ?
4971 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004972 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004973 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4974 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004975 scope_txt);
4976
Willy Tarreaueee5b512015-04-03 23:46:31 +02004977 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004978 s->logs.tv_request = now;
4979
Willy Tarreaubc18da12015-03-13 14:00:47 +01004980 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004981 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004982 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004983 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004984
4985 return 1;
4986}
4987
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004988/* This I/O handler runs as an applet embedded in a stream interface. It is
4989 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004990 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004991 * automatically unregisters itself once transfer is complete.
4992 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02004993static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004994{
Willy Tarreau00a37f02015-04-13 12:05:19 +02004995 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02004996 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004997 struct channel *req = si_oc(si);
4998 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01004999
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005000 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5001 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005002
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005003 /* check that the output is not closed */
5004 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005005 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005006
Willy Tarreau347a35d2013-11-22 17:51:09 +01005007 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005008 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005009 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005010 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005011 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005012 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005013 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005014 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005015 }
5016
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005017 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005018 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005019 unsigned int data_len;
5020 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005021 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005022
5023 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5024 /* One difficulty we're facing is that we must prevent
5025 * the input data from being automatically forwarded to
5026 * the output area. For this, we temporarily disable
5027 * forwarding on the channel.
5028 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005029 last_fwd = si_ic(si)->to_forward;
5030 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005031 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005032 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005033 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005034 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005035 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005036 }
5037 }
5038
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005039 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005040 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005041 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005042
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005043 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005044
5045 /* Now we must either adjust or remove the chunk size. This is
5046 * not easy because the chunk size might wrap at the end of the
5047 * buffer, so we pretend we have nothing in the buffer, we write
5048 * the size, then restore the buffer's contents. Note that we can
5049 * only do that because no forwarding is scheduled on the stats
5050 * applet.
5051 */
5052 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005053 si_ic(si)->total -= (last_len - prev_len);
5054 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005055
5056 if (last_len != data_len) {
5057 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005058 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005059 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005060
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005061 si_ic(si)->total += (last_len - data_len);
5062 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005063 }
5064 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005065 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005066 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005067 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005068
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005069 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005070 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005071 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005072 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005073 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005074 }
5075
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005076 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005077 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005078 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005079 }
5080
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005081 if (appctx->st0 == STAT_HTTP_DONE) {
5082 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5083 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005084 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005085 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005086 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005087 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005088 }
5089 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005090 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005091 res->flags |= CF_READ_NULL;
5092 si_shutr(si);
5093 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005094
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005095 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5096 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005097
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005098 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005099 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5100 si_shutr(si);
5101 res->flags |= CF_READ_NULL;
5102 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005103 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005104 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005105 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005106}
5107
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005108
Willy Tarreau909d5172012-11-26 03:04:41 +01005109static inline const char *get_conn_ctrl_name(const struct connection *conn)
5110{
Willy Tarreau3c728722014-01-23 13:50:42 +01005111 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005112 return "NONE";
5113 return conn->ctrl->name;
5114}
5115
5116static inline const char *get_conn_xprt_name(const struct connection *conn)
5117{
5118 static char ptr[17];
5119
Willy Tarreauaad69382014-01-23 14:21:42 +01005120 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005121 return "NONE";
5122
5123 if (conn->xprt == &raw_sock)
5124 return "RAW";
5125
5126#ifdef USE_OPENSSL
5127 if (conn->xprt == &ssl_sock)
5128 return "SSL";
5129#endif
5130 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5131 return ptr;
5132}
5133
5134static inline const char *get_conn_data_name(const struct connection *conn)
5135{
5136 static char ptr[17];
5137
5138 if (!conn->data)
5139 return "NONE";
5140
5141 if (conn->data == &sess_conn_cb)
5142 return "SESS";
5143
5144 if (conn->data == &si_conn_cb)
5145 return "STRM";
5146
5147 if (conn->data == &check_conn_cb)
5148 return "CHCK";
5149
5150 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5151 return ptr;
5152}
5153
Willy Tarreau87b09662015-04-03 00:22:06 +02005154/* This function dumps a complete stream state onto the stream interface's
5155 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005156 * 0 if the output buffer is full and it needs to be called again, otherwise
5157 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005158 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005159static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005160{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005161 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005162 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005163 extern const char *monthname[12];
5164 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005165 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005166 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005167
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005168 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005169
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005170 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005171 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005172 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005173 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005174 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005175 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005176 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005177 appctx->ctx.sess.uid = 0;
5178 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005179 return 1;
5180 }
5181
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005182 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005183 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005184 appctx->ctx.sess.uid = sess->uniq_id;
5185 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005186 /* fall through */
5187
5188 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005189 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005190 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005191 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005192 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005193 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5194 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005195 sess->uniq_id,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005196 strm_li(sess) && strm_li(sess)->proto->name ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005197
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005198 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005199 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005200 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005201 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005202 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005203 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005204 break;
5205 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005206 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005207 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005208 default:
5209 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005210 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005211 break;
5212 }
5213
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005214 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005215 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005216 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005217
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005218 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005219 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005220 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5221 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5222 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005223
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005224 if (conn)
5225 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005226
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005227 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005228 case AF_INET:
5229 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005230 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005231 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005232 break;
5233 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005234 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005235 break;
5236 default:
5237 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005238 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005239 break;
5240 }
5241
Willy Tarreau50943332011-09-02 17:33:05 +02005242 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005243 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005244 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005245 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005246 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5247 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005248 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005249
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005250 conn = objt_conn(sess->si[1].end);
5251 if (conn)
5252 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005253
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005254 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005255 case AF_INET:
5256 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005257 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005258 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005259 break;
5260 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005261 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005262 break;
5263 default:
5264 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005265 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005266 break;
5267 }
5268
5269 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005270 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005271 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005272 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5273 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005274 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005275 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005276
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005277 if (conn)
5278 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005279
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005280 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005281 case AF_INET:
5282 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005283 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005284 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005285 break;
5286 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005287 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005288 break;
5289 default:
5290 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005291 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005292 break;
5293 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005294
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005295 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005296 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005297 sess->task,
5298 sess->task->state,
5299 sess->task->nice, sess->task->calls,
5300 sess->task->expire ?
5301 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5302 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5303 TICKS_TO_MS(1000)) : "<NEVER>",
5304 task_in_rq(sess->task) ? ", running" : "");
5305
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005306 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005307 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005308 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5309
Willy Tarreaueee5b512015-04-03 23:46:31 +02005310 if (sess->txn)
5311 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005312 " 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 +02005313 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5314 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 +01005315
5316 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005317 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005318 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005319 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005320 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005321 obj_type_name(sess->si[0].end),
5322 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005323 sess->si[0].exp ?
5324 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5325 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5326 TICKS_TO_MS(1000)) : "<NEVER>",
5327 sess->si[0].err_type);
5328
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005329 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005330 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005331 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005332 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005333 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005334 obj_type_name(sess->si[1].end),
5335 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005336 sess->si[1].exp ?
5337 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5338 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5339 TICKS_TO_MS(1000)) : "<NEVER>",
5340 sess->si[1].err_type);
5341
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005342 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005343 chunk_appendf(&trash,
5344 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005345 conn,
5346 get_conn_ctrl_name(conn),
5347 get_conn_xprt_name(conn),
5348 get_conn_data_name(conn),
5349 obj_type_name(conn->target),
5350 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005351
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005352 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005353 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005354 conn->flags,
5355 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005356 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005357 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005358 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005359 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005360 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5361 chunk_appendf(&trash,
5362 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5363 tmpctx,
5364 tmpctx->st0,
5365 tmpctx->st1,
5366 tmpctx->st2,
5367 tmpctx->applet->name);
5368 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005369
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005370 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005371 chunk_appendf(&trash,
5372 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005373 conn,
5374 get_conn_ctrl_name(conn),
5375 get_conn_xprt_name(conn),
5376 get_conn_data_name(conn),
5377 obj_type_name(conn->target),
5378 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005379
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005380 chunk_appendf(&trash,
5381 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005382 conn->flags,
5383 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005384 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005385 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005386 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005387 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005388 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5389 chunk_appendf(&trash,
5390 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5391 tmpctx,
5392 tmpctx->st0,
5393 tmpctx->st1,
5394 tmpctx->st2,
5395 tmpctx->applet->name);
5396 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005397
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005398 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005399 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005400 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005401 &sess->req,
5402 sess->req.flags, sess->req.analysers,
5403 sess->req.pipe ? sess->req.pipe->data : 0,
5404 sess->req.to_forward, sess->req.total,
5405 sess->req.analyse_exp ?
5406 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005407 TICKS_TO_MS(1000)) : "<NEVER>");
5408
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005409 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005410 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005411 sess->req.rex ?
5412 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005413 TICKS_TO_MS(1000)) : "<NEVER>");
5414
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005415 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005416 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005417 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005418 sess->req.wex ?
5419 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005420 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005421 sess->req.buf,
5422 sess->req.buf->data, sess->req.buf->o,
5423 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005424 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005425 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005426
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005427 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005428 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005429 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005430 &sess->res,
5431 sess->res.flags, sess->res.analysers,
5432 sess->res.pipe ? sess->res.pipe->data : 0,
5433 sess->res.to_forward, sess->res.total,
5434 sess->res.analyse_exp ?
5435 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005436 TICKS_TO_MS(1000)) : "<NEVER>");
5437
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005438 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005439 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005440 sess->res.rex ?
5441 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005442 TICKS_TO_MS(1000)) : "<NEVER>");
5443
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005444 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005445 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005446 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005447 sess->res.wex ?
5448 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005449 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005450 sess->res.buf,
5451 sess->res.buf->data, sess->res.buf->o,
5452 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005453 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005454 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005455
Willy Tarreaubc18da12015-03-13 14:00:47 +01005456 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005457 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005458 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005459 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005460
5461 /* use other states to dump the contents */
5462 }
5463 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005464 appctx->ctx.sess.uid = 0;
5465 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005466 return 1;
5467}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005468
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005469#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5470static int stats_tlskeys_list(struct stream_interface *si) {
5471 struct appctx *appctx = __objt_appctx(si->end);
5472
5473 switch (appctx->st2) {
5474 case STAT_ST_INIT:
5475 /* Display the column headers. If the message cannot be sent,
5476 * quit the fucntion with returning 0. The function is called
5477 * later and restart at the state "STAT_ST_INIT".
5478 */
5479 chunk_reset(&trash);
5480 chunk_appendf(&trash, "# id (file)\n");
5481 if (bi_putchk(si_ic(si), &trash) == -1) {
5482 si_applet_cant_put(si);
5483 return 0;
5484 }
5485
5486 /* Now, we start the browsing of the references lists.
5487 * Note that the following call to LIST_ELEM return bad pointer. The only
5488 * avalaible field of this pointer is <list>. It is used with the function
5489 * tlskeys_list_get_next() for retruning the first avalaible entry
5490 */
5491 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
5492 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5493
5494 appctx->st2 = STAT_ST_LIST;
5495 /* fall through */
5496
5497 case STAT_ST_LIST:
5498 while (appctx->ctx.tlskeys.ref) {
5499 chunk_reset(&trash);
5500
5501 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
5502 appctx->ctx.tlskeys.ref->filename);
5503
5504 if (bi_putchk(si_ic(si), &trash) == -1) {
5505 /* 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.
5507 */
5508 si_applet_cant_put(si);
5509 return 0;
5510 }
5511
5512 /* get next list entry and check the end of the list */
5513 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5514 }
5515
5516 appctx->st2 = STAT_ST_FIN;
5517 /* fall through */
5518
5519 default:
5520 appctx->st2 = STAT_ST_FIN;
5521 return 1;
5522 }
5523 return 0;
5524}
5525#endif
5526
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005527static int stats_pats_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 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005533 /* Display the column headers. If the message cannot be sent,
5534 * quit the fucntion with returning 0. The function is called
5535 * later and restart at the state "STAT_ST_INIT".
5536 */
5537 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005538 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005539 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005540 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005541 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005542 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005543
5544 /* Now, we start the browsing of the references lists.
5545 * Note that the following call to LIST_ELEM return bad pointer. The only
5546 * avalaible field of this pointer is <list>. It is used with the function
5547 * pat_list_get_next() for retruning the first avalaible entry
5548 */
5549 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5550 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5551 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005552 appctx->st2 = STAT_ST_LIST;
5553 /* fall through */
5554
5555 case STAT_ST_LIST:
5556 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005557 chunk_reset(&trash);
5558
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005559 /* Build messages. If the reference is used by another category than
5560 * the listed categorie, display the information in the massage.
5561 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005562 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005563 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5564 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005565
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005566 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005567 /* let's try again later from this stream. We add ourselves into
5568 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005569 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005570 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005571 return 0;
5572 }
5573
5574 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005575 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5576 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005577 }
5578
5579 appctx->st2 = STAT_ST_FIN;
5580 /* fall through */
5581
5582 default:
5583 appctx->st2 = STAT_ST_FIN;
5584 return 1;
5585 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005586 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005587}
5588
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005589static int stats_map_lookup(struct stream_interface *si)
5590{
5591 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005592 struct sample sample;
5593 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005594 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005595
5596 switch (appctx->st2) {
5597 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005598 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005599 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005600 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005601 appctx->st2 = STAT_ST_LIST;
5602 /* fall through */
5603
5604 case STAT_ST_LIST:
5605 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005606 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005607 /* initialise chunk to build new message */
5608 chunk_reset(&trash);
5609
5610 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005611 sample.type = SMP_T_STR;
5612 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005613 sample.data.str.len = appctx->ctx.map.chunk.len;
5614 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005615 if (appctx->ctx.map.expr->pat_head->match &&
5616 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005617 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5618 else
5619 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005620
5621 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005622 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5623 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5624 break;
5625 if (match_method >= PAT_MATCH_NUM)
5626 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5627 else
5628 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005629
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005630 /* case sensitive */
5631 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5632 chunk_appendf(&trash, ", case=insensitive");
5633 else
5634 chunk_appendf(&trash, ", case=sensitive");
5635
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005636 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005637 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005638 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5639 chunk_appendf(&trash, ", found=no");
5640 else
5641 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005642 }
5643
5644 /* Display match and match info */
5645 else {
5646 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005647 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5648 chunk_appendf(&trash, ", found=yes");
5649 else
5650 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005651
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005652 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005653 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005654 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005655 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005656 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005657
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005658 /* display pattern */
5659 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5660 if (pat->ref && pat->ref->pattern)
5661 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5662 else
5663 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005664 }
5665 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005666 if (pat->ref && pat->ref->pattern)
5667 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5668 else
5669 chunk_appendf(&trash, ", pattern=unknown");
5670 }
5671
5672 /* display return value */
5673 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5674 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005675 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005676 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005677 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005678 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005679 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005680 }
5681
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005682 chunk_appendf(&trash, "\n");
5683
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005684 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005685 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005686 /* let's try again later from this stream. We add ourselves into
5687 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005688 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005689 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005690 return 0;
5691 }
5692
5693 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005694 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5695 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005696 }
5697
5698 appctx->st2 = STAT_ST_FIN;
5699 /* fall through */
5700
5701 default:
5702 appctx->st2 = STAT_ST_FIN;
5703 free(appctx->ctx.map.chunk.str);
5704 return 1;
5705 }
5706}
5707
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005708static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005709{
5710 struct appctx *appctx = __objt_appctx(si->end);
5711
5712 switch (appctx->st2) {
5713
5714 case STAT_ST_INIT:
5715 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005716 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5717 struct pat_ref_elt *, list);
5718 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5719 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005720 appctx->st2 = STAT_ST_LIST;
5721 /* fall through */
5722
5723 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005724 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005725 chunk_reset(&trash);
5726
5727 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005728 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005729 chunk_appendf(&trash, "%p %s %s\n",
5730 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5731 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005732 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005733 chunk_appendf(&trash, "%p %s\n",
5734 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005735
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005736 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005737 /* let's try again later from this stream. We add ourselves into
5738 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005739 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005740 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005741 return 0;
5742 }
5743
5744 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005745 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5746 struct pat_ref_elt *, list);
5747 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005748 break;
5749 }
5750
5751 appctx->st2 = STAT_ST_FIN;
5752 /* fall through */
5753
5754 default:
5755 appctx->st2 = STAT_ST_FIN;
5756 return 1;
5757 }
5758}
5759
Willy Tarreau87b09662015-04-03 00:22:06 +02005760/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005761 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005762 * to be called again, otherwise non-zero. It is designed to be called
5763 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005764 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005765static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005766{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005767 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005768 struct connection *conn;
5769
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005770 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005771 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02005772 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005773 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005774 if (appctx->st2 == STAT_ST_LIST) {
5775 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5776 LIST_DEL(&appctx->ctx.sess.bref.users);
5777 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005778 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005779 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005780 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005781 }
5782
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005783 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005784
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005785 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005786 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005787 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02005788 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005789 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02005790 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005791 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02005792 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005793 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005794 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02005795 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005796 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005797 /* fall through */
5798
Willy Tarreau295a8372011-03-10 11:25:07 +01005799 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02005800 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005801 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5802 LIST_DEL(&appctx->ctx.sess.bref.users);
5803 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005804 }
5805
5806 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02005807 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005808 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005809 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005810
Willy Tarreau87b09662015-04-03 00:22:06 +02005811 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005812
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005813 if (appctx->ctx.sess.target) {
5814 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005815 goto next_sess;
5816
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005817 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005818 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005819 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005820 return 0;
5821
Willy Tarreau87b09662015-04-03 00:22:06 +02005822 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005823 LIST_DEL(&appctx->ctx.sess.bref.users);
5824 LIST_INIT(&appctx->ctx.sess.bref.users);
5825 if (appctx->ctx.sess.target != (void *)-1) {
5826 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005827 break;
5828 }
5829 else
5830 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005831 }
5832
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005833 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005834 "%p: proto=%s",
5835 curr_sess,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005836 strm_li(curr_sess)->proto->name);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005837
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005838
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005839 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005840 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005841 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005842 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005843 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005844 " src=%s:%d fe=%s be=%s srv=%s",
5845 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005846 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005847 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005848 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005849 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005850 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005851 break;
5852 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005853 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005854 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005855 strm_li(curr_sess)->luid,
5856 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005857 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005858 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005859 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005860 break;
5861 }
5862
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005863 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005864 " ts=%02x age=%s calls=%d",
5865 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005866 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5867 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005868
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005869 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005870 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005871 curr_sess->req.flags,
5872 curr_sess->req.buf->i,
5873 curr_sess->req.analysers,
5874 curr_sess->req.rex ?
5875 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005876 TICKS_TO_MS(1000)) : "");
5877
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005878 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005879 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005880 curr_sess->req.wex ?
5881 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005882 TICKS_TO_MS(1000)) : "");
5883
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005884 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005885 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005886 curr_sess->req.analyse_exp ?
5887 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005888 TICKS_TO_MS(1000)) : "");
5889
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005890 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005891 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005892 curr_sess->res.flags,
5893 curr_sess->res.buf->i,
5894 curr_sess->res.analysers,
5895 curr_sess->res.rex ?
5896 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005897 TICKS_TO_MS(1000)) : "");
5898
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005899 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005900 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005901 curr_sess->res.wex ?
5902 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005903 TICKS_TO_MS(1000)) : "");
5904
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005905 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005906 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005907 curr_sess->res.analyse_exp ?
5908 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005909 TICKS_TO_MS(1000)) : "");
5910
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005911 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005912 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005913 " s0=[%d,%1xh,fd=%d,ex=%s]",
5914 curr_sess->si[0].state,
5915 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005916 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005917 curr_sess->si[0].exp ?
5918 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5919 TICKS_TO_MS(1000)) : "");
5920
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005921 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005922 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005923 " s1=[%d,%1xh,fd=%d,ex=%s]",
5924 curr_sess->si[1].state,
5925 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005926 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005927 curr_sess->si[1].exp ?
5928 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5929 TICKS_TO_MS(1000)) : "");
5930
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005931 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005932 " exp=%s",
5933 curr_sess->task->expire ?
5934 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5935 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005936 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005937 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005938
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005939 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005940
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005941 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005942 /* let's try again later from this stream. We add ourselves into
5943 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005944 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005945 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005946 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005947 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005948 }
5949
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005950 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005951 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005952 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005953
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005954 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005955 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005956 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005957 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005958 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005959 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005960
Willy Tarreaubc18da12015-03-13 14:00:47 +01005961 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005962 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005963 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005964 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005965
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005966 appctx->ctx.sess.target = NULL;
5967 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005968 return 1;
5969 }
5970
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005971 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005972 /* fall through */
5973
5974 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005975 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005976 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005977 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005978}
5979
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005980/* This is called when the stream interface is closed. For instance, upon an
5981 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02005982 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005983 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005984static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005985{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005986 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5987 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5988 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005989 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005990 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5991 free(appctx->ctx.cli.err);
5992 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005993 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5994 free(appctx->ctx.map.chunk.str);
5995 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005996}
5997
Willy Tarreau20e99322013-04-13 09:22:25 +02005998/* This function is used to either dump tables states (when action is set
5999 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006000 * It returns 0 if the output buffer is full and it needs to be called
6001 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006002 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006003static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006004{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006005 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006006 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006007 struct ebmb_node *eb;
6008 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006009 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006010 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006011
6012 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006013 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006014 * - STAT_ST_INIT : the first call
6015 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006016 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006017 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006018 * and the entry pointer points to the next entry to be dumped,
6019 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006020 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006021 * data though.
6022 */
6023
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006024 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006025 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006026 if (appctx->st2 == STAT_ST_LIST) {
6027 appctx->ctx.table.entry->ref_cnt--;
6028 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006029 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006030 return 1;
6031 }
6032
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006033 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006034
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006035 while (appctx->st2 != STAT_ST_FIN) {
6036 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006037 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006038 appctx->ctx.table.proxy = appctx->ctx.table.target;
6039 if (!appctx->ctx.table.proxy)
6040 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006041
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006042 appctx->ctx.table.entry = NULL;
6043 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006044 break;
6045
Willy Tarreau295a8372011-03-10 11:25:07 +01006046 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006047 if (!appctx->ctx.table.proxy ||
6048 (appctx->ctx.table.target &&
6049 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6050 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006051 break;
6052 }
6053
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006054 if (appctx->ctx.table.proxy->table.size) {
6055 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6056 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006057 return 0;
6058
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006059 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006060 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006061 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006062 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006063 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006064 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6065 appctx->ctx.table.entry->ref_cnt++;
6066 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006067 break;
6068 }
6069 }
6070 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006071 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006072 break;
6073
Willy Tarreau295a8372011-03-10 11:25:07 +01006074 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006075 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006076
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006077 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006078 /* we're filtering on some data contents */
6079 void *ptr;
6080 long long data;
6081
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006082 dt = appctx->ctx.table.data_type;
6083 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6084 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006085 dt);
6086
6087 data = 0;
6088 switch (stktable_data_types[dt].std_type) {
6089 case STD_T_SINT:
6090 data = stktable_data_cast(ptr, std_t_sint);
6091 break;
6092 case STD_T_UINT:
6093 data = stktable_data_cast(ptr, std_t_uint);
6094 break;
6095 case STD_T_ULL:
6096 data = stktable_data_cast(ptr, std_t_ull);
6097 break;
6098 case STD_T_FRQP:
6099 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006100 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006101 break;
6102 }
6103
6104 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006105 if ((data < appctx->ctx.table.value &&
6106 (appctx->ctx.table.data_op == STD_OP_EQ ||
6107 appctx->ctx.table.data_op == STD_OP_GT ||
6108 appctx->ctx.table.data_op == STD_OP_GE)) ||
6109 (data == appctx->ctx.table.value &&
6110 (appctx->ctx.table.data_op == STD_OP_NE ||
6111 appctx->ctx.table.data_op == STD_OP_GT ||
6112 appctx->ctx.table.data_op == STD_OP_LT)) ||
6113 (data > appctx->ctx.table.value &&
6114 (appctx->ctx.table.data_op == STD_OP_EQ ||
6115 appctx->ctx.table.data_op == STD_OP_LT ||
6116 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006117 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006118 }
6119
Simon Hormanc88b8872011-06-15 15:18:49 +09006120 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006121 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6122 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006123 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006124
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006125 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006126
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006127 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006128 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006129 struct stksess *old = appctx->ctx.table.entry;
6130 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006131 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006132 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6133 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6134 stksess_kill(&appctx->ctx.table.proxy->table, old);
6135 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006136 break;
6137 }
6138
Simon Hormanc88b8872011-06-15 15:18:49 +09006139
6140 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006141 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6142 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6143 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006144
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006145 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6146 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006147 break;
6148
Willy Tarreau295a8372011-03-10 11:25:07 +01006149 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006150 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006151 break;
6152 }
6153 }
6154 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006155}
6156
Willy Tarreaud426a182010-03-05 14:58:26 +01006157/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006158 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6159 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6160 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6161 * lines are respected within the limit of 70 output chars. Lines that are
6162 * continuation of a previous truncated line begin with "+" instead of " "
6163 * after the offset. The new pointer is returned.
6164 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006165static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6166 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006167{
6168 int end;
6169 unsigned char c;
6170
6171 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006172 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006173 return ptr;
6174
Willy Tarreau77804732012-10-29 16:14:26 +01006175 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006176
Willy Tarreaud426a182010-03-05 14:58:26 +01006177 while (ptr < len && ptr < bsize) {
6178 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006179 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006180 if (out->len > end - 2)
6181 break;
6182 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006183 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006184 if (out->len > end - 3)
6185 break;
6186 out->str[out->len++] = '\\';
6187 switch (c) {
6188 case '\t': c = 't'; break;
6189 case '\n': c = 'n'; break;
6190 case '\r': c = 'r'; break;
6191 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006192 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006193 }
6194 out->str[out->len++] = c;
6195 } else {
6196 if (out->len > end - 5)
6197 break;
6198 out->str[out->len++] = '\\';
6199 out->str[out->len++] = 'x';
6200 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6201 out->str[out->len++] = hextab[c & 0xF];
6202 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006203 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006204 /* we had a line break, let's return now */
6205 out->str[out->len++] = '\n';
6206 *line = ptr;
6207 return ptr;
6208 }
6209 }
6210 /* we have an incomplete line, we return it as-is */
6211 out->str[out->len++] = '\n';
6212 return ptr;
6213}
6214
Baptiste Assmann3863f972015-05-17 00:33:24 +02006215/* This function dumps counters from all resolvers section and associated name servers.
6216 * It returns 0 if the output buffer is full and it needs
6217 * to be called again, otherwise non-zero.
6218 */
6219static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6220{
6221 struct appctx *appctx = __objt_appctx(si->end);
6222 struct dns_resolvers *presolvers;
6223 struct dns_nameserver *pnameserver;
6224
6225 chunk_reset(&trash);
6226
6227 switch (appctx->st2) {
6228 case STAT_ST_INIT:
6229 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6230 /* fall through */
6231
6232 case STAT_ST_LIST:
6233 presolvers = appctx->ctx.resolvers.ptr;
6234 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6235 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6236 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6237 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6238 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6239 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6240 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6241 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6242 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6243 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6244 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6245 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6246 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6247 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6248 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6249 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6250 }
6251
6252 /* display response */
6253 if (bi_putchk(si_ic(si), &trash) == -1) {
6254 /* let's try again later from this session. We add ourselves into
6255 * this session's users so that it can remove us upon termination.
6256 */
6257 si->flags |= SI_FL_WAIT_ROOM;
6258 return 0;
6259 }
6260
6261 appctx->st2 = STAT_ST_FIN;
6262 /* fall through */
6263
6264 default:
6265 appctx->st2 = STAT_ST_FIN;
6266 return 1;
6267 }
6268}
6269
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006270/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006271 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006272 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006273 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006274static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006275{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006276 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006277 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006278
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006279 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006280 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006281
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006282 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006283
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006284 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006285 /* the function had not been called yet, let's prepare the
6286 * buffer for a response.
6287 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006288 struct tm tm;
6289
6290 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006291 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006292 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6293 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6294 error_snapshot_id);
6295
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006296 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006297 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006298 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006299 return 0;
6300 }
6301
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006302 appctx->ctx.errors.px = proxy;
6303 appctx->ctx.errors.buf = 0;
6304 appctx->ctx.errors.bol = 0;
6305 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006306 }
6307
6308 /* we have two inner loops here, one for the proxy, the other one for
6309 * the buffer.
6310 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006311 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006312 struct error_snapshot *es;
6313
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006314 if (appctx->ctx.errors.buf == 0)
6315 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006316 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006317 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006318
6319 if (!es->when.tv_sec)
6320 goto next;
6321
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006322 if (appctx->ctx.errors.iid >= 0 &&
6323 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6324 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006325 goto next;
6326
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006327 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006328 /* just print headers now */
6329
6330 char pn[INET6_ADDRSTRLEN];
6331 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006332 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006333
6334 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006335 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006336 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006337 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006338
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006339 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6340 case AF_INET:
6341 case AF_INET6:
6342 port = get_host_port(&es->src);
6343 break;
6344 default:
6345 port = 0;
6346 }
6347
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006348 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006349 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006350 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006351 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006352 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006353 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006354 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6355 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006356 break;
6357 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006358 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006359 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006360 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006361 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006362 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006363 break;
6364 }
6365
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006366 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006367 ", server %s (#%d), event #%u\n"
6368 " src %s:%d, session #%d, session flags 0x%08x\n"
6369 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6370 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6371 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6372 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6373 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6374 es->ev_id,
6375 pn, port, es->sid, es->s_flags,
6376 es->state, es->m_flags, es->t_flags,
6377 es->m_clen, es->m_blen,
6378 es->b_flags, es->b_out, es->b_tot,
6379 es->len, es->b_wrap, es->pos);
6380
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006381 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006382 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006383 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006384 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006385 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006386 appctx->ctx.errors.ptr = 0;
6387 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006388 }
6389
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006390 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006391 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006392 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006393 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006394 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006395 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006396 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006397 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006398 goto next;
6399 }
6400
6401 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006402 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006403 int newptr;
6404 int newline;
6405
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006406 newline = appctx->ctx.errors.bol;
6407 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6408 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006409 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006410
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006411 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006412 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006413 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006414 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006415 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006416 appctx->ctx.errors.ptr = newptr;
6417 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006418 };
6419 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006420 appctx->ctx.errors.bol = 0;
6421 appctx->ctx.errors.ptr = -1;
6422 appctx->ctx.errors.buf++;
6423 if (appctx->ctx.errors.buf > 1) {
6424 appctx->ctx.errors.buf = 0;
6425 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006426 }
6427 }
6428
6429 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006430 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006431}
6432
Willy Tarreaud5781202012-09-22 19:32:35 +02006433/* parse the "level" argument on the bind lines */
6434static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6435{
6436 if (!*args[cur_arg + 1]) {
6437 memprintf(err, "'%s' : missing level", args[cur_arg]);
6438 return ERR_ALERT | ERR_FATAL;
6439 }
6440
6441 if (!strcmp(args[cur_arg+1], "user"))
6442 conf->level = ACCESS_LVL_USER;
6443 else if (!strcmp(args[cur_arg+1], "operator"))
6444 conf->level = ACCESS_LVL_OPER;
6445 else if (!strcmp(args[cur_arg+1], "admin"))
6446 conf->level = ACCESS_LVL_ADMIN;
6447 else {
6448 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6449 args[cur_arg], args[cur_arg+1]);
6450 return ERR_ALERT | ERR_FATAL;
6451 }
6452
6453 return 0;
6454}
6455
Willy Tarreau30576452015-04-13 13:50:30 +02006456struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006457 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006458 .name = "<STATS>", /* used for logging */
6459 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006460 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006461};
6462
Willy Tarreau30576452015-04-13 13:50:30 +02006463static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006464 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006465 .name = "<CLI>", /* used for logging */
6466 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006467 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006468};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006469
Willy Tarreaudc13c112013-06-21 23:16:39 +02006470static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006471 { CFG_GLOBAL, "stats", stats_parse_global },
6472 { 0, NULL, NULL },
6473}};
6474
Willy Tarreaud5781202012-09-22 19:32:35 +02006475static struct bind_kw_list bind_kws = { "STAT", { }, {
6476 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6477 { NULL, NULL, 0 },
6478}};
6479
Willy Tarreau10522fd2008-07-09 20:12:41 +02006480__attribute__((constructor))
6481static void __dumpstats_module_init(void)
6482{
6483 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006484 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006485}
6486
Willy Tarreau91861262007-10-17 17:06:05 +02006487/*
6488 * Local variables:
6489 * c-indent-level: 8
6490 * c-basic-offset: 8
6491 * End:
6492 */