blob: fd35fe319c20fda7fff8392df79e988a679cc158 [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 Tarreauae795722016-02-16 11:27:28 +010097 STAT_CLI_O_ENV, /* dump environment */
Willy Tarreau91b843d2014-01-28 16:27:17 +010098};
99
Willy Tarreaued7df902014-05-22 18:04:49 +0200100/* Actions available for the stats admin forms */
101enum {
102 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +0200103
104 /* enable/disable health checks */
105 ST_ADM_ACTION_DHLTH,
106 ST_ADM_ACTION_EHLTH,
107
108 /* force health check status */
109 ST_ADM_ACTION_HRUNN,
110 ST_ADM_ACTION_HNOLB,
111 ST_ADM_ACTION_HDOWN,
112
113 /* enable/disable agent checks */
114 ST_ADM_ACTION_DAGENT,
115 ST_ADM_ACTION_EAGENT,
116
117 /* force agent check status */
118 ST_ADM_ACTION_ARUNN,
119 ST_ADM_ACTION_ADOWN,
120
121 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200122 ST_ADM_ACTION_READY,
123 ST_ADM_ACTION_DRAIN,
124 ST_ADM_ACTION_MAINT,
125 ST_ADM_ACTION_SHUTDOWN,
126 /* these are the ancient actions, still available for compatibility */
127 ST_ADM_ACTION_DISABLE,
128 ST_ADM_ACTION_ENABLE,
129 ST_ADM_ACTION_STOP,
130 ST_ADM_ACTION_START,
131};
132
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200133static int stats_dump_backend_to_buffer(struct stream_interface *si);
Willy Tarreauae795722016-02-16 11:27:28 +0100134static int stats_dump_env_to_buffer(struct stream_interface *si);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100135static int stats_dump_info_to_buffer(struct stream_interface *si);
Baptiste Assmann28289462015-07-03 08:01:20 +0200136static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100137static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200138static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900139static int stats_dump_sess_to_buffer(struct stream_interface *si);
140static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100141static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100142static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
143static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200144static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100145static int stats_pats_list(struct stream_interface *si);
146static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100147static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200148#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
149static int stats_tlskeys_list(struct stream_interface *si);
150#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200151static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900152
Baptiste Assmann28289462015-07-03 08:01:20 +0200153static void dump_servers_state(struct proxy *backend, struct chunk *buf);
154
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100155/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100156 * cli_io_handler()
157 * -> stats_dump_sess_to_buffer() // "show sess"
158 * -> stats_dump_errors_to_buffer() // "show errors"
159 * -> stats_dump_info_to_buffer() // "show info"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200160 * -> stats_dump_backend_to_buffer() // "show backend"
Baptiste Assmann28289462015-07-03 08:01:20 +0200161 * -> stats_dump_servers_state_to_buffer() // "show servers state [<backend name>]"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100162 * -> stats_dump_stat_to_buffer() // "show stat"
Baptiste Assmann3863f972015-05-17 00:33:24 +0200163 * -> stats_dump_resolvers_to_buffer() // "show stat resolver <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100164 * -> stats_dump_csv_header()
165 * -> stats_dump_proxy_to_buffer()
166 * -> stats_dump_fe_stats()
167 * -> stats_dump_li_stats()
168 * -> stats_dump_sv_stats()
169 * -> stats_dump_be_stats()
170 *
171 * http_stats_io_handler()
172 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
173 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
174 * -> stats_dump_html_head() // emits the HTML headers
175 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
176 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
177 * -> stats_dump_html_px_hdr()
178 * -> stats_dump_fe_stats()
179 * -> stats_dump_li_stats()
180 * -> stats_dump_sv_stats()
181 * -> stats_dump_be_stats()
182 * -> stats_dump_html_px_end()
183 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100184 */
185
Willy Tarreau30576452015-04-13 13:50:30 +0200186static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900187
188static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200189 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200190 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200191 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200192 " help : this message\n"
193 " prompt : toggle interactive mode with prompt\n"
194 " quit : disconnect\n"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200195 " show backend : list backends in the current running config\n"
Willy Tarreauae795722016-02-16 11:27:28 +0100196 " show env [var] : dump environment variables known to the process\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200197 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100198 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200199 " show stat : report counters for each proxy and server\n"
200 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100201 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200202 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200203 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200204 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200205 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200206 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200207 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100208 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200209 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200210 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200211 " disable : put a server or frontend in maintenance mode\n"
212 " enable : re-enable a server or frontend which is in maintenance mode\n"
213 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100214 " show acl [id] : report avalaible acls or dump an acl's contents\n"
215 " get acl : reports the patterns matching a sample for an ACL\n"
216 " add acl : add acl entry\n"
217 " del acl : delete acl entry\n"
218 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100219 " show map [id] : report avalaible maps or dump a map's contents\n"
220 " get map : reports the keys and values matching a sample for a map\n"
221 " set map : modify map entry\n"
222 " add map : add map entry\n"
223 " del map : delete map entry\n"
224 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200225 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200226 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200227
Simon Horman9bd2c732011-06-15 15:18:44 +0900228static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200229 "Permission denied\n"
230 "";
231
Willy Tarreau295a8372011-03-10 11:25:07 +0100232/* data transmission states for the stats responses */
233enum {
234 STAT_ST_INIT = 0,
235 STAT_ST_HEAD,
236 STAT_ST_INFO,
237 STAT_ST_LIST,
238 STAT_ST_END,
239 STAT_ST_FIN,
240};
241
242/* data transmission states for the stats responses inside a proxy */
243enum {
244 STAT_PX_ST_INIT = 0,
245 STAT_PX_ST_TH,
246 STAT_PX_ST_FE,
247 STAT_PX_ST_LI,
248 STAT_PX_ST_SV,
249 STAT_PX_ST_BE,
250 STAT_PX_ST_END,
251 STAT_PX_ST_FIN,
252};
253
Cyril Bonté19979e12012-04-04 12:57:21 +0200254extern const char *stat_status_codes[];
255
Willy Tarreau07e9e642010-08-17 21:48:17 +0200256/* allocate a new stats frontend named <name>, and return it
257 * (or NULL in case of lack of memory).
258 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200259static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200260{
261 struct proxy *fe;
262
263 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
264 if (!fe)
265 return NULL;
266
Willy Tarreau237250c2011-07-29 01:49:03 +0200267 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200268 fe->next = proxy;
269 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200270 fe->last_change = now.tv_sec;
271 fe->id = strdup("GLOBAL");
272 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200273 fe->maxconn = 10; /* default to 10 concurrent connections */
274 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200275 fe->conf.file = strdup(file);
276 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100277 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100278 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200279
280 /* the stats frontend is the only one able to assign ID #0 */
281 fe->conf.id.key = fe->uuid = 0;
282 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200283 return fe;
284}
285
Willy Tarreaufbee7132007-10-18 13:53:22 +0200286/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
288 * error message into the <err> buffer which will be preallocated. The trailing
289 * '\n' must not be written. The function must be called with <args> pointing to
290 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200291 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200292static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200293 struct proxy *defpx, const char *file, int line,
294 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200295{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200296 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200297 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200298
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200299 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200300 int cur_arg;
301
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200302 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200303 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 +0200304 return -1;
305 }
306
Willy Tarreau89a63132009-08-16 17:41:45 +0200307 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200308 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200309 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200310 return -1;
311 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200312 }
313
Willy Tarreau4348fad2012-09-20 16:48:07 +0200314 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200315 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200316
Willy Tarreauc53d4222012-09-20 20:19:28 +0200317 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
318 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
319 file, line, args[0], args[1], err && *err ? *err : "error");
320 return -1;
321 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200322
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200323 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200324 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200325 static int bind_dumped;
326 struct bind_kw *kw;
327
328 kw = bind_find_kw(args[cur_arg]);
329 if (kw) {
330 if (!kw->parse) {
331 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
332 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200333 return -1;
334 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200335
Willy Tarreau061b5de2015-10-13 15:06:57 +0200336 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200337 if (err && *err)
338 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
339 else
340 memprintf(err, "'%s %s' : error encountered while processing '%s'",
341 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200342 return -1;
343 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200344
345 cur_arg += 1 + kw->skip;
346 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200347 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200348
349 if (!bind_dumped) {
350 bind_dump_kws(err);
351 indent_msg(err, 4);
352 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200353 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200354
355 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
356 args[0], args[1], args[cur_arg],
357 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
358 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200359 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100360
Willy Tarreauc53d4222012-09-20 20:19:28 +0200361 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
362 l->maxconn = global.stats_fe->maxconn;
363 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200364 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200365 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100366 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200367 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
368 l->nice = -64; /* we want to boost priority for local stats */
369 global.maxsock += l->maxconn;
370 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200371 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200372 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100373 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200374 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100375
376 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200377 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100378 return -1;
379 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200380
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100381 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200382 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200383 return -1;
384 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200385 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200386 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200387 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200388 return -1;
389 }
390 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200391 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200392 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200393 else if (!strcmp(args[1], "maxconn")) {
394 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200395
396 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200397 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200398 return -1;
399 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200400
401 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200402 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200403 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200404 return -1;
405 }
406 }
407 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200408 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200409 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
410 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100411 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200412
Willy Tarreau91319572013-04-20 09:48:50 +0200413 if (!global.stats_fe) {
414 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
415 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
416 return -1;
417 }
418 }
419
Willy Tarreau35b7b162012-10-22 23:17:18 +0200420 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100421 unsigned int low, high;
422
Willy Tarreau35b7b162012-10-22 23:17:18 +0200423 if (strcmp(args[cur_arg], "all") == 0) {
424 set = 0;
425 break;
426 }
427 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100428 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200429 }
430 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100431 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200432 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100433 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100434 char *dash = strchr(args[cur_arg], '-');
435
436 low = high = str2uic(args[cur_arg]);
437 if (dash)
438 high = str2uic(dash + 1);
439
440 if (high < low) {
441 unsigned int swap = low;
442 low = high;
443 high = swap;
444 }
445
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100446 if (low < 1 || high > LONGBITS) {
447 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
448 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200449 return -1;
450 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100451 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100452 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100453 }
454 else {
455 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100456 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
457 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100458 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200459 }
460 cur_arg++;
461 }
462 global.stats_fe->bind_proc = set;
463 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200464 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200465 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200466 return -1;
467 }
468 return 0;
469}
470
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100471/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
472 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100473 * NOTE: Some tools happen to rely on the field position instead of its name,
474 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100475 */
476static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100477{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100478 chunk_appendf(&trash,
479 "# pxname,svname,"
480 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100481 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100482 "bin,bout,"
483 "dreq,dresp,"
484 "ereq,econ,eresp,"
485 "wretr,wredis,"
486 "status,weight,act,bck,"
487 "chkfail,chkdown,lastchg,downtime,qlimit,"
488 "pid,iid,sid,throttle,lbtot,tracked,type,"
489 "rate,rate_lim,rate_max,"
490 "check_status,check_code,check_duration,"
491 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
492 "req_rate,req_rate_max,req_tot,"
493 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200494 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100495 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100496}
497
Simon Hormand9366582011-06-15 15:18:45 +0900498/* print a string of text buffer to <out>. The format is :
499 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
500 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
501 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
502 */
503static int dump_text(struct chunk *out, const char *buf, int bsize)
504{
505 unsigned char c;
506 int ptr = 0;
507
508 while (buf[ptr] && ptr < bsize) {
509 c = buf[ptr];
510 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
511 if (out->len > out->size - 1)
512 break;
513 out->str[out->len++] = c;
514 }
515 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
516 if (out->len > out->size - 2)
517 break;
518 out->str[out->len++] = '\\';
519 switch (c) {
520 case ' ': c = ' '; break;
521 case '\t': c = 't'; break;
522 case '\n': c = 'n'; break;
523 case '\r': c = 'r'; break;
524 case '\e': c = 'e'; break;
525 case '\\': c = '\\'; break;
526 }
527 out->str[out->len++] = c;
528 }
529 else {
530 if (out->len > out->size - 4)
531 break;
532 out->str[out->len++] = '\\';
533 out->str[out->len++] = 'x';
534 out->str[out->len++] = hextab[(c >> 4) & 0xF];
535 out->str[out->len++] = hextab[c & 0xF];
536 }
537 ptr++;
538 }
539
540 return ptr;
541}
542
543/* print a buffer in hexa.
544 * Print stopped if <bsize> is reached, or if no more place in the chunk.
545 */
546static int dump_binary(struct chunk *out, const char *buf, int bsize)
547{
548 unsigned char c;
549 int ptr = 0;
550
551 while (ptr < bsize) {
552 c = buf[ptr];
553
554 if (out->len > out->size - 2)
555 break;
556 out->str[out->len++] = hextab[(c >> 4) & 0xF];
557 out->str[out->len++] = hextab[c & 0xF];
558
559 ptr++;
560 }
561 return ptr;
562}
563
564/* Dump the status of a table to a stream interface's
565 * read buffer. It returns 0 if the output buffer is full
566 * and needs to be called again, otherwise non-zero.
567 */
568static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
569 struct proxy *proxy, struct proxy *target)
570{
Willy Tarreau87b09662015-04-03 00:22:06 +0200571 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900572
Willy Tarreau77804732012-10-29 16:14:26 +0100573 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900574 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
575
576 /* any other information should be dumped here */
577
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200578 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100579 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900580
Willy Tarreaubc18da12015-03-13 14:00:47 +0100581 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200582 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900583 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100584 }
Simon Hormand9366582011-06-15 15:18:45 +0900585
586 return 1;
587}
588
589/* Dump the a table entry to a stream interface's
590 * read buffer. It returns 0 if the output buffer is full
591 * and needs to be called again, otherwise non-zero.
592 */
593static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
594 struct proxy *proxy, struct stksess *entry)
595{
596 int dt;
597
Willy Tarreau77804732012-10-29 16:14:26 +0100598 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900599
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200600 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900601 char addr[INET_ADDRSTRLEN];
602 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100603 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900604 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200605 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900606 char addr[INET6_ADDRSTRLEN];
607 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100608 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900609 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200610 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100611 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900612 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200613 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100614 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900615 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
616 }
617 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100618 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900619 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
620 }
621
Willy Tarreau77804732012-10-29 16:14:26 +0100622 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900623
624 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
625 void *ptr;
626
627 if (proxy->table.data_ofs[dt] == 0)
628 continue;
629 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100630 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900631 else
Willy Tarreau77804732012-10-29 16:14:26 +0100632 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900633
634 ptr = stktable_data_ptr(&proxy->table, entry, dt);
635 switch (stktable_data_types[dt].std_type) {
636 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100637 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900638 break;
639 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100640 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900641 break;
642 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100643 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900644 break;
645 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100646 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900647 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
648 proxy->table.data_arg[dt].u));
649 break;
650 }
651 }
Willy Tarreau77804732012-10-29 16:14:26 +0100652 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900653
Willy Tarreaubc18da12015-03-13 14:00:47 +0100654 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200655 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900656 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100657 }
Simon Hormand9366582011-06-15 15:18:45 +0900658
659 return 1;
660}
661
Willy Tarreaudec98142012-06-06 23:37:08 +0200662static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900663{
Willy Tarreau87b09662015-04-03 00:22:06 +0200664 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100665 struct appctx *appctx = __objt_appctx(si->end);
666 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900667 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900668 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900669 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200670 long long value;
671 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200672 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200673 void *ptr;
674 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900675
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100676 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900677
678 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100679 appctx->ctx.cli.msg = "Key value expected\n";
680 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900681 return;
682 }
683
Simon Hormanc5b89f62011-06-15 15:18:50 +0900684 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200685 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900686 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100687 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900688 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200689 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900690 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100691 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900692 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200693 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900694 {
695 char *endptr;
696 unsigned long val;
697 errno = 0;
698 val = strtoul(args[4], &endptr, 10);
699 if ((errno == ERANGE && val == ULONG_MAX) ||
700 (errno != 0 && val == 0) || endptr == args[4] ||
701 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100702 appctx->ctx.cli.msg = "Invalid key\n";
703 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900704 return;
705 }
706 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100707 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900708 break;
709 }
710 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200711 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +0100712 static_table_key->key = args[4];
713 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900714 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900715 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200716 switch (action) {
717 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100718 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 +0200719 break;
720 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100721 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 +0200722 break;
723 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100724 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200725 break;
726 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100727 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900728 return;
729 }
730
731 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200732 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100733 appctx->ctx.cli.msg = stats_permission_denied_msg;
734 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900735 return;
736 }
737
Willy Tarreau07115412012-10-29 21:56:59 +0100738 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900739
Willy Tarreaudec98142012-06-06 23:37:08 +0200740 switch (action) {
741 case STAT_CLI_O_TAB:
742 if (!ts)
743 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100744 chunk_reset(&trash);
745 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900746 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100747 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900748 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200749
750 case STAT_CLI_O_CLR:
751 if (!ts)
752 return;
753 if (ts->ref_cnt) {
754 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100755 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
756 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200757 return;
758 }
759 stksess_kill(&px->table, ts);
760 break;
Simon Horman17bce342011-06-15 15:18:47 +0900761
Willy Tarreau654694e2012-06-07 01:03:16 +0200762 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200763 if (ts)
764 stktable_touch(&px->table, ts, 1);
765 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100766 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200767 if (!ts) {
768 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100769 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
770 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200771 return;
772 }
773 stktable_store(&px->table, ts, 1);
774 }
775
Willy Tarreau47060b62013-08-01 21:11:42 +0200776 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
777 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100778 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
779 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200780 return;
781 }
782
783 data_type = stktable_get_data_type(args[cur_arg] + 5);
784 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100785 appctx->ctx.cli.msg = "Unknown data type\n";
786 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200787 return;
788 }
789
790 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100791 appctx->ctx.cli.msg = "Data type not stored in this table\n";
792 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200793 return;
794 }
795
796 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100797 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
798 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200799 return;
800 }
801
802 ptr = stktable_data_ptr(&px->table, ts, data_type);
803
804 switch (stktable_data_types[data_type].std_type) {
805 case STD_T_SINT:
806 stktable_data_cast(ptr, std_t_sint) = value;
807 break;
808 case STD_T_UINT:
809 stktable_data_cast(ptr, std_t_uint) = value;
810 break;
811 case STD_T_ULL:
812 stktable_data_cast(ptr, std_t_ull) = value;
813 break;
814 case STD_T_FRQP:
815 /* We set both the current and previous values. That way
816 * the reported frequency is stable during all the period
817 * then slowly fades out. This allows external tools to
818 * push measures without having to update them too often.
819 */
820 frqp = &stktable_data_cast(ptr, std_t_frqp);
821 frqp->curr_tick = now_ms;
822 frqp->prev_ctr = 0;
823 frqp->curr_ctr = value;
824 break;
825 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200826 }
827 break;
828
Willy Tarreaudec98142012-06-06 23:37:08 +0200829 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100830 appctx->ctx.cli.msg = "Unknown action\n";
831 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200832 break;
Simon Horman121f3052011-06-15 15:18:46 +0900833 }
Simon Horman121f3052011-06-15 15:18:46 +0900834}
835
Willy Tarreau654694e2012-06-07 01:03:16 +0200836static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900837{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100838 struct appctx *appctx = __objt_appctx(si->end);
839
Willy Tarreau04b3a192013-04-13 09:41:37 +0200840 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100841 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
842 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200843 return;
844 }
845
Simon Hormand5b9fd92011-06-15 15:18:48 +0900846 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100847 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
848 if (appctx->ctx.table.data_type < 0) {
849 appctx->ctx.cli.msg = "Unknown data type\n";
850 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900851 return;
852 }
853
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100854 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
855 appctx->ctx.cli.msg = "Data type not stored in this table\n";
856 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900857 return;
858 }
859
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100860 appctx->ctx.table.data_op = get_std_op(args[4]);
861 if (appctx->ctx.table.data_op < 0) {
862 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
863 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900864 return;
865 }
866
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100867 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
868 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
869 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900870 return;
871 }
872}
873
Willy Tarreaudec98142012-06-06 23:37:08 +0200874static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900875{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100876 struct appctx *appctx = __objt_appctx(si->end);
877
878 appctx->ctx.table.data_type = -1;
879 appctx->st2 = STAT_ST_INIT;
880 appctx->ctx.table.target = NULL;
881 appctx->ctx.table.proxy = NULL;
882 appctx->ctx.table.entry = NULL;
883 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900884
885 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +0200886 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100887 if (!appctx->ctx.table.target) {
888 appctx->ctx.cli.msg = "No such table\n";
889 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900890 return;
891 }
892 }
893 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200894 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900895 goto err_args;
896 return;
897 }
898
899 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200900 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900901 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200902 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900903 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900904 goto err_args;
905
906 return;
907
908err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200909 switch (action) {
910 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100911 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 +0200912 break;
913 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100914 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 +0200915 break;
916 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100917 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200918 break;
919 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100920 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900921}
922
Willy Tarreau532a4502011-09-07 22:37:44 +0200923/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +0200924 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +0200925 * level to be admin.
926 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200927static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +0200928{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100929 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200930 struct proxy *px;
931
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200932 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100933 appctx->ctx.cli.msg = stats_permission_denied_msg;
934 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200935 return NULL;
936 }
937
938 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100939 appctx->ctx.cli.msg = "A frontend name is expected.\n";
940 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200941 return NULL;
942 }
943
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200944 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +0200945 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100946 appctx->ctx.cli.msg = "No such frontend.\n";
947 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200948 return NULL;
949 }
950 return px;
951}
952
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200953/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
954 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +0200955 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200956 * the <arg> is modified to remove the '/'.
957 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200958static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200959{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100960 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200961 struct proxy *px;
962 struct server *sv;
963 char *line;
964
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200965 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100966 appctx->ctx.cli.msg = stats_permission_denied_msg;
967 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200968 return NULL;
969 }
970
971 /* split "backend/server" and make <line> point to server */
972 for (line = arg; *line; line++)
973 if (*line == '/') {
974 *line++ = '\0';
975 break;
976 }
977
978 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100979 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
980 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200981 return NULL;
982 }
983
984 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100985 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
986 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200987 return NULL;
988 }
989
990 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100991 appctx->ctx.cli.msg = "Proxy is disabled.\n";
992 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200993 return NULL;
994 }
995
996 return sv;
997}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200998
999/* This function is used with TLS ticket keys management. It permits to browse
1000 * each reference. The variable <getnext> must contain the current node,
1001 * <end> point to the root node.
1002 */
1003#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1004static inline
1005struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1006{
1007 struct tls_keys_ref *ref = getnext;
1008
1009 while (1) {
1010
1011 /* Get next list entry. */
1012 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1013
1014 /* If the entry is the last of the list, return NULL. */
1015 if (&ref->list == end)
1016 return NULL;
1017
1018 return ref;
1019 }
1020}
1021
1022static inline
1023struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1024{
1025 int id;
1026 char *error;
1027
1028 /* If the reference starts by a '#', this is numeric id. */
1029 if (reference[0] == '#') {
1030 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1031 id = strtol(reference + 1, &error, 10);
1032 if (*error != '\0')
1033 return NULL;
1034
1035 /* Perform the unique id lookup. */
1036 return tlskeys_ref_lookupid(id);
1037 }
1038
1039 /* Perform the string lookup. */
1040 return tlskeys_ref_lookup(reference);
1041}
1042#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001043
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001044/* This function is used with map and acl management. It permits to browse
1045 * each reference. The variable <getnext> must contain the current node,
1046 * <end> point to the root node and the <flags> permit to filter required
1047 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001048 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001049static inline
1050struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1051 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001052{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001053 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001054
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001055 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001056
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001057 /* Get next list entry. */
1058 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001059
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001060 /* If the entry is the last of the list, return NULL. */
1061 if (&ref->list == end)
1062 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001063
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001064 /* If the entry match the flag, return it. */
1065 if (ref->flags & flags)
1066 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001067 }
1068}
1069
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001070static inline
1071struct pat_ref *pat_ref_lookup_ref(const char *reference)
1072{
1073 int id;
1074 char *error;
1075
1076 /* If the reference starts by a '#', this is numeric id. */
1077 if (reference[0] == '#') {
1078 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1079 id = strtol(reference + 1, &error, 10);
1080 if (*error != '\0')
1081 return NULL;
1082
1083 /* Perform the unique id lookup. */
1084 return pat_ref_lookupid(id);
1085 }
1086
1087 /* Perform the string lookup. */
1088 return pat_ref_lookup(reference);
1089}
1090
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001091/* This function is used with map and acl management. It permits to browse
1092 * each reference.
1093 */
1094static inline
1095struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1096{
1097 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001098 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1099 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001100 return NULL;
1101 return expr;
1102}
1103
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001104/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001105 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001106 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001107 * designating the function which will have to process the request, which can
1108 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001109 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001110static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001111{
Willy Tarreau87b09662015-04-03 00:22:06 +02001112 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001113 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001114 char *args[MAX_STATS_ARGS + 1];
1115 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001116 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001117
1118 while (isspace((unsigned char)*line))
1119 line++;
1120
1121 arg = 0;
1122 args[arg] = line;
1123
1124 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001125 if (*line == '\\') {
1126 line++;
1127 if (*line == '\0')
1128 break;
1129 }
1130 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001131 *line++ = '\0';
1132
1133 while (isspace((unsigned char)*line))
1134 line++;
1135
1136 args[++arg] = line;
1137 continue;
1138 }
1139
1140 line++;
1141 }
1142
1143 while (++arg <= MAX_STATS_ARGS)
1144 args[arg] = line;
1145
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001146 /* remove \ */
1147 arg = 0;
1148 while (*args[arg] != '\0') {
1149 j = 0;
1150 for (i=0; args[arg][i] != '\0'; i++) {
1151 if (args[arg][i] == '\\')
1152 continue;
1153 args[arg][j] = args[arg][i];
1154 j++;
1155 }
1156 args[arg][j] = '\0';
1157 arg++;
1158 }
1159
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001160 appctx->ctx.stats.scope_str = 0;
1161 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001162 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001163 if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001164 if (strcmp(args[1], "backend") == 0) {
1165 appctx->st2 = STAT_ST_INIT;
1166 appctx->st0 = STAT_CLI_O_BACKEND;
1167 }
Willy Tarreauae795722016-02-16 11:27:28 +01001168 else if (strcmp(args[1], "env") == 0) {
1169 extern char **environ;
1170
1171 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1172 appctx->ctx.cli.msg = stats_permission_denied_msg;
1173 appctx->st0 = STAT_CLI_PRINT;
1174 return 1;
1175 }
1176 appctx->ctx.env.var = environ;
1177 appctx->st2 = STAT_ST_INIT;
1178 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1179
1180 if (*args[2]) {
1181 int len = strlen(args[2]);
1182
1183 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1184 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1185 (*appctx->ctx.env.var)[len] == '=')
1186 break;
1187 }
1188 if (!*appctx->ctx.env.var) {
1189 appctx->ctx.cli.msg = "Variable not found\n";
1190 appctx->st0 = STAT_CLI_PRINT;
1191 return 1;
1192 }
1193 appctx->st2 = STAT_ST_END;
1194 }
1195 }
1196 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001197 if (strcmp(args[2], "resolvers") == 0) {
1198 struct dns_resolvers *presolvers;
1199
Andrew Hayworth68d05342015-10-02 20:33:01 +00001200 if (*args[3]) {
1201 appctx->ctx.resolvers.ptr = NULL;
1202 list_for_each_entry(presolvers, &dns_resolvers, list) {
1203 if (strcmp(presolvers->id, args[3]) == 0) {
1204 appctx->ctx.resolvers.ptr = presolvers;
1205 break;
1206 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001207 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001208 if (appctx->ctx.resolvers.ptr == NULL) {
1209 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1210 appctx->st0 = STAT_CLI_PRINT;
1211 return 1;
1212 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001213 }
1214
1215 appctx->st2 = STAT_ST_INIT;
1216 appctx->st0 = STAT_CLI_O_RESOLVERS;
1217 return 1;
1218 }
1219 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001220 appctx->ctx.stats.flags |= STAT_BOUND;
1221 appctx->ctx.stats.iid = atoi(args[2]);
1222 appctx->ctx.stats.type = atoi(args[3]);
1223 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001224 }
1225
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001226 appctx->st2 = STAT_ST_INIT;
1227 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001228 }
1229 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001230 appctx->st2 = STAT_ST_INIT;
1231 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001232 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001233 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
1234 appctx->ctx.server_state.backend = NULL;
1235
1236 /* check if a backend name has been provided */
1237 if (*args[3]) {
1238 /* read server state from local file */
1239 appctx->ctx.server_state.backend = proxy_be_by_name(args[3]);
1240
1241 if (appctx->ctx.server_state.backend == NULL) {
1242 appctx->ctx.cli.msg = "Can't find backend.\n";
1243 appctx->st0 = STAT_CLI_PRINT;
1244 return 1;
1245 }
1246 }
1247 appctx->st2 = STAT_ST_INIT;
1248 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1249 return 1;
1250 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001251 else if (strcmp(args[1], "pools") == 0) {
1252 appctx->st2 = STAT_ST_INIT;
1253 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1254 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001255 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001256 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001257 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001258 appctx->ctx.cli.msg = stats_permission_denied_msg;
1259 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001260 return 1;
1261 }
Willy Tarreau76153662012-11-26 01:16:39 +01001262 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001263 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001264 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001265 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001266 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001267 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001268 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001269 appctx->ctx.sess.pos = 0;
1270 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001271 }
1272 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001273 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001274 appctx->ctx.cli.msg = stats_permission_denied_msg;
1275 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001276 return 1;
1277 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001278 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001279 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001280 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001281 appctx->ctx.errors.iid = -1;
1282 appctx->ctx.errors.px = NULL;
1283 appctx->st2 = STAT_ST_INIT;
1284 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001285 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001286 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001287 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001288 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001289 else if (strcmp(args[1], "tls-keys") == 0) {
1290#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1291 appctx->st2 = STAT_ST_INIT;
1292 appctx->st0 = STAT_CLI_O_TLSK;
1293#else
1294 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1295 "that doesn't support specifying TLS ticket keys\n";
1296 appctx->st0 = STAT_CLI_PRINT;
1297#endif
1298 return 1;
1299 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001300 else if (strcmp(args[1], "map") == 0 ||
1301 strcmp(args[1], "acl") == 0) {
1302
1303 /* Set ACL or MAP flags. */
1304 if (args[1][0] == 'm')
1305 appctx->ctx.map.display_flags = PAT_REF_MAP;
1306 else
1307 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001308
1309 /* no parameter: display all map avalaible */
1310 if (!*args[2]) {
1311 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001312 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001313 return 1;
1314 }
1315
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001316 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001317 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001318 if (!appctx->ctx.map.ref ||
1319 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1320 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001321 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001322 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001323 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001324 appctx->st0 = STAT_CLI_PRINT;
1325 return 1;
1326 }
1327 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001328 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001329 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001330 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001331 return 0;
1332 }
1333 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001334 else if (strcmp(args[0], "clear") == 0) {
1335 if (strcmp(args[1], "counters") == 0) {
1336 struct proxy *px;
1337 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001338 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001339 int clrall = 0;
1340
1341 if (strcmp(args[2], "all") == 0)
1342 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001343
Willy Tarreau6162db22009-10-10 17:13:00 +02001344 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001345 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1346 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001347 appctx->ctx.cli.msg = stats_permission_denied_msg;
1348 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001349 return 1;
1350 }
1351
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001352 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001353 if (clrall) {
1354 memset(&px->be_counters, 0, sizeof(px->be_counters));
1355 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1356 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001357 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001358 px->be_counters.conn_max = 0;
1359 px->be_counters.p.http.rps_max = 0;
1360 px->be_counters.sps_max = 0;
1361 px->be_counters.cps_max = 0;
1362 px->be_counters.nbpend_max = 0;
1363
1364 px->fe_counters.conn_max = 0;
1365 px->fe_counters.p.http.rps_max = 0;
1366 px->fe_counters.sps_max = 0;
1367 px->fe_counters.cps_max = 0;
1368 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001369 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001370
1371 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001372 if (clrall)
1373 memset(&sv->counters, 0, sizeof(sv->counters));
1374 else {
1375 sv->counters.cur_sess_max = 0;
1376 sv->counters.nbpend_max = 0;
1377 sv->counters.sps_max = 0;
1378 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001379
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001380 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001381 if (li->counters) {
1382 if (clrall)
1383 memset(li->counters, 0, sizeof(*li->counters));
1384 else
1385 li->counters->conn_max = 0;
1386 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001387 }
1388
Willy Tarreau81c25d02011-09-07 15:17:21 +02001389 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001390 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001391 return 1;
1392 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001393 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001394 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001395 /* end of processing */
1396 return 1;
1397 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001398 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1399 /* Set ACL or MAP flags. */
1400 if (args[1][0] == 'm')
1401 appctx->ctx.map.display_flags = PAT_REF_MAP;
1402 else
1403 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001404
1405 /* no parameter */
1406 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001407 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1408 appctx->ctx.cli.msg = "Missing map identifier.\n";
1409 else
1410 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001411 appctx->st0 = STAT_CLI_PRINT;
1412 return 1;
1413 }
1414
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001415 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001416 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001417 if (!appctx->ctx.map.ref ||
1418 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1419 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001420 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001421 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001422 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001423 appctx->st0 = STAT_CLI_PRINT;
1424 return 1;
1425 }
1426
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001427 /* Clear all. */
1428 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001429
1430 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001431 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001432 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001433 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001434 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001435 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001436 return 0;
1437 }
1438 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001439 else if (strcmp(args[0], "get") == 0) {
1440 if (strcmp(args[1], "weight") == 0) {
1441 struct proxy *px;
1442 struct server *sv;
1443
1444 /* split "backend/server" and make <line> point to server */
1445 for (line = args[2]; *line; line++)
1446 if (*line == '/') {
1447 *line++ = '\0';
1448 break;
1449 }
1450
1451 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001452 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1453 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001454 return 1;
1455 }
1456
1457 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001458 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1459 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001460 return 1;
1461 }
1462
1463 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001464 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001465 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001466 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001467
Willy Tarreau38338fa2009-10-10 18:37:29 +02001468 return 1;
1469 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001470 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1471 /* Set flags. */
1472 if (args[1][0] == 'm')
1473 appctx->ctx.map.display_flags = PAT_REF_MAP;
1474 else
1475 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001476
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001477 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001478 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001479 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1480 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1481 else
1482 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001483 appctx->st0 = STAT_CLI_PRINT;
1484 return 1;
1485 }
1486
1487 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001488 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001489 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001490 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001491 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001492 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001493 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001494 appctx->st0 = STAT_CLI_PRINT;
1495 return 1;
1496 }
1497
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001498 /* copy input string. The string must be allocated because
1499 * it may be used over multiple iterations. It's released
1500 * at the end and upon abort anyway.
1501 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001502 appctx->ctx.map.chunk.len = strlen(args[3]);
1503 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1504 appctx->ctx.map.chunk.str = strdup(args[3]);
1505 if (!appctx->ctx.map.chunk.str) {
1506 appctx->ctx.cli.msg = "Out of memory error.\n";
1507 appctx->st0 = STAT_CLI_PRINT;
1508 return 1;
1509 }
1510
1511 /* prepare response */
1512 appctx->st2 = STAT_ST_INIT;
1513 appctx->st0 = STAT_CLI_O_MLOOK;
1514 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001515 else { /* not "get weight" */
1516 return 0;
1517 }
1518 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001519 else if (strcmp(args[0], "set") == 0) {
1520 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001521 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001522 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001523
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001524 sv = expect_server_admin(s, si, args[2]);
1525 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001526 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001527
Simon Horman7d09b9a2013-02-12 10:45:51 +09001528 warning = server_parse_weight_change_request(sv, args[3]);
1529 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001530 appctx->ctx.cli.msg = warning;
1531 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001532 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001533 return 1;
1534 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001535 else if (strcmp(args[1], "server") == 0) {
1536 struct server *sv;
1537 const char *warning;
1538
1539 sv = expect_server_admin(s, si, args[2]);
1540 if (!sv)
1541 return 1;
1542
1543 if (strcmp(args[3], "weight") == 0) {
1544 warning = server_parse_weight_change_request(sv, args[4]);
1545 if (warning) {
1546 appctx->ctx.cli.msg = warning;
1547 appctx->st0 = STAT_CLI_PRINT;
1548 }
1549 }
1550 else if (strcmp(args[3], "state") == 0) {
1551 if (strcmp(args[4], "ready") == 0)
1552 srv_adm_set_ready(sv);
1553 else if (strcmp(args[4], "drain") == 0)
1554 srv_adm_set_drain(sv);
1555 else if (strcmp(args[4], "maint") == 0)
1556 srv_adm_set_maint(sv);
1557 else {
1558 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1559 appctx->st0 = STAT_CLI_PRINT;
1560 }
1561 }
1562 else if (strcmp(args[3], "health") == 0) {
1563 if (sv->track) {
1564 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1565 appctx->st0 = STAT_CLI_PRINT;
1566 }
1567 else if (strcmp(args[4], "up") == 0) {
1568 sv->check.health = sv->check.rise + sv->check.fall - 1;
1569 srv_set_running(sv, "changed from CLI");
1570 }
1571 else if (strcmp(args[4], "stopping") == 0) {
1572 sv->check.health = sv->check.rise + sv->check.fall - 1;
1573 srv_set_stopping(sv, "changed from CLI");
1574 }
1575 else if (strcmp(args[4], "down") == 0) {
1576 sv->check.health = 0;
1577 srv_set_stopped(sv, "changed from CLI");
1578 }
1579 else {
1580 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1581 appctx->st0 = STAT_CLI_PRINT;
1582 }
1583 }
1584 else if (strcmp(args[3], "agent") == 0) {
1585 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1586 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1587 appctx->st0 = STAT_CLI_PRINT;
1588 }
1589 else if (strcmp(args[4], "up") == 0) {
1590 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1591 srv_set_running(sv, "changed from CLI");
1592 }
1593 else if (strcmp(args[4], "down") == 0) {
1594 sv->agent.health = 0;
1595 srv_set_stopped(sv, "changed from CLI");
1596 }
1597 else {
1598 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1599 appctx->st0 = STAT_CLI_PRINT;
1600 }
1601 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001602 else if (strcmp(args[3], "addr") == 0) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001603 warning = server_parse_addr_change_request(sv, args[4], "stats command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001604 if (warning) {
1605 appctx->ctx.cli.msg = warning;
1606 appctx->st0 = STAT_CLI_PRINT;
1607 }
1608 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001609 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001610 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001611 appctx->st0 = STAT_CLI_PRINT;
1612 }
1613 return 1;
1614 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001615 else if (strcmp(args[1], "timeout") == 0) {
1616 if (strcmp(args[2], "cli") == 0) {
1617 unsigned timeout;
1618 const char *res;
1619
1620 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001621 appctx->ctx.cli.msg = "Expects an integer value.\n";
1622 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001623 return 1;
1624 }
1625
1626 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1627 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001628 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1629 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001630 return 1;
1631 }
1632
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001633 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001634 return 1;
1635 }
1636 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001637 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1638 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001639 return 1;
1640 }
1641 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001642 else if (strcmp(args[1], "maxconn") == 0) {
1643 if (strcmp(args[2], "frontend") == 0) {
1644 struct proxy *px;
1645 struct listener *l;
1646 int v;
1647
Willy Tarreau532a4502011-09-07 22:37:44 +02001648 px = expect_frontend_admin(s, si, args[3]);
1649 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001650 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001651
1652 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001653 appctx->ctx.cli.msg = "Integer value expected.\n";
1654 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001655 return 1;
1656 }
1657
1658 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001659 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001660 appctx->ctx.cli.msg = "Value out of range.\n";
1661 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001662 return 1;
1663 }
1664
1665 /* OK, the value is fine, so we assign it to the proxy and to all of
1666 * its listeners. The blocked ones will be dequeued.
1667 */
1668 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001669 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001670 l->maxconn = v;
1671 if (l->state == LI_FULL)
1672 resume_listener(l);
1673 }
1674
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001675 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1676 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001677
1678 return 1;
1679 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001680 else if (strcmp(args[2], "server") == 0) {
1681 struct server *sv;
1682 int v;
1683
1684 sv = expect_server_admin(s, si, args[3]);
1685 if (!sv)
1686 return 1;
1687
1688 if (!*args[4]) {
1689 appctx->ctx.cli.msg = "Integer value expected.\n";
1690 appctx->st0 = STAT_CLI_PRINT;
1691 return 1;
1692 }
1693
1694 v = atoi(args[4]);
1695 if (v < 0) {
1696 appctx->ctx.cli.msg = "Value out of range.\n";
1697 appctx->st0 = STAT_CLI_PRINT;
1698 return 1;
1699 }
1700
1701 if (sv->maxconn == sv->minconn) { // static maxconn
1702 sv->maxconn = sv->minconn = v;
1703 } else { // dynamic maxconn
1704 sv->maxconn = v;
1705 }
1706
1707 if (may_dequeue_tasks(sv, sv->proxy))
1708 process_srv_queue(sv);
1709
1710 return 1;
1711 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001712 else if (strcmp(args[2], "global") == 0) {
1713 int v;
1714
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001715 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001716 appctx->ctx.cli.msg = stats_permission_denied_msg;
1717 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001718 return 1;
1719 }
1720
1721 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001722 appctx->ctx.cli.msg = "Expects an integer value.\n";
1723 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001724 return 1;
1725 }
1726
1727 v = atoi(args[3]);
1728 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001729 appctx->ctx.cli.msg = "Value out of range.\n";
1730 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001731 return 1;
1732 }
1733
1734 /* check for unlimited values */
1735 if (v <= 0)
1736 v = global.hardmaxconn;
1737
1738 global.maxconn = v;
1739
1740 /* Dequeues all of the listeners waiting for a resource */
1741 if (!LIST_ISEMPTY(&global_listener_queue))
1742 dequeue_all_listeners(&global_listener_queue);
1743
1744 return 1;
1745 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001746 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001747 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001748 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001749 return 1;
1750 }
1751 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001752 else if (strcmp(args[1], "rate-limit") == 0) {
1753 if (strcmp(args[2], "connections") == 0) {
1754 if (strcmp(args[3], "global") == 0) {
1755 int v;
1756
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001757 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001758 appctx->ctx.cli.msg = stats_permission_denied_msg;
1759 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001760 return 1;
1761 }
1762
1763 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001764 appctx->ctx.cli.msg = "Expects an integer value.\n";
1765 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001766 return 1;
1767 }
1768
1769 v = atoi(args[4]);
1770 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001771 appctx->ctx.cli.msg = "Value out of range.\n";
1772 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001773 return 1;
1774 }
1775
1776 global.cps_lim = v;
1777
1778 /* Dequeues all of the listeners waiting for a resource */
1779 if (!LIST_ISEMPTY(&global_listener_queue))
1780 dequeue_all_listeners(&global_listener_queue);
1781
1782 return 1;
1783 }
1784 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001785 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1786 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001787 return 1;
1788 }
1789 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001790 else if (strcmp(args[2], "sessions") == 0) {
1791 if (strcmp(args[3], "global") == 0) {
1792 int v;
1793
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001794 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001795 appctx->ctx.cli.msg = stats_permission_denied_msg;
1796 appctx->st0 = STAT_CLI_PRINT;
1797 return 1;
1798 }
1799
1800 if (!*args[4]) {
1801 appctx->ctx.cli.msg = "Expects an integer value.\n";
1802 appctx->st0 = STAT_CLI_PRINT;
1803 return 1;
1804 }
1805
1806 v = atoi(args[4]);
1807 if (v < 0) {
1808 appctx->ctx.cli.msg = "Value out of range.\n";
1809 appctx->st0 = STAT_CLI_PRINT;
1810 return 1;
1811 }
1812
1813 global.sps_lim = v;
1814
1815 /* Dequeues all of the listeners waiting for a resource */
1816 if (!LIST_ISEMPTY(&global_listener_queue))
1817 dequeue_all_listeners(&global_listener_queue);
1818
1819 return 1;
1820 }
1821 else {
1822 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1823 appctx->st0 = STAT_CLI_PRINT;
1824 return 1;
1825 }
1826 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001827#ifdef USE_OPENSSL
1828 else if (strcmp(args[2], "ssl-sessions") == 0) {
1829 if (strcmp(args[3], "global") == 0) {
1830 int v;
1831
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001832 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001833 appctx->ctx.cli.msg = stats_permission_denied_msg;
1834 appctx->st0 = STAT_CLI_PRINT;
1835 return 1;
1836 }
1837
1838 if (!*args[4]) {
1839 appctx->ctx.cli.msg = "Expects an integer value.\n";
1840 appctx->st0 = STAT_CLI_PRINT;
1841 return 1;
1842 }
1843
1844 v = atoi(args[4]);
1845 if (v < 0) {
1846 appctx->ctx.cli.msg = "Value out of range.\n";
1847 appctx->st0 = STAT_CLI_PRINT;
1848 return 1;
1849 }
1850
1851 global.ssl_lim = v;
1852
1853 /* Dequeues all of the listeners waiting for a resource */
1854 if (!LIST_ISEMPTY(&global_listener_queue))
1855 dequeue_all_listeners(&global_listener_queue);
1856
1857 return 1;
1858 }
1859 else {
1860 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1861 appctx->st0 = STAT_CLI_PRINT;
1862 return 1;
1863 }
1864 }
1865#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001866 else if (strcmp(args[2], "http-compression") == 0) {
1867 if (strcmp(args[3], "global") == 0) {
1868 int v;
1869
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01001870 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1871 appctx->ctx.cli.msg = stats_permission_denied_msg;
1872 appctx->st0 = STAT_CLI_PRINT;
1873 return 1;
1874 }
1875
Willy Tarreau85d47f92012-11-21 00:29:50 +01001876 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001877 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1878 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001879 return 1;
1880 }
1881
William Lallemandd85f9172012-11-09 17:05:39 +01001882 v = atoi(args[4]);
1883 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1884 }
1885 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001886 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1887 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001888 return 1;
1889 }
1890 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001891 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001892 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001893 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001894 return 1;
1895 }
1896 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001897 else if (strcmp(args[1], "table") == 0) {
1898 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1899 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001900 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001901 char *err;
1902
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001903 /* Set flags. */
1904 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001905
1906 /* Expect three parameters: map name, key and new value. */
1907 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001908 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001909 appctx->st0 = STAT_CLI_PRINT;
1910 return 1;
1911 }
1912
1913 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001914 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001915 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001916 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001917 appctx->st0 = STAT_CLI_PRINT;
1918 return 1;
1919 }
1920
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001921 /* If the entry identifier start with a '#', it is considered as
1922 * pointer id
1923 */
1924 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1925 struct pat_ref_elt *ref;
1926 long long int conv;
1927 char *error;
1928
1929 /* Convert argument to integer value. */
1930 conv = strtoll(&args[3][1], &error, 16);
1931 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001932 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001933 appctx->st0 = STAT_CLI_PRINT;
1934 return 1;
1935 }
1936
1937 /* Convert and check integer to pointer. */
1938 ref = (struct pat_ref_elt *)(long)conv;
1939 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001940 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001941 appctx->st0 = STAT_CLI_PRINT;
1942 return 1;
1943 }
1944
1945 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001946 err = NULL;
1947 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1948 if (err)
1949 memprintf(&err, "%s.\n", err);
1950 appctx->ctx.cli.err = err;
1951 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001952 return 1;
1953 }
1954 }
1955 else {
1956 /* Else, use the entry identifier as pattern
1957 * string, and update the value.
1958 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001959 err = NULL;
1960 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1961 if (err)
1962 memprintf(&err, "%s.\n", err);
1963 appctx->ctx.cli.err = err;
1964 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001965 return 1;
1966 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001967 }
1968
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001969 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001970 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001971 return 1;
1972 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001973#ifdef USE_OPENSSL
1974 else if (strcmp(args[1], "ssl") == 0) {
1975 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01001976#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001977 char *err = NULL;
1978
Emeric Brunaf4ef742014-06-19 14:10:45 +02001979 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001980 if (!*args[3]) {
1981 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1982 appctx->st0 = STAT_CLI_PRINT;
1983 return 1;
1984 }
1985
1986 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1987 if (trash.len < 0) {
1988 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1989 appctx->st0 = STAT_CLI_PRINT;
1990 return 1;
1991 }
1992
1993 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1994 if (err) {
1995 memprintf(&err, "%s.\n", err);
1996 appctx->ctx.cli.err = err;
1997 appctx->st0 = STAT_CLI_PRINT_FREE;
1998 }
1999 return 1;
2000 }
2001 appctx->ctx.cli.msg = "OCSP Response updated!";
2002 appctx->st0 = STAT_CLI_PRINT;
2003 return 1;
2004#else
2005 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2006 appctx->st0 = STAT_CLI_PRINT;
2007 return 1;
2008#endif
2009 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002010 else if (strcmp(args[2], "tls-key") == 0) {
2011#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2012 /* Expect two parameters: the filename and the new new TLS key in encoding */
2013 if (!*args[3] || !*args[4]) {
2014 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2015 appctx->st0 = STAT_CLI_PRINT;
2016 return 1;
2017 }
2018
2019 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2020 if(!appctx->ctx.tlskeys.ref) {
2021 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2022 appctx->st0 = STAT_CLI_PRINT;
2023 return 1;
2024 }
2025
2026 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2027 if (trash.len != sizeof(struct tls_sess_key)) {
2028 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2029 appctx->st0 = STAT_CLI_PRINT;
2030 return 1;
2031 }
2032
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302033 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2034 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 +02002035
2036 appctx->ctx.cli.msg = "TLS ticket key updated!";
2037 appctx->st0 = STAT_CLI_PRINT;
2038 return 1;
2039#else
2040 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2041 "that doesn't support specifying TLS ticket keys\n";
2042 appctx->st0 = STAT_CLI_PRINT;
2043 return 1;
2044#endif
2045 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002046 else {
2047 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2048 appctx->st0 = STAT_CLI_PRINT;
2049 return 1;
2050 }
2051 }
2052#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002053 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002054 return 0;
2055 }
2056 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002057 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002058 if (strcmp(args[1], "agent") == 0) {
2059 struct server *sv;
2060
2061 sv = expect_server_admin(s, si, args[2]);
2062 if (!sv)
2063 return 1;
2064
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002065 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2066 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2067 appctx->st0 = STAT_CLI_PRINT;
2068 return 1;
2069 }
2070
2071 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002072 return 1;
2073 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002074 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002075 struct server *sv;
2076
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002077 sv = expect_server_admin(s, si, args[2]);
2078 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002079 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002080
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002081 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2082 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2083 appctx->st0 = STAT_CLI_PRINT;
2084 return 1;
2085 }
2086
2087 sv->check.state |= CHK_ST_ENABLED;
2088 return 1;
2089 }
2090 else if (strcmp(args[1], "server") == 0) {
2091 struct server *sv;
2092
2093 sv = expect_server_admin(s, si, args[2]);
2094 if (!sv)
2095 return 1;
2096
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002097 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002098 return 1;
2099 }
2100 else if (strcmp(args[1], "frontend") == 0) {
2101 struct proxy *px;
2102
2103 px = expect_frontend_admin(s, si, args[2]);
2104 if (!px)
2105 return 1;
2106
2107 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002108 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2109 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002110 return 1;
2111 }
2112
2113 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002114 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2115 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002116 return 1;
2117 }
2118
2119 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002120 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2121 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002122 return 1;
2123 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002124 return 1;
2125 }
2126 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002127 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002128 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002129 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002130 }
2131 }
2132 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002133 if (strcmp(args[1], "agent") == 0) {
2134 struct server *sv;
2135
2136 sv = expect_server_admin(s, si, args[2]);
2137 if (!sv)
2138 return 1;
2139
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002140 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002141 return 1;
2142 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002143 else if (strcmp(args[1], "health") == 0) {
2144 struct server *sv;
2145
2146 sv = expect_server_admin(s, si, args[2]);
2147 if (!sv)
2148 return 1;
2149
2150 sv->check.state &= ~CHK_ST_ENABLED;
2151 return 1;
2152 }
Simon Horman671b6f02013-11-25 10:46:39 +09002153 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002154 struct server *sv;
2155
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002156 sv = expect_server_admin(s, si, args[2]);
2157 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002158 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002159
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002160 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002161 return 1;
2162 }
2163 else if (strcmp(args[1], "frontend") == 0) {
2164 struct proxy *px;
2165
2166 px = expect_frontend_admin(s, si, args[2]);
2167 if (!px)
2168 return 1;
2169
2170 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002171 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2172 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002173 return 1;
2174 }
2175
2176 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002177 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2178 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002179 return 1;
2180 }
2181
2182 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002183 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2184 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002185 return 1;
2186 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002187 return 1;
2188 }
2189 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002190 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002191 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002192 return 1;
2193 }
2194 }
2195 else if (strcmp(args[0], "shutdown") == 0) {
2196 if (strcmp(args[1], "frontend") == 0) {
2197 struct proxy *px;
2198
2199 px = expect_frontend_admin(s, si, args[2]);
2200 if (!px)
2201 return 1;
2202
2203 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002204 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2205 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002206 return 1;
2207 }
2208
2209 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2210 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2211 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2212 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2213 stop_proxy(px);
2214 return 1;
2215 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002216 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002217 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002218
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002219 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002220 appctx->ctx.cli.msg = stats_permission_denied_msg;
2221 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002222 return 1;
2223 }
2224
2225 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002226 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2227 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002228 return 1;
2229 }
2230
2231 ptr = (void *)strtoul(args[2], NULL, 0);
2232
Willy Tarreau87b09662015-04-03 00:22:06 +02002233 /* first, look for the requested stream in the stream table */
2234 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002235 if (sess == ptr)
2236 break;
2237 }
2238
Willy Tarreau87b09662015-04-03 00:22:06 +02002239 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002240 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002241 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2242 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002243 return 1;
2244 }
2245
Willy Tarreaue7dff022015-04-03 01:14:29 +02002246 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002247 return 1;
2248 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002249 else if (strcmp(args[1], "sessions") == 0) {
2250 if (strcmp(args[2], "server") == 0) {
2251 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002252 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002253
2254 sv = expect_server_admin(s, si, args[3]);
2255 if (!sv)
2256 return 1;
2257
Willy Tarreau87b09662015-04-03 00:22:06 +02002258 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002259 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2260 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002261 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002262
2263 return 1;
2264 }
2265 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002266 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2267 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002268 return 1;
2269 }
2270 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002271 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002272 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2273 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002274 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002275 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002276 }
2277 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002278 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2279 if (args[1][0] == 'm')
2280 appctx->ctx.map.display_flags = PAT_REF_MAP;
2281 else
2282 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002283
2284 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002285 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2286 if (!*args[2] || !*args[3]) {
2287 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2288 appctx->st0 = STAT_CLI_PRINT;
2289 return 1;
2290 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002291 }
2292
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002293 else {
2294 if (!*args[2] || !*args[3]) {
2295 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2296 appctx->st0 = STAT_CLI_PRINT;
2297 return 1;
2298 }
2299 }
2300
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002301 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002302 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002303 if (!appctx->ctx.map.ref ||
2304 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002305 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002306 appctx->st0 = STAT_CLI_PRINT;
2307 return 1;
2308 }
2309
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002310 /* If the entry identifier start with a '#', it is considered as
2311 * pointer id
2312 */
2313 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2314 struct pat_ref_elt *ref;
2315 long long int conv;
2316 char *error;
2317
2318 /* Convert argument to integer value. */
2319 conv = strtoll(&args[3][1], &error, 16);
2320 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002321 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002322 appctx->st0 = STAT_CLI_PRINT;
2323 return 1;
2324 }
2325
2326 /* Convert and check integer to pointer. */
2327 ref = (struct pat_ref_elt *)(long)conv;
2328 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002329 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002330 appctx->st0 = STAT_CLI_PRINT;
2331 return 1;
2332 }
2333
2334 /* Try to delete the entry. */
2335 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2336 /* The entry is not found, send message. */
2337 appctx->ctx.cli.msg = "Key not found.\n";
2338 appctx->st0 = STAT_CLI_PRINT;
2339 return 1;
2340 }
2341 }
2342 else {
2343 /* Else, use the entry identifier as pattern
2344 * string and try to delete the entry.
2345 */
2346 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2347 /* The entry is not found, send message. */
2348 appctx->ctx.cli.msg = "Key not found.\n";
2349 appctx->st0 = STAT_CLI_PRINT;
2350 return 1;
2351 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002352 }
2353
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002354 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002355 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002356 return 1;
2357 }
2358 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002359 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002360 appctx->st0 = STAT_CLI_PRINT;
2361 return 1;
2362 }
2363 }
2364 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002365 if (strcmp(args[1], "map") == 0 ||
2366 strcmp(args[1], "acl") == 0) {
2367 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002368 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002369
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002370 /* Set flags. */
2371 if (args[1][0] == 'm')
2372 appctx->ctx.map.display_flags = PAT_REF_MAP;
2373 else
2374 appctx->ctx.map.display_flags = PAT_REF_ACL;
2375
2376 /* If the keywork is "map", we expect three parameters, if it
2377 * is "acl", we expect only two parameters
2378 */
2379 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2380 if (!*args[2] || !*args[3] || !*args[4]) {
2381 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2382 appctx->st0 = STAT_CLI_PRINT;
2383 return 1;
2384 }
2385 }
2386 else {
2387 if (!*args[2] || !*args[3]) {
2388 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2389 appctx->st0 = STAT_CLI_PRINT;
2390 return 1;
2391 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002392 }
2393
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002394 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002395 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002396 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002397 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002398 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002399 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002400 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002401 appctx->st0 = STAT_CLI_PRINT;
2402 return 1;
2403 }
2404
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002405 /* The command "add acl" is prohibited if the reference
2406 * use samples.
2407 */
2408 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2409 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2410 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2411 "You must use the command 'add map' to add values.\n";
2412 appctx->st0 = STAT_CLI_PRINT;
2413 return 1;
2414 }
2415
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002416 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002417 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002418 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002419 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002420 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002421 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002422 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002423 if (err)
2424 memprintf(&err, "%s.\n", err);
2425 appctx->ctx.cli.err = err;
2426 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002427 return 1;
2428 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002429
2430 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002431 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002432 return 1;
2433 }
2434 else { /* unknown "del" parameter */
2435 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2436 appctx->st0 = STAT_CLI_PRINT;
2437 return 1;
2438 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002439 }
2440 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002441 return 0;
2442 }
2443 return 1;
2444}
2445
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002446/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002447 * used to processes I/O from/to the stats unix socket. The system relies on a
2448 * state machine handling requests and various responses. We read a request,
2449 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002450 * Then we can read again. The state is stored in appctx->st0 and is one of the
2451 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002452 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002453 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002454static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002455{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002456 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002457 struct channel *req = si_oc(si);
2458 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002459 int reql;
2460 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002461
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002462 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2463 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002464
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002465 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002466 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002467 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002468 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2469 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002470 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002471 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002472 /* Let's close for real now. We just close the request
2473 * side, the conditions below will complete if needed.
2474 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002475 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002476 break;
2477 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002478 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002479 /* ensure we have some output room left in the event we
2480 * would want to return some info right after parsing.
2481 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002482 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002483 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002484 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002485 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002486
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002487 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002488 if (reql <= 0) { /* closed or EOL not found */
2489 if (reql == 0)
2490 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002491 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002492 continue;
2493 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002494
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002495 /* seek for a possible semi-colon. If we find one, we
2496 * replace it with an LF and skip only this part.
2497 */
2498 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002499 if (trash.str[len] == ';') {
2500 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002501 reql = len + 1;
2502 break;
2503 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002504
Willy Tarreau816fc222009-10-04 07:36:58 +02002505 /* now it is time to check that we have a full line,
2506 * remove the trailing \n and possibly \r, then cut the
2507 * line.
2508 */
2509 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002510 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002511 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002512 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002513 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002514
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002515 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002516 len--;
2517
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002518 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002519
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002520 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002521 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002522 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002523 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002524 continue;
2525 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002526 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002527 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002528 else if (strcmp(trash.str, "help") == 0 ||
2529 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002530 appctx->ctx.cli.msg = stats_sock_usage_msg;
2531 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002532 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002533 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002534 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002535 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002536 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002537 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002538 /* if prompt is disabled, print help on empty lines,
2539 * so that the user at least knows how to enable
2540 * prompt and find help.
2541 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002542 appctx->ctx.cli.msg = stats_sock_usage_msg;
2543 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002544 }
2545
2546 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002547 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002548 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002549 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002550 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002551 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002552 case STAT_CLI_PROMPT:
2553 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002554 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002555 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002556 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002557 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002558 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002559 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002560 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002561 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002562 free(appctx->ctx.cli.err);
2563 appctx->st0 = STAT_CLI_PROMPT;
2564 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002565 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002566 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002567 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002568 case STAT_CLI_O_BACKEND:
2569 if (stats_dump_backend_to_buffer(si))
2570 appctx->st0 = STAT_CLI_PROMPT;
2571 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002572 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002573 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002574 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002575 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002576 case STAT_CLI_O_SERVERS_STATE:
2577 if (stats_dump_servers_state_to_buffer(si))
2578 appctx->st0 = STAT_CLI_PROMPT;
2579 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002580 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002581 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002582 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002583 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002584 case STAT_CLI_O_RESOLVERS:
2585 if (stats_dump_resolvers_to_buffer(si))
2586 appctx->st0 = STAT_CLI_PROMPT;
2587 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002588 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002589 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002590 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002591 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002592 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002593 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002594 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002595 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002596 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002597 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002598 if (stats_table_request(si, appctx->st0))
2599 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002600 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002601 case STAT_CLI_O_PATS:
2602 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002603 appctx->st0 = STAT_CLI_PROMPT;
2604 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002605 case STAT_CLI_O_PAT:
2606 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002607 appctx->st0 = STAT_CLI_PROMPT;
2608 break;
2609 case STAT_CLI_O_MLOOK:
2610 if (stats_map_lookup(si))
2611 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002612 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002613 case STAT_CLI_O_POOLS:
2614 if (stats_dump_pools_to_buffer(si))
2615 appctx->st0 = STAT_CLI_PROMPT;
2616 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002617#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2618 case STAT_CLI_O_TLSK:
2619 if (stats_tlskeys_list(si))
2620 appctx->st0 = STAT_CLI_PROMPT;
2621 break;
2622#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002623 case STAT_CLI_O_ENV: /* environment dump */
2624 if (stats_dump_env_to_buffer(si))
2625 appctx->st0 = STAT_CLI_PROMPT;
2626 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002627 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002628 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002629 break;
2630 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002631
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002632 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002633 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002634 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002635 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002636 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002637 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002638 }
2639
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002640 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002641 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002642 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002643
2644 /* Now we close the output if one of the writers did so,
2645 * or if we're not in interactive mode and the request
2646 * buffer is empty. This still allows pipelined requests
2647 * to be sent in non-interactive mode.
2648 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002649 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2650 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002651 continue;
2652 }
2653
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002654 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002655 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002656 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002657 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002658
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002659 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002660 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2661 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002662 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002663 * and nothing more to consume. This is comparable to a broken pipe, so
2664 * we forward the close to the request side so that it flows upstream to
2665 * the client.
2666 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002667 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002668 }
2669
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002670 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002671 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2672 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2673 /* We have no more processing to do, and nothing more to send, and
2674 * the client side has closed. So we'll forward this state downstream
2675 * on the response buffer.
2676 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002677 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002678 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002679 }
2680
Willy Tarreau828824a2015-04-19 17:20:03 +02002681 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002682 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 +02002683 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002684 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 +02002685}
2686
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002687/* This function dumps information onto the stream interface's read buffer.
2688 * It returns 0 as long as it does not complete, non-zero upon completion.
2689 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002690 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002691static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002692{
2693 unsigned int up = (now.tv_sec - start_date.tv_sec);
2694
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002695#ifdef USE_OPENSSL
2696 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2697 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2698 int ssl_reuse = 0;
2699
2700 if (ssl_key_rate < ssl_sess_rate) {
2701 /* count the ssl reuse ratio and avoid overflows in both directions */
2702 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2703 }
2704#endif
2705
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002706 chunk_printf(&trash,
2707 "Name: " PRODUCT_NAME "\n"
2708 "Version: " HAPROXY_VERSION "\n"
2709 "Release_date: " HAPROXY_DATE "\n"
2710 "Nbproc: %d\n"
2711 "Process_num: %d\n"
2712 "Pid: %d\n"
2713 "Uptime: %dd %dh%02dm%02ds\n"
2714 "Uptime_sec: %d\n"
2715 "Memmax_MB: %d\n"
Willy Tarreau58102cf2015-10-28 16:24:21 +01002716 "PoolAlloc_MB: %d\n"
2717 "PoolUsed_MB: %d\n"
2718 "PoolFailed: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002719 "Ulimit-n: %d\n"
2720 "Maxsock: %d\n"
2721 "Maxconn: %d\n"
2722 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002723 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002724 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002725 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002726#ifdef USE_OPENSSL
2727 "MaxSslConns: %d\n"
2728 "CurrSslConns: %d\n"
2729 "CumSslConns: %d\n"
2730#endif
2731 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002732 "PipesUsed: %d\n"
2733 "PipesFree: %d\n"
2734 "ConnRate: %d\n"
2735 "ConnRateLimit: %d\n"
2736 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002737 "SessRate: %d\n"
2738 "SessRateLimit: %d\n"
2739 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002740#ifdef USE_OPENSSL
2741 "SslRate: %d\n"
2742 "SslRateLimit: %d\n"
2743 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002744 "SslFrontendKeyRate: %d\n"
2745 "SslFrontendMaxKeyRate: %d\n"
2746 "SslFrontendSessionReuse_pct: %d\n"
2747 "SslBackendKeyRate: %d\n"
2748 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002749 "SslCacheLookups: %u\n"
2750 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002751#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002752 "CompressBpsIn: %u\n"
2753 "CompressBpsOut: %u\n"
2754 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002755#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002756 "ZlibMemUsage: %ld\n"
2757 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002758#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002759 "Tasks: %d\n"
2760 "Run_queue: %d\n"
2761 "Idle_pct: %d\n"
2762 "node: %s\n"
2763 "description: %s\n"
2764 "",
2765 global.nbproc,
2766 relative_pid,
2767 pid,
2768 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2769 up,
2770 global.rlimit_memmax,
Willy Tarreau58102cf2015-10-28 16:24:21 +01002771 (int)(pool_total_allocated() / 1048576L),
2772 (int)(pool_total_used() / 1048576L),
2773 pool_total_failures(),
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002774 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002775 global.maxsock, global.maxconn, global.hardmaxconn,
2776 actconn, totalconn, global.req_count,
2777#ifdef USE_OPENSSL
2778 global.maxsslconn, sslconns, totalsslconns,
2779#endif
2780 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002781 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002782 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002783#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002784 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2785 ssl_key_rate, global.ssl_fe_keys_max,
2786 ssl_reuse,
2787 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002788 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002789#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002790 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2791 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002792#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002793 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002794#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002795 nb_tasks_cur, run_queue_cur, idle_pct,
2796 global.node, global.desc ? global.desc : ""
2797 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002798
Willy Tarreaubc18da12015-03-13 14:00:47 +01002799 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002800 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002801 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002802 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002803
2804 return 1;
2805}
2806
Baptiste Assmann28289462015-07-03 08:01:20 +02002807/* dumps server state information into <buf> for all the servers found in <backend>
2808 * These information are all the parameters which may change during HAProxy runtime.
2809 * By default, we only export to the last known server state file format.
2810 * These information can be used at next startup to recover same level of server state.
2811 */
2812static void dump_servers_state(struct proxy *backend, struct chunk *buf)
2813{
2814 struct server *srv;
2815 char srv_addr[INET6_ADDRSTRLEN + 1];
2816 time_t srv_time_since_last_change;
2817 int bk_f_forced_id, srv_f_forced_id;
2818
2819 /* we don't want to report any state if the backend is not enabled on this process */
2820 if (backend->bind_proc && !(backend->bind_proc & (1UL << (relative_pid - 1))))
2821 return;
2822
2823 srv = backend->srv;
2824
2825 while (srv) {
2826 srv_addr[0] = '\0';
2827 srv_time_since_last_change = 0;
2828 bk_f_forced_id = 0;
2829 srv_f_forced_id = 0;
2830
2831 switch (srv->addr.ss_family) {
2832 case AF_INET:
2833 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
2834 srv_addr, INET_ADDRSTRLEN + 1);
2835 break;
2836 case AF_INET6:
2837 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
2838 srv_addr, INET6_ADDRSTRLEN + 1);
2839 break;
2840 }
2841 srv_time_since_last_change = now.tv_sec - srv->last_change;
2842 bk_f_forced_id = backend->options & PR_O_FORCED_ID ? 1 : 0;
2843 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
2844
2845 chunk_appendf(buf,
2846 "%d %s "
2847 "%d %s %s "
2848 "%d %d %d %d %ld "
2849 "%d %d %d %d %d "
2850 "%d %d"
2851 "\n",
2852 backend->uuid, backend->id,
2853 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00002854 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02002855 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
2856 bk_f_forced_id, srv_f_forced_id);
2857
2858 srv = srv->next;
2859 }
2860}
2861
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002862/* Parses backend list and simply report backend names */
2863static int stats_dump_backend_to_buffer(struct stream_interface *si)
2864{
2865 extern struct proxy *proxy;
2866 struct proxy *curproxy;
2867
2868 chunk_reset(&trash);
2869 chunk_printf(&trash, "# name\n");
2870
2871 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2872 /* looking for backends only */
2873 if (!(curproxy->cap & PR_CAP_BE))
2874 continue;
2875
2876 /* we don't want to list a backend which is bound to this process */
2877 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
2878 continue;
2879
2880 chunk_appendf(&trash, "%s\n", curproxy->id);
2881 }
2882
2883 if (bi_putchk(si_ic(si), &trash) == -1) {
2884 si_applet_cant_put(si);
2885 return 0;
2886 }
2887
2888 return 1;
2889}
2890
Baptiste Assmann28289462015-07-03 08:01:20 +02002891/* Parses backend list or simply use backend name provided by the user to return
2892 * states of servers to stdout.
2893 */
2894static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
2895{
2896 struct appctx *appctx = __objt_appctx(si->end);
2897 extern struct proxy *proxy;
2898 struct proxy *curproxy;
2899
2900 chunk_reset(&trash);
2901
2902 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
2903
2904 if (appctx->ctx.server_state.backend) {
2905 dump_servers_state(appctx->ctx.server_state.backend, &trash);
2906 }
2907 else {
2908 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2909 /* servers are only in backends */
2910 if (!(curproxy->cap & PR_CAP_BE))
2911 continue;
2912
2913 dump_servers_state(curproxy, &trash);
2914 }
2915 }
2916
2917 if (bi_putchk(si_ic(si), &trash) == -1) {
2918 si_applet_cant_put(si);
2919 return 0;
2920 }
2921
2922 return 1;
2923}
2924
Willy Tarreau12833bb2014-01-28 16:49:56 +01002925/* This function dumps memory usage information onto the stream interface's
2926 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2927 * completion. No state is used.
2928 */
2929static int stats_dump_pools_to_buffer(struct stream_interface *si)
2930{
2931 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01002932 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002933 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01002934 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002935 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01002936 return 1;
2937}
2938
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002939/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2940 * the state from stream interface <si>. The caller is responsible for clearing
2941 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002942 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002943static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002944{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002945 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002946 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002947
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002948 if (!(px->cap & PR_CAP_FE))
2949 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002950
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002951 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002952 return 0;
2953
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002954 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002955 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002956 /* name, queue */
2957 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002958
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002959 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002960 /* Column sub-heading for Enable or Disable server */
2961 chunk_appendf(&trash, "<td></td>");
2962 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002963
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002964 chunk_appendf(&trash,
2965 "<td class=ac>"
2966 "<a name=\"%s/Frontend\"></a>"
2967 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2968 "<td colspan=3></td>"
2969 "",
2970 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002971
Willy Tarreau466c9b52012-12-23 02:25:03 +01002972 chunk_appendf(&trash,
2973 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002974 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002975 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2976 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2977 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002978 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2979 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2980 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002981
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002982 if (px->mode == PR_MODE_HTTP)
2983 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002984 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002985 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002986
2987 chunk_appendf(&trash,
2988 "</table></div></u></td>"
2989 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002990 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002991 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2992 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2993 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002994 U2H(px->fe_counters.sps_max),
2995 U2H(px->fe_counters.cps_max),
2996 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002997
2998 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002999 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003000 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003001 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003002
3003 chunk_appendf(&trash,
3004 "</table></div></u></td>"
3005 /* sessions rate : limit */
3006 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003007 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003008
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003009 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003010 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003011 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003012 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003013 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3014 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003015 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003016 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
3017 U2H(px->fe_counters.cum_sess),
3018 U2H(px->fe_counters.cum_conn),
3019 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003020
Willy Tarreau466c9b52012-12-23 02:25:03 +01003021 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003022 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003023 chunk_appendf(&trash,
3024 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3025 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3026 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3027 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3028 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3029 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3030 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3031 "<tr><th>- other responses:</th><td>%s</td></tr>"
3032 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3033 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003034 U2H(px->fe_counters.p.http.cum_req),
3035 U2H(px->fe_counters.p.http.rsp[1]),
3036 U2H(px->fe_counters.p.http.rsp[2]),
3037 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003038 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003039 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003040 U2H(px->fe_counters.p.http.rsp[3]),
3041 U2H(px->fe_counters.p.http.rsp[4]),
3042 U2H(px->fe_counters.p.http.rsp[5]),
3043 U2H(px->fe_counters.p.http.rsp[0]),
3044 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003045 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003046
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003047 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003048 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003049 /* sessions: lbtot, lastsess */
3050 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003051 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003052 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003053 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003054 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003055
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003056 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003057 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003058 "<td>%s%s<div class=tips><table class=det>"
3059 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3060 "<tr><th>Compression in:</th><td>%s</td></tr>"
3061 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3062 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3063 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3064 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003065 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003066 U2H(px->fe_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003067 U2H(px->fe_counters.bytes_out),
3068 U2H(px->fe_counters.comp_in),
3069 U2H(px->fe_counters.comp_out),
3070 px->fe_counters.comp_in ? (int)(px->fe_counters.comp_out * 100 / px->fe_counters.comp_in) : 0,
3071 U2H(px->fe_counters.comp_byp),
3072 U2H(px->fe_counters.comp_in - px->fe_counters.comp_out),
3073 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 +01003074 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003075
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003076 chunk_appendf(&trash,
3077 /* denied: req, resp */
3078 "<td>%s</td><td>%s</td>"
3079 /* errors : request, connect, response */
3080 "<td>%s</td><td></td><td></td>"
3081 /* warnings: retries, redispatches */
3082 "<td></td><td></td>"
3083 /* server status : reflect frontend status */
3084 "<td class=ac>%s</td>"
3085 /* rest of server: nothing */
3086 "<td class=ac colspan=8></td></tr>"
3087 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003088 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
3089 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003090 px->state == PR_STREADY ? "OPEN" :
3091 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003092 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003093 else { /* CSV mode */
3094 chunk_appendf(&trash,
3095 /* pxid, name, queue cur, queue max, */
3096 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003097 /* sessions : current, max, limit, total */
3098 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003099 /* bytes : in, out */
3100 "%lld,%lld,"
3101 /* denied: req, resp */
3102 "%lld,%lld,"
3103 /* errors : request, connect, response */
3104 "%lld,,,"
3105 /* warnings: retries, redispatches */
3106 ",,"
3107 /* server status : reflect frontend status */
3108 "%s,"
3109 /* rest of server: nothing */
3110 ",,,,,,,,"
3111 /* pid, iid, sid, throttle, lbtot, tracked, type */
3112 "%d,%d,0,,,,%d,"
3113 /* rate, rate_lim, rate_max */
3114 "%u,%u,%u,"
3115 /* check_status, check_code, check_duration */
3116 ",,,",
3117 px->id,
3118 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
3119 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
3120 px->fe_counters.denied_req, px->fe_counters.denied_resp,
3121 px->fe_counters.failed_req,
3122 px->state == PR_STREADY ? "OPEN" :
3123 px->state == PR_STFULL ? "FULL" : "STOP",
3124 relative_pid, px->uuid, STATS_TYPE_FE,
3125 read_freq_ctr(&px->fe_sess_per_sec),
3126 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003127
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003128 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3129 if (px->mode == PR_MODE_HTTP) {
3130 for (i=1; i<6; i++)
3131 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
3132 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
3133 }
3134 else
3135 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003136
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003137 /* failed health analyses */
3138 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003139
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003140 /* requests : req_rate, req_rate_max, req_tot, */
3141 chunk_appendf(&trash, "%u,%u,%lld,",
3142 read_freq_ctr(&px->fe_req_per_sec),
3143 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
3144
3145 /* errors: cli_aborts, srv_aborts */
3146 chunk_appendf(&trash, ",,");
3147
3148 /* compression: in, out, bypassed */
3149 chunk_appendf(&trash, "%lld,%lld,%lld,",
3150 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
3151
3152 /* compression: comp_rsp */
3153 chunk_appendf(&trash, "%lld,",
3154 px->fe_counters.p.http.comp_rsp);
3155
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003156 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3157 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003158
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003159 /* finish with EOL */
3160 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003161 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003162 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003163}
3164
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003165/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
3166 * from stream interface <si>, and stats flags <flags>. The caller is responsible
3167 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3168 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02003169 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003170static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02003171{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003172 struct appctx *appctx = __objt_appctx(si->end);
3173
3174 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003175 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003176 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003177 /* Column sub-heading for Enable or Disable server */
3178 chunk_appendf(&trash, "<td></td>");
3179 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003180 chunk_appendf(&trash,
3181 /* frontend name, listener name */
3182 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3183 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3184 "",
3185 px->id, l->name,
3186 (flags & ST_SHLGNDS)?"<u>":"",
3187 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02003188
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003189 if (flags & ST_SHLGNDS) {
3190 char str[INET6_ADDRSTRLEN];
3191 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02003192
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003193 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02003194
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003195 port = get_host_port(&l->addr);
3196 switch (addr_to_str(&l->addr, str, sizeof(str))) {
3197 case AF_INET:
3198 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
3199 break;
3200 case AF_INET6:
3201 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
3202 break;
3203 case AF_UNIX:
3204 chunk_appendf(&trash, "unix, ");
3205 break;
3206 case -1:
3207 chunk_appendf(&trash, "(%s), ", strerror(errno));
3208 break;
3209 }
Willy Tarreau91861262007-10-17 17:06:05 +02003210
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003211 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003212 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003213 }
Willy Tarreau91861262007-10-17 17:06:05 +02003214
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003215 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003216 /* queue */
3217 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003218 /* sessions rate: current, max, limit */
3219 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003220 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003221 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003222 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003223 /* bytes: in, out */
3224 "<td>%s</td><td>%s</td>"
3225 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003226 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003227 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
3228 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02003229
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003230 chunk_appendf(&trash,
3231 /* denied: req, resp */
3232 "<td>%s</td><td>%s</td>"
3233 /* errors: request, connect, response */
3234 "<td>%s</td><td></td><td></td>"
3235 /* warnings: retries, redispatches */
3236 "<td></td><td></td>"
3237 /* server status: reflect listener status */
3238 "<td class=ac>%s</td>"
3239 /* rest of server: nothing */
3240 "<td class=ac colspan=8></td></tr>"
3241 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003242 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
3243 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003244 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
3245 }
3246 else { /* CSV mode */
3247 chunk_appendf(&trash,
3248 /* pxid, name, queue cur, queue max, */
3249 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003250 /* sessions: current, max, limit, total */
3251 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003252 /* bytes: in, out */
3253 "%lld,%lld,"
3254 /* denied: req, resp */
3255 "%lld,%lld,"
3256 /* errors: request, connect, response */
3257 "%lld,,,"
3258 /* warnings: retries, redispatches */
3259 ",,"
3260 /* server status: reflect listener status */
3261 "%s,"
3262 /* rest of server: nothing */
3263 ",,,,,,,,"
3264 /* pid, iid, sid, throttle, lbtot, tracked, type */
3265 "%d,%d,%d,,,,%d,"
3266 /* rate, rate_lim, rate_max */
3267 ",,,"
3268 /* check_status, check_code, check_duration */
3269 ",,,"
3270 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3271 ",,,,,,"
3272 /* failed health analyses */
3273 ","
3274 /* requests : req_rate, req_rate_max, req_tot, */
3275 ",,,"
3276 /* errors: cli_aborts, srv_aborts */
3277 ",,"
3278 /* compression: in, out, bypassed, comp_rsp */
3279 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003280 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3281 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003282 "\n",
3283 px->id, l->name,
3284 l->nbconn, l->counters->conn_max,
3285 l->maxconn, l->counters->cum_conn,
3286 l->counters->bytes_in, l->counters->bytes_out,
3287 l->counters->denied_req, l->counters->denied_resp,
3288 l->counters->failed_req,
3289 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
3290 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
3291 }
3292 return 1;
3293}
Willy Tarreau91861262007-10-17 17:06:05 +02003294
Simon Horman4d2eab62015-04-23 14:51:26 +09003295enum srv_stats_state {
3296 SRV_STATS_STATE_DOWN = 0,
3297 SRV_STATS_STATE_DOWN_AGENT,
3298 SRV_STATS_STATE_GOING_UP,
3299 SRV_STATS_STATE_UP_GOING_DOWN,
3300 SRV_STATS_STATE_UP,
3301 SRV_STATS_STATE_NOLB_GOING_DOWN,
3302 SRV_STATS_STATE_NOLB,
3303 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3304 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003305 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003306 SRV_STATS_STATE_NO_CHECK,
3307
3308 SRV_STATS_STATE_COUNT, /* Must be last */
3309};
3310
Simon Horman837bfa72015-04-23 14:51:27 +09003311enum srv_stats_colour {
3312 SRV_STATS_COLOUR_DOWN = 0,
3313 SRV_STATS_COLOUR_GOING_UP,
3314 SRV_STATS_COLOUR_GOING_DOWN,
3315 SRV_STATS_COLOUR_UP,
3316 SRV_STATS_COLOUR_NOLB,
3317 SRV_STATS_COLOUR_DRAINING,
3318 SRV_STATS_COLOUR_NO_CHECK,
3319
3320 SRV_STATS_COLOUR_COUNT, /* Must be last */
3321};
3322
3323static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3324 [SRV_STATS_COLOUR_DOWN] = "down",
3325 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3326 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3327 [SRV_STATS_COLOUR_UP] = "up",
3328 [SRV_STATS_COLOUR_NOLB] = "nolb",
3329 [SRV_STATS_COLOUR_DRAINING] = "draining",
3330 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3331};
3332
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003333/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3334 * from stream interface <si>, stats flags <flags>, and server state <state>.
3335 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003336 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003337 */
Simon Horman4d2eab62015-04-23 14:51:26 +09003338static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09003339 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003340{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003341 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003342 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 char str[INET6_ADDRSTRLEN];
3344 struct chunk src;
3345 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02003346
Willy Tarreau32091232014-05-16 13:52:00 +02003347 /* we have "via" which is the tracked server as described in the configuration,
3348 * and "ref" which is the checked server and the end of the chain.
3349 */
3350 via = sv->track ? sv->track : sv;
3351 ref = via;
3352 while (ref->track)
3353 ref = ref->track;
3354
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003355 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003356 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3357 [SRV_STATS_STATE_DOWN] = "DOWN",
3358 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3359 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
3360 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
3361 [SRV_STATS_STATE_UP] = "UP",
3362 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
3363 [SRV_STATS_STATE_NOLB] = "NOLB",
3364 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
3365 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003366 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003367 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003368 };
Willy Tarreau91861262007-10-17 17:06:05 +02003369
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003370 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003371 chunk_appendf(&trash, "<tr class=\"maintain\">");
3372 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003373 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003374 "<tr class=\"%s_%s\">",
3375 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003376
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003377 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003378 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3380 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003381
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003382 chunk_appendf(&trash,
3383 "<td class=ac><a name=\"%s/%s\"></a>%s"
3384 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3385 "",
3386 px->id, sv->id,
3387 (flags & ST_SHLGNDS) ? "<u>" : "",
3388 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003389
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003390 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003391 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003392
3393 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3394 case AF_INET:
3395 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3396 break;
3397 case AF_INET6:
3398 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3399 break;
3400 case AF_UNIX:
3401 chunk_appendf(&trash, "unix, ");
3402 break;
3403 case -1:
3404 chunk_appendf(&trash, "(%s), ", strerror(errno));
3405 break;
3406 default: /* address family not supported */
3407 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003408 }
Willy Tarreau91861262007-10-17 17:06:05 +02003409
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003410 /* id */
3411 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003412
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003413 /* cookie */
3414 if (sv->cookie) {
3415 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003416
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003417 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3418 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003419
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003420 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003421 }
3422
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003423 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003424 }
Willy Tarreau91861262007-10-17 17:06:05 +02003425
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003426 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003427 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003428 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003429 /* sessions rate : current, max, limit */
3430 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003431 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003432 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003433 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3434 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003435
Willy Tarreau466c9b52012-12-23 02:25:03 +01003436
3437 chunk_appendf(&trash,
3438 /* sessions: current, max, limit, total */
3439 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003440 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003441 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3442 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003443 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3444 U2H(sv->counters.cum_sess),
3445 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003446
Willy Tarreau466c9b52012-12-23 02:25:03 +01003447 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3448 if (px->mode == PR_MODE_HTTP) {
3449 unsigned long long tot;
3450 for (tot = i = 0; i < 6; i++)
3451 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003452
Willy Tarreau466c9b52012-12-23 02:25:03 +01003453 chunk_appendf(&trash,
3454 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3455 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3456 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3457 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3458 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3459 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3460 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3461 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003462 U2H(tot),
3463 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3464 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3465 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3466 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3467 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3468 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 +02003469 }
Willy Tarreau91861262007-10-17 17:06:05 +02003470
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003471 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3472 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)));
3473 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)));
3474 if (px->mode == PR_MODE_HTTP)
3475 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)));
3476 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)));
3477
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003478 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003479 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003480 /* sessions: lbtot, last */
3481 "<td>%s</td><td>%s</td>",
3482 U2H(sv->counters.cum_lbconn),
3483 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003484
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003485 chunk_appendf(&trash,
3486 /* bytes : in, out */
3487 "<td>%s</td><td>%s</td>"
3488 /* denied: req, resp */
3489 "<td></td><td>%s</td>"
3490 /* errors : request, connect */
3491 "<td></td><td>%s</td>"
3492 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003493 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003494 /* warnings: retries, redispatches */
3495 "<td>%lld</td><td>%lld</td>"
3496 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003497 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3498 U2H(sv->counters.failed_secu),
3499 U2H(sv->counters.failed_conns),
3500 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003501 sv->counters.cli_aborts,
3502 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003503 sv->counters.retries, sv->counters.redispatches);
3504
3505 /* status, lest check */
3506 chunk_appendf(&trash, "<td class=ac>");
3507
Willy Tarreau20125212014-05-13 19:44:56 +02003508 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003509 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3510 chunk_appendf(&trash, "MAINT");
3511 }
Simon Horman0766e442014-11-12 15:55:54 +09003512 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003513 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003514 /* DOWN (agent) */
3515 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 +02003516 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003517 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003518 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3519 chunk_appendf(&trash,
3520 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003521 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3522 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003523 }
Willy Tarreau91861262007-10-17 17:06:05 +02003524
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003525 if ((sv->state == SRV_ST_STOPPED) &&
3526 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3527 chunk_appendf(&trash,
3528 "</td><td class=ac><u> %s%s",
3529 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3530 get_check_status_info(sv->agent.status));
3531
3532 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3533 chunk_appendf(&trash, "/%d", sv->agent.code);
3534
3535 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3536 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3537
3538 chunk_appendf(&trash, "<div class=tips>%s",
3539 get_check_status_description(sv->agent.status));
3540 if (*sv->agent.desc) {
3541 chunk_appendf(&trash, ": ");
3542 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3543 chunk_htmlencode(&trash, &src);
3544 }
3545 chunk_appendf(&trash, "</div></u>");
3546 }
3547 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003548 chunk_appendf(&trash,
3549 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003550 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003551 get_check_status_info(sv->check.status));
3552
3553 if (sv->check.status >= HCHK_STATUS_L57DATA)
3554 chunk_appendf(&trash, "/%d", sv->check.code);
3555
3556 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003557 chunk_appendf(&trash, " in %lums", sv->check.duration);
3558
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003559 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003560 get_check_status_description(sv->check.status));
3561 if (*sv->check.desc) {
3562 chunk_appendf(&trash, ": ");
3563 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3564 chunk_htmlencode(&trash, &src);
3565 }
3566 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003567 }
3568 else
3569 chunk_appendf(&trash, "</td><td>");
3570
3571 chunk_appendf(&trash,
3572 /* weight */
3573 "</td><td class=ac>%d</td>"
3574 /* act, bck */
3575 "<td class=ac>%s</td><td class=ac>%s</td>"
3576 "",
3577 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003578 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3579 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003580
3581 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003582 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003583 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003584
3585 if (ref->observe)
3586 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3587
3588 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003589 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003590 "<td>%lld</td><td>%s</td>"
3591 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003592 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003593 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3594 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003595 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3596 /* tracking a server */
3597 chunk_appendf(&trash,
3598 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003599 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003600 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003601 else
3602 chunk_appendf(&trash, "<td colspan=3></td>");
3603
3604 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003605 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003606 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003607 else
3608 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3609 }
3610 else { /* CSV mode */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003611 struct chunk *out = get_trash_chunk();
Simon Horman4d2eab62015-04-23 14:51:26 +09003612 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3613 [SRV_STATS_STATE_DOWN] = "DOWN,",
3614 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3615 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3616 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3617 [SRV_STATS_STATE_UP] = "UP,",
3618 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3619 [SRV_STATS_STATE_NOLB] = "NOLB,",
3620 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3621 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Raghu Udiyar0d6b7a42016-02-05 22:30:11 +05303622 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent),",
Simon Horman4d2eab62015-04-23 14:51:26 +09003623 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003624 };
3625
3626 chunk_appendf(&trash,
3627 /* pxid, name */
3628 "%s,%s,"
3629 /* queue : current, max */
3630 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003631 /* sessions : current, max, limit, total */
3632 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003633 /* bytes : in, out */
3634 "%lld,%lld,"
3635 /* denied: req, resp */
3636 ",%lld,"
3637 /* errors : request, connect, response */
3638 ",%lld,%lld,"
3639 /* warnings: retries, redispatches */
3640 "%lld,%lld,"
3641 "",
3642 px->id, sv->id,
3643 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003644 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003645 sv->counters.bytes_in, sv->counters.bytes_out,
3646 sv->counters.failed_secu,
3647 sv->counters.failed_conns, sv->counters.failed_resp,
3648 sv->counters.retries, sv->counters.redispatches);
3649
3650 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003651 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003652 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003653 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003654 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003655 else
3656 chunk_appendf(&trash,
3657 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003658 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3659 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003660
3661 chunk_appendf(&trash,
3662 /* weight, active, backup */
3663 "%d,%d,%d,"
3664 "",
3665 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003666 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3667 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003668
3669 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003670 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003671 chunk_appendf(&trash,
3672 "%lld,%lld,%d,%d,",
3673 sv->counters.failed_checks, sv->counters.down_trans,
3674 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3675 else
3676 chunk_appendf(&trash, ",,,,");
3677
3678 /* queue limit, pid, iid, sid, */
3679 chunk_appendf(&trash,
3680 "%s,"
3681 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003682 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003683 relative_pid, px->uuid, sv->puid);
3684
3685 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003686 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003687 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003688
3689 /* sessions: lbtot */
3690 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3691
3692 /* tracked */
3693 if (sv->track)
3694 chunk_appendf(&trash, "%s/%s,",
3695 sv->track->proxy->id, sv->track->id);
3696 else
3697 chunk_appendf(&trash, ",");
3698
3699 /* type */
3700 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3701
3702 /* rate */
3703 chunk_appendf(&trash, "%u,,%u,",
3704 read_freq_ctr(&sv->sess_per_sec),
3705 sv->counters.sps_max);
3706
Willy Tarreauff5ae352013-12-11 20:36:34 +01003707 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003708 /* check_status */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003709 chunk_appendf(&trash, "%s,", csv_enc(get_check_status_info(sv->check.status), 1, out));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003710
3711 /* check_code */
3712 if (sv->check.status >= HCHK_STATUS_L57DATA)
3713 chunk_appendf(&trash, "%u,", sv->check.code);
3714 else
3715 chunk_appendf(&trash, ",");
3716
3717 /* check_duration */
3718 if (sv->check.status >= HCHK_STATUS_CHECKED)
3719 chunk_appendf(&trash, "%lu,", sv->check.duration);
3720 else
3721 chunk_appendf(&trash, ",");
3722
3723 }
3724 else
3725 chunk_appendf(&trash, ",,,");
3726
3727 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3728 if (px->mode == PR_MODE_HTTP) {
3729 for (i=1; i<6; i++)
3730 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3731
3732 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3733 }
3734 else
3735 chunk_appendf(&trash, ",,,,,,");
3736
3737 /* failed health analyses */
3738 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3739
3740 /* requests : req_rate, req_rate_max, req_tot, */
3741 chunk_appendf(&trash, ",,,");
3742
3743 /* errors: cli_aborts, srv_aborts */
3744 chunk_appendf(&trash, "%lld,%lld,",
3745 sv->counters.cli_aborts, sv->counters.srv_aborts);
3746
3747 /* compression: in, out, bypassed, comp_rsp */
3748 chunk_appendf(&trash, ",,,,");
3749
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003750 /* lastsess */
3751 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3752
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003753 /* capture of last check and agent statuses */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003754 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->check.desc), 1, out) : "");
3755 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 +02003756
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003757 /* qtime, ctime, rtime, ttime, */
3758 chunk_appendf(&trash, "%u,%u,%u,%u,",
3759 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3760 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3761 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3762 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3763
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003764 /* finish with EOL */
3765 chunk_appendf(&trash, "\n");
3766 }
3767 return 1;
3768}
3769
3770/* Dumps a line for backend <px> to the trash for and uses the state from stream
3771 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3772 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3773 */
3774static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3775{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003776 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003777 struct chunk src;
3778 int i;
3779
3780 if (!(px->cap & PR_CAP_BE))
3781 return 0;
3782
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003783 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003784 return 0;
3785
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003786 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003787 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003788 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003789 /* Column sub-heading for Enable or Disable server */
3790 chunk_appendf(&trash, "<td></td>");
3791 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003792 chunk_appendf(&trash,
3793 "<td class=ac>"
3794 /* name */
3795 "%s<a name=\"%s/Backend\"></a>"
3796 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3797 "",
3798 (flags & ST_SHLGNDS)?"<u>":"",
3799 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003800
3801 if (flags & ST_SHLGNDS) {
3802 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003803 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003804 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3805
3806 /* cookie */
3807 if (px->cookie_name) {
3808 chunk_appendf(&trash, ", cookie: '");
3809 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3810 chunk_htmlencode(&trash, &src);
3811 chunk_appendf(&trash, "'");
3812 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003813 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003814 }
3815
3816 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003817 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003818 /* queue : current, max */
3819 "<td>%s</td><td>%s</td><td></td>"
3820 /* sessions rate : current, max, limit */
3821 "<td>%s</td><td>%s</td><td></td>"
3822 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003823 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003824 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3825 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003826
3827 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003828 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003829 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003830 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003831 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003832 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003833 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3834 U2H(px->be_counters.cum_conn),
3835 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003836
Willy Tarreau466c9b52012-12-23 02:25:03 +01003837 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003838 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003839 chunk_appendf(&trash,
3840 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3841 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3842 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3843 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3844 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3845 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3846 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3847 "<tr><th>- other responses:</th><td>%s</td></tr>"
3848 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003849 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003850 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003851 U2H(px->be_counters.p.http.cum_req),
3852 U2H(px->be_counters.p.http.rsp[1]),
3853 U2H(px->be_counters.p.http.rsp[2]),
3854 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003855 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003856 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003857 U2H(px->be_counters.p.http.rsp[3]),
3858 U2H(px->be_counters.p.http.rsp[4]),
3859 U2H(px->be_counters.p.http.rsp[5]),
3860 U2H(px->be_counters.p.http.rsp[0]),
3861 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003862 }
3863
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003864 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)));
3865 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)));
3866 if (px->mode == PR_MODE_HTTP)
3867 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)));
3868 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)));
3869
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003870 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003871 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003872 /* sessions: lbtot, last */
3873 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003874 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003875 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003876 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003877 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003878 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003879 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003880
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003881 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003882 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003883 "<td>%s%s<div class=tips><table class=det>"
3884 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3885 "<tr><th>Compression in:</th><td>%s</td></tr>"
3886 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3887 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3888 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3889 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003890 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003891 U2H(px->be_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003892 U2H(px->be_counters.bytes_out),
3893 U2H(px->be_counters.comp_in),
3894 U2H(px->be_counters.comp_out),
3895 px->be_counters.comp_in ? (int)(px->be_counters.comp_out * 100 / px->be_counters.comp_in) : 0,
3896 U2H(px->be_counters.comp_byp),
3897 U2H(px->be_counters.comp_in - px->be_counters.comp_out),
3898 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 +01003899 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3900
3901 chunk_appendf(&trash,
3902 /* denied: req, resp */
3903 "<td>%s</td><td>%s</td>"
3904 /* errors : request, connect */
3905 "<td></td><td>%s</td>"
3906 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003907 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003908 /* warnings: retries, redispatches */
3909 "<td>%lld</td><td>%lld</td>"
3910 /* backend status: reflect backend status (up/down): we display UP
3911 * if the backend has known working servers or if it has no server at
3912 * all (eg: for stats). Then we display the total weight, number of
3913 * active and backups. */
3914 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3915 "<td class=ac>%d</td><td class=ac>%d</td>"
3916 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003917 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3918 U2H(px->be_counters.failed_conns),
3919 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003920 px->be_counters.cli_aborts,
3921 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003922 px->be_counters.retries, px->be_counters.redispatches,
3923 human_time(now.tv_sec - px->last_change, 1),
3924 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3925 "<font color=\"red\"><b>DOWN</b></font>",
3926 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3927 px->srv_act, px->srv_bck);
3928
3929 chunk_appendf(&trash,
3930 /* rest of backend: nothing, down transitions, total downtime, throttle */
3931 "<td class=ac>&nbsp;</td><td>%d</td>"
3932 "<td>%s</td>"
3933 "<td></td>"
3934 "</tr>",
3935 px->down_trans,
3936 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3937 }
3938 else { /* CSV mode */
3939 chunk_appendf(&trash,
3940 /* pxid, name */
3941 "%s,BACKEND,"
3942 /* queue : current, max */
3943 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003944 /* sessions : current, max, limit, total */
3945 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003946 /* bytes : in, out */
3947 "%lld,%lld,"
3948 /* denied: req, resp */
3949 "%lld,%lld,"
3950 /* errors : request, connect, response */
3951 ",%lld,%lld,"
3952 /* warnings: retries, redispatches */
3953 "%lld,%lld,"
3954 /* backend status: reflect backend status (up/down): we display UP
3955 * if the backend has known working servers or if it has no server at
3956 * all (eg: for stats). Then we display the total weight, number of
3957 * active and backups. */
3958 "%s,"
3959 "%d,%d,%d,"
3960 /* rest of backend: nothing, down transitions, last change, total downtime */
3961 ",%d,%d,%d,,"
3962 /* pid, iid, sid, throttle, lbtot, tracked, type */
3963 "%d,%d,0,,%lld,,%d,"
3964 /* rate, rate_lim, rate_max, */
3965 "%u,,%u,"
3966 /* check_status, check_code, check_duration */
3967 ",,,",
3968 px->id,
3969 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3970 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3971 px->be_counters.bytes_in, px->be_counters.bytes_out,
3972 px->be_counters.denied_req, px->be_counters.denied_resp,
3973 px->be_counters.failed_conns, px->be_counters.failed_resp,
3974 px->be_counters.retries, px->be_counters.redispatches,
3975 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3976 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3977 px->srv_act, px->srv_bck,
3978 px->down_trans, (int)(now.tv_sec - px->last_change),
3979 px->srv?be_downtime(px):0,
3980 relative_pid, px->uuid,
3981 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3982 read_freq_ctr(&px->be_sess_per_sec),
3983 px->be_counters.sps_max);
3984
3985 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3986 if (px->mode == PR_MODE_HTTP) {
3987 for (i=1; i<6; i++)
3988 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3989 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3990 }
3991 else
3992 chunk_appendf(&trash, ",,,,,,");
3993
3994 /* failed health analyses */
3995 chunk_appendf(&trash, ",");
3996
3997 /* requests : req_rate, req_rate_max, req_tot, */
3998 chunk_appendf(&trash, ",,,");
3999
4000 /* errors: cli_aborts, srv_aborts */
4001 chunk_appendf(&trash, "%lld,%lld,",
4002 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
4003
4004 /* compression: in, out, bypassed */
4005 chunk_appendf(&trash, "%lld,%lld,%lld,",
4006 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
4007
4008 /* compression: comp_rsp */
4009 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
4010
Willy Tarreaua28df3e2014-06-16 16:40:14 +02004011 /* lastsess, last_chk, last_agt, */
4012 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01004013
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004014 /* qtime, ctime, rtime, ttime, */
4015 chunk_appendf(&trash, "%u,%u,%u,%u,",
4016 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
4017 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
4018 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
4019 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4020
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004021 /* finish with EOL */
4022 chunk_appendf(&trash, "\n");
4023 }
4024 return 1;
4025}
4026
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004027/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004028 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004029 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004030 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004031static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004032{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004033 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004034 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4035
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004036 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004037 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004038
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004039 /* 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 +02004040 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004041 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004042 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004043 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 +01004044 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004045 }
4046
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004047 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004048 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004049 }
4050
4051 /* print a new table */
4052 chunk_appendf(&trash,
4053 "<table class=\"tbl\" width=\"100%%\">\n"
4054 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004055 "<th class=\"pxname\" width=\"10%%\">");
4056
4057 chunk_appendf(&trash,
4058 "<a name=\"%s\"></a>%s"
4059 "<a class=px href=\"#%s\">%s</a>",
4060 px->id,
4061 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4062 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004063
4064 if (uri->flags & ST_SHLGNDS) {
4065 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004066 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004067 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4068 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004069 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004070 }
4071
4072 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004073 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004074 "<th class=\"%s\" width=\"90%%\">%s</th>"
4075 "</tr>\n"
4076 "</table>\n"
4077 "<table class=\"tbl\" width=\"100%%\">\n"
4078 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004079 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4080 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4081
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004082 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004083 /* Column heading for Enable or Disable server */
4084 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004085 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004086
4087 chunk_appendf(&trash,
4088 "<th rowspan=2></th>"
4089 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004090 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004091 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4092 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4093 "<th colspan=9>Server</th>"
4094 "</tr>\n"
4095 "<tr class=\"titre\">"
4096 "<th>Cur</th><th>Max</th><th>Limit</th>"
4097 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004098 "<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 +01004099 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4100 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4101 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4102 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4103 "<th>Thrtle</th>\n"
4104 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004105}
4106
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004107/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004108 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004109 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004110static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004111{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004112 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004113 chunk_appendf(&trash, "</table>");
4114
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004115 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004116 /* close the form used to enable/disable this proxy servers */
4117 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004118 "Choose the action to perform on the checked servers : "
4119 "<select name=action>"
4120 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004121 "<option value=\"ready\">Set state to READY</option>"
4122 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004123 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004124 "<option value=\"dhlth\">Health: disable checks</option>"
4125 "<option value=\"ehlth\">Health: enable checks</option>"
4126 "<option value=\"hrunn\">Health: force UP</option>"
4127 "<option value=\"hnolb\">Health: force NOLB</option>"
4128 "<option value=\"hdown\">Health: force DOWN</option>"
4129 "<option value=\"dagent\">Agent: disable checks</option>"
4130 "<option value=\"eagent\">Agent: enable checks</option>"
4131 "<option value=\"arunn\">Agent: force UP</option>"
4132 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004133 "<option value=\"shutdown\">Kill Sessions</option>"
4134 "</select>"
4135 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4136 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4137 "</form>",
4138 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004139 }
4140
4141 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004142}
Willy Tarreau91861262007-10-17 17:06:05 +02004143
4144/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004145 * Dumps statistics for a proxy. The output is sent to the stream interface's
4146 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4147 * buffer space, or non-zero if everything completed. This function is used
4148 * both by the CLI and the HTTP entry points, and is able to dump the output
4149 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004150 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004151static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004152{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004153 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004154 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004155 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004156 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004157 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02004158
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004159 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004160
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004161 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004162 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004163 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004164 if (uri && uri->scope) {
4165 /* we have a limited scope, we have to check the proxy name */
4166 struct stat_scope *scope;
4167 int len;
4168
4169 len = strlen(px->id);
4170 scope = uri->scope;
4171
4172 while (scope) {
4173 /* match exact proxy name */
4174 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4175 break;
4176
4177 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004178 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004179 break;
4180 scope = scope->next;
4181 }
4182
4183 /* proxy name not found : don't dump anything */
4184 if (scope == NULL)
4185 return 1;
4186 }
4187
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004188 /* if the user has requested a limited output and the proxy
4189 * name does not match, skip it.
4190 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004191 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004192 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 +02004193 return 1;
4194
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004195 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4196 (appctx->ctx.stats.iid != -1) &&
4197 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004198 return 1;
4199
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004200 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004201 /* fall through */
4202
Willy Tarreau295a8372011-03-10 11:25:07 +01004203 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004204 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004205 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004206 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004207 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004208 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004209 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004210 }
Willy Tarreau91861262007-10-17 17:06:05 +02004211
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004212 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004213 /* fall through */
4214
Willy Tarreau295a8372011-03-10 11:25:07 +01004215 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004216 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004217 if (stats_dump_fe_stats(si, px)) {
4218 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004219 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004220 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004221 }
4222 }
Willy Tarreau91861262007-10-17 17:06:05 +02004223
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004224 appctx->ctx.stats.l = px->conf.listeners.n;
4225 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004226 /* fall through */
4227
Willy Tarreau295a8372011-03-10 11:25:07 +01004228 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004229 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004230 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004231 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004232 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004233 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004234 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004235
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004236 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004237 if (!l->counters)
4238 continue;
4239
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004240 if (appctx->ctx.stats.flags & STAT_BOUND) {
4241 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004242 break;
4243
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004244 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004245 continue;
4246 }
4247
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004248 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004249 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
4250 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004251 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004252 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004253 }
4254 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004255 }
4256
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004257 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4258 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004259 /* fall through */
4260
Willy Tarreau295a8372011-03-10 11:25:07 +01004261 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004262 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004263 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004264 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09004265 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02004266
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004267 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004268 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004269 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004270 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004271
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004272 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004273
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004274 if (appctx->ctx.stats.flags & STAT_BOUND) {
4275 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004276 break;
4277
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004278 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004279 continue;
4280 }
4281
Willy Tarreau32091232014-05-16 13:52:00 +02004282 svs = sv;
4283 while (svs->track)
4284 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004285
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004286 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004287 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09004288 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004289 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004290 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4291 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004292 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004293 sv_colour = SRV_STATS_COLOUR_UP;
4294 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004295
Simon Horman4463d192015-04-23 14:51:28 +09004296 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
4297 sv_colour = SRV_STATS_COLOUR_DRAINING;
4298
4299 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09004300 if (svs->agent.state & CHK_ST_ENABLED)
4301 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
4302 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09004303 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09004304 else
Simon Horman4d2eab62015-04-23 14:51:26 +09004305 sv_state = SRV_STATS_STATE_DRAIN;
4306 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004307
Simon Horman837bfa72015-04-23 14:51:27 +09004308 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004309 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09004310 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
4311 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004312 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004313 else if (sv->state == SRV_ST_STOPPING) {
4314 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09004315 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004316 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09004317 sv_colour = SRV_STATS_COLOUR_NOLB;
4318 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004319 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004320 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4321 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004322 }
4323 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09004324 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004325 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09004326 sv_colour = SRV_STATS_COLOUR_DOWN;
4327 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004328 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09004329 sv_colour = SRV_STATS_COLOUR_DOWN;
4330 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004331 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004332 sv_colour = SRV_STATS_COLOUR_GOING_UP;
4333 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004334 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09004335 sv_colour = SRV_STATS_COLOUR_DOWN;
4336 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004337 }
Willy Tarreau91861262007-10-17 17:06:05 +02004338
Willy Tarreau9638efa2014-05-23 11:19:57 +02004339 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02004340 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004341 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02004342 continue;
4343 }
4344
Simon Horman837bfa72015-04-23 14:51:27 +09004345 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004346 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004347 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004348 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004349 }
4350 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004351 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004352
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004353 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004354 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004355
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004356 case STAT_PX_ST_BE:
4357 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004358 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
4359 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004360 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004361 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004362 }
4363 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004364
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004365 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004366 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004367
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004368 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004369 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004370 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004371 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004372 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004373 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004374 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004375 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004376
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004377 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004378 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004379
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004380 case STAT_PX_ST_FIN:
4381 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004382
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004383 default:
4384 /* unknown state, we should put an abort() here ! */
4385 return 1;
4386 }
4387}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004388
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004389/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4390 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004391 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004392static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004393{
4394 /* WARNING! This must fit in the first buffer !!! */
4395 chunk_appendf(&trash,
4396 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4397 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4398 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4399 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4400 "<style type=\"text/css\"><!--\n"
4401 "body {"
4402 " font-family: arial, helvetica, sans-serif;"
4403 " font-size: 12px;"
4404 " font-weight: normal;"
4405 " color: black;"
4406 " background: white;"
4407 "}\n"
4408 "th,td {"
4409 " font-size: 10px;"
4410 "}\n"
4411 "h1 {"
4412 " font-size: x-large;"
4413 " margin-bottom: 0.5em;"
4414 "}\n"
4415 "h2 {"
4416 " font-family: helvetica, arial;"
4417 " font-size: x-large;"
4418 " font-weight: bold;"
4419 " font-style: italic;"
4420 " color: #6020a0;"
4421 " margin-top: 0em;"
4422 " margin-bottom: 0em;"
4423 "}\n"
4424 "h3 {"
4425 " font-family: helvetica, arial;"
4426 " font-size: 16px;"
4427 " font-weight: bold;"
4428 " color: #b00040;"
4429 " background: #e8e8d0;"
4430 " margin-top: 0em;"
4431 " margin-bottom: 0em;"
4432 "}\n"
4433 "li {"
4434 " margin-top: 0.25em;"
4435 " margin-right: 2em;"
4436 "}\n"
4437 ".hr {margin-top: 0.25em;"
4438 " border-color: black;"
4439 " border-bottom-style: solid;"
4440 "}\n"
4441 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4442 ".total {background: #20D0D0;color: #ffff80;}\n"
4443 ".frontend {background: #e8e8d0;}\n"
4444 ".socket {background: #d0d0d0;}\n"
4445 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004446 ".active_down {background: #ff9090;}\n"
4447 ".active_going_up {background: #ffd020;}\n"
4448 ".active_going_down {background: #ffffa0;}\n"
4449 ".active_up {background: #c0ffc0;}\n"
4450 ".active_nolb {background: #20a0ff;}\n"
4451 ".active_draining {background: #20a0FF;}\n"
4452 ".active_no_check {background: #e0e0e0;}\n"
4453 ".backup_down {background: #ff9090;}\n"
4454 ".backup_going_up {background: #ff80ff;}\n"
4455 ".backup_going_down {background: #c060ff;}\n"
4456 ".backup_up {background: #b0d0ff;}\n"
4457 ".backup_nolb {background: #90b0e0;}\n"
4458 ".backup_draining {background: #cc9900;}\n"
4459 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004460 ".maintain {background: #c07820;}\n"
4461 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4462 "\n"
4463 "a.px:link {color: #ffff40; text-decoration: none;}"
4464 "a.px:visited {color: #ffff40; text-decoration: none;}"
4465 "a.px:hover {color: #ffffff; text-decoration: none;}"
4466 "a.lfsb:link {color: #000000; text-decoration: none;}"
4467 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4468 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4469 "\n"
4470 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4471 "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"
4472 "table.tbl td.ac { text-align: center;}\n"
4473 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4474 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4475 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4476 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4477 "\n"
4478 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4479 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4480 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004481 "table.det { border-collapse: collapse; border-style: none; }\n"
4482 "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 +01004483 "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 +01004484 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004485 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004486 " display:block;\n"
4487 " visibility:hidden;\n"
4488 " z-index:2147483647;\n"
4489 " position:absolute;\n"
4490 " padding:2px 4px 3px;\n"
4491 " background:#f0f060; color:#000000;\n"
4492 " border:1px solid #7040c0;\n"
4493 " white-space:nowrap;\n"
4494 " font-style:normal;font-size:11px;font-weight:normal;\n"
4495 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4496 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4497 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004498 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004499 "-->\n"
4500 "</style></head>\n",
4501 (uri->flags & ST_SHNODE) ? " on " : "",
4502 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4503 );
4504}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004505
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004506/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004507 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004508 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004509 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004510static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004511{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004512 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004513 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004514 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004515
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004516 /* WARNING! this has to fit the first packet too.
4517 * We are around 3.5 kB, add adding entries will
4518 * become tricky if we want to support 4kB buffers !
4519 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004520 chunk_appendf(&trash,
4521 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4522 PRODUCT_NAME "%s</a></h1>\n"
4523 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4524 "<hr width=\"100%%\" class=\"hr\">\n"
4525 "<h3>&gt; General process information</h3>\n"
4526 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4527 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4528 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4529 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4530 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4531 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4532 "Running tasks: %d/%d; idle = %d %%<br>\n"
4533 "</td><td align=\"center\" nowrap>\n"
4534 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004535 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4536 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004537 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004538 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4539 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004540 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004541 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4542 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004543 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004544 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4545 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004546 "</tr><tr>\n"
4547 "<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 -07004548 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004549 "<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 +01004550 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004551 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004552 "</td>"
4553 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4554 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4555 "",
4556 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4557 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4558 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4559 (uri->flags & ST_SHDESC) ? ": " : "",
4560 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4561 pid, relative_pid, global.nbproc,
4562 up / 86400, (up % 86400) / 3600,
4563 (up % 3600) / 60, (up % 60),
4564 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4565 global.rlimit_memmax ? " MB" : "",
4566 global.rlimit_nofile,
4567 global.maxsock, global.maxconn, global.maxpipes,
4568 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4569 run_queue_cur, nb_tasks_cur, idle_pct
4570 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004571
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004572 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004573 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 +01004574 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004575
4576 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004577 "<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 +01004578 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004579 STAT_SCOPE_TXT_MAXLEN);
4580
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004581 /* 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 +02004582 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004583 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004584 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004585 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 +01004586 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004587 }
4588
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004589 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004590 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004591 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004592 uri->uri_prefix,
4593 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004594 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004595 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004596 else
4597 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004598 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004599 uri->uri_prefix,
4600 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004601 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004602 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004603
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004604 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004605 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004606 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004607 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004608 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004609 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004610 "",
4611 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004612 else
4613 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004614 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004615 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004616 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004617 ";norefresh",
4618 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004619 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004620
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004621 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004622 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004623 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004624 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4625 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004626 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004627
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004628 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004629 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004630 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004631 (uri->refresh > 0) ? ";norefresh" : "",
4632 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004633
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004634 chunk_appendf(&trash,
4635 "</ul></td>"
4636 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4637 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4638 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4639 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4640 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4641 "</ul>"
4642 "</td>"
4643 "</tr></table>\n"
4644 ""
4645 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004646
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004647 if (appctx->ctx.stats.st_code) {
4648 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004649 case STAT_STATUS_DONE:
4650 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004651 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004652 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004653 "Action processed successfully."
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_NONE:
4660 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004661 "<p><div class=active_going_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 "Nothing has changed."
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 case STAT_STATUS_PART:
4670 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004671 "<p><div class=active_going_down>"
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 "Action partially processed.<br>"
4674 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004675 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004676 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4677 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004678 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004679 break;
4680 case STAT_STATUS_ERRP:
4681 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004682 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004683 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004684 "Action not processed because of invalid parameters."
4685 "<ul>"
4686 "<li>The action is maybe unknown.</li>"
4687 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4688 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4689 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004690 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004691 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4692 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004693 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004694 break;
4695 case STAT_STATUS_EXCD:
4696 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004697 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004698 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004699 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4700 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004701 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004702 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4703 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004704 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004705 break;
4706 case STAT_STATUS_DENY:
4707 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004708 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004709 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004710 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004711 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004712 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4713 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004714 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004715 break;
4716 default:
4717 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004718 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004719 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004720 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004721 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004722 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4723 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004724 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004725 }
4726 chunk_appendf(&trash, "<p>\n");
4727 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004728}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004729
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004730/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4731 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004732 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004733static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004734{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004735 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004736}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004737
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004738/* This function dumps statistics onto the stream interface's read buffer in
4739 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004740 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4741 * 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 +02004742 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004743 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004744 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004745static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004746{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004747 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004748 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004749 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004750
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004751 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004752
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004753 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004754 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004755 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004756 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004757
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004758 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004759 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004760 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004761 else
4762 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004763
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 Tarreauae526782010-03-04 20:34:23 +01004768
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004769 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004770 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004771
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004772 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004773 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004774 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004775 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004776 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004777 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004778 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004779 }
Willy Tarreau91861262007-10-17 17:06:05 +02004780
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004781 appctx->ctx.stats.px = proxy;
4782 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4783 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004784 /* fall through */
4785
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004786 case STAT_ST_LIST:
4787 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004788 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004789 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004790 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004791 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004792 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004793
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004794 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004795 /* skip the disabled proxies, global frontend and non-networked ones */
4796 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004797 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004798 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004799
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004800 appctx->ctx.stats.px = px->next;
4801 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004802 }
4803 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004804
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004805 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004806 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004807
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004808 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004809 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004810 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01004811 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004812 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004813 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004814 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004815 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004816
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004817 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004818 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004819
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004820 case STAT_ST_FIN:
4821 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004822
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004823 default:
4824 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004825 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004826 return -1;
4827 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004828}
Willy Tarreauae526782010-03-04 20:34:23 +01004829
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004830/* We reached the stats page through a POST request. The appctx is
4831 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004832 * Parse the posted data and enable/disable servers if necessary.
4833 * Returns 1 if request was parsed or zero if it needs more data.
4834 */
4835static int stats_process_http_post(struct stream_interface *si)
4836{
Willy Tarreau87b09662015-04-03 00:22:06 +02004837 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004838 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004839
4840 struct proxy *px = NULL;
4841 struct server *sv = NULL;
4842
4843 char key[LINESIZE];
4844 int action = ST_ADM_ACTION_NONE;
4845 int reprocess = 0;
4846
4847 int total_servers = 0;
4848 int altered_servers = 0;
4849
4850 char *first_param, *cur_param, *next_param, *end_params;
4851 char *st_cur_param = NULL;
4852 char *st_next_param = NULL;
4853
4854 struct chunk *temp;
4855 int reql;
4856
4857 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02004858 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004859 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004860 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004861 goto out;
4862 }
4863
Willy Tarreaueee5b512015-04-03 23:46:31 +02004864 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 +01004865 if (reql <= 0) {
4866 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004867 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004868 return 0;
4869 }
4870
4871 first_param = temp->str;
4872 end_params = temp->str + reql;
4873 cur_param = next_param = end_params;
4874 *end_params = '\0';
4875
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004876 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004877
4878 /*
4879 * Parse the parameters in reverse order to only store the last value.
4880 * From the html form, the backend and the action are at the end.
4881 */
4882 while (cur_param > first_param) {
4883 char *value;
4884 int poffset, plen;
4885
4886 cur_param--;
4887
4888 if ((*cur_param == '&') || (cur_param == first_param)) {
4889 reprocess_servers:
4890 /* Parse the key */
4891 poffset = (cur_param != first_param ? 1 : 0);
4892 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4893 if ((plen > 0) && (plen <= sizeof(key))) {
4894 strncpy(key, cur_param + poffset, plen);
4895 key[plen - 1] = '\0';
4896 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004897 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004898 goto out;
4899 }
4900
4901 /* Parse the value */
4902 value = key;
4903 while (*value != '\0' && *value != '=') {
4904 value++;
4905 }
4906 if (*value == '=') {
4907 /* Ok, a value is found, we can mark the end of the key */
4908 *value++ = '\0';
4909 }
4910 if (url_decode(key) < 0 || url_decode(value) < 0)
4911 break;
4912
4913 /* Now we can check the key to see what to do */
4914 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02004915 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004916 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004917 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004918 goto out;
4919 }
4920 }
4921 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004922 if (strcmp(value, "ready") == 0) {
4923 action = ST_ADM_ACTION_READY;
4924 }
4925 else if (strcmp(value, "drain") == 0) {
4926 action = ST_ADM_ACTION_DRAIN;
4927 }
4928 else if (strcmp(value, "maint") == 0) {
4929 action = ST_ADM_ACTION_MAINT;
4930 }
4931 else if (strcmp(value, "shutdown") == 0) {
4932 action = ST_ADM_ACTION_SHUTDOWN;
4933 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004934 else if (strcmp(value, "dhlth") == 0) {
4935 action = ST_ADM_ACTION_DHLTH;
4936 }
4937 else if (strcmp(value, "ehlth") == 0) {
4938 action = ST_ADM_ACTION_EHLTH;
4939 }
4940 else if (strcmp(value, "hrunn") == 0) {
4941 action = ST_ADM_ACTION_HRUNN;
4942 }
4943 else if (strcmp(value, "hnolb") == 0) {
4944 action = ST_ADM_ACTION_HNOLB;
4945 }
4946 else if (strcmp(value, "hdown") == 0) {
4947 action = ST_ADM_ACTION_HDOWN;
4948 }
4949 else if (strcmp(value, "dagent") == 0) {
4950 action = ST_ADM_ACTION_DAGENT;
4951 }
4952 else if (strcmp(value, "eagent") == 0) {
4953 action = ST_ADM_ACTION_EAGENT;
4954 }
4955 else if (strcmp(value, "arunn") == 0) {
4956 action = ST_ADM_ACTION_ARUNN;
4957 }
4958 else if (strcmp(value, "adown") == 0) {
4959 action = ST_ADM_ACTION_ADOWN;
4960 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004961 /* else these are the old supported methods */
4962 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004963 action = ST_ADM_ACTION_DISABLE;
4964 }
4965 else if (strcmp(value, "enable") == 0) {
4966 action = ST_ADM_ACTION_ENABLE;
4967 }
4968 else if (strcmp(value, "stop") == 0) {
4969 action = ST_ADM_ACTION_STOP;
4970 }
4971 else if (strcmp(value, "start") == 0) {
4972 action = ST_ADM_ACTION_START;
4973 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004974 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004975 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004976 goto out;
4977 }
4978 }
4979 else if (strcmp(key, "s") == 0) {
4980 if (!(px && action)) {
4981 /*
4982 * Indicates that we'll need to reprocess the parameters
4983 * as soon as backend and action are known
4984 */
4985 if (!reprocess) {
4986 st_cur_param = cur_param;
4987 st_next_param = next_param;
4988 }
4989 reprocess = 1;
4990 }
4991 else if ((sv = findserver(px, value)) != NULL) {
4992 switch (action) {
4993 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004994 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004995 altered_servers++;
4996 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004997 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004998 }
4999 break;
5000 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005001 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005002 altered_servers++;
5003 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005004 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005005 }
5006 break;
5007 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005008 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5009 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5010 altered_servers++;
5011 total_servers++;
5012 }
5013 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005014 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005015 if (sv->admin & SRV_ADMF_FDRAIN) {
5016 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5017 altered_servers++;
5018 total_servers++;
5019 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005020 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005021 case ST_ADM_ACTION_DHLTH:
5022 if (sv->check.state & CHK_ST_CONFIGURED) {
5023 sv->check.state &= ~CHK_ST_ENABLED;
5024 altered_servers++;
5025 total_servers++;
5026 }
5027 break;
5028 case ST_ADM_ACTION_EHLTH:
5029 if (sv->check.state & CHK_ST_CONFIGURED) {
5030 sv->check.state |= CHK_ST_ENABLED;
5031 altered_servers++;
5032 total_servers++;
5033 }
5034 break;
5035 case ST_ADM_ACTION_HRUNN:
5036 if (!(sv->track)) {
5037 sv->check.health = sv->check.rise + sv->check.fall - 1;
5038 srv_set_running(sv, "changed from Web interface");
5039 altered_servers++;
5040 total_servers++;
5041 }
5042 break;
5043 case ST_ADM_ACTION_HNOLB:
5044 if (!(sv->track)) {
5045 sv->check.health = sv->check.rise + sv->check.fall - 1;
5046 srv_set_stopping(sv, "changed from Web interface");
5047 altered_servers++;
5048 total_servers++;
5049 }
5050 break;
5051 case ST_ADM_ACTION_HDOWN:
5052 if (!(sv->track)) {
5053 sv->check.health = 0;
5054 srv_set_stopped(sv, "changed from Web interface");
5055 altered_servers++;
5056 total_servers++;
5057 }
5058 break;
5059 case ST_ADM_ACTION_DAGENT:
5060 if (sv->agent.state & CHK_ST_CONFIGURED) {
5061 sv->agent.state &= ~CHK_ST_ENABLED;
5062 altered_servers++;
5063 total_servers++;
5064 }
5065 break;
5066 case ST_ADM_ACTION_EAGENT:
5067 if (sv->agent.state & CHK_ST_CONFIGURED) {
5068 sv->agent.state |= CHK_ST_ENABLED;
5069 altered_servers++;
5070 total_servers++;
5071 }
5072 break;
5073 case ST_ADM_ACTION_ARUNN:
5074 if (sv->agent.state & CHK_ST_ENABLED) {
5075 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5076 srv_set_running(sv, "changed from Web interface");
5077 altered_servers++;
5078 total_servers++;
5079 }
5080 break;
5081 case ST_ADM_ACTION_ADOWN:
5082 if (sv->agent.state & CHK_ST_ENABLED) {
5083 sv->agent.health = 0;
5084 srv_set_stopped(sv, "changed from Web interface");
5085 altered_servers++;
5086 total_servers++;
5087 }
5088 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005089 case ST_ADM_ACTION_READY:
5090 srv_adm_set_ready(sv);
5091 altered_servers++;
5092 total_servers++;
5093 break;
5094 case ST_ADM_ACTION_DRAIN:
5095 srv_adm_set_drain(sv);
5096 altered_servers++;
5097 total_servers++;
5098 break;
5099 case ST_ADM_ACTION_MAINT:
5100 srv_adm_set_maint(sv);
5101 altered_servers++;
5102 total_servers++;
5103 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005104 case ST_ADM_ACTION_SHUTDOWN:
5105 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005106 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005107
5108 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5109 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005110 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005111
5112 altered_servers++;
5113 total_servers++;
5114 }
5115 break;
5116 }
5117 } else {
5118 /* the server name is unknown or ambiguous (duplicate names) */
5119 total_servers++;
5120 }
5121 }
5122 if (reprocess && px && action) {
5123 /* Now, we know the backend and the action chosen by the user.
5124 * We can safely restart from the first server parameter
5125 * to reprocess them
5126 */
5127 cur_param = st_cur_param;
5128 next_param = st_next_param;
5129 reprocess = 0;
5130 goto reprocess_servers;
5131 }
5132
5133 next_param = cur_param;
5134 }
5135 }
5136
5137 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005138 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005139 }
5140 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005141 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005142 }
5143 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005144 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005145 }
5146 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005147 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005148 }
5149 out:
5150 return 1;
5151}
5152
5153
5154static int stats_send_http_headers(struct stream_interface *si)
5155{
Willy Tarreau87b09662015-04-03 00:22:06 +02005156 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005157 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005158 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005159
5160 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005161 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005162 "Cache-Control: no-cache\r\n"
5163 "Connection: close\r\n"
5164 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005165 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005166
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005167 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005168 chunk_appendf(&trash, "Refresh: %d\r\n",
5169 uri->refresh);
5170
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005171 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5172
5173 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5174 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5175 else
5176 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005177
Willy Tarreaueee5b512015-04-03 23:46:31 +02005178 s->txn->status = 200;
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
5189static int stats_send_http_redirect(struct stream_interface *si)
5190{
5191 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005192 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005193 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005194 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005195
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005196 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005197 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005198 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005199 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005200 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 +01005201 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005202 }
5203
5204 /* We don't want to land on the posted stats page because a refresh will
5205 * repost the data. We don't want this to happen on accident so we redirect
5206 * the browse to the stats page with a GET.
5207 */
5208 chunk_printf(&trash,
5209 "HTTP/1.1 303 See Other\r\n"
5210 "Cache-Control: no-cache\r\n"
5211 "Content-Type: text/plain\r\n"
5212 "Connection: close\r\n"
5213 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005214 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005215 "\r\n",
5216 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005217 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5218 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5219 stat_status_codes[appctx->ctx.stats.st_code]) ?
5220 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005221 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005222 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5223 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005224 scope_txt);
5225
Willy Tarreaueee5b512015-04-03 23:46:31 +02005226 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005227 s->logs.tv_request = now;
5228
Willy Tarreaubc18da12015-03-13 14:00:47 +01005229 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005230 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005231 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005232 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005233
5234 return 1;
5235}
5236
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005237/* This I/O handler runs as an applet embedded in a stream interface. It is
5238 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005239 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005240 * automatically unregisters itself once transfer is complete.
5241 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005242static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005243{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005244 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005245 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005246 struct channel *req = si_oc(si);
5247 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005248
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005249 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5250 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005251
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005252 /* check that the output is not closed */
5253 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005254 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005255
Willy Tarreau347a35d2013-11-22 17:51:09 +01005256 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005257 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005258 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005259 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005260 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005261 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005262 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005263 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005264 }
5265
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005266 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005267 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005268 unsigned int data_len;
5269 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005270 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005271
5272 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5273 /* One difficulty we're facing is that we must prevent
5274 * the input data from being automatically forwarded to
5275 * the output area. For this, we temporarily disable
5276 * forwarding on the channel.
5277 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005278 last_fwd = si_ic(si)->to_forward;
5279 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005280 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005281 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005282 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005283 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005284 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005285 }
5286 }
5287
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005288 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005289 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005290 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005291
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005292 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005293
5294 /* Now we must either adjust or remove the chunk size. This is
5295 * not easy because the chunk size might wrap at the end of the
5296 * buffer, so we pretend we have nothing in the buffer, we write
5297 * the size, then restore the buffer's contents. Note that we can
5298 * only do that because no forwarding is scheduled on the stats
5299 * applet.
5300 */
5301 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005302 si_ic(si)->total -= (last_len - prev_len);
5303 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005304
5305 if (last_len != data_len) {
5306 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005307 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005308 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005309
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005310 si_ic(si)->total += (last_len - data_len);
5311 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005312 }
5313 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005314 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005315 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005316 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005317
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005318 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005319 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005320 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005321 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005322 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005323 }
5324
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005325 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005326 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005327 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005328 }
5329
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005330 if (appctx->st0 == STAT_HTTP_DONE) {
5331 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5332 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005333 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005334 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005335 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005336 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005337 }
5338 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005339 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005340 res->flags |= CF_READ_NULL;
5341 si_shutr(si);
5342 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005343
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005344 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5345 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005346
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005347 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005348 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5349 si_shutr(si);
5350 res->flags |= CF_READ_NULL;
5351 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005352 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005353 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005354 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005355}
5356
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005357
Willy Tarreau909d5172012-11-26 03:04:41 +01005358static inline const char *get_conn_ctrl_name(const struct connection *conn)
5359{
Willy Tarreau3c728722014-01-23 13:50:42 +01005360 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005361 return "NONE";
5362 return conn->ctrl->name;
5363}
5364
5365static inline const char *get_conn_xprt_name(const struct connection *conn)
5366{
5367 static char ptr[17];
5368
Willy Tarreauaad69382014-01-23 14:21:42 +01005369 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005370 return "NONE";
5371
5372 if (conn->xprt == &raw_sock)
5373 return "RAW";
5374
5375#ifdef USE_OPENSSL
5376 if (conn->xprt == &ssl_sock)
5377 return "SSL";
5378#endif
5379 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5380 return ptr;
5381}
5382
5383static inline const char *get_conn_data_name(const struct connection *conn)
5384{
5385 static char ptr[17];
5386
5387 if (!conn->data)
5388 return "NONE";
5389
5390 if (conn->data == &sess_conn_cb)
5391 return "SESS";
5392
5393 if (conn->data == &si_conn_cb)
5394 return "STRM";
5395
5396 if (conn->data == &check_conn_cb)
5397 return "CHCK";
5398
5399 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5400 return ptr;
5401}
5402
Willy Tarreau87b09662015-04-03 00:22:06 +02005403/* This function dumps a complete stream state onto the stream interface's
5404 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005405 * 0 if the output buffer is full and it needs to be called again, otherwise
5406 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005407 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005408static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005409{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005410 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005411 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005412 extern const char *monthname[12];
5413 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005414 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005415 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005416
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005417 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005418
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005419 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005420 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005421 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005422 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005423 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005424 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005425 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005426 appctx->ctx.sess.uid = 0;
5427 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005428 return 1;
5429 }
5430
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005431 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005432 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005433 appctx->ctx.sess.uid = sess->uniq_id;
5434 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005435 /* fall through */
5436
5437 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005438 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005439 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005440 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005441 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005442 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5443 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005444 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005445 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005446
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005447 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005448 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005449 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005450 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005451 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005452 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005453 break;
5454 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005455 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005456 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005457 default:
5458 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005459 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005460 break;
5461 }
5462
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005463 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005464 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005465 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005466
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005467 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005468 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005469 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5470 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5471 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005472
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005473 if (conn)
5474 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005475
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005476 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005477 case AF_INET:
5478 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005479 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005480 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005481 break;
5482 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005483 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005484 break;
5485 default:
5486 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005487 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005488 break;
5489 }
5490
Willy Tarreau50943332011-09-02 17:33:05 +02005491 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005492 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005493 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005494 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005495 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5496 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005497 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005498
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005499 conn = objt_conn(sess->si[1].end);
5500 if (conn)
5501 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005502
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005503 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005504 case AF_INET:
5505 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005506 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005507 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005508 break;
5509 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005510 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005511 break;
5512 default:
5513 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005514 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005515 break;
5516 }
5517
5518 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005519 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005520 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005521 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5522 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005523 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005524 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005525
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005526 if (conn)
5527 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005528
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005529 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005530 case AF_INET:
5531 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005532 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005533 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005534 break;
5535 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005536 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005537 break;
5538 default:
5539 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005540 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005541 break;
5542 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005543
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005544 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005545 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005546 sess->task,
5547 sess->task->state,
5548 sess->task->nice, sess->task->calls,
5549 sess->task->expire ?
5550 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5551 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5552 TICKS_TO_MS(1000)) : "<NEVER>",
5553 task_in_rq(sess->task) ? ", running" : "");
5554
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005555 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005556 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005557 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5558
Willy Tarreaueee5b512015-04-03 23:46:31 +02005559 if (sess->txn)
5560 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005561 " 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 +02005562 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5563 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 +01005564
5565 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005566 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005567 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005568 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005569 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005570 obj_type_name(sess->si[0].end),
5571 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005572 sess->si[0].exp ?
5573 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5574 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5575 TICKS_TO_MS(1000)) : "<NEVER>",
5576 sess->si[0].err_type);
5577
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005578 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005579 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005580 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005581 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005582 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005583 obj_type_name(sess->si[1].end),
5584 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005585 sess->si[1].exp ?
5586 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5587 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5588 TICKS_TO_MS(1000)) : "<NEVER>",
5589 sess->si[1].err_type);
5590
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005591 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005592 chunk_appendf(&trash,
5593 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005594 conn,
5595 get_conn_ctrl_name(conn),
5596 get_conn_xprt_name(conn),
5597 get_conn_data_name(conn),
5598 obj_type_name(conn->target),
5599 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005600
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005601 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005602 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005603 conn->flags,
5604 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005605 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005606 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005607 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005608 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005609 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5610 chunk_appendf(&trash,
5611 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5612 tmpctx,
5613 tmpctx->st0,
5614 tmpctx->st1,
5615 tmpctx->st2,
5616 tmpctx->applet->name);
5617 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005618
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005619 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005620 chunk_appendf(&trash,
5621 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005622 conn,
5623 get_conn_ctrl_name(conn),
5624 get_conn_xprt_name(conn),
5625 get_conn_data_name(conn),
5626 obj_type_name(conn->target),
5627 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005628
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005629 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01005630 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005631 conn->flags,
5632 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005633 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005634 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005635 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005636 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005637 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5638 chunk_appendf(&trash,
5639 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5640 tmpctx,
5641 tmpctx->st0,
5642 tmpctx->st1,
5643 tmpctx->st2,
5644 tmpctx->applet->name);
5645 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005646
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005647 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005648 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005649 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005650 &sess->req,
5651 sess->req.flags, sess->req.analysers,
5652 sess->req.pipe ? sess->req.pipe->data : 0,
5653 sess->req.to_forward, sess->req.total,
5654 sess->req.analyse_exp ?
5655 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005656 TICKS_TO_MS(1000)) : "<NEVER>");
5657
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005658 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005659 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005660 sess->req.rex ?
5661 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005662 TICKS_TO_MS(1000)) : "<NEVER>");
5663
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005664 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005665 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005666 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005667 sess->req.wex ?
5668 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005669 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005670 sess->req.buf,
5671 sess->req.buf->data, sess->req.buf->o,
5672 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005673 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005674 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005675
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005676 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005677 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005678 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005679 &sess->res,
5680 sess->res.flags, sess->res.analysers,
5681 sess->res.pipe ? sess->res.pipe->data : 0,
5682 sess->res.to_forward, sess->res.total,
5683 sess->res.analyse_exp ?
5684 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005685 TICKS_TO_MS(1000)) : "<NEVER>");
5686
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005687 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005688 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005689 sess->res.rex ?
5690 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005691 TICKS_TO_MS(1000)) : "<NEVER>");
5692
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005693 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005694 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005695 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005696 sess->res.wex ?
5697 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005698 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005699 sess->res.buf,
5700 sess->res.buf->data, sess->res.buf->o,
5701 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005702 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005703 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005704
Willy Tarreaubc18da12015-03-13 14:00:47 +01005705 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005706 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005707 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005708 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005709
5710 /* use other states to dump the contents */
5711 }
5712 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005713 appctx->ctx.sess.uid = 0;
5714 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005715 return 1;
5716}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005717
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005718#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5719static int stats_tlskeys_list(struct stream_interface *si) {
5720 struct appctx *appctx = __objt_appctx(si->end);
5721
5722 switch (appctx->st2) {
5723 case STAT_ST_INIT:
5724 /* Display the column headers. If the message cannot be sent,
5725 * quit the fucntion with returning 0. The function is called
5726 * later and restart at the state "STAT_ST_INIT".
5727 */
5728 chunk_reset(&trash);
5729 chunk_appendf(&trash, "# id (file)\n");
5730 if (bi_putchk(si_ic(si), &trash) == -1) {
5731 si_applet_cant_put(si);
5732 return 0;
5733 }
5734
5735 /* Now, we start the browsing of the references lists.
5736 * Note that the following call to LIST_ELEM return bad pointer. The only
5737 * avalaible field of this pointer is <list>. It is used with the function
5738 * tlskeys_list_get_next() for retruning the first avalaible entry
5739 */
5740 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
5741 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5742
5743 appctx->st2 = STAT_ST_LIST;
5744 /* fall through */
5745
5746 case STAT_ST_LIST:
5747 while (appctx->ctx.tlskeys.ref) {
5748 chunk_reset(&trash);
5749
5750 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
5751 appctx->ctx.tlskeys.ref->filename);
5752
5753 if (bi_putchk(si_ic(si), &trash) == -1) {
5754 /* let's try again later from this stream. We add ourselves into
5755 * this stream's users so that it can remove us upon termination.
5756 */
5757 si_applet_cant_put(si);
5758 return 0;
5759 }
5760
5761 /* get next list entry and check the end of the list */
5762 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5763 }
5764
5765 appctx->st2 = STAT_ST_FIN;
5766 /* fall through */
5767
5768 default:
5769 appctx->st2 = STAT_ST_FIN;
5770 return 1;
5771 }
5772 return 0;
5773}
5774#endif
5775
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005776static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005777{
5778 struct appctx *appctx = __objt_appctx(si->end);
5779
5780 switch (appctx->st2) {
5781 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005782 /* Display the column headers. If the message cannot be sent,
5783 * quit the fucntion with returning 0. The function is called
5784 * later and restart at the state "STAT_ST_INIT".
5785 */
5786 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005787 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005788 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005789 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005790 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005791 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005792
5793 /* Now, we start the browsing of the references lists.
5794 * Note that the following call to LIST_ELEM return bad pointer. The only
5795 * avalaible field of this pointer is <list>. It is used with the function
5796 * pat_list_get_next() for retruning the first avalaible entry
5797 */
5798 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5799 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5800 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005801 appctx->st2 = STAT_ST_LIST;
5802 /* fall through */
5803
5804 case STAT_ST_LIST:
5805 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005806 chunk_reset(&trash);
5807
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005808 /* Build messages. If the reference is used by another category than
5809 * the listed categorie, display the information in the massage.
5810 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005811 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005812 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5813 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005814
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005815 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005816 /* let's try again later from this stream. We add ourselves into
5817 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005818 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005819 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005820 return 0;
5821 }
5822
5823 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005824 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5825 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005826 }
5827
5828 appctx->st2 = STAT_ST_FIN;
5829 /* fall through */
5830
5831 default:
5832 appctx->st2 = STAT_ST_FIN;
5833 return 1;
5834 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005835 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005836}
5837
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005838static int stats_map_lookup(struct stream_interface *si)
5839{
5840 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005841 struct sample sample;
5842 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005843 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005844
5845 switch (appctx->st2) {
5846 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005847 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005848 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005849 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005850 appctx->st2 = STAT_ST_LIST;
5851 /* fall through */
5852
5853 case STAT_ST_LIST:
5854 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005855 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005856 /* initialise chunk to build new message */
5857 chunk_reset(&trash);
5858
5859 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005860 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01005861 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005862 sample.data.u.str.len = appctx->ctx.map.chunk.len;
5863 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005864 if (appctx->ctx.map.expr->pat_head->match &&
5865 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005866 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5867 else
5868 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005869
5870 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005871 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5872 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5873 break;
5874 if (match_method >= PAT_MATCH_NUM)
5875 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5876 else
5877 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005878
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005879 /* case sensitive */
5880 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5881 chunk_appendf(&trash, ", case=insensitive");
5882 else
5883 chunk_appendf(&trash, ", case=sensitive");
5884
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005885 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005886 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005887 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5888 chunk_appendf(&trash, ", found=no");
5889 else
5890 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005891 }
5892
5893 /* Display match and match info */
5894 else {
5895 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005896 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5897 chunk_appendf(&trash, ", found=yes");
5898 else
5899 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005900
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005901 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005902 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005903 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005904 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005905 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005906
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005907 /* display pattern */
5908 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5909 if (pat->ref && pat->ref->pattern)
5910 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5911 else
5912 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005913 }
5914 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005915 if (pat->ref && pat->ref->pattern)
5916 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5917 else
5918 chunk_appendf(&trash, ", pattern=unknown");
5919 }
5920
5921 /* display return value */
5922 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02005923 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02005924 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02005925 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005926 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005927 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005928 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005929 }
5930
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005931 chunk_appendf(&trash, "\n");
5932
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005933 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005934 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005935 /* let's try again later from this stream. We add ourselves into
5936 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005937 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005938 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005939 return 0;
5940 }
5941
5942 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005943 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5944 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005945 }
5946
5947 appctx->st2 = STAT_ST_FIN;
5948 /* fall through */
5949
5950 default:
5951 appctx->st2 = STAT_ST_FIN;
5952 free(appctx->ctx.map.chunk.str);
5953 return 1;
5954 }
5955}
5956
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005957static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005958{
5959 struct appctx *appctx = __objt_appctx(si->end);
5960
5961 switch (appctx->st2) {
5962
5963 case STAT_ST_INIT:
5964 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005965 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5966 struct pat_ref_elt *, list);
5967 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5968 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005969 appctx->st2 = STAT_ST_LIST;
5970 /* fall through */
5971
5972 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005973 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005974 chunk_reset(&trash);
5975
5976 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005977 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005978 chunk_appendf(&trash, "%p %s %s\n",
5979 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5980 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005981 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005982 chunk_appendf(&trash, "%p %s\n",
5983 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005984
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005985 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005986 /* let's try again later from this stream. We add ourselves into
5987 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005988 */
Willy Tarreaufe127932015-04-21 19:23:39 +02005989 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005990 return 0;
5991 }
5992
5993 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005994 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5995 struct pat_ref_elt *, list);
5996 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005997 break;
5998 }
5999
6000 appctx->st2 = STAT_ST_FIN;
6001 /* fall through */
6002
6003 default:
6004 appctx->st2 = STAT_ST_FIN;
6005 return 1;
6006 }
6007}
6008
Willy Tarreau87b09662015-04-03 00:22:06 +02006009/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006010 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006011 * to be called again, otherwise non-zero. It is designed to be called
6012 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006013 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006014static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006015{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006016 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006017 struct connection *conn;
6018
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006019 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006020 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006021 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006022 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006023 if (appctx->st2 == STAT_ST_LIST) {
6024 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6025 LIST_DEL(&appctx->ctx.sess.bref.users);
6026 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006027 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006028 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006029 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006030 }
6031
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006032 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006033
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006034 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006035 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006036 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006037 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006038 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006039 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006040 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006041 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006042 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006043 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006044 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006045 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006046 /* fall through */
6047
Willy Tarreau295a8372011-03-10 11:25:07 +01006048 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006049 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006050 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6051 LIST_DEL(&appctx->ctx.sess.bref.users);
6052 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006053 }
6054
6055 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006056 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006057 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006058 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006059
Willy Tarreau87b09662015-04-03 00:22:06 +02006060 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006061
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006062 if (appctx->ctx.sess.target) {
6063 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006064 goto next_sess;
6065
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006066 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006067 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006068 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006069 return 0;
6070
Willy Tarreau87b09662015-04-03 00:22:06 +02006071 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006072 LIST_DEL(&appctx->ctx.sess.bref.users);
6073 LIST_INIT(&appctx->ctx.sess.bref.users);
6074 if (appctx->ctx.sess.target != (void *)-1) {
6075 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006076 break;
6077 }
6078 else
6079 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006080 }
6081
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006082 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006083 "%p: proto=%s",
6084 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006085 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006086
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006087 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006088 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006089 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006090 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006091 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006092 " src=%s:%d fe=%s be=%s srv=%s",
6093 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006094 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006095 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006096 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006097 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006098 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006099 break;
6100 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006101 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006102 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006103 strm_li(curr_sess)->luid,
6104 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006105 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006106 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006107 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006108 break;
6109 }
6110
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006111 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006112 " ts=%02x age=%s calls=%d",
6113 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006114 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6115 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006116
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006117 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006118 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006119 curr_sess->req.flags,
6120 curr_sess->req.buf->i,
6121 curr_sess->req.analysers,
6122 curr_sess->req.rex ?
6123 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006124 TICKS_TO_MS(1000)) : "");
6125
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006126 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006127 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006128 curr_sess->req.wex ?
6129 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006130 TICKS_TO_MS(1000)) : "");
6131
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006132 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006133 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006134 curr_sess->req.analyse_exp ?
6135 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006136 TICKS_TO_MS(1000)) : "");
6137
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006138 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006139 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006140 curr_sess->res.flags,
6141 curr_sess->res.buf->i,
6142 curr_sess->res.analysers,
6143 curr_sess->res.rex ?
6144 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006145 TICKS_TO_MS(1000)) : "");
6146
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006147 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006148 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006149 curr_sess->res.wex ?
6150 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006151 TICKS_TO_MS(1000)) : "");
6152
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006153 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006154 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006155 curr_sess->res.analyse_exp ?
6156 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006157 TICKS_TO_MS(1000)) : "");
6158
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006159 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006160 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006161 " s0=[%d,%1xh,fd=%d,ex=%s]",
6162 curr_sess->si[0].state,
6163 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006164 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006165 curr_sess->si[0].exp ?
6166 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6167 TICKS_TO_MS(1000)) : "");
6168
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006169 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006170 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006171 " s1=[%d,%1xh,fd=%d,ex=%s]",
6172 curr_sess->si[1].state,
6173 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006174 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006175 curr_sess->si[1].exp ?
6176 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6177 TICKS_TO_MS(1000)) : "");
6178
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006179 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006180 " exp=%s",
6181 curr_sess->task->expire ?
6182 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6183 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006184 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006185 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006186
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006187 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006188
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006189 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006190 /* let's try again later from this stream. We add ourselves into
6191 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006192 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006193 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006194 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006195 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006196 }
6197
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006198 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006199 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006200 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006201
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006202 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006203 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006204 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006205 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006206 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006207 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006208
Willy Tarreaubc18da12015-03-13 14:00:47 +01006209 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006210 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006211 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006212 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006213
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006214 appctx->ctx.sess.target = NULL;
6215 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006216 return 1;
6217 }
6218
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006219 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006220 /* fall through */
6221
6222 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006223 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006224 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006225 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006226}
6227
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006228/* This is called when the stream interface is closed. For instance, upon an
6229 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006230 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006231 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006232static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006233{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006234 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6235 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6236 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006237 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006238 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6239 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006240 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006241 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006242 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6243 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006244 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006245 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006246}
6247
Willy Tarreau20e99322013-04-13 09:22:25 +02006248/* This function is used to either dump tables states (when action is set
6249 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006250 * It returns 0 if the output buffer is full and it needs to be called
6251 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006252 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006253static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006254{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006255 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006256 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006257 struct ebmb_node *eb;
6258 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006259 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006260 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006261
6262 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006263 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006264 * - STAT_ST_INIT : the first call
6265 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006266 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006267 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006268 * and the entry pointer points to the next entry to be dumped,
6269 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006270 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006271 * data though.
6272 */
6273
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006274 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006275 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006276 if (appctx->st2 == STAT_ST_LIST) {
6277 appctx->ctx.table.entry->ref_cnt--;
6278 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006279 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006280 return 1;
6281 }
6282
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006283 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006284
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006285 while (appctx->st2 != STAT_ST_FIN) {
6286 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006287 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006288 appctx->ctx.table.proxy = appctx->ctx.table.target;
6289 if (!appctx->ctx.table.proxy)
6290 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006291
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006292 appctx->ctx.table.entry = NULL;
6293 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006294 break;
6295
Willy Tarreau295a8372011-03-10 11:25:07 +01006296 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006297 if (!appctx->ctx.table.proxy ||
6298 (appctx->ctx.table.target &&
6299 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6300 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006301 break;
6302 }
6303
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006304 if (appctx->ctx.table.proxy->table.size) {
6305 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6306 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006307 return 0;
6308
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006309 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006310 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006311 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006312 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006313 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006314 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6315 appctx->ctx.table.entry->ref_cnt++;
6316 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006317 break;
6318 }
6319 }
6320 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006321 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006322 break;
6323
Willy Tarreau295a8372011-03-10 11:25:07 +01006324 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006325 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006326
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006327 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006328 /* we're filtering on some data contents */
6329 void *ptr;
6330 long long data;
6331
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006332 dt = appctx->ctx.table.data_type;
6333 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6334 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006335 dt);
6336
6337 data = 0;
6338 switch (stktable_data_types[dt].std_type) {
6339 case STD_T_SINT:
6340 data = stktable_data_cast(ptr, std_t_sint);
6341 break;
6342 case STD_T_UINT:
6343 data = stktable_data_cast(ptr, std_t_uint);
6344 break;
6345 case STD_T_ULL:
6346 data = stktable_data_cast(ptr, std_t_ull);
6347 break;
6348 case STD_T_FRQP:
6349 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006350 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006351 break;
6352 }
6353
6354 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006355 if ((data < appctx->ctx.table.value &&
6356 (appctx->ctx.table.data_op == STD_OP_EQ ||
6357 appctx->ctx.table.data_op == STD_OP_GT ||
6358 appctx->ctx.table.data_op == STD_OP_GE)) ||
6359 (data == appctx->ctx.table.value &&
6360 (appctx->ctx.table.data_op == STD_OP_NE ||
6361 appctx->ctx.table.data_op == STD_OP_GT ||
6362 appctx->ctx.table.data_op == STD_OP_LT)) ||
6363 (data > appctx->ctx.table.value &&
6364 (appctx->ctx.table.data_op == STD_OP_EQ ||
6365 appctx->ctx.table.data_op == STD_OP_LT ||
6366 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006367 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006368 }
6369
Simon Hormanc88b8872011-06-15 15:18:49 +09006370 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006371 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6372 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006373 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006374
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006375 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006376
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006377 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006378 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006379 struct stksess *old = appctx->ctx.table.entry;
6380 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006381 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006382 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6383 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6384 stksess_kill(&appctx->ctx.table.proxy->table, old);
6385 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006386 break;
6387 }
6388
Simon Hormanc88b8872011-06-15 15:18:49 +09006389
6390 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006391 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6392 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6393 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006394
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006395 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6396 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006397 break;
6398
Willy Tarreau295a8372011-03-10 11:25:07 +01006399 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006400 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006401 break;
6402 }
6403 }
6404 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006405}
6406
Willy Tarreaud426a182010-03-05 14:58:26 +01006407/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006408 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6409 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6410 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6411 * lines are respected within the limit of 70 output chars. Lines that are
6412 * continuation of a previous truncated line begin with "+" instead of " "
6413 * after the offset. The new pointer is returned.
6414 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006415static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6416 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006417{
6418 int end;
6419 unsigned char c;
6420
6421 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006422 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006423 return ptr;
6424
Willy Tarreau77804732012-10-29 16:14:26 +01006425 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006426
Willy Tarreaud426a182010-03-05 14:58:26 +01006427 while (ptr < len && ptr < bsize) {
6428 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006429 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006430 if (out->len > end - 2)
6431 break;
6432 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006433 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006434 if (out->len > end - 3)
6435 break;
6436 out->str[out->len++] = '\\';
6437 switch (c) {
6438 case '\t': c = 't'; break;
6439 case '\n': c = 'n'; break;
6440 case '\r': c = 'r'; break;
6441 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006442 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006443 }
6444 out->str[out->len++] = c;
6445 } else {
6446 if (out->len > end - 5)
6447 break;
6448 out->str[out->len++] = '\\';
6449 out->str[out->len++] = 'x';
6450 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6451 out->str[out->len++] = hextab[c & 0xF];
6452 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006453 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006454 /* we had a line break, let's return now */
6455 out->str[out->len++] = '\n';
6456 *line = ptr;
6457 return ptr;
6458 }
6459 }
6460 /* we have an incomplete line, we return it as-is */
6461 out->str[out->len++] = '\n';
6462 return ptr;
6463}
6464
Baptiste Assmann3863f972015-05-17 00:33:24 +02006465/* This function dumps counters from all resolvers section and associated name servers.
6466 * It returns 0 if the output buffer is full and it needs
6467 * to be called again, otherwise non-zero.
6468 */
6469static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6470{
6471 struct appctx *appctx = __objt_appctx(si->end);
6472 struct dns_resolvers *presolvers;
6473 struct dns_nameserver *pnameserver;
6474
6475 chunk_reset(&trash);
6476
6477 switch (appctx->st2) {
6478 case STAT_ST_INIT:
6479 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6480 /* fall through */
6481
6482 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006483 if (LIST_ISEMPTY(&dns_resolvers)) {
6484 chunk_appendf(&trash, "No resolvers found\n");
6485 }
6486 else {
6487 list_for_each_entry(presolvers, &dns_resolvers, list) {
6488 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6489 continue;
6490
6491 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6492 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6493 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6494 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6495 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6496 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6497 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6498 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6499 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6500 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6501 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6502 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6503 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6504 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6505 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6506 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6507 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6508 }
6509 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006510 }
6511
6512 /* display response */
6513 if (bi_putchk(si_ic(si), &trash) == -1) {
6514 /* let's try again later from this session. We add ourselves into
6515 * this session's users so that it can remove us upon termination.
6516 */
6517 si->flags |= SI_FL_WAIT_ROOM;
6518 return 0;
6519 }
6520
6521 appctx->st2 = STAT_ST_FIN;
6522 /* fall through */
6523
6524 default:
6525 appctx->st2 = STAT_ST_FIN;
6526 return 1;
6527 }
6528}
6529
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006530/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006531 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006532 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006533 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006534static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006535{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006536 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006537 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006538
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006539 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006540 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006541
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006542 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006543
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006544 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006545 /* the function had not been called yet, let's prepare the
6546 * buffer for a response.
6547 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006548 struct tm tm;
6549
6550 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006551 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006552 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6553 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6554 error_snapshot_id);
6555
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006556 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006557 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006558 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006559 return 0;
6560 }
6561
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006562 appctx->ctx.errors.px = proxy;
6563 appctx->ctx.errors.buf = 0;
6564 appctx->ctx.errors.bol = 0;
6565 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006566 }
6567
6568 /* we have two inner loops here, one for the proxy, the other one for
6569 * the buffer.
6570 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006571 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006572 struct error_snapshot *es;
6573
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006574 if (appctx->ctx.errors.buf == 0)
6575 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006576 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006577 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006578
6579 if (!es->when.tv_sec)
6580 goto next;
6581
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006582 if (appctx->ctx.errors.iid >= 0 &&
6583 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6584 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006585 goto next;
6586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006587 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006588 /* just print headers now */
6589
6590 char pn[INET6_ADDRSTRLEN];
6591 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006592 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006593
6594 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006595 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006596 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006597 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006598
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006599 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6600 case AF_INET:
6601 case AF_INET6:
6602 port = get_host_port(&es->src);
6603 break;
6604 default:
6605 port = 0;
6606 }
6607
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006608 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006609 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006610 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006611 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006612 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006613 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006614 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6615 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006616 break;
6617 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006618 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006619 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006620 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006621 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006622 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006623 break;
6624 }
6625
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006626 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006627 ", server %s (#%d), event #%u\n"
6628 " src %s:%d, session #%d, session flags 0x%08x\n"
6629 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6630 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6631 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6632 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6633 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6634 es->ev_id,
6635 pn, port, es->sid, es->s_flags,
6636 es->state, es->m_flags, es->t_flags,
6637 es->m_clen, es->m_blen,
6638 es->b_flags, es->b_out, es->b_tot,
6639 es->len, es->b_wrap, es->pos);
6640
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006641 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006642 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006643 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006644 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006645 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006646 appctx->ctx.errors.ptr = 0;
6647 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006648 }
6649
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006650 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006651 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006652 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006653 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006654 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006655 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006656 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006657 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006658 goto next;
6659 }
6660
6661 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006662 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006663 int newptr;
6664 int newline;
6665
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006666 newline = appctx->ctx.errors.bol;
6667 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6668 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006669 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006670
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006671 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006672 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006673 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006674 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006675 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006676 appctx->ctx.errors.ptr = newptr;
6677 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006678 };
6679 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006680 appctx->ctx.errors.bol = 0;
6681 appctx->ctx.errors.ptr = -1;
6682 appctx->ctx.errors.buf++;
6683 if (appctx->ctx.errors.buf > 1) {
6684 appctx->ctx.errors.buf = 0;
6685 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006686 }
Willy Tarreauae795722016-02-16 11:27:28 +01006687 }
6688
6689 /* dump complete */
6690 return 1;
6691}
6692
6693/* This function dumps all environmnent variables to the buffer. It returns 0
6694 * if the output buffer is full and it needs to be called again, otherwise
6695 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
6696 */
6697static int stats_dump_env_to_buffer(struct stream_interface *si)
6698{
6699 struct appctx *appctx = __objt_appctx(si->end);
6700
6701 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
6702 return 1;
6703
6704 chunk_reset(&trash);
6705
6706 /* we have two inner loops here, one for the proxy, the other one for
6707 * the buffer.
6708 */
6709 while (*appctx->ctx.env.var) {
6710 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
6711
6712 if (bi_putchk(si_ic(si), &trash) == -1) {
6713 si_applet_cant_put(si);
6714 return 0;
6715 }
6716 if (appctx->st2 == STAT_ST_END)
6717 break;
6718 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006719 }
6720
6721 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006722 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006723}
6724
Willy Tarreaud5781202012-09-22 19:32:35 +02006725/* parse the "level" argument on the bind lines */
6726static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6727{
6728 if (!*args[cur_arg + 1]) {
6729 memprintf(err, "'%s' : missing level", args[cur_arg]);
6730 return ERR_ALERT | ERR_FATAL;
6731 }
6732
6733 if (!strcmp(args[cur_arg+1], "user"))
6734 conf->level = ACCESS_LVL_USER;
6735 else if (!strcmp(args[cur_arg+1], "operator"))
6736 conf->level = ACCESS_LVL_OPER;
6737 else if (!strcmp(args[cur_arg+1], "admin"))
6738 conf->level = ACCESS_LVL_ADMIN;
6739 else {
6740 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6741 args[cur_arg], args[cur_arg+1]);
6742 return ERR_ALERT | ERR_FATAL;
6743 }
6744
6745 return 0;
6746}
6747
Willy Tarreau30576452015-04-13 13:50:30 +02006748struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006749 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006750 .name = "<STATS>", /* used for logging */
6751 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006752 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006753};
6754
Willy Tarreau30576452015-04-13 13:50:30 +02006755static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006756 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006757 .name = "<CLI>", /* used for logging */
6758 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006759 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006760};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006761
Willy Tarreaudc13c112013-06-21 23:16:39 +02006762static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006763 { CFG_GLOBAL, "stats", stats_parse_global },
6764 { 0, NULL, NULL },
6765}};
6766
Willy Tarreaud5781202012-09-22 19:32:35 +02006767static struct bind_kw_list bind_kws = { "STAT", { }, {
6768 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6769 { NULL, NULL, 0 },
6770}};
6771
Willy Tarreau10522fd2008-07-09 20:12:41 +02006772__attribute__((constructor))
6773static void __dumpstats_module_init(void)
6774{
6775 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006776 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006777}
6778
Willy Tarreau91861262007-10-17 17:06:05 +02006779/*
6780 * Local variables:
6781 * c-indent-level: 8
6782 * c-basic-offset: 8
6783 * End:
6784 */