blob: 92f2dead5bc72e0af60d4a3b839095bd13e580b4 [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
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200587 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900588 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 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200592 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900593 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 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200597 else if (proxy->table.type == SMP_T_SINT) {
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 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200600 else if (proxy->table.type == SMP_T_STR) {
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) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200672 case SMP_T_IPV4:
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;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200676 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900677 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;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200680 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900681 {
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;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200698 case SMP_T_STR:
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
Pradeep Jindalcc79b002015-08-20 18:25:17 +05301936 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 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;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001938
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 Tarreau4e5d58e2015-07-04 14:35:15 +02002830 "<td>%s%s<div class=tips><table class=det>"
2831 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
2832 "<tr><th>Compression in:</th><td>%s</td></tr>"
2833 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
2834 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
2835 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
2836 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002837 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002838 U2H(px->fe_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02002839 U2H(px->fe_counters.bytes_out),
2840 U2H(px->fe_counters.comp_in),
2841 U2H(px->fe_counters.comp_out),
2842 px->fe_counters.comp_in ? (int)(px->fe_counters.comp_out * 100 / px->fe_counters.comp_in) : 0,
2843 U2H(px->fe_counters.comp_byp),
2844 U2H(px->fe_counters.comp_in - px->fe_counters.comp_out),
2845 px->fe_counters.bytes_out ? (int)((px->fe_counters.comp_in - px->fe_counters.comp_out) * 100 / px->fe_counters.bytes_out) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002846 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002847
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002848 chunk_appendf(&trash,
2849 /* denied: req, resp */
2850 "<td>%s</td><td>%s</td>"
2851 /* errors : request, connect, response */
2852 "<td>%s</td><td></td><td></td>"
2853 /* warnings: retries, redispatches */
2854 "<td></td><td></td>"
2855 /* server status : reflect frontend status */
2856 "<td class=ac>%s</td>"
2857 /* rest of server: nothing */
2858 "<td class=ac colspan=8></td></tr>"
2859 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002860 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2861 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002862 px->state == PR_STREADY ? "OPEN" :
2863 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002864 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002865 else { /* CSV mode */
2866 chunk_appendf(&trash,
2867 /* pxid, name, queue cur, queue max, */
2868 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002869 /* sessions : current, max, limit, total */
2870 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002871 /* bytes : in, out */
2872 "%lld,%lld,"
2873 /* denied: req, resp */
2874 "%lld,%lld,"
2875 /* errors : request, connect, response */
2876 "%lld,,,"
2877 /* warnings: retries, redispatches */
2878 ",,"
2879 /* server status : reflect frontend status */
2880 "%s,"
2881 /* rest of server: nothing */
2882 ",,,,,,,,"
2883 /* pid, iid, sid, throttle, lbtot, tracked, type */
2884 "%d,%d,0,,,,%d,"
2885 /* rate, rate_lim, rate_max */
2886 "%u,%u,%u,"
2887 /* check_status, check_code, check_duration */
2888 ",,,",
2889 px->id,
2890 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2891 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2892 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2893 px->fe_counters.failed_req,
2894 px->state == PR_STREADY ? "OPEN" :
2895 px->state == PR_STFULL ? "FULL" : "STOP",
2896 relative_pid, px->uuid, STATS_TYPE_FE,
2897 read_freq_ctr(&px->fe_sess_per_sec),
2898 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002899
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002900 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2901 if (px->mode == PR_MODE_HTTP) {
2902 for (i=1; i<6; i++)
2903 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2904 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2905 }
2906 else
2907 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002908
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002909 /* failed health analyses */
2910 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002911
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002912 /* requests : req_rate, req_rate_max, req_tot, */
2913 chunk_appendf(&trash, "%u,%u,%lld,",
2914 read_freq_ctr(&px->fe_req_per_sec),
2915 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2916
2917 /* errors: cli_aborts, srv_aborts */
2918 chunk_appendf(&trash, ",,");
2919
2920 /* compression: in, out, bypassed */
2921 chunk_appendf(&trash, "%lld,%lld,%lld,",
2922 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2923
2924 /* compression: comp_rsp */
2925 chunk_appendf(&trash, "%lld,",
2926 px->fe_counters.p.http.comp_rsp);
2927
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002928 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2929 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002930
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002931 /* finish with EOL */
2932 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002933 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002934 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002935}
2936
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002937/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2938 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2939 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2940 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002941 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002942static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002943{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002944 struct appctx *appctx = __objt_appctx(si->end);
2945
2946 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002947 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002948 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002949 /* Column sub-heading for Enable or Disable server */
2950 chunk_appendf(&trash, "<td></td>");
2951 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002952 chunk_appendf(&trash,
2953 /* frontend name, listener name */
2954 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2955 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2956 "",
2957 px->id, l->name,
2958 (flags & ST_SHLGNDS)?"<u>":"",
2959 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002960
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002961 if (flags & ST_SHLGNDS) {
2962 char str[INET6_ADDRSTRLEN];
2963 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002964
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002965 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002966
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002967 port = get_host_port(&l->addr);
2968 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2969 case AF_INET:
2970 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2971 break;
2972 case AF_INET6:
2973 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2974 break;
2975 case AF_UNIX:
2976 chunk_appendf(&trash, "unix, ");
2977 break;
2978 case -1:
2979 chunk_appendf(&trash, "(%s), ", strerror(errno));
2980 break;
2981 }
Willy Tarreau91861262007-10-17 17:06:05 +02002982
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002983 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002984 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002985 }
Willy Tarreau91861262007-10-17 17:06:05 +02002986
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002987 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002988 /* queue */
2989 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002990 /* sessions rate: current, max, limit */
2991 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002992 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002993 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002994 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002995 /* bytes: in, out */
2996 "<td>%s</td><td>%s</td>"
2997 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002998 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002999 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
3000 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02003001
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003002 chunk_appendf(&trash,
3003 /* denied: req, resp */
3004 "<td>%s</td><td>%s</td>"
3005 /* errors: request, connect, response */
3006 "<td>%s</td><td></td><td></td>"
3007 /* warnings: retries, redispatches */
3008 "<td></td><td></td>"
3009 /* server status: reflect listener status */
3010 "<td class=ac>%s</td>"
3011 /* rest of server: nothing */
3012 "<td class=ac colspan=8></td></tr>"
3013 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003014 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
3015 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003016 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
3017 }
3018 else { /* CSV mode */
3019 chunk_appendf(&trash,
3020 /* pxid, name, queue cur, queue max, */
3021 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003022 /* sessions: current, max, limit, total */
3023 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003024 /* bytes: in, out */
3025 "%lld,%lld,"
3026 /* denied: req, resp */
3027 "%lld,%lld,"
3028 /* errors: request, connect, response */
3029 "%lld,,,"
3030 /* warnings: retries, redispatches */
3031 ",,"
3032 /* server status: reflect listener status */
3033 "%s,"
3034 /* rest of server: nothing */
3035 ",,,,,,,,"
3036 /* pid, iid, sid, throttle, lbtot, tracked, type */
3037 "%d,%d,%d,,,,%d,"
3038 /* rate, rate_lim, rate_max */
3039 ",,,"
3040 /* check_status, check_code, check_duration */
3041 ",,,"
3042 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3043 ",,,,,,"
3044 /* failed health analyses */
3045 ","
3046 /* requests : req_rate, req_rate_max, req_tot, */
3047 ",,,"
3048 /* errors: cli_aborts, srv_aborts */
3049 ",,"
3050 /* compression: in, out, bypassed, comp_rsp */
3051 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003052 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3053 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003054 "\n",
3055 px->id, l->name,
3056 l->nbconn, l->counters->conn_max,
3057 l->maxconn, l->counters->cum_conn,
3058 l->counters->bytes_in, l->counters->bytes_out,
3059 l->counters->denied_req, l->counters->denied_resp,
3060 l->counters->failed_req,
3061 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
3062 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
3063 }
3064 return 1;
3065}
Willy Tarreau91861262007-10-17 17:06:05 +02003066
Simon Horman4d2eab62015-04-23 14:51:26 +09003067enum srv_stats_state {
3068 SRV_STATS_STATE_DOWN = 0,
3069 SRV_STATS_STATE_DOWN_AGENT,
3070 SRV_STATS_STATE_GOING_UP,
3071 SRV_STATS_STATE_UP_GOING_DOWN,
3072 SRV_STATS_STATE_UP,
3073 SRV_STATS_STATE_NOLB_GOING_DOWN,
3074 SRV_STATS_STATE_NOLB,
3075 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3076 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003077 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003078 SRV_STATS_STATE_NO_CHECK,
3079
3080 SRV_STATS_STATE_COUNT, /* Must be last */
3081};
3082
Simon Horman837bfa72015-04-23 14:51:27 +09003083enum srv_stats_colour {
3084 SRV_STATS_COLOUR_DOWN = 0,
3085 SRV_STATS_COLOUR_GOING_UP,
3086 SRV_STATS_COLOUR_GOING_DOWN,
3087 SRV_STATS_COLOUR_UP,
3088 SRV_STATS_COLOUR_NOLB,
3089 SRV_STATS_COLOUR_DRAINING,
3090 SRV_STATS_COLOUR_NO_CHECK,
3091
3092 SRV_STATS_COLOUR_COUNT, /* Must be last */
3093};
3094
3095static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3096 [SRV_STATS_COLOUR_DOWN] = "down",
3097 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3098 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3099 [SRV_STATS_COLOUR_UP] = "up",
3100 [SRV_STATS_COLOUR_NOLB] = "nolb",
3101 [SRV_STATS_COLOUR_DRAINING] = "draining",
3102 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3103};
3104
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003105/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3106 * from stream interface <si>, stats flags <flags>, and server state <state>.
3107 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003108 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003109 */
Simon Horman4d2eab62015-04-23 14:51:26 +09003110static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09003111 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003112{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003113 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003114 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003115 char str[INET6_ADDRSTRLEN];
3116 struct chunk src;
3117 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02003118
Willy Tarreau32091232014-05-16 13:52:00 +02003119 /* we have "via" which is the tracked server as described in the configuration,
3120 * and "ref" which is the checked server and the end of the chain.
3121 */
3122 via = sv->track ? sv->track : sv;
3123 ref = via;
3124 while (ref->track)
3125 ref = ref->track;
3126
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003127 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003128 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3129 [SRV_STATS_STATE_DOWN] = "DOWN",
3130 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3131 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
3132 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
3133 [SRV_STATS_STATE_UP] = "UP",
3134 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
3135 [SRV_STATS_STATE_NOLB] = "NOLB",
3136 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
3137 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003138 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003139 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003140 };
Willy Tarreau91861262007-10-17 17:06:05 +02003141
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003142 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003143 chunk_appendf(&trash, "<tr class=\"maintain\">");
3144 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003145 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003146 "<tr class=\"%s_%s\">",
3147 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003148
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003149 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003150 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003151 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3152 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003153
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003154 chunk_appendf(&trash,
3155 "<td class=ac><a name=\"%s/%s\"></a>%s"
3156 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3157 "",
3158 px->id, sv->id,
3159 (flags & ST_SHLGNDS) ? "<u>" : "",
3160 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003161
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003162 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003163 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003164
3165 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3166 case AF_INET:
3167 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3168 break;
3169 case AF_INET6:
3170 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3171 break;
3172 case AF_UNIX:
3173 chunk_appendf(&trash, "unix, ");
3174 break;
3175 case -1:
3176 chunk_appendf(&trash, "(%s), ", strerror(errno));
3177 break;
3178 default: /* address family not supported */
3179 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003180 }
Willy Tarreau91861262007-10-17 17:06:05 +02003181
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003182 /* id */
3183 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003184
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003185 /* cookie */
3186 if (sv->cookie) {
3187 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003188
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003189 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3190 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003191
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003192 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003193 }
3194
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003195 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003196 }
Willy Tarreau91861262007-10-17 17:06:05 +02003197
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003198 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003199 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003200 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003201 /* sessions rate : current, max, limit */
3202 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003203 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003204 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003205 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3206 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003207
Willy Tarreau466c9b52012-12-23 02:25:03 +01003208
3209 chunk_appendf(&trash,
3210 /* sessions: current, max, limit, total */
3211 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003212 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003213 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3214 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003215 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3216 U2H(sv->counters.cum_sess),
3217 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003218
Willy Tarreau466c9b52012-12-23 02:25:03 +01003219 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3220 if (px->mode == PR_MODE_HTTP) {
3221 unsigned long long tot;
3222 for (tot = i = 0; i < 6; i++)
3223 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003224
Willy Tarreau466c9b52012-12-23 02:25:03 +01003225 chunk_appendf(&trash,
3226 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3227 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3228 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3229 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3230 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3231 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3232 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3233 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003234 U2H(tot),
3235 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3236 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3237 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3238 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3239 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3240 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 +02003241 }
Willy Tarreau91861262007-10-17 17:06:05 +02003242
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003243 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3244 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)));
3245 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)));
3246 if (px->mode == PR_MODE_HTTP)
3247 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)));
3248 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)));
3249
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003250 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003251 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003252 /* sessions: lbtot, last */
3253 "<td>%s</td><td>%s</td>",
3254 U2H(sv->counters.cum_lbconn),
3255 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003256
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003257 chunk_appendf(&trash,
3258 /* bytes : in, out */
3259 "<td>%s</td><td>%s</td>"
3260 /* denied: req, resp */
3261 "<td></td><td>%s</td>"
3262 /* errors : request, connect */
3263 "<td></td><td>%s</td>"
3264 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003265 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003266 /* warnings: retries, redispatches */
3267 "<td>%lld</td><td>%lld</td>"
3268 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003269 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3270 U2H(sv->counters.failed_secu),
3271 U2H(sv->counters.failed_conns),
3272 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003273 sv->counters.cli_aborts,
3274 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003275 sv->counters.retries, sv->counters.redispatches);
3276
3277 /* status, lest check */
3278 chunk_appendf(&trash, "<td class=ac>");
3279
Willy Tarreau20125212014-05-13 19:44:56 +02003280 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003281 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3282 chunk_appendf(&trash, "MAINT");
3283 }
Simon Horman0766e442014-11-12 15:55:54 +09003284 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003285 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003286 /* DOWN (agent) */
3287 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 +02003288 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003289 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003290 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3291 chunk_appendf(&trash,
3292 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003293 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3294 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003295 }
Willy Tarreau91861262007-10-17 17:06:05 +02003296
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003297 if ((sv->state == SRV_ST_STOPPED) &&
3298 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3299 chunk_appendf(&trash,
3300 "</td><td class=ac><u> %s%s",
3301 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3302 get_check_status_info(sv->agent.status));
3303
3304 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3305 chunk_appendf(&trash, "/%d", sv->agent.code);
3306
3307 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3308 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3309
3310 chunk_appendf(&trash, "<div class=tips>%s",
3311 get_check_status_description(sv->agent.status));
3312 if (*sv->agent.desc) {
3313 chunk_appendf(&trash, ": ");
3314 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3315 chunk_htmlencode(&trash, &src);
3316 }
3317 chunk_appendf(&trash, "</div></u>");
3318 }
3319 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003320 chunk_appendf(&trash,
3321 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003322 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003323 get_check_status_info(sv->check.status));
3324
3325 if (sv->check.status >= HCHK_STATUS_L57DATA)
3326 chunk_appendf(&trash, "/%d", sv->check.code);
3327
3328 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003329 chunk_appendf(&trash, " in %lums", sv->check.duration);
3330
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003331 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003332 get_check_status_description(sv->check.status));
3333 if (*sv->check.desc) {
3334 chunk_appendf(&trash, ": ");
3335 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3336 chunk_htmlencode(&trash, &src);
3337 }
3338 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003339 }
3340 else
3341 chunk_appendf(&trash, "</td><td>");
3342
3343 chunk_appendf(&trash,
3344 /* weight */
3345 "</td><td class=ac>%d</td>"
3346 /* act, bck */
3347 "<td class=ac>%s</td><td class=ac>%s</td>"
3348 "",
3349 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003350 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3351 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003352
3353 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003354 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003355 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003356
3357 if (ref->observe)
3358 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3359
3360 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003361 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003362 "<td>%lld</td><td>%s</td>"
3363 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003364 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003365 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3366 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003367 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3368 /* tracking a server */
3369 chunk_appendf(&trash,
3370 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003371 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003372 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 else
3374 chunk_appendf(&trash, "<td colspan=3></td>");
3375
3376 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003377 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003378 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 else
3380 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3381 }
3382 else { /* CSV mode */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003383 struct chunk *out = get_trash_chunk();
Simon Horman4d2eab62015-04-23 14:51:26 +09003384 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3385 [SRV_STATS_STATE_DOWN] = "DOWN,",
3386 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3387 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3388 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3389 [SRV_STATS_STATE_UP] = "UP,",
3390 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3391 [SRV_STATS_STATE_NOLB] = "NOLB,",
3392 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3393 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003394 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003395 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003396 };
3397
3398 chunk_appendf(&trash,
3399 /* pxid, name */
3400 "%s,%s,"
3401 /* queue : current, max */
3402 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003403 /* sessions : current, max, limit, total */
3404 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405 /* bytes : in, out */
3406 "%lld,%lld,"
3407 /* denied: req, resp */
3408 ",%lld,"
3409 /* errors : request, connect, response */
3410 ",%lld,%lld,"
3411 /* warnings: retries, redispatches */
3412 "%lld,%lld,"
3413 "",
3414 px->id, sv->id,
3415 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003416 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003417 sv->counters.bytes_in, sv->counters.bytes_out,
3418 sv->counters.failed_secu,
3419 sv->counters.failed_conns, sv->counters.failed_resp,
3420 sv->counters.retries, sv->counters.redispatches);
3421
3422 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003423 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003424 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003425 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003426 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003427 else
3428 chunk_appendf(&trash,
3429 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003430 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3431 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003432
3433 chunk_appendf(&trash,
3434 /* weight, active, backup */
3435 "%d,%d,%d,"
3436 "",
3437 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003438 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3439 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003440
3441 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003442 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003443 chunk_appendf(&trash,
3444 "%lld,%lld,%d,%d,",
3445 sv->counters.failed_checks, sv->counters.down_trans,
3446 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3447 else
3448 chunk_appendf(&trash, ",,,,");
3449
3450 /* queue limit, pid, iid, sid, */
3451 chunk_appendf(&trash,
3452 "%s,"
3453 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003454 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003455 relative_pid, px->uuid, sv->puid);
3456
3457 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003458 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003459 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003460
3461 /* sessions: lbtot */
3462 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3463
3464 /* tracked */
3465 if (sv->track)
3466 chunk_appendf(&trash, "%s/%s,",
3467 sv->track->proxy->id, sv->track->id);
3468 else
3469 chunk_appendf(&trash, ",");
3470
3471 /* type */
3472 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3473
3474 /* rate */
3475 chunk_appendf(&trash, "%u,,%u,",
3476 read_freq_ctr(&sv->sess_per_sec),
3477 sv->counters.sps_max);
3478
Willy Tarreauff5ae352013-12-11 20:36:34 +01003479 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003480 /* check_status */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003481 chunk_appendf(&trash, "%s,", csv_enc(get_check_status_info(sv->check.status), 1, out));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003482
3483 /* check_code */
3484 if (sv->check.status >= HCHK_STATUS_L57DATA)
3485 chunk_appendf(&trash, "%u,", sv->check.code);
3486 else
3487 chunk_appendf(&trash, ",");
3488
3489 /* check_duration */
3490 if (sv->check.status >= HCHK_STATUS_CHECKED)
3491 chunk_appendf(&trash, "%lu,", sv->check.duration);
3492 else
3493 chunk_appendf(&trash, ",");
3494
3495 }
3496 else
3497 chunk_appendf(&trash, ",,,");
3498
3499 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3500 if (px->mode == PR_MODE_HTTP) {
3501 for (i=1; i<6; i++)
3502 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3503
3504 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3505 }
3506 else
3507 chunk_appendf(&trash, ",,,,,,");
3508
3509 /* failed health analyses */
3510 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3511
3512 /* requests : req_rate, req_rate_max, req_tot, */
3513 chunk_appendf(&trash, ",,,");
3514
3515 /* errors: cli_aborts, srv_aborts */
3516 chunk_appendf(&trash, "%lld,%lld,",
3517 sv->counters.cli_aborts, sv->counters.srv_aborts);
3518
3519 /* compression: in, out, bypassed, comp_rsp */
3520 chunk_appendf(&trash, ",,,,");
3521
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003522 /* lastsess */
3523 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3524
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003525 /* capture of last check and agent statuses */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003526 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->check.desc), 1, out) : "");
3527 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 +02003528
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003529 /* qtime, ctime, rtime, ttime, */
3530 chunk_appendf(&trash, "%u,%u,%u,%u,",
3531 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3532 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3533 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3534 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3535
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003536 /* finish with EOL */
3537 chunk_appendf(&trash, "\n");
3538 }
3539 return 1;
3540}
3541
3542/* Dumps a line for backend <px> to the trash for and uses the state from stream
3543 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3544 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3545 */
3546static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3547{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003548 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003549 struct chunk src;
3550 int i;
3551
3552 if (!(px->cap & PR_CAP_BE))
3553 return 0;
3554
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003555 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003556 return 0;
3557
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003558 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003559 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003560 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003561 /* Column sub-heading for Enable or Disable server */
3562 chunk_appendf(&trash, "<td></td>");
3563 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003564 chunk_appendf(&trash,
3565 "<td class=ac>"
3566 /* name */
3567 "%s<a name=\"%s/Backend\"></a>"
3568 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3569 "",
3570 (flags & ST_SHLGNDS)?"<u>":"",
3571 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003572
3573 if (flags & ST_SHLGNDS) {
3574 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003575 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003576 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3577
3578 /* cookie */
3579 if (px->cookie_name) {
3580 chunk_appendf(&trash, ", cookie: '");
3581 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3582 chunk_htmlencode(&trash, &src);
3583 chunk_appendf(&trash, "'");
3584 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003585 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003586 }
3587
3588 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003589 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003590 /* queue : current, max */
3591 "<td>%s</td><td>%s</td><td></td>"
3592 /* sessions rate : current, max, limit */
3593 "<td>%s</td><td>%s</td><td></td>"
3594 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003595 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003596 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3597 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003598
3599 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003600 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003601 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003602 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003603 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003604 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003605 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3606 U2H(px->be_counters.cum_conn),
3607 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003608
Willy Tarreau466c9b52012-12-23 02:25:03 +01003609 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003610 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003611 chunk_appendf(&trash,
3612 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3613 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3614 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3615 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3616 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3617 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3618 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3619 "<tr><th>- other responses:</th><td>%s</td></tr>"
3620 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003621 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003622 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003623 U2H(px->be_counters.p.http.cum_req),
3624 U2H(px->be_counters.p.http.rsp[1]),
3625 U2H(px->be_counters.p.http.rsp[2]),
3626 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003627 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003628 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003629 U2H(px->be_counters.p.http.rsp[3]),
3630 U2H(px->be_counters.p.http.rsp[4]),
3631 U2H(px->be_counters.p.http.rsp[5]),
3632 U2H(px->be_counters.p.http.rsp[0]),
3633 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003634 }
3635
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003636 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)));
3637 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)));
3638 if (px->mode == PR_MODE_HTTP)
3639 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)));
3640 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)));
3641
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003642 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003643 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003644 /* sessions: lbtot, last */
3645 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003646 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003647 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003648 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003649 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003650 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003651 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003652
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003653 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003654 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003655 "<td>%s%s<div class=tips><table class=det>"
3656 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3657 "<tr><th>Compression in:</th><td>%s</td></tr>"
3658 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3659 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3660 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3661 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003662 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003663 U2H(px->be_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003664 U2H(px->be_counters.bytes_out),
3665 U2H(px->be_counters.comp_in),
3666 U2H(px->be_counters.comp_out),
3667 px->be_counters.comp_in ? (int)(px->be_counters.comp_out * 100 / px->be_counters.comp_in) : 0,
3668 U2H(px->be_counters.comp_byp),
3669 U2H(px->be_counters.comp_in - px->be_counters.comp_out),
3670 px->be_counters.bytes_out ? (int)((px->be_counters.comp_in - px->be_counters.comp_out) * 100 / px->be_counters.bytes_out) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003671 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3672
3673 chunk_appendf(&trash,
3674 /* denied: req, resp */
3675 "<td>%s</td><td>%s</td>"
3676 /* errors : request, connect */
3677 "<td></td><td>%s</td>"
3678 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003679 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003680 /* warnings: retries, redispatches */
3681 "<td>%lld</td><td>%lld</td>"
3682 /* backend status: reflect backend status (up/down): we display UP
3683 * if the backend has known working servers or if it has no server at
3684 * all (eg: for stats). Then we display the total weight, number of
3685 * active and backups. */
3686 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3687 "<td class=ac>%d</td><td class=ac>%d</td>"
3688 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003689 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3690 U2H(px->be_counters.failed_conns),
3691 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003692 px->be_counters.cli_aborts,
3693 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003694 px->be_counters.retries, px->be_counters.redispatches,
3695 human_time(now.tv_sec - px->last_change, 1),
3696 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3697 "<font color=\"red\"><b>DOWN</b></font>",
3698 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3699 px->srv_act, px->srv_bck);
3700
3701 chunk_appendf(&trash,
3702 /* rest of backend: nothing, down transitions, total downtime, throttle */
3703 "<td class=ac>&nbsp;</td><td>%d</td>"
3704 "<td>%s</td>"
3705 "<td></td>"
3706 "</tr>",
3707 px->down_trans,
3708 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3709 }
3710 else { /* CSV mode */
3711 chunk_appendf(&trash,
3712 /* pxid, name */
3713 "%s,BACKEND,"
3714 /* queue : current, max */
3715 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003716 /* sessions : current, max, limit, total */
3717 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003718 /* bytes : in, out */
3719 "%lld,%lld,"
3720 /* denied: req, resp */
3721 "%lld,%lld,"
3722 /* errors : request, connect, response */
3723 ",%lld,%lld,"
3724 /* warnings: retries, redispatches */
3725 "%lld,%lld,"
3726 /* backend status: reflect backend status (up/down): we display UP
3727 * if the backend has known working servers or if it has no server at
3728 * all (eg: for stats). Then we display the total weight, number of
3729 * active and backups. */
3730 "%s,"
3731 "%d,%d,%d,"
3732 /* rest of backend: nothing, down transitions, last change, total downtime */
3733 ",%d,%d,%d,,"
3734 /* pid, iid, sid, throttle, lbtot, tracked, type */
3735 "%d,%d,0,,%lld,,%d,"
3736 /* rate, rate_lim, rate_max, */
3737 "%u,,%u,"
3738 /* check_status, check_code, check_duration */
3739 ",,,",
3740 px->id,
3741 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3742 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3743 px->be_counters.bytes_in, px->be_counters.bytes_out,
3744 px->be_counters.denied_req, px->be_counters.denied_resp,
3745 px->be_counters.failed_conns, px->be_counters.failed_resp,
3746 px->be_counters.retries, px->be_counters.redispatches,
3747 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3748 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3749 px->srv_act, px->srv_bck,
3750 px->down_trans, (int)(now.tv_sec - px->last_change),
3751 px->srv?be_downtime(px):0,
3752 relative_pid, px->uuid,
3753 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3754 read_freq_ctr(&px->be_sess_per_sec),
3755 px->be_counters.sps_max);
3756
3757 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3758 if (px->mode == PR_MODE_HTTP) {
3759 for (i=1; i<6; i++)
3760 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3761 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3762 }
3763 else
3764 chunk_appendf(&trash, ",,,,,,");
3765
3766 /* failed health analyses */
3767 chunk_appendf(&trash, ",");
3768
3769 /* requests : req_rate, req_rate_max, req_tot, */
3770 chunk_appendf(&trash, ",,,");
3771
3772 /* errors: cli_aborts, srv_aborts */
3773 chunk_appendf(&trash, "%lld,%lld,",
3774 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3775
3776 /* compression: in, out, bypassed */
3777 chunk_appendf(&trash, "%lld,%lld,%lld,",
3778 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3779
3780 /* compression: comp_rsp */
3781 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3782
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003783 /* lastsess, last_chk, last_agt, */
3784 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003785
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003786 /* qtime, ctime, rtime, ttime, */
3787 chunk_appendf(&trash, "%u,%u,%u,%u,",
3788 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3789 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3790 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3791 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3792
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003793 /* finish with EOL */
3794 chunk_appendf(&trash, "\n");
3795 }
3796 return 1;
3797}
3798
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003799/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003800 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003801 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003802 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003803static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003804{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003805 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003806 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3807
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003808 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003809 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003810
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003811 /* 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 +02003812 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003813 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003814 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003815 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 +01003816 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003817 }
3818
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003819 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05003820 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003821 }
3822
3823 /* print a new table */
3824 chunk_appendf(&trash,
3825 "<table class=\"tbl\" width=\"100%%\">\n"
3826 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003827 "<th class=\"pxname\" width=\"10%%\">");
3828
3829 chunk_appendf(&trash,
3830 "<a name=\"%s\"></a>%s"
3831 "<a class=px href=\"#%s\">%s</a>",
3832 px->id,
3833 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3834 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003835
3836 if (uri->flags & ST_SHLGNDS) {
3837 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003838 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003839 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3840 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003841 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003842 }
3843
3844 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003845 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003846 "<th class=\"%s\" width=\"90%%\">%s</th>"
3847 "</tr>\n"
3848 "</table>\n"
3849 "<table class=\"tbl\" width=\"100%%\">\n"
3850 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003851 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3852 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3853
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003854 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003855 /* Column heading for Enable or Disable server */
3856 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003857 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003858
3859 chunk_appendf(&trash,
3860 "<th rowspan=2></th>"
3861 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003862 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003863 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3864 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3865 "<th colspan=9>Server</th>"
3866 "</tr>\n"
3867 "<tr class=\"titre\">"
3868 "<th>Cur</th><th>Max</th><th>Limit</th>"
3869 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003870 "<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 +01003871 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3872 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3873 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3874 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3875 "<th>Thrtle</th>\n"
3876 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003877}
3878
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003879/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003880 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003881 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003882static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003883{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003884 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003885 chunk_appendf(&trash, "</table>");
3886
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003887 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003888 /* close the form used to enable/disable this proxy servers */
3889 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003890 "Choose the action to perform on the checked servers : "
3891 "<select name=action>"
3892 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003893 "<option value=\"ready\">Set state to READY</option>"
3894 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02003895 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003896 "<option value=\"dhlth\">Health: disable checks</option>"
3897 "<option value=\"ehlth\">Health: enable checks</option>"
3898 "<option value=\"hrunn\">Health: force UP</option>"
3899 "<option value=\"hnolb\">Health: force NOLB</option>"
3900 "<option value=\"hdown\">Health: force DOWN</option>"
3901 "<option value=\"dagent\">Agent: disable checks</option>"
3902 "<option value=\"eagent\">Agent: enable checks</option>"
3903 "<option value=\"arunn\">Agent: force UP</option>"
3904 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003905 "<option value=\"shutdown\">Kill Sessions</option>"
3906 "</select>"
3907 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3908 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3909 "</form>",
3910 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003911 }
3912
3913 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003914}
Willy Tarreau91861262007-10-17 17:06:05 +02003915
3916/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003917 * Dumps statistics for a proxy. The output is sent to the stream interface's
3918 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3919 * buffer space, or non-zero if everything completed. This function is used
3920 * both by the CLI and the HTTP entry points, and is able to dump the output
3921 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003922 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003923static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003924{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003925 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02003926 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003927 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02003928 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003929 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003930
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003931 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003932
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003933 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003934 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003935 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003936 if (uri && uri->scope) {
3937 /* we have a limited scope, we have to check the proxy name */
3938 struct stat_scope *scope;
3939 int len;
3940
3941 len = strlen(px->id);
3942 scope = uri->scope;
3943
3944 while (scope) {
3945 /* match exact proxy name */
3946 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3947 break;
3948
3949 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003950 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003951 break;
3952 scope = scope->next;
3953 }
3954
3955 /* proxy name not found : don't dump anything */
3956 if (scope == NULL)
3957 return 1;
3958 }
3959
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003960 /* if the user has requested a limited output and the proxy
3961 * name does not match, skip it.
3962 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003963 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003964 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 +02003965 return 1;
3966
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003967 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3968 (appctx->ctx.stats.iid != -1) &&
3969 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003970 return 1;
3971
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003972 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003973 /* fall through */
3974
Willy Tarreau295a8372011-03-10 11:25:07 +01003975 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003976 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003977 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003978 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003979 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02003980 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003981 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003982 }
Willy Tarreau91861262007-10-17 17:06:05 +02003983
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003984 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003985 /* fall through */
3986
Willy Tarreau295a8372011-03-10 11:25:07 +01003987 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003988 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003989 if (stats_dump_fe_stats(si, px)) {
3990 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003991 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02003992 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003993 }
3994 }
Willy Tarreau91861262007-10-17 17:06:05 +02003995
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003996 appctx->ctx.stats.l = px->conf.listeners.n;
3997 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003998 /* fall through */
3999
Willy Tarreau295a8372011-03-10 11:25:07 +01004000 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004001 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004002 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004003 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004004 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004005 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004006 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004007
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004008 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004009 if (!l->counters)
4010 continue;
4011
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004012 if (appctx->ctx.stats.flags & STAT_BOUND) {
4013 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004014 break;
4015
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004016 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004017 continue;
4018 }
4019
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004020 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004021 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
4022 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004023 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004024 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004025 }
4026 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004027 }
4028
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004029 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4030 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004031 /* fall through */
4032
Willy Tarreau295a8372011-03-10 11:25:07 +01004033 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004034 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004035 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004036 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09004037 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02004038
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004039 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004040 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004041 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004042 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004043
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004044 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004045
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004046 if (appctx->ctx.stats.flags & STAT_BOUND) {
4047 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004048 break;
4049
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004050 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004051 continue;
4052 }
4053
Willy Tarreau32091232014-05-16 13:52:00 +02004054 svs = sv;
4055 while (svs->track)
4056 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004057
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004058 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004059 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09004060 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004061 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004062 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4063 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004064 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004065 sv_colour = SRV_STATS_COLOUR_UP;
4066 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004067
Simon Horman4463d192015-04-23 14:51:28 +09004068 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
4069 sv_colour = SRV_STATS_COLOUR_DRAINING;
4070
4071 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09004072 if (svs->agent.state & CHK_ST_ENABLED)
4073 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
4074 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09004075 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09004076 else
Simon Horman4d2eab62015-04-23 14:51:26 +09004077 sv_state = SRV_STATS_STATE_DRAIN;
4078 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004079
Simon Horman837bfa72015-04-23 14:51:27 +09004080 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004081 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09004082 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
4083 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004084 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004085 else if (sv->state == SRV_ST_STOPPING) {
4086 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09004087 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004088 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09004089 sv_colour = SRV_STATS_COLOUR_NOLB;
4090 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004091 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004092 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4093 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004094 }
4095 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09004096 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004097 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09004098 sv_colour = SRV_STATS_COLOUR_DOWN;
4099 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004100 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09004101 sv_colour = SRV_STATS_COLOUR_DOWN;
4102 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004103 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004104 sv_colour = SRV_STATS_COLOUR_GOING_UP;
4105 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004106 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09004107 sv_colour = SRV_STATS_COLOUR_DOWN;
4108 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004109 }
Willy Tarreau91861262007-10-17 17:06:05 +02004110
Willy Tarreau9638efa2014-05-23 11:19:57 +02004111 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02004112 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004113 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02004114 continue;
4115 }
4116
Simon Horman837bfa72015-04-23 14:51:27 +09004117 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004118 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004119 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004120 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004121 }
4122 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004123 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004124
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004125 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004126 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004127
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004128 case STAT_PX_ST_BE:
4129 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004130 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
4131 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004132 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004133 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004134 }
4135 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004136
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004137 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004138 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004139
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004140 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004141 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004142 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004143 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004144 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004145 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004146 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004147 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004148
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004149 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004150 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004151
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004152 case STAT_PX_ST_FIN:
4153 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004154
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004155 default:
4156 /* unknown state, we should put an abort() here ! */
4157 return 1;
4158 }
4159}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004160
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004161/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4162 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004163 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004164static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004165{
4166 /* WARNING! This must fit in the first buffer !!! */
4167 chunk_appendf(&trash,
4168 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4169 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4170 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4171 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4172 "<style type=\"text/css\"><!--\n"
4173 "body {"
4174 " font-family: arial, helvetica, sans-serif;"
4175 " font-size: 12px;"
4176 " font-weight: normal;"
4177 " color: black;"
4178 " background: white;"
4179 "}\n"
4180 "th,td {"
4181 " font-size: 10px;"
4182 "}\n"
4183 "h1 {"
4184 " font-size: x-large;"
4185 " margin-bottom: 0.5em;"
4186 "}\n"
4187 "h2 {"
4188 " font-family: helvetica, arial;"
4189 " font-size: x-large;"
4190 " font-weight: bold;"
4191 " font-style: italic;"
4192 " color: #6020a0;"
4193 " margin-top: 0em;"
4194 " margin-bottom: 0em;"
4195 "}\n"
4196 "h3 {"
4197 " font-family: helvetica, arial;"
4198 " font-size: 16px;"
4199 " font-weight: bold;"
4200 " color: #b00040;"
4201 " background: #e8e8d0;"
4202 " margin-top: 0em;"
4203 " margin-bottom: 0em;"
4204 "}\n"
4205 "li {"
4206 " margin-top: 0.25em;"
4207 " margin-right: 2em;"
4208 "}\n"
4209 ".hr {margin-top: 0.25em;"
4210 " border-color: black;"
4211 " border-bottom-style: solid;"
4212 "}\n"
4213 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4214 ".total {background: #20D0D0;color: #ffff80;}\n"
4215 ".frontend {background: #e8e8d0;}\n"
4216 ".socket {background: #d0d0d0;}\n"
4217 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004218 ".active_down {background: #ff9090;}\n"
4219 ".active_going_up {background: #ffd020;}\n"
4220 ".active_going_down {background: #ffffa0;}\n"
4221 ".active_up {background: #c0ffc0;}\n"
4222 ".active_nolb {background: #20a0ff;}\n"
4223 ".active_draining {background: #20a0FF;}\n"
4224 ".active_no_check {background: #e0e0e0;}\n"
4225 ".backup_down {background: #ff9090;}\n"
4226 ".backup_going_up {background: #ff80ff;}\n"
4227 ".backup_going_down {background: #c060ff;}\n"
4228 ".backup_up {background: #b0d0ff;}\n"
4229 ".backup_nolb {background: #90b0e0;}\n"
4230 ".backup_draining {background: #cc9900;}\n"
4231 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004232 ".maintain {background: #c07820;}\n"
4233 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4234 "\n"
4235 "a.px:link {color: #ffff40; text-decoration: none;}"
4236 "a.px:visited {color: #ffff40; text-decoration: none;}"
4237 "a.px:hover {color: #ffffff; text-decoration: none;}"
4238 "a.lfsb:link {color: #000000; text-decoration: none;}"
4239 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4240 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4241 "\n"
4242 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4243 "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"
4244 "table.tbl td.ac { text-align: center;}\n"
4245 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4246 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4247 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4248 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4249 "\n"
4250 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4251 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4252 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004253 "table.det { border-collapse: collapse; border-style: none; }\n"
4254 "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 +01004255 "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 +01004256 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004257 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004258 " display:block;\n"
4259 " visibility:hidden;\n"
4260 " z-index:2147483647;\n"
4261 " position:absolute;\n"
4262 " padding:2px 4px 3px;\n"
4263 " background:#f0f060; color:#000000;\n"
4264 " border:1px solid #7040c0;\n"
4265 " white-space:nowrap;\n"
4266 " font-style:normal;font-size:11px;font-weight:normal;\n"
4267 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4268 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4269 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004270 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004271 "-->\n"
4272 "</style></head>\n",
4273 (uri->flags & ST_SHNODE) ? " on " : "",
4274 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4275 );
4276}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004277
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004278/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004279 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004280 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004281 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004282static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004283{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004284 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004285 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004286 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004287
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004288 /* WARNING! this has to fit the first packet too.
4289 * We are around 3.5 kB, add adding entries will
4290 * become tricky if we want to support 4kB buffers !
4291 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004292 chunk_appendf(&trash,
4293 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4294 PRODUCT_NAME "%s</a></h1>\n"
4295 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4296 "<hr width=\"100%%\" class=\"hr\">\n"
4297 "<h3>&gt; General process information</h3>\n"
4298 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4299 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4300 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4301 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4302 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4303 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4304 "Running tasks: %d/%d; idle = %d %%<br>\n"
4305 "</td><td align=\"center\" nowrap>\n"
4306 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004307 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4308 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004309 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004310 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4311 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004312 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004313 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4314 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004315 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004316 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4317 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004318 "</tr><tr>\n"
4319 "<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 -07004320 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004321 "<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 +01004322 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004323 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004324 "</td>"
4325 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4326 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4327 "",
4328 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4329 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4330 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4331 (uri->flags & ST_SHDESC) ? ": " : "",
4332 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4333 pid, relative_pid, global.nbproc,
4334 up / 86400, (up % 86400) / 3600,
4335 (up % 3600) / 60, (up % 60),
4336 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4337 global.rlimit_memmax ? " MB" : "",
4338 global.rlimit_nofile,
4339 global.maxsock, global.maxconn, global.maxpipes,
4340 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4341 run_queue_cur, nb_tasks_cur, idle_pct
4342 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004343
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004344 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004345 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 +01004346 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004347
4348 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004349 "<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 +01004350 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004351 STAT_SCOPE_TXT_MAXLEN);
4352
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004353 /* 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 +02004354 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004355 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004356 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004357 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 +01004358 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004359 }
4360
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004361 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004362 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004363 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004364 uri->uri_prefix,
4365 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004366 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004367 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004368 else
4369 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004370 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004371 uri->uri_prefix,
4372 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004373 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004374 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004375
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004376 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004377 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004378 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004379 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004380 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004381 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004382 "",
4383 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004384 else
4385 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004386 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004387 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004388 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004389 ";norefresh",
4390 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004391 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004392
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004393 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004394 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004395 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004396 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4397 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004398 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004399
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004400 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004401 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004402 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004403 (uri->refresh > 0) ? ";norefresh" : "",
4404 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004405
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004406 chunk_appendf(&trash,
4407 "</ul></td>"
4408 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4409 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4410 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4411 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4412 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4413 "</ul>"
4414 "</td>"
4415 "</tr></table>\n"
4416 ""
4417 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004418
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004419 if (appctx->ctx.stats.st_code) {
4420 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004421 case STAT_STATUS_DONE:
4422 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004423 "<p><div class=active_up>"
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 processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004426 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004427 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4428 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004429 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004430 break;
4431 case STAT_STATUS_NONE:
4432 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004433 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004434 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004435 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004436 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004437 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4438 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004439 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004440 break;
4441 case STAT_STATUS_PART:
4442 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004443 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004444 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004445 "Action partially processed.<br>"
4446 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004447 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004448 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4449 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004450 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004451 break;
4452 case STAT_STATUS_ERRP:
4453 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004454 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004455 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004456 "Action not processed because of invalid parameters."
4457 "<ul>"
4458 "<li>The action is maybe unknown.</li>"
4459 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4460 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4461 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004462 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004463 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4464 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004465 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004466 break;
4467 case STAT_STATUS_EXCD:
4468 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004469 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004470 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004471 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4472 "You should retry with less servers at a time.</b>"
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 break;
4478 case STAT_STATUS_DENY:
4479 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004480 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004481 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004482 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004483 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004484 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4485 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004486 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004487 break;
4488 default:
4489 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004490 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004491 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004492 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004493 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004494 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4495 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004496 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004497 }
4498 chunk_appendf(&trash, "<p>\n");
4499 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004500}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004501
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004502/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4503 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004504 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004505static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004506{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004507 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004508}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004509
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004510/* This function dumps statistics onto the stream interface's read buffer in
4511 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004512 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4513 * 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 +02004514 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004515 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004516 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004517static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004518{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004519 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004520 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004521 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004522
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004523 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004524
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004525 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004526 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004527 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004528 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004529
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004530 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004531 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004532 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004533 else
4534 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004535
Willy Tarreaubc18da12015-03-13 14:00:47 +01004536 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004537 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004538 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004539 }
Willy Tarreauae526782010-03-04 20:34:23 +01004540
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004541 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004542 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004543
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004544 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004545 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004546 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004547 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004548 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004549 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004550 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004551 }
Willy Tarreau91861262007-10-17 17:06:05 +02004552
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004553 appctx->ctx.stats.px = proxy;
4554 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4555 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004556 /* fall through */
4557
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004558 case STAT_ST_LIST:
4559 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004560 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004561 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004562 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004563 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004564 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004565
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004566 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004567 /* skip the disabled proxies, global frontend and non-networked ones */
4568 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004569 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004570 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004571
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004572 appctx->ctx.stats.px = px->next;
4573 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004574 }
4575 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004576
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004577 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004578 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004579
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004580 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004581 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004582 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01004583 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004584 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004585 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004586 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004587 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004588
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004589 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004590 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004591
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004592 case STAT_ST_FIN:
4593 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004594
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004595 default:
4596 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004597 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004598 return -1;
4599 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004600}
Willy Tarreauae526782010-03-04 20:34:23 +01004601
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004602/* We reached the stats page through a POST request. The appctx is
4603 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004604 * Parse the posted data and enable/disable servers if necessary.
4605 * Returns 1 if request was parsed or zero if it needs more data.
4606 */
4607static int stats_process_http_post(struct stream_interface *si)
4608{
Willy Tarreau87b09662015-04-03 00:22:06 +02004609 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004610 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004611
4612 struct proxy *px = NULL;
4613 struct server *sv = NULL;
4614
4615 char key[LINESIZE];
4616 int action = ST_ADM_ACTION_NONE;
4617 int reprocess = 0;
4618
4619 int total_servers = 0;
4620 int altered_servers = 0;
4621
4622 char *first_param, *cur_param, *next_param, *end_params;
4623 char *st_cur_param = NULL;
4624 char *st_next_param = NULL;
4625
4626 struct chunk *temp;
4627 int reql;
4628
4629 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02004630 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004631 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004632 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004633 goto out;
4634 }
4635
Willy Tarreaueee5b512015-04-03 23:46:31 +02004636 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 +01004637 if (reql <= 0) {
4638 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004639 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004640 return 0;
4641 }
4642
4643 first_param = temp->str;
4644 end_params = temp->str + reql;
4645 cur_param = next_param = end_params;
4646 *end_params = '\0';
4647
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004648 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004649
4650 /*
4651 * Parse the parameters in reverse order to only store the last value.
4652 * From the html form, the backend and the action are at the end.
4653 */
4654 while (cur_param > first_param) {
4655 char *value;
4656 int poffset, plen;
4657
4658 cur_param--;
4659
4660 if ((*cur_param == '&') || (cur_param == first_param)) {
4661 reprocess_servers:
4662 /* Parse the key */
4663 poffset = (cur_param != first_param ? 1 : 0);
4664 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4665 if ((plen > 0) && (plen <= sizeof(key))) {
4666 strncpy(key, cur_param + poffset, plen);
4667 key[plen - 1] = '\0';
4668 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004669 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004670 goto out;
4671 }
4672
4673 /* Parse the value */
4674 value = key;
4675 while (*value != '\0' && *value != '=') {
4676 value++;
4677 }
4678 if (*value == '=') {
4679 /* Ok, a value is found, we can mark the end of the key */
4680 *value++ = '\0';
4681 }
4682 if (url_decode(key) < 0 || url_decode(value) < 0)
4683 break;
4684
4685 /* Now we can check the key to see what to do */
4686 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02004687 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004688 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004689 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004690 goto out;
4691 }
4692 }
4693 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004694 if (strcmp(value, "ready") == 0) {
4695 action = ST_ADM_ACTION_READY;
4696 }
4697 else if (strcmp(value, "drain") == 0) {
4698 action = ST_ADM_ACTION_DRAIN;
4699 }
4700 else if (strcmp(value, "maint") == 0) {
4701 action = ST_ADM_ACTION_MAINT;
4702 }
4703 else if (strcmp(value, "shutdown") == 0) {
4704 action = ST_ADM_ACTION_SHUTDOWN;
4705 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004706 else if (strcmp(value, "dhlth") == 0) {
4707 action = ST_ADM_ACTION_DHLTH;
4708 }
4709 else if (strcmp(value, "ehlth") == 0) {
4710 action = ST_ADM_ACTION_EHLTH;
4711 }
4712 else if (strcmp(value, "hrunn") == 0) {
4713 action = ST_ADM_ACTION_HRUNN;
4714 }
4715 else if (strcmp(value, "hnolb") == 0) {
4716 action = ST_ADM_ACTION_HNOLB;
4717 }
4718 else if (strcmp(value, "hdown") == 0) {
4719 action = ST_ADM_ACTION_HDOWN;
4720 }
4721 else if (strcmp(value, "dagent") == 0) {
4722 action = ST_ADM_ACTION_DAGENT;
4723 }
4724 else if (strcmp(value, "eagent") == 0) {
4725 action = ST_ADM_ACTION_EAGENT;
4726 }
4727 else if (strcmp(value, "arunn") == 0) {
4728 action = ST_ADM_ACTION_ARUNN;
4729 }
4730 else if (strcmp(value, "adown") == 0) {
4731 action = ST_ADM_ACTION_ADOWN;
4732 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004733 /* else these are the old supported methods */
4734 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004735 action = ST_ADM_ACTION_DISABLE;
4736 }
4737 else if (strcmp(value, "enable") == 0) {
4738 action = ST_ADM_ACTION_ENABLE;
4739 }
4740 else if (strcmp(value, "stop") == 0) {
4741 action = ST_ADM_ACTION_STOP;
4742 }
4743 else if (strcmp(value, "start") == 0) {
4744 action = ST_ADM_ACTION_START;
4745 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004746 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004747 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004748 goto out;
4749 }
4750 }
4751 else if (strcmp(key, "s") == 0) {
4752 if (!(px && action)) {
4753 /*
4754 * Indicates that we'll need to reprocess the parameters
4755 * as soon as backend and action are known
4756 */
4757 if (!reprocess) {
4758 st_cur_param = cur_param;
4759 st_next_param = next_param;
4760 }
4761 reprocess = 1;
4762 }
4763 else if ((sv = findserver(px, value)) != NULL) {
4764 switch (action) {
4765 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004766 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004767 altered_servers++;
4768 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004769 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004770 }
4771 break;
4772 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004773 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004774 altered_servers++;
4775 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004776 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004777 }
4778 break;
4779 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004780 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4781 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4782 altered_servers++;
4783 total_servers++;
4784 }
4785 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004786 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004787 if (sv->admin & SRV_ADMF_FDRAIN) {
4788 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4789 altered_servers++;
4790 total_servers++;
4791 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004792 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004793 case ST_ADM_ACTION_DHLTH:
4794 if (sv->check.state & CHK_ST_CONFIGURED) {
4795 sv->check.state &= ~CHK_ST_ENABLED;
4796 altered_servers++;
4797 total_servers++;
4798 }
4799 break;
4800 case ST_ADM_ACTION_EHLTH:
4801 if (sv->check.state & CHK_ST_CONFIGURED) {
4802 sv->check.state |= CHK_ST_ENABLED;
4803 altered_servers++;
4804 total_servers++;
4805 }
4806 break;
4807 case ST_ADM_ACTION_HRUNN:
4808 if (!(sv->track)) {
4809 sv->check.health = sv->check.rise + sv->check.fall - 1;
4810 srv_set_running(sv, "changed from Web interface");
4811 altered_servers++;
4812 total_servers++;
4813 }
4814 break;
4815 case ST_ADM_ACTION_HNOLB:
4816 if (!(sv->track)) {
4817 sv->check.health = sv->check.rise + sv->check.fall - 1;
4818 srv_set_stopping(sv, "changed from Web interface");
4819 altered_servers++;
4820 total_servers++;
4821 }
4822 break;
4823 case ST_ADM_ACTION_HDOWN:
4824 if (!(sv->track)) {
4825 sv->check.health = 0;
4826 srv_set_stopped(sv, "changed from Web interface");
4827 altered_servers++;
4828 total_servers++;
4829 }
4830 break;
4831 case ST_ADM_ACTION_DAGENT:
4832 if (sv->agent.state & CHK_ST_CONFIGURED) {
4833 sv->agent.state &= ~CHK_ST_ENABLED;
4834 altered_servers++;
4835 total_servers++;
4836 }
4837 break;
4838 case ST_ADM_ACTION_EAGENT:
4839 if (sv->agent.state & CHK_ST_CONFIGURED) {
4840 sv->agent.state |= CHK_ST_ENABLED;
4841 altered_servers++;
4842 total_servers++;
4843 }
4844 break;
4845 case ST_ADM_ACTION_ARUNN:
4846 if (sv->agent.state & CHK_ST_ENABLED) {
4847 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4848 srv_set_running(sv, "changed from Web interface");
4849 altered_servers++;
4850 total_servers++;
4851 }
4852 break;
4853 case ST_ADM_ACTION_ADOWN:
4854 if (sv->agent.state & CHK_ST_ENABLED) {
4855 sv->agent.health = 0;
4856 srv_set_stopped(sv, "changed from Web interface");
4857 altered_servers++;
4858 total_servers++;
4859 }
4860 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004861 case ST_ADM_ACTION_READY:
4862 srv_adm_set_ready(sv);
4863 altered_servers++;
4864 total_servers++;
4865 break;
4866 case ST_ADM_ACTION_DRAIN:
4867 srv_adm_set_drain(sv);
4868 altered_servers++;
4869 total_servers++;
4870 break;
4871 case ST_ADM_ACTION_MAINT:
4872 srv_adm_set_maint(sv);
4873 altered_servers++;
4874 total_servers++;
4875 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004876 case ST_ADM_ACTION_SHUTDOWN:
4877 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02004878 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004879
4880 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4881 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02004882 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004883
4884 altered_servers++;
4885 total_servers++;
4886 }
4887 break;
4888 }
4889 } else {
4890 /* the server name is unknown or ambiguous (duplicate names) */
4891 total_servers++;
4892 }
4893 }
4894 if (reprocess && px && action) {
4895 /* Now, we know the backend and the action chosen by the user.
4896 * We can safely restart from the first server parameter
4897 * to reprocess them
4898 */
4899 cur_param = st_cur_param;
4900 next_param = st_next_param;
4901 reprocess = 0;
4902 goto reprocess_servers;
4903 }
4904
4905 next_param = cur_param;
4906 }
4907 }
4908
4909 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004910 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004911 }
4912 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004913 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004914 }
4915 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004916 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004917 }
4918 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004919 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004920 }
4921 out:
4922 return 1;
4923}
4924
4925
4926static int stats_send_http_headers(struct stream_interface *si)
4927{
Willy Tarreau87b09662015-04-03 00:22:06 +02004928 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004929 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004930 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004931
4932 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004933 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004934 "Cache-Control: no-cache\r\n"
4935 "Connection: close\r\n"
4936 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004937 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004938
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004939 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004940 chunk_appendf(&trash, "Refresh: %d\r\n",
4941 uri->refresh);
4942
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004943 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4944
4945 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4946 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4947 else
4948 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004949
Willy Tarreaueee5b512015-04-03 23:46:31 +02004950 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004951 s->logs.tv_request = now;
4952
Willy Tarreaubc18da12015-03-13 14:00:47 +01004953 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004954 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004955 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004956 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004957
4958 return 1;
4959}
4960
4961static int stats_send_http_redirect(struct stream_interface *si)
4962{
4963 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02004964 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004965 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004966 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004967
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004968 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004969 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004970 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004971 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004972 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 +01004973 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004974 }
4975
4976 /* We don't want to land on the posted stats page because a refresh will
4977 * repost the data. We don't want this to happen on accident so we redirect
4978 * the browse to the stats page with a GET.
4979 */
4980 chunk_printf(&trash,
4981 "HTTP/1.1 303 See Other\r\n"
4982 "Cache-Control: no-cache\r\n"
4983 "Content-Type: text/plain\r\n"
4984 "Connection: close\r\n"
4985 "Location: %s;st=%s%s%s%s\r\n"
4986 "\r\n",
4987 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004988 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4989 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4990 stat_status_codes[appctx->ctx.stats.st_code]) ?
4991 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004992 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004993 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4994 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004995 scope_txt);
4996
Willy Tarreaueee5b512015-04-03 23:46:31 +02004997 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004998 s->logs.tv_request = now;
4999
Willy Tarreaubc18da12015-03-13 14:00:47 +01005000 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005001 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005002 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005003 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005004
5005 return 1;
5006}
5007
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005008/* This I/O handler runs as an applet embedded in a stream interface. It is
5009 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005010 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005011 * automatically unregisters itself once transfer is complete.
5012 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005013static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005014{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005015 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005016 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005017 struct channel *req = si_oc(si);
5018 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005019
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005020 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5021 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005022
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005023 /* check that the output is not closed */
5024 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005025 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005026
Willy Tarreau347a35d2013-11-22 17:51:09 +01005027 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005028 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005029 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005030 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005031 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005032 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005033 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005034 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005035 }
5036
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005037 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005038 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005039 unsigned int data_len;
5040 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005041 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005042
5043 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5044 /* One difficulty we're facing is that we must prevent
5045 * the input data from being automatically forwarded to
5046 * the output area. For this, we temporarily disable
5047 * forwarding on the channel.
5048 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005049 last_fwd = si_ic(si)->to_forward;
5050 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005051 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005052 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005053 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005054 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005055 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005056 }
5057 }
5058
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005059 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005060 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005061 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005062
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005063 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005064
5065 /* Now we must either adjust or remove the chunk size. This is
5066 * not easy because the chunk size might wrap at the end of the
5067 * buffer, so we pretend we have nothing in the buffer, we write
5068 * the size, then restore the buffer's contents. Note that we can
5069 * only do that because no forwarding is scheduled on the stats
5070 * applet.
5071 */
5072 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005073 si_ic(si)->total -= (last_len - prev_len);
5074 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005075
5076 if (last_len != data_len) {
5077 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005078 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005079 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005080
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005081 si_ic(si)->total += (last_len - data_len);
5082 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005083 }
5084 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005085 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005086 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005087 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005088
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005089 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005090 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005091 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005092 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005093 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005094 }
5095
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005096 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005097 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005098 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005099 }
5100
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005101 if (appctx->st0 == STAT_HTTP_DONE) {
5102 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5103 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005104 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005105 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005106 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005107 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005108 }
5109 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005110 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005111 res->flags |= CF_READ_NULL;
5112 si_shutr(si);
5113 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005114
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005115 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5116 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005117
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005118 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005119 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5120 si_shutr(si);
5121 res->flags |= CF_READ_NULL;
5122 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005123 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005124 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005125 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005126}
5127
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005128
Willy Tarreau909d5172012-11-26 03:04:41 +01005129static inline const char *get_conn_ctrl_name(const struct connection *conn)
5130{
Willy Tarreau3c728722014-01-23 13:50:42 +01005131 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005132 return "NONE";
5133 return conn->ctrl->name;
5134}
5135
5136static inline const char *get_conn_xprt_name(const struct connection *conn)
5137{
5138 static char ptr[17];
5139
Willy Tarreauaad69382014-01-23 14:21:42 +01005140 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005141 return "NONE";
5142
5143 if (conn->xprt == &raw_sock)
5144 return "RAW";
5145
5146#ifdef USE_OPENSSL
5147 if (conn->xprt == &ssl_sock)
5148 return "SSL";
5149#endif
5150 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5151 return ptr;
5152}
5153
5154static inline const char *get_conn_data_name(const struct connection *conn)
5155{
5156 static char ptr[17];
5157
5158 if (!conn->data)
5159 return "NONE";
5160
5161 if (conn->data == &sess_conn_cb)
5162 return "SESS";
5163
5164 if (conn->data == &si_conn_cb)
5165 return "STRM";
5166
5167 if (conn->data == &check_conn_cb)
5168 return "CHCK";
5169
5170 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5171 return ptr;
5172}
5173
Willy Tarreau87b09662015-04-03 00:22:06 +02005174/* This function dumps a complete stream state onto the stream interface's
5175 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005176 * 0 if the output buffer is full and it needs to be called again, otherwise
5177 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005178 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005179static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005180{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005181 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005182 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005183 extern const char *monthname[12];
5184 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005185 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005186 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005187
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005188 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005189
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005190 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005191 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005192 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005193 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005194 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005195 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005196 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005197 appctx->ctx.sess.uid = 0;
5198 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005199 return 1;
5200 }
5201
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005202 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005203 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005204 appctx->ctx.sess.uid = sess->uniq_id;
5205 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005206 /* fall through */
5207
5208 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005209 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005210 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005211 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005212 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005213 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5214 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005215 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005216 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005217
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005218 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005219 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005220 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005221 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005222 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005223 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005224 break;
5225 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005226 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005227 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005228 default:
5229 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005230 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005231 break;
5232 }
5233
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005234 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005235 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005236 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005237
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005238 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005239 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005240 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5241 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5242 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005243
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005244 if (conn)
5245 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005246
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005247 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005248 case AF_INET:
5249 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005250 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005251 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005252 break;
5253 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005254 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005255 break;
5256 default:
5257 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005258 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005259 break;
5260 }
5261
Willy Tarreau50943332011-09-02 17:33:05 +02005262 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005263 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005264 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005265 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005266 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5267 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005268 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005269
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005270 conn = objt_conn(sess->si[1].end);
5271 if (conn)
5272 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005273
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005274 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005275 case AF_INET:
5276 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005277 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005278 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005279 break;
5280 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005281 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005282 break;
5283 default:
5284 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005285 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005286 break;
5287 }
5288
5289 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005290 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005291 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005292 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5293 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005294 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005295 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005296
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005297 if (conn)
5298 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005299
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005300 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005301 case AF_INET:
5302 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005303 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005304 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005305 break;
5306 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005307 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005308 break;
5309 default:
5310 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005311 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005312 break;
5313 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005314
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005315 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005316 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005317 sess->task,
5318 sess->task->state,
5319 sess->task->nice, sess->task->calls,
5320 sess->task->expire ?
5321 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5322 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5323 TICKS_TO_MS(1000)) : "<NEVER>",
5324 task_in_rq(sess->task) ? ", running" : "");
5325
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005326 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005327 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005328 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5329
Willy Tarreaueee5b512015-04-03 23:46:31 +02005330 if (sess->txn)
5331 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005332 " 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 +02005333 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5334 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 +01005335
5336 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005337 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005338 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005339 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005340 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005341 obj_type_name(sess->si[0].end),
5342 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005343 sess->si[0].exp ?
5344 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5345 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5346 TICKS_TO_MS(1000)) : "<NEVER>",
5347 sess->si[0].err_type);
5348
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005349 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005350 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005351 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005352 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005353 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005354 obj_type_name(sess->si[1].end),
5355 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005356 sess->si[1].exp ?
5357 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5358 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5359 TICKS_TO_MS(1000)) : "<NEVER>",
5360 sess->si[1].err_type);
5361
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005362 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005363 chunk_appendf(&trash,
5364 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005365 conn,
5366 get_conn_ctrl_name(conn),
5367 get_conn_xprt_name(conn),
5368 get_conn_data_name(conn),
5369 obj_type_name(conn->target),
5370 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005371
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005372 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005373 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005374 conn->flags,
5375 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005376 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005377 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005378 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005379 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005380 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5381 chunk_appendf(&trash,
5382 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5383 tmpctx,
5384 tmpctx->st0,
5385 tmpctx->st1,
5386 tmpctx->st2,
5387 tmpctx->applet->name);
5388 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005389
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005390 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005391 chunk_appendf(&trash,
5392 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005393 conn,
5394 get_conn_ctrl_name(conn),
5395 get_conn_xprt_name(conn),
5396 get_conn_data_name(conn),
5397 obj_type_name(conn->target),
5398 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005399
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005400 chunk_appendf(&trash,
5401 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005402 conn->flags,
5403 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005404 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005405 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005406 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005407 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005408 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5409 chunk_appendf(&trash,
5410 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5411 tmpctx,
5412 tmpctx->st0,
5413 tmpctx->st1,
5414 tmpctx->st2,
5415 tmpctx->applet->name);
5416 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005417
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005418 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005419 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005420 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005421 &sess->req,
5422 sess->req.flags, sess->req.analysers,
5423 sess->req.pipe ? sess->req.pipe->data : 0,
5424 sess->req.to_forward, sess->req.total,
5425 sess->req.analyse_exp ?
5426 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005427 TICKS_TO_MS(1000)) : "<NEVER>");
5428
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005429 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005430 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005431 sess->req.rex ?
5432 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005433 TICKS_TO_MS(1000)) : "<NEVER>");
5434
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005435 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005436 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005437 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005438 sess->req.wex ?
5439 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005440 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005441 sess->req.buf,
5442 sess->req.buf->data, sess->req.buf->o,
5443 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005444 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005445 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005446
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005447 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005448 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005449 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005450 &sess->res,
5451 sess->res.flags, sess->res.analysers,
5452 sess->res.pipe ? sess->res.pipe->data : 0,
5453 sess->res.to_forward, sess->res.total,
5454 sess->res.analyse_exp ?
5455 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005456 TICKS_TO_MS(1000)) : "<NEVER>");
5457
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005458 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005459 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005460 sess->res.rex ?
5461 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005462 TICKS_TO_MS(1000)) : "<NEVER>");
5463
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005464 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005465 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005466 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005467 sess->res.wex ?
5468 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005469 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005470 sess->res.buf,
5471 sess->res.buf->data, sess->res.buf->o,
5472 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005473 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005474 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005475
Willy Tarreaubc18da12015-03-13 14:00:47 +01005476 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005477 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005478 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005479 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005480
5481 /* use other states to dump the contents */
5482 }
5483 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005484 appctx->ctx.sess.uid = 0;
5485 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005486 return 1;
5487}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005488
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005489#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5490static int stats_tlskeys_list(struct stream_interface *si) {
5491 struct appctx *appctx = __objt_appctx(si->end);
5492
5493 switch (appctx->st2) {
5494 case STAT_ST_INIT:
5495 /* Display the column headers. If the message cannot be sent,
5496 * quit the fucntion with returning 0. The function is called
5497 * later and restart at the state "STAT_ST_INIT".
5498 */
5499 chunk_reset(&trash);
5500 chunk_appendf(&trash, "# id (file)\n");
5501 if (bi_putchk(si_ic(si), &trash) == -1) {
5502 si_applet_cant_put(si);
5503 return 0;
5504 }
5505
5506 /* Now, we start the browsing of the references lists.
5507 * Note that the following call to LIST_ELEM return bad pointer. The only
5508 * avalaible field of this pointer is <list>. It is used with the function
5509 * tlskeys_list_get_next() for retruning the first avalaible entry
5510 */
5511 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
5512 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5513
5514 appctx->st2 = STAT_ST_LIST;
5515 /* fall through */
5516
5517 case STAT_ST_LIST:
5518 while (appctx->ctx.tlskeys.ref) {
5519 chunk_reset(&trash);
5520
5521 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
5522 appctx->ctx.tlskeys.ref->filename);
5523
5524 if (bi_putchk(si_ic(si), &trash) == -1) {
5525 /* let's try again later from this stream. We add ourselves into
5526 * this stream's users so that it can remove us upon termination.
5527 */
5528 si_applet_cant_put(si);
5529 return 0;
5530 }
5531
5532 /* get next list entry and check the end of the list */
5533 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5534 }
5535
5536 appctx->st2 = STAT_ST_FIN;
5537 /* fall through */
5538
5539 default:
5540 appctx->st2 = STAT_ST_FIN;
5541 return 1;
5542 }
5543 return 0;
5544}
5545#endif
5546
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005547static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005548{
5549 struct appctx *appctx = __objt_appctx(si->end);
5550
5551 switch (appctx->st2) {
5552 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005553 /* Display the column headers. If the message cannot be sent,
5554 * quit the fucntion with returning 0. The function is called
5555 * later and restart at the state "STAT_ST_INIT".
5556 */
5557 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005558 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005559 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005560 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005561 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005562 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005563
5564 /* Now, we start the browsing of the references lists.
5565 * Note that the following call to LIST_ELEM return bad pointer. The only
5566 * avalaible field of this pointer is <list>. It is used with the function
5567 * pat_list_get_next() for retruning the first avalaible entry
5568 */
5569 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5570 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5571 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005572 appctx->st2 = STAT_ST_LIST;
5573 /* fall through */
5574
5575 case STAT_ST_LIST:
5576 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005577 chunk_reset(&trash);
5578
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005579 /* Build messages. If the reference is used by another category than
5580 * the listed categorie, display the information in the massage.
5581 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005582 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005583 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5584 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005585
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005586 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005587 /* let's try again later from this stream. We add ourselves into
5588 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005589 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005590 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005591 return 0;
5592 }
5593
5594 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005595 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5596 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005597 }
5598
5599 appctx->st2 = STAT_ST_FIN;
5600 /* fall through */
5601
5602 default:
5603 appctx->st2 = STAT_ST_FIN;
5604 return 1;
5605 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005606 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005607}
5608
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005609static int stats_map_lookup(struct stream_interface *si)
5610{
5611 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005612 struct sample sample;
5613 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005614 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005615
5616 switch (appctx->st2) {
5617 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005618 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005619 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005620 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005621 appctx->st2 = STAT_ST_LIST;
5622 /* fall through */
5623
5624 case STAT_ST_LIST:
5625 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005626 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005627 /* initialise chunk to build new message */
5628 chunk_reset(&trash);
5629
5630 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005631 sample.data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005632 sample.flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005633 sample.data.u.str.len = appctx->ctx.map.chunk.len;
5634 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005635 if (appctx->ctx.map.expr->pat_head->match &&
5636 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005637 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5638 else
5639 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005640
5641 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005642 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5643 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5644 break;
5645 if (match_method >= PAT_MATCH_NUM)
5646 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5647 else
5648 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005649
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005650 /* case sensitive */
5651 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5652 chunk_appendf(&trash, ", case=insensitive");
5653 else
5654 chunk_appendf(&trash, ", case=sensitive");
5655
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005656 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005657 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005658 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5659 chunk_appendf(&trash, ", found=no");
5660 else
5661 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005662 }
5663
5664 /* Display match and match info */
5665 else {
5666 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005667 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5668 chunk_appendf(&trash, ", found=yes");
5669 else
5670 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005671
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005672 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005673 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005674 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005675 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005676 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005677
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005678 /* display pattern */
5679 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5680 if (pat->ref && pat->ref->pattern)
5681 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5682 else
5683 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005684 }
5685 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005686 if (pat->ref && pat->ref->pattern)
5687 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5688 else
5689 chunk_appendf(&trash, ", pattern=unknown");
5690 }
5691
5692 /* display return value */
5693 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02005694 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02005695 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02005696 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005697 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005698 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005699 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005700 }
5701
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005702 chunk_appendf(&trash, "\n");
5703
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005704 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005705 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005706 /* let's try again later from this stream. We add ourselves into
5707 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005708 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005709 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005710 return 0;
5711 }
5712
5713 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005714 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5715 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005716 }
5717
5718 appctx->st2 = STAT_ST_FIN;
5719 /* fall through */
5720
5721 default:
5722 appctx->st2 = STAT_ST_FIN;
5723 free(appctx->ctx.map.chunk.str);
5724 return 1;
5725 }
5726}
5727
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005728static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005729{
5730 struct appctx *appctx = __objt_appctx(si->end);
5731
5732 switch (appctx->st2) {
5733
5734 case STAT_ST_INIT:
5735 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005736 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5737 struct pat_ref_elt *, list);
5738 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5739 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005740 appctx->st2 = STAT_ST_LIST;
5741 /* fall through */
5742
5743 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005744 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005745 chunk_reset(&trash);
5746
5747 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005748 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005749 chunk_appendf(&trash, "%p %s %s\n",
5750 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5751 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005752 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005753 chunk_appendf(&trash, "%p %s\n",
5754 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005755
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005756 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005757 /* let's try again later from this stream. We add ourselves into
5758 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005759 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005760 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005761 return 0;
5762 }
5763
5764 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005765 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5766 struct pat_ref_elt *, list);
5767 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005768 break;
5769 }
5770
5771 appctx->st2 = STAT_ST_FIN;
5772 /* fall through */
5773
5774 default:
5775 appctx->st2 = STAT_ST_FIN;
5776 return 1;
5777 }
5778}
5779
Willy Tarreau87b09662015-04-03 00:22:06 +02005780/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005781 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005782 * to be called again, otherwise non-zero. It is designed to be called
5783 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005784 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005785static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005786{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005787 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005788 struct connection *conn;
5789
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005790 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005791 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02005792 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005793 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005794 if (appctx->st2 == STAT_ST_LIST) {
5795 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5796 LIST_DEL(&appctx->ctx.sess.bref.users);
5797 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005798 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005799 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005800 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005801 }
5802
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005803 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005804
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005805 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005806 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005807 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02005808 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005809 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02005810 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005811 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02005812 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005813 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005814 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02005815 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005816 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005817 /* fall through */
5818
Willy Tarreau295a8372011-03-10 11:25:07 +01005819 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02005820 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005821 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5822 LIST_DEL(&appctx->ctx.sess.bref.users);
5823 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005824 }
5825
5826 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02005827 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005828 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005829 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005830
Willy Tarreau87b09662015-04-03 00:22:06 +02005831 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005832
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005833 if (appctx->ctx.sess.target) {
5834 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005835 goto next_sess;
5836
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005837 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005838 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005839 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005840 return 0;
5841
Willy Tarreau87b09662015-04-03 00:22:06 +02005842 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005843 LIST_DEL(&appctx->ctx.sess.bref.users);
5844 LIST_INIT(&appctx->ctx.sess.bref.users);
5845 if (appctx->ctx.sess.target != (void *)-1) {
5846 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005847 break;
5848 }
5849 else
5850 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005851 }
5852
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005853 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005854 "%p: proto=%s",
5855 curr_sess,
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005856 strm_li(curr_sess)->proto->name);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005857
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005858
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005859 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005860 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005861 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005862 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005863 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005864 " src=%s:%d fe=%s be=%s srv=%s",
5865 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005866 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005867 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005868 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005869 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005870 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005871 break;
5872 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005873 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005874 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005875 strm_li(curr_sess)->luid,
5876 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005877 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005878 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005879 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005880 break;
5881 }
5882
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005883 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005884 " ts=%02x age=%s calls=%d",
5885 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005886 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5887 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005888
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005889 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005890 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005891 curr_sess->req.flags,
5892 curr_sess->req.buf->i,
5893 curr_sess->req.analysers,
5894 curr_sess->req.rex ?
5895 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005896 TICKS_TO_MS(1000)) : "");
5897
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005898 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005899 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005900 curr_sess->req.wex ?
5901 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005902 TICKS_TO_MS(1000)) : "");
5903
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005904 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005905 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005906 curr_sess->req.analyse_exp ?
5907 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005908 TICKS_TO_MS(1000)) : "");
5909
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005910 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005911 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005912 curr_sess->res.flags,
5913 curr_sess->res.buf->i,
5914 curr_sess->res.analysers,
5915 curr_sess->res.rex ?
5916 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005917 TICKS_TO_MS(1000)) : "");
5918
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005919 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005920 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005921 curr_sess->res.wex ?
5922 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005923 TICKS_TO_MS(1000)) : "");
5924
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005925 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005926 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005927 curr_sess->res.analyse_exp ?
5928 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005929 TICKS_TO_MS(1000)) : "");
5930
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005931 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005932 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005933 " s0=[%d,%1xh,fd=%d,ex=%s]",
5934 curr_sess->si[0].state,
5935 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005936 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005937 curr_sess->si[0].exp ?
5938 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5939 TICKS_TO_MS(1000)) : "");
5940
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005941 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005942 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005943 " s1=[%d,%1xh,fd=%d,ex=%s]",
5944 curr_sess->si[1].state,
5945 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005946 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005947 curr_sess->si[1].exp ?
5948 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5949 TICKS_TO_MS(1000)) : "");
5950
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005951 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005952 " exp=%s",
5953 curr_sess->task->expire ?
5954 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5955 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005956 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005957 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005958
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005959 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005960
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005961 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005962 /* let's try again later from this stream. We add ourselves into
5963 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005964 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005965 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005966 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005967 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005968 }
5969
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005970 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005971 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005972 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005973
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005974 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005975 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005976 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005977 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005978 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005979 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005980
Willy Tarreaubc18da12015-03-13 14:00:47 +01005981 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005982 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005983 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005984 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005985
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005986 appctx->ctx.sess.target = NULL;
5987 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005988 return 1;
5989 }
5990
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005991 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005992 /* fall through */
5993
5994 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005995 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005996 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005997 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005998}
5999
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006000/* This is called when the stream interface is closed. For instance, upon an
6001 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006002 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006003 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006004static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006005{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006006 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6007 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6008 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006009 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006010 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6011 free(appctx->ctx.cli.err);
6012 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006013 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6014 free(appctx->ctx.map.chunk.str);
6015 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006016}
6017
Willy Tarreau20e99322013-04-13 09:22:25 +02006018/* This function is used to either dump tables states (when action is set
6019 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006020 * It returns 0 if the output buffer is full and it needs to be called
6021 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006022 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006023static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006024{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006025 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006026 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006027 struct ebmb_node *eb;
6028 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006029 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006030 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006031
6032 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006033 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006034 * - STAT_ST_INIT : the first call
6035 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006036 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006037 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006038 * and the entry pointer points to the next entry to be dumped,
6039 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006040 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006041 * data though.
6042 */
6043
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006044 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006045 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006046 if (appctx->st2 == STAT_ST_LIST) {
6047 appctx->ctx.table.entry->ref_cnt--;
6048 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006049 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006050 return 1;
6051 }
6052
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006053 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006054
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006055 while (appctx->st2 != STAT_ST_FIN) {
6056 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006057 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006058 appctx->ctx.table.proxy = appctx->ctx.table.target;
6059 if (!appctx->ctx.table.proxy)
6060 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006061
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006062 appctx->ctx.table.entry = NULL;
6063 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006064 break;
6065
Willy Tarreau295a8372011-03-10 11:25:07 +01006066 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006067 if (!appctx->ctx.table.proxy ||
6068 (appctx->ctx.table.target &&
6069 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6070 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006071 break;
6072 }
6073
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006074 if (appctx->ctx.table.proxy->table.size) {
6075 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6076 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006077 return 0;
6078
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006079 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006080 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006081 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006082 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006083 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006084 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6085 appctx->ctx.table.entry->ref_cnt++;
6086 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006087 break;
6088 }
6089 }
6090 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006091 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006092 break;
6093
Willy Tarreau295a8372011-03-10 11:25:07 +01006094 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006095 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006096
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006097 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006098 /* we're filtering on some data contents */
6099 void *ptr;
6100 long long data;
6101
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006102 dt = appctx->ctx.table.data_type;
6103 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6104 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006105 dt);
6106
6107 data = 0;
6108 switch (stktable_data_types[dt].std_type) {
6109 case STD_T_SINT:
6110 data = stktable_data_cast(ptr, std_t_sint);
6111 break;
6112 case STD_T_UINT:
6113 data = stktable_data_cast(ptr, std_t_uint);
6114 break;
6115 case STD_T_ULL:
6116 data = stktable_data_cast(ptr, std_t_ull);
6117 break;
6118 case STD_T_FRQP:
6119 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006120 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006121 break;
6122 }
6123
6124 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006125 if ((data < appctx->ctx.table.value &&
6126 (appctx->ctx.table.data_op == STD_OP_EQ ||
6127 appctx->ctx.table.data_op == STD_OP_GT ||
6128 appctx->ctx.table.data_op == STD_OP_GE)) ||
6129 (data == appctx->ctx.table.value &&
6130 (appctx->ctx.table.data_op == STD_OP_NE ||
6131 appctx->ctx.table.data_op == STD_OP_GT ||
6132 appctx->ctx.table.data_op == STD_OP_LT)) ||
6133 (data > appctx->ctx.table.value &&
6134 (appctx->ctx.table.data_op == STD_OP_EQ ||
6135 appctx->ctx.table.data_op == STD_OP_LT ||
6136 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006137 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006138 }
6139
Simon Hormanc88b8872011-06-15 15:18:49 +09006140 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006141 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6142 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006143 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006144
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006145 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006146
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006147 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006148 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006149 struct stksess *old = appctx->ctx.table.entry;
6150 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006151 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006152 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6153 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6154 stksess_kill(&appctx->ctx.table.proxy->table, old);
6155 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006156 break;
6157 }
6158
Simon Hormanc88b8872011-06-15 15:18:49 +09006159
6160 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006161 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6162 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6163 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006164
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006165 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6166 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006167 break;
6168
Willy Tarreau295a8372011-03-10 11:25:07 +01006169 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006170 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006171 break;
6172 }
6173 }
6174 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006175}
6176
Willy Tarreaud426a182010-03-05 14:58:26 +01006177/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006178 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6179 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6180 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6181 * lines are respected within the limit of 70 output chars. Lines that are
6182 * continuation of a previous truncated line begin with "+" instead of " "
6183 * after the offset. The new pointer is returned.
6184 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006185static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6186 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006187{
6188 int end;
6189 unsigned char c;
6190
6191 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006192 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006193 return ptr;
6194
Willy Tarreau77804732012-10-29 16:14:26 +01006195 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006196
Willy Tarreaud426a182010-03-05 14:58:26 +01006197 while (ptr < len && ptr < bsize) {
6198 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006199 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006200 if (out->len > end - 2)
6201 break;
6202 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006203 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006204 if (out->len > end - 3)
6205 break;
6206 out->str[out->len++] = '\\';
6207 switch (c) {
6208 case '\t': c = 't'; break;
6209 case '\n': c = 'n'; break;
6210 case '\r': c = 'r'; break;
6211 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006212 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006213 }
6214 out->str[out->len++] = c;
6215 } else {
6216 if (out->len > end - 5)
6217 break;
6218 out->str[out->len++] = '\\';
6219 out->str[out->len++] = 'x';
6220 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6221 out->str[out->len++] = hextab[c & 0xF];
6222 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006223 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006224 /* we had a line break, let's return now */
6225 out->str[out->len++] = '\n';
6226 *line = ptr;
6227 return ptr;
6228 }
6229 }
6230 /* we have an incomplete line, we return it as-is */
6231 out->str[out->len++] = '\n';
6232 return ptr;
6233}
6234
Baptiste Assmann3863f972015-05-17 00:33:24 +02006235/* This function dumps counters from all resolvers section and associated name servers.
6236 * It returns 0 if the output buffer is full and it needs
6237 * to be called again, otherwise non-zero.
6238 */
6239static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6240{
6241 struct appctx *appctx = __objt_appctx(si->end);
6242 struct dns_resolvers *presolvers;
6243 struct dns_nameserver *pnameserver;
6244
6245 chunk_reset(&trash);
6246
6247 switch (appctx->st2) {
6248 case STAT_ST_INIT:
6249 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6250 /* fall through */
6251
6252 case STAT_ST_LIST:
6253 presolvers = appctx->ctx.resolvers.ptr;
6254 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6255 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6256 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6257 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6258 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6259 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6260 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6261 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6262 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6263 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6264 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6265 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6266 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6267 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6268 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
Baptiste Assmann6cdea932015-09-02 21:56:05 +02006269 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
Baptiste Assmann3863f972015-05-17 00:33:24 +02006270 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6271 }
6272
6273 /* display response */
6274 if (bi_putchk(si_ic(si), &trash) == -1) {
6275 /* let's try again later from this session. We add ourselves into
6276 * this session's users so that it can remove us upon termination.
6277 */
6278 si->flags |= SI_FL_WAIT_ROOM;
6279 return 0;
6280 }
6281
6282 appctx->st2 = STAT_ST_FIN;
6283 /* fall through */
6284
6285 default:
6286 appctx->st2 = STAT_ST_FIN;
6287 return 1;
6288 }
6289}
6290
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006291/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006292 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006293 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006294 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006295static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006296{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006297 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006298 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006299
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006300 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006301 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006302
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006303 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006304
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006305 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006306 /* the function had not been called yet, let's prepare the
6307 * buffer for a response.
6308 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006309 struct tm tm;
6310
6311 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006312 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006313 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6314 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6315 error_snapshot_id);
6316
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006317 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006318 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006319 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006320 return 0;
6321 }
6322
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006323 appctx->ctx.errors.px = proxy;
6324 appctx->ctx.errors.buf = 0;
6325 appctx->ctx.errors.bol = 0;
6326 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006327 }
6328
6329 /* we have two inner loops here, one for the proxy, the other one for
6330 * the buffer.
6331 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006332 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006333 struct error_snapshot *es;
6334
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006335 if (appctx->ctx.errors.buf == 0)
6336 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006337 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006338 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006339
6340 if (!es->when.tv_sec)
6341 goto next;
6342
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006343 if (appctx->ctx.errors.iid >= 0 &&
6344 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6345 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006346 goto next;
6347
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006348 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006349 /* just print headers now */
6350
6351 char pn[INET6_ADDRSTRLEN];
6352 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006353 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006354
6355 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006356 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006357 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006358 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006359
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006360 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6361 case AF_INET:
6362 case AF_INET6:
6363 port = get_host_port(&es->src);
6364 break;
6365 default:
6366 port = 0;
6367 }
6368
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006369 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006370 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006371 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006372 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006373 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006374 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006375 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6376 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006377 break;
6378 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006379 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006380 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006381 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006382 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006383 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006384 break;
6385 }
6386
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006387 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006388 ", server %s (#%d), event #%u\n"
6389 " src %s:%d, session #%d, session flags 0x%08x\n"
6390 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6391 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6392 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6393 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6394 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6395 es->ev_id,
6396 pn, port, es->sid, es->s_flags,
6397 es->state, es->m_flags, es->t_flags,
6398 es->m_clen, es->m_blen,
6399 es->b_flags, es->b_out, es->b_tot,
6400 es->len, es->b_wrap, es->pos);
6401
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006402 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006403 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006404 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006405 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006406 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006407 appctx->ctx.errors.ptr = 0;
6408 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006409 }
6410
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006411 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006412 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006413 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006414 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006415 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006416 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006417 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006418 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006419 goto next;
6420 }
6421
6422 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006423 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006424 int newptr;
6425 int newline;
6426
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006427 newline = appctx->ctx.errors.bol;
6428 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6429 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006430 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006431
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006432 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006433 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006434 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006435 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006436 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006437 appctx->ctx.errors.ptr = newptr;
6438 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006439 };
6440 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006441 appctx->ctx.errors.bol = 0;
6442 appctx->ctx.errors.ptr = -1;
6443 appctx->ctx.errors.buf++;
6444 if (appctx->ctx.errors.buf > 1) {
6445 appctx->ctx.errors.buf = 0;
6446 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006447 }
6448 }
6449
6450 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006451 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006452}
6453
Willy Tarreaud5781202012-09-22 19:32:35 +02006454/* parse the "level" argument on the bind lines */
6455static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6456{
6457 if (!*args[cur_arg + 1]) {
6458 memprintf(err, "'%s' : missing level", args[cur_arg]);
6459 return ERR_ALERT | ERR_FATAL;
6460 }
6461
6462 if (!strcmp(args[cur_arg+1], "user"))
6463 conf->level = ACCESS_LVL_USER;
6464 else if (!strcmp(args[cur_arg+1], "operator"))
6465 conf->level = ACCESS_LVL_OPER;
6466 else if (!strcmp(args[cur_arg+1], "admin"))
6467 conf->level = ACCESS_LVL_ADMIN;
6468 else {
6469 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6470 args[cur_arg], args[cur_arg+1]);
6471 return ERR_ALERT | ERR_FATAL;
6472 }
6473
6474 return 0;
6475}
6476
Willy Tarreau30576452015-04-13 13:50:30 +02006477struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006478 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006479 .name = "<STATS>", /* used for logging */
6480 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006481 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006482};
6483
Willy Tarreau30576452015-04-13 13:50:30 +02006484static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006485 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006486 .name = "<CLI>", /* used for logging */
6487 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006488 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006489};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006490
Willy Tarreaudc13c112013-06-21 23:16:39 +02006491static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006492 { CFG_GLOBAL, "stats", stats_parse_global },
6493 { 0, NULL, NULL },
6494}};
6495
Willy Tarreaud5781202012-09-22 19:32:35 +02006496static struct bind_kw_list bind_kws = { "STAT", { }, {
6497 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6498 { NULL, NULL, 0 },
6499}};
6500
Willy Tarreau10522fd2008-07-09 20:12:41 +02006501__attribute__((constructor))
6502static void __dumpstats_module_init(void)
6503{
6504 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006505 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006506}
6507
Willy Tarreau91861262007-10-17 17:06:05 +02006508/*
6509 * Local variables:
6510 * c-indent-level: 8
6511 * c-basic-offset: 8
6512 * End:
6513 */