blob: b83dfb80b6bf7d39e7d3b172d51da43aab36728c [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
Willy Tarreau061b5de2015-10-13 15:06:57 +0200333 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200334 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
Andrew Hayworth68d05342015-10-02 20:33:01 +00001169 if (*args[3]) {
1170 appctx->ctx.resolvers.ptr = NULL;
1171 list_for_each_entry(presolvers, &dns_resolvers, list) {
1172 if (strcmp(presolvers->id, args[3]) == 0) {
1173 appctx->ctx.resolvers.ptr = presolvers;
1174 break;
1175 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001176 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001177 if (appctx->ctx.resolvers.ptr == NULL) {
1178 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1179 appctx->st0 = STAT_CLI_PRINT;
1180 return 1;
1181 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001182 }
1183
1184 appctx->st2 = STAT_ST_INIT;
1185 appctx->st0 = STAT_CLI_O_RESOLVERS;
1186 return 1;
1187 }
1188 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001189 appctx->ctx.stats.flags |= STAT_BOUND;
1190 appctx->ctx.stats.iid = atoi(args[2]);
1191 appctx->ctx.stats.type = atoi(args[3]);
1192 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001193 }
1194
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001195 appctx->st2 = STAT_ST_INIT;
1196 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001197 }
1198 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001199 appctx->st2 = STAT_ST_INIT;
1200 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001201 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001202 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
1203 appctx->ctx.server_state.backend = NULL;
1204
1205 /* check if a backend name has been provided */
1206 if (*args[3]) {
1207 /* read server state from local file */
1208 appctx->ctx.server_state.backend = proxy_be_by_name(args[3]);
1209
1210 if (appctx->ctx.server_state.backend == NULL) {
1211 appctx->ctx.cli.msg = "Can't find backend.\n";
1212 appctx->st0 = STAT_CLI_PRINT;
1213 return 1;
1214 }
1215 }
1216 appctx->st2 = STAT_ST_INIT;
1217 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1218 return 1;
1219 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001220 else if (strcmp(args[1], "pools") == 0) {
1221 appctx->st2 = STAT_ST_INIT;
1222 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1223 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001224 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001225 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001226 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001227 appctx->ctx.cli.msg = stats_permission_denied_msg;
1228 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001229 return 1;
1230 }
Willy Tarreau76153662012-11-26 01:16:39 +01001231 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001232 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001233 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001234 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001235 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001236 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001237 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001238 appctx->ctx.sess.pos = 0;
1239 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001240 }
1241 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001242 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001243 appctx->ctx.cli.msg = stats_permission_denied_msg;
1244 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001245 return 1;
1246 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001247 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001248 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001249 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001250 appctx->ctx.errors.iid = -1;
1251 appctx->ctx.errors.px = NULL;
1252 appctx->st2 = STAT_ST_INIT;
1253 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001254 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001255 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001256 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001257 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001258 else if (strcmp(args[1], "tls-keys") == 0) {
1259#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1260 appctx->st2 = STAT_ST_INIT;
1261 appctx->st0 = STAT_CLI_O_TLSK;
1262#else
1263 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1264 "that doesn't support specifying TLS ticket keys\n";
1265 appctx->st0 = STAT_CLI_PRINT;
1266#endif
1267 return 1;
1268 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001269 else if (strcmp(args[1], "map") == 0 ||
1270 strcmp(args[1], "acl") == 0) {
1271
1272 /* Set ACL or MAP flags. */
1273 if (args[1][0] == 'm')
1274 appctx->ctx.map.display_flags = PAT_REF_MAP;
1275 else
1276 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001277
1278 /* no parameter: display all map avalaible */
1279 if (!*args[2]) {
1280 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001281 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001282 return 1;
1283 }
1284
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001285 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001286 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001287 if (!appctx->ctx.map.ref ||
1288 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1289 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001290 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001291 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001292 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001293 appctx->st0 = STAT_CLI_PRINT;
1294 return 1;
1295 }
1296 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001297 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001298 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001299 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001300 return 0;
1301 }
1302 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001303 else if (strcmp(args[0], "clear") == 0) {
1304 if (strcmp(args[1], "counters") == 0) {
1305 struct proxy *px;
1306 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001307 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001308 int clrall = 0;
1309
1310 if (strcmp(args[2], "all") == 0)
1311 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001312
Willy Tarreau6162db22009-10-10 17:13:00 +02001313 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001314 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1315 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001316 appctx->ctx.cli.msg = stats_permission_denied_msg;
1317 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001318 return 1;
1319 }
1320
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001321 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001322 if (clrall) {
1323 memset(&px->be_counters, 0, sizeof(px->be_counters));
1324 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1325 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001326 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001327 px->be_counters.conn_max = 0;
1328 px->be_counters.p.http.rps_max = 0;
1329 px->be_counters.sps_max = 0;
1330 px->be_counters.cps_max = 0;
1331 px->be_counters.nbpend_max = 0;
1332
1333 px->fe_counters.conn_max = 0;
1334 px->fe_counters.p.http.rps_max = 0;
1335 px->fe_counters.sps_max = 0;
1336 px->fe_counters.cps_max = 0;
1337 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001338 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001339
1340 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001341 if (clrall)
1342 memset(&sv->counters, 0, sizeof(sv->counters));
1343 else {
1344 sv->counters.cur_sess_max = 0;
1345 sv->counters.nbpend_max = 0;
1346 sv->counters.sps_max = 0;
1347 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001348
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001349 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001350 if (li->counters) {
1351 if (clrall)
1352 memset(li->counters, 0, sizeof(*li->counters));
1353 else
1354 li->counters->conn_max = 0;
1355 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001356 }
1357
Willy Tarreau81c25d02011-09-07 15:17:21 +02001358 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001359 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001360 return 1;
1361 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001362 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001363 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001364 /* end of processing */
1365 return 1;
1366 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001367 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1368 /* Set ACL or MAP flags. */
1369 if (args[1][0] == 'm')
1370 appctx->ctx.map.display_flags = PAT_REF_MAP;
1371 else
1372 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001373
1374 /* no parameter */
1375 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001376 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1377 appctx->ctx.cli.msg = "Missing map identifier.\n";
1378 else
1379 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001380 appctx->st0 = STAT_CLI_PRINT;
1381 return 1;
1382 }
1383
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001384 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001385 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001386 if (!appctx->ctx.map.ref ||
1387 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1388 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001389 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001390 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001391 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001392 appctx->st0 = STAT_CLI_PRINT;
1393 return 1;
1394 }
1395
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001396 /* Clear all. */
1397 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001398
1399 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001400 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001401 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001402 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001403 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001404 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001405 return 0;
1406 }
1407 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001408 else if (strcmp(args[0], "get") == 0) {
1409 if (strcmp(args[1], "weight") == 0) {
1410 struct proxy *px;
1411 struct server *sv;
1412
1413 /* split "backend/server" and make <line> point to server */
1414 for (line = args[2]; *line; line++)
1415 if (*line == '/') {
1416 *line++ = '\0';
1417 break;
1418 }
1419
1420 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001421 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1422 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001423 return 1;
1424 }
1425
1426 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001427 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1428 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001429 return 1;
1430 }
1431
1432 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001433 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001434 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001435 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001436
Willy Tarreau38338fa2009-10-10 18:37:29 +02001437 return 1;
1438 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001439 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1440 /* Set flags. */
1441 if (args[1][0] == 'm')
1442 appctx->ctx.map.display_flags = PAT_REF_MAP;
1443 else
1444 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001445
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001446 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001447 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001448 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1449 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1450 else
1451 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001452 appctx->st0 = STAT_CLI_PRINT;
1453 return 1;
1454 }
1455
1456 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001457 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001458 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001459 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001460 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001461 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001462 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001463 appctx->st0 = STAT_CLI_PRINT;
1464 return 1;
1465 }
1466
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001467 /* copy input string. The string must be allocated because
1468 * it may be used over multiple iterations. It's released
1469 * at the end and upon abort anyway.
1470 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001471 appctx->ctx.map.chunk.len = strlen(args[3]);
1472 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1473 appctx->ctx.map.chunk.str = strdup(args[3]);
1474 if (!appctx->ctx.map.chunk.str) {
1475 appctx->ctx.cli.msg = "Out of memory error.\n";
1476 appctx->st0 = STAT_CLI_PRINT;
1477 return 1;
1478 }
1479
1480 /* prepare response */
1481 appctx->st2 = STAT_ST_INIT;
1482 appctx->st0 = STAT_CLI_O_MLOOK;
1483 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001484 else { /* not "get weight" */
1485 return 0;
1486 }
1487 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001488 else if (strcmp(args[0], "set") == 0) {
1489 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001490 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001491 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001492
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001493 sv = expect_server_admin(s, si, args[2]);
1494 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001495 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001496
Simon Horman7d09b9a2013-02-12 10:45:51 +09001497 warning = server_parse_weight_change_request(sv, args[3]);
1498 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001499 appctx->ctx.cli.msg = warning;
1500 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001501 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001502 return 1;
1503 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001504 else if (strcmp(args[1], "server") == 0) {
1505 struct server *sv;
1506 const char *warning;
1507
1508 sv = expect_server_admin(s, si, args[2]);
1509 if (!sv)
1510 return 1;
1511
1512 if (strcmp(args[3], "weight") == 0) {
1513 warning = server_parse_weight_change_request(sv, args[4]);
1514 if (warning) {
1515 appctx->ctx.cli.msg = warning;
1516 appctx->st0 = STAT_CLI_PRINT;
1517 }
1518 }
1519 else if (strcmp(args[3], "state") == 0) {
1520 if (strcmp(args[4], "ready") == 0)
1521 srv_adm_set_ready(sv);
1522 else if (strcmp(args[4], "drain") == 0)
1523 srv_adm_set_drain(sv);
1524 else if (strcmp(args[4], "maint") == 0)
1525 srv_adm_set_maint(sv);
1526 else {
1527 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1528 appctx->st0 = STAT_CLI_PRINT;
1529 }
1530 }
1531 else if (strcmp(args[3], "health") == 0) {
1532 if (sv->track) {
1533 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1534 appctx->st0 = STAT_CLI_PRINT;
1535 }
1536 else if (strcmp(args[4], "up") == 0) {
1537 sv->check.health = sv->check.rise + sv->check.fall - 1;
1538 srv_set_running(sv, "changed from CLI");
1539 }
1540 else if (strcmp(args[4], "stopping") == 0) {
1541 sv->check.health = sv->check.rise + sv->check.fall - 1;
1542 srv_set_stopping(sv, "changed from CLI");
1543 }
1544 else if (strcmp(args[4], "down") == 0) {
1545 sv->check.health = 0;
1546 srv_set_stopped(sv, "changed from CLI");
1547 }
1548 else {
1549 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1550 appctx->st0 = STAT_CLI_PRINT;
1551 }
1552 }
1553 else if (strcmp(args[3], "agent") == 0) {
1554 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1555 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1556 appctx->st0 = STAT_CLI_PRINT;
1557 }
1558 else if (strcmp(args[4], "up") == 0) {
1559 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1560 srv_set_running(sv, "changed from CLI");
1561 }
1562 else if (strcmp(args[4], "down") == 0) {
1563 sv->agent.health = 0;
1564 srv_set_stopped(sv, "changed from CLI");
1565 }
1566 else {
1567 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1568 appctx->st0 = STAT_CLI_PRINT;
1569 }
1570 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001571 else if (strcmp(args[3], "addr") == 0) {
1572 warning = server_parse_addr_change_request(sv, args[4]);
1573 if (warning) {
1574 appctx->ctx.cli.msg = warning;
1575 appctx->st0 = STAT_CLI_PRINT;
1576 }
1577 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001578 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001579 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001580 appctx->st0 = STAT_CLI_PRINT;
1581 }
1582 return 1;
1583 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001584 else if (strcmp(args[1], "timeout") == 0) {
1585 if (strcmp(args[2], "cli") == 0) {
1586 unsigned timeout;
1587 const char *res;
1588
1589 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001590 appctx->ctx.cli.msg = "Expects an integer value.\n";
1591 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001592 return 1;
1593 }
1594
1595 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1596 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001597 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1598 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001599 return 1;
1600 }
1601
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001602 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001603 return 1;
1604 }
1605 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001606 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1607 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001608 return 1;
1609 }
1610 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001611 else if (strcmp(args[1], "maxconn") == 0) {
1612 if (strcmp(args[2], "frontend") == 0) {
1613 struct proxy *px;
1614 struct listener *l;
1615 int v;
1616
Willy Tarreau532a4502011-09-07 22:37:44 +02001617 px = expect_frontend_admin(s, si, args[3]);
1618 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001619 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001620
1621 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001622 appctx->ctx.cli.msg = "Integer value expected.\n";
1623 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001624 return 1;
1625 }
1626
1627 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001628 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001629 appctx->ctx.cli.msg = "Value out of range.\n";
1630 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001631 return 1;
1632 }
1633
1634 /* OK, the value is fine, so we assign it to the proxy and to all of
1635 * its listeners. The blocked ones will be dequeued.
1636 */
1637 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001638 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001639 l->maxconn = v;
1640 if (l->state == LI_FULL)
1641 resume_listener(l);
1642 }
1643
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001644 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1645 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001646
1647 return 1;
1648 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001649 else if (strcmp(args[2], "server") == 0) {
1650 struct server *sv;
1651 int v;
1652
1653 sv = expect_server_admin(s, si, args[3]);
1654 if (!sv)
1655 return 1;
1656
1657 if (!*args[4]) {
1658 appctx->ctx.cli.msg = "Integer value expected.\n";
1659 appctx->st0 = STAT_CLI_PRINT;
1660 return 1;
1661 }
1662
1663 v = atoi(args[4]);
1664 if (v < 0) {
1665 appctx->ctx.cli.msg = "Value out of range.\n";
1666 appctx->st0 = STAT_CLI_PRINT;
1667 return 1;
1668 }
1669
1670 if (sv->maxconn == sv->minconn) { // static maxconn
1671 sv->maxconn = sv->minconn = v;
1672 } else { // dynamic maxconn
1673 sv->maxconn = v;
1674 }
1675
1676 if (may_dequeue_tasks(sv, sv->proxy))
1677 process_srv_queue(sv);
1678
1679 return 1;
1680 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001681 else if (strcmp(args[2], "global") == 0) {
1682 int v;
1683
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001684 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001685 appctx->ctx.cli.msg = stats_permission_denied_msg;
1686 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001687 return 1;
1688 }
1689
1690 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001691 appctx->ctx.cli.msg = "Expects an integer value.\n";
1692 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001693 return 1;
1694 }
1695
1696 v = atoi(args[3]);
1697 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001698 appctx->ctx.cli.msg = "Value out of range.\n";
1699 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001700 return 1;
1701 }
1702
1703 /* check for unlimited values */
1704 if (v <= 0)
1705 v = global.hardmaxconn;
1706
1707 global.maxconn = v;
1708
1709 /* Dequeues all of the listeners waiting for a resource */
1710 if (!LIST_ISEMPTY(&global_listener_queue))
1711 dequeue_all_listeners(&global_listener_queue);
1712
1713 return 1;
1714 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001715 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001716 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001717 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001718 return 1;
1719 }
1720 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001721 else if (strcmp(args[1], "rate-limit") == 0) {
1722 if (strcmp(args[2], "connections") == 0) {
1723 if (strcmp(args[3], "global") == 0) {
1724 int v;
1725
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001726 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001727 appctx->ctx.cli.msg = stats_permission_denied_msg;
1728 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001729 return 1;
1730 }
1731
1732 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001733 appctx->ctx.cli.msg = "Expects an integer value.\n";
1734 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001735 return 1;
1736 }
1737
1738 v = atoi(args[4]);
1739 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001740 appctx->ctx.cli.msg = "Value out of range.\n";
1741 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001742 return 1;
1743 }
1744
1745 global.cps_lim = v;
1746
1747 /* Dequeues all of the listeners waiting for a resource */
1748 if (!LIST_ISEMPTY(&global_listener_queue))
1749 dequeue_all_listeners(&global_listener_queue);
1750
1751 return 1;
1752 }
1753 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001754 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1755 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001756 return 1;
1757 }
1758 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001759 else if (strcmp(args[2], "sessions") == 0) {
1760 if (strcmp(args[3], "global") == 0) {
1761 int v;
1762
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001763 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001764 appctx->ctx.cli.msg = stats_permission_denied_msg;
1765 appctx->st0 = STAT_CLI_PRINT;
1766 return 1;
1767 }
1768
1769 if (!*args[4]) {
1770 appctx->ctx.cli.msg = "Expects an integer value.\n";
1771 appctx->st0 = STAT_CLI_PRINT;
1772 return 1;
1773 }
1774
1775 v = atoi(args[4]);
1776 if (v < 0) {
1777 appctx->ctx.cli.msg = "Value out of range.\n";
1778 appctx->st0 = STAT_CLI_PRINT;
1779 return 1;
1780 }
1781
1782 global.sps_lim = v;
1783
1784 /* Dequeues all of the listeners waiting for a resource */
1785 if (!LIST_ISEMPTY(&global_listener_queue))
1786 dequeue_all_listeners(&global_listener_queue);
1787
1788 return 1;
1789 }
1790 else {
1791 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1792 appctx->st0 = STAT_CLI_PRINT;
1793 return 1;
1794 }
1795 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001796#ifdef USE_OPENSSL
1797 else if (strcmp(args[2], "ssl-sessions") == 0) {
1798 if (strcmp(args[3], "global") == 0) {
1799 int v;
1800
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001801 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001802 appctx->ctx.cli.msg = stats_permission_denied_msg;
1803 appctx->st0 = STAT_CLI_PRINT;
1804 return 1;
1805 }
1806
1807 if (!*args[4]) {
1808 appctx->ctx.cli.msg = "Expects an integer value.\n";
1809 appctx->st0 = STAT_CLI_PRINT;
1810 return 1;
1811 }
1812
1813 v = atoi(args[4]);
1814 if (v < 0) {
1815 appctx->ctx.cli.msg = "Value out of range.\n";
1816 appctx->st0 = STAT_CLI_PRINT;
1817 return 1;
1818 }
1819
1820 global.ssl_lim = v;
1821
1822 /* Dequeues all of the listeners waiting for a resource */
1823 if (!LIST_ISEMPTY(&global_listener_queue))
1824 dequeue_all_listeners(&global_listener_queue);
1825
1826 return 1;
1827 }
1828 else {
1829 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1830 appctx->st0 = STAT_CLI_PRINT;
1831 return 1;
1832 }
1833 }
1834#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001835 else if (strcmp(args[2], "http-compression") == 0) {
1836 if (strcmp(args[3], "global") == 0) {
1837 int v;
1838
Willy Tarreau85d47f92012-11-21 00:29:50 +01001839 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001840 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1841 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001842 return 1;
1843 }
1844
William Lallemandd85f9172012-11-09 17:05:39 +01001845 v = atoi(args[4]);
1846 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1847 }
1848 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001849 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1850 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001851 return 1;
1852 }
1853 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001854 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001855 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001856 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001857 return 1;
1858 }
1859 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001860 else if (strcmp(args[1], "table") == 0) {
1861 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1862 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001863 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001864 char *err;
1865
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001866 /* Set flags. */
1867 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001868
1869 /* Expect three parameters: map name, key and new value. */
1870 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001871 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001872 appctx->st0 = STAT_CLI_PRINT;
1873 return 1;
1874 }
1875
1876 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001877 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001878 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001879 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001880 appctx->st0 = STAT_CLI_PRINT;
1881 return 1;
1882 }
1883
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001884 /* If the entry identifier start with a '#', it is considered as
1885 * pointer id
1886 */
1887 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1888 struct pat_ref_elt *ref;
1889 long long int conv;
1890 char *error;
1891
1892 /* Convert argument to integer value. */
1893 conv = strtoll(&args[3][1], &error, 16);
1894 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001895 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001896 appctx->st0 = STAT_CLI_PRINT;
1897 return 1;
1898 }
1899
1900 /* Convert and check integer to pointer. */
1901 ref = (struct pat_ref_elt *)(long)conv;
1902 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001903 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001904 appctx->st0 = STAT_CLI_PRINT;
1905 return 1;
1906 }
1907
1908 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001909 err = NULL;
1910 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1911 if (err)
1912 memprintf(&err, "%s.\n", err);
1913 appctx->ctx.cli.err = err;
1914 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001915 return 1;
1916 }
1917 }
1918 else {
1919 /* Else, use the entry identifier as pattern
1920 * string, and update the value.
1921 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001922 err = NULL;
1923 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1924 if (err)
1925 memprintf(&err, "%s.\n", err);
1926 appctx->ctx.cli.err = err;
1927 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001928 return 1;
1929 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001930 }
1931
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001932 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001933 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001934 return 1;
1935 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001936#ifdef USE_OPENSSL
1937 else if (strcmp(args[1], "ssl") == 0) {
1938 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01001939#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001940 char *err = NULL;
1941
Emeric Brunaf4ef742014-06-19 14:10:45 +02001942 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001943 if (!*args[3]) {
1944 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1945 appctx->st0 = STAT_CLI_PRINT;
1946 return 1;
1947 }
1948
1949 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1950 if (trash.len < 0) {
1951 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1952 appctx->st0 = STAT_CLI_PRINT;
1953 return 1;
1954 }
1955
1956 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1957 if (err) {
1958 memprintf(&err, "%s.\n", err);
1959 appctx->ctx.cli.err = err;
1960 appctx->st0 = STAT_CLI_PRINT_FREE;
1961 }
1962 return 1;
1963 }
1964 appctx->ctx.cli.msg = "OCSP Response updated!";
1965 appctx->st0 = STAT_CLI_PRINT;
1966 return 1;
1967#else
1968 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1969 appctx->st0 = STAT_CLI_PRINT;
1970 return 1;
1971#endif
1972 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001973 else if (strcmp(args[2], "tls-key") == 0) {
1974#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1975 /* Expect two parameters: the filename and the new new TLS key in encoding */
1976 if (!*args[3] || !*args[4]) {
1977 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
1978 appctx->st0 = STAT_CLI_PRINT;
1979 return 1;
1980 }
1981
1982 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
1983 if(!appctx->ctx.tlskeys.ref) {
1984 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
1985 appctx->st0 = STAT_CLI_PRINT;
1986 return 1;
1987 }
1988
1989 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
1990 if (trash.len != sizeof(struct tls_sess_key)) {
1991 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
1992 appctx->st0 = STAT_CLI_PRINT;
1993 return 1;
1994 }
1995
Pradeep Jindalcc79b002015-08-20 18:25:17 +05301996 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
1997 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 +02001998
1999 appctx->ctx.cli.msg = "TLS ticket key updated!";
2000 appctx->st0 = STAT_CLI_PRINT;
2001 return 1;
2002#else
2003 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2004 "that doesn't support specifying TLS ticket keys\n";
2005 appctx->st0 = STAT_CLI_PRINT;
2006 return 1;
2007#endif
2008 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002009 else {
2010 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2011 appctx->st0 = STAT_CLI_PRINT;
2012 return 1;
2013 }
2014 }
2015#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002016 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002017 return 0;
2018 }
2019 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002020 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002021 if (strcmp(args[1], "agent") == 0) {
2022 struct server *sv;
2023
2024 sv = expect_server_admin(s, si, args[2]);
2025 if (!sv)
2026 return 1;
2027
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002028 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2029 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2030 appctx->st0 = STAT_CLI_PRINT;
2031 return 1;
2032 }
2033
2034 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002035 return 1;
2036 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002037 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002038 struct server *sv;
2039
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002040 sv = expect_server_admin(s, si, args[2]);
2041 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002042 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002043
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002044 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2045 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2046 appctx->st0 = STAT_CLI_PRINT;
2047 return 1;
2048 }
2049
2050 sv->check.state |= CHK_ST_ENABLED;
2051 return 1;
2052 }
2053 else if (strcmp(args[1], "server") == 0) {
2054 struct server *sv;
2055
2056 sv = expect_server_admin(s, si, args[2]);
2057 if (!sv)
2058 return 1;
2059
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002060 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002061 return 1;
2062 }
2063 else if (strcmp(args[1], "frontend") == 0) {
2064 struct proxy *px;
2065
2066 px = expect_frontend_admin(s, si, args[2]);
2067 if (!px)
2068 return 1;
2069
2070 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002071 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2072 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002073 return 1;
2074 }
2075
2076 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002077 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2078 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002079 return 1;
2080 }
2081
2082 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002083 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2084 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002085 return 1;
2086 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002087 return 1;
2088 }
2089 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002090 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002091 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002092 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002093 }
2094 }
2095 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002096 if (strcmp(args[1], "agent") == 0) {
2097 struct server *sv;
2098
2099 sv = expect_server_admin(s, si, args[2]);
2100 if (!sv)
2101 return 1;
2102
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002103 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002104 return 1;
2105 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002106 else if (strcmp(args[1], "health") == 0) {
2107 struct server *sv;
2108
2109 sv = expect_server_admin(s, si, args[2]);
2110 if (!sv)
2111 return 1;
2112
2113 sv->check.state &= ~CHK_ST_ENABLED;
2114 return 1;
2115 }
Simon Horman671b6f02013-11-25 10:46:39 +09002116 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002117 struct server *sv;
2118
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002119 sv = expect_server_admin(s, si, args[2]);
2120 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002121 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002122
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002123 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002124 return 1;
2125 }
2126 else if (strcmp(args[1], "frontend") == 0) {
2127 struct proxy *px;
2128
2129 px = expect_frontend_admin(s, si, args[2]);
2130 if (!px)
2131 return 1;
2132
2133 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002134 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2135 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002136 return 1;
2137 }
2138
2139 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002140 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2141 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002142 return 1;
2143 }
2144
2145 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002146 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2147 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002148 return 1;
2149 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002150 return 1;
2151 }
2152 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002153 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002154 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002155 return 1;
2156 }
2157 }
2158 else if (strcmp(args[0], "shutdown") == 0) {
2159 if (strcmp(args[1], "frontend") == 0) {
2160 struct proxy *px;
2161
2162 px = expect_frontend_admin(s, si, args[2]);
2163 if (!px)
2164 return 1;
2165
2166 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002167 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2168 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002169 return 1;
2170 }
2171
2172 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2173 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2174 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2175 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2176 stop_proxy(px);
2177 return 1;
2178 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002179 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002180 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002181
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002182 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002183 appctx->ctx.cli.msg = stats_permission_denied_msg;
2184 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002185 return 1;
2186 }
2187
2188 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002189 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2190 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002191 return 1;
2192 }
2193
2194 ptr = (void *)strtoul(args[2], NULL, 0);
2195
Willy Tarreau87b09662015-04-03 00:22:06 +02002196 /* first, look for the requested stream in the stream table */
2197 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002198 if (sess == ptr)
2199 break;
2200 }
2201
Willy Tarreau87b09662015-04-03 00:22:06 +02002202 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002203 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002204 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2205 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002206 return 1;
2207 }
2208
Willy Tarreaue7dff022015-04-03 01:14:29 +02002209 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002210 return 1;
2211 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002212 else if (strcmp(args[1], "sessions") == 0) {
2213 if (strcmp(args[2], "server") == 0) {
2214 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002215 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002216
2217 sv = expect_server_admin(s, si, args[3]);
2218 if (!sv)
2219 return 1;
2220
Willy Tarreau87b09662015-04-03 00:22:06 +02002221 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002222 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2223 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002224 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002225
2226 return 1;
2227 }
2228 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002229 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2230 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002231 return 1;
2232 }
2233 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002234 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002235 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2236 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002237 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002238 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002239 }
2240 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002241 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2242 if (args[1][0] == 'm')
2243 appctx->ctx.map.display_flags = PAT_REF_MAP;
2244 else
2245 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002246
2247 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002248 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2249 if (!*args[2] || !*args[3]) {
2250 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2251 appctx->st0 = STAT_CLI_PRINT;
2252 return 1;
2253 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002254 }
2255
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002256 else {
2257 if (!*args[2] || !*args[3]) {
2258 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2259 appctx->st0 = STAT_CLI_PRINT;
2260 return 1;
2261 }
2262 }
2263
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002264 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002265 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002266 if (!appctx->ctx.map.ref ||
2267 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002268 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002269 appctx->st0 = STAT_CLI_PRINT;
2270 return 1;
2271 }
2272
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002273 /* If the entry identifier start with a '#', it is considered as
2274 * pointer id
2275 */
2276 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2277 struct pat_ref_elt *ref;
2278 long long int conv;
2279 char *error;
2280
2281 /* Convert argument to integer value. */
2282 conv = strtoll(&args[3][1], &error, 16);
2283 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002284 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002285 appctx->st0 = STAT_CLI_PRINT;
2286 return 1;
2287 }
2288
2289 /* Convert and check integer to pointer. */
2290 ref = (struct pat_ref_elt *)(long)conv;
2291 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002292 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002293 appctx->st0 = STAT_CLI_PRINT;
2294 return 1;
2295 }
2296
2297 /* Try to delete the entry. */
2298 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2299 /* The entry is not found, send message. */
2300 appctx->ctx.cli.msg = "Key not found.\n";
2301 appctx->st0 = STAT_CLI_PRINT;
2302 return 1;
2303 }
2304 }
2305 else {
2306 /* Else, use the entry identifier as pattern
2307 * string and try to delete the entry.
2308 */
2309 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2310 /* The entry is not found, send message. */
2311 appctx->ctx.cli.msg = "Key not found.\n";
2312 appctx->st0 = STAT_CLI_PRINT;
2313 return 1;
2314 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002315 }
2316
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002317 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002318 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002319 return 1;
2320 }
2321 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002322 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002323 appctx->st0 = STAT_CLI_PRINT;
2324 return 1;
2325 }
2326 }
2327 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002328 if (strcmp(args[1], "map") == 0 ||
2329 strcmp(args[1], "acl") == 0) {
2330 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002331 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002332
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002333 /* Set flags. */
2334 if (args[1][0] == 'm')
2335 appctx->ctx.map.display_flags = PAT_REF_MAP;
2336 else
2337 appctx->ctx.map.display_flags = PAT_REF_ACL;
2338
2339 /* If the keywork is "map", we expect three parameters, if it
2340 * is "acl", we expect only two parameters
2341 */
2342 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2343 if (!*args[2] || !*args[3] || !*args[4]) {
2344 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2345 appctx->st0 = STAT_CLI_PRINT;
2346 return 1;
2347 }
2348 }
2349 else {
2350 if (!*args[2] || !*args[3]) {
2351 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2352 appctx->st0 = STAT_CLI_PRINT;
2353 return 1;
2354 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002355 }
2356
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002357 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002358 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002359 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002360 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002361 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002362 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002363 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002364 appctx->st0 = STAT_CLI_PRINT;
2365 return 1;
2366 }
2367
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002368 /* The command "add acl" is prohibited if the reference
2369 * use samples.
2370 */
2371 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2372 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2373 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2374 "You must use the command 'add map' to add values.\n";
2375 appctx->st0 = STAT_CLI_PRINT;
2376 return 1;
2377 }
2378
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002379 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002380 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002381 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002382 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002383 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002384 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002385 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002386 if (err)
2387 memprintf(&err, "%s.\n", err);
2388 appctx->ctx.cli.err = err;
2389 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002390 return 1;
2391 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002392
2393 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002394 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002395 return 1;
2396 }
2397 else { /* unknown "del" parameter */
2398 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2399 appctx->st0 = STAT_CLI_PRINT;
2400 return 1;
2401 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002402 }
2403 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002404 return 0;
2405 }
2406 return 1;
2407}
2408
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002409/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002410 * used to processes I/O from/to the stats unix socket. The system relies on a
2411 * state machine handling requests and various responses. We read a request,
2412 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002413 * Then we can read again. The state is stored in appctx->st0 and is one of the
2414 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002415 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002416 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002417static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002418{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002419 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002420 struct channel *req = si_oc(si);
2421 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002422 int reql;
2423 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002424
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002425 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2426 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002427
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002428 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002429 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002430 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002431 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2432 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002433 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002434 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002435 /* Let's close for real now. We just close the request
2436 * side, the conditions below will complete if needed.
2437 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002438 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002439 break;
2440 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002441 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002442 /* ensure we have some output room left in the event we
2443 * would want to return some info right after parsing.
2444 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002445 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002446 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002447 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002448 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002449
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002450 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002451 if (reql <= 0) { /* closed or EOL not found */
2452 if (reql == 0)
2453 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002454 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002455 continue;
2456 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002457
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002458 /* seek for a possible semi-colon. If we find one, we
2459 * replace it with an LF and skip only this part.
2460 */
2461 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002462 if (trash.str[len] == ';') {
2463 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002464 reql = len + 1;
2465 break;
2466 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002467
Willy Tarreau816fc222009-10-04 07:36:58 +02002468 /* now it is time to check that we have a full line,
2469 * remove the trailing \n and possibly \r, then cut the
2470 * line.
2471 */
2472 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002473 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002474 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002475 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002476 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002477
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002478 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002479 len--;
2480
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002481 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002482
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002483 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002484 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002485 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002486 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002487 continue;
2488 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002489 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002490 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002491 else if (strcmp(trash.str, "help") == 0 ||
2492 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002493 appctx->ctx.cli.msg = stats_sock_usage_msg;
2494 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002495 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002496 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002497 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002498 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002499 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002500 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002501 /* if prompt is disabled, print help on empty lines,
2502 * so that the user at least knows how to enable
2503 * prompt and find help.
2504 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002505 appctx->ctx.cli.msg = stats_sock_usage_msg;
2506 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002507 }
2508
2509 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002510 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002511 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002512 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002513 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002514 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002515 case STAT_CLI_PROMPT:
2516 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002517 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002518 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002519 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002520 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002521 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002522 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002523 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002524 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002525 free(appctx->ctx.cli.err);
2526 appctx->st0 = STAT_CLI_PROMPT;
2527 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002528 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002529 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002530 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002531 case STAT_CLI_O_BACKEND:
2532 if (stats_dump_backend_to_buffer(si))
2533 appctx->st0 = STAT_CLI_PROMPT;
2534 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002535 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002536 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002537 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002538 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002539 case STAT_CLI_O_SERVERS_STATE:
2540 if (stats_dump_servers_state_to_buffer(si))
2541 appctx->st0 = STAT_CLI_PROMPT;
2542 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002543 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002544 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002545 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002546 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002547 case STAT_CLI_O_RESOLVERS:
2548 if (stats_dump_resolvers_to_buffer(si))
2549 appctx->st0 = STAT_CLI_PROMPT;
2550 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002551 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002552 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002553 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002554 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002555 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002556 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002557 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002558 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002559 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002560 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002561 if (stats_table_request(si, appctx->st0))
2562 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002563 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002564 case STAT_CLI_O_PATS:
2565 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002566 appctx->st0 = STAT_CLI_PROMPT;
2567 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002568 case STAT_CLI_O_PAT:
2569 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002570 appctx->st0 = STAT_CLI_PROMPT;
2571 break;
2572 case STAT_CLI_O_MLOOK:
2573 if (stats_map_lookup(si))
2574 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002575 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002576 case STAT_CLI_O_POOLS:
2577 if (stats_dump_pools_to_buffer(si))
2578 appctx->st0 = STAT_CLI_PROMPT;
2579 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002580#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2581 case STAT_CLI_O_TLSK:
2582 if (stats_tlskeys_list(si))
2583 appctx->st0 = STAT_CLI_PROMPT;
2584 break;
2585#endif
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002586 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002587 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002588 break;
2589 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002590
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002591 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002592 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002593 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002594 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002595 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002596 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002597 }
2598
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002599 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002600 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002601 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002602
2603 /* Now we close the output if one of the writers did so,
2604 * or if we're not in interactive mode and the request
2605 * buffer is empty. This still allows pipelined requests
2606 * to be sent in non-interactive mode.
2607 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002608 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2609 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002610 continue;
2611 }
2612
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002613 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002614 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002615 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002616 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002617
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002618 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002619 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2620 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002621 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002622 * and nothing more to consume. This is comparable to a broken pipe, so
2623 * we forward the close to the request side so that it flows upstream to
2624 * the client.
2625 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002626 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002627 }
2628
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002629 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002630 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2631 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2632 /* We have no more processing to do, and nothing more to send, and
2633 * the client side has closed. So we'll forward this state downstream
2634 * on the response buffer.
2635 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002636 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002637 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002638 }
2639
Willy Tarreau828824a2015-04-19 17:20:03 +02002640 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002641 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 +02002642 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002643 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 +02002644}
2645
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002646/* This function dumps information onto the stream interface's read buffer.
2647 * It returns 0 as long as it does not complete, non-zero upon completion.
2648 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002649 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002650static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002651{
2652 unsigned int up = (now.tv_sec - start_date.tv_sec);
2653
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002654#ifdef USE_OPENSSL
2655 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2656 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2657 int ssl_reuse = 0;
2658
2659 if (ssl_key_rate < ssl_sess_rate) {
2660 /* count the ssl reuse ratio and avoid overflows in both directions */
2661 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2662 }
2663#endif
2664
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002665 chunk_printf(&trash,
2666 "Name: " PRODUCT_NAME "\n"
2667 "Version: " HAPROXY_VERSION "\n"
2668 "Release_date: " HAPROXY_DATE "\n"
2669 "Nbproc: %d\n"
2670 "Process_num: %d\n"
2671 "Pid: %d\n"
2672 "Uptime: %dd %dh%02dm%02ds\n"
2673 "Uptime_sec: %d\n"
2674 "Memmax_MB: %d\n"
2675 "Ulimit-n: %d\n"
2676 "Maxsock: %d\n"
2677 "Maxconn: %d\n"
2678 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002679 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002680 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002681 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002682#ifdef USE_OPENSSL
2683 "MaxSslConns: %d\n"
2684 "CurrSslConns: %d\n"
2685 "CumSslConns: %d\n"
2686#endif
2687 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002688 "PipesUsed: %d\n"
2689 "PipesFree: %d\n"
2690 "ConnRate: %d\n"
2691 "ConnRateLimit: %d\n"
2692 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002693 "SessRate: %d\n"
2694 "SessRateLimit: %d\n"
2695 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002696#ifdef USE_OPENSSL
2697 "SslRate: %d\n"
2698 "SslRateLimit: %d\n"
2699 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002700 "SslFrontendKeyRate: %d\n"
2701 "SslFrontendMaxKeyRate: %d\n"
2702 "SslFrontendSessionReuse_pct: %d\n"
2703 "SslBackendKeyRate: %d\n"
2704 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002705 "SslCacheLookups: %u\n"
2706 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002707#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002708 "CompressBpsIn: %u\n"
2709 "CompressBpsOut: %u\n"
2710 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002711#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002712 "ZlibMemUsage: %ld\n"
2713 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002714#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002715 "Tasks: %d\n"
2716 "Run_queue: %d\n"
2717 "Idle_pct: %d\n"
2718 "node: %s\n"
2719 "description: %s\n"
2720 "",
2721 global.nbproc,
2722 relative_pid,
2723 pid,
2724 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2725 up,
2726 global.rlimit_memmax,
2727 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002728 global.maxsock, global.maxconn, global.hardmaxconn,
2729 actconn, totalconn, global.req_count,
2730#ifdef USE_OPENSSL
2731 global.maxsslconn, sslconns, totalsslconns,
2732#endif
2733 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002734 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002735 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002736#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002737 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2738 ssl_key_rate, global.ssl_fe_keys_max,
2739 ssl_reuse,
2740 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002741 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002742#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002743 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2744 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002745#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002746 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002747#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002748 nb_tasks_cur, run_queue_cur, idle_pct,
2749 global.node, global.desc ? global.desc : ""
2750 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002751
Willy Tarreaubc18da12015-03-13 14:00:47 +01002752 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002753 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002754 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002755 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002756
2757 return 1;
2758}
2759
Baptiste Assmann28289462015-07-03 08:01:20 +02002760/* dumps server state information into <buf> for all the servers found in <backend>
2761 * These information are all the parameters which may change during HAProxy runtime.
2762 * By default, we only export to the last known server state file format.
2763 * These information can be used at next startup to recover same level of server state.
2764 */
2765static void dump_servers_state(struct proxy *backend, struct chunk *buf)
2766{
2767 struct server *srv;
2768 char srv_addr[INET6_ADDRSTRLEN + 1];
2769 time_t srv_time_since_last_change;
2770 int bk_f_forced_id, srv_f_forced_id;
2771
2772 /* we don't want to report any state if the backend is not enabled on this process */
2773 if (backend->bind_proc && !(backend->bind_proc & (1UL << (relative_pid - 1))))
2774 return;
2775
2776 srv = backend->srv;
2777
2778 while (srv) {
2779 srv_addr[0] = '\0';
2780 srv_time_since_last_change = 0;
2781 bk_f_forced_id = 0;
2782 srv_f_forced_id = 0;
2783
2784 switch (srv->addr.ss_family) {
2785 case AF_INET:
2786 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
2787 srv_addr, INET_ADDRSTRLEN + 1);
2788 break;
2789 case AF_INET6:
2790 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
2791 srv_addr, INET6_ADDRSTRLEN + 1);
2792 break;
2793 }
2794 srv_time_since_last_change = now.tv_sec - srv->last_change;
2795 bk_f_forced_id = backend->options & PR_O_FORCED_ID ? 1 : 0;
2796 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
2797
2798 chunk_appendf(buf,
2799 "%d %s "
2800 "%d %s %s "
2801 "%d %d %d %d %ld "
2802 "%d %d %d %d %d "
2803 "%d %d"
2804 "\n",
2805 backend->uuid, backend->id,
2806 srv->puid, srv->id, srv_addr,
2807 srv->state, srv->admin, srv->uweight, srv->iweight, srv_time_since_last_change,
2808 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
2809 bk_f_forced_id, srv_f_forced_id);
2810
2811 srv = srv->next;
2812 }
2813}
2814
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002815/* Parses backend list and simply report backend names */
2816static int stats_dump_backend_to_buffer(struct stream_interface *si)
2817{
2818 extern struct proxy *proxy;
2819 struct proxy *curproxy;
2820
2821 chunk_reset(&trash);
2822 chunk_printf(&trash, "# name\n");
2823
2824 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2825 /* looking for backends only */
2826 if (!(curproxy->cap & PR_CAP_BE))
2827 continue;
2828
2829 /* we don't want to list a backend which is bound to this process */
2830 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
2831 continue;
2832
2833 chunk_appendf(&trash, "%s\n", curproxy->id);
2834 }
2835
2836 if (bi_putchk(si_ic(si), &trash) == -1) {
2837 si_applet_cant_put(si);
2838 return 0;
2839 }
2840
2841 return 1;
2842}
2843
Baptiste Assmann28289462015-07-03 08:01:20 +02002844/* Parses backend list or simply use backend name provided by the user to return
2845 * states of servers to stdout.
2846 */
2847static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
2848{
2849 struct appctx *appctx = __objt_appctx(si->end);
2850 extern struct proxy *proxy;
2851 struct proxy *curproxy;
2852
2853 chunk_reset(&trash);
2854
2855 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
2856
2857 if (appctx->ctx.server_state.backend) {
2858 dump_servers_state(appctx->ctx.server_state.backend, &trash);
2859 }
2860 else {
2861 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2862 /* servers are only in backends */
2863 if (!(curproxy->cap & PR_CAP_BE))
2864 continue;
2865
2866 dump_servers_state(curproxy, &trash);
2867 }
2868 }
2869
2870 if (bi_putchk(si_ic(si), &trash) == -1) {
2871 si_applet_cant_put(si);
2872 return 0;
2873 }
2874
2875 return 1;
2876}
2877
Willy Tarreau12833bb2014-01-28 16:49:56 +01002878/* This function dumps memory usage information onto the stream interface's
2879 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2880 * completion. No state is used.
2881 */
2882static int stats_dump_pools_to_buffer(struct stream_interface *si)
2883{
2884 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01002885 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002886 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01002887 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002888 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01002889 return 1;
2890}
2891
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002892/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2893 * the state from stream interface <si>. The caller is responsible for clearing
2894 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002895 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002896static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002897{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002898 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002899 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002900
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002901 if (!(px->cap & PR_CAP_FE))
2902 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002903
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002904 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002905 return 0;
2906
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002907 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002908 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002909 /* name, queue */
2910 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002911
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002912 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002913 /* Column sub-heading for Enable or Disable server */
2914 chunk_appendf(&trash, "<td></td>");
2915 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002916
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002917 chunk_appendf(&trash,
2918 "<td class=ac>"
2919 "<a name=\"%s/Frontend\"></a>"
2920 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2921 "<td colspan=3></td>"
2922 "",
2923 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002924
Willy Tarreau466c9b52012-12-23 02:25:03 +01002925 chunk_appendf(&trash,
2926 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002927 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002928 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2929 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2930 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002931 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2932 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2933 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002934
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002935 if (px->mode == PR_MODE_HTTP)
2936 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002937 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002938 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002939
2940 chunk_appendf(&trash,
2941 "</table></div></u></td>"
2942 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002943 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002944 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2945 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2946 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002947 U2H(px->fe_counters.sps_max),
2948 U2H(px->fe_counters.cps_max),
2949 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002950
2951 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002952 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002953 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002954 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002955
2956 chunk_appendf(&trash,
2957 "</table></div></u></td>"
2958 /* sessions rate : limit */
2959 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002960 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002961
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002962 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002963 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002964 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002965 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002966 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2967 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002968 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002969 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2970 U2H(px->fe_counters.cum_sess),
2971 U2H(px->fe_counters.cum_conn),
2972 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002973
Willy Tarreau466c9b52012-12-23 02:25:03 +01002974 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002975 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002976 chunk_appendf(&trash,
2977 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2978 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2979 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2980 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2981 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2982 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2983 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2984 "<tr><th>- other responses:</th><td>%s</td></tr>"
2985 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2986 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002987 U2H(px->fe_counters.p.http.cum_req),
2988 U2H(px->fe_counters.p.http.rsp[1]),
2989 U2H(px->fe_counters.p.http.rsp[2]),
2990 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002991 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002992 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002993 U2H(px->fe_counters.p.http.rsp[3]),
2994 U2H(px->fe_counters.p.http.rsp[4]),
2995 U2H(px->fe_counters.p.http.rsp[5]),
2996 U2H(px->fe_counters.p.http.rsp[0]),
2997 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002998 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002999
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003000 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003001 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003002 /* sessions: lbtot, lastsess */
3003 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003004 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003005 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003006 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003007 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003008
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003009 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003010 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003011 "<td>%s%s<div class=tips><table class=det>"
3012 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3013 "<tr><th>Compression in:</th><td>%s</td></tr>"
3014 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3015 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3016 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3017 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003018 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003019 U2H(px->fe_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003020 U2H(px->fe_counters.bytes_out),
3021 U2H(px->fe_counters.comp_in),
3022 U2H(px->fe_counters.comp_out),
3023 px->fe_counters.comp_in ? (int)(px->fe_counters.comp_out * 100 / px->fe_counters.comp_in) : 0,
3024 U2H(px->fe_counters.comp_byp),
3025 U2H(px->fe_counters.comp_in - px->fe_counters.comp_out),
3026 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 +01003027 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003028
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003029 chunk_appendf(&trash,
3030 /* denied: req, resp */
3031 "<td>%s</td><td>%s</td>"
3032 /* errors : request, connect, response */
3033 "<td>%s</td><td></td><td></td>"
3034 /* warnings: retries, redispatches */
3035 "<td></td><td></td>"
3036 /* server status : reflect frontend status */
3037 "<td class=ac>%s</td>"
3038 /* rest of server: nothing */
3039 "<td class=ac colspan=8></td></tr>"
3040 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003041 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
3042 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003043 px->state == PR_STREADY ? "OPEN" :
3044 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003045 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003046 else { /* CSV mode */
3047 chunk_appendf(&trash,
3048 /* pxid, name, queue cur, queue max, */
3049 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003050 /* sessions : current, max, limit, total */
3051 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003052 /* bytes : in, out */
3053 "%lld,%lld,"
3054 /* denied: req, resp */
3055 "%lld,%lld,"
3056 /* errors : request, connect, response */
3057 "%lld,,,"
3058 /* warnings: retries, redispatches */
3059 ",,"
3060 /* server status : reflect frontend status */
3061 "%s,"
3062 /* rest of server: nothing */
3063 ",,,,,,,,"
3064 /* pid, iid, sid, throttle, lbtot, tracked, type */
3065 "%d,%d,0,,,,%d,"
3066 /* rate, rate_lim, rate_max */
3067 "%u,%u,%u,"
3068 /* check_status, check_code, check_duration */
3069 ",,,",
3070 px->id,
3071 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
3072 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
3073 px->fe_counters.denied_req, px->fe_counters.denied_resp,
3074 px->fe_counters.failed_req,
3075 px->state == PR_STREADY ? "OPEN" :
3076 px->state == PR_STFULL ? "FULL" : "STOP",
3077 relative_pid, px->uuid, STATS_TYPE_FE,
3078 read_freq_ctr(&px->fe_sess_per_sec),
3079 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003080
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003081 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3082 if (px->mode == PR_MODE_HTTP) {
3083 for (i=1; i<6; i++)
3084 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
3085 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
3086 }
3087 else
3088 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003089
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003090 /* failed health analyses */
3091 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003092
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003093 /* requests : req_rate, req_rate_max, req_tot, */
3094 chunk_appendf(&trash, "%u,%u,%lld,",
3095 read_freq_ctr(&px->fe_req_per_sec),
3096 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
3097
3098 /* errors: cli_aborts, srv_aborts */
3099 chunk_appendf(&trash, ",,");
3100
3101 /* compression: in, out, bypassed */
3102 chunk_appendf(&trash, "%lld,%lld,%lld,",
3103 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
3104
3105 /* compression: comp_rsp */
3106 chunk_appendf(&trash, "%lld,",
3107 px->fe_counters.p.http.comp_rsp);
3108
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003109 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3110 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003111
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003112 /* finish with EOL */
3113 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003114 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003115 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003116}
3117
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003118/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
3119 * from stream interface <si>, and stats flags <flags>. The caller is responsible
3120 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3121 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02003122 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003123static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02003124{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003125 struct appctx *appctx = __objt_appctx(si->end);
3126
3127 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003128 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003129 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003130 /* Column sub-heading for Enable or Disable server */
3131 chunk_appendf(&trash, "<td></td>");
3132 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003133 chunk_appendf(&trash,
3134 /* frontend name, listener name */
3135 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3136 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3137 "",
3138 px->id, l->name,
3139 (flags & ST_SHLGNDS)?"<u>":"",
3140 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02003141
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003142 if (flags & ST_SHLGNDS) {
3143 char str[INET6_ADDRSTRLEN];
3144 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02003145
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003146 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02003147
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003148 port = get_host_port(&l->addr);
3149 switch (addr_to_str(&l->addr, str, sizeof(str))) {
3150 case AF_INET:
3151 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
3152 break;
3153 case AF_INET6:
3154 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
3155 break;
3156 case AF_UNIX:
3157 chunk_appendf(&trash, "unix, ");
3158 break;
3159 case -1:
3160 chunk_appendf(&trash, "(%s), ", strerror(errno));
3161 break;
3162 }
Willy Tarreau91861262007-10-17 17:06:05 +02003163
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003164 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003165 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003166 }
Willy Tarreau91861262007-10-17 17:06:05 +02003167
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003168 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003169 /* queue */
3170 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003171 /* sessions rate: current, max, limit */
3172 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003173 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003174 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003175 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003176 /* bytes: in, out */
3177 "<td>%s</td><td>%s</td>"
3178 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003179 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003180 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
3181 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02003182
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003183 chunk_appendf(&trash,
3184 /* denied: req, resp */
3185 "<td>%s</td><td>%s</td>"
3186 /* errors: request, connect, response */
3187 "<td>%s</td><td></td><td></td>"
3188 /* warnings: retries, redispatches */
3189 "<td></td><td></td>"
3190 /* server status: reflect listener status */
3191 "<td class=ac>%s</td>"
3192 /* rest of server: nothing */
3193 "<td class=ac colspan=8></td></tr>"
3194 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003195 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
3196 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003197 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
3198 }
3199 else { /* CSV mode */
3200 chunk_appendf(&trash,
3201 /* pxid, name, queue cur, queue max, */
3202 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003203 /* sessions: current, max, limit, total */
3204 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003205 /* bytes: in, out */
3206 "%lld,%lld,"
3207 /* denied: req, resp */
3208 "%lld,%lld,"
3209 /* errors: request, connect, response */
3210 "%lld,,,"
3211 /* warnings: retries, redispatches */
3212 ",,"
3213 /* server status: reflect listener status */
3214 "%s,"
3215 /* rest of server: nothing */
3216 ",,,,,,,,"
3217 /* pid, iid, sid, throttle, lbtot, tracked, type */
3218 "%d,%d,%d,,,,%d,"
3219 /* rate, rate_lim, rate_max */
3220 ",,,"
3221 /* check_status, check_code, check_duration */
3222 ",,,"
3223 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3224 ",,,,,,"
3225 /* failed health analyses */
3226 ","
3227 /* requests : req_rate, req_rate_max, req_tot, */
3228 ",,,"
3229 /* errors: cli_aborts, srv_aborts */
3230 ",,"
3231 /* compression: in, out, bypassed, comp_rsp */
3232 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003233 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3234 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003235 "\n",
3236 px->id, l->name,
3237 l->nbconn, l->counters->conn_max,
3238 l->maxconn, l->counters->cum_conn,
3239 l->counters->bytes_in, l->counters->bytes_out,
3240 l->counters->denied_req, l->counters->denied_resp,
3241 l->counters->failed_req,
3242 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
3243 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
3244 }
3245 return 1;
3246}
Willy Tarreau91861262007-10-17 17:06:05 +02003247
Simon Horman4d2eab62015-04-23 14:51:26 +09003248enum srv_stats_state {
3249 SRV_STATS_STATE_DOWN = 0,
3250 SRV_STATS_STATE_DOWN_AGENT,
3251 SRV_STATS_STATE_GOING_UP,
3252 SRV_STATS_STATE_UP_GOING_DOWN,
3253 SRV_STATS_STATE_UP,
3254 SRV_STATS_STATE_NOLB_GOING_DOWN,
3255 SRV_STATS_STATE_NOLB,
3256 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3257 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003258 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003259 SRV_STATS_STATE_NO_CHECK,
3260
3261 SRV_STATS_STATE_COUNT, /* Must be last */
3262};
3263
Simon Horman837bfa72015-04-23 14:51:27 +09003264enum srv_stats_colour {
3265 SRV_STATS_COLOUR_DOWN = 0,
3266 SRV_STATS_COLOUR_GOING_UP,
3267 SRV_STATS_COLOUR_GOING_DOWN,
3268 SRV_STATS_COLOUR_UP,
3269 SRV_STATS_COLOUR_NOLB,
3270 SRV_STATS_COLOUR_DRAINING,
3271 SRV_STATS_COLOUR_NO_CHECK,
3272
3273 SRV_STATS_COLOUR_COUNT, /* Must be last */
3274};
3275
3276static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3277 [SRV_STATS_COLOUR_DOWN] = "down",
3278 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3279 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3280 [SRV_STATS_COLOUR_UP] = "up",
3281 [SRV_STATS_COLOUR_NOLB] = "nolb",
3282 [SRV_STATS_COLOUR_DRAINING] = "draining",
3283 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3284};
3285
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003286/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3287 * from stream interface <si>, stats flags <flags>, and server state <state>.
3288 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003289 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003290 */
Simon Horman4d2eab62015-04-23 14:51:26 +09003291static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09003292 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003293{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003294 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003295 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003296 char str[INET6_ADDRSTRLEN];
3297 struct chunk src;
3298 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02003299
Willy Tarreau32091232014-05-16 13:52:00 +02003300 /* we have "via" which is the tracked server as described in the configuration,
3301 * and "ref" which is the checked server and the end of the chain.
3302 */
3303 via = sv->track ? sv->track : sv;
3304 ref = via;
3305 while (ref->track)
3306 ref = ref->track;
3307
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003308 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003309 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3310 [SRV_STATS_STATE_DOWN] = "DOWN",
3311 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3312 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
3313 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
3314 [SRV_STATS_STATE_UP] = "UP",
3315 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
3316 [SRV_STATS_STATE_NOLB] = "NOLB",
3317 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
3318 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003319 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003320 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003321 };
Willy Tarreau91861262007-10-17 17:06:05 +02003322
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003323 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003324 chunk_appendf(&trash, "<tr class=\"maintain\">");
3325 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003326 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003327 "<tr class=\"%s_%s\">",
3328 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003329
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003330 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003331 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3333 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003334
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003335 chunk_appendf(&trash,
3336 "<td class=ac><a name=\"%s/%s\"></a>%s"
3337 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3338 "",
3339 px->id, sv->id,
3340 (flags & ST_SHLGNDS) ? "<u>" : "",
3341 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003342
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003344 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003345
3346 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3347 case AF_INET:
3348 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3349 break;
3350 case AF_INET6:
3351 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3352 break;
3353 case AF_UNIX:
3354 chunk_appendf(&trash, "unix, ");
3355 break;
3356 case -1:
3357 chunk_appendf(&trash, "(%s), ", strerror(errno));
3358 break;
3359 default: /* address family not supported */
3360 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003361 }
Willy Tarreau91861262007-10-17 17:06:05 +02003362
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 /* id */
3364 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003365
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003366 /* cookie */
3367 if (sv->cookie) {
3368 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003369
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003370 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3371 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003372
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003374 }
3375
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003376 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003377 }
Willy Tarreau91861262007-10-17 17:06:05 +02003378
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003380 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003381 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003382 /* sessions rate : current, max, limit */
3383 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003384 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003385 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003386 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3387 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003388
Willy Tarreau466c9b52012-12-23 02:25:03 +01003389
3390 chunk_appendf(&trash,
3391 /* sessions: current, max, limit, total */
3392 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003393 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003394 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3395 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003396 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3397 U2H(sv->counters.cum_sess),
3398 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003399
Willy Tarreau466c9b52012-12-23 02:25:03 +01003400 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3401 if (px->mode == PR_MODE_HTTP) {
3402 unsigned long long tot;
3403 for (tot = i = 0; i < 6; i++)
3404 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405
Willy Tarreau466c9b52012-12-23 02:25:03 +01003406 chunk_appendf(&trash,
3407 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3408 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3409 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3410 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3411 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3412 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3413 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3414 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003415 U2H(tot),
3416 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3417 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3418 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3419 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3420 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3421 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 +02003422 }
Willy Tarreau91861262007-10-17 17:06:05 +02003423
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003424 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3425 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)));
3426 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)));
3427 if (px->mode == PR_MODE_HTTP)
3428 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)));
3429 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)));
3430
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003431 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003432 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003433 /* sessions: lbtot, last */
3434 "<td>%s</td><td>%s</td>",
3435 U2H(sv->counters.cum_lbconn),
3436 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003437
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 chunk_appendf(&trash,
3439 /* bytes : in, out */
3440 "<td>%s</td><td>%s</td>"
3441 /* denied: req, resp */
3442 "<td></td><td>%s</td>"
3443 /* errors : request, connect */
3444 "<td></td><td>%s</td>"
3445 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003446 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003447 /* warnings: retries, redispatches */
3448 "<td>%lld</td><td>%lld</td>"
3449 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003450 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3451 U2H(sv->counters.failed_secu),
3452 U2H(sv->counters.failed_conns),
3453 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003454 sv->counters.cli_aborts,
3455 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003456 sv->counters.retries, sv->counters.redispatches);
3457
3458 /* status, lest check */
3459 chunk_appendf(&trash, "<td class=ac>");
3460
Willy Tarreau20125212014-05-13 19:44:56 +02003461 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003462 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3463 chunk_appendf(&trash, "MAINT");
3464 }
Simon Horman0766e442014-11-12 15:55:54 +09003465 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003466 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003467 /* DOWN (agent) */
3468 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 +02003469 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003470 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003471 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3472 chunk_appendf(&trash,
3473 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003474 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3475 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003476 }
Willy Tarreau91861262007-10-17 17:06:05 +02003477
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003478 if ((sv->state == SRV_ST_STOPPED) &&
3479 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3480 chunk_appendf(&trash,
3481 "</td><td class=ac><u> %s%s",
3482 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3483 get_check_status_info(sv->agent.status));
3484
3485 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3486 chunk_appendf(&trash, "/%d", sv->agent.code);
3487
3488 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3489 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3490
3491 chunk_appendf(&trash, "<div class=tips>%s",
3492 get_check_status_description(sv->agent.status));
3493 if (*sv->agent.desc) {
3494 chunk_appendf(&trash, ": ");
3495 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3496 chunk_htmlencode(&trash, &src);
3497 }
3498 chunk_appendf(&trash, "</div></u>");
3499 }
3500 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003501 chunk_appendf(&trash,
3502 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003503 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003504 get_check_status_info(sv->check.status));
3505
3506 if (sv->check.status >= HCHK_STATUS_L57DATA)
3507 chunk_appendf(&trash, "/%d", sv->check.code);
3508
3509 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003510 chunk_appendf(&trash, " in %lums", sv->check.duration);
3511
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003512 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003513 get_check_status_description(sv->check.status));
3514 if (*sv->check.desc) {
3515 chunk_appendf(&trash, ": ");
3516 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3517 chunk_htmlencode(&trash, &src);
3518 }
3519 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003520 }
3521 else
3522 chunk_appendf(&trash, "</td><td>");
3523
3524 chunk_appendf(&trash,
3525 /* weight */
3526 "</td><td class=ac>%d</td>"
3527 /* act, bck */
3528 "<td class=ac>%s</td><td class=ac>%s</td>"
3529 "",
3530 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003531 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3532 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003533
3534 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003535 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003536 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003537
3538 if (ref->observe)
3539 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3540
3541 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003542 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003543 "<td>%lld</td><td>%s</td>"
3544 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003545 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003546 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3547 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003548 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3549 /* tracking a server */
3550 chunk_appendf(&trash,
3551 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003552 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003553 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003554 else
3555 chunk_appendf(&trash, "<td colspan=3></td>");
3556
3557 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003558 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003559 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003560 else
3561 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3562 }
3563 else { /* CSV mode */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003564 struct chunk *out = get_trash_chunk();
Simon Horman4d2eab62015-04-23 14:51:26 +09003565 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3566 [SRV_STATS_STATE_DOWN] = "DOWN,",
3567 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3568 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3569 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3570 [SRV_STATS_STATE_UP] = "UP,",
3571 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3572 [SRV_STATS_STATE_NOLB] = "NOLB,",
3573 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3574 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003575 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003576 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003577 };
3578
3579 chunk_appendf(&trash,
3580 /* pxid, name */
3581 "%s,%s,"
3582 /* queue : current, max */
3583 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003584 /* sessions : current, max, limit, total */
3585 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003586 /* bytes : in, out */
3587 "%lld,%lld,"
3588 /* denied: req, resp */
3589 ",%lld,"
3590 /* errors : request, connect, response */
3591 ",%lld,%lld,"
3592 /* warnings: retries, redispatches */
3593 "%lld,%lld,"
3594 "",
3595 px->id, sv->id,
3596 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003597 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003598 sv->counters.bytes_in, sv->counters.bytes_out,
3599 sv->counters.failed_secu,
3600 sv->counters.failed_conns, sv->counters.failed_resp,
3601 sv->counters.retries, sv->counters.redispatches);
3602
3603 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003604 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003605 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003606 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003607 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003608 else
3609 chunk_appendf(&trash,
3610 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003611 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3612 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003613
3614 chunk_appendf(&trash,
3615 /* weight, active, backup */
3616 "%d,%d,%d,"
3617 "",
3618 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003619 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3620 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003621
3622 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003623 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003624 chunk_appendf(&trash,
3625 "%lld,%lld,%d,%d,",
3626 sv->counters.failed_checks, sv->counters.down_trans,
3627 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3628 else
3629 chunk_appendf(&trash, ",,,,");
3630
3631 /* queue limit, pid, iid, sid, */
3632 chunk_appendf(&trash,
3633 "%s,"
3634 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003635 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003636 relative_pid, px->uuid, sv->puid);
3637
3638 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003639 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003640 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003641
3642 /* sessions: lbtot */
3643 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3644
3645 /* tracked */
3646 if (sv->track)
3647 chunk_appendf(&trash, "%s/%s,",
3648 sv->track->proxy->id, sv->track->id);
3649 else
3650 chunk_appendf(&trash, ",");
3651
3652 /* type */
3653 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3654
3655 /* rate */
3656 chunk_appendf(&trash, "%u,,%u,",
3657 read_freq_ctr(&sv->sess_per_sec),
3658 sv->counters.sps_max);
3659
Willy Tarreauff5ae352013-12-11 20:36:34 +01003660 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003661 /* check_status */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003662 chunk_appendf(&trash, "%s,", csv_enc(get_check_status_info(sv->check.status), 1, out));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003663
3664 /* check_code */
3665 if (sv->check.status >= HCHK_STATUS_L57DATA)
3666 chunk_appendf(&trash, "%u,", sv->check.code);
3667 else
3668 chunk_appendf(&trash, ",");
3669
3670 /* check_duration */
3671 if (sv->check.status >= HCHK_STATUS_CHECKED)
3672 chunk_appendf(&trash, "%lu,", sv->check.duration);
3673 else
3674 chunk_appendf(&trash, ",");
3675
3676 }
3677 else
3678 chunk_appendf(&trash, ",,,");
3679
3680 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3681 if (px->mode == PR_MODE_HTTP) {
3682 for (i=1; i<6; i++)
3683 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3684
3685 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3686 }
3687 else
3688 chunk_appendf(&trash, ",,,,,,");
3689
3690 /* failed health analyses */
3691 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3692
3693 /* requests : req_rate, req_rate_max, req_tot, */
3694 chunk_appendf(&trash, ",,,");
3695
3696 /* errors: cli_aborts, srv_aborts */
3697 chunk_appendf(&trash, "%lld,%lld,",
3698 sv->counters.cli_aborts, sv->counters.srv_aborts);
3699
3700 /* compression: in, out, bypassed, comp_rsp */
3701 chunk_appendf(&trash, ",,,,");
3702
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003703 /* lastsess */
3704 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3705
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003706 /* capture of last check and agent statuses */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003707 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->check.desc), 1, out) : "");
3708 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 +02003709
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003710 /* qtime, ctime, rtime, ttime, */
3711 chunk_appendf(&trash, "%u,%u,%u,%u,",
3712 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3713 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3714 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3715 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3716
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003717 /* finish with EOL */
3718 chunk_appendf(&trash, "\n");
3719 }
3720 return 1;
3721}
3722
3723/* Dumps a line for backend <px> to the trash for and uses the state from stream
3724 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3725 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3726 */
3727static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3728{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003729 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003730 struct chunk src;
3731 int i;
3732
3733 if (!(px->cap & PR_CAP_BE))
3734 return 0;
3735
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003736 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003737 return 0;
3738
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003739 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003740 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003741 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003742 /* Column sub-heading for Enable or Disable server */
3743 chunk_appendf(&trash, "<td></td>");
3744 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003745 chunk_appendf(&trash,
3746 "<td class=ac>"
3747 /* name */
3748 "%s<a name=\"%s/Backend\"></a>"
3749 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3750 "",
3751 (flags & ST_SHLGNDS)?"<u>":"",
3752 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003753
3754 if (flags & ST_SHLGNDS) {
3755 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003756 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003757 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3758
3759 /* cookie */
3760 if (px->cookie_name) {
3761 chunk_appendf(&trash, ", cookie: '");
3762 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3763 chunk_htmlencode(&trash, &src);
3764 chunk_appendf(&trash, "'");
3765 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003766 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003767 }
3768
3769 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003770 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003771 /* queue : current, max */
3772 "<td>%s</td><td>%s</td><td></td>"
3773 /* sessions rate : current, max, limit */
3774 "<td>%s</td><td>%s</td><td></td>"
3775 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003776 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003777 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3778 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003779
3780 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003781 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003782 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003783 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003784 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003785 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003786 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3787 U2H(px->be_counters.cum_conn),
3788 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003789
Willy Tarreau466c9b52012-12-23 02:25:03 +01003790 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003791 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003792 chunk_appendf(&trash,
3793 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3794 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3795 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3796 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3797 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3798 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3799 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3800 "<tr><th>- other responses:</th><td>%s</td></tr>"
3801 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003802 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003803 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003804 U2H(px->be_counters.p.http.cum_req),
3805 U2H(px->be_counters.p.http.rsp[1]),
3806 U2H(px->be_counters.p.http.rsp[2]),
3807 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003808 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003809 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003810 U2H(px->be_counters.p.http.rsp[3]),
3811 U2H(px->be_counters.p.http.rsp[4]),
3812 U2H(px->be_counters.p.http.rsp[5]),
3813 U2H(px->be_counters.p.http.rsp[0]),
3814 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003815 }
3816
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003817 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)));
3818 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)));
3819 if (px->mode == PR_MODE_HTTP)
3820 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)));
3821 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)));
3822
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003823 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003824 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003825 /* sessions: lbtot, last */
3826 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003827 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003828 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003829 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003830 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003831 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003832 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003833
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003834 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003835 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003836 "<td>%s%s<div class=tips><table class=det>"
3837 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3838 "<tr><th>Compression in:</th><td>%s</td></tr>"
3839 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3840 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3841 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3842 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003843 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003844 U2H(px->be_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003845 U2H(px->be_counters.bytes_out),
3846 U2H(px->be_counters.comp_in),
3847 U2H(px->be_counters.comp_out),
3848 px->be_counters.comp_in ? (int)(px->be_counters.comp_out * 100 / px->be_counters.comp_in) : 0,
3849 U2H(px->be_counters.comp_byp),
3850 U2H(px->be_counters.comp_in - px->be_counters.comp_out),
3851 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 +01003852 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3853
3854 chunk_appendf(&trash,
3855 /* denied: req, resp */
3856 "<td>%s</td><td>%s</td>"
3857 /* errors : request, connect */
3858 "<td></td><td>%s</td>"
3859 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003860 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003861 /* warnings: retries, redispatches */
3862 "<td>%lld</td><td>%lld</td>"
3863 /* backend status: reflect backend status (up/down): we display UP
3864 * if the backend has known working servers or if it has no server at
3865 * all (eg: for stats). Then we display the total weight, number of
3866 * active and backups. */
3867 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3868 "<td class=ac>%d</td><td class=ac>%d</td>"
3869 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003870 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3871 U2H(px->be_counters.failed_conns),
3872 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003873 px->be_counters.cli_aborts,
3874 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003875 px->be_counters.retries, px->be_counters.redispatches,
3876 human_time(now.tv_sec - px->last_change, 1),
3877 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3878 "<font color=\"red\"><b>DOWN</b></font>",
3879 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3880 px->srv_act, px->srv_bck);
3881
3882 chunk_appendf(&trash,
3883 /* rest of backend: nothing, down transitions, total downtime, throttle */
3884 "<td class=ac>&nbsp;</td><td>%d</td>"
3885 "<td>%s</td>"
3886 "<td></td>"
3887 "</tr>",
3888 px->down_trans,
3889 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3890 }
3891 else { /* CSV mode */
3892 chunk_appendf(&trash,
3893 /* pxid, name */
3894 "%s,BACKEND,"
3895 /* queue : current, max */
3896 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003897 /* sessions : current, max, limit, total */
3898 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003899 /* bytes : in, out */
3900 "%lld,%lld,"
3901 /* denied: req, resp */
3902 "%lld,%lld,"
3903 /* errors : request, connect, response */
3904 ",%lld,%lld,"
3905 /* warnings: retries, redispatches */
3906 "%lld,%lld,"
3907 /* backend status: reflect backend status (up/down): we display UP
3908 * if the backend has known working servers or if it has no server at
3909 * all (eg: for stats). Then we display the total weight, number of
3910 * active and backups. */
3911 "%s,"
3912 "%d,%d,%d,"
3913 /* rest of backend: nothing, down transitions, last change, total downtime */
3914 ",%d,%d,%d,,"
3915 /* pid, iid, sid, throttle, lbtot, tracked, type */
3916 "%d,%d,0,,%lld,,%d,"
3917 /* rate, rate_lim, rate_max, */
3918 "%u,,%u,"
3919 /* check_status, check_code, check_duration */
3920 ",,,",
3921 px->id,
3922 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3923 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3924 px->be_counters.bytes_in, px->be_counters.bytes_out,
3925 px->be_counters.denied_req, px->be_counters.denied_resp,
3926 px->be_counters.failed_conns, px->be_counters.failed_resp,
3927 px->be_counters.retries, px->be_counters.redispatches,
3928 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3929 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3930 px->srv_act, px->srv_bck,
3931 px->down_trans, (int)(now.tv_sec - px->last_change),
3932 px->srv?be_downtime(px):0,
3933 relative_pid, px->uuid,
3934 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3935 read_freq_ctr(&px->be_sess_per_sec),
3936 px->be_counters.sps_max);
3937
3938 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3939 if (px->mode == PR_MODE_HTTP) {
3940 for (i=1; i<6; i++)
3941 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3942 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3943 }
3944 else
3945 chunk_appendf(&trash, ",,,,,,");
3946
3947 /* failed health analyses */
3948 chunk_appendf(&trash, ",");
3949
3950 /* requests : req_rate, req_rate_max, req_tot, */
3951 chunk_appendf(&trash, ",,,");
3952
3953 /* errors: cli_aborts, srv_aborts */
3954 chunk_appendf(&trash, "%lld,%lld,",
3955 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3956
3957 /* compression: in, out, bypassed */
3958 chunk_appendf(&trash, "%lld,%lld,%lld,",
3959 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3960
3961 /* compression: comp_rsp */
3962 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3963
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003964 /* lastsess, last_chk, last_agt, */
3965 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003966
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003967 /* qtime, ctime, rtime, ttime, */
3968 chunk_appendf(&trash, "%u,%u,%u,%u,",
3969 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3970 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3971 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3972 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3973
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003974 /* finish with EOL */
3975 chunk_appendf(&trash, "\n");
3976 }
3977 return 1;
3978}
3979
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003980/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003981 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003982 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003983 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003984static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003985{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003986 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003987 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3988
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003989 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003990 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003991
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003992 /* 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 +02003993 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003994 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003995 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003996 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 +01003997 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003998 }
3999
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004000 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004001 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004002 }
4003
4004 /* print a new table */
4005 chunk_appendf(&trash,
4006 "<table class=\"tbl\" width=\"100%%\">\n"
4007 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004008 "<th class=\"pxname\" width=\"10%%\">");
4009
4010 chunk_appendf(&trash,
4011 "<a name=\"%s\"></a>%s"
4012 "<a class=px href=\"#%s\">%s</a>",
4013 px->id,
4014 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4015 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004016
4017 if (uri->flags & ST_SHLGNDS) {
4018 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004019 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004020 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4021 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004022 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004023 }
4024
4025 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004026 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004027 "<th class=\"%s\" width=\"90%%\">%s</th>"
4028 "</tr>\n"
4029 "</table>\n"
4030 "<table class=\"tbl\" width=\"100%%\">\n"
4031 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004032 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4033 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4034
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004035 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004036 /* Column heading for Enable or Disable server */
4037 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004038 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004039
4040 chunk_appendf(&trash,
4041 "<th rowspan=2></th>"
4042 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004043 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004044 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4045 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4046 "<th colspan=9>Server</th>"
4047 "</tr>\n"
4048 "<tr class=\"titre\">"
4049 "<th>Cur</th><th>Max</th><th>Limit</th>"
4050 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004051 "<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 +01004052 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4053 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4054 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4055 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4056 "<th>Thrtle</th>\n"
4057 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004058}
4059
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004060/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004061 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004062 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004063static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004064{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004065 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004066 chunk_appendf(&trash, "</table>");
4067
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004068 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004069 /* close the form used to enable/disable this proxy servers */
4070 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004071 "Choose the action to perform on the checked servers : "
4072 "<select name=action>"
4073 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004074 "<option value=\"ready\">Set state to READY</option>"
4075 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004076 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004077 "<option value=\"dhlth\">Health: disable checks</option>"
4078 "<option value=\"ehlth\">Health: enable checks</option>"
4079 "<option value=\"hrunn\">Health: force UP</option>"
4080 "<option value=\"hnolb\">Health: force NOLB</option>"
4081 "<option value=\"hdown\">Health: force DOWN</option>"
4082 "<option value=\"dagent\">Agent: disable checks</option>"
4083 "<option value=\"eagent\">Agent: enable checks</option>"
4084 "<option value=\"arunn\">Agent: force UP</option>"
4085 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004086 "<option value=\"shutdown\">Kill Sessions</option>"
4087 "</select>"
4088 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4089 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4090 "</form>",
4091 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004092 }
4093
4094 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004095}
Willy Tarreau91861262007-10-17 17:06:05 +02004096
4097/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004098 * Dumps statistics for a proxy. The output is sent to the stream interface's
4099 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4100 * buffer space, or non-zero if everything completed. This function is used
4101 * both by the CLI and the HTTP entry points, and is able to dump the output
4102 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004103 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004104static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004105{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004106 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004107 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004108 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004109 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004110 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02004111
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004112 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004113
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004114 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004115 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004116 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004117 if (uri && uri->scope) {
4118 /* we have a limited scope, we have to check the proxy name */
4119 struct stat_scope *scope;
4120 int len;
4121
4122 len = strlen(px->id);
4123 scope = uri->scope;
4124
4125 while (scope) {
4126 /* match exact proxy name */
4127 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4128 break;
4129
4130 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004131 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004132 break;
4133 scope = scope->next;
4134 }
4135
4136 /* proxy name not found : don't dump anything */
4137 if (scope == NULL)
4138 return 1;
4139 }
4140
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004141 /* if the user has requested a limited output and the proxy
4142 * name does not match, skip it.
4143 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004144 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004145 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 +02004146 return 1;
4147
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004148 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4149 (appctx->ctx.stats.iid != -1) &&
4150 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004151 return 1;
4152
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004153 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004154 /* fall through */
4155
Willy Tarreau295a8372011-03-10 11:25:07 +01004156 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004157 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004158 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004159 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004160 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004161 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004162 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004163 }
Willy Tarreau91861262007-10-17 17:06:05 +02004164
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004165 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004166 /* fall through */
4167
Willy Tarreau295a8372011-03-10 11:25:07 +01004168 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004169 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004170 if (stats_dump_fe_stats(si, px)) {
4171 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004172 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004173 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004174 }
4175 }
Willy Tarreau91861262007-10-17 17:06:05 +02004176
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004177 appctx->ctx.stats.l = px->conf.listeners.n;
4178 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004179 /* fall through */
4180
Willy Tarreau295a8372011-03-10 11:25:07 +01004181 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004182 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004183 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004184 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004185 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004186 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004187 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004188
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004189 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004190 if (!l->counters)
4191 continue;
4192
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004193 if (appctx->ctx.stats.flags & STAT_BOUND) {
4194 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004195 break;
4196
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004197 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004198 continue;
4199 }
4200
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004201 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004202 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
4203 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004204 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004205 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004206 }
4207 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004208 }
4209
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004210 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4211 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004212 /* fall through */
4213
Willy Tarreau295a8372011-03-10 11:25:07 +01004214 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004215 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004216 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004217 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09004218 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02004219
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004220 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004221 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004222 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004223 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004224
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004225 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004226
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004227 if (appctx->ctx.stats.flags & STAT_BOUND) {
4228 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004229 break;
4230
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004231 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004232 continue;
4233 }
4234
Willy Tarreau32091232014-05-16 13:52:00 +02004235 svs = sv;
4236 while (svs->track)
4237 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004238
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004239 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004240 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09004241 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004242 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004243 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4244 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004245 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004246 sv_colour = SRV_STATS_COLOUR_UP;
4247 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004248
Simon Horman4463d192015-04-23 14:51:28 +09004249 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
4250 sv_colour = SRV_STATS_COLOUR_DRAINING;
4251
4252 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09004253 if (svs->agent.state & CHK_ST_ENABLED)
4254 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
4255 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09004256 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09004257 else
Simon Horman4d2eab62015-04-23 14:51:26 +09004258 sv_state = SRV_STATS_STATE_DRAIN;
4259 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004260
Simon Horman837bfa72015-04-23 14:51:27 +09004261 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004262 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09004263 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
4264 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004265 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004266 else if (sv->state == SRV_ST_STOPPING) {
4267 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09004268 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004269 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09004270 sv_colour = SRV_STATS_COLOUR_NOLB;
4271 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004272 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004273 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4274 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004275 }
4276 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09004277 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004278 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09004279 sv_colour = SRV_STATS_COLOUR_DOWN;
4280 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004281 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09004282 sv_colour = SRV_STATS_COLOUR_DOWN;
4283 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004284 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004285 sv_colour = SRV_STATS_COLOUR_GOING_UP;
4286 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004287 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09004288 sv_colour = SRV_STATS_COLOUR_DOWN;
4289 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004290 }
Willy Tarreau91861262007-10-17 17:06:05 +02004291
Willy Tarreau9638efa2014-05-23 11:19:57 +02004292 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02004293 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004294 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02004295 continue;
4296 }
4297
Simon Horman837bfa72015-04-23 14:51:27 +09004298 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004299 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004300 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004301 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004302 }
4303 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004304 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004305
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004306 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004307 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004308
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004309 case STAT_PX_ST_BE:
4310 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004311 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
4312 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004313 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004314 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004315 }
4316 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004317
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004318 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004319 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004320
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004321 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004322 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004323 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004324 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004325 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004326 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004327 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004328 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004329
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004330 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004331 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004332
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004333 case STAT_PX_ST_FIN:
4334 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004335
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004336 default:
4337 /* unknown state, we should put an abort() here ! */
4338 return 1;
4339 }
4340}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004341
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004342/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4343 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004344 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004345static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004346{
4347 /* WARNING! This must fit in the first buffer !!! */
4348 chunk_appendf(&trash,
4349 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4350 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4351 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4352 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4353 "<style type=\"text/css\"><!--\n"
4354 "body {"
4355 " font-family: arial, helvetica, sans-serif;"
4356 " font-size: 12px;"
4357 " font-weight: normal;"
4358 " color: black;"
4359 " background: white;"
4360 "}\n"
4361 "th,td {"
4362 " font-size: 10px;"
4363 "}\n"
4364 "h1 {"
4365 " font-size: x-large;"
4366 " margin-bottom: 0.5em;"
4367 "}\n"
4368 "h2 {"
4369 " font-family: helvetica, arial;"
4370 " font-size: x-large;"
4371 " font-weight: bold;"
4372 " font-style: italic;"
4373 " color: #6020a0;"
4374 " margin-top: 0em;"
4375 " margin-bottom: 0em;"
4376 "}\n"
4377 "h3 {"
4378 " font-family: helvetica, arial;"
4379 " font-size: 16px;"
4380 " font-weight: bold;"
4381 " color: #b00040;"
4382 " background: #e8e8d0;"
4383 " margin-top: 0em;"
4384 " margin-bottom: 0em;"
4385 "}\n"
4386 "li {"
4387 " margin-top: 0.25em;"
4388 " margin-right: 2em;"
4389 "}\n"
4390 ".hr {margin-top: 0.25em;"
4391 " border-color: black;"
4392 " border-bottom-style: solid;"
4393 "}\n"
4394 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4395 ".total {background: #20D0D0;color: #ffff80;}\n"
4396 ".frontend {background: #e8e8d0;}\n"
4397 ".socket {background: #d0d0d0;}\n"
4398 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004399 ".active_down {background: #ff9090;}\n"
4400 ".active_going_up {background: #ffd020;}\n"
4401 ".active_going_down {background: #ffffa0;}\n"
4402 ".active_up {background: #c0ffc0;}\n"
4403 ".active_nolb {background: #20a0ff;}\n"
4404 ".active_draining {background: #20a0FF;}\n"
4405 ".active_no_check {background: #e0e0e0;}\n"
4406 ".backup_down {background: #ff9090;}\n"
4407 ".backup_going_up {background: #ff80ff;}\n"
4408 ".backup_going_down {background: #c060ff;}\n"
4409 ".backup_up {background: #b0d0ff;}\n"
4410 ".backup_nolb {background: #90b0e0;}\n"
4411 ".backup_draining {background: #cc9900;}\n"
4412 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004413 ".maintain {background: #c07820;}\n"
4414 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4415 "\n"
4416 "a.px:link {color: #ffff40; text-decoration: none;}"
4417 "a.px:visited {color: #ffff40; text-decoration: none;}"
4418 "a.px:hover {color: #ffffff; text-decoration: none;}"
4419 "a.lfsb:link {color: #000000; text-decoration: none;}"
4420 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4421 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4422 "\n"
4423 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4424 "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"
4425 "table.tbl td.ac { text-align: center;}\n"
4426 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4427 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4428 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4429 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4430 "\n"
4431 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4432 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4433 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004434 "table.det { border-collapse: collapse; border-style: none; }\n"
4435 "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 +01004436 "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 +01004437 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004438 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004439 " display:block;\n"
4440 " visibility:hidden;\n"
4441 " z-index:2147483647;\n"
4442 " position:absolute;\n"
4443 " padding:2px 4px 3px;\n"
4444 " background:#f0f060; color:#000000;\n"
4445 " border:1px solid #7040c0;\n"
4446 " white-space:nowrap;\n"
4447 " font-style:normal;font-size:11px;font-weight:normal;\n"
4448 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4449 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4450 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004451 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004452 "-->\n"
4453 "</style></head>\n",
4454 (uri->flags & ST_SHNODE) ? " on " : "",
4455 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4456 );
4457}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004458
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004459/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004460 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004461 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004462 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004463static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004464{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004465 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004466 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004467 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004468
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004469 /* WARNING! this has to fit the first packet too.
4470 * We are around 3.5 kB, add adding entries will
4471 * become tricky if we want to support 4kB buffers !
4472 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004473 chunk_appendf(&trash,
4474 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4475 PRODUCT_NAME "%s</a></h1>\n"
4476 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4477 "<hr width=\"100%%\" class=\"hr\">\n"
4478 "<h3>&gt; General process information</h3>\n"
4479 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4480 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4481 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4482 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4483 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4484 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4485 "Running tasks: %d/%d; idle = %d %%<br>\n"
4486 "</td><td align=\"center\" nowrap>\n"
4487 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004488 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4489 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004490 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004491 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4492 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004493 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004494 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4495 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004496 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004497 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4498 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004499 "</tr><tr>\n"
4500 "<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 -07004501 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004502 "<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 +01004503 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004504 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004505 "</td>"
4506 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4507 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4508 "",
4509 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4510 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4511 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4512 (uri->flags & ST_SHDESC) ? ": " : "",
4513 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4514 pid, relative_pid, global.nbproc,
4515 up / 86400, (up % 86400) / 3600,
4516 (up % 3600) / 60, (up % 60),
4517 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4518 global.rlimit_memmax ? " MB" : "",
4519 global.rlimit_nofile,
4520 global.maxsock, global.maxconn, global.maxpipes,
4521 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4522 run_queue_cur, nb_tasks_cur, idle_pct
4523 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004524
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004525 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004526 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 +01004527 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004528
4529 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004530 "<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 +01004531 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004532 STAT_SCOPE_TXT_MAXLEN);
4533
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004534 /* 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 +02004535 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004536 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004537 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004538 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 +01004539 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004540 }
4541
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004542 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004543 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004544 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004545 uri->uri_prefix,
4546 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004547 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004548 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004549 else
4550 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004551 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004552 uri->uri_prefix,
4553 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004554 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004555 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004556
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004557 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004558 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004559 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004560 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004561 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004562 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004563 "",
4564 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004565 else
4566 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004567 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004568 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004569 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004570 ";norefresh",
4571 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004572 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004573
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004574 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004575 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004576 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004577 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4578 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004579 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004580
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004581 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004582 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004583 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004584 (uri->refresh > 0) ? ";norefresh" : "",
4585 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004586
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004587 chunk_appendf(&trash,
4588 "</ul></td>"
4589 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4590 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4591 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4592 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4593 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4594 "</ul>"
4595 "</td>"
4596 "</tr></table>\n"
4597 ""
4598 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004599
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004600 if (appctx->ctx.stats.st_code) {
4601 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004602 case STAT_STATUS_DONE:
4603 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004604 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004605 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004606 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004607 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004608 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4609 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004610 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004611 break;
4612 case STAT_STATUS_NONE:
4613 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004614 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004615 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004616 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004617 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004618 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4619 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004620 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004621 break;
4622 case STAT_STATUS_PART:
4623 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004624 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004625 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004626 "Action partially processed.<br>"
4627 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004628 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004629 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4630 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004631 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004632 break;
4633 case STAT_STATUS_ERRP:
4634 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004635 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004636 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004637 "Action not processed because of invalid parameters."
4638 "<ul>"
4639 "<li>The action is maybe unknown.</li>"
4640 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4641 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4642 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004643 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004644 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4645 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004646 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004647 break;
4648 case STAT_STATUS_EXCD:
4649 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004650 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004651 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004652 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4653 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004654 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004655 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4656 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004657 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004658 break;
4659 case STAT_STATUS_DENY:
4660 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004661 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004662 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004663 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004664 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004665 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4666 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004667 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004668 break;
4669 default:
4670 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004671 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004672 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004673 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004674 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004675 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4676 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004677 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004678 }
4679 chunk_appendf(&trash, "<p>\n");
4680 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004681}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004682
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004683/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4684 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004685 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004686static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004687{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004688 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004689}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004690
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004691/* This function dumps statistics onto the stream interface's read buffer in
4692 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004693 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4694 * 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 +02004695 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004696 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004697 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004698static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004699{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004700 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004701 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004702 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004703
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004704 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004705
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004706 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004707 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004708 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004709 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004710
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004711 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004712 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004713 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004714 else
4715 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004716
Willy Tarreaubc18da12015-03-13 14:00:47 +01004717 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004718 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004719 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004720 }
Willy Tarreauae526782010-03-04 20:34:23 +01004721
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004722 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004723 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004724
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004725 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004726 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004727 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004728 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004729 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004730 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004731 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004732 }
Willy Tarreau91861262007-10-17 17:06:05 +02004733
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004734 appctx->ctx.stats.px = proxy;
4735 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4736 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004737 /* fall through */
4738
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004739 case STAT_ST_LIST:
4740 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004741 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004742 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004743 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004744 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004745 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004746
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004747 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004748 /* skip the disabled proxies, global frontend and non-networked ones */
4749 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004750 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004751 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004752
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004753 appctx->ctx.stats.px = px->next;
4754 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004755 }
4756 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004757
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004758 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004759 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004760
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004761 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004762 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004763 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01004764 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004765 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004766 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004767 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004768 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004769
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004770 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004771 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004772
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004773 case STAT_ST_FIN:
4774 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004775
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004776 default:
4777 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004778 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004779 return -1;
4780 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004781}
Willy Tarreauae526782010-03-04 20:34:23 +01004782
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004783/* We reached the stats page through a POST request. The appctx is
4784 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004785 * Parse the posted data and enable/disable servers if necessary.
4786 * Returns 1 if request was parsed or zero if it needs more data.
4787 */
4788static int stats_process_http_post(struct stream_interface *si)
4789{
Willy Tarreau87b09662015-04-03 00:22:06 +02004790 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004791 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004792
4793 struct proxy *px = NULL;
4794 struct server *sv = NULL;
4795
4796 char key[LINESIZE];
4797 int action = ST_ADM_ACTION_NONE;
4798 int reprocess = 0;
4799
4800 int total_servers = 0;
4801 int altered_servers = 0;
4802
4803 char *first_param, *cur_param, *next_param, *end_params;
4804 char *st_cur_param = NULL;
4805 char *st_next_param = NULL;
4806
4807 struct chunk *temp;
4808 int reql;
4809
4810 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02004811 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004812 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004813 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004814 goto out;
4815 }
4816
Willy Tarreaueee5b512015-04-03 23:46:31 +02004817 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 +01004818 if (reql <= 0) {
4819 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004820 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004821 return 0;
4822 }
4823
4824 first_param = temp->str;
4825 end_params = temp->str + reql;
4826 cur_param = next_param = end_params;
4827 *end_params = '\0';
4828
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004829 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004830
4831 /*
4832 * Parse the parameters in reverse order to only store the last value.
4833 * From the html form, the backend and the action are at the end.
4834 */
4835 while (cur_param > first_param) {
4836 char *value;
4837 int poffset, plen;
4838
4839 cur_param--;
4840
4841 if ((*cur_param == '&') || (cur_param == first_param)) {
4842 reprocess_servers:
4843 /* Parse the key */
4844 poffset = (cur_param != first_param ? 1 : 0);
4845 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4846 if ((plen > 0) && (plen <= sizeof(key))) {
4847 strncpy(key, cur_param + poffset, plen);
4848 key[plen - 1] = '\0';
4849 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004850 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004851 goto out;
4852 }
4853
4854 /* Parse the value */
4855 value = key;
4856 while (*value != '\0' && *value != '=') {
4857 value++;
4858 }
4859 if (*value == '=') {
4860 /* Ok, a value is found, we can mark the end of the key */
4861 *value++ = '\0';
4862 }
4863 if (url_decode(key) < 0 || url_decode(value) < 0)
4864 break;
4865
4866 /* Now we can check the key to see what to do */
4867 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02004868 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004869 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004870 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004871 goto out;
4872 }
4873 }
4874 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004875 if (strcmp(value, "ready") == 0) {
4876 action = ST_ADM_ACTION_READY;
4877 }
4878 else if (strcmp(value, "drain") == 0) {
4879 action = ST_ADM_ACTION_DRAIN;
4880 }
4881 else if (strcmp(value, "maint") == 0) {
4882 action = ST_ADM_ACTION_MAINT;
4883 }
4884 else if (strcmp(value, "shutdown") == 0) {
4885 action = ST_ADM_ACTION_SHUTDOWN;
4886 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004887 else if (strcmp(value, "dhlth") == 0) {
4888 action = ST_ADM_ACTION_DHLTH;
4889 }
4890 else if (strcmp(value, "ehlth") == 0) {
4891 action = ST_ADM_ACTION_EHLTH;
4892 }
4893 else if (strcmp(value, "hrunn") == 0) {
4894 action = ST_ADM_ACTION_HRUNN;
4895 }
4896 else if (strcmp(value, "hnolb") == 0) {
4897 action = ST_ADM_ACTION_HNOLB;
4898 }
4899 else if (strcmp(value, "hdown") == 0) {
4900 action = ST_ADM_ACTION_HDOWN;
4901 }
4902 else if (strcmp(value, "dagent") == 0) {
4903 action = ST_ADM_ACTION_DAGENT;
4904 }
4905 else if (strcmp(value, "eagent") == 0) {
4906 action = ST_ADM_ACTION_EAGENT;
4907 }
4908 else if (strcmp(value, "arunn") == 0) {
4909 action = ST_ADM_ACTION_ARUNN;
4910 }
4911 else if (strcmp(value, "adown") == 0) {
4912 action = ST_ADM_ACTION_ADOWN;
4913 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004914 /* else these are the old supported methods */
4915 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004916 action = ST_ADM_ACTION_DISABLE;
4917 }
4918 else if (strcmp(value, "enable") == 0) {
4919 action = ST_ADM_ACTION_ENABLE;
4920 }
4921 else if (strcmp(value, "stop") == 0) {
4922 action = ST_ADM_ACTION_STOP;
4923 }
4924 else if (strcmp(value, "start") == 0) {
4925 action = ST_ADM_ACTION_START;
4926 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004927 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004928 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004929 goto out;
4930 }
4931 }
4932 else if (strcmp(key, "s") == 0) {
4933 if (!(px && action)) {
4934 /*
4935 * Indicates that we'll need to reprocess the parameters
4936 * as soon as backend and action are known
4937 */
4938 if (!reprocess) {
4939 st_cur_param = cur_param;
4940 st_next_param = next_param;
4941 }
4942 reprocess = 1;
4943 }
4944 else if ((sv = findserver(px, value)) != NULL) {
4945 switch (action) {
4946 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004947 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004948 altered_servers++;
4949 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004950 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004951 }
4952 break;
4953 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004954 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004955 altered_servers++;
4956 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004957 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004958 }
4959 break;
4960 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004961 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4962 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4963 altered_servers++;
4964 total_servers++;
4965 }
4966 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004967 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004968 if (sv->admin & SRV_ADMF_FDRAIN) {
4969 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4970 altered_servers++;
4971 total_servers++;
4972 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004973 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004974 case ST_ADM_ACTION_DHLTH:
4975 if (sv->check.state & CHK_ST_CONFIGURED) {
4976 sv->check.state &= ~CHK_ST_ENABLED;
4977 altered_servers++;
4978 total_servers++;
4979 }
4980 break;
4981 case ST_ADM_ACTION_EHLTH:
4982 if (sv->check.state & CHK_ST_CONFIGURED) {
4983 sv->check.state |= CHK_ST_ENABLED;
4984 altered_servers++;
4985 total_servers++;
4986 }
4987 break;
4988 case ST_ADM_ACTION_HRUNN:
4989 if (!(sv->track)) {
4990 sv->check.health = sv->check.rise + sv->check.fall - 1;
4991 srv_set_running(sv, "changed from Web interface");
4992 altered_servers++;
4993 total_servers++;
4994 }
4995 break;
4996 case ST_ADM_ACTION_HNOLB:
4997 if (!(sv->track)) {
4998 sv->check.health = sv->check.rise + sv->check.fall - 1;
4999 srv_set_stopping(sv, "changed from Web interface");
5000 altered_servers++;
5001 total_servers++;
5002 }
5003 break;
5004 case ST_ADM_ACTION_HDOWN:
5005 if (!(sv->track)) {
5006 sv->check.health = 0;
5007 srv_set_stopped(sv, "changed from Web interface");
5008 altered_servers++;
5009 total_servers++;
5010 }
5011 break;
5012 case ST_ADM_ACTION_DAGENT:
5013 if (sv->agent.state & CHK_ST_CONFIGURED) {
5014 sv->agent.state &= ~CHK_ST_ENABLED;
5015 altered_servers++;
5016 total_servers++;
5017 }
5018 break;
5019 case ST_ADM_ACTION_EAGENT:
5020 if (sv->agent.state & CHK_ST_CONFIGURED) {
5021 sv->agent.state |= CHK_ST_ENABLED;
5022 altered_servers++;
5023 total_servers++;
5024 }
5025 break;
5026 case ST_ADM_ACTION_ARUNN:
5027 if (sv->agent.state & CHK_ST_ENABLED) {
5028 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5029 srv_set_running(sv, "changed from Web interface");
5030 altered_servers++;
5031 total_servers++;
5032 }
5033 break;
5034 case ST_ADM_ACTION_ADOWN:
5035 if (sv->agent.state & CHK_ST_ENABLED) {
5036 sv->agent.health = 0;
5037 srv_set_stopped(sv, "changed from Web interface");
5038 altered_servers++;
5039 total_servers++;
5040 }
5041 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005042 case ST_ADM_ACTION_READY:
5043 srv_adm_set_ready(sv);
5044 altered_servers++;
5045 total_servers++;
5046 break;
5047 case ST_ADM_ACTION_DRAIN:
5048 srv_adm_set_drain(sv);
5049 altered_servers++;
5050 total_servers++;
5051 break;
5052 case ST_ADM_ACTION_MAINT:
5053 srv_adm_set_maint(sv);
5054 altered_servers++;
5055 total_servers++;
5056 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005057 case ST_ADM_ACTION_SHUTDOWN:
5058 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005059 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005060
5061 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5062 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005063 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005064
5065 altered_servers++;
5066 total_servers++;
5067 }
5068 break;
5069 }
5070 } else {
5071 /* the server name is unknown or ambiguous (duplicate names) */
5072 total_servers++;
5073 }
5074 }
5075 if (reprocess && px && action) {
5076 /* Now, we know the backend and the action chosen by the user.
5077 * We can safely restart from the first server parameter
5078 * to reprocess them
5079 */
5080 cur_param = st_cur_param;
5081 next_param = st_next_param;
5082 reprocess = 0;
5083 goto reprocess_servers;
5084 }
5085
5086 next_param = cur_param;
5087 }
5088 }
5089
5090 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005091 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005092 }
5093 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005094 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005095 }
5096 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005097 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005098 }
5099 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005100 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005101 }
5102 out:
5103 return 1;
5104}
5105
5106
5107static int stats_send_http_headers(struct stream_interface *si)
5108{
Willy Tarreau87b09662015-04-03 00:22:06 +02005109 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005110 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005111 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005112
5113 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005114 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005115 "Cache-Control: no-cache\r\n"
5116 "Connection: close\r\n"
5117 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005118 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005119
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005120 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005121 chunk_appendf(&trash, "Refresh: %d\r\n",
5122 uri->refresh);
5123
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005124 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5125
5126 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5127 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5128 else
5129 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005130
Willy Tarreaueee5b512015-04-03 23:46:31 +02005131 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005132 s->logs.tv_request = now;
5133
Willy Tarreaubc18da12015-03-13 14:00:47 +01005134 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005135 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005136 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005137 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005138
5139 return 1;
5140}
5141
5142static int stats_send_http_redirect(struct stream_interface *si)
5143{
5144 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005145 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005146 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005147 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005148
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005149 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005150 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005151 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005152 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005153 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 +01005154 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005155 }
5156
5157 /* We don't want to land on the posted stats page because a refresh will
5158 * repost the data. We don't want this to happen on accident so we redirect
5159 * the browse to the stats page with a GET.
5160 */
5161 chunk_printf(&trash,
5162 "HTTP/1.1 303 See Other\r\n"
5163 "Cache-Control: no-cache\r\n"
5164 "Content-Type: text/plain\r\n"
5165 "Connection: close\r\n"
5166 "Location: %s;st=%s%s%s%s\r\n"
5167 "\r\n",
5168 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005169 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5170 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5171 stat_status_codes[appctx->ctx.stats.st_code]) ?
5172 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005173 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005174 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5175 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005176 scope_txt);
5177
Willy Tarreaueee5b512015-04-03 23:46:31 +02005178 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005179 s->logs.tv_request = now;
5180
Willy Tarreaubc18da12015-03-13 14:00:47 +01005181 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005182 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005183 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005184 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005185
5186 return 1;
5187}
5188
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005189/* This I/O handler runs as an applet embedded in a stream interface. It is
5190 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005191 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005192 * automatically unregisters itself once transfer is complete.
5193 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005194static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005195{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005196 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005197 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005198 struct channel *req = si_oc(si);
5199 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005200
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005201 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5202 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005203
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005204 /* check that the output is not closed */
5205 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005206 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005207
Willy Tarreau347a35d2013-11-22 17:51:09 +01005208 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005209 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005210 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005211 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005212 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005213 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005214 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005215 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005216 }
5217
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005218 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005219 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005220 unsigned int data_len;
5221 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005222 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005223
5224 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5225 /* One difficulty we're facing is that we must prevent
5226 * the input data from being automatically forwarded to
5227 * the output area. For this, we temporarily disable
5228 * forwarding on the channel.
5229 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005230 last_fwd = si_ic(si)->to_forward;
5231 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005232 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005233 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005234 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005235 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005236 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005237 }
5238 }
5239
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005240 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005241 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005242 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005243
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005244 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005245
5246 /* Now we must either adjust or remove the chunk size. This is
5247 * not easy because the chunk size might wrap at the end of the
5248 * buffer, so we pretend we have nothing in the buffer, we write
5249 * the size, then restore the buffer's contents. Note that we can
5250 * only do that because no forwarding is scheduled on the stats
5251 * applet.
5252 */
5253 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005254 si_ic(si)->total -= (last_len - prev_len);
5255 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005256
5257 if (last_len != data_len) {
5258 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005259 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005260 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005261
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005262 si_ic(si)->total += (last_len - data_len);
5263 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005264 }
5265 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005266 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005267 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005268 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005269
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005270 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005271 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005272 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005273 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005274 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005275 }
5276
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005277 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005278 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005279 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005280 }
5281
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005282 if (appctx->st0 == STAT_HTTP_DONE) {
5283 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5284 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005285 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005286 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005287 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005288 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005289 }
5290 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005291 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005292 res->flags |= CF_READ_NULL;
5293 si_shutr(si);
5294 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005295
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005296 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5297 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005298
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005299 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005300 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5301 si_shutr(si);
5302 res->flags |= CF_READ_NULL;
5303 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005304 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005305 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005306 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005307}
5308
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005309
Willy Tarreau909d5172012-11-26 03:04:41 +01005310static inline const char *get_conn_ctrl_name(const struct connection *conn)
5311{
Willy Tarreau3c728722014-01-23 13:50:42 +01005312 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005313 return "NONE";
5314 return conn->ctrl->name;
5315}
5316
5317static inline const char *get_conn_xprt_name(const struct connection *conn)
5318{
5319 static char ptr[17];
5320
Willy Tarreauaad69382014-01-23 14:21:42 +01005321 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005322 return "NONE";
5323
5324 if (conn->xprt == &raw_sock)
5325 return "RAW";
5326
5327#ifdef USE_OPENSSL
5328 if (conn->xprt == &ssl_sock)
5329 return "SSL";
5330#endif
5331 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5332 return ptr;
5333}
5334
5335static inline const char *get_conn_data_name(const struct connection *conn)
5336{
5337 static char ptr[17];
5338
5339 if (!conn->data)
5340 return "NONE";
5341
5342 if (conn->data == &sess_conn_cb)
5343 return "SESS";
5344
5345 if (conn->data == &si_conn_cb)
5346 return "STRM";
5347
5348 if (conn->data == &check_conn_cb)
5349 return "CHCK";
5350
5351 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5352 return ptr;
5353}
5354
Willy Tarreau87b09662015-04-03 00:22:06 +02005355/* This function dumps a complete stream state onto the stream interface's
5356 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005357 * 0 if the output buffer is full and it needs to be called again, otherwise
5358 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005359 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005360static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005361{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005362 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005363 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005364 extern const char *monthname[12];
5365 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005366 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005367 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005368
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005369 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005370
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005371 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005372 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005373 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005374 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005375 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005376 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005377 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005378 appctx->ctx.sess.uid = 0;
5379 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005380 return 1;
5381 }
5382
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005383 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005384 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005385 appctx->ctx.sess.uid = sess->uniq_id;
5386 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005387 /* fall through */
5388
5389 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005390 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005391 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005392 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005393 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005394 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5395 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005396 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005397 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005398
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005399 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005400 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005401 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005402 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005403 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005404 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005405 break;
5406 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005407 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005408 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005409 default:
5410 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005411 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005412 break;
5413 }
5414
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005415 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005416 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005417 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005418
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005419 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005420 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005421 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5422 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5423 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005424
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005425 if (conn)
5426 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005427
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005428 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005429 case AF_INET:
5430 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005431 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005432 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005433 break;
5434 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005435 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005436 break;
5437 default:
5438 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005439 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005440 break;
5441 }
5442
Willy Tarreau50943332011-09-02 17:33:05 +02005443 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005444 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005445 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005446 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005447 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5448 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005449 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005450
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005451 conn = objt_conn(sess->si[1].end);
5452 if (conn)
5453 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005454
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005455 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005456 case AF_INET:
5457 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005458 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005459 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005460 break;
5461 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005462 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005463 break;
5464 default:
5465 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005466 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005467 break;
5468 }
5469
5470 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005471 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005472 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005473 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5474 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005475 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005476 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005477
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005478 if (conn)
5479 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005480
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005481 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005482 case AF_INET:
5483 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005484 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005485 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005486 break;
5487 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005488 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005489 break;
5490 default:
5491 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005492 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005493 break;
5494 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005495
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005496 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005497 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005498 sess->task,
5499 sess->task->state,
5500 sess->task->nice, sess->task->calls,
5501 sess->task->expire ?
5502 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5503 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5504 TICKS_TO_MS(1000)) : "<NEVER>",
5505 task_in_rq(sess->task) ? ", running" : "");
5506
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005507 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005508 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005509 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5510
Willy Tarreaueee5b512015-04-03 23:46:31 +02005511 if (sess->txn)
5512 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005513 " 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 +02005514 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5515 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 +01005516
5517 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005518 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005519 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005520 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005521 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005522 obj_type_name(sess->si[0].end),
5523 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005524 sess->si[0].exp ?
5525 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5526 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5527 TICKS_TO_MS(1000)) : "<NEVER>",
5528 sess->si[0].err_type);
5529
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005530 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005531 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005532 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005533 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005534 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005535 obj_type_name(sess->si[1].end),
5536 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005537 sess->si[1].exp ?
5538 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5539 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5540 TICKS_TO_MS(1000)) : "<NEVER>",
5541 sess->si[1].err_type);
5542
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005543 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005544 chunk_appendf(&trash,
5545 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005546 conn,
5547 get_conn_ctrl_name(conn),
5548 get_conn_xprt_name(conn),
5549 get_conn_data_name(conn),
5550 obj_type_name(conn->target),
5551 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005552
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005553 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005554 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005555 conn->flags,
5556 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005557 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005558 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005559 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005560 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005561 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5562 chunk_appendf(&trash,
5563 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5564 tmpctx,
5565 tmpctx->st0,
5566 tmpctx->st1,
5567 tmpctx->st2,
5568 tmpctx->applet->name);
5569 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005570
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005571 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005572 chunk_appendf(&trash,
5573 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005574 conn,
5575 get_conn_ctrl_name(conn),
5576 get_conn_xprt_name(conn),
5577 get_conn_data_name(conn),
5578 obj_type_name(conn->target),
5579 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005580
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005581 chunk_appendf(&trash,
5582 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005583 conn->flags,
5584 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005585 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005586 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005587 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005588 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005589 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5590 chunk_appendf(&trash,
5591 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5592 tmpctx,
5593 tmpctx->st0,
5594 tmpctx->st1,
5595 tmpctx->st2,
5596 tmpctx->applet->name);
5597 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005598
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005599 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005600 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005601 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005602 &sess->req,
5603 sess->req.flags, sess->req.analysers,
5604 sess->req.pipe ? sess->req.pipe->data : 0,
5605 sess->req.to_forward, sess->req.total,
5606 sess->req.analyse_exp ?
5607 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005608 TICKS_TO_MS(1000)) : "<NEVER>");
5609
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005610 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005611 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005612 sess->req.rex ?
5613 human_time(TICKS_TO_MS(sess->req.rex - 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 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005618 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005619 sess->req.wex ?
5620 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005621 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005622 sess->req.buf,
5623 sess->req.buf->data, sess->req.buf->o,
5624 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005625 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005626 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005627
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005628 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005629 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005630 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005631 &sess->res,
5632 sess->res.flags, sess->res.analysers,
5633 sess->res.pipe ? sess->res.pipe->data : 0,
5634 sess->res.to_forward, sess->res.total,
5635 sess->res.analyse_exp ?
5636 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005637 TICKS_TO_MS(1000)) : "<NEVER>");
5638
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005639 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005640 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005641 sess->res.rex ?
5642 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005643 TICKS_TO_MS(1000)) : "<NEVER>");
5644
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005645 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005646 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005647 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005648 sess->res.wex ?
5649 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005650 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005651 sess->res.buf,
5652 sess->res.buf->data, sess->res.buf->o,
5653 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005654 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005655 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005656
Willy Tarreaubc18da12015-03-13 14:00:47 +01005657 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005658 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005659 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005660 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005661
5662 /* use other states to dump the contents */
5663 }
5664 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005665 appctx->ctx.sess.uid = 0;
5666 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005667 return 1;
5668}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005669
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005670#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5671static int stats_tlskeys_list(struct stream_interface *si) {
5672 struct appctx *appctx = __objt_appctx(si->end);
5673
5674 switch (appctx->st2) {
5675 case STAT_ST_INIT:
5676 /* Display the column headers. If the message cannot be sent,
5677 * quit the fucntion with returning 0. The function is called
5678 * later and restart at the state "STAT_ST_INIT".
5679 */
5680 chunk_reset(&trash);
5681 chunk_appendf(&trash, "# id (file)\n");
5682 if (bi_putchk(si_ic(si), &trash) == -1) {
5683 si_applet_cant_put(si);
5684 return 0;
5685 }
5686
5687 /* Now, we start the browsing of the references lists.
5688 * Note that the following call to LIST_ELEM return bad pointer. The only
5689 * avalaible field of this pointer is <list>. It is used with the function
5690 * tlskeys_list_get_next() for retruning the first avalaible entry
5691 */
5692 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
5693 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5694
5695 appctx->st2 = STAT_ST_LIST;
5696 /* fall through */
5697
5698 case STAT_ST_LIST:
5699 while (appctx->ctx.tlskeys.ref) {
5700 chunk_reset(&trash);
5701
5702 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
5703 appctx->ctx.tlskeys.ref->filename);
5704
5705 if (bi_putchk(si_ic(si), &trash) == -1) {
5706 /* let's try again later from this stream. We add ourselves into
5707 * this stream's users so that it can remove us upon termination.
5708 */
5709 si_applet_cant_put(si);
5710 return 0;
5711 }
5712
5713 /* get next list entry and check the end of the list */
5714 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5715 }
5716
5717 appctx->st2 = STAT_ST_FIN;
5718 /* fall through */
5719
5720 default:
5721 appctx->st2 = STAT_ST_FIN;
5722 return 1;
5723 }
5724 return 0;
5725}
5726#endif
5727
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005728static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005729{
5730 struct appctx *appctx = __objt_appctx(si->end);
5731
5732 switch (appctx->st2) {
5733 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005734 /* Display the column headers. If the message cannot be sent,
5735 * quit the fucntion with returning 0. The function is called
5736 * later and restart at the state "STAT_ST_INIT".
5737 */
5738 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005739 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005740 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005741 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005742 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005743 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005744
5745 /* Now, we start the browsing of the references lists.
5746 * Note that the following call to LIST_ELEM return bad pointer. The only
5747 * avalaible field of this pointer is <list>. It is used with the function
5748 * pat_list_get_next() for retruning the first avalaible entry
5749 */
5750 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5751 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5752 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005753 appctx->st2 = STAT_ST_LIST;
5754 /* fall through */
5755
5756 case STAT_ST_LIST:
5757 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005758 chunk_reset(&trash);
5759
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005760 /* Build messages. If the reference is used by another category than
5761 * the listed categorie, display the information in the massage.
5762 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005763 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005764 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5765 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005766
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005767 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005768 /* let's try again later from this stream. We add ourselves into
5769 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005770 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005771 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005772 return 0;
5773 }
5774
5775 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005776 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5777 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005778 }
5779
5780 appctx->st2 = STAT_ST_FIN;
5781 /* fall through */
5782
5783 default:
5784 appctx->st2 = STAT_ST_FIN;
5785 return 1;
5786 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005787 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005788}
5789
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005790static int stats_map_lookup(struct stream_interface *si)
5791{
5792 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005793 struct sample sample;
5794 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005795 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005796
5797 switch (appctx->st2) {
5798 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005799 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005800 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005801 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005802 appctx->st2 = STAT_ST_LIST;
5803 /* fall through */
5804
5805 case STAT_ST_LIST:
5806 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005807 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005808 /* initialise chunk to build new message */
5809 chunk_reset(&trash);
5810
5811 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005812 sample.data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005813 sample.flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005814 sample.data.u.str.len = appctx->ctx.map.chunk.len;
5815 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005816 if (appctx->ctx.map.expr->pat_head->match &&
5817 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005818 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5819 else
5820 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005821
5822 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005823 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5824 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5825 break;
5826 if (match_method >= PAT_MATCH_NUM)
5827 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5828 else
5829 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005830
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005831 /* case sensitive */
5832 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5833 chunk_appendf(&trash, ", case=insensitive");
5834 else
5835 chunk_appendf(&trash, ", case=sensitive");
5836
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005837 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005838 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005839 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5840 chunk_appendf(&trash, ", found=no");
5841 else
5842 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005843 }
5844
5845 /* Display match and match info */
5846 else {
5847 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005848 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5849 chunk_appendf(&trash, ", found=yes");
5850 else
5851 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005852
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005853 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005854 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005855 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005856 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005857 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005858
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005859 /* display pattern */
5860 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5861 if (pat->ref && pat->ref->pattern)
5862 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5863 else
5864 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005865 }
5866 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005867 if (pat->ref && pat->ref->pattern)
5868 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5869 else
5870 chunk_appendf(&trash, ", pattern=unknown");
5871 }
5872
5873 /* display return value */
5874 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02005875 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02005876 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02005877 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005878 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005879 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005880 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005881 }
5882
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005883 chunk_appendf(&trash, "\n");
5884
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005885 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005886 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005887 /* let's try again later from this stream. We add ourselves into
5888 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005889 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005890 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005891 return 0;
5892 }
5893
5894 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005895 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5896 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005897 }
5898
5899 appctx->st2 = STAT_ST_FIN;
5900 /* fall through */
5901
5902 default:
5903 appctx->st2 = STAT_ST_FIN;
5904 free(appctx->ctx.map.chunk.str);
5905 return 1;
5906 }
5907}
5908
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005909static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005910{
5911 struct appctx *appctx = __objt_appctx(si->end);
5912
5913 switch (appctx->st2) {
5914
5915 case STAT_ST_INIT:
5916 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005917 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5918 struct pat_ref_elt *, list);
5919 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5920 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005921 appctx->st2 = STAT_ST_LIST;
5922 /* fall through */
5923
5924 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005925 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005926 chunk_reset(&trash);
5927
5928 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005929 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005930 chunk_appendf(&trash, "%p %s %s\n",
5931 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5932 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005933 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005934 chunk_appendf(&trash, "%p %s\n",
5935 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005936
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005937 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005938 /* let's try again later from this stream. We add ourselves into
5939 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005940 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005941 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005942 return 0;
5943 }
5944
5945 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005946 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5947 struct pat_ref_elt *, list);
5948 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005949 break;
5950 }
5951
5952 appctx->st2 = STAT_ST_FIN;
5953 /* fall through */
5954
5955 default:
5956 appctx->st2 = STAT_ST_FIN;
5957 return 1;
5958 }
5959}
5960
Willy Tarreau87b09662015-04-03 00:22:06 +02005961/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005962 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005963 * to be called again, otherwise non-zero. It is designed to be called
5964 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005965 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005966static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005967{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005968 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005969 struct connection *conn;
5970
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005971 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005972 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02005973 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005974 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005975 if (appctx->st2 == STAT_ST_LIST) {
5976 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5977 LIST_DEL(&appctx->ctx.sess.bref.users);
5978 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005979 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005980 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005981 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005982 }
5983
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005984 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005985
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005986 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005987 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005988 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02005989 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005990 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02005991 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005992 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02005993 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005994 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005995 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02005996 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005997 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005998 /* fall through */
5999
Willy Tarreau295a8372011-03-10 11:25:07 +01006000 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006001 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006002 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6003 LIST_DEL(&appctx->ctx.sess.bref.users);
6004 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006005 }
6006
6007 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006008 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006009 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006010 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006011
Willy Tarreau87b09662015-04-03 00:22:06 +02006012 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006013
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006014 if (appctx->ctx.sess.target) {
6015 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006016 goto next_sess;
6017
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006018 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006019 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006020 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006021 return 0;
6022
Willy Tarreau87b09662015-04-03 00:22:06 +02006023 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006024 LIST_DEL(&appctx->ctx.sess.bref.users);
6025 LIST_INIT(&appctx->ctx.sess.bref.users);
6026 if (appctx->ctx.sess.target != (void *)-1) {
6027 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006028 break;
6029 }
6030 else
6031 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006032 }
6033
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006034 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006035 "%p: proto=%s",
6036 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006037 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006038
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006039 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006040 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006041 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006042 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006043 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006044 " src=%s:%d fe=%s be=%s srv=%s",
6045 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006046 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006047 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006048 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006049 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006050 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006051 break;
6052 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006053 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006054 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006055 strm_li(curr_sess)->luid,
6056 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006057 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006058 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006059 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006060 break;
6061 }
6062
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006063 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006064 " ts=%02x age=%s calls=%d",
6065 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006066 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6067 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006068
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006069 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006070 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006071 curr_sess->req.flags,
6072 curr_sess->req.buf->i,
6073 curr_sess->req.analysers,
6074 curr_sess->req.rex ?
6075 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006076 TICKS_TO_MS(1000)) : "");
6077
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006078 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006079 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006080 curr_sess->req.wex ?
6081 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006082 TICKS_TO_MS(1000)) : "");
6083
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006084 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006085 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006086 curr_sess->req.analyse_exp ?
6087 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006088 TICKS_TO_MS(1000)) : "");
6089
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006090 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006091 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006092 curr_sess->res.flags,
6093 curr_sess->res.buf->i,
6094 curr_sess->res.analysers,
6095 curr_sess->res.rex ?
6096 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006097 TICKS_TO_MS(1000)) : "");
6098
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006099 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006100 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006101 curr_sess->res.wex ?
6102 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006103 TICKS_TO_MS(1000)) : "");
6104
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006105 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006106 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006107 curr_sess->res.analyse_exp ?
6108 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006109 TICKS_TO_MS(1000)) : "");
6110
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006111 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006112 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006113 " s0=[%d,%1xh,fd=%d,ex=%s]",
6114 curr_sess->si[0].state,
6115 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006116 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006117 curr_sess->si[0].exp ?
6118 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6119 TICKS_TO_MS(1000)) : "");
6120
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006121 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006122 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006123 " s1=[%d,%1xh,fd=%d,ex=%s]",
6124 curr_sess->si[1].state,
6125 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006126 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006127 curr_sess->si[1].exp ?
6128 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6129 TICKS_TO_MS(1000)) : "");
6130
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006131 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006132 " exp=%s",
6133 curr_sess->task->expire ?
6134 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6135 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006136 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006137 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006138
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006139 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006140
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006141 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006142 /* let's try again later from this stream. We add ourselves into
6143 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006144 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006145 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006146 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006147 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006148 }
6149
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006150 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006151 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006152 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006153
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006154 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006155 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006156 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006157 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006158 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006159 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006160
Willy Tarreaubc18da12015-03-13 14:00:47 +01006161 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006162 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006163 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006164 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006165
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006166 appctx->ctx.sess.target = NULL;
6167 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006168 return 1;
6169 }
6170
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006171 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006172 /* fall through */
6173
6174 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006175 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006176 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006177 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006178}
6179
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006180/* This is called when the stream interface is closed. For instance, upon an
6181 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006182 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006183 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006184static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006185{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006186 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6187 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6188 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006189 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006190 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6191 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006192 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006193 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006194 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6195 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006196 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006197 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006198}
6199
Willy Tarreau20e99322013-04-13 09:22:25 +02006200/* This function is used to either dump tables states (when action is set
6201 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006202 * It returns 0 if the output buffer is full and it needs to be called
6203 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006204 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006205static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006206{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006207 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006208 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006209 struct ebmb_node *eb;
6210 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006211 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006212 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006213
6214 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006215 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006216 * - STAT_ST_INIT : the first call
6217 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006218 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006219 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006220 * and the entry pointer points to the next entry to be dumped,
6221 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006222 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006223 * data though.
6224 */
6225
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006226 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006227 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006228 if (appctx->st2 == STAT_ST_LIST) {
6229 appctx->ctx.table.entry->ref_cnt--;
6230 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006231 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006232 return 1;
6233 }
6234
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006235 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006236
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006237 while (appctx->st2 != STAT_ST_FIN) {
6238 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006239 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006240 appctx->ctx.table.proxy = appctx->ctx.table.target;
6241 if (!appctx->ctx.table.proxy)
6242 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006243
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006244 appctx->ctx.table.entry = NULL;
6245 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006246 break;
6247
Willy Tarreau295a8372011-03-10 11:25:07 +01006248 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006249 if (!appctx->ctx.table.proxy ||
6250 (appctx->ctx.table.target &&
6251 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6252 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006253 break;
6254 }
6255
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006256 if (appctx->ctx.table.proxy->table.size) {
6257 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6258 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006259 return 0;
6260
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006261 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006262 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006263 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006264 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006265 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006266 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6267 appctx->ctx.table.entry->ref_cnt++;
6268 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006269 break;
6270 }
6271 }
6272 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006273 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006274 break;
6275
Willy Tarreau295a8372011-03-10 11:25:07 +01006276 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006277 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006278
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006279 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006280 /* we're filtering on some data contents */
6281 void *ptr;
6282 long long data;
6283
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006284 dt = appctx->ctx.table.data_type;
6285 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6286 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006287 dt);
6288
6289 data = 0;
6290 switch (stktable_data_types[dt].std_type) {
6291 case STD_T_SINT:
6292 data = stktable_data_cast(ptr, std_t_sint);
6293 break;
6294 case STD_T_UINT:
6295 data = stktable_data_cast(ptr, std_t_uint);
6296 break;
6297 case STD_T_ULL:
6298 data = stktable_data_cast(ptr, std_t_ull);
6299 break;
6300 case STD_T_FRQP:
6301 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006302 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006303 break;
6304 }
6305
6306 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006307 if ((data < appctx->ctx.table.value &&
6308 (appctx->ctx.table.data_op == STD_OP_EQ ||
6309 appctx->ctx.table.data_op == STD_OP_GT ||
6310 appctx->ctx.table.data_op == STD_OP_GE)) ||
6311 (data == appctx->ctx.table.value &&
6312 (appctx->ctx.table.data_op == STD_OP_NE ||
6313 appctx->ctx.table.data_op == STD_OP_GT ||
6314 appctx->ctx.table.data_op == STD_OP_LT)) ||
6315 (data > appctx->ctx.table.value &&
6316 (appctx->ctx.table.data_op == STD_OP_EQ ||
6317 appctx->ctx.table.data_op == STD_OP_LT ||
6318 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006319 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006320 }
6321
Simon Hormanc88b8872011-06-15 15:18:49 +09006322 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006323 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6324 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006325 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006326
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006327 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006328
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006329 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006330 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006331 struct stksess *old = appctx->ctx.table.entry;
6332 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006333 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006334 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6335 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6336 stksess_kill(&appctx->ctx.table.proxy->table, old);
6337 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006338 break;
6339 }
6340
Simon Hormanc88b8872011-06-15 15:18:49 +09006341
6342 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006343 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6344 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6345 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006346
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006347 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6348 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006349 break;
6350
Willy Tarreau295a8372011-03-10 11:25:07 +01006351 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006352 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006353 break;
6354 }
6355 }
6356 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006357}
6358
Willy Tarreaud426a182010-03-05 14:58:26 +01006359/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006360 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6361 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6362 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6363 * lines are respected within the limit of 70 output chars. Lines that are
6364 * continuation of a previous truncated line begin with "+" instead of " "
6365 * after the offset. The new pointer is returned.
6366 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006367static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6368 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006369{
6370 int end;
6371 unsigned char c;
6372
6373 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006374 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006375 return ptr;
6376
Willy Tarreau77804732012-10-29 16:14:26 +01006377 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006378
Willy Tarreaud426a182010-03-05 14:58:26 +01006379 while (ptr < len && ptr < bsize) {
6380 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006381 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006382 if (out->len > end - 2)
6383 break;
6384 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006385 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006386 if (out->len > end - 3)
6387 break;
6388 out->str[out->len++] = '\\';
6389 switch (c) {
6390 case '\t': c = 't'; break;
6391 case '\n': c = 'n'; break;
6392 case '\r': c = 'r'; break;
6393 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006394 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006395 }
6396 out->str[out->len++] = c;
6397 } else {
6398 if (out->len > end - 5)
6399 break;
6400 out->str[out->len++] = '\\';
6401 out->str[out->len++] = 'x';
6402 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6403 out->str[out->len++] = hextab[c & 0xF];
6404 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006405 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006406 /* we had a line break, let's return now */
6407 out->str[out->len++] = '\n';
6408 *line = ptr;
6409 return ptr;
6410 }
6411 }
6412 /* we have an incomplete line, we return it as-is */
6413 out->str[out->len++] = '\n';
6414 return ptr;
6415}
6416
Baptiste Assmann3863f972015-05-17 00:33:24 +02006417/* This function dumps counters from all resolvers section and associated name servers.
6418 * It returns 0 if the output buffer is full and it needs
6419 * to be called again, otherwise non-zero.
6420 */
6421static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6422{
6423 struct appctx *appctx = __objt_appctx(si->end);
6424 struct dns_resolvers *presolvers;
6425 struct dns_nameserver *pnameserver;
6426
6427 chunk_reset(&trash);
6428
6429 switch (appctx->st2) {
6430 case STAT_ST_INIT:
6431 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6432 /* fall through */
6433
6434 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006435 if (LIST_ISEMPTY(&dns_resolvers)) {
6436 chunk_appendf(&trash, "No resolvers found\n");
6437 }
6438 else {
6439 list_for_each_entry(presolvers, &dns_resolvers, list) {
6440 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6441 continue;
6442
6443 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6444 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6445 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6446 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6447 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6448 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6449 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6450 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6451 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6452 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6453 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6454 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6455 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6456 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6457 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6458 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6459 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6460 }
6461 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006462 }
6463
6464 /* display response */
6465 if (bi_putchk(si_ic(si), &trash) == -1) {
6466 /* let's try again later from this session. We add ourselves into
6467 * this session's users so that it can remove us upon termination.
6468 */
6469 si->flags |= SI_FL_WAIT_ROOM;
6470 return 0;
6471 }
6472
6473 appctx->st2 = STAT_ST_FIN;
6474 /* fall through */
6475
6476 default:
6477 appctx->st2 = STAT_ST_FIN;
6478 return 1;
6479 }
6480}
6481
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006482/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006483 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006484 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006485 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006486static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006487{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006488 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006489 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006490
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006491 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006492 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006493
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006494 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006495
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006496 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006497 /* the function had not been called yet, let's prepare the
6498 * buffer for a response.
6499 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006500 struct tm tm;
6501
6502 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006503 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006504 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6505 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6506 error_snapshot_id);
6507
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006508 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006509 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006510 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006511 return 0;
6512 }
6513
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006514 appctx->ctx.errors.px = proxy;
6515 appctx->ctx.errors.buf = 0;
6516 appctx->ctx.errors.bol = 0;
6517 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006518 }
6519
6520 /* we have two inner loops here, one for the proxy, the other one for
6521 * the buffer.
6522 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006523 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006524 struct error_snapshot *es;
6525
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006526 if (appctx->ctx.errors.buf == 0)
6527 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006528 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006529 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006530
6531 if (!es->when.tv_sec)
6532 goto next;
6533
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006534 if (appctx->ctx.errors.iid >= 0 &&
6535 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6536 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006537 goto next;
6538
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006539 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006540 /* just print headers now */
6541
6542 char pn[INET6_ADDRSTRLEN];
6543 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006544 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006545
6546 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006547 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006548 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006549 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006550
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006551 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6552 case AF_INET:
6553 case AF_INET6:
6554 port = get_host_port(&es->src);
6555 break;
6556 default:
6557 port = 0;
6558 }
6559
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006560 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006561 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006562 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006563 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006564 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006565 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006566 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6567 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006568 break;
6569 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006570 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006571 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006572 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006573 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006574 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006575 break;
6576 }
6577
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006578 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006579 ", server %s (#%d), event #%u\n"
6580 " src %s:%d, session #%d, session flags 0x%08x\n"
6581 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6582 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6583 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6584 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6585 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6586 es->ev_id,
6587 pn, port, es->sid, es->s_flags,
6588 es->state, es->m_flags, es->t_flags,
6589 es->m_clen, es->m_blen,
6590 es->b_flags, es->b_out, es->b_tot,
6591 es->len, es->b_wrap, es->pos);
6592
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006593 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006594 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006595 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006596 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006597 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006598 appctx->ctx.errors.ptr = 0;
6599 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006600 }
6601
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006602 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006603 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006604 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006605 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006606 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006607 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006608 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006609 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006610 goto next;
6611 }
6612
6613 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006614 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006615 int newptr;
6616 int newline;
6617
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006618 newline = appctx->ctx.errors.bol;
6619 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6620 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006621 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006622
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006623 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006624 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006625 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006626 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006627 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006628 appctx->ctx.errors.ptr = newptr;
6629 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006630 };
6631 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006632 appctx->ctx.errors.bol = 0;
6633 appctx->ctx.errors.ptr = -1;
6634 appctx->ctx.errors.buf++;
6635 if (appctx->ctx.errors.buf > 1) {
6636 appctx->ctx.errors.buf = 0;
6637 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006638 }
6639 }
6640
6641 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006642 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006643}
6644
Willy Tarreaud5781202012-09-22 19:32:35 +02006645/* parse the "level" argument on the bind lines */
6646static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6647{
6648 if (!*args[cur_arg + 1]) {
6649 memprintf(err, "'%s' : missing level", args[cur_arg]);
6650 return ERR_ALERT | ERR_FATAL;
6651 }
6652
6653 if (!strcmp(args[cur_arg+1], "user"))
6654 conf->level = ACCESS_LVL_USER;
6655 else if (!strcmp(args[cur_arg+1], "operator"))
6656 conf->level = ACCESS_LVL_OPER;
6657 else if (!strcmp(args[cur_arg+1], "admin"))
6658 conf->level = ACCESS_LVL_ADMIN;
6659 else {
6660 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6661 args[cur_arg], args[cur_arg+1]);
6662 return ERR_ALERT | ERR_FATAL;
6663 }
6664
6665 return 0;
6666}
6667
Willy Tarreau30576452015-04-13 13:50:30 +02006668struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006669 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006670 .name = "<STATS>", /* used for logging */
6671 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006672 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006673};
6674
Willy Tarreau30576452015-04-13 13:50:30 +02006675static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006676 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006677 .name = "<CLI>", /* used for logging */
6678 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006679 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006680};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006681
Willy Tarreaudc13c112013-06-21 23:16:39 +02006682static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006683 { CFG_GLOBAL, "stats", stats_parse_global },
6684 { 0, NULL, NULL },
6685}};
6686
Willy Tarreaud5781202012-09-22 19:32:35 +02006687static struct bind_kw_list bind_kws = { "STAT", { }, {
6688 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6689 { NULL, NULL, 0 },
6690}};
6691
Willy Tarreau10522fd2008-07-09 20:12:41 +02006692__attribute__((constructor))
6693static void __dumpstats_module_init(void)
6694{
6695 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006696 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006697}
6698
Willy Tarreau91861262007-10-17 17:06:05 +02006699/*
6700 * Local variables:
6701 * c-indent-level: 8
6702 * c-basic-offset: 8
6703 * End:
6704 */