blob: c23e9888b0aabf1593eb6be63dc1decc6e96cf0c [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 Tarreau91861262007-10-17 17:06:05 +020040#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041
42#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020043#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020044#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010045#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020046#include <proto/dumpstats.h>
47#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010048#include <proto/freq_ctr.h>
Willy Tarreau32b60d42015-03-13 16:14:57 +010049#include <proto/frontend.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020050#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010051#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010052#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020053#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010054#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010055#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020056#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020057#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010058#include <proto/sample.h>
Willy Tarreau91861262007-10-17 17:06:05 +020059#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020060#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020061#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010062#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010063#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020064
Willy Tarreau7a0169a2012-11-19 17:13:16 +010065#ifdef USE_OPENSSL
66#include <proto/ssl_sock.h>
67#endif
68
Willy Tarreau91b843d2014-01-28 16:27:17 +010069/* stats socket states */
70enum {
71 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
72 STAT_CLI_END, /* final state, let's close */
73 STAT_CLI_GETREQ, /* wait for a request */
74 STAT_CLI_OUTPUT, /* all states after this one are responses */
75 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
76 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010077 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010078 STAT_CLI_O_INFO, /* dump info */
79 STAT_CLI_O_SESS, /* dump sessions */
80 STAT_CLI_O_ERR, /* dump errors */
81 STAT_CLI_O_TAB, /* dump tables */
82 STAT_CLI_O_CLR, /* clear tables */
83 STAT_CLI_O_SET, /* set entries in tables */
84 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010085 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
86 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010087 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010088 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010089};
90
Willy Tarreaued7df902014-05-22 18:04:49 +020091/* Actions available for the stats admin forms */
92enum {
93 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +020094
95 /* enable/disable health checks */
96 ST_ADM_ACTION_DHLTH,
97 ST_ADM_ACTION_EHLTH,
98
99 /* force health check status */
100 ST_ADM_ACTION_HRUNN,
101 ST_ADM_ACTION_HNOLB,
102 ST_ADM_ACTION_HDOWN,
103
104 /* enable/disable agent checks */
105 ST_ADM_ACTION_DAGENT,
106 ST_ADM_ACTION_EAGENT,
107
108 /* force agent check status */
109 ST_ADM_ACTION_ARUNN,
110 ST_ADM_ACTION_ADOWN,
111
112 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200113 ST_ADM_ACTION_READY,
114 ST_ADM_ACTION_DRAIN,
115 ST_ADM_ACTION_MAINT,
116 ST_ADM_ACTION_SHUTDOWN,
117 /* these are the ancient actions, still available for compatibility */
118 ST_ADM_ACTION_DISABLE,
119 ST_ADM_ACTION_ENABLE,
120 ST_ADM_ACTION_STOP,
121 ST_ADM_ACTION_START,
122};
123
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100124static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100125static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +0100126static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900127static int stats_dump_sess_to_buffer(struct stream_interface *si);
128static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100129static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100130static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
131static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100132static int stats_pats_list(struct stream_interface *si);
133static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100134static int stats_map_lookup(struct stream_interface *si);
Willy Tarreau44cf5452014-10-22 19:06:31 +0200135static void cli_release_handler(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +0900136
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100137/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100138 * cli_io_handler()
139 * -> stats_dump_sess_to_buffer() // "show sess"
140 * -> stats_dump_errors_to_buffer() // "show errors"
141 * -> stats_dump_info_to_buffer() // "show info"
142 * -> stats_dump_stat_to_buffer() // "show stat"
143 * -> stats_dump_csv_header()
144 * -> stats_dump_proxy_to_buffer()
145 * -> stats_dump_fe_stats()
146 * -> stats_dump_li_stats()
147 * -> stats_dump_sv_stats()
148 * -> stats_dump_be_stats()
149 *
150 * http_stats_io_handler()
151 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
152 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
153 * -> stats_dump_html_head() // emits the HTML headers
154 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
155 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
156 * -> stats_dump_html_px_hdr()
157 * -> stats_dump_fe_stats()
158 * -> stats_dump_li_stats()
159 * -> stats_dump_sv_stats()
160 * -> stats_dump_be_stats()
161 * -> stats_dump_html_px_end()
162 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100163 */
164
Simon Horman9bd2c732011-06-15 15:18:44 +0900165static struct si_applet cli_applet;
166
167static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200168 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200169 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200170 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200171 " help : this message\n"
172 " prompt : toggle interactive mode with prompt\n"
173 " quit : disconnect\n"
174 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100175 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200176 " show stat : report counters for each proxy and server\n"
177 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100178 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200179 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200180 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200181 " set weight : change a server's weight\n"
Willy Tarreau2a4b70f2014-05-22 18:42:35 +0200182 " set server : change a server's state or weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200183 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100184 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200185 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200186 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200187 " disable : put a server or frontend in maintenance mode\n"
188 " enable : re-enable a server or frontend which is in maintenance mode\n"
189 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100190 " show acl [id] : report avalaible acls or dump an acl's contents\n"
191 " get acl : reports the patterns matching a sample for an ACL\n"
192 " add acl : add acl entry\n"
193 " del acl : delete acl entry\n"
194 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100195 " show map [id] : report avalaible maps or dump a map's contents\n"
196 " get map : reports the keys and values matching a sample for a map\n"
197 " set map : modify map entry\n"
198 " add map : add map entry\n"
199 " del map : delete map entry\n"
200 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200201 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200202 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200203
Simon Horman9bd2c732011-06-15 15:18:44 +0900204static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200205 "Permission denied\n"
206 "";
207
Willy Tarreau295a8372011-03-10 11:25:07 +0100208/* data transmission states for the stats responses */
209enum {
210 STAT_ST_INIT = 0,
211 STAT_ST_HEAD,
212 STAT_ST_INFO,
213 STAT_ST_LIST,
214 STAT_ST_END,
215 STAT_ST_FIN,
216};
217
218/* data transmission states for the stats responses inside a proxy */
219enum {
220 STAT_PX_ST_INIT = 0,
221 STAT_PX_ST_TH,
222 STAT_PX_ST_FE,
223 STAT_PX_ST_LI,
224 STAT_PX_ST_SV,
225 STAT_PX_ST_BE,
226 STAT_PX_ST_END,
227 STAT_PX_ST_FIN,
228};
229
Cyril Bonté19979e12012-04-04 12:57:21 +0200230extern const char *stat_status_codes[];
231
Willy Tarreau07e9e642010-08-17 21:48:17 +0200232/* allocate a new stats frontend named <name>, and return it
233 * (or NULL in case of lack of memory).
234 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200235static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200236{
237 struct proxy *fe;
238
239 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
240 if (!fe)
241 return NULL;
242
Willy Tarreau237250c2011-07-29 01:49:03 +0200243 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200244 fe->next = proxy;
245 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200246 fe->last_change = now.tv_sec;
247 fe->id = strdup("GLOBAL");
248 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200249 fe->maxconn = 10; /* default to 10 concurrent connections */
250 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200251 fe->conf.file = strdup(file);
252 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100253 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100254 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200255
256 /* the stats frontend is the only one able to assign ID #0 */
257 fe->conf.id.key = fe->uuid = 0;
258 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200259 return fe;
260}
261
Willy Tarreaufbee7132007-10-18 13:53:22 +0200262/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200263 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
264 * error message into the <err> buffer which will be preallocated. The trailing
265 * '\n' must not be written. The function must be called with <args> pointing to
266 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200267 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200268static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200269 struct proxy *defpx, const char *file, int line,
270 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200271{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200272 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200273 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200274
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200275 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200276 int cur_arg;
277
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200278 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200279 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 +0200280 return -1;
281 }
282
Willy Tarreau89a63132009-08-16 17:41:45 +0200283 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200284 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200285 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200286 return -1;
287 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200288 }
289
Willy Tarreau4348fad2012-09-20 16:48:07 +0200290 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200291 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200292
Willy Tarreauc53d4222012-09-20 20:19:28 +0200293 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
294 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
295 file, line, args[0], args[1], err && *err ? *err : "error");
296 return -1;
297 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200298
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200299 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200300 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200301 static int bind_dumped;
302 struct bind_kw *kw;
303
304 kw = bind_find_kw(args[cur_arg]);
305 if (kw) {
306 if (!kw->parse) {
307 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
308 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200309 return -1;
310 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200311
312 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
313 if (err && *err)
314 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
315 else
316 memprintf(err, "'%s %s' : error encountered while processing '%s'",
317 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200318 return -1;
319 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200320
321 cur_arg += 1 + kw->skip;
322 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200323 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200324
325 if (!bind_dumped) {
326 bind_dump_kws(err);
327 indent_msg(err, 4);
328 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200329 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200330
331 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
332 args[0], args[1], args[cur_arg],
333 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
334 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200335 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100336
Willy Tarreauc53d4222012-09-20 20:19:28 +0200337 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
338 l->maxconn = global.stats_fe->maxconn;
339 l->backlog = global.stats_fe->backlog;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200340 l->accept = session_accept;
341 l->handler = process_session;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100342 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200343 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
344 l->nice = -64; /* we want to boost priority for local stats */
345 global.maxsock += l->maxconn;
346 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200347 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200348 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100349 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200350 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100351
352 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200353 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100354 return -1;
355 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200356
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100357 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200358 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200359 return -1;
360 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200361 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200362 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200363 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200364 return -1;
365 }
366 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200367 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200368 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200369 else if (!strcmp(args[1], "maxconn")) {
370 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200371
372 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200373 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200374 return -1;
375 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200376
377 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200378 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200379 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200380 return -1;
381 }
382 }
383 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200384 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200385 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
386 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100387 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200388
Willy Tarreau91319572013-04-20 09:48:50 +0200389 if (!global.stats_fe) {
390 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
391 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
392 return -1;
393 }
394 }
395
Willy Tarreau35b7b162012-10-22 23:17:18 +0200396 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100397 unsigned int low, high;
398
Willy Tarreau35b7b162012-10-22 23:17:18 +0200399 if (strcmp(args[cur_arg], "all") == 0) {
400 set = 0;
401 break;
402 }
403 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100404 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200405 }
406 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100407 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200408 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100409 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100410 char *dash = strchr(args[cur_arg], '-');
411
412 low = high = str2uic(args[cur_arg]);
413 if (dash)
414 high = str2uic(dash + 1);
415
416 if (high < low) {
417 unsigned int swap = low;
418 low = high;
419 high = swap;
420 }
421
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100422 if (low < 1 || high > LONGBITS) {
423 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
424 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200425 return -1;
426 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100427 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100428 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100429 }
430 else {
431 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100432 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
433 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100434 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200435 }
436 cur_arg++;
437 }
438 global.stats_fe->bind_proc = set;
439 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200440 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200441 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200442 return -1;
443 }
444 return 0;
445}
446
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100447/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
448 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100449 * NOTE: Some tools happen to rely on the field position instead of its name,
450 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100451 */
452static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100453{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100454 chunk_appendf(&trash,
455 "# pxname,svname,"
456 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100457 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100458 "bin,bout,"
459 "dreq,dresp,"
460 "ereq,econ,eresp,"
461 "wretr,wredis,"
462 "status,weight,act,bck,"
463 "chkfail,chkdown,lastchg,downtime,qlimit,"
464 "pid,iid,sid,throttle,lbtot,tracked,type,"
465 "rate,rate_lim,rate_max,"
466 "check_status,check_code,check_duration,"
467 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
468 "req_rate,req_rate_max,req_tot,"
469 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200470 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100471 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100472}
473
Simon Hormand9366582011-06-15 15:18:45 +0900474/* print a string of text buffer to <out>. The format is :
475 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
476 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
477 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
478 */
479static int dump_text(struct chunk *out, const char *buf, int bsize)
480{
481 unsigned char c;
482 int ptr = 0;
483
484 while (buf[ptr] && ptr < bsize) {
485 c = buf[ptr];
486 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
487 if (out->len > out->size - 1)
488 break;
489 out->str[out->len++] = c;
490 }
491 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
492 if (out->len > out->size - 2)
493 break;
494 out->str[out->len++] = '\\';
495 switch (c) {
496 case ' ': c = ' '; break;
497 case '\t': c = 't'; break;
498 case '\n': c = 'n'; break;
499 case '\r': c = 'r'; break;
500 case '\e': c = 'e'; break;
501 case '\\': c = '\\'; break;
502 }
503 out->str[out->len++] = c;
504 }
505 else {
506 if (out->len > out->size - 4)
507 break;
508 out->str[out->len++] = '\\';
509 out->str[out->len++] = 'x';
510 out->str[out->len++] = hextab[(c >> 4) & 0xF];
511 out->str[out->len++] = hextab[c & 0xF];
512 }
513 ptr++;
514 }
515
516 return ptr;
517}
518
519/* print a buffer in hexa.
520 * Print stopped if <bsize> is reached, or if no more place in the chunk.
521 */
522static int dump_binary(struct chunk *out, const char *buf, int bsize)
523{
524 unsigned char c;
525 int ptr = 0;
526
527 while (ptr < bsize) {
528 c = buf[ptr];
529
530 if (out->len > out->size - 2)
531 break;
532 out->str[out->len++] = hextab[(c >> 4) & 0xF];
533 out->str[out->len++] = hextab[c & 0xF];
534
535 ptr++;
536 }
537 return ptr;
538}
539
540/* Dump the status of a table to a stream interface's
541 * read buffer. It returns 0 if the output buffer is full
542 * and needs to be called again, otherwise non-zero.
543 */
544static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
545 struct proxy *proxy, struct proxy *target)
546{
Willy Tarreau3c23a852014-12-28 12:19:57 +0100547 struct session *s = si_sess(si);
Simon Hormand9366582011-06-15 15:18:45 +0900548
Willy Tarreau77804732012-10-29 16:14:26 +0100549 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900550 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
551
552 /* any other information should be dumped here */
553
Willy Tarreau290e63a2012-09-20 18:07:14 +0200554 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100555 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900556
Willy Tarreaubc18da12015-03-13 14:00:47 +0100557 if (bi_putchk(si_ic(si), msg) == -1) {
558 si->flags |= SI_FL_WAIT_ROOM;
Simon Hormand9366582011-06-15 15:18:45 +0900559 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100560 }
Simon Hormand9366582011-06-15 15:18:45 +0900561
562 return 1;
563}
564
565/* Dump the a table entry to a stream interface's
566 * read buffer. It returns 0 if the output buffer is full
567 * and needs to be called again, otherwise non-zero.
568 */
569static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
570 struct proxy *proxy, struct stksess *entry)
571{
572 int dt;
573
Willy Tarreau77804732012-10-29 16:14:26 +0100574 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900575
576 if (proxy->table.type == STKTABLE_TYPE_IP) {
577 char addr[INET_ADDRSTRLEN];
578 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100579 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900580 }
581 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
582 char addr[INET6_ADDRSTRLEN];
583 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100584 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900585 }
586 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100587 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900588 }
589 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100590 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900591 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
592 }
593 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100594 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900595 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
596 }
597
Willy Tarreau77804732012-10-29 16:14:26 +0100598 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900599
600 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
601 void *ptr;
602
603 if (proxy->table.data_ofs[dt] == 0)
604 continue;
605 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100606 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900607 else
Willy Tarreau77804732012-10-29 16:14:26 +0100608 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900609
610 ptr = stktable_data_ptr(&proxy->table, entry, dt);
611 switch (stktable_data_types[dt].std_type) {
612 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100613 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900614 break;
615 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100616 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900617 break;
618 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100619 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900620 break;
621 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100622 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900623 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
624 proxy->table.data_arg[dt].u));
625 break;
626 }
627 }
Willy Tarreau77804732012-10-29 16:14:26 +0100628 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900629
Willy Tarreaubc18da12015-03-13 14:00:47 +0100630 if (bi_putchk(si_ic(si), msg) == -1) {
631 si->flags |= SI_FL_WAIT_ROOM;
Simon Hormand9366582011-06-15 15:18:45 +0900632 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100633 }
Simon Hormand9366582011-06-15 15:18:45 +0900634
635 return 1;
636}
637
Willy Tarreaudec98142012-06-06 23:37:08 +0200638static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900639{
Willy Tarreau3c23a852014-12-28 12:19:57 +0100640 struct session *s = si_sess(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100641 struct appctx *appctx = __objt_appctx(si->end);
642 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900643 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900644 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900645 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200646 long long value;
647 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200648 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200649 void *ptr;
650 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900651
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100652 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900653
654 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100655 appctx->ctx.cli.msg = "Key value expected\n";
656 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900657 return;
658 }
659
Simon Hormanc5b89f62011-06-15 15:18:50 +0900660 switch (px->table.type) {
661 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900662 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100663 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900664 break;
665 case STKTABLE_TYPE_IPV6:
666 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100667 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900668 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900669 case STKTABLE_TYPE_INTEGER:
670 {
671 char *endptr;
672 unsigned long val;
673 errno = 0;
674 val = strtoul(args[4], &endptr, 10);
675 if ((errno == ERANGE && val == ULONG_MAX) ||
676 (errno != 0 && val == 0) || endptr == args[4] ||
677 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100678 appctx->ctx.cli.msg = "Invalid key\n";
679 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900680 return;
681 }
682 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100683 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900684 break;
685 }
686 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900687 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100688 static_table_key->key = args[4];
689 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900690 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900691 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200692 switch (action) {
693 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100694 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 +0200695 break;
696 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100697 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 +0200698 break;
699 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100700 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200701 break;
702 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100703 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900704 return;
705 }
706
707 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200708 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100709 appctx->ctx.cli.msg = stats_permission_denied_msg;
710 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900711 return;
712 }
713
Willy Tarreau07115412012-10-29 21:56:59 +0100714 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900715
Willy Tarreaudec98142012-06-06 23:37:08 +0200716 switch (action) {
717 case STAT_CLI_O_TAB:
718 if (!ts)
719 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100720 chunk_reset(&trash);
721 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900722 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100723 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900724 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200725
726 case STAT_CLI_O_CLR:
727 if (!ts)
728 return;
729 if (ts->ref_cnt) {
730 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100731 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
732 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200733 return;
734 }
735 stksess_kill(&px->table, ts);
736 break;
Simon Horman17bce342011-06-15 15:18:47 +0900737
Willy Tarreau654694e2012-06-07 01:03:16 +0200738 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200739 if (ts)
740 stktable_touch(&px->table, ts, 1);
741 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100742 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200743 if (!ts) {
744 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100745 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
746 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200747 return;
748 }
749 stktable_store(&px->table, ts, 1);
750 }
751
Willy Tarreau47060b62013-08-01 21:11:42 +0200752 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
753 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100754 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
755 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200756 return;
757 }
758
759 data_type = stktable_get_data_type(args[cur_arg] + 5);
760 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100761 appctx->ctx.cli.msg = "Unknown data type\n";
762 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200763 return;
764 }
765
766 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100767 appctx->ctx.cli.msg = "Data type not stored in this table\n";
768 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200769 return;
770 }
771
772 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100773 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
774 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200775 return;
776 }
777
778 ptr = stktable_data_ptr(&px->table, ts, data_type);
779
780 switch (stktable_data_types[data_type].std_type) {
781 case STD_T_SINT:
782 stktable_data_cast(ptr, std_t_sint) = value;
783 break;
784 case STD_T_UINT:
785 stktable_data_cast(ptr, std_t_uint) = value;
786 break;
787 case STD_T_ULL:
788 stktable_data_cast(ptr, std_t_ull) = value;
789 break;
790 case STD_T_FRQP:
791 /* We set both the current and previous values. That way
792 * the reported frequency is stable during all the period
793 * then slowly fades out. This allows external tools to
794 * push measures without having to update them too often.
795 */
796 frqp = &stktable_data_cast(ptr, std_t_frqp);
797 frqp->curr_tick = now_ms;
798 frqp->prev_ctr = 0;
799 frqp->curr_ctr = value;
800 break;
801 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200802 }
803 break;
804
Willy Tarreaudec98142012-06-06 23:37:08 +0200805 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100806 appctx->ctx.cli.msg = "Unknown action\n";
807 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200808 break;
Simon Horman121f3052011-06-15 15:18:46 +0900809 }
Simon Horman121f3052011-06-15 15:18:46 +0900810}
811
Willy Tarreau654694e2012-06-07 01:03:16 +0200812static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900813{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100814 struct appctx *appctx = __objt_appctx(si->end);
815
Willy Tarreau04b3a192013-04-13 09:41:37 +0200816 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100817 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
818 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200819 return;
820 }
821
Simon Hormand5b9fd92011-06-15 15:18:48 +0900822 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100823 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
824 if (appctx->ctx.table.data_type < 0) {
825 appctx->ctx.cli.msg = "Unknown data type\n";
826 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900827 return;
828 }
829
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100830 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
831 appctx->ctx.cli.msg = "Data type not stored in this table\n";
832 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900833 return;
834 }
835
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100836 appctx->ctx.table.data_op = get_std_op(args[4]);
837 if (appctx->ctx.table.data_op < 0) {
838 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
839 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900840 return;
841 }
842
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100843 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
844 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
845 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900846 return;
847 }
848}
849
Willy Tarreaudec98142012-06-06 23:37:08 +0200850static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900851{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100852 struct appctx *appctx = __objt_appctx(si->end);
853
854 appctx->ctx.table.data_type = -1;
855 appctx->st2 = STAT_ST_INIT;
856 appctx->ctx.table.target = NULL;
857 appctx->ctx.table.proxy = NULL;
858 appctx->ctx.table.entry = NULL;
859 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900860
861 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100862 appctx->ctx.table.target = find_stktable(args[2]);
863 if (!appctx->ctx.table.target) {
864 appctx->ctx.cli.msg = "No such table\n";
865 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900866 return;
867 }
868 }
869 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200870 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900871 goto err_args;
872 return;
873 }
874
875 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200876 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900877 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200878 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900879 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900880 goto err_args;
881
882 return;
883
884err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200885 switch (action) {
886 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100887 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 +0200888 break;
889 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100890 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 +0200891 break;
892 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100893 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200894 break;
895 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100896 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900897}
898
Willy Tarreau532a4502011-09-07 22:37:44 +0200899/* Expects to find a frontend named <arg> and returns it, otherwise displays various
900 * adequate error messages and returns NULL. This function also expects the session
901 * level to be admin.
902 */
903static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
904{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100905 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200906 struct proxy *px;
907
Willy Tarreau290e63a2012-09-20 18:07:14 +0200908 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100909 appctx->ctx.cli.msg = stats_permission_denied_msg;
910 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200911 return NULL;
912 }
913
914 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100915 appctx->ctx.cli.msg = "A frontend name is expected.\n";
916 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200917 return NULL;
918 }
919
920 px = findproxy(arg, PR_CAP_FE);
921 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100922 appctx->ctx.cli.msg = "No such frontend.\n";
923 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200924 return NULL;
925 }
926 return px;
927}
928
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200929/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
930 * and returns the pointer to the server. Otherwise, display adequate error messages
931 * and returns NULL. This function also expects the session level to be admin. Note:
932 * the <arg> is modified to remove the '/'.
933 */
934static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
935{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100936 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200937 struct proxy *px;
938 struct server *sv;
939 char *line;
940
Willy Tarreau290e63a2012-09-20 18:07:14 +0200941 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100942 appctx->ctx.cli.msg = stats_permission_denied_msg;
943 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200944 return NULL;
945 }
946
947 /* split "backend/server" and make <line> point to server */
948 for (line = arg; *line; line++)
949 if (*line == '/') {
950 *line++ = '\0';
951 break;
952 }
953
954 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100955 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
956 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200957 return NULL;
958 }
959
960 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100961 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
962 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200963 return NULL;
964 }
965
966 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100967 appctx->ctx.cli.msg = "Proxy is disabled.\n";
968 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200969 return NULL;
970 }
971
972 return sv;
973}
974
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100975/* This function is used with map and acl management. It permits to browse
976 * each reference. The variable <getnext> must contain the current node,
977 * <end> point to the root node and the <flags> permit to filter required
978 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100979 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100980static inline
981struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
982 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100983{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100984 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100985
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100986 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100987
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100988 /* Get next list entry. */
989 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100990
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100991 /* If the entry is the last of the list, return NULL. */
992 if (&ref->list == end)
993 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100994
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100995 /* If the entry match the flag, return it. */
996 if (ref->flags & flags)
997 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100998 }
999}
1000
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001001static inline
1002struct pat_ref *pat_ref_lookup_ref(const char *reference)
1003{
1004 int id;
1005 char *error;
1006
1007 /* If the reference starts by a '#', this is numeric id. */
1008 if (reference[0] == '#') {
1009 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1010 id = strtol(reference + 1, &error, 10);
1011 if (*error != '\0')
1012 return NULL;
1013
1014 /* Perform the unique id lookup. */
1015 return pat_ref_lookupid(id);
1016 }
1017
1018 /* Perform the string lookup. */
1019 return pat_ref_lookup(reference);
1020}
1021
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001022/* This function is used with map and acl management. It permits to browse
1023 * each reference.
1024 */
1025static inline
1026struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1027{
1028 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001029 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1030 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001031 return NULL;
1032 return expr;
1033}
1034
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001035/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001036 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001037 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001038 * designating the function which will have to process the request, which can
1039 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001040 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001041static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001042{
Willy Tarreau3c23a852014-12-28 12:19:57 +01001043 struct session *s = si_sess(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001044 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001045 char *args[MAX_STATS_ARGS + 1];
1046 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001047 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001048
1049 while (isspace((unsigned char)*line))
1050 line++;
1051
1052 arg = 0;
1053 args[arg] = line;
1054
1055 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001056 if (*line == '\\') {
1057 line++;
1058 if (*line == '\0')
1059 break;
1060 }
1061 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001062 *line++ = '\0';
1063
1064 while (isspace((unsigned char)*line))
1065 line++;
1066
1067 args[++arg] = line;
1068 continue;
1069 }
1070
1071 line++;
1072 }
1073
1074 while (++arg <= MAX_STATS_ARGS)
1075 args[arg] = line;
1076
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001077 /* remove \ */
1078 arg = 0;
1079 while (*args[arg] != '\0') {
1080 j = 0;
1081 for (i=0; args[arg][i] != '\0'; i++) {
1082 if (args[arg][i] == '\\')
1083 continue;
1084 args[arg][j] = args[arg][i];
1085 j++;
1086 }
1087 args[arg][j] = '\0';
1088 arg++;
1089 }
1090
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001091 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001092 if (strcmp(args[0], "show") == 0) {
1093 if (strcmp(args[1], "stat") == 0) {
1094 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001095 appctx->ctx.stats.flags |= STAT_BOUND;
1096 appctx->ctx.stats.iid = atoi(args[2]);
1097 appctx->ctx.stats.type = atoi(args[3]);
1098 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001099 }
1100
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001101 appctx->st2 = STAT_ST_INIT;
1102 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001103 }
1104 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001105 appctx->st2 = STAT_ST_INIT;
1106 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001107 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001108 else if (strcmp(args[1], "pools") == 0) {
1109 appctx->st2 = STAT_ST_INIT;
1110 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1111 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001112 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001113 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001114 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001115 appctx->ctx.cli.msg = stats_permission_denied_msg;
1116 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001117 return 1;
1118 }
Willy Tarreau76153662012-11-26 01:16:39 +01001119 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001120 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001121 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001122 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001123 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001124 appctx->ctx.sess.target = NULL;
1125 appctx->ctx.sess.section = 0; /* start with session status */
1126 appctx->ctx.sess.pos = 0;
1127 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001128 }
1129 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001130 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001131 appctx->ctx.cli.msg = stats_permission_denied_msg;
1132 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001133 return 1;
1134 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001135 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001136 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001137 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001138 appctx->ctx.errors.iid = -1;
1139 appctx->ctx.errors.px = NULL;
1140 appctx->st2 = STAT_ST_INIT;
1141 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001142 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001143 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001144 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001145 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001146 else if (strcmp(args[1], "map") == 0 ||
1147 strcmp(args[1], "acl") == 0) {
1148
1149 /* Set ACL or MAP flags. */
1150 if (args[1][0] == 'm')
1151 appctx->ctx.map.display_flags = PAT_REF_MAP;
1152 else
1153 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001154
1155 /* no parameter: display all map avalaible */
1156 if (!*args[2]) {
1157 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001158 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001159 return 1;
1160 }
1161
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001162 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001163 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001164 if (!appctx->ctx.map.ref ||
1165 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1166 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001167 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001168 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001169 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001170 appctx->st0 = STAT_CLI_PRINT;
1171 return 1;
1172 }
1173 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001174 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001175 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001176 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001177 return 0;
1178 }
1179 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001180 else if (strcmp(args[0], "clear") == 0) {
1181 if (strcmp(args[1], "counters") == 0) {
1182 struct proxy *px;
1183 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001184 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001185 int clrall = 0;
1186
1187 if (strcmp(args[2], "all") == 0)
1188 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001189
Willy Tarreau6162db22009-10-10 17:13:00 +02001190 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001191 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1192 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001193 appctx->ctx.cli.msg = stats_permission_denied_msg;
1194 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001195 return 1;
1196 }
1197
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001198 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001199 if (clrall) {
1200 memset(&px->be_counters, 0, sizeof(px->be_counters));
1201 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1202 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001203 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001204 px->be_counters.conn_max = 0;
1205 px->be_counters.p.http.rps_max = 0;
1206 px->be_counters.sps_max = 0;
1207 px->be_counters.cps_max = 0;
1208 px->be_counters.nbpend_max = 0;
1209
1210 px->fe_counters.conn_max = 0;
1211 px->fe_counters.p.http.rps_max = 0;
1212 px->fe_counters.sps_max = 0;
1213 px->fe_counters.cps_max = 0;
1214 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001215 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001216
1217 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001218 if (clrall)
1219 memset(&sv->counters, 0, sizeof(sv->counters));
1220 else {
1221 sv->counters.cur_sess_max = 0;
1222 sv->counters.nbpend_max = 0;
1223 sv->counters.sps_max = 0;
1224 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001225
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001226 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001227 if (li->counters) {
1228 if (clrall)
1229 memset(li->counters, 0, sizeof(*li->counters));
1230 else
1231 li->counters->conn_max = 0;
1232 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001233 }
1234
Willy Tarreau81c25d02011-09-07 15:17:21 +02001235 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001236 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001237 return 1;
1238 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001239 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001240 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001241 /* end of processing */
1242 return 1;
1243 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001244 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1245 /* Set ACL or MAP flags. */
1246 if (args[1][0] == 'm')
1247 appctx->ctx.map.display_flags = PAT_REF_MAP;
1248 else
1249 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001250
1251 /* no parameter */
1252 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001253 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1254 appctx->ctx.cli.msg = "Missing map identifier.\n";
1255 else
1256 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001257 appctx->st0 = STAT_CLI_PRINT;
1258 return 1;
1259 }
1260
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001261 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001262 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001263 if (!appctx->ctx.map.ref ||
1264 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1265 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001266 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001267 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001268 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001269 appctx->st0 = STAT_CLI_PRINT;
1270 return 1;
1271 }
1272
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001273 /* Clear all. */
1274 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001275
1276 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001277 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001278 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001279 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001280 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001281 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001282 return 0;
1283 }
1284 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001285 else if (strcmp(args[0], "get") == 0) {
1286 if (strcmp(args[1], "weight") == 0) {
1287 struct proxy *px;
1288 struct server *sv;
1289
1290 /* split "backend/server" and make <line> point to server */
1291 for (line = args[2]; *line; line++)
1292 if (*line == '/') {
1293 *line++ = '\0';
1294 break;
1295 }
1296
1297 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001298 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1299 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001300 return 1;
1301 }
1302
1303 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001304 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1305 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001306 return 1;
1307 }
1308
1309 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001310 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001311 if (bi_putstr(si_ic(si), trash.str) == -1)
1312 si->flags |= SI_FL_WAIT_ROOM;
1313
Willy Tarreau38338fa2009-10-10 18:37:29 +02001314 return 1;
1315 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001316 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1317 /* Set flags. */
1318 if (args[1][0] == 'm')
1319 appctx->ctx.map.display_flags = PAT_REF_MAP;
1320 else
1321 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001322
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001323 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001324 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001325 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1326 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1327 else
1328 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001329 appctx->st0 = STAT_CLI_PRINT;
1330 return 1;
1331 }
1332
1333 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001334 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001335 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001336 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001337 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001338 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001339 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001340 appctx->st0 = STAT_CLI_PRINT;
1341 return 1;
1342 }
1343
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001344 /* copy input string. The string must be allocated because
1345 * it may be used over multiple iterations. It's released
1346 * at the end and upon abort anyway.
1347 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001348 appctx->ctx.map.chunk.len = strlen(args[3]);
1349 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1350 appctx->ctx.map.chunk.str = strdup(args[3]);
1351 if (!appctx->ctx.map.chunk.str) {
1352 appctx->ctx.cli.msg = "Out of memory error.\n";
1353 appctx->st0 = STAT_CLI_PRINT;
1354 return 1;
1355 }
1356
1357 /* prepare response */
1358 appctx->st2 = STAT_ST_INIT;
1359 appctx->st0 = STAT_CLI_O_MLOOK;
1360 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001361 else { /* not "get weight" */
1362 return 0;
1363 }
1364 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001365 else if (strcmp(args[0], "set") == 0) {
1366 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001367 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001368 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001369
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001370 sv = expect_server_admin(s, si, args[2]);
1371 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001372 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001373
Simon Horman7d09b9a2013-02-12 10:45:51 +09001374 warning = server_parse_weight_change_request(sv, args[3]);
1375 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001376 appctx->ctx.cli.msg = warning;
1377 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001378 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001379 return 1;
1380 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001381 else if (strcmp(args[1], "server") == 0) {
1382 struct server *sv;
1383 const char *warning;
1384
1385 sv = expect_server_admin(s, si, args[2]);
1386 if (!sv)
1387 return 1;
1388
1389 if (strcmp(args[3], "weight") == 0) {
1390 warning = server_parse_weight_change_request(sv, args[4]);
1391 if (warning) {
1392 appctx->ctx.cli.msg = warning;
1393 appctx->st0 = STAT_CLI_PRINT;
1394 }
1395 }
1396 else if (strcmp(args[3], "state") == 0) {
1397 if (strcmp(args[4], "ready") == 0)
1398 srv_adm_set_ready(sv);
1399 else if (strcmp(args[4], "drain") == 0)
1400 srv_adm_set_drain(sv);
1401 else if (strcmp(args[4], "maint") == 0)
1402 srv_adm_set_maint(sv);
1403 else {
1404 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1405 appctx->st0 = STAT_CLI_PRINT;
1406 }
1407 }
1408 else if (strcmp(args[3], "health") == 0) {
1409 if (sv->track) {
1410 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1411 appctx->st0 = STAT_CLI_PRINT;
1412 }
1413 else if (strcmp(args[4], "up") == 0) {
1414 sv->check.health = sv->check.rise + sv->check.fall - 1;
1415 srv_set_running(sv, "changed from CLI");
1416 }
1417 else if (strcmp(args[4], "stopping") == 0) {
1418 sv->check.health = sv->check.rise + sv->check.fall - 1;
1419 srv_set_stopping(sv, "changed from CLI");
1420 }
1421 else if (strcmp(args[4], "down") == 0) {
1422 sv->check.health = 0;
1423 srv_set_stopped(sv, "changed from CLI");
1424 }
1425 else {
1426 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1427 appctx->st0 = STAT_CLI_PRINT;
1428 }
1429 }
1430 else if (strcmp(args[3], "agent") == 0) {
1431 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1432 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1433 appctx->st0 = STAT_CLI_PRINT;
1434 }
1435 else if (strcmp(args[4], "up") == 0) {
1436 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1437 srv_set_running(sv, "changed from CLI");
1438 }
1439 else if (strcmp(args[4], "down") == 0) {
1440 sv->agent.health = 0;
1441 srv_set_stopped(sv, "changed from CLI");
1442 }
1443 else {
1444 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1445 appctx->st0 = STAT_CLI_PRINT;
1446 }
1447 }
1448 else {
1449 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1450 appctx->st0 = STAT_CLI_PRINT;
1451 }
1452 return 1;
1453 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001454 else if (strcmp(args[1], "timeout") == 0) {
1455 if (strcmp(args[2], "cli") == 0) {
1456 unsigned timeout;
1457 const char *res;
1458
1459 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001460 appctx->ctx.cli.msg = "Expects an integer value.\n";
1461 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001462 return 1;
1463 }
1464
1465 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1466 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001467 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1468 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001469 return 1;
1470 }
1471
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001472 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001473 return 1;
1474 }
1475 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001476 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1477 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001478 return 1;
1479 }
1480 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001481 else if (strcmp(args[1], "maxconn") == 0) {
1482 if (strcmp(args[2], "frontend") == 0) {
1483 struct proxy *px;
1484 struct listener *l;
1485 int v;
1486
Willy Tarreau532a4502011-09-07 22:37:44 +02001487 px = expect_frontend_admin(s, si, args[3]);
1488 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001489 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001490
1491 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001492 appctx->ctx.cli.msg = "Integer value expected.\n";
1493 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001494 return 1;
1495 }
1496
1497 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001498 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001499 appctx->ctx.cli.msg = "Value out of range.\n";
1500 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001501 return 1;
1502 }
1503
1504 /* OK, the value is fine, so we assign it to the proxy and to all of
1505 * its listeners. The blocked ones will be dequeued.
1506 */
1507 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001508 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001509 l->maxconn = v;
1510 if (l->state == LI_FULL)
1511 resume_listener(l);
1512 }
1513
1514 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1515 dequeue_all_listeners(&s->fe->listener_queue);
1516
1517 return 1;
1518 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001519 else if (strcmp(args[2], "global") == 0) {
1520 int v;
1521
Willy Tarreau290e63a2012-09-20 18:07:14 +02001522 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001523 appctx->ctx.cli.msg = stats_permission_denied_msg;
1524 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001525 return 1;
1526 }
1527
1528 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001529 appctx->ctx.cli.msg = "Expects an integer value.\n";
1530 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001531 return 1;
1532 }
1533
1534 v = atoi(args[3]);
1535 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001536 appctx->ctx.cli.msg = "Value out of range.\n";
1537 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001538 return 1;
1539 }
1540
1541 /* check for unlimited values */
1542 if (v <= 0)
1543 v = global.hardmaxconn;
1544
1545 global.maxconn = v;
1546
1547 /* Dequeues all of the listeners waiting for a resource */
1548 if (!LIST_ISEMPTY(&global_listener_queue))
1549 dequeue_all_listeners(&global_listener_queue);
1550
1551 return 1;
1552 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001553 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001554 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1555 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001556 return 1;
1557 }
1558 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001559 else if (strcmp(args[1], "rate-limit") == 0) {
1560 if (strcmp(args[2], "connections") == 0) {
1561 if (strcmp(args[3], "global") == 0) {
1562 int v;
1563
Willy Tarreau290e63a2012-09-20 18:07:14 +02001564 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001565 appctx->ctx.cli.msg = stats_permission_denied_msg;
1566 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001567 return 1;
1568 }
1569
1570 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001571 appctx->ctx.cli.msg = "Expects an integer value.\n";
1572 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001573 return 1;
1574 }
1575
1576 v = atoi(args[4]);
1577 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001578 appctx->ctx.cli.msg = "Value out of range.\n";
1579 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001580 return 1;
1581 }
1582
1583 global.cps_lim = v;
1584
1585 /* Dequeues all of the listeners waiting for a resource */
1586 if (!LIST_ISEMPTY(&global_listener_queue))
1587 dequeue_all_listeners(&global_listener_queue);
1588
1589 return 1;
1590 }
1591 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001592 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1593 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001594 return 1;
1595 }
1596 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001597 else if (strcmp(args[2], "sessions") == 0) {
1598 if (strcmp(args[3], "global") == 0) {
1599 int v;
1600
1601 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1602 appctx->ctx.cli.msg = stats_permission_denied_msg;
1603 appctx->st0 = STAT_CLI_PRINT;
1604 return 1;
1605 }
1606
1607 if (!*args[4]) {
1608 appctx->ctx.cli.msg = "Expects an integer value.\n";
1609 appctx->st0 = STAT_CLI_PRINT;
1610 return 1;
1611 }
1612
1613 v = atoi(args[4]);
1614 if (v < 0) {
1615 appctx->ctx.cli.msg = "Value out of range.\n";
1616 appctx->st0 = STAT_CLI_PRINT;
1617 return 1;
1618 }
1619
1620 global.sps_lim = v;
1621
1622 /* Dequeues all of the listeners waiting for a resource */
1623 if (!LIST_ISEMPTY(&global_listener_queue))
1624 dequeue_all_listeners(&global_listener_queue);
1625
1626 return 1;
1627 }
1628 else {
1629 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1630 appctx->st0 = STAT_CLI_PRINT;
1631 return 1;
1632 }
1633 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001634#ifdef USE_OPENSSL
1635 else if (strcmp(args[2], "ssl-sessions") == 0) {
1636 if (strcmp(args[3], "global") == 0) {
1637 int v;
1638
1639 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1640 appctx->ctx.cli.msg = stats_permission_denied_msg;
1641 appctx->st0 = STAT_CLI_PRINT;
1642 return 1;
1643 }
1644
1645 if (!*args[4]) {
1646 appctx->ctx.cli.msg = "Expects an integer value.\n";
1647 appctx->st0 = STAT_CLI_PRINT;
1648 return 1;
1649 }
1650
1651 v = atoi(args[4]);
1652 if (v < 0) {
1653 appctx->ctx.cli.msg = "Value out of range.\n";
1654 appctx->st0 = STAT_CLI_PRINT;
1655 return 1;
1656 }
1657
1658 global.ssl_lim = v;
1659
1660 /* Dequeues all of the listeners waiting for a resource */
1661 if (!LIST_ISEMPTY(&global_listener_queue))
1662 dequeue_all_listeners(&global_listener_queue);
1663
1664 return 1;
1665 }
1666 else {
1667 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1668 appctx->st0 = STAT_CLI_PRINT;
1669 return 1;
1670 }
1671 }
1672#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001673 else if (strcmp(args[2], "http-compression") == 0) {
1674 if (strcmp(args[3], "global") == 0) {
1675 int v;
1676
Willy Tarreau85d47f92012-11-21 00:29:50 +01001677 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001678 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1679 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001680 return 1;
1681 }
1682
William Lallemandd85f9172012-11-09 17:05:39 +01001683 v = atoi(args[4]);
1684 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1685 }
1686 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001687 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1688 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001689 return 1;
1690 }
1691 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001692 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001693 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001694 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001695 return 1;
1696 }
1697 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001698 else if (strcmp(args[1], "table") == 0) {
1699 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1700 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001701 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001702 char *err;
1703
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001704 /* Set flags. */
1705 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001706
1707 /* Expect three parameters: map name, key and new value. */
1708 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001709 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001710 appctx->st0 = STAT_CLI_PRINT;
1711 return 1;
1712 }
1713
1714 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001715 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001716 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001717 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001718 appctx->st0 = STAT_CLI_PRINT;
1719 return 1;
1720 }
1721
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001722 /* If the entry identifier start with a '#', it is considered as
1723 * pointer id
1724 */
1725 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1726 struct pat_ref_elt *ref;
1727 long long int conv;
1728 char *error;
1729
1730 /* Convert argument to integer value. */
1731 conv = strtoll(&args[3][1], &error, 16);
1732 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001733 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001734 appctx->st0 = STAT_CLI_PRINT;
1735 return 1;
1736 }
1737
1738 /* Convert and check integer to pointer. */
1739 ref = (struct pat_ref_elt *)(long)conv;
1740 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001741 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001742 appctx->st0 = STAT_CLI_PRINT;
1743 return 1;
1744 }
1745
1746 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001747 err = NULL;
1748 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1749 if (err)
1750 memprintf(&err, "%s.\n", err);
1751 appctx->ctx.cli.err = err;
1752 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001753 return 1;
1754 }
1755 }
1756 else {
1757 /* Else, use the entry identifier as pattern
1758 * string, and update the value.
1759 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001760 err = NULL;
1761 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1762 if (err)
1763 memprintf(&err, "%s.\n", err);
1764 appctx->ctx.cli.err = err;
1765 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001766 return 1;
1767 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001768 }
1769
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001770 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001771 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001772 return 1;
1773 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001774#ifdef USE_OPENSSL
1775 else if (strcmp(args[1], "ssl") == 0) {
1776 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01001777#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001778 char *err = NULL;
1779
Emeric Brunaf4ef742014-06-19 14:10:45 +02001780 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001781 if (!*args[3]) {
1782 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
1783 appctx->st0 = STAT_CLI_PRINT;
1784 return 1;
1785 }
1786
1787 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
1788 if (trash.len < 0) {
1789 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
1790 appctx->st0 = STAT_CLI_PRINT;
1791 return 1;
1792 }
1793
1794 if (ssl_sock_update_ocsp_response(&trash, &err)) {
1795 if (err) {
1796 memprintf(&err, "%s.\n", err);
1797 appctx->ctx.cli.err = err;
1798 appctx->st0 = STAT_CLI_PRINT_FREE;
1799 }
1800 return 1;
1801 }
1802 appctx->ctx.cli.msg = "OCSP Response updated!";
1803 appctx->st0 = STAT_CLI_PRINT;
1804 return 1;
1805#else
1806 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
1807 appctx->st0 = STAT_CLI_PRINT;
1808 return 1;
1809#endif
1810 }
1811 else {
1812 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
1813 appctx->st0 = STAT_CLI_PRINT;
1814 return 1;
1815 }
1816 }
1817#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01001818 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001819 return 0;
1820 }
1821 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001822 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001823 if (strcmp(args[1], "agent") == 0) {
1824 struct server *sv;
1825
1826 sv = expect_server_admin(s, si, args[2]);
1827 if (!sv)
1828 return 1;
1829
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001830 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1831 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1832 appctx->st0 = STAT_CLI_PRINT;
1833 return 1;
1834 }
1835
1836 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001837 return 1;
1838 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001839 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001840 struct server *sv;
1841
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001842 sv = expect_server_admin(s, si, args[2]);
1843 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001844 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001845
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001846 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1847 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1848 appctx->st0 = STAT_CLI_PRINT;
1849 return 1;
1850 }
1851
1852 sv->check.state |= CHK_ST_ENABLED;
1853 return 1;
1854 }
1855 else if (strcmp(args[1], "server") == 0) {
1856 struct server *sv;
1857
1858 sv = expect_server_admin(s, si, args[2]);
1859 if (!sv)
1860 return 1;
1861
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001862 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001863 return 1;
1864 }
1865 else if (strcmp(args[1], "frontend") == 0) {
1866 struct proxy *px;
1867
1868 px = expect_frontend_admin(s, si, args[2]);
1869 if (!px)
1870 return 1;
1871
1872 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001873 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1874 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001875 return 1;
1876 }
1877
1878 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001879 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1880 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001881 return 1;
1882 }
1883
1884 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001885 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1886 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001887 return 1;
1888 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001889 return 1;
1890 }
1891 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001892 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001893 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001894 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001895 }
1896 }
1897 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001898 if (strcmp(args[1], "agent") == 0) {
1899 struct server *sv;
1900
1901 sv = expect_server_admin(s, si, args[2]);
1902 if (!sv)
1903 return 1;
1904
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001905 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001906 return 1;
1907 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001908 else if (strcmp(args[1], "health") == 0) {
1909 struct server *sv;
1910
1911 sv = expect_server_admin(s, si, args[2]);
1912 if (!sv)
1913 return 1;
1914
1915 sv->check.state &= ~CHK_ST_ENABLED;
1916 return 1;
1917 }
Simon Horman671b6f02013-11-25 10:46:39 +09001918 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001919 struct server *sv;
1920
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001921 sv = expect_server_admin(s, si, args[2]);
1922 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001923 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001924
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001925 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001926 return 1;
1927 }
1928 else if (strcmp(args[1], "frontend") == 0) {
1929 struct proxy *px;
1930
1931 px = expect_frontend_admin(s, si, args[2]);
1932 if (!px)
1933 return 1;
1934
1935 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001936 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1937 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001938 return 1;
1939 }
1940
1941 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001942 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1943 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001944 return 1;
1945 }
1946
1947 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001948 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1949 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001950 return 1;
1951 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001952 return 1;
1953 }
1954 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001955 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001956 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001957 return 1;
1958 }
1959 }
1960 else if (strcmp(args[0], "shutdown") == 0) {
1961 if (strcmp(args[1], "frontend") == 0) {
1962 struct proxy *px;
1963
1964 px = expect_frontend_admin(s, si, args[2]);
1965 if (!px)
1966 return 1;
1967
1968 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001969 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1970 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001971 return 1;
1972 }
1973
1974 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1975 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1976 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1977 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1978 stop_proxy(px);
1979 return 1;
1980 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001981 else if (strcmp(args[1], "session") == 0) {
1982 struct session *sess, *ptr;
1983
Willy Tarreau290e63a2012-09-20 18:07:14 +02001984 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001985 appctx->ctx.cli.msg = stats_permission_denied_msg;
1986 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001987 return 1;
1988 }
1989
1990 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001991 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1992 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001993 return 1;
1994 }
1995
1996 ptr = (void *)strtoul(args[2], NULL, 0);
1997
1998 /* first, look for the requested session in the session table */
1999 list_for_each_entry(sess, &sessions, list) {
2000 if (sess == ptr)
2001 break;
2002 }
2003
2004 /* do we have the session ? */
2005 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002006 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2007 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002008 return 1;
2009 }
2010
2011 session_shutdown(sess, SN_ERR_KILLED);
2012 return 1;
2013 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002014 else if (strcmp(args[1], "sessions") == 0) {
2015 if (strcmp(args[2], "server") == 0) {
2016 struct server *sv;
2017 struct session *sess, *sess_bck;
2018
2019 sv = expect_server_admin(s, si, args[3]);
2020 if (!sv)
2021 return 1;
2022
2023 /* kill all the session that are on this server */
2024 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2025 if (sess->srv_conn == sv)
2026 session_shutdown(sess, SN_ERR_KILLED);
2027
2028 return 1;
2029 }
2030 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002031 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2032 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002033 return 1;
2034 }
2035 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002036 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002037 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2038 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002039 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002040 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002041 }
2042 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002043 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2044 if (args[1][0] == 'm')
2045 appctx->ctx.map.display_flags = PAT_REF_MAP;
2046 else
2047 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002048
2049 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002050 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2051 if (!*args[2] || !*args[3]) {
2052 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2053 appctx->st0 = STAT_CLI_PRINT;
2054 return 1;
2055 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002056 }
2057
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002058 else {
2059 if (!*args[2] || !*args[3]) {
2060 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2061 appctx->st0 = STAT_CLI_PRINT;
2062 return 1;
2063 }
2064 }
2065
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002066 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002067 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002068 if (!appctx->ctx.map.ref ||
2069 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002070 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002071 appctx->st0 = STAT_CLI_PRINT;
2072 return 1;
2073 }
2074
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002075 /* If the entry identifier start with a '#', it is considered as
2076 * pointer id
2077 */
2078 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2079 struct pat_ref_elt *ref;
2080 long long int conv;
2081 char *error;
2082
2083 /* Convert argument to integer value. */
2084 conv = strtoll(&args[3][1], &error, 16);
2085 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002086 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002087 appctx->st0 = STAT_CLI_PRINT;
2088 return 1;
2089 }
2090
2091 /* Convert and check integer to pointer. */
2092 ref = (struct pat_ref_elt *)(long)conv;
2093 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002094 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002095 appctx->st0 = STAT_CLI_PRINT;
2096 return 1;
2097 }
2098
2099 /* Try to delete the entry. */
2100 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2101 /* The entry is not found, send message. */
2102 appctx->ctx.cli.msg = "Key not found.\n";
2103 appctx->st0 = STAT_CLI_PRINT;
2104 return 1;
2105 }
2106 }
2107 else {
2108 /* Else, use the entry identifier as pattern
2109 * string and try to delete the entry.
2110 */
2111 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2112 /* The entry is not found, send message. */
2113 appctx->ctx.cli.msg = "Key not found.\n";
2114 appctx->st0 = STAT_CLI_PRINT;
2115 return 1;
2116 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002117 }
2118
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002119 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002120 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002121 return 1;
2122 }
2123 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002124 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002125 appctx->st0 = STAT_CLI_PRINT;
2126 return 1;
2127 }
2128 }
2129 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002130 if (strcmp(args[1], "map") == 0 ||
2131 strcmp(args[1], "acl") == 0) {
2132 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002133 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002134
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002135 /* Set flags. */
2136 if (args[1][0] == 'm')
2137 appctx->ctx.map.display_flags = PAT_REF_MAP;
2138 else
2139 appctx->ctx.map.display_flags = PAT_REF_ACL;
2140
2141 /* If the keywork is "map", we expect three parameters, if it
2142 * is "acl", we expect only two parameters
2143 */
2144 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2145 if (!*args[2] || !*args[3] || !*args[4]) {
2146 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2147 appctx->st0 = STAT_CLI_PRINT;
2148 return 1;
2149 }
2150 }
2151 else {
2152 if (!*args[2] || !*args[3]) {
2153 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2154 appctx->st0 = STAT_CLI_PRINT;
2155 return 1;
2156 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002157 }
2158
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002159 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002160 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002161 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002162 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002163 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002164 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002165 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002166 appctx->st0 = STAT_CLI_PRINT;
2167 return 1;
2168 }
2169
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002170 /* The command "add acl" is prohibited if the reference
2171 * use samples.
2172 */
2173 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2174 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2175 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2176 "You must use the command 'add map' to add values.\n";
2177 appctx->st0 = STAT_CLI_PRINT;
2178 return 1;
2179 }
2180
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002181 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002182 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002183 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002184 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002185 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002186 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002187 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002188 if (err)
2189 memprintf(&err, "%s.\n", err);
2190 appctx->ctx.cli.err = err;
2191 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002192 return 1;
2193 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002194
2195 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002196 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002197 return 1;
2198 }
2199 else { /* unknown "del" parameter */
2200 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2201 appctx->st0 = STAT_CLI_PRINT;
2202 return 1;
2203 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002204 }
2205 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002206 return 0;
2207 }
2208 return 1;
2209}
2210
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002211/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002212 * used to processes I/O from/to the stats unix socket. The system relies on a
2213 * state machine handling requests and various responses. We read a request,
2214 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002215 * Then we can read again. The state is stored in appctx->st0 and is one of the
2216 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002217 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002218 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002219static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002220{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002221 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002222 struct channel *req = si_oc(si);
2223 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002224 int reql;
2225 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002226
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002227 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2228 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002229
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002230 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002231 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002232 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002233 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2234 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002235 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002236 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002237 /* Let's close for real now. We just close the request
2238 * side, the conditions below will complete if needed.
2239 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002240 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002241 break;
2242 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002243 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002244 /* ensure we have some output room left in the event we
2245 * would want to return some info right after parsing.
2246 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002247 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002248 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002249 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002250 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002251
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002252 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002253 if (reql <= 0) { /* closed or EOL not found */
2254 if (reql == 0)
2255 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002256 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002257 continue;
2258 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002259
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002260 /* seek for a possible semi-colon. If we find one, we
2261 * replace it with an LF and skip only this part.
2262 */
2263 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002264 if (trash.str[len] == ';') {
2265 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002266 reql = len + 1;
2267 break;
2268 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002269
Willy Tarreau816fc222009-10-04 07:36:58 +02002270 /* now it is time to check that we have a full line,
2271 * remove the trailing \n and possibly \r, then cut the
2272 * line.
2273 */
2274 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002275 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002276 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002277 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002278 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002279
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002280 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002281 len--;
2282
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002283 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002284
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002285 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002286 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002287 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002288 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002289 continue;
2290 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002291 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002292 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002293 else if (strcmp(trash.str, "help") == 0 ||
2294 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002295 appctx->ctx.cli.msg = stats_sock_usage_msg;
2296 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002297 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002298 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002299 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002300 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002301 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002302 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002303 /* if prompt is disabled, print help on empty lines,
2304 * so that the user at least knows how to enable
2305 * prompt and find help.
2306 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002307 appctx->ctx.cli.msg = stats_sock_usage_msg;
2308 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002309 }
2310
2311 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002312 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002313 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002314 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002315 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002316 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau44cf5452014-10-22 19:06:31 +02002317 cli_release_handler(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002318 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002319 continue;
2320 }
2321
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002322 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002323 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002324 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002325 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002326 else
2327 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002328 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002329 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002330 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002331 free(appctx->ctx.cli.err);
2332 appctx->st0 = STAT_CLI_PROMPT;
2333 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002334 else
2335 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002336 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002337 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002338 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002339 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002340 break;
2341 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002342 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002343 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002344 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002345 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002346 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002347 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002348 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002349 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002350 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002351 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002352 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002353 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002354 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002355 if (stats_table_request(si, appctx->st0))
2356 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002357 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002358 case STAT_CLI_O_PATS:
2359 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002360 appctx->st0 = STAT_CLI_PROMPT;
2361 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002362 case STAT_CLI_O_PAT:
2363 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002364 appctx->st0 = STAT_CLI_PROMPT;
2365 break;
2366 case STAT_CLI_O_MLOOK:
2367 if (stats_map_lookup(si))
2368 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002369 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002370 case STAT_CLI_O_POOLS:
2371 if (stats_dump_pools_to_buffer(si))
2372 appctx->st0 = STAT_CLI_PROMPT;
2373 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002374 default: /* abnormal state */
Willy Tarreau44cf5452014-10-22 19:06:31 +02002375 cli_release_handler(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002376 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002377 break;
2378 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002379
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002380 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002381 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002382 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002383 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002384 else
2385 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002386 }
2387
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002388 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002389 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002390 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002391
2392 /* Now we close the output if one of the writers did so,
2393 * or if we're not in interactive mode and the request
2394 * buffer is empty. This still allows pipelined requests
2395 * to be sent in non-interactive mode.
2396 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002397 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2398 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002399 continue;
2400 }
2401
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002402 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002403 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002404 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002405 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002406
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002407 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002408 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2409 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002410 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002411 * and nothing more to consume. This is comparable to a broken pipe, so
2412 * we forward the close to the request side so that it flows upstream to
2413 * the client.
2414 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002415 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002416 }
2417
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002418 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002419 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2420 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2421 /* We have no more processing to do, and nothing more to send, and
2422 * the client side has closed. So we'll forward this state downstream
2423 * on the response buffer.
2424 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002425 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002426 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002427 }
2428
2429 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002430 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002431
2432 /* we don't want to expire timeouts while we're processing requests */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002433 si_ic(si)->rex = TICK_ETERNITY;
2434 si_oc(si)->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002435
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002436 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002437 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 +02002438 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002439 si->state, req->flags, res->flags, req->buf->i, req->buf->o, res->buf->i, res->buf->o);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002440
2441 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2442 /* check that we have released everything then unregister */
2443 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002444 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002445}
2446
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002447/* This function dumps information onto the stream interface's read buffer.
2448 * It returns 0 as long as it does not complete, non-zero upon completion.
2449 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002450 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002451static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002452{
2453 unsigned int up = (now.tv_sec - start_date.tv_sec);
2454
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002455#ifdef USE_OPENSSL
2456 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2457 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2458 int ssl_reuse = 0;
2459
2460 if (ssl_key_rate < ssl_sess_rate) {
2461 /* count the ssl reuse ratio and avoid overflows in both directions */
2462 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2463 }
2464#endif
2465
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002466 chunk_printf(&trash,
2467 "Name: " PRODUCT_NAME "\n"
2468 "Version: " HAPROXY_VERSION "\n"
2469 "Release_date: " HAPROXY_DATE "\n"
2470 "Nbproc: %d\n"
2471 "Process_num: %d\n"
2472 "Pid: %d\n"
2473 "Uptime: %dd %dh%02dm%02ds\n"
2474 "Uptime_sec: %d\n"
2475 "Memmax_MB: %d\n"
2476 "Ulimit-n: %d\n"
2477 "Maxsock: %d\n"
2478 "Maxconn: %d\n"
2479 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002480 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002481 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002482 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002483#ifdef USE_OPENSSL
2484 "MaxSslConns: %d\n"
2485 "CurrSslConns: %d\n"
2486 "CumSslConns: %d\n"
2487#endif
2488 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002489 "PipesUsed: %d\n"
2490 "PipesFree: %d\n"
2491 "ConnRate: %d\n"
2492 "ConnRateLimit: %d\n"
2493 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002494 "SessRate: %d\n"
2495 "SessRateLimit: %d\n"
2496 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002497#ifdef USE_OPENSSL
2498 "SslRate: %d\n"
2499 "SslRateLimit: %d\n"
2500 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002501 "SslFrontendKeyRate: %d\n"
2502 "SslFrontendMaxKeyRate: %d\n"
2503 "SslFrontendSessionReuse_pct: %d\n"
2504 "SslBackendKeyRate: %d\n"
2505 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002506 "SslCacheLookups: %u\n"
2507 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002508#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002509 "CompressBpsIn: %u\n"
2510 "CompressBpsOut: %u\n"
2511 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002512#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002513 "ZlibMemUsage: %ld\n"
2514 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002515#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002516 "Tasks: %d\n"
2517 "Run_queue: %d\n"
2518 "Idle_pct: %d\n"
2519 "node: %s\n"
2520 "description: %s\n"
2521 "",
2522 global.nbproc,
2523 relative_pid,
2524 pid,
2525 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2526 up,
2527 global.rlimit_memmax,
2528 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002529 global.maxsock, global.maxconn, global.hardmaxconn,
2530 actconn, totalconn, global.req_count,
2531#ifdef USE_OPENSSL
2532 global.maxsslconn, sslconns, totalsslconns,
2533#endif
2534 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002535 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002536 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002537#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002538 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2539 ssl_key_rate, global.ssl_fe_keys_max,
2540 ssl_reuse,
2541 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002542 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002543#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002544 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2545 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002546#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002547 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002548#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002549 nb_tasks_cur, run_queue_cur, idle_pct,
2550 global.node, global.desc ? global.desc : ""
2551 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002552
Willy Tarreaubc18da12015-03-13 14:00:47 +01002553 if (bi_putchk(si_ic(si), &trash) == -1) {
2554 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002555 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002556 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002557
2558 return 1;
2559}
2560
Willy Tarreau12833bb2014-01-28 16:49:56 +01002561/* This function dumps memory usage information onto the stream interface's
2562 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2563 * completion. No state is used.
2564 */
2565static int stats_dump_pools_to_buffer(struct stream_interface *si)
2566{
2567 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01002568 if (bi_putchk(si_ic(si), &trash) == -1) {
2569 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002570 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002571 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01002572 return 1;
2573}
2574
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002575/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2576 * the state from stream interface <si>. The caller is responsible for clearing
2577 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002578 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002579static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002580{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002581 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002582 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002583
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002584 if (!(px->cap & PR_CAP_FE))
2585 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002587 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002588 return 0;
2589
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002590 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002591 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002592 /* name, queue */
2593 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002594
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002595 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002596 /* Column sub-heading for Enable or Disable server */
2597 chunk_appendf(&trash, "<td></td>");
2598 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002599
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002600 chunk_appendf(&trash,
2601 "<td class=ac>"
2602 "<a name=\"%s/Frontend\"></a>"
2603 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2604 "<td colspan=3></td>"
2605 "",
2606 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002607
Willy Tarreau466c9b52012-12-23 02:25:03 +01002608 chunk_appendf(&trash,
2609 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002610 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002611 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2612 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2613 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002614 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2615 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2616 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002617
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002618 if (px->mode == PR_MODE_HTTP)
2619 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002620 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002621 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002622
2623 chunk_appendf(&trash,
2624 "</table></div></u></td>"
2625 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002626 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002627 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2628 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2629 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002630 U2H(px->fe_counters.sps_max),
2631 U2H(px->fe_counters.cps_max),
2632 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002633
2634 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002635 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002636 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002637 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002638
2639 chunk_appendf(&trash,
2640 "</table></div></u></td>"
2641 /* sessions rate : limit */
2642 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002643 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002644
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002645 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002646 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002647 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002648 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002649 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2650 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002651 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002652 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2653 U2H(px->fe_counters.cum_sess),
2654 U2H(px->fe_counters.cum_conn),
2655 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002656
Willy Tarreau466c9b52012-12-23 02:25:03 +01002657 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002658 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002659 chunk_appendf(&trash,
2660 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2661 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2662 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2663 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2664 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2665 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2666 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2667 "<tr><th>- other responses:</th><td>%s</td></tr>"
2668 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2669 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002670 U2H(px->fe_counters.p.http.cum_req),
2671 U2H(px->fe_counters.p.http.rsp[1]),
2672 U2H(px->fe_counters.p.http.rsp[2]),
2673 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002674 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002675 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002676 U2H(px->fe_counters.p.http.rsp[3]),
2677 U2H(px->fe_counters.p.http.rsp[4]),
2678 U2H(px->fe_counters.p.http.rsp[5]),
2679 U2H(px->fe_counters.p.http.rsp[0]),
2680 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002681 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002682
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002683 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002684 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002685 /* sessions: lbtot, lastsess */
2686 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002687 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002688 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002689 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002690 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002691
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002692 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002693 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002694 "<td>%s%s<div class=tips>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002695 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002696 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002697 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2698 px->fe_counters.comp_in ?
2699 (int)((px->fe_counters.comp_in - px->fe_counters.comp_out)*100/px->fe_counters.comp_in) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002700 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002701
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002702 chunk_appendf(&trash,
2703 /* denied: req, resp */
2704 "<td>%s</td><td>%s</td>"
2705 /* errors : request, connect, response */
2706 "<td>%s</td><td></td><td></td>"
2707 /* warnings: retries, redispatches */
2708 "<td></td><td></td>"
2709 /* server status : reflect frontend status */
2710 "<td class=ac>%s</td>"
2711 /* rest of server: nothing */
2712 "<td class=ac colspan=8></td></tr>"
2713 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002714 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2715 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002716 px->state == PR_STREADY ? "OPEN" :
2717 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002718 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002719 else { /* CSV mode */
2720 chunk_appendf(&trash,
2721 /* pxid, name, queue cur, queue max, */
2722 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002723 /* sessions : current, max, limit, total */
2724 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002725 /* bytes : in, out */
2726 "%lld,%lld,"
2727 /* denied: req, resp */
2728 "%lld,%lld,"
2729 /* errors : request, connect, response */
2730 "%lld,,,"
2731 /* warnings: retries, redispatches */
2732 ",,"
2733 /* server status : reflect frontend status */
2734 "%s,"
2735 /* rest of server: nothing */
2736 ",,,,,,,,"
2737 /* pid, iid, sid, throttle, lbtot, tracked, type */
2738 "%d,%d,0,,,,%d,"
2739 /* rate, rate_lim, rate_max */
2740 "%u,%u,%u,"
2741 /* check_status, check_code, check_duration */
2742 ",,,",
2743 px->id,
2744 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2745 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2746 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2747 px->fe_counters.failed_req,
2748 px->state == PR_STREADY ? "OPEN" :
2749 px->state == PR_STFULL ? "FULL" : "STOP",
2750 relative_pid, px->uuid, STATS_TYPE_FE,
2751 read_freq_ctr(&px->fe_sess_per_sec),
2752 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002753
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002754 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2755 if (px->mode == PR_MODE_HTTP) {
2756 for (i=1; i<6; i++)
2757 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2758 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2759 }
2760 else
2761 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002762
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002763 /* failed health analyses */
2764 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002765
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002766 /* requests : req_rate, req_rate_max, req_tot, */
2767 chunk_appendf(&trash, "%u,%u,%lld,",
2768 read_freq_ctr(&px->fe_req_per_sec),
2769 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2770
2771 /* errors: cli_aborts, srv_aborts */
2772 chunk_appendf(&trash, ",,");
2773
2774 /* compression: in, out, bypassed */
2775 chunk_appendf(&trash, "%lld,%lld,%lld,",
2776 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2777
2778 /* compression: comp_rsp */
2779 chunk_appendf(&trash, "%lld,",
2780 px->fe_counters.p.http.comp_rsp);
2781
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002782 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2783 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002784
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002785 /* finish with EOL */
2786 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002787 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002788 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002789}
2790
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002791/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2792 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2793 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2794 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002795 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002796static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002797{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002798 struct appctx *appctx = __objt_appctx(si->end);
2799
2800 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002801 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002802 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002803 /* Column sub-heading for Enable or Disable server */
2804 chunk_appendf(&trash, "<td></td>");
2805 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002806 chunk_appendf(&trash,
2807 /* frontend name, listener name */
2808 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2809 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2810 "",
2811 px->id, l->name,
2812 (flags & ST_SHLGNDS)?"<u>":"",
2813 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002814
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002815 if (flags & ST_SHLGNDS) {
2816 char str[INET6_ADDRSTRLEN];
2817 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002818
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002819 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002820
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002821 port = get_host_port(&l->addr);
2822 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2823 case AF_INET:
2824 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2825 break;
2826 case AF_INET6:
2827 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2828 break;
2829 case AF_UNIX:
2830 chunk_appendf(&trash, "unix, ");
2831 break;
2832 case -1:
2833 chunk_appendf(&trash, "(%s), ", strerror(errno));
2834 break;
2835 }
Willy Tarreau91861262007-10-17 17:06:05 +02002836
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002837 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002838 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002839 }
Willy Tarreau91861262007-10-17 17:06:05 +02002840
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002841 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002842 /* queue */
2843 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002844 /* sessions rate: current, max, limit */
2845 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002846 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002847 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002848 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002849 /* bytes: in, out */
2850 "<td>%s</td><td>%s</td>"
2851 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002852 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002853 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2854 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002855
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002856 chunk_appendf(&trash,
2857 /* denied: req, resp */
2858 "<td>%s</td><td>%s</td>"
2859 /* errors: request, connect, response */
2860 "<td>%s</td><td></td><td></td>"
2861 /* warnings: retries, redispatches */
2862 "<td></td><td></td>"
2863 /* server status: reflect listener status */
2864 "<td class=ac>%s</td>"
2865 /* rest of server: nothing */
2866 "<td class=ac colspan=8></td></tr>"
2867 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002868 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2869 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002870 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2871 }
2872 else { /* CSV mode */
2873 chunk_appendf(&trash,
2874 /* pxid, name, queue cur, queue max, */
2875 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002876 /* sessions: current, max, limit, total */
2877 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002878 /* bytes: in, out */
2879 "%lld,%lld,"
2880 /* denied: req, resp */
2881 "%lld,%lld,"
2882 /* errors: request, connect, response */
2883 "%lld,,,"
2884 /* warnings: retries, redispatches */
2885 ",,"
2886 /* server status: reflect listener status */
2887 "%s,"
2888 /* rest of server: nothing */
2889 ",,,,,,,,"
2890 /* pid, iid, sid, throttle, lbtot, tracked, type */
2891 "%d,%d,%d,,,,%d,"
2892 /* rate, rate_lim, rate_max */
2893 ",,,"
2894 /* check_status, check_code, check_duration */
2895 ",,,"
2896 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2897 ",,,,,,"
2898 /* failed health analyses */
2899 ","
2900 /* requests : req_rate, req_rate_max, req_tot, */
2901 ",,,"
2902 /* errors: cli_aborts, srv_aborts */
2903 ",,"
2904 /* compression: in, out, bypassed, comp_rsp */
2905 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02002906 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
2907 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002908 "\n",
2909 px->id, l->name,
2910 l->nbconn, l->counters->conn_max,
2911 l->maxconn, l->counters->cum_conn,
2912 l->counters->bytes_in, l->counters->bytes_out,
2913 l->counters->denied_req, l->counters->denied_resp,
2914 l->counters->failed_req,
2915 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2916 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2917 }
2918 return 1;
2919}
Willy Tarreau91861262007-10-17 17:06:05 +02002920
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002921/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2922 * from stream interface <si>, stats flags <flags>, and server state <state>.
2923 * The caller is responsible for clearing the trash if needed. Returns non-zero
2924 * if it emits anything, zero otherwise. The <state> parameter can take the
Willy Tarreau9638efa2014-05-23 11:19:57 +02002925 * following values : 0=DOWN, 1=DOWN(agent) 2=going up, 3=going down, 4=UP, 5,6=NOLB,
2926 * 7,8=DRAIN, 9=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002927 */
2928static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2929{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002930 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002931 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002932 char str[INET6_ADDRSTRLEN];
2933 struct chunk src;
2934 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002935
Willy Tarreau32091232014-05-16 13:52:00 +02002936 /* we have "via" which is the tracked server as described in the configuration,
2937 * and "ref" which is the checked server and the end of the chain.
2938 */
2939 via = sv->track ? sv->track : sv;
2940 ref = via;
2941 while (ref->track)
2942 ref = ref->track;
2943
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002944 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02002945 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002946 "DOWN",
Willy Tarreau9638efa2014-05-23 11:19:57 +02002947 "DOWN (agent)",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002948 "DN %d/%d &uarr;",
2949 "UP %d/%d &darr;",
2950 "UP",
2951 "NOLB %d/%d &darr;",
2952 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002953 "DRAIN %d/%d &darr;",
2954 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002955 "<i>no check</i>"
2956 };
Willy Tarreau91861262007-10-17 17:06:05 +02002957
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002958 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002959 chunk_appendf(&trash, "<tr class=\"maintain\">");
2960 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002961 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002962 "<tr class=\"%s%d\">",
Willy Tarreauc93cd162014-05-13 15:54:22 +02002963 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002964
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002965 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002966 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002967 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2968 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002969
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002970 chunk_appendf(&trash,
2971 "<td class=ac><a name=\"%s/%s\"></a>%s"
2972 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2973 "",
2974 px->id, sv->id,
2975 (flags & ST_SHLGNDS) ? "<u>" : "",
2976 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002977
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002978 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002979 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002980
2981 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2982 case AF_INET:
2983 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2984 break;
2985 case AF_INET6:
2986 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2987 break;
2988 case AF_UNIX:
2989 chunk_appendf(&trash, "unix, ");
2990 break;
2991 case -1:
2992 chunk_appendf(&trash, "(%s), ", strerror(errno));
2993 break;
2994 default: /* address family not supported */
2995 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002996 }
Willy Tarreau91861262007-10-17 17:06:05 +02002997
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002998 /* id */
2999 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003000
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003001 /* cookie */
3002 if (sv->cookie) {
3003 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003004
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003005 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3006 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003007
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003008 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003009 }
3010
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003011 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003012 }
Willy Tarreau91861262007-10-17 17:06:05 +02003013
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003014 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003015 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003016 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003017 /* sessions rate : current, max, limit */
3018 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003019 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003020 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003021 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3022 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003023
Willy Tarreau466c9b52012-12-23 02:25:03 +01003024
3025 chunk_appendf(&trash,
3026 /* sessions: current, max, limit, total */
3027 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003028 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003029 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3030 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003031 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3032 U2H(sv->counters.cum_sess),
3033 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003034
Willy Tarreau466c9b52012-12-23 02:25:03 +01003035 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3036 if (px->mode == PR_MODE_HTTP) {
3037 unsigned long long tot;
3038 for (tot = i = 0; i < 6; i++)
3039 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003040
Willy Tarreau466c9b52012-12-23 02:25:03 +01003041 chunk_appendf(&trash,
3042 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3043 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3044 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3045 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3046 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3047 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3048 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3049 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003050 U2H(tot),
3051 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3052 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3053 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3054 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3055 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3056 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 +02003057 }
Willy Tarreau91861262007-10-17 17:06:05 +02003058
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003059 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3060 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)));
3061 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)));
3062 if (px->mode == PR_MODE_HTTP)
3063 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)));
3064 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)));
3065
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003066 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003067 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003068 /* sessions: lbtot, last */
3069 "<td>%s</td><td>%s</td>",
3070 U2H(sv->counters.cum_lbconn),
3071 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003072
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003073 chunk_appendf(&trash,
3074 /* bytes : in, out */
3075 "<td>%s</td><td>%s</td>"
3076 /* denied: req, resp */
3077 "<td></td><td>%s</td>"
3078 /* errors : request, connect */
3079 "<td></td><td>%s</td>"
3080 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003081 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003082 /* warnings: retries, redispatches */
3083 "<td>%lld</td><td>%lld</td>"
3084 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003085 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3086 U2H(sv->counters.failed_secu),
3087 U2H(sv->counters.failed_conns),
3088 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003089 sv->counters.cli_aborts,
3090 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003091 sv->counters.retries, sv->counters.redispatches);
3092
3093 /* status, lest check */
3094 chunk_appendf(&trash, "<td class=ac>");
3095
Willy Tarreau20125212014-05-13 19:44:56 +02003096 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003097 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3098 chunk_appendf(&trash, "MAINT");
3099 }
Simon Horman0766e442014-11-12 15:55:54 +09003100 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003101 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003102 /* DOWN (agent) */
3103 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 +02003104 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003105 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003106 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3107 chunk_appendf(&trash,
3108 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003109 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3110 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003111 }
Willy Tarreau91861262007-10-17 17:06:05 +02003112
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003113 if ((sv->state == SRV_ST_STOPPED) &&
3114 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3115 chunk_appendf(&trash,
3116 "</td><td class=ac><u> %s%s",
3117 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3118 get_check_status_info(sv->agent.status));
3119
3120 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3121 chunk_appendf(&trash, "/%d", sv->agent.code);
3122
3123 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3124 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3125
3126 chunk_appendf(&trash, "<div class=tips>%s",
3127 get_check_status_description(sv->agent.status));
3128 if (*sv->agent.desc) {
3129 chunk_appendf(&trash, ": ");
3130 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3131 chunk_htmlencode(&trash, &src);
3132 }
3133 chunk_appendf(&trash, "</div></u>");
3134 }
3135 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003136 chunk_appendf(&trash,
3137 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003138 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003139 get_check_status_info(sv->check.status));
3140
3141 if (sv->check.status >= HCHK_STATUS_L57DATA)
3142 chunk_appendf(&trash, "/%d", sv->check.code);
3143
3144 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003145 chunk_appendf(&trash, " in %lums", sv->check.duration);
3146
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003147 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003148 get_check_status_description(sv->check.status));
3149 if (*sv->check.desc) {
3150 chunk_appendf(&trash, ": ");
3151 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3152 chunk_htmlencode(&trash, &src);
3153 }
3154 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003155 }
3156 else
3157 chunk_appendf(&trash, "</td><td>");
3158
3159 chunk_appendf(&trash,
3160 /* weight */
3161 "</td><td class=ac>%d</td>"
3162 /* act, bck */
3163 "<td class=ac>%s</td><td class=ac>%s</td>"
3164 "",
3165 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003166 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3167 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003168
3169 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003170 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003171 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003172
3173 if (ref->observe)
3174 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3175
3176 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003177 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003178 "<td>%lld</td><td>%s</td>"
3179 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003180 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003181 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3182 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003183 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3184 /* tracking a server */
3185 chunk_appendf(&trash,
3186 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003187 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003188 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003189 else
3190 chunk_appendf(&trash, "<td colspan=3></td>");
3191
3192 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003193 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003194 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003195 else
3196 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3197 }
3198 else { /* CSV mode */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003199 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003200 "DOWN,",
Willy Tarreau9638efa2014-05-23 11:19:57 +02003201 "DOWN (agent),",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003202 "DOWN %d/%d,",
3203 "UP %d/%d,",
3204 "UP,",
3205 "NOLB %d/%d,",
3206 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09003207 "DRAIN %d/%d,",
3208 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003209 "no check,"
3210 };
3211
3212 chunk_appendf(&trash,
3213 /* pxid, name */
3214 "%s,%s,"
3215 /* queue : current, max */
3216 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003217 /* sessions : current, max, limit, total */
3218 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003219 /* bytes : in, out */
3220 "%lld,%lld,"
3221 /* denied: req, resp */
3222 ",%lld,"
3223 /* errors : request, connect, response */
3224 ",%lld,%lld,"
3225 /* warnings: retries, redispatches */
3226 "%lld,%lld,"
3227 "",
3228 px->id, sv->id,
3229 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003230 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003231 sv->counters.bytes_in, sv->counters.bytes_out,
3232 sv->counters.failed_secu,
3233 sv->counters.failed_conns, sv->counters.failed_resp,
3234 sv->counters.retries, sv->counters.redispatches);
3235
3236 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003237 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003238 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003239 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003240 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003241 else
3242 chunk_appendf(&trash,
3243 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003244 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3245 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003246
3247 chunk_appendf(&trash,
3248 /* weight, active, backup */
3249 "%d,%d,%d,"
3250 "",
3251 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003252 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3253 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003254
3255 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003256 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003257 chunk_appendf(&trash,
3258 "%lld,%lld,%d,%d,",
3259 sv->counters.failed_checks, sv->counters.down_trans,
3260 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3261 else
3262 chunk_appendf(&trash, ",,,,");
3263
3264 /* queue limit, pid, iid, sid, */
3265 chunk_appendf(&trash,
3266 "%s,"
3267 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003268 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003269 relative_pid, px->uuid, sv->puid);
3270
3271 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003272 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003273 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003274
3275 /* sessions: lbtot */
3276 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3277
3278 /* tracked */
3279 if (sv->track)
3280 chunk_appendf(&trash, "%s/%s,",
3281 sv->track->proxy->id, sv->track->id);
3282 else
3283 chunk_appendf(&trash, ",");
3284
3285 /* type */
3286 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3287
3288 /* rate */
3289 chunk_appendf(&trash, "%u,,%u,",
3290 read_freq_ctr(&sv->sess_per_sec),
3291 sv->counters.sps_max);
3292
Willy Tarreauff5ae352013-12-11 20:36:34 +01003293 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003294 /* check_status */
3295 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3296
3297 /* check_code */
3298 if (sv->check.status >= HCHK_STATUS_L57DATA)
3299 chunk_appendf(&trash, "%u,", sv->check.code);
3300 else
3301 chunk_appendf(&trash, ",");
3302
3303 /* check_duration */
3304 if (sv->check.status >= HCHK_STATUS_CHECKED)
3305 chunk_appendf(&trash, "%lu,", sv->check.duration);
3306 else
3307 chunk_appendf(&trash, ",");
3308
3309 }
3310 else
3311 chunk_appendf(&trash, ",,,");
3312
3313 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3314 if (px->mode == PR_MODE_HTTP) {
3315 for (i=1; i<6; i++)
3316 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3317
3318 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3319 }
3320 else
3321 chunk_appendf(&trash, ",,,,,,");
3322
3323 /* failed health analyses */
3324 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3325
3326 /* requests : req_rate, req_rate_max, req_tot, */
3327 chunk_appendf(&trash, ",,,");
3328
3329 /* errors: cli_aborts, srv_aborts */
3330 chunk_appendf(&trash, "%lld,%lld,",
3331 sv->counters.cli_aborts, sv->counters.srv_aborts);
3332
3333 /* compression: in, out, bypassed, comp_rsp */
3334 chunk_appendf(&trash, ",,,,");
3335
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003336 /* lastsess */
3337 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3338
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003339 /* capture of last check and agent statuses */
3340 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->check.desc) : "");
3341 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? cstr(sv->agent.desc) : "");
3342
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003343 /* qtime, ctime, rtime, ttime, */
3344 chunk_appendf(&trash, "%u,%u,%u,%u,",
3345 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3346 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3347 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3348 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3349
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003350 /* finish with EOL */
3351 chunk_appendf(&trash, "\n");
3352 }
3353 return 1;
3354}
3355
3356/* Dumps a line for backend <px> to the trash for and uses the state from stream
3357 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3358 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3359 */
3360static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3361{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003362 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 struct chunk src;
3364 int i;
3365
3366 if (!(px->cap & PR_CAP_BE))
3367 return 0;
3368
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003369 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003370 return 0;
3371
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003372 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003374 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003375 /* Column sub-heading for Enable or Disable server */
3376 chunk_appendf(&trash, "<td></td>");
3377 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003378 chunk_appendf(&trash,
3379 "<td class=ac>"
3380 /* name */
3381 "%s<a name=\"%s/Backend\"></a>"
3382 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3383 "",
3384 (flags & ST_SHLGNDS)?"<u>":"",
3385 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003386
3387 if (flags & ST_SHLGNDS) {
3388 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003389 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003390 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3391
3392 /* cookie */
3393 if (px->cookie_name) {
3394 chunk_appendf(&trash, ", cookie: '");
3395 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3396 chunk_htmlencode(&trash, &src);
3397 chunk_appendf(&trash, "'");
3398 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003399 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003400 }
3401
3402 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003403 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003404 /* queue : current, max */
3405 "<td>%s</td><td>%s</td><td></td>"
3406 /* sessions rate : current, max, limit */
3407 "<td>%s</td><td>%s</td><td></td>"
3408 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003409 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003410 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3411 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003412
3413 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003414 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003415 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003416 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003417 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003418 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003419 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3420 U2H(px->be_counters.cum_conn),
3421 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422
Willy Tarreau466c9b52012-12-23 02:25:03 +01003423 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003424 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003425 chunk_appendf(&trash,
3426 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3427 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3428 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3429 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3430 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3431 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3432 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3433 "<tr><th>- other responses:</th><td>%s</td></tr>"
3434 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003435 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003436 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003437 U2H(px->be_counters.p.http.cum_req),
3438 U2H(px->be_counters.p.http.rsp[1]),
3439 U2H(px->be_counters.p.http.rsp[2]),
3440 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003441 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003442 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003443 U2H(px->be_counters.p.http.rsp[3]),
3444 U2H(px->be_counters.p.http.rsp[4]),
3445 U2H(px->be_counters.p.http.rsp[5]),
3446 U2H(px->be_counters.p.http.rsp[0]),
3447 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003448 }
3449
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003450 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)));
3451 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)));
3452 if (px->mode == PR_MODE_HTTP)
3453 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)));
3454 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)));
3455
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003456 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003457 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003458 /* sessions: lbtot, last */
3459 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003460 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003461 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003462 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003463 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003464 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003465 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003466
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003467 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003468 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003469 "<td>%s%s<div class=tips>compression: in=%lld out=%lld bypassed=%lld savings=%d%%</div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003470 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003471 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003472 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3473 px->be_counters.comp_in ?
3474 (int)((px->be_counters.comp_in - px->be_counters.comp_out)*100/px->be_counters.comp_in) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003475 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3476
3477 chunk_appendf(&trash,
3478 /* denied: req, resp */
3479 "<td>%s</td><td>%s</td>"
3480 /* errors : request, connect */
3481 "<td></td><td>%s</td>"
3482 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003483 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003484 /* warnings: retries, redispatches */
3485 "<td>%lld</td><td>%lld</td>"
3486 /* backend status: reflect backend status (up/down): we display UP
3487 * if the backend has known working servers or if it has no server at
3488 * all (eg: for stats). Then we display the total weight, number of
3489 * active and backups. */
3490 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3491 "<td class=ac>%d</td><td class=ac>%d</td>"
3492 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003493 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3494 U2H(px->be_counters.failed_conns),
3495 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003496 px->be_counters.cli_aborts,
3497 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003498 px->be_counters.retries, px->be_counters.redispatches,
3499 human_time(now.tv_sec - px->last_change, 1),
3500 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3501 "<font color=\"red\"><b>DOWN</b></font>",
3502 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3503 px->srv_act, px->srv_bck);
3504
3505 chunk_appendf(&trash,
3506 /* rest of backend: nothing, down transitions, total downtime, throttle */
3507 "<td class=ac>&nbsp;</td><td>%d</td>"
3508 "<td>%s</td>"
3509 "<td></td>"
3510 "</tr>",
3511 px->down_trans,
3512 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3513 }
3514 else { /* CSV mode */
3515 chunk_appendf(&trash,
3516 /* pxid, name */
3517 "%s,BACKEND,"
3518 /* queue : current, max */
3519 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003520 /* sessions : current, max, limit, total */
3521 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003522 /* bytes : in, out */
3523 "%lld,%lld,"
3524 /* denied: req, resp */
3525 "%lld,%lld,"
3526 /* errors : request, connect, response */
3527 ",%lld,%lld,"
3528 /* warnings: retries, redispatches */
3529 "%lld,%lld,"
3530 /* backend status: reflect backend status (up/down): we display UP
3531 * if the backend has known working servers or if it has no server at
3532 * all (eg: for stats). Then we display the total weight, number of
3533 * active and backups. */
3534 "%s,"
3535 "%d,%d,%d,"
3536 /* rest of backend: nothing, down transitions, last change, total downtime */
3537 ",%d,%d,%d,,"
3538 /* pid, iid, sid, throttle, lbtot, tracked, type */
3539 "%d,%d,0,,%lld,,%d,"
3540 /* rate, rate_lim, rate_max, */
3541 "%u,,%u,"
3542 /* check_status, check_code, check_duration */
3543 ",,,",
3544 px->id,
3545 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3546 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3547 px->be_counters.bytes_in, px->be_counters.bytes_out,
3548 px->be_counters.denied_req, px->be_counters.denied_resp,
3549 px->be_counters.failed_conns, px->be_counters.failed_resp,
3550 px->be_counters.retries, px->be_counters.redispatches,
3551 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3552 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3553 px->srv_act, px->srv_bck,
3554 px->down_trans, (int)(now.tv_sec - px->last_change),
3555 px->srv?be_downtime(px):0,
3556 relative_pid, px->uuid,
3557 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3558 read_freq_ctr(&px->be_sess_per_sec),
3559 px->be_counters.sps_max);
3560
3561 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3562 if (px->mode == PR_MODE_HTTP) {
3563 for (i=1; i<6; i++)
3564 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3565 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3566 }
3567 else
3568 chunk_appendf(&trash, ",,,,,,");
3569
3570 /* failed health analyses */
3571 chunk_appendf(&trash, ",");
3572
3573 /* requests : req_rate, req_rate_max, req_tot, */
3574 chunk_appendf(&trash, ",,,");
3575
3576 /* errors: cli_aborts, srv_aborts */
3577 chunk_appendf(&trash, "%lld,%lld,",
3578 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3579
3580 /* compression: in, out, bypassed */
3581 chunk_appendf(&trash, "%lld,%lld,%lld,",
3582 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3583
3584 /* compression: comp_rsp */
3585 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3586
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003587 /* lastsess, last_chk, last_agt, */
3588 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003589
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003590 /* qtime, ctime, rtime, ttime, */
3591 chunk_appendf(&trash, "%u,%u,%u,%u,",
3592 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
3593 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
3594 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
3595 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
3596
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003597 /* finish with EOL */
3598 chunk_appendf(&trash, "\n");
3599 }
3600 return 1;
3601}
3602
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003603/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003604 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003605 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003606 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003607static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003608{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003609 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003610 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3611
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003612 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003613 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003614
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003615 /* 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 +02003616 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003617 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003618 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003619 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 +01003620 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003621 }
3622
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003623 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05003624 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003625 }
3626
3627 /* print a new table */
3628 chunk_appendf(&trash,
3629 "<table class=\"tbl\" width=\"100%%\">\n"
3630 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003631 "<th class=\"pxname\" width=\"10%%\">");
3632
3633 chunk_appendf(&trash,
3634 "<a name=\"%s\"></a>%s"
3635 "<a class=px href=\"#%s\">%s</a>",
3636 px->id,
3637 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3638 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003639
3640 if (uri->flags & ST_SHLGNDS) {
3641 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003642 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003643 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3644 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003645 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003646 }
3647
3648 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003649 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003650 "<th class=\"%s\" width=\"90%%\">%s</th>"
3651 "</tr>\n"
3652 "</table>\n"
3653 "<table class=\"tbl\" width=\"100%%\">\n"
3654 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003655 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3656 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3657
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003658 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003659 /* Column heading for Enable or Disable server */
3660 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003661 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003662
3663 chunk_appendf(&trash,
3664 "<th rowspan=2></th>"
3665 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003666 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003667 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3668 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3669 "<th colspan=9>Server</th>"
3670 "</tr>\n"
3671 "<tr class=\"titre\">"
3672 "<th>Cur</th><th>Max</th><th>Limit</th>"
3673 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003674 "<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 +01003675 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3676 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3677 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3678 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3679 "<th>Thrtle</th>\n"
3680 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003681}
3682
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003683/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003684 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003685 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003686static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003687{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003688 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003689 chunk_appendf(&trash, "</table>");
3690
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003691 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003692 /* close the form used to enable/disable this proxy servers */
3693 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003694 "Choose the action to perform on the checked servers : "
3695 "<select name=action>"
3696 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003697 "<option value=\"ready\">Set state to READY</option>"
3698 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02003699 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003700 "<option value=\"dhlth\">Health: disable checks</option>"
3701 "<option value=\"ehlth\">Health: enable checks</option>"
3702 "<option value=\"hrunn\">Health: force UP</option>"
3703 "<option value=\"hnolb\">Health: force NOLB</option>"
3704 "<option value=\"hdown\">Health: force DOWN</option>"
3705 "<option value=\"dagent\">Agent: disable checks</option>"
3706 "<option value=\"eagent\">Agent: enable checks</option>"
3707 "<option value=\"arunn\">Agent: force UP</option>"
3708 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003709 "<option value=\"shutdown\">Kill Sessions</option>"
3710 "</select>"
3711 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3712 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3713 "</form>",
3714 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003715 }
3716
3717 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003718}
Willy Tarreau91861262007-10-17 17:06:05 +02003719
3720/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003721 * Dumps statistics for a proxy. The output is sent to the stream interface's
3722 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3723 * buffer space, or non-zero if everything completed. This function is used
3724 * both by the CLI and the HTTP entry points, and is able to dump the output
3725 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003726 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003727static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003728{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003729 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau3c23a852014-12-28 12:19:57 +01003730 struct session *s = si_sess(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01003731 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02003732 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003733 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003734
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003735 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003736
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003737 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003738 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003739 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003740 if (uri && uri->scope) {
3741 /* we have a limited scope, we have to check the proxy name */
3742 struct stat_scope *scope;
3743 int len;
3744
3745 len = strlen(px->id);
3746 scope = uri->scope;
3747
3748 while (scope) {
3749 /* match exact proxy name */
3750 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3751 break;
3752
3753 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003754 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003755 break;
3756 scope = scope->next;
3757 }
3758
3759 /* proxy name not found : don't dump anything */
3760 if (scope == NULL)
3761 return 1;
3762 }
3763
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003764 /* if the user has requested a limited output and the proxy
3765 * name does not match, skip it.
3766 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003767 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01003768 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 +02003769 return 1;
3770
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003771 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3772 (appctx->ctx.stats.iid != -1) &&
3773 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003774 return 1;
3775
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003776 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003777 /* fall through */
3778
Willy Tarreau295a8372011-03-10 11:25:07 +01003779 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003780 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003781 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003782 if (bi_putchk(rep, &trash) == -1) {
3783 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003784 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003785 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003786 }
Willy Tarreau91861262007-10-17 17:06:05 +02003787
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003788 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003789 /* fall through */
3790
Willy Tarreau295a8372011-03-10 11:25:07 +01003791 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003792 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003793 if (stats_dump_fe_stats(si, px)) {
3794 if (bi_putchk(rep, &trash) == -1) {
3795 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau91861262007-10-17 17:06:05 +02003796 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003797 }
3798 }
Willy Tarreau91861262007-10-17 17:06:05 +02003799
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003800 appctx->ctx.stats.l = px->conf.listeners.n;
3801 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003802 /* fall through */
3803
Willy Tarreau295a8372011-03-10 11:25:07 +01003804 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003805 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003806 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003807 if (buffer_almost_full(rep->buf)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01003808 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003809 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003810 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003811
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003812 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003813 if (!l->counters)
3814 continue;
3815
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003816 if (appctx->ctx.stats.flags & STAT_BOUND) {
3817 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003818 break;
3819
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003820 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003821 continue;
3822 }
3823
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003824 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003825 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
3826 if (bi_putchk(rep, &trash) == -1) {
3827 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003828 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003829 }
3830 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003831 }
3832
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003833 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3834 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003835 /* fall through */
3836
Willy Tarreau295a8372011-03-10 11:25:07 +01003837 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003838 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003839 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02003840 int sv_state;
Willy Tarreau91861262007-10-17 17:06:05 +02003841
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003842 if (buffer_almost_full(rep->buf)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01003843 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003844 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003845 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003846
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003847 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003848
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003849 if (appctx->ctx.stats.flags & STAT_BOUND) {
3850 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003851 break;
3852
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003853 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003854 continue;
3855 }
3856
Willy Tarreau32091232014-05-16 13:52:00 +02003857 svs = sv;
3858 while (svs->track)
3859 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003860
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003861 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
3862 /* server is UP. The possibilities are :
Willy Tarreau9638efa2014-05-23 11:19:57 +02003863 * - UP, draining, going down => state = 7
3864 * - UP, going down => state = 3
3865 * - UP, draining => state = 8
3866 * - UP, checked => state = 4
3867 * - UP, not checked nor tracked => state = 9
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003868 */
3869
3870 if ((svs->check.state & CHK_ST_ENABLED) &&
3871 (svs->check.health < svs->check.rise + svs->check.fall - 1))
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003872 sv_state = 3;
Willy Tarreau9638efa2014-05-23 11:19:57 +02003873 else
3874 sv_state = 4;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003875
Willy Tarreauefe28222014-05-21 17:13:13 +02003876 if (server_is_draining(sv))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003877 sv_state += 4;
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003878
Willy Tarreau9638efa2014-05-23 11:19:57 +02003879 if (sv_state == 4 && !(svs->check.state & CHK_ST_ENABLED))
3880 sv_state = 9; /* unchecked UP */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003881 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003882 else if (sv->state == SRV_ST_STOPPING) {
3883 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
3884 (svs->check.health == svs->check.rise + svs->check.fall - 1))
Willy Tarreau9638efa2014-05-23 11:19:57 +02003885 sv_state = 6; /* NOLB */
Willy Tarreau91861262007-10-17 17:06:05 +02003886 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003887 sv_state = 5; /* NOLB going down */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003888 }
3889 else { /* stopped */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003890 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health)
3891 sv_state = 1; /* DOWN (agent) */
3892 else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003893 sv_state = 0; /* DOWN */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003894 else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED))
3895 sv_state = 2; /* going up */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003896 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003897 sv_state = 0; /* DOWN, unchecked */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003898 }
Willy Tarreau91861262007-10-17 17:06:05 +02003899
Willy Tarreau9638efa2014-05-23 11:19:57 +02003900 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003901 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003902 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003903 continue;
3904 }
3905
Willy Tarreaubc18da12015-03-13 14:00:47 +01003906 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state)) {
3907 if (bi_putchk(rep, &trash) == -1) {
3908 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003909 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003910 }
3911 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003912 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003913
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003914 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003915 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003916
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003917 case STAT_PX_ST_BE:
3918 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01003919 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
3920 if (bi_putchk(rep, &trash) == -1) {
3921 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003922 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003923 }
3924 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003925
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003926 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003927 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003928
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003929 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003930 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003931 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01003932 if (bi_putchk(rep, &trash) == -1) {
3933 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003934 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003935 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003936 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003937
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003938 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003939 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003940
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003941 case STAT_PX_ST_FIN:
3942 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003943
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003944 default:
3945 /* unknown state, we should put an abort() here ! */
3946 return 1;
3947 }
3948}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003949
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003950/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3951 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003952 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003953static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003954{
3955 /* WARNING! This must fit in the first buffer !!! */
3956 chunk_appendf(&trash,
3957 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3958 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3959 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3960 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3961 "<style type=\"text/css\"><!--\n"
3962 "body {"
3963 " font-family: arial, helvetica, sans-serif;"
3964 " font-size: 12px;"
3965 " font-weight: normal;"
3966 " color: black;"
3967 " background: white;"
3968 "}\n"
3969 "th,td {"
3970 " font-size: 10px;"
3971 "}\n"
3972 "h1 {"
3973 " font-size: x-large;"
3974 " margin-bottom: 0.5em;"
3975 "}\n"
3976 "h2 {"
3977 " font-family: helvetica, arial;"
3978 " font-size: x-large;"
3979 " font-weight: bold;"
3980 " font-style: italic;"
3981 " color: #6020a0;"
3982 " margin-top: 0em;"
3983 " margin-bottom: 0em;"
3984 "}\n"
3985 "h3 {"
3986 " font-family: helvetica, arial;"
3987 " font-size: 16px;"
3988 " font-weight: bold;"
3989 " color: #b00040;"
3990 " background: #e8e8d0;"
3991 " margin-top: 0em;"
3992 " margin-bottom: 0em;"
3993 "}\n"
3994 "li {"
3995 " margin-top: 0.25em;"
3996 " margin-right: 2em;"
3997 "}\n"
3998 ".hr {margin-top: 0.25em;"
3999 " border-color: black;"
4000 " border-bottom-style: solid;"
4001 "}\n"
4002 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4003 ".total {background: #20D0D0;color: #ffff80;}\n"
4004 ".frontend {background: #e8e8d0;}\n"
4005 ".socket {background: #d0d0d0;}\n"
4006 ".backend {background: #e8e8d0;}\n"
4007 ".active0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004008 ".active1 {background: #ff9090;}\n"
4009 ".active2 {background: #ffd020;}\n"
4010 ".active3 {background: #ffffa0;}\n"
4011 ".active4 {background: #c0ffc0;}\n"
4012 ".active5 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
4013 ".active6 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
4014 ".active7 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
4015 ".active8 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
4016 ".active9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004017 ".backup0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004018 ".backup1 {background: #ff9090;}\n"
4019 ".backup2 {background: #ff80ff;}\n"
4020 ".backup3 {background: #c060ff;}\n"
4021 ".backup4 {background: #b0d0ff;}\n"
4022 ".backup5 {background: #c060ff;}\n" /* NOLB state shows same as going down */
4023 ".backup6 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
4024 ".backup7 {background: #c060ff;}\n"
4025 ".backup8 {background: #cc9900;}\n"
4026 ".backup9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004027 ".maintain {background: #c07820;}\n"
4028 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4029 "\n"
4030 "a.px:link {color: #ffff40; text-decoration: none;}"
4031 "a.px:visited {color: #ffff40; text-decoration: none;}"
4032 "a.px:hover {color: #ffffff; text-decoration: none;}"
4033 "a.lfsb:link {color: #000000; text-decoration: none;}"
4034 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4035 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4036 "\n"
4037 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4038 "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"
4039 "table.tbl td.ac { text-align: center;}\n"
4040 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4041 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4042 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4043 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4044 "\n"
4045 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4046 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4047 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004048 "table.det { border-collapse: collapse; border-style: none; }\n"
4049 "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 +01004050 "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 +01004051 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004052 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004053 " display:block;\n"
4054 " visibility:hidden;\n"
4055 " z-index:2147483647;\n"
4056 " position:absolute;\n"
4057 " padding:2px 4px 3px;\n"
4058 " background:#f0f060; color:#000000;\n"
4059 " border:1px solid #7040c0;\n"
4060 " white-space:nowrap;\n"
4061 " font-style:normal;font-size:11px;font-weight:normal;\n"
4062 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4063 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4064 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004065 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004066 "-->\n"
4067 "</style></head>\n",
4068 (uri->flags & ST_SHNODE) ? " on " : "",
4069 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4070 );
4071}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004072
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004073/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004074 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004075 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004076 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004077static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004078{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004079 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004080 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004081 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004082
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004083 /* WARNING! this has to fit the first packet too.
4084 * We are around 3.5 kB, add adding entries will
4085 * become tricky if we want to support 4kB buffers !
4086 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004087 chunk_appendf(&trash,
4088 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4089 PRODUCT_NAME "%s</a></h1>\n"
4090 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4091 "<hr width=\"100%%\" class=\"hr\">\n"
4092 "<h3>&gt; General process information</h3>\n"
4093 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4094 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4095 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4096 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4097 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4098 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4099 "Running tasks: %d/%d; idle = %d %%<br>\n"
4100 "</td><td align=\"center\" nowrap>\n"
4101 "<table class=\"lgd\"><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004102 "<td class=\"active4\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4103 "<td class=\"backup4\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004104 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004105 "<td class=\"active3\"></td><td class=\"noborder\">active UP, going down </td>"
4106 "<td class=\"backup3\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004107 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004108 "<td class=\"active2\"></td><td class=\"noborder\">active DOWN, going up </td>"
4109 "<td class=\"backup2\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004110 "</tr><tr>\n"
4111 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004112 "<td class=\"active9\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004113 "</tr><tr>\n"
4114 "<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 -07004115 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004116 "<td class=\"active8\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004117 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004118 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004119 "</td>"
4120 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4121 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4122 "",
4123 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4124 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4125 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4126 (uri->flags & ST_SHDESC) ? ": " : "",
4127 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4128 pid, relative_pid, global.nbproc,
4129 up / 86400, (up % 86400) / 3600,
4130 (up % 3600) / 60, (up % 60),
4131 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4132 global.rlimit_memmax ? " MB" : "",
4133 global.rlimit_nofile,
4134 global.maxsock, global.maxconn, global.maxpipes,
4135 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4136 run_queue_cur, nb_tasks_cur, idle_pct
4137 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004138
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004139 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004140 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 +01004141 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004142
4143 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004144 "<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 +01004145 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004146 STAT_SCOPE_TXT_MAXLEN);
4147
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004148 /* 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 +02004149 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004150 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004151 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004152 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 +01004153 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004154 }
4155
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004156 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004157 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004158 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004159 uri->uri_prefix,
4160 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004161 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004162 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004163 else
4164 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004165 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004166 uri->uri_prefix,
4167 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004168 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004169 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004170
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004171 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004172 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004173 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004174 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004175 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004176 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004177 "",
4178 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004179 else
4180 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004181 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004182 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004183 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004184 ";norefresh",
4185 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004186 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004187
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004188 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004189 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004190 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004191 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4192 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004193 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004194
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004195 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004196 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004197 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004198 (uri->refresh > 0) ? ";norefresh" : "",
4199 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004200
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004201 chunk_appendf(&trash,
4202 "</ul></td>"
4203 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4204 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4205 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4206 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4207 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4208 "</ul>"
4209 "</td>"
4210 "</tr></table>\n"
4211 ""
4212 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004213
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004214 if (appctx->ctx.stats.st_code) {
4215 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004216 case STAT_STATUS_DONE:
4217 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004218 "<p><div class=active4>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004219 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004220 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004221 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004222 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4223 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004224 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004225 break;
4226 case STAT_STATUS_NONE:
4227 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004228 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004229 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004230 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004231 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004232 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4233 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004234 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004235 break;
4236 case STAT_STATUS_PART:
4237 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004238 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004239 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004240 "Action partially processed.<br>"
4241 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004242 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004243 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4244 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004245 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004246 break;
4247 case STAT_STATUS_ERRP:
4248 chunk_appendf(&trash,
4249 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004250 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004251 "Action not processed because of invalid parameters."
4252 "<ul>"
4253 "<li>The action is maybe unknown.</li>"
4254 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4255 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4256 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004257 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004258 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4259 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004260 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004261 break;
4262 case STAT_STATUS_EXCD:
4263 chunk_appendf(&trash,
4264 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004265 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004266 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4267 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004268 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004269 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4270 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004271 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004272 break;
4273 case STAT_STATUS_DENY:
4274 chunk_appendf(&trash,
4275 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004276 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004277 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004278 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004279 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4280 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004281 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004282 break;
4283 default:
4284 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004285 "<p><div class=active9>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004286 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004287 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004288 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004289 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4290 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004291 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004292 }
4293 chunk_appendf(&trash, "<p>\n");
4294 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004295}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004296
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004297/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4298 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004299 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004300static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004301{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004302 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004303}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004304
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004305/* This function dumps statistics onto the stream interface's read buffer in
4306 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004307 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4308 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
4309 * and the session must be closed, or -1 in case of any error. This function is
4310 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004311 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004312static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004313{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004314 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004315 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004316 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004317
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004318 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004319
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004320 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004321 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004322 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004323 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004324
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004325 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004326 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004327 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004328 else
4329 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004330
Willy Tarreaubc18da12015-03-13 14:00:47 +01004331 if (bi_putchk(rep, &trash) == -1) {
4332 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004333 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004334 }
Willy Tarreauae526782010-03-04 20:34:23 +01004335
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004336 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004337 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004338
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004339 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004340 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004341 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004342 if (bi_putchk(rep, &trash) == -1) {
4343 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau91861262007-10-17 17:06:05 +02004344 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004345 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004346 }
Willy Tarreau91861262007-10-17 17:06:05 +02004347
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004348 appctx->ctx.stats.px = proxy;
4349 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4350 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004351 /* fall through */
4352
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004353 case STAT_ST_LIST:
4354 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004355 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004356 if (buffer_almost_full(rep->buf)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004357 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004358 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004359 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004360
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004361 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004362 /* skip the disabled proxies, global frontend and non-networked ones */
4363 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004364 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004365 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004366
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004367 appctx->ctx.stats.px = px->next;
4368 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004369 }
4370 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004371
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004372 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004373 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004374
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004375 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004376 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004377 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01004378 if (bi_putchk(rep, &trash) == -1) {
4379 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004380 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004381 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004382 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004383
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004384 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004385 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004386
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004387 case STAT_ST_FIN:
4388 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004389
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004390 default:
4391 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004392 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004393 return -1;
4394 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004395}
Willy Tarreauae526782010-03-04 20:34:23 +01004396
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004397/* We reached the stats page through a POST request. The appctx is
4398 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004399 * Parse the posted data and enable/disable servers if necessary.
4400 * Returns 1 if request was parsed or zero if it needs more data.
4401 */
4402static int stats_process_http_post(struct stream_interface *si)
4403{
Willy Tarreau3c23a852014-12-28 12:19:57 +01004404 struct session *s = si_sess(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004405 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004406
4407 struct proxy *px = NULL;
4408 struct server *sv = NULL;
4409
4410 char key[LINESIZE];
4411 int action = ST_ADM_ACTION_NONE;
4412 int reprocess = 0;
4413
4414 int total_servers = 0;
4415 int altered_servers = 0;
4416
4417 char *first_param, *cur_param, *next_param, *end_params;
4418 char *st_cur_param = NULL;
4419 char *st_next_param = NULL;
4420
4421 struct chunk *temp;
4422 int reql;
4423
4424 temp = get_trash_chunk();
4425 if (temp->size < s->txn.req.body_len) {
4426 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004427 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004428 goto out;
4429 }
4430
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004431 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 +01004432 if (reql <= 0) {
4433 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004434 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004435 return 0;
4436 }
4437
4438 first_param = temp->str;
4439 end_params = temp->str + reql;
4440 cur_param = next_param = end_params;
4441 *end_params = '\0';
4442
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004443 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004444
4445 /*
4446 * Parse the parameters in reverse order to only store the last value.
4447 * From the html form, the backend and the action are at the end.
4448 */
4449 while (cur_param > first_param) {
4450 char *value;
4451 int poffset, plen;
4452
4453 cur_param--;
4454
4455 if ((*cur_param == '&') || (cur_param == first_param)) {
4456 reprocess_servers:
4457 /* Parse the key */
4458 poffset = (cur_param != first_param ? 1 : 0);
4459 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4460 if ((plen > 0) && (plen <= sizeof(key))) {
4461 strncpy(key, cur_param + poffset, plen);
4462 key[plen - 1] = '\0';
4463 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004464 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004465 goto out;
4466 }
4467
4468 /* Parse the value */
4469 value = key;
4470 while (*value != '\0' && *value != '=') {
4471 value++;
4472 }
4473 if (*value == '=') {
4474 /* Ok, a value is found, we can mark the end of the key */
4475 *value++ = '\0';
4476 }
4477 if (url_decode(key) < 0 || url_decode(value) < 0)
4478 break;
4479
4480 /* Now we can check the key to see what to do */
4481 if (!px && (strcmp(key, "b") == 0)) {
4482 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4483 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004484 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004485 goto out;
4486 }
4487 }
4488 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004489 if (strcmp(value, "ready") == 0) {
4490 action = ST_ADM_ACTION_READY;
4491 }
4492 else if (strcmp(value, "drain") == 0) {
4493 action = ST_ADM_ACTION_DRAIN;
4494 }
4495 else if (strcmp(value, "maint") == 0) {
4496 action = ST_ADM_ACTION_MAINT;
4497 }
4498 else if (strcmp(value, "shutdown") == 0) {
4499 action = ST_ADM_ACTION_SHUTDOWN;
4500 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004501 else if (strcmp(value, "dhlth") == 0) {
4502 action = ST_ADM_ACTION_DHLTH;
4503 }
4504 else if (strcmp(value, "ehlth") == 0) {
4505 action = ST_ADM_ACTION_EHLTH;
4506 }
4507 else if (strcmp(value, "hrunn") == 0) {
4508 action = ST_ADM_ACTION_HRUNN;
4509 }
4510 else if (strcmp(value, "hnolb") == 0) {
4511 action = ST_ADM_ACTION_HNOLB;
4512 }
4513 else if (strcmp(value, "hdown") == 0) {
4514 action = ST_ADM_ACTION_HDOWN;
4515 }
4516 else if (strcmp(value, "dagent") == 0) {
4517 action = ST_ADM_ACTION_DAGENT;
4518 }
4519 else if (strcmp(value, "eagent") == 0) {
4520 action = ST_ADM_ACTION_EAGENT;
4521 }
4522 else if (strcmp(value, "arunn") == 0) {
4523 action = ST_ADM_ACTION_ARUNN;
4524 }
4525 else if (strcmp(value, "adown") == 0) {
4526 action = ST_ADM_ACTION_ADOWN;
4527 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004528 /* else these are the old supported methods */
4529 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004530 action = ST_ADM_ACTION_DISABLE;
4531 }
4532 else if (strcmp(value, "enable") == 0) {
4533 action = ST_ADM_ACTION_ENABLE;
4534 }
4535 else if (strcmp(value, "stop") == 0) {
4536 action = ST_ADM_ACTION_STOP;
4537 }
4538 else if (strcmp(value, "start") == 0) {
4539 action = ST_ADM_ACTION_START;
4540 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004541 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004542 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004543 goto out;
4544 }
4545 }
4546 else if (strcmp(key, "s") == 0) {
4547 if (!(px && action)) {
4548 /*
4549 * Indicates that we'll need to reprocess the parameters
4550 * as soon as backend and action are known
4551 */
4552 if (!reprocess) {
4553 st_cur_param = cur_param;
4554 st_next_param = next_param;
4555 }
4556 reprocess = 1;
4557 }
4558 else if ((sv = findserver(px, value)) != NULL) {
4559 switch (action) {
4560 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004561 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004562 altered_servers++;
4563 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004564 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004565 }
4566 break;
4567 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004568 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004569 altered_servers++;
4570 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004571 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004572 }
4573 break;
4574 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004575 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4576 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4577 altered_servers++;
4578 total_servers++;
4579 }
4580 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004581 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004582 if (sv->admin & SRV_ADMF_FDRAIN) {
4583 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4584 altered_servers++;
4585 total_servers++;
4586 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004587 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004588 case ST_ADM_ACTION_DHLTH:
4589 if (sv->check.state & CHK_ST_CONFIGURED) {
4590 sv->check.state &= ~CHK_ST_ENABLED;
4591 altered_servers++;
4592 total_servers++;
4593 }
4594 break;
4595 case ST_ADM_ACTION_EHLTH:
4596 if (sv->check.state & CHK_ST_CONFIGURED) {
4597 sv->check.state |= CHK_ST_ENABLED;
4598 altered_servers++;
4599 total_servers++;
4600 }
4601 break;
4602 case ST_ADM_ACTION_HRUNN:
4603 if (!(sv->track)) {
4604 sv->check.health = sv->check.rise + sv->check.fall - 1;
4605 srv_set_running(sv, "changed from Web interface");
4606 altered_servers++;
4607 total_servers++;
4608 }
4609 break;
4610 case ST_ADM_ACTION_HNOLB:
4611 if (!(sv->track)) {
4612 sv->check.health = sv->check.rise + sv->check.fall - 1;
4613 srv_set_stopping(sv, "changed from Web interface");
4614 altered_servers++;
4615 total_servers++;
4616 }
4617 break;
4618 case ST_ADM_ACTION_HDOWN:
4619 if (!(sv->track)) {
4620 sv->check.health = 0;
4621 srv_set_stopped(sv, "changed from Web interface");
4622 altered_servers++;
4623 total_servers++;
4624 }
4625 break;
4626 case ST_ADM_ACTION_DAGENT:
4627 if (sv->agent.state & CHK_ST_CONFIGURED) {
4628 sv->agent.state &= ~CHK_ST_ENABLED;
4629 altered_servers++;
4630 total_servers++;
4631 }
4632 break;
4633 case ST_ADM_ACTION_EAGENT:
4634 if (sv->agent.state & CHK_ST_CONFIGURED) {
4635 sv->agent.state |= CHK_ST_ENABLED;
4636 altered_servers++;
4637 total_servers++;
4638 }
4639 break;
4640 case ST_ADM_ACTION_ARUNN:
4641 if (sv->agent.state & CHK_ST_ENABLED) {
4642 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4643 srv_set_running(sv, "changed from Web interface");
4644 altered_servers++;
4645 total_servers++;
4646 }
4647 break;
4648 case ST_ADM_ACTION_ADOWN:
4649 if (sv->agent.state & CHK_ST_ENABLED) {
4650 sv->agent.health = 0;
4651 srv_set_stopped(sv, "changed from Web interface");
4652 altered_servers++;
4653 total_servers++;
4654 }
4655 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004656 case ST_ADM_ACTION_READY:
4657 srv_adm_set_ready(sv);
4658 altered_servers++;
4659 total_servers++;
4660 break;
4661 case ST_ADM_ACTION_DRAIN:
4662 srv_adm_set_drain(sv);
4663 altered_servers++;
4664 total_servers++;
4665 break;
4666 case ST_ADM_ACTION_MAINT:
4667 srv_adm_set_maint(sv);
4668 altered_servers++;
4669 total_servers++;
4670 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004671 case ST_ADM_ACTION_SHUTDOWN:
4672 if (px->state != PR_STSTOPPED) {
4673 struct session *sess, *sess_bck;
4674
4675 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4676 if (sess->srv_conn == sv)
4677 session_shutdown(sess, SN_ERR_KILLED);
4678
4679 altered_servers++;
4680 total_servers++;
4681 }
4682 break;
4683 }
4684 } else {
4685 /* the server name is unknown or ambiguous (duplicate names) */
4686 total_servers++;
4687 }
4688 }
4689 if (reprocess && px && action) {
4690 /* Now, we know the backend and the action chosen by the user.
4691 * We can safely restart from the first server parameter
4692 * to reprocess them
4693 */
4694 cur_param = st_cur_param;
4695 next_param = st_next_param;
4696 reprocess = 0;
4697 goto reprocess_servers;
4698 }
4699
4700 next_param = cur_param;
4701 }
4702 }
4703
4704 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004705 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004706 }
4707 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004708 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004709 }
4710 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004711 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004712 }
4713 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004714 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004715 }
4716 out:
4717 return 1;
4718}
4719
4720
4721static int stats_send_http_headers(struct stream_interface *si)
4722{
Willy Tarreau3c23a852014-12-28 12:19:57 +01004723 struct session *s = si_sess(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004724 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004725 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004726
4727 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004728 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004729 "Cache-Control: no-cache\r\n"
4730 "Connection: close\r\n"
4731 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004732 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004733
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004734 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004735 chunk_appendf(&trash, "Refresh: %d\r\n",
4736 uri->refresh);
4737
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004738 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4739
4740 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4741 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4742 else
4743 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004744
4745 s->txn.status = 200;
4746 s->logs.tv_request = now;
4747
Willy Tarreaubc18da12015-03-13 14:00:47 +01004748 if (bi_putchk(si_ic(si), &trash) == -1) {
4749 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004750 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004751 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004752
4753 return 1;
4754}
4755
4756static int stats_send_http_redirect(struct stream_interface *si)
4757{
4758 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau3c23a852014-12-28 12:19:57 +01004759 struct session *s = si_sess(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004760 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004761 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004762
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004763 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004764 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004765 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004766 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004767 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 +01004768 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004769 }
4770
4771 /* We don't want to land on the posted stats page because a refresh will
4772 * repost the data. We don't want this to happen on accident so we redirect
4773 * the browse to the stats page with a GET.
4774 */
4775 chunk_printf(&trash,
4776 "HTTP/1.1 303 See Other\r\n"
4777 "Cache-Control: no-cache\r\n"
4778 "Content-Type: text/plain\r\n"
4779 "Connection: close\r\n"
4780 "Location: %s;st=%s%s%s%s\r\n"
4781 "\r\n",
4782 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004783 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4784 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4785 stat_status_codes[appctx->ctx.stats.st_code]) ?
4786 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004787 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004788 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4789 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004790 scope_txt);
4791
4792 s->txn.status = 303;
4793 s->logs.tv_request = now;
4794
Willy Tarreaubc18da12015-03-13 14:00:47 +01004795 if (bi_putchk(si_ic(si), &trash) == -1) {
4796 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004797 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004798 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004799
4800 return 1;
4801}
4802
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004803/* This I/O handler runs as an applet embedded in a stream interface. It is
4804 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004805 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004806 * automatically unregisters itself once transfer is complete.
4807 */
4808static void http_stats_io_handler(struct stream_interface *si)
4809{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004810 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau3c23a852014-12-28 12:19:57 +01004811 struct session *s = si_sess(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004812 struct channel *req = si_oc(si);
4813 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01004814
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004815 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4816 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004817
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004818 /* check that the output is not closed */
4819 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004820 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004821
Willy Tarreau347a35d2013-11-22 17:51:09 +01004822 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004823 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004824 if (stats_send_http_headers(si)) {
4825 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004826 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004827 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004828 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004829 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004830 }
4831
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004832 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004833 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004834 unsigned int data_len;
4835 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004836 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004837
4838 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4839 /* One difficulty we're facing is that we must prevent
4840 * the input data from being automatically forwarded to
4841 * the output area. For this, we temporarily disable
4842 * forwarding on the channel.
4843 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004844 last_fwd = si_ic(si)->to_forward;
4845 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004846 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004847 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004848 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004849 si_ic(si)->to_forward = last_fwd;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004850 goto fail;
4851 }
4852 }
4853
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004854 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004855 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004856 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004857
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004858 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004859
4860 /* Now we must either adjust or remove the chunk size. This is
4861 * not easy because the chunk size might wrap at the end of the
4862 * buffer, so we pretend we have nothing in the buffer, we write
4863 * the size, then restore the buffer's contents. Note that we can
4864 * only do that because no forwarding is scheduled on the stats
4865 * applet.
4866 */
4867 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004868 si_ic(si)->total -= (last_len - prev_len);
4869 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004870
4871 if (last_len != data_len) {
4872 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01004873 if (bi_putchk(si_ic(si), &trash) == -1)
4874 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004875
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004876 si_ic(si)->total += (last_len - data_len);
4877 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004878 }
4879 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004880 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004881 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004882 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004883
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004884 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004885 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004886 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004887 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004888 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004889 }
4890
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004891 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004892 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004893 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004894 }
4895
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004896 if (appctx->st0 == STAT_HTTP_DONE) {
4897 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4898 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01004899 if (bi_putchk(si_ic(si), &trash) == -1) {
4900 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004901 goto fail;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004902 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004903 }
4904 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004905 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004906 res->flags |= CF_READ_NULL;
4907 si_shutr(si);
4908 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004909
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004910 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4911 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004912
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004913 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004914 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4915 si_shutr(si);
4916 res->flags |= CF_READ_NULL;
4917 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004918 }
Willy Tarreau91861262007-10-17 17:06:05 +02004919
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004920 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004921 /* update all other flags and resync with the other side */
4922 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004923
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004924 /* we don't want to expire timeouts while we're processing requests */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004925 si_ic(si)->rex = TICK_ETERNITY;
4926 si_oc(si)->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004927
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004928 out:
4929 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4930 /* check that we have released everything then unregister */
4931 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004932 }
4933}
4934
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004935
Willy Tarreau909d5172012-11-26 03:04:41 +01004936static inline const char *get_conn_ctrl_name(const struct connection *conn)
4937{
Willy Tarreau3c728722014-01-23 13:50:42 +01004938 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004939 return "NONE";
4940 return conn->ctrl->name;
4941}
4942
4943static inline const char *get_conn_xprt_name(const struct connection *conn)
4944{
4945 static char ptr[17];
4946
Willy Tarreauaad69382014-01-23 14:21:42 +01004947 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004948 return "NONE";
4949
4950 if (conn->xprt == &raw_sock)
4951 return "RAW";
4952
4953#ifdef USE_OPENSSL
4954 if (conn->xprt == &ssl_sock)
4955 return "SSL";
4956#endif
4957 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4958 return ptr;
4959}
4960
4961static inline const char *get_conn_data_name(const struct connection *conn)
4962{
4963 static char ptr[17];
4964
4965 if (!conn->data)
4966 return "NONE";
4967
4968 if (conn->data == &sess_conn_cb)
4969 return "SESS";
4970
4971 if (conn->data == &si_conn_cb)
4972 return "STRM";
4973
4974 if (conn->data == &check_conn_cb)
4975 return "CHCK";
4976
4977 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4978 return ptr;
4979}
4980
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004981/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004982 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004983 * 0 if the output buffer is full and it needs to be called again, otherwise
4984 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004985 */
Willy Tarreau76153662012-11-26 01:16:39 +01004986static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004987{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004988 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004989 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004990 extern const char *monthname[12];
4991 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004992 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004993 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004994
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004995 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004996
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004997 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004998 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004999 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005000 if (bi_putchk(si_ic(si), &trash) == -1) {
5001 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005002 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005003 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005004 appctx->ctx.sess.uid = 0;
5005 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005006 return 1;
5007 }
5008
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005009 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005010 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005011 appctx->ctx.sess.uid = sess->uniq_id;
5012 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005013 /* fall through */
5014
5015 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005016 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005017 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005018 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005019 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005020 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5021 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005022 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01005023 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005024
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005025 conn = objt_conn(sess->si[0].end);
5026 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005027 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005028 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005029 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005030 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005031 break;
5032 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005033 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005034 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005035 default:
5036 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005037 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005038 break;
5039 }
5040
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005041 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005042 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005043 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005044
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005045 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005046 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005047 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
5048 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
5049 sess->listener ? sess->listener->luid : 0);
5050
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005051 if (conn)
5052 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005053
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005054 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005055 case AF_INET:
5056 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005057 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005058 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005059 break;
5060 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005061 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005062 break;
5063 default:
5064 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005065 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005066 break;
5067 }
5068
Willy Tarreau50943332011-09-02 17:33:05 +02005069 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005070 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005071 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005072 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005073 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5074 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005075 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005076
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005077 conn = objt_conn(sess->si[1].end);
5078 if (conn)
5079 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005080
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005081 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005082 case AF_INET:
5083 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005084 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005085 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005086 break;
5087 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005088 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005089 break;
5090 default:
5091 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005092 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005093 break;
5094 }
5095
5096 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005097 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005098 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005099 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5100 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005101 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005102 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005103
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005104 if (conn)
5105 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005106
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005107 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005108 case AF_INET:
5109 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005110 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005111 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005112 break;
5113 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005114 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005115 break;
5116 default:
5117 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005118 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005119 break;
5120 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005121
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005122 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005123 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005124 sess->task,
5125 sess->task->state,
5126 sess->task->nice, sess->task->calls,
5127 sess->task->expire ?
5128 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5129 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5130 TICKS_TO_MS(1000)) : "<NEVER>",
5131 task_in_rq(sess->task) ? ", running" : "");
5132
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005133 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005134 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005135 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5136
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005137 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005138 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s waiting=%d\n",
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005139 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
Willy Tarreau98410192014-11-26 18:05:38 +01005140 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 +01005141
5142 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005143 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005144 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005145 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005146 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005147 obj_type_name(sess->si[0].end),
5148 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005149 sess->si[0].exp ?
5150 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5151 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5152 TICKS_TO_MS(1000)) : "<NEVER>",
5153 sess->si[0].err_type);
5154
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005155 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005156 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005157 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005158 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005159 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005160 obj_type_name(sess->si[1].end),
5161 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005162 sess->si[1].exp ?
5163 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5164 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5165 TICKS_TO_MS(1000)) : "<NEVER>",
5166 sess->si[1].err_type);
5167
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005168 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005169 chunk_appendf(&trash,
5170 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005171 conn,
5172 get_conn_ctrl_name(conn),
5173 get_conn_xprt_name(conn),
5174 get_conn_data_name(conn),
5175 obj_type_name(conn->target),
5176 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005177
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005178 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005179 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005180 conn->flags,
5181 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005182 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005183 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005184 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005185 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005186 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5187 chunk_appendf(&trash,
5188 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5189 tmpctx,
5190 tmpctx->st0,
5191 tmpctx->st1,
5192 tmpctx->st2,
5193 tmpctx->applet->name);
5194 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005195
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005196 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005197 chunk_appendf(&trash,
5198 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005199 conn,
5200 get_conn_ctrl_name(conn),
5201 get_conn_xprt_name(conn),
5202 get_conn_data_name(conn),
5203 obj_type_name(conn->target),
5204 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005205
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005206 chunk_appendf(&trash,
5207 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005208 conn->flags,
5209 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005210 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005211 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005212 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005213 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005214 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5215 chunk_appendf(&trash,
5216 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5217 tmpctx,
5218 tmpctx->st0,
5219 tmpctx->st1,
5220 tmpctx->st2,
5221 tmpctx->applet->name);
5222 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005223
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005224 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005225 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005226 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005227 &sess->req,
5228 sess->req.flags, sess->req.analysers,
5229 sess->req.pipe ? sess->req.pipe->data : 0,
5230 sess->req.to_forward, sess->req.total,
5231 sess->req.analyse_exp ?
5232 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005233 TICKS_TO_MS(1000)) : "<NEVER>");
5234
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005235 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005236 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005237 sess->req.rex ?
5238 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005239 TICKS_TO_MS(1000)) : "<NEVER>");
5240
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005241 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005242 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005243 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005244 sess->req.wex ?
5245 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005246 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005247 sess->req.buf,
5248 sess->req.buf->data, sess->req.buf->o,
5249 (int)(sess->req.buf->p - sess->req.buf->data),
5250 sess->txn.req.next, sess->req.buf->i,
5251 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005252
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005253 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005254 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005255 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005256 &sess->res,
5257 sess->res.flags, sess->res.analysers,
5258 sess->res.pipe ? sess->res.pipe->data : 0,
5259 sess->res.to_forward, sess->res.total,
5260 sess->res.analyse_exp ?
5261 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005262 TICKS_TO_MS(1000)) : "<NEVER>");
5263
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005264 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005265 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005266 sess->res.rex ?
5267 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005268 TICKS_TO_MS(1000)) : "<NEVER>");
5269
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005270 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005271 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005272 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005273 sess->res.wex ?
5274 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005275 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005276 sess->res.buf,
5277 sess->res.buf->data, sess->res.buf->o,
5278 (int)(sess->res.buf->p - sess->res.buf->data),
5279 sess->txn.rsp.next, sess->res.buf->i,
5280 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005281
Willy Tarreaubc18da12015-03-13 14:00:47 +01005282 if (bi_putchk(si_ic(si), &trash) == -1) {
5283 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005284 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005285 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005286
5287 /* use other states to dump the contents */
5288 }
5289 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005290 appctx->ctx.sess.uid = 0;
5291 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005292 return 1;
5293}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005294
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005295static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005296{
5297 struct appctx *appctx = __objt_appctx(si->end);
5298
5299 switch (appctx->st2) {
5300 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005301 /* Display the column headers. If the message cannot be sent,
5302 * quit the fucntion with returning 0. The function is called
5303 * later and restart at the state "STAT_ST_INIT".
5304 */
5305 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005306 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005307 if (bi_putchk(si_ic(si), &trash) == -1) {
5308 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005309 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005310 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005311
5312 /* Now, we start the browsing of the references lists.
5313 * Note that the following call to LIST_ELEM return bad pointer. The only
5314 * avalaible field of this pointer is <list>. It is used with the function
5315 * pat_list_get_next() for retruning the first avalaible entry
5316 */
5317 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5318 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5319 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005320 appctx->st2 = STAT_ST_LIST;
5321 /* fall through */
5322
5323 case STAT_ST_LIST:
5324 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005325 chunk_reset(&trash);
5326
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005327 /* Build messages. If the reference is used by another category than
5328 * the listed categorie, display the information in the massage.
5329 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005330 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005331 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5332 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005333
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005334 if (bi_putchk(si_ic(si), &trash) == -1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005335 /* let's try again later from this session. We add ourselves into
5336 * this session's users so that it can remove us upon termination.
5337 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005338 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005339 return 0;
5340 }
5341
5342 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005343 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5344 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005345 }
5346
5347 appctx->st2 = STAT_ST_FIN;
5348 /* fall through */
5349
5350 default:
5351 appctx->st2 = STAT_ST_FIN;
5352 return 1;
5353 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005354 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005355}
5356
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005357static int stats_map_lookup(struct stream_interface *si)
5358{
5359 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005360 struct sample sample;
5361 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005362 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005363
5364 switch (appctx->st2) {
5365 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005366 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005367 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005368 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005369 appctx->st2 = STAT_ST_LIST;
5370 /* fall through */
5371
5372 case STAT_ST_LIST:
5373 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005374 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005375 /* initialise chunk to build new message */
5376 chunk_reset(&trash);
5377
5378 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005379 sample.type = SMP_T_STR;
5380 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005381 sample.data.str.len = appctx->ctx.map.chunk.len;
5382 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005383 if (appctx->ctx.map.expr->pat_head->match &&
5384 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005385 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5386 else
5387 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005388
5389 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005390 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5391 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5392 break;
5393 if (match_method >= PAT_MATCH_NUM)
5394 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5395 else
5396 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005397
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005398 /* case sensitive */
5399 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5400 chunk_appendf(&trash, ", case=insensitive");
5401 else
5402 chunk_appendf(&trash, ", case=sensitive");
5403
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005404 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005405 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005406 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5407 chunk_appendf(&trash, ", found=no");
5408 else
5409 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005410 }
5411
5412 /* Display match and match info */
5413 else {
5414 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005415 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5416 chunk_appendf(&trash, ", found=yes");
5417 else
5418 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005419
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005420 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005421 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005422 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005423 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005424 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005425
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005426 /* display pattern */
5427 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5428 if (pat->ref && pat->ref->pattern)
5429 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5430 else
5431 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005432 }
5433 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005434 if (pat->ref && pat->ref->pattern)
5435 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5436 else
5437 chunk_appendf(&trash, ", pattern=unknown");
5438 }
5439
5440 /* display return value */
5441 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5442 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005443 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005444 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005445 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005446 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005447 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005448 }
5449
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005450 chunk_appendf(&trash, "\n");
5451
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005452 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005453 if (bi_putchk(si_ic(si), &trash) == -1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005454 /* let's try again later from this session. We add ourselves into
5455 * this session's users so that it can remove us upon termination.
5456 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005457 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005458 return 0;
5459 }
5460
5461 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005462 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5463 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005464 }
5465
5466 appctx->st2 = STAT_ST_FIN;
5467 /* fall through */
5468
5469 default:
5470 appctx->st2 = STAT_ST_FIN;
5471 free(appctx->ctx.map.chunk.str);
5472 return 1;
5473 }
5474}
5475
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005476static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005477{
5478 struct appctx *appctx = __objt_appctx(si->end);
5479
5480 switch (appctx->st2) {
5481
5482 case STAT_ST_INIT:
5483 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005484 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5485 struct pat_ref_elt *, list);
5486 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5487 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005488 appctx->st2 = STAT_ST_LIST;
5489 /* fall through */
5490
5491 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005492 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005493 chunk_reset(&trash);
5494
5495 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005496 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005497 chunk_appendf(&trash, "%p %s %s\n",
5498 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5499 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005500 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005501 chunk_appendf(&trash, "%p %s\n",
5502 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005503
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005504 if (bi_putchk(si_ic(si), &trash) == -1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005505 /* let's try again later from this session. We add ourselves into
5506 * this session's users so that it can remove us upon termination.
5507 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005508 si->flags |= SI_FL_WAIT_ROOM;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005509 return 0;
5510 }
5511
5512 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005513 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5514 struct pat_ref_elt *, list);
5515 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005516 break;
5517 }
5518
5519 appctx->st2 = STAT_ST_FIN;
5520 /* fall through */
5521
5522 default:
5523 appctx->st2 = STAT_ST_FIN;
5524 return 1;
5525 }
5526}
5527
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005528/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005529 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005530 * to be called again, otherwise non-zero. It is designed to be called
5531 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005532 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005533static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005534{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005535 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005536 struct connection *conn;
5537
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005538 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005539 /* If we're forced to shut down, we might have to remove our
5540 * reference to the last session being dumped.
5541 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005542 if (appctx->st2 == STAT_ST_LIST) {
5543 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5544 LIST_DEL(&appctx->ctx.sess.bref.users);
5545 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005546 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005547 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005548 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005549 }
5550
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005551 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005552
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005553 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005554 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005555 /* the function had not been called yet, let's prepare the
5556 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005557 * pointer to the first in the global list. When a target
5558 * session is being destroyed, it is responsible for updating
5559 * this pointer. We know we have reached the end when this
5560 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005561 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005562 LIST_INIT(&appctx->ctx.sess.bref.users);
5563 appctx->ctx.sess.bref.ref = sessions.n;
5564 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005565 /* fall through */
5566
Willy Tarreau295a8372011-03-10 11:25:07 +01005567 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005568 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005569 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5570 LIST_DEL(&appctx->ctx.sess.bref.users);
5571 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005572 }
5573
5574 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005575 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005576 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005577 struct session *curr_sess;
5578
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005579 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005580
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005581 if (appctx->ctx.sess.target) {
5582 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005583 goto next_sess;
5584
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005585 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005586 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005587 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005588 return 0;
5589
5590 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005591 LIST_DEL(&appctx->ctx.sess.bref.users);
5592 LIST_INIT(&appctx->ctx.sess.bref.users);
5593 if (appctx->ctx.sess.target != (void *)-1) {
5594 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005595 break;
5596 }
5597 else
5598 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005599 }
5600
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005601 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005602 "%p: proto=%s",
5603 curr_sess,
5604 curr_sess->listener->proto->name);
5605
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005606
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005607 conn = objt_conn(curr_sess->si[0].end);
5608 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005609 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005610 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005611 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005612 " src=%s:%d fe=%s be=%s srv=%s",
5613 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005614 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005615 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005616 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005617 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005618 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005619 break;
5620 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005621 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005622 " src=unix:%d fe=%s be=%s srv=%s",
5623 curr_sess->listener->luid,
5624 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005625 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005626 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005627 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005628 break;
5629 }
5630
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005631 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005632 " ts=%02x age=%s calls=%d",
5633 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005634 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5635 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005636
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005637 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005638 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005639 curr_sess->req.flags,
5640 curr_sess->req.buf->i,
5641 curr_sess->req.analysers,
5642 curr_sess->req.rex ?
5643 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005644 TICKS_TO_MS(1000)) : "");
5645
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005646 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005647 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005648 curr_sess->req.wex ?
5649 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005650 TICKS_TO_MS(1000)) : "");
5651
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005652 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005653 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005654 curr_sess->req.analyse_exp ?
5655 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005656 TICKS_TO_MS(1000)) : "");
5657
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005658 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005659 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005660 curr_sess->res.flags,
5661 curr_sess->res.buf->i,
5662 curr_sess->res.analysers,
5663 curr_sess->res.rex ?
5664 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005665 TICKS_TO_MS(1000)) : "");
5666
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005667 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005668 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005669 curr_sess->res.wex ?
5670 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005671 TICKS_TO_MS(1000)) : "");
5672
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005673 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005674 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005675 curr_sess->res.analyse_exp ?
5676 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005677 TICKS_TO_MS(1000)) : "");
5678
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005679 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005680 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005681 " s0=[%d,%1xh,fd=%d,ex=%s]",
5682 curr_sess->si[0].state,
5683 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005684 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005685 curr_sess->si[0].exp ?
5686 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5687 TICKS_TO_MS(1000)) : "");
5688
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005689 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005690 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005691 " s1=[%d,%1xh,fd=%d,ex=%s]",
5692 curr_sess->si[1].state,
5693 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005694 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005695 curr_sess->si[1].exp ?
5696 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5697 TICKS_TO_MS(1000)) : "");
5698
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005699 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005700 " exp=%s",
5701 curr_sess->task->expire ?
5702 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5703 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005704 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005705 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005706
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005707 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005708
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005709 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005710 /* let's try again later from this session. We add ourselves into
5711 * this session's users so that it can remove us upon termination.
5712 */
Willy Tarreaubc18da12015-03-13 14:00:47 +01005713 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005714 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005715 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005716 }
5717
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005718 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005719 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005720 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005721
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005722 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005723 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005724 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005725 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005726 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005727 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005728
Willy Tarreaubc18da12015-03-13 14:00:47 +01005729 if (bi_putchk(si_ic(si), &trash) == -1) {
5730 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005731 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005732 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005733
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005734 appctx->ctx.sess.target = NULL;
5735 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005736 return 1;
5737 }
5738
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005739 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005740 /* fall through */
5741
5742 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005743 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005744 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005745 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005746}
5747
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005748/* This is called when the stream interface is closed. For instance, upon an
5749 * external abort, we won't call the i/o handler anymore so we may need to
5750 * remove back references to the session currently being dumped.
5751 */
Simon Horman74d88312013-02-12 10:45:50 +09005752static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005753{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005754 struct appctx *appctx = __objt_appctx(si->end);
5755
5756 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5757 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5758 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005759 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005760 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5761 free(appctx->ctx.cli.err);
5762 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005763 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5764 free(appctx->ctx.map.chunk.str);
5765 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005766}
5767
Willy Tarreau20e99322013-04-13 09:22:25 +02005768/* This function is used to either dump tables states (when action is set
5769 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005770 * It returns 0 if the output buffer is full and it needs to be called
5771 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005772 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005773static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005774{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005775 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau3c23a852014-12-28 12:19:57 +01005776 struct session *s = si_sess(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005777 struct ebmb_node *eb;
5778 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005779 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005780 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005781
5782 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005783 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005784 * - STAT_ST_INIT : the first call
5785 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005786 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005787 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005788 * and the entry pointer points to the next entry to be dumped,
5789 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005790 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005791 * data though.
5792 */
5793
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005794 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005795 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005796 if (appctx->st2 == STAT_ST_LIST) {
5797 appctx->ctx.table.entry->ref_cnt--;
5798 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005799 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005800 return 1;
5801 }
5802
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005803 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005804
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005805 while (appctx->st2 != STAT_ST_FIN) {
5806 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005807 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005808 appctx->ctx.table.proxy = appctx->ctx.table.target;
5809 if (!appctx->ctx.table.proxy)
5810 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005811
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005812 appctx->ctx.table.entry = NULL;
5813 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005814 break;
5815
Willy Tarreau295a8372011-03-10 11:25:07 +01005816 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005817 if (!appctx->ctx.table.proxy ||
5818 (appctx->ctx.table.target &&
5819 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5820 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005821 break;
5822 }
5823
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005824 if (appctx->ctx.table.proxy->table.size) {
5825 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5826 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005827 return 0;
5828
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005829 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005830 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005831 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005832 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005833 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005834 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5835 appctx->ctx.table.entry->ref_cnt++;
5836 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005837 break;
5838 }
5839 }
5840 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005841 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005842 break;
5843
Willy Tarreau295a8372011-03-10 11:25:07 +01005844 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005845 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005846
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005847 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005848 /* we're filtering on some data contents */
5849 void *ptr;
5850 long long data;
5851
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005852 dt = appctx->ctx.table.data_type;
5853 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5854 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005855 dt);
5856
5857 data = 0;
5858 switch (stktable_data_types[dt].std_type) {
5859 case STD_T_SINT:
5860 data = stktable_data_cast(ptr, std_t_sint);
5861 break;
5862 case STD_T_UINT:
5863 data = stktable_data_cast(ptr, std_t_uint);
5864 break;
5865 case STD_T_ULL:
5866 data = stktable_data_cast(ptr, std_t_ull);
5867 break;
5868 case STD_T_FRQP:
5869 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005870 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005871 break;
5872 }
5873
5874 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005875 if ((data < appctx->ctx.table.value &&
5876 (appctx->ctx.table.data_op == STD_OP_EQ ||
5877 appctx->ctx.table.data_op == STD_OP_GT ||
5878 appctx->ctx.table.data_op == STD_OP_GE)) ||
5879 (data == appctx->ctx.table.value &&
5880 (appctx->ctx.table.data_op == STD_OP_NE ||
5881 appctx->ctx.table.data_op == STD_OP_GT ||
5882 appctx->ctx.table.data_op == STD_OP_LT)) ||
5883 (data > appctx->ctx.table.value &&
5884 (appctx->ctx.table.data_op == STD_OP_EQ ||
5885 appctx->ctx.table.data_op == STD_OP_LT ||
5886 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005887 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005888 }
5889
Simon Hormanc88b8872011-06-15 15:18:49 +09005890 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005891 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5892 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005893 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005894
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005895 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005896
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005897 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005898 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005899 struct stksess *old = appctx->ctx.table.entry;
5900 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005901 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005902 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5903 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5904 stksess_kill(&appctx->ctx.table.proxy->table, old);
5905 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005906 break;
5907 }
5908
Simon Hormanc88b8872011-06-15 15:18:49 +09005909
5910 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005911 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5912 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5913 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005914
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005915 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5916 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005917 break;
5918
Willy Tarreau295a8372011-03-10 11:25:07 +01005919 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005920 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005921 break;
5922 }
5923 }
5924 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005925}
5926
Willy Tarreaud426a182010-03-05 14:58:26 +01005927/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005928 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5929 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5930 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5931 * lines are respected within the limit of 70 output chars. Lines that are
5932 * continuation of a previous truncated line begin with "+" instead of " "
5933 * after the offset. The new pointer is returned.
5934 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005935static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5936 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005937{
5938 int end;
5939 unsigned char c;
5940
5941 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005942 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005943 return ptr;
5944
Willy Tarreau77804732012-10-29 16:14:26 +01005945 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005946
Willy Tarreaud426a182010-03-05 14:58:26 +01005947 while (ptr < len && ptr < bsize) {
5948 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005949 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005950 if (out->len > end - 2)
5951 break;
5952 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005953 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005954 if (out->len > end - 3)
5955 break;
5956 out->str[out->len++] = '\\';
5957 switch (c) {
5958 case '\t': c = 't'; break;
5959 case '\n': c = 'n'; break;
5960 case '\r': c = 'r'; break;
5961 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005962 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005963 }
5964 out->str[out->len++] = c;
5965 } else {
5966 if (out->len > end - 5)
5967 break;
5968 out->str[out->len++] = '\\';
5969 out->str[out->len++] = 'x';
5970 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5971 out->str[out->len++] = hextab[c & 0xF];
5972 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005973 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005974 /* we had a line break, let's return now */
5975 out->str[out->len++] = '\n';
5976 *line = ptr;
5977 return ptr;
5978 }
5979 }
5980 /* we have an incomplete line, we return it as-is */
5981 out->str[out->len++] = '\n';
5982 return ptr;
5983}
5984
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005985/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005986 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005987 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005988 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005989static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005990{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005991 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005992 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005993
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005994 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005995 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005996
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005997 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005998
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005999 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006000 /* the function had not been called yet, let's prepare the
6001 * buffer for a response.
6002 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006003 struct tm tm;
6004
6005 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006006 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006007 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6008 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6009 error_snapshot_id);
6010
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006011 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006012 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaubc18da12015-03-13 14:00:47 +01006013 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau10479e42010-12-12 14:00:34 +01006014 return 0;
6015 }
6016
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006017 appctx->ctx.errors.px = proxy;
6018 appctx->ctx.errors.buf = 0;
6019 appctx->ctx.errors.bol = 0;
6020 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006021 }
6022
6023 /* we have two inner loops here, one for the proxy, the other one for
6024 * the buffer.
6025 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006026 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006027 struct error_snapshot *es;
6028
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006029 if (appctx->ctx.errors.buf == 0)
6030 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006031 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006032 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006033
6034 if (!es->when.tv_sec)
6035 goto next;
6036
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006037 if (appctx->ctx.errors.iid >= 0 &&
6038 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6039 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006040 goto next;
6041
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006042 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006043 /* just print headers now */
6044
6045 char pn[INET6_ADDRSTRLEN];
6046 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006047 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006048
6049 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006050 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006051 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006052 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006053
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006054 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6055 case AF_INET:
6056 case AF_INET6:
6057 port = get_host_port(&es->src);
6058 break;
6059 default:
6060 port = 0;
6061 }
6062
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006063 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006064 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006065 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006066 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006067 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006068 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006069 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6070 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006071 break;
6072 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006073 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006074 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006075 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006076 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006077 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006078 break;
6079 }
6080
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006081 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006082 ", server %s (#%d), event #%u\n"
6083 " src %s:%d, session #%d, session flags 0x%08x\n"
6084 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6085 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6086 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6087 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6088 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6089 es->ev_id,
6090 pn, port, es->sid, es->s_flags,
6091 es->state, es->m_flags, es->t_flags,
6092 es->m_clen, es->m_blen,
6093 es->b_flags, es->b_out, es->b_tot,
6094 es->len, es->b_wrap, es->pos);
6095
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006096 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006097 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaubc18da12015-03-13 14:00:47 +01006098 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau61b34732009-10-03 23:49:35 +02006099 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006100 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006101 appctx->ctx.errors.ptr = 0;
6102 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006103 }
6104
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006105 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006106 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006107 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006108 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006109 if (bi_putchk(si_ic(si), &trash) == -1) {
6110 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau61b34732009-10-03 23:49:35 +02006111 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006112 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006113 goto next;
6114 }
6115
6116 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006117 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006118 int newptr;
6119 int newline;
6120
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006121 newline = appctx->ctx.errors.bol;
6122 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6123 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006124 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006125
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006126 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006127 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaubc18da12015-03-13 14:00:47 +01006128 si->flags |= SI_FL_WAIT_ROOM;
Willy Tarreau61b34732009-10-03 23:49:35 +02006129 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006130 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006131 appctx->ctx.errors.ptr = newptr;
6132 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006133 };
6134 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006135 appctx->ctx.errors.bol = 0;
6136 appctx->ctx.errors.ptr = -1;
6137 appctx->ctx.errors.buf++;
6138 if (appctx->ctx.errors.buf > 1) {
6139 appctx->ctx.errors.buf = 0;
6140 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006141 }
6142 }
6143
6144 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006145 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006146}
6147
Willy Tarreaud5781202012-09-22 19:32:35 +02006148/* parse the "level" argument on the bind lines */
6149static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6150{
6151 if (!*args[cur_arg + 1]) {
6152 memprintf(err, "'%s' : missing level", args[cur_arg]);
6153 return ERR_ALERT | ERR_FATAL;
6154 }
6155
6156 if (!strcmp(args[cur_arg+1], "user"))
6157 conf->level = ACCESS_LVL_USER;
6158 else if (!strcmp(args[cur_arg+1], "operator"))
6159 conf->level = ACCESS_LVL_OPER;
6160 else if (!strcmp(args[cur_arg+1], "admin"))
6161 conf->level = ACCESS_LVL_ADMIN;
6162 else {
6163 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6164 args[cur_arg], args[cur_arg+1]);
6165 return ERR_ALERT | ERR_FATAL;
6166 }
6167
6168 return 0;
6169}
6170
Willy Tarreaub24281b2011-02-13 13:16:36 +01006171struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006172 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006173 .name = "<STATS>", /* used for logging */
6174 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006175 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006176};
6177
Simon Horman9bd2c732011-06-15 15:18:44 +09006178static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006179 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006180 .name = "<CLI>", /* used for logging */
6181 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006182 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006183};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006184
Willy Tarreaudc13c112013-06-21 23:16:39 +02006185static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006186 { CFG_GLOBAL, "stats", stats_parse_global },
6187 { 0, NULL, NULL },
6188}};
6189
Willy Tarreaud5781202012-09-22 19:32:35 +02006190static struct bind_kw_list bind_kws = { "STAT", { }, {
6191 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6192 { NULL, NULL, 0 },
6193}};
6194
Willy Tarreau10522fd2008-07-09 20:12:41 +02006195__attribute__((constructor))
6196static void __dumpstats_module_init(void)
6197{
6198 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006199 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006200}
6201
Willy Tarreau91861262007-10-17 17:06:05 +02006202/*
6203 * Local variables:
6204 * c-indent-level: 8
6205 * c-basic-offset: 8
6206 * End:
6207 */