blob: 39bdd17142bc3631061917a81efa62f357ebd09c [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 */
Baptiste Assmann28289462015-07-03 08:01:20 +020095 STAT_CLI_O_SERVERS_STATE, /* dump server state and changing information */
Baptiste Assmann9b6857e2015-09-18 14:49:12 +020096 STAT_CLI_O_BACKEND, /* dump backend list */
Willy Tarreau91b843d2014-01-28 16:27:17 +010097};
98
Willy Tarreaued7df902014-05-22 18:04:49 +020099/* Actions available for the stats admin forms */
100enum {
101 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +0200102
103 /* enable/disable health checks */
104 ST_ADM_ACTION_DHLTH,
105 ST_ADM_ACTION_EHLTH,
106
107 /* force health check status */
108 ST_ADM_ACTION_HRUNN,
109 ST_ADM_ACTION_HNOLB,
110 ST_ADM_ACTION_HDOWN,
111
112 /* enable/disable agent checks */
113 ST_ADM_ACTION_DAGENT,
114 ST_ADM_ACTION_EAGENT,
115
116 /* force agent check status */
117 ST_ADM_ACTION_ARUNN,
118 ST_ADM_ACTION_ADOWN,
119
120 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200121 ST_ADM_ACTION_READY,
122 ST_ADM_ACTION_DRAIN,
123 ST_ADM_ACTION_MAINT,
124 ST_ADM_ACTION_SHUTDOWN,
125 /* these are the ancient actions, still available for compatibility */
126 ST_ADM_ACTION_DISABLE,
127 ST_ADM_ACTION_ENABLE,
128 ST_ADM_ACTION_STOP,
129 ST_ADM_ACTION_START,
130};
131
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200132static int stats_dump_backend_to_buffer(struct stream_interface *si);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100133static int stats_dump_info_to_buffer(struct stream_interface *si);
Baptiste Assmann28289462015-07-03 08:01:20 +0200134static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100135static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200136static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900137static int stats_dump_sess_to_buffer(struct stream_interface *si);
138static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100139static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100140static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
141static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200142static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100143static int stats_pats_list(struct stream_interface *si);
144static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100145static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200146#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
147static int stats_tlskeys_list(struct stream_interface *si);
148#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200149static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900150
Baptiste Assmann28289462015-07-03 08:01:20 +0200151static void dump_servers_state(struct proxy *backend, struct chunk *buf);
152
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100153/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100154 * cli_io_handler()
155 * -> stats_dump_sess_to_buffer() // "show sess"
156 * -> stats_dump_errors_to_buffer() // "show errors"
157 * -> stats_dump_info_to_buffer() // "show info"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200158 * -> stats_dump_backend_to_buffer() // "show backend"
Baptiste Assmann28289462015-07-03 08:01:20 +0200159 * -> stats_dump_servers_state_to_buffer() // "show servers state [<backend name>]"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100160 * -> stats_dump_stat_to_buffer() // "show stat"
Baptiste Assmann3863f972015-05-17 00:33:24 +0200161 * -> stats_dump_resolvers_to_buffer() // "show stat resolver <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100162 * -> stats_dump_csv_header()
163 * -> stats_dump_proxy_to_buffer()
164 * -> stats_dump_fe_stats()
165 * -> stats_dump_li_stats()
166 * -> stats_dump_sv_stats()
167 * -> stats_dump_be_stats()
168 *
169 * http_stats_io_handler()
170 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
171 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
172 * -> stats_dump_html_head() // emits the HTML headers
173 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
174 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
175 * -> stats_dump_html_px_hdr()
176 * -> stats_dump_fe_stats()
177 * -> stats_dump_li_stats()
178 * -> stats_dump_sv_stats()
179 * -> stats_dump_be_stats()
180 * -> stats_dump_html_px_end()
181 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100182 */
183
Willy Tarreau30576452015-04-13 13:50:30 +0200184static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900185
186static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200187 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200188 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200189 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200190 " help : this message\n"
191 " prompt : toggle interactive mode with prompt\n"
192 " quit : disconnect\n"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200193 " show backend : list backends in the current running config\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200194 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100195 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200196 " show stat : report counters for each proxy and server\n"
197 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100198 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200199 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200200 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200201 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200202 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200203 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200204 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100205 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200206 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200207 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200208 " disable : put a server or frontend in maintenance mode\n"
209 " enable : re-enable a server or frontend which is in maintenance mode\n"
210 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100211 " show acl [id] : report avalaible acls or dump an acl's contents\n"
212 " get acl : reports the patterns matching a sample for an ACL\n"
213 " add acl : add acl entry\n"
214 " del acl : delete acl entry\n"
215 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100216 " show map [id] : report avalaible maps or dump a map's contents\n"
217 " get map : reports the keys and values matching a sample for a map\n"
218 " set map : modify map entry\n"
219 " add map : add map entry\n"
220 " del map : delete map entry\n"
221 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200222 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200223 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200224
Simon Horman9bd2c732011-06-15 15:18:44 +0900225static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200226 "Permission denied\n"
227 "";
228
Willy Tarreau295a8372011-03-10 11:25:07 +0100229/* data transmission states for the stats responses */
230enum {
231 STAT_ST_INIT = 0,
232 STAT_ST_HEAD,
233 STAT_ST_INFO,
234 STAT_ST_LIST,
235 STAT_ST_END,
236 STAT_ST_FIN,
237};
238
239/* data transmission states for the stats responses inside a proxy */
240enum {
241 STAT_PX_ST_INIT = 0,
242 STAT_PX_ST_TH,
243 STAT_PX_ST_FE,
244 STAT_PX_ST_LI,
245 STAT_PX_ST_SV,
246 STAT_PX_ST_BE,
247 STAT_PX_ST_END,
248 STAT_PX_ST_FIN,
249};
250
Cyril Bonté19979e12012-04-04 12:57:21 +0200251extern const char *stat_status_codes[];
252
Willy Tarreau07e9e642010-08-17 21:48:17 +0200253/* allocate a new stats frontend named <name>, and return it
254 * (or NULL in case of lack of memory).
255 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200256static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200257{
258 struct proxy *fe;
259
260 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
261 if (!fe)
262 return NULL;
263
Willy Tarreau237250c2011-07-29 01:49:03 +0200264 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200265 fe->next = proxy;
266 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200267 fe->last_change = now.tv_sec;
268 fe->id = strdup("GLOBAL");
269 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200270 fe->maxconn = 10; /* default to 10 concurrent connections */
271 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200272 fe->conf.file = strdup(file);
273 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100274 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100275 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200276
277 /* the stats frontend is the only one able to assign ID #0 */
278 fe->conf.id.key = fe->uuid = 0;
279 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200280 return fe;
281}
282
Willy Tarreaufbee7132007-10-18 13:53:22 +0200283/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200284 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
285 * error message into the <err> buffer which will be preallocated. The trailing
286 * '\n' must not be written. The function must be called with <args> pointing to
287 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200288 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200289static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200290 struct proxy *defpx, const char *file, int line,
291 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200292{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200293 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200294 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200295
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200296 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200297 int cur_arg;
298
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200299 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200300 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 +0200301 return -1;
302 }
303
Willy Tarreau89a63132009-08-16 17:41:45 +0200304 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200305 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200306 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200307 return -1;
308 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200309 }
310
Willy Tarreau4348fad2012-09-20 16:48:07 +0200311 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200312 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200313
Willy Tarreauc53d4222012-09-20 20:19:28 +0200314 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
315 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
316 file, line, args[0], args[1], err && *err ? *err : "error");
317 return -1;
318 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200319
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200320 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200321 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200322 static int bind_dumped;
323 struct bind_kw *kw;
324
325 kw = bind_find_kw(args[cur_arg]);
326 if (kw) {
327 if (!kw->parse) {
328 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
329 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200330 return -1;
331 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200332
333 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
334 if (err && *err)
335 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
336 else
337 memprintf(err, "'%s %s' : error encountered while processing '%s'",
338 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200339 return -1;
340 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200341
342 cur_arg += 1 + kw->skip;
343 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200344 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200345
346 if (!bind_dumped) {
347 bind_dump_kws(err);
348 indent_msg(err, 4);
349 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200350 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200351
352 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
353 args[0], args[1], args[cur_arg],
354 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
355 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200356 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100357
Willy Tarreauc53d4222012-09-20 20:19:28 +0200358 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
359 l->maxconn = global.stats_fe->maxconn;
360 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200361 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200362 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100363 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200364 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
365 l->nice = -64; /* we want to boost priority for local stats */
366 global.maxsock += l->maxconn;
367 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200368 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200369 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100370 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200371 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100372
373 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200374 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100375 return -1;
376 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200377
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100378 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200379 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200380 return -1;
381 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200382 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200383 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200384 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200385 return -1;
386 }
387 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200388 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200389 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200390 else if (!strcmp(args[1], "maxconn")) {
391 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200392
393 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200394 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200395 return -1;
396 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200397
398 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200399 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200400 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200401 return -1;
402 }
403 }
404 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200405 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200406 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
407 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100408 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200409
Willy Tarreau91319572013-04-20 09:48:50 +0200410 if (!global.stats_fe) {
411 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
412 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
413 return -1;
414 }
415 }
416
Willy Tarreau35b7b162012-10-22 23:17:18 +0200417 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100418 unsigned int low, high;
419
Willy Tarreau35b7b162012-10-22 23:17:18 +0200420 if (strcmp(args[cur_arg], "all") == 0) {
421 set = 0;
422 break;
423 }
424 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100425 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200426 }
427 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100428 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200429 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100430 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100431 char *dash = strchr(args[cur_arg], '-');
432
433 low = high = str2uic(args[cur_arg]);
434 if (dash)
435 high = str2uic(dash + 1);
436
437 if (high < low) {
438 unsigned int swap = low;
439 low = high;
440 high = swap;
441 }
442
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100443 if (low < 1 || high > LONGBITS) {
444 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
445 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200446 return -1;
447 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100448 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100449 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100450 }
451 else {
452 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100453 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
454 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100455 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200456 }
457 cur_arg++;
458 }
459 global.stats_fe->bind_proc = set;
460 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200461 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200462 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200463 return -1;
464 }
465 return 0;
466}
467
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100468/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
469 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100470 * NOTE: Some tools happen to rely on the field position instead of its name,
471 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100472 */
473static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100474{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100475 chunk_appendf(&trash,
476 "# pxname,svname,"
477 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100478 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100479 "bin,bout,"
480 "dreq,dresp,"
481 "ereq,econ,eresp,"
482 "wretr,wredis,"
483 "status,weight,act,bck,"
484 "chkfail,chkdown,lastchg,downtime,qlimit,"
485 "pid,iid,sid,throttle,lbtot,tracked,type,"
486 "rate,rate_lim,rate_max,"
487 "check_status,check_code,check_duration,"
488 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
489 "req_rate,req_rate_max,req_tot,"
490 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200491 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100492 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100493}
494
Simon Hormand9366582011-06-15 15:18:45 +0900495/* print a string of text buffer to <out>. The format is :
496 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
497 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
498 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
499 */
500static int dump_text(struct chunk *out, const char *buf, int bsize)
501{
502 unsigned char c;
503 int ptr = 0;
504
505 while (buf[ptr] && ptr < bsize) {
506 c = buf[ptr];
507 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
508 if (out->len > out->size - 1)
509 break;
510 out->str[out->len++] = c;
511 }
512 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
513 if (out->len > out->size - 2)
514 break;
515 out->str[out->len++] = '\\';
516 switch (c) {
517 case ' ': c = ' '; break;
518 case '\t': c = 't'; break;
519 case '\n': c = 'n'; break;
520 case '\r': c = 'r'; break;
521 case '\e': c = 'e'; break;
522 case '\\': c = '\\'; break;
523 }
524 out->str[out->len++] = c;
525 }
526 else {
527 if (out->len > out->size - 4)
528 break;
529 out->str[out->len++] = '\\';
530 out->str[out->len++] = 'x';
531 out->str[out->len++] = hextab[(c >> 4) & 0xF];
532 out->str[out->len++] = hextab[c & 0xF];
533 }
534 ptr++;
535 }
536
537 return ptr;
538}
539
540/* print a buffer in hexa.
541 * Print stopped if <bsize> is reached, or if no more place in the chunk.
542 */
543static int dump_binary(struct chunk *out, const char *buf, int bsize)
544{
545 unsigned char c;
546 int ptr = 0;
547
548 while (ptr < bsize) {
549 c = buf[ptr];
550
551 if (out->len > out->size - 2)
552 break;
553 out->str[out->len++] = hextab[(c >> 4) & 0xF];
554 out->str[out->len++] = hextab[c & 0xF];
555
556 ptr++;
557 }
558 return ptr;
559}
560
561/* Dump the status of a table to a stream interface's
562 * read buffer. It returns 0 if the output buffer is full
563 * and needs to be called again, otherwise non-zero.
564 */
565static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
566 struct proxy *proxy, struct proxy *target)
567{
Willy Tarreau87b09662015-04-03 00:22:06 +0200568 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900569
Willy Tarreau77804732012-10-29 16:14:26 +0100570 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900571 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
572
573 /* any other information should be dumped here */
574
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200575 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100576 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900577
Willy Tarreaubc18da12015-03-13 14:00:47 +0100578 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200579 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900580 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100581 }
Simon Hormand9366582011-06-15 15:18:45 +0900582
583 return 1;
584}
585
586/* Dump the a table entry to a stream interface's
587 * read buffer. It returns 0 if the output buffer is full
588 * and needs to be called again, otherwise non-zero.
589 */
590static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
591 struct proxy *proxy, struct stksess *entry)
592{
593 int dt;
594
Willy Tarreau77804732012-10-29 16:14:26 +0100595 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900596
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200597 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900598 char addr[INET_ADDRSTRLEN];
599 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100600 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900601 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200602 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900603 char addr[INET6_ADDRSTRLEN];
604 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100605 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900606 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200607 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100608 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900609 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200610 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100611 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900612 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
613 }
614 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100615 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900616 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
617 }
618
Willy Tarreau77804732012-10-29 16:14:26 +0100619 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900620
621 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
622 void *ptr;
623
624 if (proxy->table.data_ofs[dt] == 0)
625 continue;
626 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100627 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900628 else
Willy Tarreau77804732012-10-29 16:14:26 +0100629 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900630
631 ptr = stktable_data_ptr(&proxy->table, entry, dt);
632 switch (stktable_data_types[dt].std_type) {
633 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100634 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900635 break;
636 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100637 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900638 break;
639 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100640 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900641 break;
642 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100643 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900644 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
645 proxy->table.data_arg[dt].u));
646 break;
647 }
648 }
Willy Tarreau77804732012-10-29 16:14:26 +0100649 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900650
Willy Tarreaubc18da12015-03-13 14:00:47 +0100651 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200652 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900653 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100654 }
Simon Hormand9366582011-06-15 15:18:45 +0900655
656 return 1;
657}
658
Willy Tarreaudec98142012-06-06 23:37:08 +0200659static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900660{
Willy Tarreau87b09662015-04-03 00:22:06 +0200661 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100662 struct appctx *appctx = __objt_appctx(si->end);
663 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900664 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900665 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900666 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200667 long long value;
668 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200669 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200670 void *ptr;
671 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900672
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100673 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900674
675 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100676 appctx->ctx.cli.msg = "Key value expected\n";
677 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900678 return;
679 }
680
Simon Hormanc5b89f62011-06-15 15:18:50 +0900681 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200682 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900683 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100684 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900685 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200686 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900687 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100688 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900689 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200690 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900691 {
692 char *endptr;
693 unsigned long val;
694 errno = 0;
695 val = strtoul(args[4], &endptr, 10);
696 if ((errno == ERANGE && val == ULONG_MAX) ||
697 (errno != 0 && val == 0) || endptr == args[4] ||
698 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100699 appctx->ctx.cli.msg = "Invalid key\n";
700 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900701 return;
702 }
703 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100704 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900705 break;
706 }
707 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200708 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +0100709 static_table_key->key = args[4];
710 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900711 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900712 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200713 switch (action) {
714 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100715 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200716 break;
717 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100718 appctx->ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200719 break;
720 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100721 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200722 break;
723 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100724 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900725 return;
726 }
727
728 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200729 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100730 appctx->ctx.cli.msg = stats_permission_denied_msg;
731 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900732 return;
733 }
734
Willy Tarreau07115412012-10-29 21:56:59 +0100735 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900736
Willy Tarreaudec98142012-06-06 23:37:08 +0200737 switch (action) {
738 case STAT_CLI_O_TAB:
739 if (!ts)
740 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100741 chunk_reset(&trash);
742 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900743 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100744 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900745 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200746
747 case STAT_CLI_O_CLR:
748 if (!ts)
749 return;
750 if (ts->ref_cnt) {
751 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100752 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
753 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200754 return;
755 }
756 stksess_kill(&px->table, ts);
757 break;
Simon Horman17bce342011-06-15 15:18:47 +0900758
Willy Tarreau654694e2012-06-07 01:03:16 +0200759 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200760 if (ts)
761 stktable_touch(&px->table, ts, 1);
762 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100763 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200764 if (!ts) {
765 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100766 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
767 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200768 return;
769 }
770 stktable_store(&px->table, ts, 1);
771 }
772
Willy Tarreau47060b62013-08-01 21:11:42 +0200773 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
774 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100775 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
776 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200777 return;
778 }
779
780 data_type = stktable_get_data_type(args[cur_arg] + 5);
781 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100782 appctx->ctx.cli.msg = "Unknown data type\n";
783 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200784 return;
785 }
786
787 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100788 appctx->ctx.cli.msg = "Data type not stored in this table\n";
789 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200790 return;
791 }
792
793 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100794 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
795 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200796 return;
797 }
798
799 ptr = stktable_data_ptr(&px->table, ts, data_type);
800
801 switch (stktable_data_types[data_type].std_type) {
802 case STD_T_SINT:
803 stktable_data_cast(ptr, std_t_sint) = value;
804 break;
805 case STD_T_UINT:
806 stktable_data_cast(ptr, std_t_uint) = value;
807 break;
808 case STD_T_ULL:
809 stktable_data_cast(ptr, std_t_ull) = value;
810 break;
811 case STD_T_FRQP:
812 /* We set both the current and previous values. That way
813 * the reported frequency is stable during all the period
814 * then slowly fades out. This allows external tools to
815 * push measures without having to update them too often.
816 */
817 frqp = &stktable_data_cast(ptr, std_t_frqp);
818 frqp->curr_tick = now_ms;
819 frqp->prev_ctr = 0;
820 frqp->curr_ctr = value;
821 break;
822 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200823 }
824 break;
825
Willy Tarreaudec98142012-06-06 23:37:08 +0200826 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100827 appctx->ctx.cli.msg = "Unknown action\n";
828 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200829 break;
Simon Horman121f3052011-06-15 15:18:46 +0900830 }
Simon Horman121f3052011-06-15 15:18:46 +0900831}
832
Willy Tarreau654694e2012-06-07 01:03:16 +0200833static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900834{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100835 struct appctx *appctx = __objt_appctx(si->end);
836
Willy Tarreau04b3a192013-04-13 09:41:37 +0200837 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100838 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
839 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200840 return;
841 }
842
Simon Hormand5b9fd92011-06-15 15:18:48 +0900843 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100844 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
845 if (appctx->ctx.table.data_type < 0) {
846 appctx->ctx.cli.msg = "Unknown data type\n";
847 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900848 return;
849 }
850
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100851 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
852 appctx->ctx.cli.msg = "Data type not stored in this table\n";
853 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900854 return;
855 }
856
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100857 appctx->ctx.table.data_op = get_std_op(args[4]);
858 if (appctx->ctx.table.data_op < 0) {
859 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
860 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900861 return;
862 }
863
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100864 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
865 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
866 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900867 return;
868 }
869}
870
Willy Tarreaudec98142012-06-06 23:37:08 +0200871static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900872{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100873 struct appctx *appctx = __objt_appctx(si->end);
874
875 appctx->ctx.table.data_type = -1;
876 appctx->st2 = STAT_ST_INIT;
877 appctx->ctx.table.target = NULL;
878 appctx->ctx.table.proxy = NULL;
879 appctx->ctx.table.entry = NULL;
880 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900881
882 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200883 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100884 if (!appctx->ctx.table.target) {
885 appctx->ctx.cli.msg = "No such table\n";
886 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900887 return;
888 }
889 }
890 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200891 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900892 goto err_args;
893 return;
894 }
895
896 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200897 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900898 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200899 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900900 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900901 goto err_args;
902
903 return;
904
905err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200906 switch (action) {
907 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100908 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200909 break;
910 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100911 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200912 break;
913 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100914 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200915 break;
916 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100917 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900918}
919
Willy Tarreau532a4502011-09-07 22:37:44 +0200920/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +0200921 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +0200922 * level to be admin.
923 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200924static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +0200925{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100926 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200927 struct proxy *px;
928
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200929 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100930 appctx->ctx.cli.msg = stats_permission_denied_msg;
931 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200932 return NULL;
933 }
934
935 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100936 appctx->ctx.cli.msg = "A frontend name is expected.\n";
937 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200938 return NULL;
939 }
940
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200941 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +0200942 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100943 appctx->ctx.cli.msg = "No such frontend.\n";
944 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200945 return NULL;
946 }
947 return px;
948}
949
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200950/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
951 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +0200952 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200953 * the <arg> is modified to remove the '/'.
954 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200955static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200956{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100957 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200958 struct proxy *px;
959 struct server *sv;
960 char *line;
961
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200962 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100963 appctx->ctx.cli.msg = stats_permission_denied_msg;
964 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200965 return NULL;
966 }
967
968 /* split "backend/server" and make <line> point to server */
969 for (line = arg; *line; line++)
970 if (*line == '/') {
971 *line++ = '\0';
972 break;
973 }
974
975 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100976 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
977 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200978 return NULL;
979 }
980
981 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100982 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
983 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200984 return NULL;
985 }
986
987 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100988 appctx->ctx.cli.msg = "Proxy is disabled.\n";
989 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200990 return NULL;
991 }
992
993 return sv;
994}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200995
996/* This function is used with TLS ticket keys management. It permits to browse
997 * each reference. The variable <getnext> must contain the current node,
998 * <end> point to the root node.
999 */
1000#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1001static inline
1002struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1003{
1004 struct tls_keys_ref *ref = getnext;
1005
1006 while (1) {
1007
1008 /* Get next list entry. */
1009 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1010
1011 /* If the entry is the last of the list, return NULL. */
1012 if (&ref->list == end)
1013 return NULL;
1014
1015 return ref;
1016 }
1017}
1018
1019static inline
1020struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1021{
1022 int id;
1023 char *error;
1024
1025 /* If the reference starts by a '#', this is numeric id. */
1026 if (reference[0] == '#') {
1027 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1028 id = strtol(reference + 1, &error, 10);
1029 if (*error != '\0')
1030 return NULL;
1031
1032 /* Perform the unique id lookup. */
1033 return tlskeys_ref_lookupid(id);
1034 }
1035
1036 /* Perform the string lookup. */
1037 return tlskeys_ref_lookup(reference);
1038}
1039#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001040
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001041/* This function is used with map and acl management. It permits to browse
1042 * each reference. The variable <getnext> must contain the current node,
1043 * <end> point to the root node and the <flags> permit to filter required
1044 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001045 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001046static inline
1047struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1048 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001049{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001050 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001051
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001052 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001053
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001054 /* Get next list entry. */
1055 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001056
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001057 /* If the entry is the last of the list, return NULL. */
1058 if (&ref->list == end)
1059 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001060
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001061 /* If the entry match the flag, return it. */
1062 if (ref->flags & flags)
1063 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001064 }
1065}
1066
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001067static inline
1068struct pat_ref *pat_ref_lookup_ref(const char *reference)
1069{
1070 int id;
1071 char *error;
1072
1073 /* If the reference starts by a '#', this is numeric id. */
1074 if (reference[0] == '#') {
1075 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1076 id = strtol(reference + 1, &error, 10);
1077 if (*error != '\0')
1078 return NULL;
1079
1080 /* Perform the unique id lookup. */
1081 return pat_ref_lookupid(id);
1082 }
1083
1084 /* Perform the string lookup. */
1085 return pat_ref_lookup(reference);
1086}
1087
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001088/* This function is used with map and acl management. It permits to browse
1089 * each reference.
1090 */
1091static inline
1092struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1093{
1094 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001095 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1096 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001097 return NULL;
1098 return expr;
1099}
1100
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001101/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001102 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001103 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001104 * designating the function which will have to process the request, which can
1105 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001106 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001107static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001108{
Willy Tarreau87b09662015-04-03 00:22:06 +02001109 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001110 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001111 char *args[MAX_STATS_ARGS + 1];
1112 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001113 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001114
1115 while (isspace((unsigned char)*line))
1116 line++;
1117
1118 arg = 0;
1119 args[arg] = line;
1120
1121 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001122 if (*line == '\\') {
1123 line++;
1124 if (*line == '\0')
1125 break;
1126 }
1127 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001128 *line++ = '\0';
1129
1130 while (isspace((unsigned char)*line))
1131 line++;
1132
1133 args[++arg] = line;
1134 continue;
1135 }
1136
1137 line++;
1138 }
1139
1140 while (++arg <= MAX_STATS_ARGS)
1141 args[arg] = line;
1142
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001143 /* remove \ */
1144 arg = 0;
1145 while (*args[arg] != '\0') {
1146 j = 0;
1147 for (i=0; args[arg][i] != '\0'; i++) {
1148 if (args[arg][i] == '\\')
1149 continue;
1150 args[arg][j] = args[arg][i];
1151 j++;
1152 }
1153 args[arg][j] = '\0';
1154 arg++;
1155 }
1156
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001157 appctx->ctx.stats.scope_str = 0;
1158 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001159 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001160 if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001161 if (strcmp(args[1], "backend") == 0) {
1162 appctx->st2 = STAT_ST_INIT;
1163 appctx->st0 = STAT_CLI_O_BACKEND;
1164 }
1165 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001166 if (strcmp(args[2], "resolvers") == 0) {
1167 struct dns_resolvers *presolvers;
1168
1169 if (!*args[3]) {
1170 appctx->ctx.cli.msg = "Missing resolver section identifier.\n";
1171 appctx->st0 = STAT_CLI_PRINT;
1172 return 1;
1173 }
1174
1175 appctx->ctx.resolvers.ptr = NULL;
1176 list_for_each_entry(presolvers, &dns_resolvers, list) {
1177 if (strcmp(presolvers->id, args[3]) == 0) {
1178 appctx->ctx.resolvers.ptr = presolvers;
1179 break;
1180 }
1181 }
1182 if (appctx->ctx.resolvers.ptr == NULL) {
1183 appctx->ctx.cli.msg = "Can't find resolvers section.\n";
1184 appctx->st0 = STAT_CLI_PRINT;
1185 return 1;
1186 }
1187
1188 appctx->st2 = STAT_ST_INIT;
1189 appctx->st0 = STAT_CLI_O_RESOLVERS;
1190 return 1;
1191 }
1192 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001193 appctx->ctx.stats.flags |= STAT_BOUND;
1194 appctx->ctx.stats.iid = atoi(args[2]);
1195 appctx->ctx.stats.type = atoi(args[3]);
1196 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001197 }
1198
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001199 appctx->st2 = STAT_ST_INIT;
1200 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001201 }
1202 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001203 appctx->st2 = STAT_ST_INIT;
1204 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001205 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001206 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
1207 appctx->ctx.server_state.backend = NULL;
1208
1209 /* check if a backend name has been provided */
1210 if (*args[3]) {
1211 /* read server state from local file */
1212 appctx->ctx.server_state.backend = proxy_be_by_name(args[3]);
1213
1214 if (appctx->ctx.server_state.backend == NULL) {
1215 appctx->ctx.cli.msg = "Can't find backend.\n";
1216 appctx->st0 = STAT_CLI_PRINT;
1217 return 1;
1218 }
1219 }
1220 appctx->st2 = STAT_ST_INIT;
1221 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1222 return 1;
1223 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001224 else if (strcmp(args[1], "pools") == 0) {
1225 appctx->st2 = STAT_ST_INIT;
1226 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1227 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001228 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001229 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001230 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001231 appctx->ctx.cli.msg = stats_permission_denied_msg;
1232 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001233 return 1;
1234 }
Willy Tarreau76153662012-11-26 01:16:39 +01001235 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001236 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001237 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001238 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001239 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001240 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001241 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001242 appctx->ctx.sess.pos = 0;
1243 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001244 }
1245 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001246 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001247 appctx->ctx.cli.msg = stats_permission_denied_msg;
1248 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001249 return 1;
1250 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001251 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001252 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001253 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001254 appctx->ctx.errors.iid = -1;
1255 appctx->ctx.errors.px = NULL;
1256 appctx->st2 = STAT_ST_INIT;
1257 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001258 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001259 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001260 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001261 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001262 else if (strcmp(args[1], "tls-keys") == 0) {
1263#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1264 appctx->st2 = STAT_ST_INIT;
1265 appctx->st0 = STAT_CLI_O_TLSK;
1266#else
1267 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1268 "that doesn't support specifying TLS ticket keys\n";
1269 appctx->st0 = STAT_CLI_PRINT;
1270#endif
1271 return 1;
1272 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001273 else if (strcmp(args[1], "map") == 0 ||
1274 strcmp(args[1], "acl") == 0) {
1275
1276 /* Set ACL or MAP flags. */
1277 if (args[1][0] == 'm')
1278 appctx->ctx.map.display_flags = PAT_REF_MAP;
1279 else
1280 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001281
1282 /* no parameter: display all map avalaible */
1283 if (!*args[2]) {
1284 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001285 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001286 return 1;
1287 }
1288
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001289 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001290 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001291 if (!appctx->ctx.map.ref ||
1292 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1293 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001294 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001295 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001296 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001297 appctx->st0 = STAT_CLI_PRINT;
1298 return 1;
1299 }
1300 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001301 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001302 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001303 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001304 return 0;
1305 }
1306 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001307 else if (strcmp(args[0], "clear") == 0) {
1308 if (strcmp(args[1], "counters") == 0) {
1309 struct proxy *px;
1310 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001311 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001312 int clrall = 0;
1313
1314 if (strcmp(args[2], "all") == 0)
1315 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001316
Willy Tarreau6162db22009-10-10 17:13:00 +02001317 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001318 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1319 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001320 appctx->ctx.cli.msg = stats_permission_denied_msg;
1321 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001322 return 1;
1323 }
1324
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001325 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001326 if (clrall) {
1327 memset(&px->be_counters, 0, sizeof(px->be_counters));
1328 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1329 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001330 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001331 px->be_counters.conn_max = 0;
1332 px->be_counters.p.http.rps_max = 0;
1333 px->be_counters.sps_max = 0;
1334 px->be_counters.cps_max = 0;
1335 px->be_counters.nbpend_max = 0;
1336
1337 px->fe_counters.conn_max = 0;
1338 px->fe_counters.p.http.rps_max = 0;
1339 px->fe_counters.sps_max = 0;
1340 px->fe_counters.cps_max = 0;
1341 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001342 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001343
1344 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001345 if (clrall)
1346 memset(&sv->counters, 0, sizeof(sv->counters));
1347 else {
1348 sv->counters.cur_sess_max = 0;
1349 sv->counters.nbpend_max = 0;
1350 sv->counters.sps_max = 0;
1351 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001352
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001353 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001354 if (li->counters) {
1355 if (clrall)
1356 memset(li->counters, 0, sizeof(*li->counters));
1357 else
1358 li->counters->conn_max = 0;
1359 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001360 }
1361
Willy Tarreau81c25d02011-09-07 15:17:21 +02001362 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001363 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001364 return 1;
1365 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001366 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001367 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001368 /* end of processing */
1369 return 1;
1370 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001371 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1372 /* Set ACL or MAP flags. */
1373 if (args[1][0] == 'm')
1374 appctx->ctx.map.display_flags = PAT_REF_MAP;
1375 else
1376 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001377
1378 /* no parameter */
1379 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001380 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1381 appctx->ctx.cli.msg = "Missing map identifier.\n";
1382 else
1383 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001384 appctx->st0 = STAT_CLI_PRINT;
1385 return 1;
1386 }
1387
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001388 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001389 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001390 if (!appctx->ctx.map.ref ||
1391 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1392 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001393 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001394 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001395 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001396 appctx->st0 = STAT_CLI_PRINT;
1397 return 1;
1398 }
1399
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001400 /* Clear all. */
1401 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001402
1403 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001404 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001405 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001406 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001407 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001408 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001409 return 0;
1410 }
1411 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001412 else if (strcmp(args[0], "get") == 0) {
1413 if (strcmp(args[1], "weight") == 0) {
1414 struct proxy *px;
1415 struct server *sv;
1416
1417 /* split "backend/server" and make <line> point to server */
1418 for (line = args[2]; *line; line++)
1419 if (*line == '/') {
1420 *line++ = '\0';
1421 break;
1422 }
1423
1424 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001425 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1426 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001427 return 1;
1428 }
1429
1430 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001431 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1432 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001433 return 1;
1434 }
1435
1436 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001437 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001438 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001439 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001440
Willy Tarreau38338fa2009-10-10 18:37:29 +02001441 return 1;
1442 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001443 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1444 /* Set flags. */
1445 if (args[1][0] == 'm')
1446 appctx->ctx.map.display_flags = PAT_REF_MAP;
1447 else
1448 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001449
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001450 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001451 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001452 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1453 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1454 else
1455 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001456 appctx->st0 = STAT_CLI_PRINT;
1457 return 1;
1458 }
1459
1460 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001461 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001462 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001463 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001464 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001465 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001466 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001467 appctx->st0 = STAT_CLI_PRINT;
1468 return 1;
1469 }
1470
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001471 /* copy input string. The string must be allocated because
1472 * it may be used over multiple iterations. It's released
1473 * at the end and upon abort anyway.
1474 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001475 appctx->ctx.map.chunk.len = strlen(args[3]);
1476 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1477 appctx->ctx.map.chunk.str = strdup(args[3]);
1478 if (!appctx->ctx.map.chunk.str) {
1479 appctx->ctx.cli.msg = "Out of memory error.\n";
1480 appctx->st0 = STAT_CLI_PRINT;
1481 return 1;
1482 }
1483
1484 /* prepare response */
1485 appctx->st2 = STAT_ST_INIT;
1486 appctx->st0 = STAT_CLI_O_MLOOK;
1487 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001488 else { /* not "get weight" */
1489 return 0;
1490 }
1491 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001492 else if (strcmp(args[0], "set") == 0) {
1493 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001494 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001495 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001496
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001497 sv = expect_server_admin(s, si, args[2]);
1498 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001499 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001500
Simon Horman7d09b9a2013-02-12 10:45:51 +09001501 warning = server_parse_weight_change_request(sv, args[3]);
1502 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001503 appctx->ctx.cli.msg = warning;
1504 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001505 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001506 return 1;
1507 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001508 else if (strcmp(args[1], "server") == 0) {
1509 struct server *sv;
1510 const char *warning;
1511
1512 sv = expect_server_admin(s, si, args[2]);
1513 if (!sv)
1514 return 1;
1515
1516 if (strcmp(args[3], "weight") == 0) {
1517 warning = server_parse_weight_change_request(sv, args[4]);
1518 if (warning) {
1519 appctx->ctx.cli.msg = warning;
1520 appctx->st0 = STAT_CLI_PRINT;
1521 }
1522 }
1523 else if (strcmp(args[3], "state") == 0) {
1524 if (strcmp(args[4], "ready") == 0)
1525 srv_adm_set_ready(sv);
1526 else if (strcmp(args[4], "drain") == 0)
1527 srv_adm_set_drain(sv);
1528 else if (strcmp(args[4], "maint") == 0)
1529 srv_adm_set_maint(sv);
1530 else {
1531 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1532 appctx->st0 = STAT_CLI_PRINT;
1533 }
1534 }
1535 else if (strcmp(args[3], "health") == 0) {
1536 if (sv->track) {
1537 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1538 appctx->st0 = STAT_CLI_PRINT;
1539 }
1540 else if (strcmp(args[4], "up") == 0) {
1541 sv->check.health = sv->check.rise + sv->check.fall - 1;
1542 srv_set_running(sv, "changed from CLI");
1543 }
1544 else if (strcmp(args[4], "stopping") == 0) {
1545 sv->check.health = sv->check.rise + sv->check.fall - 1;
1546 srv_set_stopping(sv, "changed from CLI");
1547 }
1548 else if (strcmp(args[4], "down") == 0) {
1549 sv->check.health = 0;
1550 srv_set_stopped(sv, "changed from CLI");
1551 }
1552 else {
1553 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1554 appctx->st0 = STAT_CLI_PRINT;
1555 }
1556 }
1557 else if (strcmp(args[3], "agent") == 0) {
1558 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1559 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1560 appctx->st0 = STAT_CLI_PRINT;
1561 }
1562 else if (strcmp(args[4], "up") == 0) {
1563 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1564 srv_set_running(sv, "changed from CLI");
1565 }
1566 else if (strcmp(args[4], "down") == 0) {
1567 sv->agent.health = 0;
1568 srv_set_stopped(sv, "changed from CLI");
1569 }
1570 else {
1571 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1572 appctx->st0 = STAT_CLI_PRINT;
1573 }
1574 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001575 else if (strcmp(args[3], "addr") == 0) {
1576 warning = server_parse_addr_change_request(sv, args[4]);
1577 if (warning) {
1578 appctx->ctx.cli.msg = warning;
1579 appctx->st0 = STAT_CLI_PRINT;
1580 }
1581 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001582 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001583 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001584 appctx->st0 = STAT_CLI_PRINT;
1585 }
1586 return 1;
1587 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001588 else if (strcmp(args[1], "timeout") == 0) {
1589 if (strcmp(args[2], "cli") == 0) {
1590 unsigned timeout;
1591 const char *res;
1592
1593 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001594 appctx->ctx.cli.msg = "Expects an integer value.\n";
1595 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001596 return 1;
1597 }
1598
1599 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1600 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001601 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1602 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001603 return 1;
1604 }
1605
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001606 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001607 return 1;
1608 }
1609 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001610 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1611 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001612 return 1;
1613 }
1614 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001615 else if (strcmp(args[1], "maxconn") == 0) {
1616 if (strcmp(args[2], "frontend") == 0) {
1617 struct proxy *px;
1618 struct listener *l;
1619 int v;
1620
Willy Tarreau532a4502011-09-07 22:37:44 +02001621 px = expect_frontend_admin(s, si, args[3]);
1622 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001623 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001624
1625 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001626 appctx->ctx.cli.msg = "Integer value expected.\n";
1627 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001628 return 1;
1629 }
1630
1631 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001632 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001633 appctx->ctx.cli.msg = "Value out of range.\n";
1634 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001635 return 1;
1636 }
1637
1638 /* OK, the value is fine, so we assign it to the proxy and to all of
1639 * its listeners. The blocked ones will be dequeued.
1640 */
1641 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001642 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001643 l->maxconn = v;
1644 if (l->state == LI_FULL)
1645 resume_listener(l);
1646 }
1647
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001648 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1649 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001650
1651 return 1;
1652 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001653 else if (strcmp(args[2], "global") == 0) {
1654 int v;
1655
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001656 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001657 appctx->ctx.cli.msg = stats_permission_denied_msg;
1658 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001659 return 1;
1660 }
1661
1662 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001663 appctx->ctx.cli.msg = "Expects an integer value.\n";
1664 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001665 return 1;
1666 }
1667
1668 v = atoi(args[3]);
1669 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001670 appctx->ctx.cli.msg = "Value out of range.\n";
1671 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001672 return 1;
1673 }
1674
1675 /* check for unlimited values */
1676 if (v <= 0)
1677 v = global.hardmaxconn;
1678
1679 global.maxconn = v;
1680
1681 /* Dequeues all of the listeners waiting for a resource */
1682 if (!LIST_ISEMPTY(&global_listener_queue))
1683 dequeue_all_listeners(&global_listener_queue);
1684
1685 return 1;
1686 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001687 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001688 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1689 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001690 return 1;
1691 }
1692 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001693 else if (strcmp(args[1], "rate-limit") == 0) {
1694 if (strcmp(args[2], "connections") == 0) {
1695 if (strcmp(args[3], "global") == 0) {
1696 int v;
1697
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001698 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001699 appctx->ctx.cli.msg = stats_permission_denied_msg;
1700 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001701 return 1;
1702 }
1703
1704 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001705 appctx->ctx.cli.msg = "Expects an integer value.\n";
1706 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001707 return 1;
1708 }
1709
1710 v = atoi(args[4]);
1711 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001712 appctx->ctx.cli.msg = "Value out of range.\n";
1713 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001714 return 1;
1715 }
1716
1717 global.cps_lim = v;
1718
1719 /* Dequeues all of the listeners waiting for a resource */
1720 if (!LIST_ISEMPTY(&global_listener_queue))
1721 dequeue_all_listeners(&global_listener_queue);
1722
1723 return 1;
1724 }
1725 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001726 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1727 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001728 return 1;
1729 }
1730 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001731 else if (strcmp(args[2], "sessions") == 0) {
1732 if (strcmp(args[3], "global") == 0) {
1733 int v;
1734
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001735 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001736 appctx->ctx.cli.msg = stats_permission_denied_msg;
1737 appctx->st0 = STAT_CLI_PRINT;
1738 return 1;
1739 }
1740
1741 if (!*args[4]) {
1742 appctx->ctx.cli.msg = "Expects an integer value.\n";
1743 appctx->st0 = STAT_CLI_PRINT;
1744 return 1;
1745 }
1746
1747 v = atoi(args[4]);
1748 if (v < 0) {
1749 appctx->ctx.cli.msg = "Value out of range.\n";
1750 appctx->st0 = STAT_CLI_PRINT;
1751 return 1;
1752 }
1753
1754 global.sps_lim = v;
1755
1756 /* Dequeues all of the listeners waiting for a resource */
1757 if (!LIST_ISEMPTY(&global_listener_queue))
1758 dequeue_all_listeners(&global_listener_queue);
1759
1760 return 1;
1761 }
1762 else {
1763 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1764 appctx->st0 = STAT_CLI_PRINT;
1765 return 1;
1766 }
1767 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001768#ifdef USE_OPENSSL
1769 else if (strcmp(args[2], "ssl-sessions") == 0) {
1770 if (strcmp(args[3], "global") == 0) {
1771 int v;
1772
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001773 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001774 appctx->ctx.cli.msg = stats_permission_denied_msg;
1775 appctx->st0 = STAT_CLI_PRINT;
1776 return 1;
1777 }
1778
1779 if (!*args[4]) {
1780 appctx->ctx.cli.msg = "Expects an integer value.\n";
1781 appctx->st0 = STAT_CLI_PRINT;
1782 return 1;
1783 }
1784
1785 v = atoi(args[4]);
1786 if (v < 0) {
1787 appctx->ctx.cli.msg = "Value out of range.\n";
1788 appctx->st0 = STAT_CLI_PRINT;
1789 return 1;
1790 }
1791
1792 global.ssl_lim = v;
1793
1794 /* Dequeues all of the listeners waiting for a resource */
1795 if (!LIST_ISEMPTY(&global_listener_queue))
1796 dequeue_all_listeners(&global_listener_queue);
1797
1798 return 1;
1799 }
1800 else {
1801 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1802 appctx->st0 = STAT_CLI_PRINT;
1803 return 1;
1804 }
1805 }
1806#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001807 else if (strcmp(args[2], "http-compression") == 0) {
1808 if (strcmp(args[3], "global") == 0) {
1809 int v;
1810
Willy Tarreau85d47f92012-11-21 00:29:50 +01001811 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001812 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1813 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001814 return 1;
1815 }
1816
William Lallemandd85f9172012-11-09 17:05:39 +01001817 v = atoi(args[4]);
1818 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1819 }
1820 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001821 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1822 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001823 return 1;
1824 }
1825 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001826 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001827 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001828 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001829 return 1;
1830 }
1831 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001832 else if (strcmp(args[1], "table") == 0) {
1833 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1834 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001835 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001836 char *err;
1837
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001838 /* Set flags. */
1839 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001840
1841 /* Expect three parameters: map name, key and new value. */
1842 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001843 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001844 appctx->st0 = STAT_CLI_PRINT;
1845 return 1;
1846 }
1847
1848 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001849 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001850 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001851 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001852 appctx->st0 = STAT_CLI_PRINT;
1853 return 1;
1854 }
1855
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001856 /* If the entry identifier start with a '#', it is considered as
1857 * pointer id
1858 */
1859 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1860 struct pat_ref_elt *ref;
1861 long long int conv;
1862 char *error;
1863
1864 /* Convert argument to integer value. */
1865 conv = strtoll(&args[3][1], &error, 16);
1866 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001867 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001868 appctx->st0 = STAT_CLI_PRINT;
1869 return 1;
1870 }
1871
1872 /* Convert and check integer to pointer. */
1873 ref = (struct pat_ref_elt *)(long)conv;
1874 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001875 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001876 appctx->st0 = STAT_CLI_PRINT;
1877 return 1;
1878 }
1879
1880 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001881 err = NULL;
1882 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1883 if (err)
1884 memprintf(&err, "%s.\n", err);
1885 appctx->ctx.cli.err = err;
1886 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001887 return 1;
1888 }
1889 }
1890 else {
1891 /* Else, use the entry identifier as pattern
1892 * string, and update the value.
1893 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001894 err = NULL;
1895 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1896 if (err)
1897 memprintf(&err, "%s.\n", err);
1898 appctx->ctx.cli.err = err;
1899 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001900 return 1;
1901 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001902 }
1903
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001904 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001905 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001906 return 1;
1907 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001908#ifdef USE_OPENSSL
1909 else if (strcmp(args[1], "ssl") == 0) {
1910 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01001911#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001912 char *err = NULL;
1913
Emeric Brunaf4ef742014-06-19 14:10:45 +02001914 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001915 if (!*args[3]) {
1916 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1917 appctx->st0 = STAT_CLI_PRINT;
1918 return 1;
1919 }
1920
1921 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1922 if (trash.len < 0) {
1923 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1924 appctx->st0 = STAT_CLI_PRINT;
1925 return 1;
1926 }
1927
1928 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1929 if (err) {
1930 memprintf(&err, "%s.\n", err);
1931 appctx->ctx.cli.err = err;
1932 appctx->st0 = STAT_CLI_PRINT_FREE;
1933 }
1934 return 1;
1935 }
1936 appctx->ctx.cli.msg = "OCSP Response updated!";
1937 appctx->st0 = STAT_CLI_PRINT;
1938 return 1;
1939#else
1940 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1941 appctx->st0 = STAT_CLI_PRINT;
1942 return 1;
1943#endif
1944 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001945 else if (strcmp(args[2], "tls-key") == 0) {
1946#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1947 /* Expect two parameters: the filename and the new new TLS key in encoding */
1948 if (!*args[3] || !*args[4]) {
1949 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
1950 appctx->st0 = STAT_CLI_PRINT;
1951 return 1;
1952 }
1953
1954 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
1955 if(!appctx->ctx.tlskeys.ref) {
1956 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
1957 appctx->st0 = STAT_CLI_PRINT;
1958 return 1;
1959 }
1960
1961 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
1962 if (trash.len != sizeof(struct tls_sess_key)) {
1963 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
1964 appctx->st0 = STAT_CLI_PRINT;
1965 return 1;
1966 }
1967
Pradeep Jindalcc79b002015-08-20 18:25:17 +05301968 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
1969 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 +02001970
1971 appctx->ctx.cli.msg = "TLS ticket key updated!";
1972 appctx->st0 = STAT_CLI_PRINT;
1973 return 1;
1974#else
1975 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1976 "that doesn't support specifying TLS ticket keys\n";
1977 appctx->st0 = STAT_CLI_PRINT;
1978 return 1;
1979#endif
1980 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001981 else {
1982 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1983 appctx->st0 = STAT_CLI_PRINT;
1984 return 1;
1985 }
1986 }
1987#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001988 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001989 return 0;
1990 }
1991 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001992 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001993 if (strcmp(args[1], "agent") == 0) {
1994 struct server *sv;
1995
1996 sv = expect_server_admin(s, si, args[2]);
1997 if (!sv)
1998 return 1;
1999
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002000 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2001 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2002 appctx->st0 = STAT_CLI_PRINT;
2003 return 1;
2004 }
2005
2006 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002007 return 1;
2008 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002009 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002010 struct server *sv;
2011
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002012 sv = expect_server_admin(s, si, args[2]);
2013 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002014 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002015
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002016 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2017 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2018 appctx->st0 = STAT_CLI_PRINT;
2019 return 1;
2020 }
2021
2022 sv->check.state |= CHK_ST_ENABLED;
2023 return 1;
2024 }
2025 else if (strcmp(args[1], "server") == 0) {
2026 struct server *sv;
2027
2028 sv = expect_server_admin(s, si, args[2]);
2029 if (!sv)
2030 return 1;
2031
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002032 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002033 return 1;
2034 }
2035 else if (strcmp(args[1], "frontend") == 0) {
2036 struct proxy *px;
2037
2038 px = expect_frontend_admin(s, si, args[2]);
2039 if (!px)
2040 return 1;
2041
2042 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002043 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2044 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002045 return 1;
2046 }
2047
2048 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002049 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2050 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002051 return 1;
2052 }
2053
2054 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002055 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2056 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002057 return 1;
2058 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002059 return 1;
2060 }
2061 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002062 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002063 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002064 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002065 }
2066 }
2067 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002068 if (strcmp(args[1], "agent") == 0) {
2069 struct server *sv;
2070
2071 sv = expect_server_admin(s, si, args[2]);
2072 if (!sv)
2073 return 1;
2074
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002075 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002076 return 1;
2077 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002078 else if (strcmp(args[1], "health") == 0) {
2079 struct server *sv;
2080
2081 sv = expect_server_admin(s, si, args[2]);
2082 if (!sv)
2083 return 1;
2084
2085 sv->check.state &= ~CHK_ST_ENABLED;
2086 return 1;
2087 }
Simon Horman671b6f02013-11-25 10:46:39 +09002088 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002089 struct server *sv;
2090
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002091 sv = expect_server_admin(s, si, args[2]);
2092 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002093 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002094
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002095 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002096 return 1;
2097 }
2098 else if (strcmp(args[1], "frontend") == 0) {
2099 struct proxy *px;
2100
2101 px = expect_frontend_admin(s, si, args[2]);
2102 if (!px)
2103 return 1;
2104
2105 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002106 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2107 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002108 return 1;
2109 }
2110
2111 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002112 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2113 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002114 return 1;
2115 }
2116
2117 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002118 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2119 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002120 return 1;
2121 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002122 return 1;
2123 }
2124 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002125 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002126 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002127 return 1;
2128 }
2129 }
2130 else if (strcmp(args[0], "shutdown") == 0) {
2131 if (strcmp(args[1], "frontend") == 0) {
2132 struct proxy *px;
2133
2134 px = expect_frontend_admin(s, si, args[2]);
2135 if (!px)
2136 return 1;
2137
2138 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002139 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2140 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002141 return 1;
2142 }
2143
2144 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2145 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2146 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2147 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2148 stop_proxy(px);
2149 return 1;
2150 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002151 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002152 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002153
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002154 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002155 appctx->ctx.cli.msg = stats_permission_denied_msg;
2156 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002157 return 1;
2158 }
2159
2160 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002161 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2162 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002163 return 1;
2164 }
2165
2166 ptr = (void *)strtoul(args[2], NULL, 0);
2167
Willy Tarreau87b09662015-04-03 00:22:06 +02002168 /* first, look for the requested stream in the stream table */
2169 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002170 if (sess == ptr)
2171 break;
2172 }
2173
Willy Tarreau87b09662015-04-03 00:22:06 +02002174 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002175 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002176 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2177 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002178 return 1;
2179 }
2180
Willy Tarreaue7dff022015-04-03 01:14:29 +02002181 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002182 return 1;
2183 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002184 else if (strcmp(args[1], "sessions") == 0) {
2185 if (strcmp(args[2], "server") == 0) {
2186 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002187 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002188
2189 sv = expect_server_admin(s, si, args[3]);
2190 if (!sv)
2191 return 1;
2192
Willy Tarreau87b09662015-04-03 00:22:06 +02002193 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002194 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2195 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002196 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002197
2198 return 1;
2199 }
2200 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002201 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2202 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002203 return 1;
2204 }
2205 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002206 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002207 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2208 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002209 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002210 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002211 }
2212 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002213 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2214 if (args[1][0] == 'm')
2215 appctx->ctx.map.display_flags = PAT_REF_MAP;
2216 else
2217 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002218
2219 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002220 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2221 if (!*args[2] || !*args[3]) {
2222 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2223 appctx->st0 = STAT_CLI_PRINT;
2224 return 1;
2225 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002226 }
2227
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002228 else {
2229 if (!*args[2] || !*args[3]) {
2230 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2231 appctx->st0 = STAT_CLI_PRINT;
2232 return 1;
2233 }
2234 }
2235
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002236 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002237 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002238 if (!appctx->ctx.map.ref ||
2239 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002240 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002241 appctx->st0 = STAT_CLI_PRINT;
2242 return 1;
2243 }
2244
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002245 /* If the entry identifier start with a '#', it is considered as
2246 * pointer id
2247 */
2248 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2249 struct pat_ref_elt *ref;
2250 long long int conv;
2251 char *error;
2252
2253 /* Convert argument to integer value. */
2254 conv = strtoll(&args[3][1], &error, 16);
2255 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002256 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002257 appctx->st0 = STAT_CLI_PRINT;
2258 return 1;
2259 }
2260
2261 /* Convert and check integer to pointer. */
2262 ref = (struct pat_ref_elt *)(long)conv;
2263 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002264 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002265 appctx->st0 = STAT_CLI_PRINT;
2266 return 1;
2267 }
2268
2269 /* Try to delete the entry. */
2270 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2271 /* The entry is not found, send message. */
2272 appctx->ctx.cli.msg = "Key not found.\n";
2273 appctx->st0 = STAT_CLI_PRINT;
2274 return 1;
2275 }
2276 }
2277 else {
2278 /* Else, use the entry identifier as pattern
2279 * string and try to delete the entry.
2280 */
2281 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2282 /* The entry is not found, send message. */
2283 appctx->ctx.cli.msg = "Key not found.\n";
2284 appctx->st0 = STAT_CLI_PRINT;
2285 return 1;
2286 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002287 }
2288
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002289 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002290 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002291 return 1;
2292 }
2293 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002294 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002295 appctx->st0 = STAT_CLI_PRINT;
2296 return 1;
2297 }
2298 }
2299 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002300 if (strcmp(args[1], "map") == 0 ||
2301 strcmp(args[1], "acl") == 0) {
2302 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002303 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002304
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002305 /* Set flags. */
2306 if (args[1][0] == 'm')
2307 appctx->ctx.map.display_flags = PAT_REF_MAP;
2308 else
2309 appctx->ctx.map.display_flags = PAT_REF_ACL;
2310
2311 /* If the keywork is "map", we expect three parameters, if it
2312 * is "acl", we expect only two parameters
2313 */
2314 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2315 if (!*args[2] || !*args[3] || !*args[4]) {
2316 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2317 appctx->st0 = STAT_CLI_PRINT;
2318 return 1;
2319 }
2320 }
2321 else {
2322 if (!*args[2] || !*args[3]) {
2323 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2324 appctx->st0 = STAT_CLI_PRINT;
2325 return 1;
2326 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002327 }
2328
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002329 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002330 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002331 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002332 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002333 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002334 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002335 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002336 appctx->st0 = STAT_CLI_PRINT;
2337 return 1;
2338 }
2339
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002340 /* The command "add acl" is prohibited if the reference
2341 * use samples.
2342 */
2343 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2344 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2345 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2346 "You must use the command 'add map' to add values.\n";
2347 appctx->st0 = STAT_CLI_PRINT;
2348 return 1;
2349 }
2350
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002351 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002352 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002353 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002354 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002355 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002356 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002357 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002358 if (err)
2359 memprintf(&err, "%s.\n", err);
2360 appctx->ctx.cli.err = err;
2361 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002362 return 1;
2363 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002364
2365 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002366 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002367 return 1;
2368 }
2369 else { /* unknown "del" parameter */
2370 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2371 appctx->st0 = STAT_CLI_PRINT;
2372 return 1;
2373 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002374 }
2375 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002376 return 0;
2377 }
2378 return 1;
2379}
2380
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002381/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002382 * used to processes I/O from/to the stats unix socket. The system relies on a
2383 * state machine handling requests and various responses. We read a request,
2384 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002385 * Then we can read again. The state is stored in appctx->st0 and is one of the
2386 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002387 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002388 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002389static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002390{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002391 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002392 struct channel *req = si_oc(si);
2393 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002394 int reql;
2395 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002396
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002397 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2398 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002399
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002400 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002401 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002402 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002403 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2404 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002405 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002406 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002407 /* Let's close for real now. We just close the request
2408 * side, the conditions below will complete if needed.
2409 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002410 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002411 break;
2412 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002413 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002414 /* ensure we have some output room left in the event we
2415 * would want to return some info right after parsing.
2416 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002417 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002418 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002419 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002420 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002421
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002422 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002423 if (reql <= 0) { /* closed or EOL not found */
2424 if (reql == 0)
2425 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002426 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002427 continue;
2428 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002429
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002430 /* seek for a possible semi-colon. If we find one, we
2431 * replace it with an LF and skip only this part.
2432 */
2433 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002434 if (trash.str[len] == ';') {
2435 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002436 reql = len + 1;
2437 break;
2438 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002439
Willy Tarreau816fc222009-10-04 07:36:58 +02002440 /* now it is time to check that we have a full line,
2441 * remove the trailing \n and possibly \r, then cut the
2442 * line.
2443 */
2444 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002445 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002446 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002447 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002448 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002449
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002450 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002451 len--;
2452
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002453 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002454
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002455 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002456 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002457 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002458 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002459 continue;
2460 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002461 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002462 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002463 else if (strcmp(trash.str, "help") == 0 ||
2464 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002465 appctx->ctx.cli.msg = stats_sock_usage_msg;
2466 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002467 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002468 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002469 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002470 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002471 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002472 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002473 /* if prompt is disabled, print help on empty lines,
2474 * so that the user at least knows how to enable
2475 * prompt and find help.
2476 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002477 appctx->ctx.cli.msg = stats_sock_usage_msg;
2478 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002479 }
2480
2481 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002482 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002483 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002484 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002485 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002486 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau00a37f02015-04-13 12:05:19 +02002487 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002488 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002489 continue;
2490 }
2491
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002492 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002493 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002494 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002495 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002496 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002497 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002498 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002499 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002500 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002501 free(appctx->ctx.cli.err);
2502 appctx->st0 = STAT_CLI_PROMPT;
2503 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002504 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002505 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002506 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002507 case STAT_CLI_O_BACKEND:
2508 if (stats_dump_backend_to_buffer(si))
2509 appctx->st0 = STAT_CLI_PROMPT;
2510 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002511 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002512 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002513 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002514 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002515 case STAT_CLI_O_SERVERS_STATE:
2516 if (stats_dump_servers_state_to_buffer(si))
2517 appctx->st0 = STAT_CLI_PROMPT;
2518 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002519 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002520 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002521 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002522 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002523 case STAT_CLI_O_RESOLVERS:
2524 if (stats_dump_resolvers_to_buffer(si))
2525 appctx->st0 = STAT_CLI_PROMPT;
2526 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002527 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002528 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002529 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002530 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002531 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002532 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002533 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002534 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002535 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002536 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002537 if (stats_table_request(si, appctx->st0))
2538 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002539 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002540 case STAT_CLI_O_PATS:
2541 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002542 appctx->st0 = STAT_CLI_PROMPT;
2543 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002544 case STAT_CLI_O_PAT:
2545 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002546 appctx->st0 = STAT_CLI_PROMPT;
2547 break;
2548 case STAT_CLI_O_MLOOK:
2549 if (stats_map_lookup(si))
2550 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002551 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002552 case STAT_CLI_O_POOLS:
2553 if (stats_dump_pools_to_buffer(si))
2554 appctx->st0 = STAT_CLI_PROMPT;
2555 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002556#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2557 case STAT_CLI_O_TLSK:
2558 if (stats_tlskeys_list(si))
2559 appctx->st0 = STAT_CLI_PROMPT;
2560 break;
2561#endif
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002562 default: /* abnormal state */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002563 cli_release_handler(appctx);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002564 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002565 break;
2566 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002567
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002568 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002569 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002570 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002571 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002572 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002573 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002574 }
2575
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002576 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002577 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002578 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002579
2580 /* Now we close the output if one of the writers did so,
2581 * or if we're not in interactive mode and the request
2582 * buffer is empty. This still allows pipelined requests
2583 * to be sent in non-interactive mode.
2584 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002585 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2586 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002587 continue;
2588 }
2589
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002590 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002591 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002592 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002593 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002594
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002595 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002596 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2597 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002598 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002599 * and nothing more to consume. This is comparable to a broken pipe, so
2600 * we forward the close to the request side so that it flows upstream to
2601 * the client.
2602 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002603 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002604 }
2605
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002606 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002607 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2608 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2609 /* We have no more processing to do, and nothing more to send, and
2610 * the client side has closed. So we'll forward this state downstream
2611 * on the response buffer.
2612 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002613 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002614 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002615 }
2616
Willy Tarreau828824a2015-04-19 17:20:03 +02002617 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002618 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 +02002619 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002620 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 +02002621}
2622
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002623/* This function dumps information onto the stream interface's read buffer.
2624 * It returns 0 as long as it does not complete, non-zero upon completion.
2625 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002626 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002627static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002628{
2629 unsigned int up = (now.tv_sec - start_date.tv_sec);
2630
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002631#ifdef USE_OPENSSL
2632 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2633 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2634 int ssl_reuse = 0;
2635
2636 if (ssl_key_rate < ssl_sess_rate) {
2637 /* count the ssl reuse ratio and avoid overflows in both directions */
2638 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2639 }
2640#endif
2641
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002642 chunk_printf(&trash,
2643 "Name: " PRODUCT_NAME "\n"
2644 "Version: " HAPROXY_VERSION "\n"
2645 "Release_date: " HAPROXY_DATE "\n"
2646 "Nbproc: %d\n"
2647 "Process_num: %d\n"
2648 "Pid: %d\n"
2649 "Uptime: %dd %dh%02dm%02ds\n"
2650 "Uptime_sec: %d\n"
2651 "Memmax_MB: %d\n"
2652 "Ulimit-n: %d\n"
2653 "Maxsock: %d\n"
2654 "Maxconn: %d\n"
2655 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002656 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002657 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002658 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002659#ifdef USE_OPENSSL
2660 "MaxSslConns: %d\n"
2661 "CurrSslConns: %d\n"
2662 "CumSslConns: %d\n"
2663#endif
2664 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002665 "PipesUsed: %d\n"
2666 "PipesFree: %d\n"
2667 "ConnRate: %d\n"
2668 "ConnRateLimit: %d\n"
2669 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002670 "SessRate: %d\n"
2671 "SessRateLimit: %d\n"
2672 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002673#ifdef USE_OPENSSL
2674 "SslRate: %d\n"
2675 "SslRateLimit: %d\n"
2676 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002677 "SslFrontendKeyRate: %d\n"
2678 "SslFrontendMaxKeyRate: %d\n"
2679 "SslFrontendSessionReuse_pct: %d\n"
2680 "SslBackendKeyRate: %d\n"
2681 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002682 "SslCacheLookups: %u\n"
2683 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002684#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002685 "CompressBpsIn: %u\n"
2686 "CompressBpsOut: %u\n"
2687 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002688#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002689 "ZlibMemUsage: %ld\n"
2690 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002691#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002692 "Tasks: %d\n"
2693 "Run_queue: %d\n"
2694 "Idle_pct: %d\n"
2695 "node: %s\n"
2696 "description: %s\n"
2697 "",
2698 global.nbproc,
2699 relative_pid,
2700 pid,
2701 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2702 up,
2703 global.rlimit_memmax,
2704 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002705 global.maxsock, global.maxconn, global.hardmaxconn,
2706 actconn, totalconn, global.req_count,
2707#ifdef USE_OPENSSL
2708 global.maxsslconn, sslconns, totalsslconns,
2709#endif
2710 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002711 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002712 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002713#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002714 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2715 ssl_key_rate, global.ssl_fe_keys_max,
2716 ssl_reuse,
2717 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002718 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002719#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002720 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2721 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002722#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002723 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002724#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002725 nb_tasks_cur, run_queue_cur, idle_pct,
2726 global.node, global.desc ? global.desc : ""
2727 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002728
Willy Tarreaubc18da12015-03-13 14:00:47 +01002729 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002730 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002731 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002732 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002733
2734 return 1;
2735}
2736
Baptiste Assmann28289462015-07-03 08:01:20 +02002737/* dumps server state information into <buf> for all the servers found in <backend>
2738 * These information are all the parameters which may change during HAProxy runtime.
2739 * By default, we only export to the last known server state file format.
2740 * These information can be used at next startup to recover same level of server state.
2741 */
2742static void dump_servers_state(struct proxy *backend, struct chunk *buf)
2743{
2744 struct server *srv;
2745 char srv_addr[INET6_ADDRSTRLEN + 1];
2746 time_t srv_time_since_last_change;
2747 int bk_f_forced_id, srv_f_forced_id;
2748
2749 /* we don't want to report any state if the backend is not enabled on this process */
2750 if (backend->bind_proc && !(backend->bind_proc & (1UL << (relative_pid - 1))))
2751 return;
2752
2753 srv = backend->srv;
2754
2755 while (srv) {
2756 srv_addr[0] = '\0';
2757 srv_time_since_last_change = 0;
2758 bk_f_forced_id = 0;
2759 srv_f_forced_id = 0;
2760
2761 switch (srv->addr.ss_family) {
2762 case AF_INET:
2763 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
2764 srv_addr, INET_ADDRSTRLEN + 1);
2765 break;
2766 case AF_INET6:
2767 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
2768 srv_addr, INET6_ADDRSTRLEN + 1);
2769 break;
2770 }
2771 srv_time_since_last_change = now.tv_sec - srv->last_change;
2772 bk_f_forced_id = backend->options & PR_O_FORCED_ID ? 1 : 0;
2773 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
2774
2775 chunk_appendf(buf,
2776 "%d %s "
2777 "%d %s %s "
2778 "%d %d %d %d %ld "
2779 "%d %d %d %d %d "
2780 "%d %d"
2781 "\n",
2782 backend->uuid, backend->id,
2783 srv->puid, srv->id, srv_addr,
2784 srv->state, srv->admin, srv->uweight, srv->iweight, srv_time_since_last_change,
2785 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
2786 bk_f_forced_id, srv_f_forced_id);
2787
2788 srv = srv->next;
2789 }
2790}
2791
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002792/* Parses backend list and simply report backend names */
2793static int stats_dump_backend_to_buffer(struct stream_interface *si)
2794{
2795 extern struct proxy *proxy;
2796 struct proxy *curproxy;
2797
2798 chunk_reset(&trash);
2799 chunk_printf(&trash, "# name\n");
2800
2801 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2802 /* looking for backends only */
2803 if (!(curproxy->cap & PR_CAP_BE))
2804 continue;
2805
2806 /* we don't want to list a backend which is bound to this process */
2807 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
2808 continue;
2809
2810 chunk_appendf(&trash, "%s\n", curproxy->id);
2811 }
2812
2813 if (bi_putchk(si_ic(si), &trash) == -1) {
2814 si_applet_cant_put(si);
2815 return 0;
2816 }
2817
2818 return 1;
2819}
2820
Baptiste Assmann28289462015-07-03 08:01:20 +02002821/* Parses backend list or simply use backend name provided by the user to return
2822 * states of servers to stdout.
2823 */
2824static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
2825{
2826 struct appctx *appctx = __objt_appctx(si->end);
2827 extern struct proxy *proxy;
2828 struct proxy *curproxy;
2829
2830 chunk_reset(&trash);
2831
2832 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
2833
2834 if (appctx->ctx.server_state.backend) {
2835 dump_servers_state(appctx->ctx.server_state.backend, &trash);
2836 }
2837 else {
2838 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2839 /* servers are only in backends */
2840 if (!(curproxy->cap & PR_CAP_BE))
2841 continue;
2842
2843 dump_servers_state(curproxy, &trash);
2844 }
2845 }
2846
2847 if (bi_putchk(si_ic(si), &trash) == -1) {
2848 si_applet_cant_put(si);
2849 return 0;
2850 }
2851
2852 return 1;
2853}
2854
Willy Tarreau12833bb2014-01-28 16:49:56 +01002855/* This function dumps memory usage information onto the stream interface's
2856 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2857 * completion. No state is used.
2858 */
2859static int stats_dump_pools_to_buffer(struct stream_interface *si)
2860{
2861 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01002862 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002863 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01002864 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002865 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01002866 return 1;
2867}
2868
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002869/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2870 * the state from stream interface <si>. The caller is responsible for clearing
2871 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002872 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002873static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002874{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002875 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002876 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002877
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002878 if (!(px->cap & PR_CAP_FE))
2879 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002880
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002881 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002882 return 0;
2883
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002884 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002885 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002886 /* name, queue */
2887 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002888
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002889 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002890 /* Column sub-heading for Enable or Disable server */
2891 chunk_appendf(&trash, "<td></td>");
2892 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002893
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002894 chunk_appendf(&trash,
2895 "<td class=ac>"
2896 "<a name=\"%s/Frontend\"></a>"
2897 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2898 "<td colspan=3></td>"
2899 "",
2900 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002901
Willy Tarreau466c9b52012-12-23 02:25:03 +01002902 chunk_appendf(&trash,
2903 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002904 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002905 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2906 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2907 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002908 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2909 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2910 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002911
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002912 if (px->mode == PR_MODE_HTTP)
2913 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002914 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002915 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002916
2917 chunk_appendf(&trash,
2918 "</table></div></u></td>"
2919 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002920 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002921 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2922 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2923 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002924 U2H(px->fe_counters.sps_max),
2925 U2H(px->fe_counters.cps_max),
2926 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002927
2928 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002929 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002930 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002931 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002932
2933 chunk_appendf(&trash,
2934 "</table></div></u></td>"
2935 /* sessions rate : limit */
2936 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002937 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002938
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002939 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002940 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002941 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002942 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002943 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2944 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002945 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002946 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2947 U2H(px->fe_counters.cum_sess),
2948 U2H(px->fe_counters.cum_conn),
2949 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002950
Willy Tarreau466c9b52012-12-23 02:25:03 +01002951 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002952 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002953 chunk_appendf(&trash,
2954 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2955 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2956 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2957 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2958 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2959 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2960 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2961 "<tr><th>- other responses:</th><td>%s</td></tr>"
2962 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2963 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002964 U2H(px->fe_counters.p.http.cum_req),
2965 U2H(px->fe_counters.p.http.rsp[1]),
2966 U2H(px->fe_counters.p.http.rsp[2]),
2967 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002968 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002969 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002970 U2H(px->fe_counters.p.http.rsp[3]),
2971 U2H(px->fe_counters.p.http.rsp[4]),
2972 U2H(px->fe_counters.p.http.rsp[5]),
2973 U2H(px->fe_counters.p.http.rsp[0]),
2974 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002975 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002976
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002977 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002978 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002979 /* sessions: lbtot, lastsess */
2980 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002981 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002982 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002983 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002984 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002985
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002986 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002987 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02002988 "<td>%s%s<div class=tips><table class=det>"
2989 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
2990 "<tr><th>Compression in:</th><td>%s</td></tr>"
2991 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
2992 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
2993 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
2994 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002995 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002996 U2H(px->fe_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02002997 U2H(px->fe_counters.bytes_out),
2998 U2H(px->fe_counters.comp_in),
2999 U2H(px->fe_counters.comp_out),
3000 px->fe_counters.comp_in ? (int)(px->fe_counters.comp_out * 100 / px->fe_counters.comp_in) : 0,
3001 U2H(px->fe_counters.comp_byp),
3002 U2H(px->fe_counters.comp_in - px->fe_counters.comp_out),
3003 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 +01003004 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003005
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003006 chunk_appendf(&trash,
3007 /* denied: req, resp */
3008 "<td>%s</td><td>%s</td>"
3009 /* errors : request, connect, response */
3010 "<td>%s</td><td></td><td></td>"
3011 /* warnings: retries, redispatches */
3012 "<td></td><td></td>"
3013 /* server status : reflect frontend status */
3014 "<td class=ac>%s</td>"
3015 /* rest of server: nothing */
3016 "<td class=ac colspan=8></td></tr>"
3017 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003018 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
3019 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003020 px->state == PR_STREADY ? "OPEN" :
3021 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003022 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003023 else { /* CSV mode */
3024 chunk_appendf(&trash,
3025 /* pxid, name, queue cur, queue max, */
3026 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003027 /* sessions : current, max, limit, total */
3028 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003029 /* bytes : in, out */
3030 "%lld,%lld,"
3031 /* denied: req, resp */
3032 "%lld,%lld,"
3033 /* errors : request, connect, response */
3034 "%lld,,,"
3035 /* warnings: retries, redispatches */
3036 ",,"
3037 /* server status : reflect frontend status */
3038 "%s,"
3039 /* rest of server: nothing */
3040 ",,,,,,,,"
3041 /* pid, iid, sid, throttle, lbtot, tracked, type */
3042 "%d,%d,0,,,,%d,"
3043 /* rate, rate_lim, rate_max */
3044 "%u,%u,%u,"
3045 /* check_status, check_code, check_duration */
3046 ",,,",
3047 px->id,
3048 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
3049 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
3050 px->fe_counters.denied_req, px->fe_counters.denied_resp,
3051 px->fe_counters.failed_req,
3052 px->state == PR_STREADY ? "OPEN" :
3053 px->state == PR_STFULL ? "FULL" : "STOP",
3054 relative_pid, px->uuid, STATS_TYPE_FE,
3055 read_freq_ctr(&px->fe_sess_per_sec),
3056 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003057
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003058 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3059 if (px->mode == PR_MODE_HTTP) {
3060 for (i=1; i<6; i++)
3061 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
3062 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
3063 }
3064 else
3065 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003066
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003067 /* failed health analyses */
3068 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003069
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003070 /* requests : req_rate, req_rate_max, req_tot, */
3071 chunk_appendf(&trash, "%u,%u,%lld,",
3072 read_freq_ctr(&px->fe_req_per_sec),
3073 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
3074
3075 /* errors: cli_aborts, srv_aborts */
3076 chunk_appendf(&trash, ",,");
3077
3078 /* compression: in, out, bypassed */
3079 chunk_appendf(&trash, "%lld,%lld,%lld,",
3080 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
3081
3082 /* compression: comp_rsp */
3083 chunk_appendf(&trash, "%lld,",
3084 px->fe_counters.p.http.comp_rsp);
3085
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003086 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3087 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003088
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003089 /* finish with EOL */
3090 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003091 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003092 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003093}
3094
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003095/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
3096 * from stream interface <si>, and stats flags <flags>. The caller is responsible
3097 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3098 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02003099 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003100static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02003101{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003102 struct appctx *appctx = __objt_appctx(si->end);
3103
3104 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003105 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003106 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003107 /* Column sub-heading for Enable or Disable server */
3108 chunk_appendf(&trash, "<td></td>");
3109 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003110 chunk_appendf(&trash,
3111 /* frontend name, listener name */
3112 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3113 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3114 "",
3115 px->id, l->name,
3116 (flags & ST_SHLGNDS)?"<u>":"",
3117 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02003118
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003119 if (flags & ST_SHLGNDS) {
3120 char str[INET6_ADDRSTRLEN];
3121 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02003122
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003123 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02003124
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003125 port = get_host_port(&l->addr);
3126 switch (addr_to_str(&l->addr, str, sizeof(str))) {
3127 case AF_INET:
3128 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
3129 break;
3130 case AF_INET6:
3131 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
3132 break;
3133 case AF_UNIX:
3134 chunk_appendf(&trash, "unix, ");
3135 break;
3136 case -1:
3137 chunk_appendf(&trash, "(%s), ", strerror(errno));
3138 break;
3139 }
Willy Tarreau91861262007-10-17 17:06:05 +02003140
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003141 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003142 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003143 }
Willy Tarreau91861262007-10-17 17:06:05 +02003144
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003145 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003146 /* queue */
3147 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003148 /* sessions rate: current, max, limit */
3149 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003150 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003151 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003152 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003153 /* bytes: in, out */
3154 "<td>%s</td><td>%s</td>"
3155 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003156 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003157 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
3158 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02003159
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003160 chunk_appendf(&trash,
3161 /* denied: req, resp */
3162 "<td>%s</td><td>%s</td>"
3163 /* errors: request, connect, response */
3164 "<td>%s</td><td></td><td></td>"
3165 /* warnings: retries, redispatches */
3166 "<td></td><td></td>"
3167 /* server status: reflect listener status */
3168 "<td class=ac>%s</td>"
3169 /* rest of server: nothing */
3170 "<td class=ac colspan=8></td></tr>"
3171 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003172 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
3173 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003174 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
3175 }
3176 else { /* CSV mode */
3177 chunk_appendf(&trash,
3178 /* pxid, name, queue cur, queue max, */
3179 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003180 /* sessions: current, max, limit, total */
3181 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003182 /* bytes: in, out */
3183 "%lld,%lld,"
3184 /* denied: req, resp */
3185 "%lld,%lld,"
3186 /* errors: request, connect, response */
3187 "%lld,,,"
3188 /* warnings: retries, redispatches */
3189 ",,"
3190 /* server status: reflect listener status */
3191 "%s,"
3192 /* rest of server: nothing */
3193 ",,,,,,,,"
3194 /* pid, iid, sid, throttle, lbtot, tracked, type */
3195 "%d,%d,%d,,,,%d,"
3196 /* rate, rate_lim, rate_max */
3197 ",,,"
3198 /* check_status, check_code, check_duration */
3199 ",,,"
3200 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3201 ",,,,,,"
3202 /* failed health analyses */
3203 ","
3204 /* requests : req_rate, req_rate_max, req_tot, */
3205 ",,,"
3206 /* errors: cli_aborts, srv_aborts */
3207 ",,"
3208 /* compression: in, out, bypassed, comp_rsp */
3209 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003210 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3211 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003212 "\n",
3213 px->id, l->name,
3214 l->nbconn, l->counters->conn_max,
3215 l->maxconn, l->counters->cum_conn,
3216 l->counters->bytes_in, l->counters->bytes_out,
3217 l->counters->denied_req, l->counters->denied_resp,
3218 l->counters->failed_req,
3219 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
3220 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
3221 }
3222 return 1;
3223}
Willy Tarreau91861262007-10-17 17:06:05 +02003224
Simon Horman4d2eab62015-04-23 14:51:26 +09003225enum srv_stats_state {
3226 SRV_STATS_STATE_DOWN = 0,
3227 SRV_STATS_STATE_DOWN_AGENT,
3228 SRV_STATS_STATE_GOING_UP,
3229 SRV_STATS_STATE_UP_GOING_DOWN,
3230 SRV_STATS_STATE_UP,
3231 SRV_STATS_STATE_NOLB_GOING_DOWN,
3232 SRV_STATS_STATE_NOLB,
3233 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3234 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003235 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003236 SRV_STATS_STATE_NO_CHECK,
3237
3238 SRV_STATS_STATE_COUNT, /* Must be last */
3239};
3240
Simon Horman837bfa72015-04-23 14:51:27 +09003241enum srv_stats_colour {
3242 SRV_STATS_COLOUR_DOWN = 0,
3243 SRV_STATS_COLOUR_GOING_UP,
3244 SRV_STATS_COLOUR_GOING_DOWN,
3245 SRV_STATS_COLOUR_UP,
3246 SRV_STATS_COLOUR_NOLB,
3247 SRV_STATS_COLOUR_DRAINING,
3248 SRV_STATS_COLOUR_NO_CHECK,
3249
3250 SRV_STATS_COLOUR_COUNT, /* Must be last */
3251};
3252
3253static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3254 [SRV_STATS_COLOUR_DOWN] = "down",
3255 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3256 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3257 [SRV_STATS_COLOUR_UP] = "up",
3258 [SRV_STATS_COLOUR_NOLB] = "nolb",
3259 [SRV_STATS_COLOUR_DRAINING] = "draining",
3260 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3261};
3262
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003263/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3264 * from stream interface <si>, stats flags <flags>, and server state <state>.
3265 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003266 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003267 */
Simon Horman4d2eab62015-04-23 14:51:26 +09003268static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09003269 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003270{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003271 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003272 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003273 char str[INET6_ADDRSTRLEN];
3274 struct chunk src;
3275 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02003276
Willy Tarreau32091232014-05-16 13:52:00 +02003277 /* we have "via" which is the tracked server as described in the configuration,
3278 * and "ref" which is the checked server and the end of the chain.
3279 */
3280 via = sv->track ? sv->track : sv;
3281 ref = via;
3282 while (ref->track)
3283 ref = ref->track;
3284
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003285 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003286 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3287 [SRV_STATS_STATE_DOWN] = "DOWN",
3288 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3289 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
3290 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
3291 [SRV_STATS_STATE_UP] = "UP",
3292 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
3293 [SRV_STATS_STATE_NOLB] = "NOLB",
3294 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
3295 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003296 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003297 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003298 };
Willy Tarreau91861262007-10-17 17:06:05 +02003299
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003300 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003301 chunk_appendf(&trash, "<tr class=\"maintain\">");
3302 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003303 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003304 "<tr class=\"%s_%s\">",
3305 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003306
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003307 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003308 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003309 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3310 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003311
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003312 chunk_appendf(&trash,
3313 "<td class=ac><a name=\"%s/%s\"></a>%s"
3314 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3315 "",
3316 px->id, sv->id,
3317 (flags & ST_SHLGNDS) ? "<u>" : "",
3318 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003319
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003320 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003321 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003322
3323 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3324 case AF_INET:
3325 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3326 break;
3327 case AF_INET6:
3328 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3329 break;
3330 case AF_UNIX:
3331 chunk_appendf(&trash, "unix, ");
3332 break;
3333 case -1:
3334 chunk_appendf(&trash, "(%s), ", strerror(errno));
3335 break;
3336 default: /* address family not supported */
3337 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003338 }
Willy Tarreau91861262007-10-17 17:06:05 +02003339
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003340 /* id */
3341 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003342
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 /* cookie */
3344 if (sv->cookie) {
3345 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003346
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003347 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3348 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003349
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003350 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003351 }
3352
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003353 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003354 }
Willy Tarreau91861262007-10-17 17:06:05 +02003355
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003356 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003357 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003358 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003359 /* sessions rate : current, max, limit */
3360 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003361 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003362 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003363 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3364 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003365
Willy Tarreau466c9b52012-12-23 02:25:03 +01003366
3367 chunk_appendf(&trash,
3368 /* sessions: current, max, limit, total */
3369 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003370 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003371 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3372 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003373 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3374 U2H(sv->counters.cum_sess),
3375 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003376
Willy Tarreau466c9b52012-12-23 02:25:03 +01003377 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3378 if (px->mode == PR_MODE_HTTP) {
3379 unsigned long long tot;
3380 for (tot = i = 0; i < 6; i++)
3381 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003382
Willy Tarreau466c9b52012-12-23 02:25:03 +01003383 chunk_appendf(&trash,
3384 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3385 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3386 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3387 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3388 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3389 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3390 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3391 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003392 U2H(tot),
3393 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3394 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3395 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3396 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3397 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3398 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 +02003399 }
Willy Tarreau91861262007-10-17 17:06:05 +02003400
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003401 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3402 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)));
3403 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)));
3404 if (px->mode == PR_MODE_HTTP)
3405 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)));
3406 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)));
3407
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003408 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003409 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003410 /* sessions: lbtot, last */
3411 "<td>%s</td><td>%s</td>",
3412 U2H(sv->counters.cum_lbconn),
3413 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003414
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003415 chunk_appendf(&trash,
3416 /* bytes : in, out */
3417 "<td>%s</td><td>%s</td>"
3418 /* denied: req, resp */
3419 "<td></td><td>%s</td>"
3420 /* errors : request, connect */
3421 "<td></td><td>%s</td>"
3422 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003423 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003424 /* warnings: retries, redispatches */
3425 "<td>%lld</td><td>%lld</td>"
3426 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003427 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3428 U2H(sv->counters.failed_secu),
3429 U2H(sv->counters.failed_conns),
3430 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003431 sv->counters.cli_aborts,
3432 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003433 sv->counters.retries, sv->counters.redispatches);
3434
3435 /* status, lest check */
3436 chunk_appendf(&trash, "<td class=ac>");
3437
Willy Tarreau20125212014-05-13 19:44:56 +02003438 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003439 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3440 chunk_appendf(&trash, "MAINT");
3441 }
Simon Horman0766e442014-11-12 15:55:54 +09003442 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003443 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003444 /* DOWN (agent) */
3445 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 +02003446 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003447 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003448 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3449 chunk_appendf(&trash,
3450 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003451 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3452 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003453 }
Willy Tarreau91861262007-10-17 17:06:05 +02003454
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003455 if ((sv->state == SRV_ST_STOPPED) &&
3456 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3457 chunk_appendf(&trash,
3458 "</td><td class=ac><u> %s%s",
3459 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3460 get_check_status_info(sv->agent.status));
3461
3462 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3463 chunk_appendf(&trash, "/%d", sv->agent.code);
3464
3465 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3466 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3467
3468 chunk_appendf(&trash, "<div class=tips>%s",
3469 get_check_status_description(sv->agent.status));
3470 if (*sv->agent.desc) {
3471 chunk_appendf(&trash, ": ");
3472 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3473 chunk_htmlencode(&trash, &src);
3474 }
3475 chunk_appendf(&trash, "</div></u>");
3476 }
3477 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003478 chunk_appendf(&trash,
3479 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003480 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003481 get_check_status_info(sv->check.status));
3482
3483 if (sv->check.status >= HCHK_STATUS_L57DATA)
3484 chunk_appendf(&trash, "/%d", sv->check.code);
3485
3486 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003487 chunk_appendf(&trash, " in %lums", sv->check.duration);
3488
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003489 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003490 get_check_status_description(sv->check.status));
3491 if (*sv->check.desc) {
3492 chunk_appendf(&trash, ": ");
3493 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3494 chunk_htmlencode(&trash, &src);
3495 }
3496 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003497 }
3498 else
3499 chunk_appendf(&trash, "</td><td>");
3500
3501 chunk_appendf(&trash,
3502 /* weight */
3503 "</td><td class=ac>%d</td>"
3504 /* act, bck */
3505 "<td class=ac>%s</td><td class=ac>%s</td>"
3506 "",
3507 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003508 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3509 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003510
3511 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003512 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003513 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003514
3515 if (ref->observe)
3516 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3517
3518 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003519 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003520 "<td>%lld</td><td>%s</td>"
3521 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003522 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003523 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3524 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003525 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3526 /* tracking a server */
3527 chunk_appendf(&trash,
3528 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003529 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003530 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003531 else
3532 chunk_appendf(&trash, "<td colspan=3></td>");
3533
3534 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003535 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003536 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003537 else
3538 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3539 }
3540 else { /* CSV mode */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003541 struct chunk *out = get_trash_chunk();
Simon Horman4d2eab62015-04-23 14:51:26 +09003542 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3543 [SRV_STATS_STATE_DOWN] = "DOWN,",
3544 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3545 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3546 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3547 [SRV_STATS_STATE_UP] = "UP,",
3548 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3549 [SRV_STATS_STATE_NOLB] = "NOLB,",
3550 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3551 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003552 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003553 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003554 };
3555
3556 chunk_appendf(&trash,
3557 /* pxid, name */
3558 "%s,%s,"
3559 /* queue : current, max */
3560 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003561 /* sessions : current, max, limit, total */
3562 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003563 /* bytes : in, out */
3564 "%lld,%lld,"
3565 /* denied: req, resp */
3566 ",%lld,"
3567 /* errors : request, connect, response */
3568 ",%lld,%lld,"
3569 /* warnings: retries, redispatches */
3570 "%lld,%lld,"
3571 "",
3572 px->id, sv->id,
3573 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003574 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003575 sv->counters.bytes_in, sv->counters.bytes_out,
3576 sv->counters.failed_secu,
3577 sv->counters.failed_conns, sv->counters.failed_resp,
3578 sv->counters.retries, sv->counters.redispatches);
3579
3580 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003581 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003582 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003583 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003584 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003585 else
3586 chunk_appendf(&trash,
3587 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003588 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3589 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003590
3591 chunk_appendf(&trash,
3592 /* weight, active, backup */
3593 "%d,%d,%d,"
3594 "",
3595 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003596 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3597 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003598
3599 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003600 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003601 chunk_appendf(&trash,
3602 "%lld,%lld,%d,%d,",
3603 sv->counters.failed_checks, sv->counters.down_trans,
3604 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3605 else
3606 chunk_appendf(&trash, ",,,,");
3607
3608 /* queue limit, pid, iid, sid, */
3609 chunk_appendf(&trash,
3610 "%s,"
3611 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003612 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003613 relative_pid, px->uuid, sv->puid);
3614
3615 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003616 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003617 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003618
3619 /* sessions: lbtot */
3620 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3621
3622 /* tracked */
3623 if (sv->track)
3624 chunk_appendf(&trash, "%s/%s,",
3625 sv->track->proxy->id, sv->track->id);
3626 else
3627 chunk_appendf(&trash, ",");
3628
3629 /* type */
3630 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3631
3632 /* rate */
3633 chunk_appendf(&trash, "%u,,%u,",
3634 read_freq_ctr(&sv->sess_per_sec),
3635 sv->counters.sps_max);
3636
Willy Tarreauff5ae352013-12-11 20:36:34 +01003637 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003638 /* check_status */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003639 chunk_appendf(&trash, "%s,", csv_enc(get_check_status_info(sv->check.status), 1, out));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003640
3641 /* check_code */
3642 if (sv->check.status >= HCHK_STATUS_L57DATA)
3643 chunk_appendf(&trash, "%u,", sv->check.code);
3644 else
3645 chunk_appendf(&trash, ",");
3646
3647 /* check_duration */
3648 if (sv->check.status >= HCHK_STATUS_CHECKED)
3649 chunk_appendf(&trash, "%lu,", sv->check.duration);
3650 else
3651 chunk_appendf(&trash, ",");
3652
3653 }
3654 else
3655 chunk_appendf(&trash, ",,,");
3656
3657 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3658 if (px->mode == PR_MODE_HTTP) {
3659 for (i=1; i<6; i++)
3660 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3661
3662 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3663 }
3664 else
3665 chunk_appendf(&trash, ",,,,,,");
3666
3667 /* failed health analyses */
3668 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3669
3670 /* requests : req_rate, req_rate_max, req_tot, */
3671 chunk_appendf(&trash, ",,,");
3672
3673 /* errors: cli_aborts, srv_aborts */
3674 chunk_appendf(&trash, "%lld,%lld,",
3675 sv->counters.cli_aborts, sv->counters.srv_aborts);
3676
3677 /* compression: in, out, bypassed, comp_rsp */
3678 chunk_appendf(&trash, ",,,,");
3679
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003680 /* lastsess */
3681 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3682
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003683 /* capture of last check and agent statuses */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003684 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->check.desc), 1, out) : "");
3685 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 +02003686
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003687 /* qtime, ctime, rtime, ttime, */
3688 chunk_appendf(&trash, "%u,%u,%u,%u,",
3689 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3690 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3691 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3692 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3693
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003694 /* finish with EOL */
3695 chunk_appendf(&trash, "\n");
3696 }
3697 return 1;
3698}
3699
3700/* Dumps a line for backend <px> to the trash for and uses the state from stream
3701 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3702 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3703 */
3704static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3705{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003706 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003707 struct chunk src;
3708 int i;
3709
3710 if (!(px->cap & PR_CAP_BE))
3711 return 0;
3712
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003713 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003714 return 0;
3715
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003716 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003717 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003718 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003719 /* Column sub-heading for Enable or Disable server */
3720 chunk_appendf(&trash, "<td></td>");
3721 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003722 chunk_appendf(&trash,
3723 "<td class=ac>"
3724 /* name */
3725 "%s<a name=\"%s/Backend\"></a>"
3726 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3727 "",
3728 (flags & ST_SHLGNDS)?"<u>":"",
3729 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003730
3731 if (flags & ST_SHLGNDS) {
3732 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003733 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003734 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3735
3736 /* cookie */
3737 if (px->cookie_name) {
3738 chunk_appendf(&trash, ", cookie: '");
3739 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3740 chunk_htmlencode(&trash, &src);
3741 chunk_appendf(&trash, "'");
3742 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003743 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003744 }
3745
3746 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003747 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003748 /* queue : current, max */
3749 "<td>%s</td><td>%s</td><td></td>"
3750 /* sessions rate : current, max, limit */
3751 "<td>%s</td><td>%s</td><td></td>"
3752 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003753 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003754 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3755 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003756
3757 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003758 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003759 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003760 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003761 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003762 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003763 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3764 U2H(px->be_counters.cum_conn),
3765 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003766
Willy Tarreau466c9b52012-12-23 02:25:03 +01003767 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003768 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003769 chunk_appendf(&trash,
3770 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3771 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3772 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3773 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3774 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3775 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3776 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3777 "<tr><th>- other responses:</th><td>%s</td></tr>"
3778 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003779 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003780 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003781 U2H(px->be_counters.p.http.cum_req),
3782 U2H(px->be_counters.p.http.rsp[1]),
3783 U2H(px->be_counters.p.http.rsp[2]),
3784 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003785 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003786 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003787 U2H(px->be_counters.p.http.rsp[3]),
3788 U2H(px->be_counters.p.http.rsp[4]),
3789 U2H(px->be_counters.p.http.rsp[5]),
3790 U2H(px->be_counters.p.http.rsp[0]),
3791 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003792 }
3793
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003794 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)));
3795 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)));
3796 if (px->mode == PR_MODE_HTTP)
3797 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)));
3798 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)));
3799
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003800 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003801 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003802 /* sessions: lbtot, last */
3803 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003804 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003805 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003806 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003807 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003808 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003809 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003810
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003811 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003812 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003813 "<td>%s%s<div class=tips><table class=det>"
3814 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3815 "<tr><th>Compression in:</th><td>%s</td></tr>"
3816 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3817 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3818 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3819 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003820 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003821 U2H(px->be_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003822 U2H(px->be_counters.bytes_out),
3823 U2H(px->be_counters.comp_in),
3824 U2H(px->be_counters.comp_out),
3825 px->be_counters.comp_in ? (int)(px->be_counters.comp_out * 100 / px->be_counters.comp_in) : 0,
3826 U2H(px->be_counters.comp_byp),
3827 U2H(px->be_counters.comp_in - px->be_counters.comp_out),
3828 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 +01003829 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3830
3831 chunk_appendf(&trash,
3832 /* denied: req, resp */
3833 "<td>%s</td><td>%s</td>"
3834 /* errors : request, connect */
3835 "<td></td><td>%s</td>"
3836 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003837 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003838 /* warnings: retries, redispatches */
3839 "<td>%lld</td><td>%lld</td>"
3840 /* backend status: reflect backend status (up/down): we display UP
3841 * if the backend has known working servers or if it has no server at
3842 * all (eg: for stats). Then we display the total weight, number of
3843 * active and backups. */
3844 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3845 "<td class=ac>%d</td><td class=ac>%d</td>"
3846 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003847 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3848 U2H(px->be_counters.failed_conns),
3849 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003850 px->be_counters.cli_aborts,
3851 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003852 px->be_counters.retries, px->be_counters.redispatches,
3853 human_time(now.tv_sec - px->last_change, 1),
3854 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3855 "<font color=\"red\"><b>DOWN</b></font>",
3856 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3857 px->srv_act, px->srv_bck);
3858
3859 chunk_appendf(&trash,
3860 /* rest of backend: nothing, down transitions, total downtime, throttle */
3861 "<td class=ac>&nbsp;</td><td>%d</td>"
3862 "<td>%s</td>"
3863 "<td></td>"
3864 "</tr>",
3865 px->down_trans,
3866 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3867 }
3868 else { /* CSV mode */
3869 chunk_appendf(&trash,
3870 /* pxid, name */
3871 "%s,BACKEND,"
3872 /* queue : current, max */
3873 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003874 /* sessions : current, max, limit, total */
3875 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003876 /* bytes : in, out */
3877 "%lld,%lld,"
3878 /* denied: req, resp */
3879 "%lld,%lld,"
3880 /* errors : request, connect, response */
3881 ",%lld,%lld,"
3882 /* warnings: retries, redispatches */
3883 "%lld,%lld,"
3884 /* backend status: reflect backend status (up/down): we display UP
3885 * if the backend has known working servers or if it has no server at
3886 * all (eg: for stats). Then we display the total weight, number of
3887 * active and backups. */
3888 "%s,"
3889 "%d,%d,%d,"
3890 /* rest of backend: nothing, down transitions, last change, total downtime */
3891 ",%d,%d,%d,,"
3892 /* pid, iid, sid, throttle, lbtot, tracked, type */
3893 "%d,%d,0,,%lld,,%d,"
3894 /* rate, rate_lim, rate_max, */
3895 "%u,,%u,"
3896 /* check_status, check_code, check_duration */
3897 ",,,",
3898 px->id,
3899 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3900 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3901 px->be_counters.bytes_in, px->be_counters.bytes_out,
3902 px->be_counters.denied_req, px->be_counters.denied_resp,
3903 px->be_counters.failed_conns, px->be_counters.failed_resp,
3904 px->be_counters.retries, px->be_counters.redispatches,
3905 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3906 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3907 px->srv_act, px->srv_bck,
3908 px->down_trans, (int)(now.tv_sec - px->last_change),
3909 px->srv?be_downtime(px):0,
3910 relative_pid, px->uuid,
3911 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3912 read_freq_ctr(&px->be_sess_per_sec),
3913 px->be_counters.sps_max);
3914
3915 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3916 if (px->mode == PR_MODE_HTTP) {
3917 for (i=1; i<6; i++)
3918 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3919 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3920 }
3921 else
3922 chunk_appendf(&trash, ",,,,,,");
3923
3924 /* failed health analyses */
3925 chunk_appendf(&trash, ",");
3926
3927 /* requests : req_rate, req_rate_max, req_tot, */
3928 chunk_appendf(&trash, ",,,");
3929
3930 /* errors: cli_aborts, srv_aborts */
3931 chunk_appendf(&trash, "%lld,%lld,",
3932 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3933
3934 /* compression: in, out, bypassed */
3935 chunk_appendf(&trash, "%lld,%lld,%lld,",
3936 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3937
3938 /* compression: comp_rsp */
3939 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3940
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003941 /* lastsess, last_chk, last_agt, */
3942 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003943
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003944 /* qtime, ctime, rtime, ttime, */
3945 chunk_appendf(&trash, "%u,%u,%u,%u,",
3946 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3947 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3948 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3949 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3950
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003951 /* finish with EOL */
3952 chunk_appendf(&trash, "\n");
3953 }
3954 return 1;
3955}
3956
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003957/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003958 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003959 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003960 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003961static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003962{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003963 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003964 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3965
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003966 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003967 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003968
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003969 /* 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 +02003970 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003971 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003972 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003973 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 +01003974 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003975 }
3976
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003977 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05003978 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003979 }
3980
3981 /* print a new table */
3982 chunk_appendf(&trash,
3983 "<table class=\"tbl\" width=\"100%%\">\n"
3984 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003985 "<th class=\"pxname\" width=\"10%%\">");
3986
3987 chunk_appendf(&trash,
3988 "<a name=\"%s\"></a>%s"
3989 "<a class=px href=\"#%s\">%s</a>",
3990 px->id,
3991 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3992 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003993
3994 if (uri->flags & ST_SHLGNDS) {
3995 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003996 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003997 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3998 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003999 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004000 }
4001
4002 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004003 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004004 "<th class=\"%s\" width=\"90%%\">%s</th>"
4005 "</tr>\n"
4006 "</table>\n"
4007 "<table class=\"tbl\" width=\"100%%\">\n"
4008 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004009 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4010 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4011
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004012 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004013 /* Column heading for Enable or Disable server */
4014 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004015 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004016
4017 chunk_appendf(&trash,
4018 "<th rowspan=2></th>"
4019 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004020 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004021 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4022 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4023 "<th colspan=9>Server</th>"
4024 "</tr>\n"
4025 "<tr class=\"titre\">"
4026 "<th>Cur</th><th>Max</th><th>Limit</th>"
4027 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004028 "<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 +01004029 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4030 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4031 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4032 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4033 "<th>Thrtle</th>\n"
4034 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004035}
4036
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004037/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004038 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004039 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004040static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004041{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004042 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004043 chunk_appendf(&trash, "</table>");
4044
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004045 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004046 /* close the form used to enable/disable this proxy servers */
4047 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004048 "Choose the action to perform on the checked servers : "
4049 "<select name=action>"
4050 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004051 "<option value=\"ready\">Set state to READY</option>"
4052 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004053 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004054 "<option value=\"dhlth\">Health: disable checks</option>"
4055 "<option value=\"ehlth\">Health: enable checks</option>"
4056 "<option value=\"hrunn\">Health: force UP</option>"
4057 "<option value=\"hnolb\">Health: force NOLB</option>"
4058 "<option value=\"hdown\">Health: force DOWN</option>"
4059 "<option value=\"dagent\">Agent: disable checks</option>"
4060 "<option value=\"eagent\">Agent: enable checks</option>"
4061 "<option value=\"arunn\">Agent: force UP</option>"
4062 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004063 "<option value=\"shutdown\">Kill Sessions</option>"
4064 "</select>"
4065 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4066 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4067 "</form>",
4068 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004069 }
4070
4071 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004072}
Willy Tarreau91861262007-10-17 17:06:05 +02004073
4074/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004075 * Dumps statistics for a proxy. The output is sent to the stream interface's
4076 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4077 * buffer space, or non-zero if everything completed. This function is used
4078 * both by the CLI and the HTTP entry points, and is able to dump the output
4079 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004080 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004081static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004082{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004083 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004084 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004085 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004086 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004087 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02004088
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004089 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004090
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004091 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004092 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004093 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004094 if (uri && uri->scope) {
4095 /* we have a limited scope, we have to check the proxy name */
4096 struct stat_scope *scope;
4097 int len;
4098
4099 len = strlen(px->id);
4100 scope = uri->scope;
4101
4102 while (scope) {
4103 /* match exact proxy name */
4104 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4105 break;
4106
4107 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004108 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004109 break;
4110 scope = scope->next;
4111 }
4112
4113 /* proxy name not found : don't dump anything */
4114 if (scope == NULL)
4115 return 1;
4116 }
4117
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004118 /* if the user has requested a limited output and the proxy
4119 * name does not match, skip it.
4120 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004121 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004122 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 +02004123 return 1;
4124
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004125 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4126 (appctx->ctx.stats.iid != -1) &&
4127 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004128 return 1;
4129
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004130 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004131 /* fall through */
4132
Willy Tarreau295a8372011-03-10 11:25:07 +01004133 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004134 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004135 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004136 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004137 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004138 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004139 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004140 }
Willy Tarreau91861262007-10-17 17:06:05 +02004141
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004142 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004143 /* fall through */
4144
Willy Tarreau295a8372011-03-10 11:25:07 +01004145 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004146 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004147 if (stats_dump_fe_stats(si, px)) {
4148 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004149 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004150 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004151 }
4152 }
Willy Tarreau91861262007-10-17 17:06:05 +02004153
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004154 appctx->ctx.stats.l = px->conf.listeners.n;
4155 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004156 /* fall through */
4157
Willy Tarreau295a8372011-03-10 11:25:07 +01004158 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004159 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004160 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004161 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004162 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004163 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004164 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004165
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004166 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004167 if (!l->counters)
4168 continue;
4169
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004170 if (appctx->ctx.stats.flags & STAT_BOUND) {
4171 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004172 break;
4173
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004174 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004175 continue;
4176 }
4177
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004178 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004179 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
4180 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004181 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004182 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004183 }
4184 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004185 }
4186
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004187 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4188 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004189 /* fall through */
4190
Willy Tarreau295a8372011-03-10 11:25:07 +01004191 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004192 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004193 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004194 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09004195 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02004196
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004197 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004198 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004199 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004200 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004201
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004202 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004203
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004204 if (appctx->ctx.stats.flags & STAT_BOUND) {
4205 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004206 break;
4207
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004208 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004209 continue;
4210 }
4211
Willy Tarreau32091232014-05-16 13:52:00 +02004212 svs = sv;
4213 while (svs->track)
4214 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004215
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004216 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004217 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09004218 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004219 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004220 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4221 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004222 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004223 sv_colour = SRV_STATS_COLOUR_UP;
4224 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004225
Simon Horman4463d192015-04-23 14:51:28 +09004226 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
4227 sv_colour = SRV_STATS_COLOUR_DRAINING;
4228
4229 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09004230 if (svs->agent.state & CHK_ST_ENABLED)
4231 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
4232 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09004233 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09004234 else
Simon Horman4d2eab62015-04-23 14:51:26 +09004235 sv_state = SRV_STATS_STATE_DRAIN;
4236 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004237
Simon Horman837bfa72015-04-23 14:51:27 +09004238 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004239 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09004240 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
4241 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004242 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004243 else if (sv->state == SRV_ST_STOPPING) {
4244 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09004245 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004246 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09004247 sv_colour = SRV_STATS_COLOUR_NOLB;
4248 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004249 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004250 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4251 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004252 }
4253 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09004254 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004255 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09004256 sv_colour = SRV_STATS_COLOUR_DOWN;
4257 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004258 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09004259 sv_colour = SRV_STATS_COLOUR_DOWN;
4260 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004261 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004262 sv_colour = SRV_STATS_COLOUR_GOING_UP;
4263 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004264 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09004265 sv_colour = SRV_STATS_COLOUR_DOWN;
4266 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004267 }
Willy Tarreau91861262007-10-17 17:06:05 +02004268
Willy Tarreau9638efa2014-05-23 11:19:57 +02004269 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02004270 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004271 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02004272 continue;
4273 }
4274
Simon Horman837bfa72015-04-23 14:51:27 +09004275 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004276 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004277 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004278 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004279 }
4280 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004281 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004282
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004283 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004284 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004285
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004286 case STAT_PX_ST_BE:
4287 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004288 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
4289 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004290 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004291 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004292 }
4293 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004294
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004295 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004296 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004297
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004298 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004299 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004300 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004301 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004302 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004303 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004304 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004305 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004306
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004307 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004308 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004309
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004310 case STAT_PX_ST_FIN:
4311 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004312
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004313 default:
4314 /* unknown state, we should put an abort() here ! */
4315 return 1;
4316 }
4317}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004318
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004319/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4320 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004321 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004322static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004323{
4324 /* WARNING! This must fit in the first buffer !!! */
4325 chunk_appendf(&trash,
4326 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4327 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4328 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4329 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4330 "<style type=\"text/css\"><!--\n"
4331 "body {"
4332 " font-family: arial, helvetica, sans-serif;"
4333 " font-size: 12px;"
4334 " font-weight: normal;"
4335 " color: black;"
4336 " background: white;"
4337 "}\n"
4338 "th,td {"
4339 " font-size: 10px;"
4340 "}\n"
4341 "h1 {"
4342 " font-size: x-large;"
4343 " margin-bottom: 0.5em;"
4344 "}\n"
4345 "h2 {"
4346 " font-family: helvetica, arial;"
4347 " font-size: x-large;"
4348 " font-weight: bold;"
4349 " font-style: italic;"
4350 " color: #6020a0;"
4351 " margin-top: 0em;"
4352 " margin-bottom: 0em;"
4353 "}\n"
4354 "h3 {"
4355 " font-family: helvetica, arial;"
4356 " font-size: 16px;"
4357 " font-weight: bold;"
4358 " color: #b00040;"
4359 " background: #e8e8d0;"
4360 " margin-top: 0em;"
4361 " margin-bottom: 0em;"
4362 "}\n"
4363 "li {"
4364 " margin-top: 0.25em;"
4365 " margin-right: 2em;"
4366 "}\n"
4367 ".hr {margin-top: 0.25em;"
4368 " border-color: black;"
4369 " border-bottom-style: solid;"
4370 "}\n"
4371 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4372 ".total {background: #20D0D0;color: #ffff80;}\n"
4373 ".frontend {background: #e8e8d0;}\n"
4374 ".socket {background: #d0d0d0;}\n"
4375 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004376 ".active_down {background: #ff9090;}\n"
4377 ".active_going_up {background: #ffd020;}\n"
4378 ".active_going_down {background: #ffffa0;}\n"
4379 ".active_up {background: #c0ffc0;}\n"
4380 ".active_nolb {background: #20a0ff;}\n"
4381 ".active_draining {background: #20a0FF;}\n"
4382 ".active_no_check {background: #e0e0e0;}\n"
4383 ".backup_down {background: #ff9090;}\n"
4384 ".backup_going_up {background: #ff80ff;}\n"
4385 ".backup_going_down {background: #c060ff;}\n"
4386 ".backup_up {background: #b0d0ff;}\n"
4387 ".backup_nolb {background: #90b0e0;}\n"
4388 ".backup_draining {background: #cc9900;}\n"
4389 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004390 ".maintain {background: #c07820;}\n"
4391 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4392 "\n"
4393 "a.px:link {color: #ffff40; text-decoration: none;}"
4394 "a.px:visited {color: #ffff40; text-decoration: none;}"
4395 "a.px:hover {color: #ffffff; text-decoration: none;}"
4396 "a.lfsb:link {color: #000000; text-decoration: none;}"
4397 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4398 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4399 "\n"
4400 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4401 "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"
4402 "table.tbl td.ac { text-align: center;}\n"
4403 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4404 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4405 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4406 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4407 "\n"
4408 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4409 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4410 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004411 "table.det { border-collapse: collapse; border-style: none; }\n"
4412 "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 +01004413 "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 +01004414 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004415 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004416 " display:block;\n"
4417 " visibility:hidden;\n"
4418 " z-index:2147483647;\n"
4419 " position:absolute;\n"
4420 " padding:2px 4px 3px;\n"
4421 " background:#f0f060; color:#000000;\n"
4422 " border:1px solid #7040c0;\n"
4423 " white-space:nowrap;\n"
4424 " font-style:normal;font-size:11px;font-weight:normal;\n"
4425 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4426 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4427 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004428 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004429 "-->\n"
4430 "</style></head>\n",
4431 (uri->flags & ST_SHNODE) ? " on " : "",
4432 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4433 );
4434}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004435
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004436/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004437 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004438 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004439 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004440static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004441{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004442 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004443 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004444 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004445
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004446 /* WARNING! this has to fit the first packet too.
4447 * We are around 3.5 kB, add adding entries will
4448 * become tricky if we want to support 4kB buffers !
4449 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004450 chunk_appendf(&trash,
4451 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4452 PRODUCT_NAME "%s</a></h1>\n"
4453 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4454 "<hr width=\"100%%\" class=\"hr\">\n"
4455 "<h3>&gt; General process information</h3>\n"
4456 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4457 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4458 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4459 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4460 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4461 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4462 "Running tasks: %d/%d; idle = %d %%<br>\n"
4463 "</td><td align=\"center\" nowrap>\n"
4464 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004465 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4466 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004467 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004468 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4469 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004470 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004471 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4472 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004473 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004474 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4475 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004476 "</tr><tr>\n"
4477 "<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 -07004478 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004479 "<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 +01004480 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004481 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004482 "</td>"
4483 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4484 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4485 "",
4486 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4487 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4488 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4489 (uri->flags & ST_SHDESC) ? ": " : "",
4490 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4491 pid, relative_pid, global.nbproc,
4492 up / 86400, (up % 86400) / 3600,
4493 (up % 3600) / 60, (up % 60),
4494 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4495 global.rlimit_memmax ? " MB" : "",
4496 global.rlimit_nofile,
4497 global.maxsock, global.maxconn, global.maxpipes,
4498 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4499 run_queue_cur, nb_tasks_cur, idle_pct
4500 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004501
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004502 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004503 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 +01004504 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004505
4506 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004507 "<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 +01004508 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004509 STAT_SCOPE_TXT_MAXLEN);
4510
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004511 /* 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 +02004512 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004513 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004514 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004515 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 +01004516 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004517 }
4518
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004519 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004520 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004521 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004522 uri->uri_prefix,
4523 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004524 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004525 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004526 else
4527 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004528 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004529 uri->uri_prefix,
4530 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004531 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004532 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004533
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004534 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004535 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004536 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004537 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004538 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004539 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004540 "",
4541 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004542 else
4543 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004544 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004545 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004546 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004547 ";norefresh",
4548 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004549 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004550
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004551 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004552 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004553 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004554 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4555 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004556 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004557
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004558 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004559 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004560 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004561 (uri->refresh > 0) ? ";norefresh" : "",
4562 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004563
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004564 chunk_appendf(&trash,
4565 "</ul></td>"
4566 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4567 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4568 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4569 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4570 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4571 "</ul>"
4572 "</td>"
4573 "</tr></table>\n"
4574 ""
4575 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004576
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004577 if (appctx->ctx.stats.st_code) {
4578 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004579 case STAT_STATUS_DONE:
4580 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004581 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004582 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004583 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004584 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004585 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4586 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004587 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004588 break;
4589 case STAT_STATUS_NONE:
4590 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004591 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004592 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004593 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004594 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004595 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4596 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004597 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004598 break;
4599 case STAT_STATUS_PART:
4600 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004601 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004602 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004603 "Action partially processed.<br>"
4604 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004605 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004606 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4607 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004608 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004609 break;
4610 case STAT_STATUS_ERRP:
4611 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004612 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004613 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004614 "Action not processed because of invalid parameters."
4615 "<ul>"
4616 "<li>The action is maybe unknown.</li>"
4617 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4618 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4619 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004620 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004621 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4622 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004623 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004624 break;
4625 case STAT_STATUS_EXCD:
4626 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004627 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004628 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004629 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4630 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004631 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004632 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4633 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004634 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004635 break;
4636 case STAT_STATUS_DENY:
4637 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004638 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004639 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004640 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004641 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004642 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4643 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004644 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004645 break;
4646 default:
4647 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004648 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004649 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004650 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004651 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004652 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4653 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004654 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004655 }
4656 chunk_appendf(&trash, "<p>\n");
4657 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004658}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004659
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004660/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4661 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004662 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004663static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004664{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004665 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004666}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004667
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004668/* This function dumps statistics onto the stream interface's read buffer in
4669 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004670 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4671 * 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 +02004672 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004673 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004674 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004675static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004676{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004677 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004678 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004679 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004680
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004681 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004682
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004683 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004684 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004685 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004686 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004687
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004688 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004689 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004690 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004691 else
4692 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004693
Willy Tarreaubc18da12015-03-13 14:00:47 +01004694 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004695 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004696 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004697 }
Willy Tarreauae526782010-03-04 20:34:23 +01004698
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004699 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004700 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004701
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004702 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004703 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004704 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004705 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004706 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004707 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004708 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004709 }
Willy Tarreau91861262007-10-17 17:06:05 +02004710
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004711 appctx->ctx.stats.px = proxy;
4712 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4713 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004714 /* fall through */
4715
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004716 case STAT_ST_LIST:
4717 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004718 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004719 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004720 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004721 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004722 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004723
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004724 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004725 /* skip the disabled proxies, global frontend and non-networked ones */
4726 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004727 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004728 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004729
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004730 appctx->ctx.stats.px = px->next;
4731 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004732 }
4733 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004734
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004735 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004736 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004737
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004738 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004739 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004740 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01004741 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004742 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004743 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004744 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004745 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004746
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004747 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004748 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004749
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004750 case STAT_ST_FIN:
4751 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004752
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004753 default:
4754 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004755 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004756 return -1;
4757 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004758}
Willy Tarreauae526782010-03-04 20:34:23 +01004759
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004760/* We reached the stats page through a POST request. The appctx is
4761 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004762 * Parse the posted data and enable/disable servers if necessary.
4763 * Returns 1 if request was parsed or zero if it needs more data.
4764 */
4765static int stats_process_http_post(struct stream_interface *si)
4766{
Willy Tarreau87b09662015-04-03 00:22:06 +02004767 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004768 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004769
4770 struct proxy *px = NULL;
4771 struct server *sv = NULL;
4772
4773 char key[LINESIZE];
4774 int action = ST_ADM_ACTION_NONE;
4775 int reprocess = 0;
4776
4777 int total_servers = 0;
4778 int altered_servers = 0;
4779
4780 char *first_param, *cur_param, *next_param, *end_params;
4781 char *st_cur_param = NULL;
4782 char *st_next_param = NULL;
4783
4784 struct chunk *temp;
4785 int reql;
4786
4787 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02004788 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004789 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004790 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004791 goto out;
4792 }
4793
Willy Tarreaueee5b512015-04-03 23:46:31 +02004794 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 +01004795 if (reql <= 0) {
4796 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004797 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004798 return 0;
4799 }
4800
4801 first_param = temp->str;
4802 end_params = temp->str + reql;
4803 cur_param = next_param = end_params;
4804 *end_params = '\0';
4805
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004806 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004807
4808 /*
4809 * Parse the parameters in reverse order to only store the last value.
4810 * From the html form, the backend and the action are at the end.
4811 */
4812 while (cur_param > first_param) {
4813 char *value;
4814 int poffset, plen;
4815
4816 cur_param--;
4817
4818 if ((*cur_param == '&') || (cur_param == first_param)) {
4819 reprocess_servers:
4820 /* Parse the key */
4821 poffset = (cur_param != first_param ? 1 : 0);
4822 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4823 if ((plen > 0) && (plen <= sizeof(key))) {
4824 strncpy(key, cur_param + poffset, plen);
4825 key[plen - 1] = '\0';
4826 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004827 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004828 goto out;
4829 }
4830
4831 /* Parse the value */
4832 value = key;
4833 while (*value != '\0' && *value != '=') {
4834 value++;
4835 }
4836 if (*value == '=') {
4837 /* Ok, a value is found, we can mark the end of the key */
4838 *value++ = '\0';
4839 }
4840 if (url_decode(key) < 0 || url_decode(value) < 0)
4841 break;
4842
4843 /* Now we can check the key to see what to do */
4844 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02004845 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004846 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004847 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004848 goto out;
4849 }
4850 }
4851 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004852 if (strcmp(value, "ready") == 0) {
4853 action = ST_ADM_ACTION_READY;
4854 }
4855 else if (strcmp(value, "drain") == 0) {
4856 action = ST_ADM_ACTION_DRAIN;
4857 }
4858 else if (strcmp(value, "maint") == 0) {
4859 action = ST_ADM_ACTION_MAINT;
4860 }
4861 else if (strcmp(value, "shutdown") == 0) {
4862 action = ST_ADM_ACTION_SHUTDOWN;
4863 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004864 else if (strcmp(value, "dhlth") == 0) {
4865 action = ST_ADM_ACTION_DHLTH;
4866 }
4867 else if (strcmp(value, "ehlth") == 0) {
4868 action = ST_ADM_ACTION_EHLTH;
4869 }
4870 else if (strcmp(value, "hrunn") == 0) {
4871 action = ST_ADM_ACTION_HRUNN;
4872 }
4873 else if (strcmp(value, "hnolb") == 0) {
4874 action = ST_ADM_ACTION_HNOLB;
4875 }
4876 else if (strcmp(value, "hdown") == 0) {
4877 action = ST_ADM_ACTION_HDOWN;
4878 }
4879 else if (strcmp(value, "dagent") == 0) {
4880 action = ST_ADM_ACTION_DAGENT;
4881 }
4882 else if (strcmp(value, "eagent") == 0) {
4883 action = ST_ADM_ACTION_EAGENT;
4884 }
4885 else if (strcmp(value, "arunn") == 0) {
4886 action = ST_ADM_ACTION_ARUNN;
4887 }
4888 else if (strcmp(value, "adown") == 0) {
4889 action = ST_ADM_ACTION_ADOWN;
4890 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004891 /* else these are the old supported methods */
4892 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004893 action = ST_ADM_ACTION_DISABLE;
4894 }
4895 else if (strcmp(value, "enable") == 0) {
4896 action = ST_ADM_ACTION_ENABLE;
4897 }
4898 else if (strcmp(value, "stop") == 0) {
4899 action = ST_ADM_ACTION_STOP;
4900 }
4901 else if (strcmp(value, "start") == 0) {
4902 action = ST_ADM_ACTION_START;
4903 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004904 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004905 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004906 goto out;
4907 }
4908 }
4909 else if (strcmp(key, "s") == 0) {
4910 if (!(px && action)) {
4911 /*
4912 * Indicates that we'll need to reprocess the parameters
4913 * as soon as backend and action are known
4914 */
4915 if (!reprocess) {
4916 st_cur_param = cur_param;
4917 st_next_param = next_param;
4918 }
4919 reprocess = 1;
4920 }
4921 else if ((sv = findserver(px, value)) != NULL) {
4922 switch (action) {
4923 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004924 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004925 altered_servers++;
4926 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004927 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004928 }
4929 break;
4930 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004931 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004932 altered_servers++;
4933 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004934 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004935 }
4936 break;
4937 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004938 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4939 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4940 altered_servers++;
4941 total_servers++;
4942 }
4943 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004944 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004945 if (sv->admin & SRV_ADMF_FDRAIN) {
4946 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4947 altered_servers++;
4948 total_servers++;
4949 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004950 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004951 case ST_ADM_ACTION_DHLTH:
4952 if (sv->check.state & CHK_ST_CONFIGURED) {
4953 sv->check.state &= ~CHK_ST_ENABLED;
4954 altered_servers++;
4955 total_servers++;
4956 }
4957 break;
4958 case ST_ADM_ACTION_EHLTH:
4959 if (sv->check.state & CHK_ST_CONFIGURED) {
4960 sv->check.state |= CHK_ST_ENABLED;
4961 altered_servers++;
4962 total_servers++;
4963 }
4964 break;
4965 case ST_ADM_ACTION_HRUNN:
4966 if (!(sv->track)) {
4967 sv->check.health = sv->check.rise + sv->check.fall - 1;
4968 srv_set_running(sv, "changed from Web interface");
4969 altered_servers++;
4970 total_servers++;
4971 }
4972 break;
4973 case ST_ADM_ACTION_HNOLB:
4974 if (!(sv->track)) {
4975 sv->check.health = sv->check.rise + sv->check.fall - 1;
4976 srv_set_stopping(sv, "changed from Web interface");
4977 altered_servers++;
4978 total_servers++;
4979 }
4980 break;
4981 case ST_ADM_ACTION_HDOWN:
4982 if (!(sv->track)) {
4983 sv->check.health = 0;
4984 srv_set_stopped(sv, "changed from Web interface");
4985 altered_servers++;
4986 total_servers++;
4987 }
4988 break;
4989 case ST_ADM_ACTION_DAGENT:
4990 if (sv->agent.state & CHK_ST_CONFIGURED) {
4991 sv->agent.state &= ~CHK_ST_ENABLED;
4992 altered_servers++;
4993 total_servers++;
4994 }
4995 break;
4996 case ST_ADM_ACTION_EAGENT:
4997 if (sv->agent.state & CHK_ST_CONFIGURED) {
4998 sv->agent.state |= CHK_ST_ENABLED;
4999 altered_servers++;
5000 total_servers++;
5001 }
5002 break;
5003 case ST_ADM_ACTION_ARUNN:
5004 if (sv->agent.state & CHK_ST_ENABLED) {
5005 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5006 srv_set_running(sv, "changed from Web interface");
5007 altered_servers++;
5008 total_servers++;
5009 }
5010 break;
5011 case ST_ADM_ACTION_ADOWN:
5012 if (sv->agent.state & CHK_ST_ENABLED) {
5013 sv->agent.health = 0;
5014 srv_set_stopped(sv, "changed from Web interface");
5015 altered_servers++;
5016 total_servers++;
5017 }
5018 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005019 case ST_ADM_ACTION_READY:
5020 srv_adm_set_ready(sv);
5021 altered_servers++;
5022 total_servers++;
5023 break;
5024 case ST_ADM_ACTION_DRAIN:
5025 srv_adm_set_drain(sv);
5026 altered_servers++;
5027 total_servers++;
5028 break;
5029 case ST_ADM_ACTION_MAINT:
5030 srv_adm_set_maint(sv);
5031 altered_servers++;
5032 total_servers++;
5033 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005034 case ST_ADM_ACTION_SHUTDOWN:
5035 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005036 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005037
5038 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5039 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005040 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005041
5042 altered_servers++;
5043 total_servers++;
5044 }
5045 break;
5046 }
5047 } else {
5048 /* the server name is unknown or ambiguous (duplicate names) */
5049 total_servers++;
5050 }
5051 }
5052 if (reprocess && px && action) {
5053 /* Now, we know the backend and the action chosen by the user.
5054 * We can safely restart from the first server parameter
5055 * to reprocess them
5056 */
5057 cur_param = st_cur_param;
5058 next_param = st_next_param;
5059 reprocess = 0;
5060 goto reprocess_servers;
5061 }
5062
5063 next_param = cur_param;
5064 }
5065 }
5066
5067 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005068 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005069 }
5070 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005071 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005072 }
5073 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005074 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005075 }
5076 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005077 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005078 }
5079 out:
5080 return 1;
5081}
5082
5083
5084static int stats_send_http_headers(struct stream_interface *si)
5085{
Willy Tarreau87b09662015-04-03 00:22:06 +02005086 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005087 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005088 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005089
5090 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005091 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005092 "Cache-Control: no-cache\r\n"
5093 "Connection: close\r\n"
5094 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005095 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005096
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005097 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005098 chunk_appendf(&trash, "Refresh: %d\r\n",
5099 uri->refresh);
5100
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005101 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5102
5103 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5104 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5105 else
5106 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005107
Willy Tarreaueee5b512015-04-03 23:46:31 +02005108 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005109 s->logs.tv_request = now;
5110
Willy Tarreaubc18da12015-03-13 14:00:47 +01005111 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005112 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005113 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005114 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005115
5116 return 1;
5117}
5118
5119static int stats_send_http_redirect(struct stream_interface *si)
5120{
5121 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005122 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005123 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005124 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005125
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005126 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005127 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005128 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005129 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005130 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 +01005131 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005132 }
5133
5134 /* We don't want to land on the posted stats page because a refresh will
5135 * repost the data. We don't want this to happen on accident so we redirect
5136 * the browse to the stats page with a GET.
5137 */
5138 chunk_printf(&trash,
5139 "HTTP/1.1 303 See Other\r\n"
5140 "Cache-Control: no-cache\r\n"
5141 "Content-Type: text/plain\r\n"
5142 "Connection: close\r\n"
5143 "Location: %s;st=%s%s%s%s\r\n"
5144 "\r\n",
5145 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005146 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5147 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5148 stat_status_codes[appctx->ctx.stats.st_code]) ?
5149 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005150 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005151 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5152 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005153 scope_txt);
5154
Willy Tarreaueee5b512015-04-03 23:46:31 +02005155 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005156 s->logs.tv_request = now;
5157
Willy Tarreaubc18da12015-03-13 14:00:47 +01005158 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005159 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005160 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005161 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005162
5163 return 1;
5164}
5165
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005166/* This I/O handler runs as an applet embedded in a stream interface. It is
5167 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005168 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005169 * automatically unregisters itself once transfer is complete.
5170 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005171static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005172{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005173 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005174 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005175 struct channel *req = si_oc(si);
5176 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005177
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005178 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5179 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005180
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005181 /* check that the output is not closed */
5182 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005183 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005184
Willy Tarreau347a35d2013-11-22 17:51:09 +01005185 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005186 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005187 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005188 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005189 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005190 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005191 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005192 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005193 }
5194
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005195 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005196 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005197 unsigned int data_len;
5198 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005199 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005200
5201 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5202 /* One difficulty we're facing is that we must prevent
5203 * the input data from being automatically forwarded to
5204 * the output area. For this, we temporarily disable
5205 * forwarding on the channel.
5206 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005207 last_fwd = si_ic(si)->to_forward;
5208 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005209 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005210 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005211 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005212 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005213 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005214 }
5215 }
5216
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005217 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005218 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005219 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005220
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005221 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005222
5223 /* Now we must either adjust or remove the chunk size. This is
5224 * not easy because the chunk size might wrap at the end of the
5225 * buffer, so we pretend we have nothing in the buffer, we write
5226 * the size, then restore the buffer's contents. Note that we can
5227 * only do that because no forwarding is scheduled on the stats
5228 * applet.
5229 */
5230 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005231 si_ic(si)->total -= (last_len - prev_len);
5232 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005233
5234 if (last_len != data_len) {
5235 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005236 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005237 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005238
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005239 si_ic(si)->total += (last_len - data_len);
5240 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005241 }
5242 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005243 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005244 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005245 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005246
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005247 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005248 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005249 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005250 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005251 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005252 }
5253
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005254 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005255 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005256 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005257 }
5258
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005259 if (appctx->st0 == STAT_HTTP_DONE) {
5260 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5261 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005262 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005263 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005264 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005265 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005266 }
5267 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005268 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005269 res->flags |= CF_READ_NULL;
5270 si_shutr(si);
5271 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005272
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005273 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5274 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005275
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005276 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005277 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5278 si_shutr(si);
5279 res->flags |= CF_READ_NULL;
5280 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005281 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005282 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005283 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005284}
5285
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005286
Willy Tarreau909d5172012-11-26 03:04:41 +01005287static inline const char *get_conn_ctrl_name(const struct connection *conn)
5288{
Willy Tarreau3c728722014-01-23 13:50:42 +01005289 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005290 return "NONE";
5291 return conn->ctrl->name;
5292}
5293
5294static inline const char *get_conn_xprt_name(const struct connection *conn)
5295{
5296 static char ptr[17];
5297
Willy Tarreauaad69382014-01-23 14:21:42 +01005298 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005299 return "NONE";
5300
5301 if (conn->xprt == &raw_sock)
5302 return "RAW";
5303
5304#ifdef USE_OPENSSL
5305 if (conn->xprt == &ssl_sock)
5306 return "SSL";
5307#endif
5308 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5309 return ptr;
5310}
5311
5312static inline const char *get_conn_data_name(const struct connection *conn)
5313{
5314 static char ptr[17];
5315
5316 if (!conn->data)
5317 return "NONE";
5318
5319 if (conn->data == &sess_conn_cb)
5320 return "SESS";
5321
5322 if (conn->data == &si_conn_cb)
5323 return "STRM";
5324
5325 if (conn->data == &check_conn_cb)
5326 return "CHCK";
5327
5328 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5329 return ptr;
5330}
5331
Willy Tarreau87b09662015-04-03 00:22:06 +02005332/* This function dumps a complete stream state onto the stream interface's
5333 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005334 * 0 if the output buffer is full and it needs to be called again, otherwise
5335 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005336 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005337static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005338{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005339 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005340 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005341 extern const char *monthname[12];
5342 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005343 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005344 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005345
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005346 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005347
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005348 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005349 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005350 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005351 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005352 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005353 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005354 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005355 appctx->ctx.sess.uid = 0;
5356 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005357 return 1;
5358 }
5359
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005360 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005361 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005362 appctx->ctx.sess.uid = sess->uniq_id;
5363 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005364 /* fall through */
5365
5366 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005367 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005368 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005369 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005370 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005371 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5372 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005373 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005374 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005375
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005376 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005377 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005378 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005379 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005380 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005381 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005382 break;
5383 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005384 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005385 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005386 default:
5387 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005388 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005389 break;
5390 }
5391
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005392 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005393 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005394 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005395
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005396 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005397 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005398 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5399 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5400 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005401
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005402 if (conn)
5403 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005404
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005405 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005406 case AF_INET:
5407 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005408 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005409 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005410 break;
5411 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005412 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005413 break;
5414 default:
5415 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005416 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005417 break;
5418 }
5419
Willy Tarreau50943332011-09-02 17:33:05 +02005420 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005421 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005422 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005423 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005424 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5425 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005426 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005427
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005428 conn = objt_conn(sess->si[1].end);
5429 if (conn)
5430 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005431
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005432 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005433 case AF_INET:
5434 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005435 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005436 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005437 break;
5438 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005439 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005440 break;
5441 default:
5442 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005443 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005444 break;
5445 }
5446
5447 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005448 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005449 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005450 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5451 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005452 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005453 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005454
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005455 if (conn)
5456 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005457
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005458 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005459 case AF_INET:
5460 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005461 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005462 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005463 break;
5464 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005465 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005466 break;
5467 default:
5468 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005469 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005470 break;
5471 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005472
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005473 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005474 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005475 sess->task,
5476 sess->task->state,
5477 sess->task->nice, sess->task->calls,
5478 sess->task->expire ?
5479 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5480 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5481 TICKS_TO_MS(1000)) : "<NEVER>",
5482 task_in_rq(sess->task) ? ", running" : "");
5483
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005484 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005485 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005486 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5487
Willy Tarreaueee5b512015-04-03 23:46:31 +02005488 if (sess->txn)
5489 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005490 " 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 +02005491 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5492 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 +01005493
5494 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005495 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005496 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005497 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005498 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005499 obj_type_name(sess->si[0].end),
5500 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005501 sess->si[0].exp ?
5502 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5503 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5504 TICKS_TO_MS(1000)) : "<NEVER>",
5505 sess->si[0].err_type);
5506
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005507 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005508 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005509 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005510 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005511 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005512 obj_type_name(sess->si[1].end),
5513 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005514 sess->si[1].exp ?
5515 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5516 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5517 TICKS_TO_MS(1000)) : "<NEVER>",
5518 sess->si[1].err_type);
5519
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005520 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005521 chunk_appendf(&trash,
5522 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005523 conn,
5524 get_conn_ctrl_name(conn),
5525 get_conn_xprt_name(conn),
5526 get_conn_data_name(conn),
5527 obj_type_name(conn->target),
5528 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005529
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005530 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005531 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005532 conn->flags,
5533 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005534 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005535 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005536 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005537 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005538 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5539 chunk_appendf(&trash,
5540 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5541 tmpctx,
5542 tmpctx->st0,
5543 tmpctx->st1,
5544 tmpctx->st2,
5545 tmpctx->applet->name);
5546 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005547
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005548 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005549 chunk_appendf(&trash,
5550 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005551 conn,
5552 get_conn_ctrl_name(conn),
5553 get_conn_xprt_name(conn),
5554 get_conn_data_name(conn),
5555 obj_type_name(conn->target),
5556 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005557
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005558 chunk_appendf(&trash,
5559 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005560 conn->flags,
5561 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005562 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005563 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005564 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005565 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005566 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5567 chunk_appendf(&trash,
5568 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5569 tmpctx,
5570 tmpctx->st0,
5571 tmpctx->st1,
5572 tmpctx->st2,
5573 tmpctx->applet->name);
5574 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005575
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005576 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005577 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005578 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005579 &sess->req,
5580 sess->req.flags, sess->req.analysers,
5581 sess->req.pipe ? sess->req.pipe->data : 0,
5582 sess->req.to_forward, sess->req.total,
5583 sess->req.analyse_exp ?
5584 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005585 TICKS_TO_MS(1000)) : "<NEVER>");
5586
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005587 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005588 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005589 sess->req.rex ?
5590 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005591 TICKS_TO_MS(1000)) : "<NEVER>");
5592
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005593 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005594 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005595 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005596 sess->req.wex ?
5597 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005598 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005599 sess->req.buf,
5600 sess->req.buf->data, sess->req.buf->o,
5601 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005602 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005603 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005604
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005605 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005606 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005607 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005608 &sess->res,
5609 sess->res.flags, sess->res.analysers,
5610 sess->res.pipe ? sess->res.pipe->data : 0,
5611 sess->res.to_forward, sess->res.total,
5612 sess->res.analyse_exp ?
5613 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005614 TICKS_TO_MS(1000)) : "<NEVER>");
5615
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005616 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005617 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005618 sess->res.rex ?
5619 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005620 TICKS_TO_MS(1000)) : "<NEVER>");
5621
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005622 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005623 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005624 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005625 sess->res.wex ?
5626 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005627 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005628 sess->res.buf,
5629 sess->res.buf->data, sess->res.buf->o,
5630 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005631 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005632 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005633
Willy Tarreaubc18da12015-03-13 14:00:47 +01005634 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005635 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005636 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005637 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005638
5639 /* use other states to dump the contents */
5640 }
5641 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005642 appctx->ctx.sess.uid = 0;
5643 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005644 return 1;
5645}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005646
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005647#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5648static int stats_tlskeys_list(struct stream_interface *si) {
5649 struct appctx *appctx = __objt_appctx(si->end);
5650
5651 switch (appctx->st2) {
5652 case STAT_ST_INIT:
5653 /* Display the column headers. If the message cannot be sent,
5654 * quit the fucntion with returning 0. The function is called
5655 * later and restart at the state "STAT_ST_INIT".
5656 */
5657 chunk_reset(&trash);
5658 chunk_appendf(&trash, "# id (file)\n");
5659 if (bi_putchk(si_ic(si), &trash) == -1) {
5660 si_applet_cant_put(si);
5661 return 0;
5662 }
5663
5664 /* Now, we start the browsing of the references lists.
5665 * Note that the following call to LIST_ELEM return bad pointer. The only
5666 * avalaible field of this pointer is <list>. It is used with the function
5667 * tlskeys_list_get_next() for retruning the first avalaible entry
5668 */
5669 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
5670 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5671
5672 appctx->st2 = STAT_ST_LIST;
5673 /* fall through */
5674
5675 case STAT_ST_LIST:
5676 while (appctx->ctx.tlskeys.ref) {
5677 chunk_reset(&trash);
5678
5679 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
5680 appctx->ctx.tlskeys.ref->filename);
5681
5682 if (bi_putchk(si_ic(si), &trash) == -1) {
5683 /* let's try again later from this stream. We add ourselves into
5684 * this stream's users so that it can remove us upon termination.
5685 */
5686 si_applet_cant_put(si);
5687 return 0;
5688 }
5689
5690 /* get next list entry and check the end of the list */
5691 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5692 }
5693
5694 appctx->st2 = STAT_ST_FIN;
5695 /* fall through */
5696
5697 default:
5698 appctx->st2 = STAT_ST_FIN;
5699 return 1;
5700 }
5701 return 0;
5702}
5703#endif
5704
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005705static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005706{
5707 struct appctx *appctx = __objt_appctx(si->end);
5708
5709 switch (appctx->st2) {
5710 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005711 /* Display the column headers. If the message cannot be sent,
5712 * quit the fucntion with returning 0. The function is called
5713 * later and restart at the state "STAT_ST_INIT".
5714 */
5715 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005716 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005717 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005718 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005719 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005720 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005721
5722 /* Now, we start the browsing of the references lists.
5723 * Note that the following call to LIST_ELEM return bad pointer. The only
5724 * avalaible field of this pointer is <list>. It is used with the function
5725 * pat_list_get_next() for retruning the first avalaible entry
5726 */
5727 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5728 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5729 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005730 appctx->st2 = STAT_ST_LIST;
5731 /* fall through */
5732
5733 case STAT_ST_LIST:
5734 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005735 chunk_reset(&trash);
5736
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005737 /* Build messages. If the reference is used by another category than
5738 * the listed categorie, display the information in the massage.
5739 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005740 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005741 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5742 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005743
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005744 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005745 /* let's try again later from this stream. We add ourselves into
5746 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005747 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005748 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005749 return 0;
5750 }
5751
5752 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005753 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5754 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005755 }
5756
5757 appctx->st2 = STAT_ST_FIN;
5758 /* fall through */
5759
5760 default:
5761 appctx->st2 = STAT_ST_FIN;
5762 return 1;
5763 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005764 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005765}
5766
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005767static int stats_map_lookup(struct stream_interface *si)
5768{
5769 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005770 struct sample sample;
5771 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005772 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005773
5774 switch (appctx->st2) {
5775 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005776 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005777 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005778 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005779 appctx->st2 = STAT_ST_LIST;
5780 /* fall through */
5781
5782 case STAT_ST_LIST:
5783 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005784 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005785 /* initialise chunk to build new message */
5786 chunk_reset(&trash);
5787
5788 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005789 sample.data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005790 sample.flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005791 sample.data.u.str.len = appctx->ctx.map.chunk.len;
5792 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005793 if (appctx->ctx.map.expr->pat_head->match &&
5794 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005795 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5796 else
5797 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005798
5799 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005800 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5801 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5802 break;
5803 if (match_method >= PAT_MATCH_NUM)
5804 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5805 else
5806 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005807
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005808 /* case sensitive */
5809 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5810 chunk_appendf(&trash, ", case=insensitive");
5811 else
5812 chunk_appendf(&trash, ", case=sensitive");
5813
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005814 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005815 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005816 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5817 chunk_appendf(&trash, ", found=no");
5818 else
5819 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005820 }
5821
5822 /* Display match and match info */
5823 else {
5824 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005825 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5826 chunk_appendf(&trash, ", found=yes");
5827 else
5828 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005829
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005830 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005831 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005832 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005833 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005834 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005835
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005836 /* display pattern */
5837 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5838 if (pat->ref && pat->ref->pattern)
5839 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5840 else
5841 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005842 }
5843 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005844 if (pat->ref && pat->ref->pattern)
5845 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5846 else
5847 chunk_appendf(&trash, ", pattern=unknown");
5848 }
5849
5850 /* display return value */
5851 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02005852 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02005853 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02005854 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005855 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005856 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005857 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005858 }
5859
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005860 chunk_appendf(&trash, "\n");
5861
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005862 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005863 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005864 /* let's try again later from this stream. We add ourselves into
5865 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005866 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005867 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005868 return 0;
5869 }
5870
5871 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005872 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5873 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005874 }
5875
5876 appctx->st2 = STAT_ST_FIN;
5877 /* fall through */
5878
5879 default:
5880 appctx->st2 = STAT_ST_FIN;
5881 free(appctx->ctx.map.chunk.str);
5882 return 1;
5883 }
5884}
5885
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005886static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005887{
5888 struct appctx *appctx = __objt_appctx(si->end);
5889
5890 switch (appctx->st2) {
5891
5892 case STAT_ST_INIT:
5893 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005894 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5895 struct pat_ref_elt *, list);
5896 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5897 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005898 appctx->st2 = STAT_ST_LIST;
5899 /* fall through */
5900
5901 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005902 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005903 chunk_reset(&trash);
5904
5905 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005906 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005907 chunk_appendf(&trash, "%p %s %s\n",
5908 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5909 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005910 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005911 chunk_appendf(&trash, "%p %s\n",
5912 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005913
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005914 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005915 /* let's try again later from this stream. We add ourselves into
5916 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005917 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005918 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005919 return 0;
5920 }
5921
5922 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005923 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5924 struct pat_ref_elt *, list);
5925 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005926 break;
5927 }
5928
5929 appctx->st2 = STAT_ST_FIN;
5930 /* fall through */
5931
5932 default:
5933 appctx->st2 = STAT_ST_FIN;
5934 return 1;
5935 }
5936}
5937
Willy Tarreau87b09662015-04-03 00:22:06 +02005938/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005939 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005940 * to be called again, otherwise non-zero. It is designed to be called
5941 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005942 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005943static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005944{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005945 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005946 struct connection *conn;
5947
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005948 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005949 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02005950 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005951 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005952 if (appctx->st2 == STAT_ST_LIST) {
5953 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5954 LIST_DEL(&appctx->ctx.sess.bref.users);
5955 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005956 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005957 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005958 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005959 }
5960
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005961 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005962
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005963 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005964 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005965 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02005966 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005967 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02005968 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005969 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02005970 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005971 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005972 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02005973 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005974 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005975 /* fall through */
5976
Willy Tarreau295a8372011-03-10 11:25:07 +01005977 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02005978 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005979 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5980 LIST_DEL(&appctx->ctx.sess.bref.users);
5981 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005982 }
5983
5984 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02005985 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005986 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005987 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005988
Willy Tarreau87b09662015-04-03 00:22:06 +02005989 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005990
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005991 if (appctx->ctx.sess.target) {
5992 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005993 goto next_sess;
5994
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005995 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005996 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005997 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005998 return 0;
5999
Willy Tarreau87b09662015-04-03 00:22:06 +02006000 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006001 LIST_DEL(&appctx->ctx.sess.bref.users);
6002 LIST_INIT(&appctx->ctx.sess.bref.users);
6003 if (appctx->ctx.sess.target != (void *)-1) {
6004 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006005 break;
6006 }
6007 else
6008 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006009 }
6010
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006011 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006012 "%p: proto=%s",
6013 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006014 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006015
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006016 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006017 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006018 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006019 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006020 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006021 " src=%s:%d fe=%s be=%s srv=%s",
6022 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006023 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006024 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006025 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006026 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006027 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006028 break;
6029 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006030 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006031 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006032 strm_li(curr_sess)->luid,
6033 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006034 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006035 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006036 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006037 break;
6038 }
6039
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006040 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006041 " ts=%02x age=%s calls=%d",
6042 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006043 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6044 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006045
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006046 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006047 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006048 curr_sess->req.flags,
6049 curr_sess->req.buf->i,
6050 curr_sess->req.analysers,
6051 curr_sess->req.rex ?
6052 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006053 TICKS_TO_MS(1000)) : "");
6054
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006055 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006056 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006057 curr_sess->req.wex ?
6058 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006059 TICKS_TO_MS(1000)) : "");
6060
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006061 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006062 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006063 curr_sess->req.analyse_exp ?
6064 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006065 TICKS_TO_MS(1000)) : "");
6066
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006067 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006068 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006069 curr_sess->res.flags,
6070 curr_sess->res.buf->i,
6071 curr_sess->res.analysers,
6072 curr_sess->res.rex ?
6073 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006074 TICKS_TO_MS(1000)) : "");
6075
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006076 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006077 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006078 curr_sess->res.wex ?
6079 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006080 TICKS_TO_MS(1000)) : "");
6081
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006082 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006083 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006084 curr_sess->res.analyse_exp ?
6085 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006086 TICKS_TO_MS(1000)) : "");
6087
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006088 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006089 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006090 " s0=[%d,%1xh,fd=%d,ex=%s]",
6091 curr_sess->si[0].state,
6092 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006093 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006094 curr_sess->si[0].exp ?
6095 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6096 TICKS_TO_MS(1000)) : "");
6097
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006098 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006099 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006100 " s1=[%d,%1xh,fd=%d,ex=%s]",
6101 curr_sess->si[1].state,
6102 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006103 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006104 curr_sess->si[1].exp ?
6105 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6106 TICKS_TO_MS(1000)) : "");
6107
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006108 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006109 " exp=%s",
6110 curr_sess->task->expire ?
6111 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6112 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006113 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006114 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006115
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006116 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006117
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006118 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006119 /* let's try again later from this stream. We add ourselves into
6120 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006121 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006122 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006123 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006124 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006125 }
6126
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006127 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006128 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006129 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006130
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006131 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006132 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006133 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006134 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006135 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006136 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006137
Willy Tarreaubc18da12015-03-13 14:00:47 +01006138 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006139 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006140 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006141 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006142
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006143 appctx->ctx.sess.target = NULL;
6144 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006145 return 1;
6146 }
6147
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006148 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006149 /* fall through */
6150
6151 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006152 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006153 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006154 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006155}
6156
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006157/* This is called when the stream interface is closed. For instance, upon an
6158 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006159 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006160 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006161static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006162{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006163 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6164 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6165 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006166 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006167 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6168 free(appctx->ctx.cli.err);
6169 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006170 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6171 free(appctx->ctx.map.chunk.str);
6172 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006173}
6174
Willy Tarreau20e99322013-04-13 09:22:25 +02006175/* This function is used to either dump tables states (when action is set
6176 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006177 * It returns 0 if the output buffer is full and it needs to be called
6178 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006179 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006180static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006181{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006182 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006183 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006184 struct ebmb_node *eb;
6185 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006186 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006187 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006188
6189 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006190 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006191 * - STAT_ST_INIT : the first call
6192 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006193 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006194 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006195 * and the entry pointer points to the next entry to be dumped,
6196 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006197 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006198 * data though.
6199 */
6200
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006201 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006202 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006203 if (appctx->st2 == STAT_ST_LIST) {
6204 appctx->ctx.table.entry->ref_cnt--;
6205 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006206 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006207 return 1;
6208 }
6209
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006210 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006211
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006212 while (appctx->st2 != STAT_ST_FIN) {
6213 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006214 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006215 appctx->ctx.table.proxy = appctx->ctx.table.target;
6216 if (!appctx->ctx.table.proxy)
6217 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006218
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006219 appctx->ctx.table.entry = NULL;
6220 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006221 break;
6222
Willy Tarreau295a8372011-03-10 11:25:07 +01006223 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006224 if (!appctx->ctx.table.proxy ||
6225 (appctx->ctx.table.target &&
6226 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6227 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006228 break;
6229 }
6230
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006231 if (appctx->ctx.table.proxy->table.size) {
6232 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6233 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006234 return 0;
6235
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006236 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006237 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006238 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006239 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006240 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006241 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6242 appctx->ctx.table.entry->ref_cnt++;
6243 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006244 break;
6245 }
6246 }
6247 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006248 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006249 break;
6250
Willy Tarreau295a8372011-03-10 11:25:07 +01006251 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006252 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006253
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006254 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006255 /* we're filtering on some data contents */
6256 void *ptr;
6257 long long data;
6258
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006259 dt = appctx->ctx.table.data_type;
6260 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6261 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006262 dt);
6263
6264 data = 0;
6265 switch (stktable_data_types[dt].std_type) {
6266 case STD_T_SINT:
6267 data = stktable_data_cast(ptr, std_t_sint);
6268 break;
6269 case STD_T_UINT:
6270 data = stktable_data_cast(ptr, std_t_uint);
6271 break;
6272 case STD_T_ULL:
6273 data = stktable_data_cast(ptr, std_t_ull);
6274 break;
6275 case STD_T_FRQP:
6276 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006277 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006278 break;
6279 }
6280
6281 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006282 if ((data < appctx->ctx.table.value &&
6283 (appctx->ctx.table.data_op == STD_OP_EQ ||
6284 appctx->ctx.table.data_op == STD_OP_GT ||
6285 appctx->ctx.table.data_op == STD_OP_GE)) ||
6286 (data == appctx->ctx.table.value &&
6287 (appctx->ctx.table.data_op == STD_OP_NE ||
6288 appctx->ctx.table.data_op == STD_OP_GT ||
6289 appctx->ctx.table.data_op == STD_OP_LT)) ||
6290 (data > appctx->ctx.table.value &&
6291 (appctx->ctx.table.data_op == STD_OP_EQ ||
6292 appctx->ctx.table.data_op == STD_OP_LT ||
6293 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006294 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006295 }
6296
Simon Hormanc88b8872011-06-15 15:18:49 +09006297 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006298 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6299 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006300 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006301
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006302 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006303
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006304 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006305 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006306 struct stksess *old = appctx->ctx.table.entry;
6307 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006308 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006309 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6310 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6311 stksess_kill(&appctx->ctx.table.proxy->table, old);
6312 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006313 break;
6314 }
6315
Simon Hormanc88b8872011-06-15 15:18:49 +09006316
6317 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006318 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6319 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6320 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006321
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006322 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6323 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006324 break;
6325
Willy Tarreau295a8372011-03-10 11:25:07 +01006326 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006327 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006328 break;
6329 }
6330 }
6331 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006332}
6333
Willy Tarreaud426a182010-03-05 14:58:26 +01006334/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006335 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6336 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6337 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6338 * lines are respected within the limit of 70 output chars. Lines that are
6339 * continuation of a previous truncated line begin with "+" instead of " "
6340 * after the offset. The new pointer is returned.
6341 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006342static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6343 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006344{
6345 int end;
6346 unsigned char c;
6347
6348 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006349 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006350 return ptr;
6351
Willy Tarreau77804732012-10-29 16:14:26 +01006352 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006353
Willy Tarreaud426a182010-03-05 14:58:26 +01006354 while (ptr < len && ptr < bsize) {
6355 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006356 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006357 if (out->len > end - 2)
6358 break;
6359 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006360 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006361 if (out->len > end - 3)
6362 break;
6363 out->str[out->len++] = '\\';
6364 switch (c) {
6365 case '\t': c = 't'; break;
6366 case '\n': c = 'n'; break;
6367 case '\r': c = 'r'; break;
6368 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006369 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006370 }
6371 out->str[out->len++] = c;
6372 } else {
6373 if (out->len > end - 5)
6374 break;
6375 out->str[out->len++] = '\\';
6376 out->str[out->len++] = 'x';
6377 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6378 out->str[out->len++] = hextab[c & 0xF];
6379 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006380 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006381 /* we had a line break, let's return now */
6382 out->str[out->len++] = '\n';
6383 *line = ptr;
6384 return ptr;
6385 }
6386 }
6387 /* we have an incomplete line, we return it as-is */
6388 out->str[out->len++] = '\n';
6389 return ptr;
6390}
6391
Baptiste Assmann3863f972015-05-17 00:33:24 +02006392/* This function dumps counters from all resolvers section and associated name servers.
6393 * It returns 0 if the output buffer is full and it needs
6394 * to be called again, otherwise non-zero.
6395 */
6396static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6397{
6398 struct appctx *appctx = __objt_appctx(si->end);
6399 struct dns_resolvers *presolvers;
6400 struct dns_nameserver *pnameserver;
6401
6402 chunk_reset(&trash);
6403
6404 switch (appctx->st2) {
6405 case STAT_ST_INIT:
6406 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6407 /* fall through */
6408
6409 case STAT_ST_LIST:
6410 presolvers = appctx->ctx.resolvers.ptr;
6411 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6412 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6413 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6414 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6415 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6416 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6417 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6418 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6419 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6420 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6421 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6422 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6423 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6424 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6425 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
Baptiste Assmann6cdea932015-09-02 21:56:05 +02006426 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
Baptiste Assmann3863f972015-05-17 00:33:24 +02006427 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6428 }
6429
6430 /* display response */
6431 if (bi_putchk(si_ic(si), &trash) == -1) {
6432 /* let's try again later from this session. We add ourselves into
6433 * this session's users so that it can remove us upon termination.
6434 */
6435 si->flags |= SI_FL_WAIT_ROOM;
6436 return 0;
6437 }
6438
6439 appctx->st2 = STAT_ST_FIN;
6440 /* fall through */
6441
6442 default:
6443 appctx->st2 = STAT_ST_FIN;
6444 return 1;
6445 }
6446}
6447
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006448/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006449 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006450 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006451 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006452static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006453{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006454 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006455 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006456
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006457 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006458 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006459
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006460 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006461
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006462 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006463 /* the function had not been called yet, let's prepare the
6464 * buffer for a response.
6465 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006466 struct tm tm;
6467
6468 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006469 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006470 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6471 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6472 error_snapshot_id);
6473
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006474 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006475 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006476 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006477 return 0;
6478 }
6479
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006480 appctx->ctx.errors.px = proxy;
6481 appctx->ctx.errors.buf = 0;
6482 appctx->ctx.errors.bol = 0;
6483 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006484 }
6485
6486 /* we have two inner loops here, one for the proxy, the other one for
6487 * the buffer.
6488 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006489 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006490 struct error_snapshot *es;
6491
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006492 if (appctx->ctx.errors.buf == 0)
6493 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006494 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006495 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006496
6497 if (!es->when.tv_sec)
6498 goto next;
6499
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006500 if (appctx->ctx.errors.iid >= 0 &&
6501 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6502 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006503 goto next;
6504
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006505 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006506 /* just print headers now */
6507
6508 char pn[INET6_ADDRSTRLEN];
6509 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006510 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006511
6512 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006513 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006514 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006515 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006516
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006517 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6518 case AF_INET:
6519 case AF_INET6:
6520 port = get_host_port(&es->src);
6521 break;
6522 default:
6523 port = 0;
6524 }
6525
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006526 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006527 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006528 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006529 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006530 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006531 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006532 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6533 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006534 break;
6535 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006536 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006537 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006538 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006539 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006540 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006541 break;
6542 }
6543
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006544 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006545 ", server %s (#%d), event #%u\n"
6546 " src %s:%d, session #%d, session flags 0x%08x\n"
6547 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6548 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6549 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6550 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6551 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6552 es->ev_id,
6553 pn, port, es->sid, es->s_flags,
6554 es->state, es->m_flags, es->t_flags,
6555 es->m_clen, es->m_blen,
6556 es->b_flags, es->b_out, es->b_tot,
6557 es->len, es->b_wrap, es->pos);
6558
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006559 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006560 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006561 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006562 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006563 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006564 appctx->ctx.errors.ptr = 0;
6565 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006566 }
6567
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006568 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006569 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006570 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006571 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006572 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006573 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006574 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006575 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006576 goto next;
6577 }
6578
6579 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006580 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006581 int newptr;
6582 int newline;
6583
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006584 newline = appctx->ctx.errors.bol;
6585 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6586 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006587 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006588
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006589 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006590 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006591 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006592 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006593 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006594 appctx->ctx.errors.ptr = newptr;
6595 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006596 };
6597 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006598 appctx->ctx.errors.bol = 0;
6599 appctx->ctx.errors.ptr = -1;
6600 appctx->ctx.errors.buf++;
6601 if (appctx->ctx.errors.buf > 1) {
6602 appctx->ctx.errors.buf = 0;
6603 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006604 }
6605 }
6606
6607 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006608 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006609}
6610
Willy Tarreaud5781202012-09-22 19:32:35 +02006611/* parse the "level" argument on the bind lines */
6612static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6613{
6614 if (!*args[cur_arg + 1]) {
6615 memprintf(err, "'%s' : missing level", args[cur_arg]);
6616 return ERR_ALERT | ERR_FATAL;
6617 }
6618
6619 if (!strcmp(args[cur_arg+1], "user"))
6620 conf->level = ACCESS_LVL_USER;
6621 else if (!strcmp(args[cur_arg+1], "operator"))
6622 conf->level = ACCESS_LVL_OPER;
6623 else if (!strcmp(args[cur_arg+1], "admin"))
6624 conf->level = ACCESS_LVL_ADMIN;
6625 else {
6626 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6627 args[cur_arg], args[cur_arg+1]);
6628 return ERR_ALERT | ERR_FATAL;
6629 }
6630
6631 return 0;
6632}
6633
Willy Tarreau30576452015-04-13 13:50:30 +02006634struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006635 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006636 .name = "<STATS>", /* used for logging */
6637 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006638 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006639};
6640
Willy Tarreau30576452015-04-13 13:50:30 +02006641static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006642 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006643 .name = "<CLI>", /* used for logging */
6644 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006645 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006646};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006647
Willy Tarreaudc13c112013-06-21 23:16:39 +02006648static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006649 { CFG_GLOBAL, "stats", stats_parse_global },
6650 { 0, NULL, NULL },
6651}};
6652
Willy Tarreaud5781202012-09-22 19:32:35 +02006653static struct bind_kw_list bind_kws = { "STAT", { }, {
6654 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6655 { NULL, NULL, 0 },
6656}};
6657
Willy Tarreau10522fd2008-07-09 20:12:41 +02006658__attribute__((constructor))
6659static void __dumpstats_module_init(void)
6660{
6661 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006662 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006663}
6664
Willy Tarreau91861262007-10-17 17:06:05 +02006665/*
6666 * Local variables:
6667 * c-indent-level: 8
6668 * c-basic-offset: 8
6669 * End:
6670 */