blob: 7c2170149b738e87f1c3fd6ba59c58a34cc8e42e [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>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010044#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010049#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020051#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010052#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010053#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020054#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020055#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010056#include <proto/sample.h>
Willy Tarreau91861262007-10-17 17:06:05 +020057#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020058#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020059#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010060#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010061#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020062
Willy Tarreau7a0169a2012-11-19 17:13:16 +010063#ifdef USE_OPENSSL
64#include <proto/ssl_sock.h>
65#endif
66
Willy Tarreau91b843d2014-01-28 16:27:17 +010067/* stats socket states */
68enum {
69 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
70 STAT_CLI_END, /* final state, let's close */
71 STAT_CLI_GETREQ, /* wait for a request */
72 STAT_CLI_OUTPUT, /* all states after this one are responses */
73 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
74 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010075 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010076 STAT_CLI_O_INFO, /* dump info */
77 STAT_CLI_O_SESS, /* dump sessions */
78 STAT_CLI_O_ERR, /* dump errors */
79 STAT_CLI_O_TAB, /* dump tables */
80 STAT_CLI_O_CLR, /* clear tables */
81 STAT_CLI_O_SET, /* set entries in tables */
82 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010083 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
84 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010085 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010086 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010087};
88
Willy Tarreaued7df902014-05-22 18:04:49 +020089/* Actions available for the stats admin forms */
90enum {
91 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +020092
93 /* enable/disable health checks */
94 ST_ADM_ACTION_DHLTH,
95 ST_ADM_ACTION_EHLTH,
96
97 /* force health check status */
98 ST_ADM_ACTION_HRUNN,
99 ST_ADM_ACTION_HNOLB,
100 ST_ADM_ACTION_HDOWN,
101
102 /* enable/disable agent checks */
103 ST_ADM_ACTION_DAGENT,
104 ST_ADM_ACTION_EAGENT,
105
106 /* force agent check status */
107 ST_ADM_ACTION_ARUNN,
108 ST_ADM_ACTION_ADOWN,
109
110 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200111 ST_ADM_ACTION_READY,
112 ST_ADM_ACTION_DRAIN,
113 ST_ADM_ACTION_MAINT,
114 ST_ADM_ACTION_SHUTDOWN,
115 /* these are the ancient actions, still available for compatibility */
116 ST_ADM_ACTION_DISABLE,
117 ST_ADM_ACTION_ENABLE,
118 ST_ADM_ACTION_STOP,
119 ST_ADM_ACTION_START,
120};
121
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100122static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100123static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +0100124static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900125static int stats_dump_sess_to_buffer(struct stream_interface *si);
126static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100127static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100128static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
129static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100130static int stats_pats_list(struct stream_interface *si);
131static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100132static int stats_map_lookup(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +0900133
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100134/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100135 * cli_io_handler()
136 * -> stats_dump_sess_to_buffer() // "show sess"
137 * -> stats_dump_errors_to_buffer() // "show errors"
138 * -> stats_dump_info_to_buffer() // "show info"
139 * -> stats_dump_stat_to_buffer() // "show stat"
140 * -> stats_dump_csv_header()
141 * -> stats_dump_proxy_to_buffer()
142 * -> stats_dump_fe_stats()
143 * -> stats_dump_li_stats()
144 * -> stats_dump_sv_stats()
145 * -> stats_dump_be_stats()
146 *
147 * http_stats_io_handler()
148 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
149 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
150 * -> stats_dump_html_head() // emits the HTML headers
151 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
152 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
153 * -> stats_dump_html_px_hdr()
154 * -> stats_dump_fe_stats()
155 * -> stats_dump_li_stats()
156 * -> stats_dump_sv_stats()
157 * -> stats_dump_be_stats()
158 * -> stats_dump_html_px_end()
159 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100160 */
161
Simon Horman9bd2c732011-06-15 15:18:44 +0900162static struct si_applet cli_applet;
163
164static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200165 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200166 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200167 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200168 " help : this message\n"
169 " prompt : toggle interactive mode with prompt\n"
170 " quit : disconnect\n"
171 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100172 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200173 " show stat : report counters for each proxy and server\n"
174 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100175 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200176 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200177 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200178 " set weight : change a server's weight\n"
Willy Tarreau2a4b70f2014-05-22 18:42:35 +0200179 " set server : change a server's state or weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200180 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100181 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200182 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200183 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200184 " disable : put a server or frontend in maintenance mode\n"
185 " enable : re-enable a server or frontend which is in maintenance mode\n"
186 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100187 " show acl [id] : report avalaible acls or dump an acl's contents\n"
188 " get acl : reports the patterns matching a sample for an ACL\n"
189 " add acl : add acl entry\n"
190 " del acl : delete acl entry\n"
191 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100192 " show map [id] : report avalaible maps or dump a map's contents\n"
193 " get map : reports the keys and values matching a sample for a map\n"
194 " set map : modify map entry\n"
195 " add map : add map entry\n"
196 " del map : delete map entry\n"
197 " clear map <id> : clear the content of this map\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200198 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200199
Simon Horman9bd2c732011-06-15 15:18:44 +0900200static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200201 "Permission denied\n"
202 "";
203
Willy Tarreau295a8372011-03-10 11:25:07 +0100204/* data transmission states for the stats responses */
205enum {
206 STAT_ST_INIT = 0,
207 STAT_ST_HEAD,
208 STAT_ST_INFO,
209 STAT_ST_LIST,
210 STAT_ST_END,
211 STAT_ST_FIN,
212};
213
214/* data transmission states for the stats responses inside a proxy */
215enum {
216 STAT_PX_ST_INIT = 0,
217 STAT_PX_ST_TH,
218 STAT_PX_ST_FE,
219 STAT_PX_ST_LI,
220 STAT_PX_ST_SV,
221 STAT_PX_ST_BE,
222 STAT_PX_ST_END,
223 STAT_PX_ST_FIN,
224};
225
Cyril Bonté19979e12012-04-04 12:57:21 +0200226extern const char *stat_status_codes[];
227
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200228/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200229 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200230 * needs to be closed and ignored, or a negative value upon critical failure.
231 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900232static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200233{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100234 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100235 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200236
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200237 tv_zero(&s->logs.tv_request);
238 s->logs.t_queue = 0;
239 s->logs.t_connect = 0;
240 s->logs.t_data = 0;
241 s->logs.t_close = 0;
242 s->logs.bytes_in = s->logs.bytes_out = 0;
243 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
244 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200245
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200246 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200247
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200248 if (s->listener->timeout) {
249 s->req->rto = *s->listener->timeout;
250 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200251 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200252 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200253}
254
Willy Tarreau07e9e642010-08-17 21:48:17 +0200255/* allocate a new stats frontend named <name>, and return it
256 * (or NULL in case of lack of memory).
257 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200258static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200259{
260 struct proxy *fe;
261
262 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
263 if (!fe)
264 return NULL;
265
Willy Tarreau237250c2011-07-29 01:49:03 +0200266 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200267 fe->next = proxy;
268 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200269 fe->last_change = now.tv_sec;
270 fe->id = strdup("GLOBAL");
271 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200272 fe->maxconn = 10; /* default to 10 concurrent connections */
273 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200274 fe->conf.file = strdup(file);
275 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200276 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200277
278 /* the stats frontend is the only one able to assign ID #0 */
279 fe->conf.id.key = fe->uuid = 0;
280 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200281 return fe;
282}
283
Willy Tarreaufbee7132007-10-18 13:53:22 +0200284/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200285 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
286 * error message into the <err> buffer which will be preallocated. The trailing
287 * '\n' must not be written. The function must be called with <args> pointing to
288 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200289 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200290static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200291 struct proxy *defpx, const char *file, int line,
292 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200293{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200294 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200295 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200296
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200297 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200298 int cur_arg;
299
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200300 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200301 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 +0200302 return -1;
303 }
304
Willy Tarreau89a63132009-08-16 17:41:45 +0200305 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200306 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200307 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200308 return -1;
309 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200310 }
311
Willy Tarreau4348fad2012-09-20 16:48:07 +0200312 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200313 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200314
Willy Tarreauc53d4222012-09-20 20:19:28 +0200315 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
316 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
317 file, line, args[0], args[1], err && *err ? *err : "error");
318 return -1;
319 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200320
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200321 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200322 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200323 static int bind_dumped;
324 struct bind_kw *kw;
325
326 kw = bind_find_kw(args[cur_arg]);
327 if (kw) {
328 if (!kw->parse) {
329 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
330 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200331 return -1;
332 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200333
334 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
335 if (err && *err)
336 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
337 else
338 memprintf(err, "'%s %s' : error encountered while processing '%s'",
339 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200340 return -1;
341 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200342
343 cur_arg += 1 + kw->skip;
344 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200345 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200346
347 if (!bind_dumped) {
348 bind_dump_kws(err);
349 indent_msg(err, 4);
350 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200351 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200352
353 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
354 args[0], args[1], args[cur_arg],
355 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
356 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200357 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100358
Willy Tarreauc53d4222012-09-20 20:19:28 +0200359 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
360 l->maxconn = global.stats_fe->maxconn;
361 l->backlog = global.stats_fe->backlog;
362 l->timeout = &global.stats_fe->timeout.client;
363 l->accept = session_accept;
364 l->handler = process_session;
365 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
366 l->nice = -64; /* we want to boost priority for local stats */
367 global.maxsock += l->maxconn;
368 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200369 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200370 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100371 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200372 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100373
374 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200375 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100376 return -1;
377 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200378
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100379 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200380 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200381 return -1;
382 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200383 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200384 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200385 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200386 return -1;
387 }
388 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200389 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200390 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200391 else if (!strcmp(args[1], "maxconn")) {
392 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200393
394 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200395 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200396 return -1;
397 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200398
399 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200400 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200401 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200402 return -1;
403 }
404 }
405 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200406 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200407 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
408 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100409 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200410
Willy Tarreau91319572013-04-20 09:48:50 +0200411 if (!global.stats_fe) {
412 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
413 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
414 return -1;
415 }
416 }
417
Willy Tarreau35b7b162012-10-22 23:17:18 +0200418 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100419 unsigned int low, high;
420
Willy Tarreau35b7b162012-10-22 23:17:18 +0200421 if (strcmp(args[cur_arg], "all") == 0) {
422 set = 0;
423 break;
424 }
425 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100426 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200427 }
428 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100429 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200430 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100431 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100432 char *dash = strchr(args[cur_arg], '-');
433
434 low = high = str2uic(args[cur_arg]);
435 if (dash)
436 high = str2uic(dash + 1);
437
438 if (high < low) {
439 unsigned int swap = low;
440 low = high;
441 high = swap;
442 }
443
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100444 if (low < 1 || high > LONGBITS) {
445 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
446 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200447 return -1;
448 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100449 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100450 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100451 }
452 else {
453 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100454 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
455 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100456 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200457 }
458 cur_arg++;
459 }
460 global.stats_fe->bind_proc = set;
461 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200462 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200463 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200464 return -1;
465 }
466 return 0;
467}
468
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100469/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
470 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100471 * NOTE: Some tools happen to rely on the field position instead of its name,
472 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100473 */
474static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100475{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100476 chunk_appendf(&trash,
477 "# pxname,svname,"
478 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100479 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100480 "bin,bout,"
481 "dreq,dresp,"
482 "ereq,econ,eresp,"
483 "wretr,wredis,"
484 "status,weight,act,bck,"
485 "chkfail,chkdown,lastchg,downtime,qlimit,"
486 "pid,iid,sid,throttle,lbtot,tracked,type,"
487 "rate,rate_lim,rate_max,"
488 "check_status,check_code,check_duration,"
489 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
490 "req_rate,req_rate_max,req_tot,"
491 "cli_abrt,srv_abrt,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100492 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100493 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100494}
495
Simon Hormand9366582011-06-15 15:18:45 +0900496/* print a string of text buffer to <out>. The format is :
497 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
498 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
499 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
500 */
501static int dump_text(struct chunk *out, const char *buf, int bsize)
502{
503 unsigned char c;
504 int ptr = 0;
505
506 while (buf[ptr] && ptr < bsize) {
507 c = buf[ptr];
508 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
509 if (out->len > out->size - 1)
510 break;
511 out->str[out->len++] = c;
512 }
513 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
514 if (out->len > out->size - 2)
515 break;
516 out->str[out->len++] = '\\';
517 switch (c) {
518 case ' ': c = ' '; break;
519 case '\t': c = 't'; break;
520 case '\n': c = 'n'; break;
521 case '\r': c = 'r'; break;
522 case '\e': c = 'e'; break;
523 case '\\': c = '\\'; break;
524 }
525 out->str[out->len++] = c;
526 }
527 else {
528 if (out->len > out->size - 4)
529 break;
530 out->str[out->len++] = '\\';
531 out->str[out->len++] = 'x';
532 out->str[out->len++] = hextab[(c >> 4) & 0xF];
533 out->str[out->len++] = hextab[c & 0xF];
534 }
535 ptr++;
536 }
537
538 return ptr;
539}
540
541/* print a buffer in hexa.
542 * Print stopped if <bsize> is reached, or if no more place in the chunk.
543 */
544static int dump_binary(struct chunk *out, const char *buf, int bsize)
545{
546 unsigned char c;
547 int ptr = 0;
548
549 while (ptr < bsize) {
550 c = buf[ptr];
551
552 if (out->len > out->size - 2)
553 break;
554 out->str[out->len++] = hextab[(c >> 4) & 0xF];
555 out->str[out->len++] = hextab[c & 0xF];
556
557 ptr++;
558 }
559 return ptr;
560}
561
562/* Dump the status of a table to a stream interface's
563 * read buffer. It returns 0 if the output buffer is full
564 * and needs to be called again, otherwise non-zero.
565 */
566static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
567 struct proxy *proxy, struct proxy *target)
568{
Willy Tarreau306f8302013-07-08 15:53:06 +0200569 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900570
Willy Tarreau77804732012-10-29 16:14:26 +0100571 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900572 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
573
574 /* any other information should be dumped here */
575
Willy Tarreau290e63a2012-09-20 18:07:14 +0200576 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100577 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900578
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200579 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900580 return 0;
581
582 return 1;
583}
584
585/* Dump the a table entry to a stream interface's
586 * read buffer. It returns 0 if the output buffer is full
587 * and needs to be called again, otherwise non-zero.
588 */
589static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
590 struct proxy *proxy, struct stksess *entry)
591{
592 int dt;
593
Willy Tarreau77804732012-10-29 16:14:26 +0100594 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900595
596 if (proxy->table.type == STKTABLE_TYPE_IP) {
597 char addr[INET_ADDRSTRLEN];
598 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100599 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900600 }
601 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
602 char addr[INET6_ADDRSTRLEN];
603 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100604 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900605 }
606 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100607 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900608 }
609 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100610 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900611 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
612 }
613 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100614 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900615 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
616 }
617
Willy Tarreau77804732012-10-29 16:14:26 +0100618 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900619
620 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
621 void *ptr;
622
623 if (proxy->table.data_ofs[dt] == 0)
624 continue;
625 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100626 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900627 else
Willy Tarreau77804732012-10-29 16:14:26 +0100628 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900629
630 ptr = stktable_data_ptr(&proxy->table, entry, dt);
631 switch (stktable_data_types[dt].std_type) {
632 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100633 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900634 break;
635 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100636 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900637 break;
638 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100639 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900640 break;
641 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100642 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900643 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
644 proxy->table.data_arg[dt].u));
645 break;
646 }
647 }
Willy Tarreau77804732012-10-29 16:14:26 +0100648 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900649
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200650 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900651 return 0;
652
653 return 1;
654}
655
Willy Tarreaudec98142012-06-06 23:37:08 +0200656static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900657{
Willy Tarreau306f8302013-07-08 15:53:06 +0200658 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100659 struct appctx *appctx = __objt_appctx(si->end);
660 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900661 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900662 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900663 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200664 long long value;
665 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200666 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200667 void *ptr;
668 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900669
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100670 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900671
672 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100673 appctx->ctx.cli.msg = "Key value expected\n";
674 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900675 return;
676 }
677
Simon Hormanc5b89f62011-06-15 15:18:50 +0900678 switch (px->table.type) {
679 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900680 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100681 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900682 break;
683 case STKTABLE_TYPE_IPV6:
684 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100685 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900686 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900687 case STKTABLE_TYPE_INTEGER:
688 {
689 char *endptr;
690 unsigned long val;
691 errno = 0;
692 val = strtoul(args[4], &endptr, 10);
693 if ((errno == ERANGE && val == ULONG_MAX) ||
694 (errno != 0 && val == 0) || endptr == args[4] ||
695 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100696 appctx->ctx.cli.msg = "Invalid key\n";
697 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900698 return;
699 }
700 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100701 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900702 break;
703 }
704 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900705 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100706 static_table_key->key = args[4];
707 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900708 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900709 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200710 switch (action) {
711 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100712 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 +0200713 break;
714 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100715 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 +0200716 break;
717 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100718 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200719 break;
720 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100721 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900722 return;
723 }
724
725 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200726 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100727 appctx->ctx.cli.msg = stats_permission_denied_msg;
728 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900729 return;
730 }
731
Willy Tarreau07115412012-10-29 21:56:59 +0100732 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900733
Willy Tarreaudec98142012-06-06 23:37:08 +0200734 switch (action) {
735 case STAT_CLI_O_TAB:
736 if (!ts)
737 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100738 chunk_reset(&trash);
739 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900740 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100741 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900742 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200743
744 case STAT_CLI_O_CLR:
745 if (!ts)
746 return;
747 if (ts->ref_cnt) {
748 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100749 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
750 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200751 return;
752 }
753 stksess_kill(&px->table, ts);
754 break;
Simon Horman17bce342011-06-15 15:18:47 +0900755
Willy Tarreau654694e2012-06-07 01:03:16 +0200756 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200757 if (ts)
758 stktable_touch(&px->table, ts, 1);
759 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100760 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200761 if (!ts) {
762 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100763 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
764 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200765 return;
766 }
767 stktable_store(&px->table, ts, 1);
768 }
769
Willy Tarreau47060b62013-08-01 21:11:42 +0200770 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
771 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100772 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
773 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200774 return;
775 }
776
777 data_type = stktable_get_data_type(args[cur_arg] + 5);
778 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100779 appctx->ctx.cli.msg = "Unknown data type\n";
780 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200781 return;
782 }
783
784 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100785 appctx->ctx.cli.msg = "Data type not stored in this table\n";
786 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200787 return;
788 }
789
790 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100791 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
792 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200793 return;
794 }
795
796 ptr = stktable_data_ptr(&px->table, ts, data_type);
797
798 switch (stktable_data_types[data_type].std_type) {
799 case STD_T_SINT:
800 stktable_data_cast(ptr, std_t_sint) = value;
801 break;
802 case STD_T_UINT:
803 stktable_data_cast(ptr, std_t_uint) = value;
804 break;
805 case STD_T_ULL:
806 stktable_data_cast(ptr, std_t_ull) = value;
807 break;
808 case STD_T_FRQP:
809 /* We set both the current and previous values. That way
810 * the reported frequency is stable during all the period
811 * then slowly fades out. This allows external tools to
812 * push measures without having to update them too often.
813 */
814 frqp = &stktable_data_cast(ptr, std_t_frqp);
815 frqp->curr_tick = now_ms;
816 frqp->prev_ctr = 0;
817 frqp->curr_ctr = value;
818 break;
819 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200820 }
821 break;
822
Willy Tarreaudec98142012-06-06 23:37:08 +0200823 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100824 appctx->ctx.cli.msg = "Unknown action\n";
825 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200826 break;
Simon Horman121f3052011-06-15 15:18:46 +0900827 }
Simon Horman121f3052011-06-15 15:18:46 +0900828}
829
Willy Tarreau654694e2012-06-07 01:03:16 +0200830static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900831{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100832 struct appctx *appctx = __objt_appctx(si->end);
833
Willy Tarreau04b3a192013-04-13 09:41:37 +0200834 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100835 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
836 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200837 return;
838 }
839
Simon Hormand5b9fd92011-06-15 15:18:48 +0900840 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100841 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
842 if (appctx->ctx.table.data_type < 0) {
843 appctx->ctx.cli.msg = "Unknown data type\n";
844 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900845 return;
846 }
847
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100848 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
849 appctx->ctx.cli.msg = "Data type not stored in this table\n";
850 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900851 return;
852 }
853
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100854 appctx->ctx.table.data_op = get_std_op(args[4]);
855 if (appctx->ctx.table.data_op < 0) {
856 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
857 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900858 return;
859 }
860
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100861 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
862 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
863 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900864 return;
865 }
866}
867
Willy Tarreaudec98142012-06-06 23:37:08 +0200868static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900869{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100870 struct appctx *appctx = __objt_appctx(si->end);
871
872 appctx->ctx.table.data_type = -1;
873 appctx->st2 = STAT_ST_INIT;
874 appctx->ctx.table.target = NULL;
875 appctx->ctx.table.proxy = NULL;
876 appctx->ctx.table.entry = NULL;
877 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900878
879 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100880 appctx->ctx.table.target = find_stktable(args[2]);
881 if (!appctx->ctx.table.target) {
882 appctx->ctx.cli.msg = "No such table\n";
883 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900884 return;
885 }
886 }
887 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200888 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900889 goto err_args;
890 return;
891 }
892
893 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200894 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900895 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200896 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900897 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900898 goto err_args;
899
900 return;
901
902err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200903 switch (action) {
904 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100905 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 +0200906 break;
907 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100908 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 +0200909 break;
910 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100911 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200912 break;
913 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100914 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900915}
916
Willy Tarreau532a4502011-09-07 22:37:44 +0200917/* Expects to find a frontend named <arg> and returns it, otherwise displays various
918 * adequate error messages and returns NULL. This function also expects the session
919 * level to be admin.
920 */
921static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
922{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100923 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200924 struct proxy *px;
925
Willy Tarreau290e63a2012-09-20 18:07:14 +0200926 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100927 appctx->ctx.cli.msg = stats_permission_denied_msg;
928 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200929 return NULL;
930 }
931
932 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100933 appctx->ctx.cli.msg = "A frontend name is expected.\n";
934 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200935 return NULL;
936 }
937
938 px = findproxy(arg, PR_CAP_FE);
939 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100940 appctx->ctx.cli.msg = "No such frontend.\n";
941 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200942 return NULL;
943 }
944 return px;
945}
946
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200947/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
948 * and returns the pointer to the server. Otherwise, display adequate error messages
949 * and returns NULL. This function also expects the session level to be admin. Note:
950 * the <arg> is modified to remove the '/'.
951 */
952static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
953{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100954 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200955 struct proxy *px;
956 struct server *sv;
957 char *line;
958
Willy Tarreau290e63a2012-09-20 18:07:14 +0200959 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100960 appctx->ctx.cli.msg = stats_permission_denied_msg;
961 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200962 return NULL;
963 }
964
965 /* split "backend/server" and make <line> point to server */
966 for (line = arg; *line; line++)
967 if (*line == '/') {
968 *line++ = '\0';
969 break;
970 }
971
972 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100973 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
974 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200975 return NULL;
976 }
977
978 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100979 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
980 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200981 return NULL;
982 }
983
984 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100985 appctx->ctx.cli.msg = "Proxy is disabled.\n";
986 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200987 return NULL;
988 }
989
990 return sv;
991}
992
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100993/* This function is used with map and acl management. It permits to browse
994 * each reference. The variable <getnext> must contain the current node,
995 * <end> point to the root node and the <flags> permit to filter required
996 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100997 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100998static inline
999struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1000 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001001{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001002 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001003
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001004 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001005
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001006 /* Get next list entry. */
1007 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001008
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001009 /* If the entry is the last of the list, return NULL. */
1010 if (&ref->list == end)
1011 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001012
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001013 /* If the entry match the flag, return it. */
1014 if (ref->flags & flags)
1015 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001016 }
1017}
1018
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001019static inline
1020struct pat_ref *pat_ref_lookup_ref(const char *reference)
1021{
1022 int id;
1023 char *error;
1024
1025 /* If the reference starts by a '#', this is numeric id. */
1026 if (reference[0] == '#') {
1027 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1028 id = strtol(reference + 1, &error, 10);
1029 if (*error != '\0')
1030 return NULL;
1031
1032 /* Perform the unique id lookup. */
1033 return pat_ref_lookupid(id);
1034 }
1035
1036 /* Perform the string lookup. */
1037 return pat_ref_lookup(reference);
1038}
1039
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001040/* This function is used with map and acl management. It permits to browse
1041 * each reference.
1042 */
1043static inline
1044struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1045{
1046 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001047 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1048 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001049 return NULL;
1050 return expr;
1051}
1052
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001053/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001054 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001055 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001056 * designating the function which will have to process the request, which can
1057 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001058 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001059static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001060{
Willy Tarreau306f8302013-07-08 15:53:06 +02001061 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001062 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001063 char *args[MAX_STATS_ARGS + 1];
1064 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001065 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001066
1067 while (isspace((unsigned char)*line))
1068 line++;
1069
1070 arg = 0;
1071 args[arg] = line;
1072
1073 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001074 if (*line == '\\') {
1075 line++;
1076 if (*line == '\0')
1077 break;
1078 }
1079 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001080 *line++ = '\0';
1081
1082 while (isspace((unsigned char)*line))
1083 line++;
1084
1085 args[++arg] = line;
1086 continue;
1087 }
1088
1089 line++;
1090 }
1091
1092 while (++arg <= MAX_STATS_ARGS)
1093 args[arg] = line;
1094
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001095 /* remove \ */
1096 arg = 0;
1097 while (*args[arg] != '\0') {
1098 j = 0;
1099 for (i=0; args[arg][i] != '\0'; i++) {
1100 if (args[arg][i] == '\\')
1101 continue;
1102 args[arg][j] = args[arg][i];
1103 j++;
1104 }
1105 args[arg][j] = '\0';
1106 arg++;
1107 }
1108
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001109 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001110 if (strcmp(args[0], "show") == 0) {
1111 if (strcmp(args[1], "stat") == 0) {
1112 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001113 appctx->ctx.stats.flags |= STAT_BOUND;
1114 appctx->ctx.stats.iid = atoi(args[2]);
1115 appctx->ctx.stats.type = atoi(args[3]);
1116 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001117 }
1118
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001119 appctx->st2 = STAT_ST_INIT;
1120 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001121 }
1122 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001123 appctx->st2 = STAT_ST_INIT;
1124 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001125 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001126 else if (strcmp(args[1], "pools") == 0) {
1127 appctx->st2 = STAT_ST_INIT;
1128 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1129 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001130 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001131 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001132 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001133 appctx->ctx.cli.msg = stats_permission_denied_msg;
1134 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001135 return 1;
1136 }
Willy Tarreau76153662012-11-26 01:16:39 +01001137 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001138 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001139 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001140 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001141 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001142 appctx->ctx.sess.target = NULL;
1143 appctx->ctx.sess.section = 0; /* start with session status */
1144 appctx->ctx.sess.pos = 0;
1145 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001146 }
1147 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001148 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001149 appctx->ctx.cli.msg = stats_permission_denied_msg;
1150 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001151 return 1;
1152 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001153 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001154 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001155 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001156 appctx->ctx.errors.iid = -1;
1157 appctx->ctx.errors.px = NULL;
1158 appctx->st2 = STAT_ST_INIT;
1159 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001160 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001161 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001162 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001163 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001164 else if (strcmp(args[1], "map") == 0 ||
1165 strcmp(args[1], "acl") == 0) {
1166
1167 /* Set ACL or MAP flags. */
1168 if (args[1][0] == 'm')
1169 appctx->ctx.map.display_flags = PAT_REF_MAP;
1170 else
1171 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001172
1173 /* no parameter: display all map avalaible */
1174 if (!*args[2]) {
1175 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001176 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001177 return 1;
1178 }
1179
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001180 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001181 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001182 if (!appctx->ctx.map.ref ||
1183 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1184 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001185 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001186 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001187 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001188 appctx->st0 = STAT_CLI_PRINT;
1189 return 1;
1190 }
1191 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001192 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001193 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001194 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001195 return 0;
1196 }
1197 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001198 else if (strcmp(args[0], "clear") == 0) {
1199 if (strcmp(args[1], "counters") == 0) {
1200 struct proxy *px;
1201 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001202 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001203 int clrall = 0;
1204
1205 if (strcmp(args[2], "all") == 0)
1206 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001207
Willy Tarreau6162db22009-10-10 17:13:00 +02001208 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001209 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1210 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001211 appctx->ctx.cli.msg = stats_permission_denied_msg;
1212 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001213 return 1;
1214 }
1215
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001216 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001217 if (clrall) {
1218 memset(&px->be_counters, 0, sizeof(px->be_counters));
1219 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1220 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001221 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001222 px->be_counters.conn_max = 0;
1223 px->be_counters.p.http.rps_max = 0;
1224 px->be_counters.sps_max = 0;
1225 px->be_counters.cps_max = 0;
1226 px->be_counters.nbpend_max = 0;
1227
1228 px->fe_counters.conn_max = 0;
1229 px->fe_counters.p.http.rps_max = 0;
1230 px->fe_counters.sps_max = 0;
1231 px->fe_counters.cps_max = 0;
1232 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001233 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001234
1235 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001236 if (clrall)
1237 memset(&sv->counters, 0, sizeof(sv->counters));
1238 else {
1239 sv->counters.cur_sess_max = 0;
1240 sv->counters.nbpend_max = 0;
1241 sv->counters.sps_max = 0;
1242 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001243
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001244 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001245 if (li->counters) {
1246 if (clrall)
1247 memset(li->counters, 0, sizeof(*li->counters));
1248 else
1249 li->counters->conn_max = 0;
1250 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001251 }
1252
Willy Tarreau81c25d02011-09-07 15:17:21 +02001253 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001254 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001255 return 1;
1256 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001257 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001258 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001259 /* end of processing */
1260 return 1;
1261 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001262 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1263 /* Set ACL or MAP flags. */
1264 if (args[1][0] == 'm')
1265 appctx->ctx.map.display_flags = PAT_REF_MAP;
1266 else
1267 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001268
1269 /* no parameter */
1270 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001271 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1272 appctx->ctx.cli.msg = "Missing map identifier.\n";
1273 else
1274 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001275 appctx->st0 = STAT_CLI_PRINT;
1276 return 1;
1277 }
1278
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001279 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001280 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001281 if (!appctx->ctx.map.ref ||
1282 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1283 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001284 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001285 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001286 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001287 appctx->st0 = STAT_CLI_PRINT;
1288 return 1;
1289 }
1290
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001291 /* Clear all. */
1292 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001293
1294 /* return response */
1295 appctx->ctx.cli.msg = "Done.\n";
1296 appctx->st0 = STAT_CLI_PRINT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001297 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001298 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001299 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001300 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001301 return 0;
1302 }
1303 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001304 else if (strcmp(args[0], "get") == 0) {
1305 if (strcmp(args[1], "weight") == 0) {
1306 struct proxy *px;
1307 struct server *sv;
1308
1309 /* split "backend/server" and make <line> point to server */
1310 for (line = args[2]; *line; line++)
1311 if (*line == '/') {
1312 *line++ = '\0';
1313 break;
1314 }
1315
1316 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001317 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1318 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001319 return 1;
1320 }
1321
1322 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001323 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1324 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001325 return 1;
1326 }
1327
1328 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001329 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1330 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001331 return 1;
1332 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001333 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1334 /* Set flags. */
1335 if (args[1][0] == 'm')
1336 appctx->ctx.map.display_flags = PAT_REF_MAP;
1337 else
1338 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001339
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001340 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001341 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001342 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1343 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1344 else
1345 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001346 appctx->st0 = STAT_CLI_PRINT;
1347 return 1;
1348 }
1349
1350 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001351 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001352 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001353 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001354 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001355 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001356 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001357 appctx->st0 = STAT_CLI_PRINT;
1358 return 1;
1359 }
1360
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001361 /* copy input string. The string must be allocated because
1362 * it may be used over multiple iterations. It's released
1363 * at the end and upon abort anyway.
1364 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001365 appctx->ctx.map.chunk.len = strlen(args[3]);
1366 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1367 appctx->ctx.map.chunk.str = strdup(args[3]);
1368 if (!appctx->ctx.map.chunk.str) {
1369 appctx->ctx.cli.msg = "Out of memory error.\n";
1370 appctx->st0 = STAT_CLI_PRINT;
1371 return 1;
1372 }
1373
1374 /* prepare response */
1375 appctx->st2 = STAT_ST_INIT;
1376 appctx->st0 = STAT_CLI_O_MLOOK;
1377 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001378 else { /* not "get weight" */
1379 return 0;
1380 }
1381 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001382 else if (strcmp(args[0], "set") == 0) {
1383 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001384 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001385 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001386
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001387 sv = expect_server_admin(s, si, args[2]);
1388 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001389 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001390
Simon Horman7d09b9a2013-02-12 10:45:51 +09001391 warning = server_parse_weight_change_request(sv, args[3]);
1392 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001393 appctx->ctx.cli.msg = warning;
1394 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001395 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001396 return 1;
1397 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001398 else if (strcmp(args[1], "server") == 0) {
1399 struct server *sv;
1400 const char *warning;
1401
1402 sv = expect_server_admin(s, si, args[2]);
1403 if (!sv)
1404 return 1;
1405
1406 if (strcmp(args[3], "weight") == 0) {
1407 warning = server_parse_weight_change_request(sv, args[4]);
1408 if (warning) {
1409 appctx->ctx.cli.msg = warning;
1410 appctx->st0 = STAT_CLI_PRINT;
1411 }
1412 }
1413 else if (strcmp(args[3], "state") == 0) {
1414 if (strcmp(args[4], "ready") == 0)
1415 srv_adm_set_ready(sv);
1416 else if (strcmp(args[4], "drain") == 0)
1417 srv_adm_set_drain(sv);
1418 else if (strcmp(args[4], "maint") == 0)
1419 srv_adm_set_maint(sv);
1420 else {
1421 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1422 appctx->st0 = STAT_CLI_PRINT;
1423 }
1424 }
1425 else if (strcmp(args[3], "health") == 0) {
1426 if (sv->track) {
1427 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1428 appctx->st0 = STAT_CLI_PRINT;
1429 }
1430 else if (strcmp(args[4], "up") == 0) {
1431 sv->check.health = sv->check.rise + sv->check.fall - 1;
1432 srv_set_running(sv, "changed from CLI");
1433 }
1434 else if (strcmp(args[4], "stopping") == 0) {
1435 sv->check.health = sv->check.rise + sv->check.fall - 1;
1436 srv_set_stopping(sv, "changed from CLI");
1437 }
1438 else if (strcmp(args[4], "down") == 0) {
1439 sv->check.health = 0;
1440 srv_set_stopped(sv, "changed from CLI");
1441 }
1442 else {
1443 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1444 appctx->st0 = STAT_CLI_PRINT;
1445 }
1446 }
1447 else if (strcmp(args[3], "agent") == 0) {
1448 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1449 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1450 appctx->st0 = STAT_CLI_PRINT;
1451 }
1452 else if (strcmp(args[4], "up") == 0) {
1453 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1454 srv_set_running(sv, "changed from CLI");
1455 }
1456 else if (strcmp(args[4], "down") == 0) {
1457 sv->agent.health = 0;
1458 srv_set_stopped(sv, "changed from CLI");
1459 }
1460 else {
1461 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1462 appctx->st0 = STAT_CLI_PRINT;
1463 }
1464 }
1465 else {
1466 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1467 appctx->st0 = STAT_CLI_PRINT;
1468 }
1469 return 1;
1470 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001471 else if (strcmp(args[1], "timeout") == 0) {
1472 if (strcmp(args[2], "cli") == 0) {
1473 unsigned timeout;
1474 const char *res;
1475
1476 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001477 appctx->ctx.cli.msg = "Expects an integer value.\n";
1478 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001479 return 1;
1480 }
1481
1482 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1483 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001484 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1485 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001486 return 1;
1487 }
1488
1489 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1490 return 1;
1491 }
1492 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001493 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1494 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001495 return 1;
1496 }
1497 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001498 else if (strcmp(args[1], "maxconn") == 0) {
1499 if (strcmp(args[2], "frontend") == 0) {
1500 struct proxy *px;
1501 struct listener *l;
1502 int v;
1503
Willy Tarreau532a4502011-09-07 22:37:44 +02001504 px = expect_frontend_admin(s, si, args[3]);
1505 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001506 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001507
1508 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001509 appctx->ctx.cli.msg = "Integer value expected.\n";
1510 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001511 return 1;
1512 }
1513
1514 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001515 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001516 appctx->ctx.cli.msg = "Value out of range.\n";
1517 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001518 return 1;
1519 }
1520
1521 /* OK, the value is fine, so we assign it to the proxy and to all of
1522 * its listeners. The blocked ones will be dequeued.
1523 */
1524 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001525 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001526 l->maxconn = v;
1527 if (l->state == LI_FULL)
1528 resume_listener(l);
1529 }
1530
1531 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1532 dequeue_all_listeners(&s->fe->listener_queue);
1533
1534 return 1;
1535 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001536 else if (strcmp(args[2], "global") == 0) {
1537 int v;
1538
Willy Tarreau290e63a2012-09-20 18:07:14 +02001539 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001540 appctx->ctx.cli.msg = stats_permission_denied_msg;
1541 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001542 return 1;
1543 }
1544
1545 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001546 appctx->ctx.cli.msg = "Expects an integer value.\n";
1547 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001548 return 1;
1549 }
1550
1551 v = atoi(args[3]);
1552 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001553 appctx->ctx.cli.msg = "Value out of range.\n";
1554 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001555 return 1;
1556 }
1557
1558 /* check for unlimited values */
1559 if (v <= 0)
1560 v = global.hardmaxconn;
1561
1562 global.maxconn = v;
1563
1564 /* Dequeues all of the listeners waiting for a resource */
1565 if (!LIST_ISEMPTY(&global_listener_queue))
1566 dequeue_all_listeners(&global_listener_queue);
1567
1568 return 1;
1569 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001570 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001571 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1572 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001573 return 1;
1574 }
1575 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001576 else if (strcmp(args[1], "rate-limit") == 0) {
1577 if (strcmp(args[2], "connections") == 0) {
1578 if (strcmp(args[3], "global") == 0) {
1579 int v;
1580
Willy Tarreau290e63a2012-09-20 18:07:14 +02001581 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001582 appctx->ctx.cli.msg = stats_permission_denied_msg;
1583 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001584 return 1;
1585 }
1586
1587 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001588 appctx->ctx.cli.msg = "Expects an integer value.\n";
1589 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001590 return 1;
1591 }
1592
1593 v = atoi(args[4]);
1594 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001595 appctx->ctx.cli.msg = "Value out of range.\n";
1596 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001597 return 1;
1598 }
1599
1600 global.cps_lim = v;
1601
1602 /* Dequeues all of the listeners waiting for a resource */
1603 if (!LIST_ISEMPTY(&global_listener_queue))
1604 dequeue_all_listeners(&global_listener_queue);
1605
1606 return 1;
1607 }
1608 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001609 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1610 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001611 return 1;
1612 }
1613 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001614 else if (strcmp(args[2], "sessions") == 0) {
1615 if (strcmp(args[3], "global") == 0) {
1616 int v;
1617
1618 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1619 appctx->ctx.cli.msg = stats_permission_denied_msg;
1620 appctx->st0 = STAT_CLI_PRINT;
1621 return 1;
1622 }
1623
1624 if (!*args[4]) {
1625 appctx->ctx.cli.msg = "Expects an integer value.\n";
1626 appctx->st0 = STAT_CLI_PRINT;
1627 return 1;
1628 }
1629
1630 v = atoi(args[4]);
1631 if (v < 0) {
1632 appctx->ctx.cli.msg = "Value out of range.\n";
1633 appctx->st0 = STAT_CLI_PRINT;
1634 return 1;
1635 }
1636
1637 global.sps_lim = v;
1638
1639 /* Dequeues all of the listeners waiting for a resource */
1640 if (!LIST_ISEMPTY(&global_listener_queue))
1641 dequeue_all_listeners(&global_listener_queue);
1642
1643 return 1;
1644 }
1645 else {
1646 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1647 appctx->st0 = STAT_CLI_PRINT;
1648 return 1;
1649 }
1650 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001651#ifdef USE_OPENSSL
1652 else if (strcmp(args[2], "ssl-sessions") == 0) {
1653 if (strcmp(args[3], "global") == 0) {
1654 int v;
1655
1656 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1657 appctx->ctx.cli.msg = stats_permission_denied_msg;
1658 appctx->st0 = STAT_CLI_PRINT;
1659 return 1;
1660 }
1661
1662 if (!*args[4]) {
1663 appctx->ctx.cli.msg = "Expects an integer value.\n";
1664 appctx->st0 = STAT_CLI_PRINT;
1665 return 1;
1666 }
1667
1668 v = atoi(args[4]);
1669 if (v < 0) {
1670 appctx->ctx.cli.msg = "Value out of range.\n";
1671 appctx->st0 = STAT_CLI_PRINT;
1672 return 1;
1673 }
1674
1675 global.ssl_lim = v;
1676
1677 /* Dequeues all of the listeners waiting for a resource */
1678 if (!LIST_ISEMPTY(&global_listener_queue))
1679 dequeue_all_listeners(&global_listener_queue);
1680
1681 return 1;
1682 }
1683 else {
1684 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1685 appctx->st0 = STAT_CLI_PRINT;
1686 return 1;
1687 }
1688 }
1689#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001690 else if (strcmp(args[2], "http-compression") == 0) {
1691 if (strcmp(args[3], "global") == 0) {
1692 int v;
1693
Willy Tarreau85d47f92012-11-21 00:29:50 +01001694 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001695 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1696 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001697 return 1;
1698 }
1699
William Lallemandd85f9172012-11-09 17:05:39 +01001700 v = atoi(args[4]);
1701 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1702 }
1703 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001704 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1705 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001706 return 1;
1707 }
1708 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001709 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001710 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001711 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001712 return 1;
1713 }
1714 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001715 else if (strcmp(args[1], "table") == 0) {
1716 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1717 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001718 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001719 char *err;
1720
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001721 /* Set flags. */
1722 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001723
1724 /* Expect three parameters: map name, key and new value. */
1725 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001726 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001727 appctx->st0 = STAT_CLI_PRINT;
1728 return 1;
1729 }
1730
1731 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001732 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001733 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001734 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001735 appctx->st0 = STAT_CLI_PRINT;
1736 return 1;
1737 }
1738
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001739 /* If the entry identifier start with a '#', it is considered as
1740 * pointer id
1741 */
1742 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1743 struct pat_ref_elt *ref;
1744 long long int conv;
1745 char *error;
1746
1747 /* Convert argument to integer value. */
1748 conv = strtoll(&args[3][1], &error, 16);
1749 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001750 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001751 appctx->st0 = STAT_CLI_PRINT;
1752 return 1;
1753 }
1754
1755 /* Convert and check integer to pointer. */
1756 ref = (struct pat_ref_elt *)(long)conv;
1757 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001758 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001759 appctx->st0 = STAT_CLI_PRINT;
1760 return 1;
1761 }
1762
1763 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001764 err = NULL;
1765 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1766 if (err)
1767 memprintf(&err, "%s.\n", err);
1768 appctx->ctx.cli.err = err;
1769 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001770 return 1;
1771 }
1772 }
1773 else {
1774 /* Else, use the entry identifier as pattern
1775 * string, and update the value.
1776 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001777 err = NULL;
1778 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1779 if (err)
1780 memprintf(&err, "%s.\n", err);
1781 appctx->ctx.cli.err = err;
1782 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001783 return 1;
1784 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001785 }
1786
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001787 /* The set is done, send message. */
1788 appctx->ctx.cli.msg = "Done.\n";
1789 appctx->st0 = STAT_CLI_PRINT;
1790 return 1;
1791 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001792 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001793 return 0;
1794 }
1795 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001796 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001797 if (strcmp(args[1], "agent") == 0) {
1798 struct server *sv;
1799
1800 sv = expect_server_admin(s, si, args[2]);
1801 if (!sv)
1802 return 1;
1803
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001804 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1805 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1806 appctx->st0 = STAT_CLI_PRINT;
1807 return 1;
1808 }
1809
1810 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001811 return 1;
1812 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001813 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001814 struct server *sv;
1815
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001816 sv = expect_server_admin(s, si, args[2]);
1817 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001818 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001819
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001820 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
1821 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
1822 appctx->st0 = STAT_CLI_PRINT;
1823 return 1;
1824 }
1825
1826 sv->check.state |= CHK_ST_ENABLED;
1827 return 1;
1828 }
1829 else if (strcmp(args[1], "server") == 0) {
1830 struct server *sv;
1831
1832 sv = expect_server_admin(s, si, args[2]);
1833 if (!sv)
1834 return 1;
1835
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001836 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001837 return 1;
1838 }
1839 else if (strcmp(args[1], "frontend") == 0) {
1840 struct proxy *px;
1841
1842 px = expect_frontend_admin(s, si, args[2]);
1843 if (!px)
1844 return 1;
1845
1846 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001847 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1848 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001849 return 1;
1850 }
1851
1852 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001853 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1854 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001855 return 1;
1856 }
1857
1858 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001859 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1860 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001861 return 1;
1862 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001863 return 1;
1864 }
1865 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001866 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001867 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001868 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001869 }
1870 }
1871 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001872 if (strcmp(args[1], "agent") == 0) {
1873 struct server *sv;
1874
1875 sv = expect_server_admin(s, si, args[2]);
1876 if (!sv)
1877 return 1;
1878
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001879 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001880 return 1;
1881 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001882 else if (strcmp(args[1], "health") == 0) {
1883 struct server *sv;
1884
1885 sv = expect_server_admin(s, si, args[2]);
1886 if (!sv)
1887 return 1;
1888
1889 sv->check.state &= ~CHK_ST_ENABLED;
1890 return 1;
1891 }
Simon Horman671b6f02013-11-25 10:46:39 +09001892 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001893 struct server *sv;
1894
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001895 sv = expect_server_admin(s, si, args[2]);
1896 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001897 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001898
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001899 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001900 return 1;
1901 }
1902 else if (strcmp(args[1], "frontend") == 0) {
1903 struct proxy *px;
1904
1905 px = expect_frontend_admin(s, si, args[2]);
1906 if (!px)
1907 return 1;
1908
1909 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001910 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1911 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001912 return 1;
1913 }
1914
1915 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001916 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1917 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001918 return 1;
1919 }
1920
1921 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001922 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1923 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001924 return 1;
1925 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001926 return 1;
1927 }
1928 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02001929 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001930 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001931 return 1;
1932 }
1933 }
1934 else if (strcmp(args[0], "shutdown") == 0) {
1935 if (strcmp(args[1], "frontend") == 0) {
1936 struct proxy *px;
1937
1938 px = expect_frontend_admin(s, si, args[2]);
1939 if (!px)
1940 return 1;
1941
1942 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001943 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1944 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001945 return 1;
1946 }
1947
1948 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1949 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1950 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1951 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1952 stop_proxy(px);
1953 return 1;
1954 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001955 else if (strcmp(args[1], "session") == 0) {
1956 struct session *sess, *ptr;
1957
Willy Tarreau290e63a2012-09-20 18:07:14 +02001958 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001959 appctx->ctx.cli.msg = stats_permission_denied_msg;
1960 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001961 return 1;
1962 }
1963
1964 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001965 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1966 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001967 return 1;
1968 }
1969
1970 ptr = (void *)strtoul(args[2], NULL, 0);
1971
1972 /* first, look for the requested session in the session table */
1973 list_for_each_entry(sess, &sessions, list) {
1974 if (sess == ptr)
1975 break;
1976 }
1977
1978 /* do we have the session ? */
1979 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001980 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1981 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001982 return 1;
1983 }
1984
1985 session_shutdown(sess, SN_ERR_KILLED);
1986 return 1;
1987 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001988 else if (strcmp(args[1], "sessions") == 0) {
1989 if (strcmp(args[2], "server") == 0) {
1990 struct server *sv;
1991 struct session *sess, *sess_bck;
1992
1993 sv = expect_server_admin(s, si, args[3]);
1994 if (!sv)
1995 return 1;
1996
1997 /* kill all the session that are on this server */
1998 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1999 if (sess->srv_conn == sv)
2000 session_shutdown(sess, SN_ERR_KILLED);
2001
2002 return 1;
2003 }
2004 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002005 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2006 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002007 return 1;
2008 }
2009 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002010 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002011 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2012 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002013 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002014 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002015 }
2016 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002017 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2018 if (args[1][0] == 'm')
2019 appctx->ctx.map.display_flags = PAT_REF_MAP;
2020 else
2021 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002022
2023 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002024 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2025 if (!*args[2] || !*args[3]) {
2026 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2027 appctx->st0 = STAT_CLI_PRINT;
2028 return 1;
2029 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002030 }
2031
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002032 else {
2033 if (!*args[2] || !*args[3]) {
2034 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2035 appctx->st0 = STAT_CLI_PRINT;
2036 return 1;
2037 }
2038 }
2039
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002040 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002041 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002042 if (!appctx->ctx.map.ref ||
2043 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002044 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002045 appctx->st0 = STAT_CLI_PRINT;
2046 return 1;
2047 }
2048
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002049 /* If the entry identifier start with a '#', it is considered as
2050 * pointer id
2051 */
2052 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2053 struct pat_ref_elt *ref;
2054 long long int conv;
2055 char *error;
2056
2057 /* Convert argument to integer value. */
2058 conv = strtoll(&args[3][1], &error, 16);
2059 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002060 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002061 appctx->st0 = STAT_CLI_PRINT;
2062 return 1;
2063 }
2064
2065 /* Convert and check integer to pointer. */
2066 ref = (struct pat_ref_elt *)(long)conv;
2067 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002068 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002069 appctx->st0 = STAT_CLI_PRINT;
2070 return 1;
2071 }
2072
2073 /* Try to delete the entry. */
2074 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2075 /* The entry is not found, send message. */
2076 appctx->ctx.cli.msg = "Key not found.\n";
2077 appctx->st0 = STAT_CLI_PRINT;
2078 return 1;
2079 }
2080 }
2081 else {
2082 /* Else, use the entry identifier as pattern
2083 * string and try to delete the entry.
2084 */
2085 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2086 /* The entry is not found, send message. */
2087 appctx->ctx.cli.msg = "Key not found.\n";
2088 appctx->st0 = STAT_CLI_PRINT;
2089 return 1;
2090 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002091 }
2092
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002093 /* The deletion is done, send message. */
2094 appctx->ctx.cli.msg = "Done.\n";
2095 appctx->st0 = STAT_CLI_PRINT;
2096 return 1;
2097 }
2098 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002099 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002100 appctx->st0 = STAT_CLI_PRINT;
2101 return 1;
2102 }
2103 }
2104 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002105 if (strcmp(args[1], "map") == 0 ||
2106 strcmp(args[1], "acl") == 0) {
2107 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002108 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002109
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002110 /* Set flags. */
2111 if (args[1][0] == 'm')
2112 appctx->ctx.map.display_flags = PAT_REF_MAP;
2113 else
2114 appctx->ctx.map.display_flags = PAT_REF_ACL;
2115
2116 /* If the keywork is "map", we expect three parameters, if it
2117 * is "acl", we expect only two parameters
2118 */
2119 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2120 if (!*args[2] || !*args[3] || !*args[4]) {
2121 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2122 appctx->st0 = STAT_CLI_PRINT;
2123 return 1;
2124 }
2125 }
2126 else {
2127 if (!*args[2] || !*args[3]) {
2128 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2129 appctx->st0 = STAT_CLI_PRINT;
2130 return 1;
2131 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002132 }
2133
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002134 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002135 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002136 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002137 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002138 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002139 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002140 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002141 appctx->st0 = STAT_CLI_PRINT;
2142 return 1;
2143 }
2144
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002145 /* The command "add acl" is prohibited if the reference
2146 * use samples.
2147 */
2148 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2149 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2150 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2151 "You must use the command 'add map' to add values.\n";
2152 appctx->st0 = STAT_CLI_PRINT;
2153 return 1;
2154 }
2155
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002156 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002157 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002158 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002159 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002160 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002161 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002162 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002163 if (err)
2164 memprintf(&err, "%s.\n", err);
2165 appctx->ctx.cli.err = err;
2166 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002167 return 1;
2168 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002169
2170 /* The add is done, send message. */
2171 appctx->ctx.cli.msg = "Done.\n";
2172 appctx->st0 = STAT_CLI_PRINT;
2173 return 1;
2174 }
2175 else { /* unknown "del" parameter */
2176 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2177 appctx->st0 = STAT_CLI_PRINT;
2178 return 1;
2179 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002180 }
2181 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002182 return 0;
2183 }
2184 return 1;
2185}
2186
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002187/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002188 * used to processes I/O from/to the stats unix socket. The system relies on a
2189 * state machine handling requests and various responses. We read a request,
2190 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002191 * Then we can read again. The state is stored in appctx->st0 and is one of the
2192 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002193 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002194 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002195static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002196{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002197 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002198 struct channel *req = si->ob;
2199 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002200 int reql;
2201 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002202
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002203 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2204 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002205
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002206 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002207 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002208 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002209 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2210 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002211 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002212 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002213 /* Let's close for real now. We just close the request
2214 * side, the conditions below will complete if needed.
2215 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002216 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002217 break;
2218 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002219 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002220 /* ensure we have some output room left in the event we
2221 * would want to return some info right after parsing.
2222 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002223 if (buffer_almost_full(si->ib->buf)) {
2224 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002225 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002226 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002227
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002228 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002229 if (reql <= 0) { /* closed or EOL not found */
2230 if (reql == 0)
2231 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002232 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002233 continue;
2234 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002235
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002236 /* seek for a possible semi-colon. If we find one, we
2237 * replace it with an LF and skip only this part.
2238 */
2239 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002240 if (trash.str[len] == ';') {
2241 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002242 reql = len + 1;
2243 break;
2244 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002245
Willy Tarreau816fc222009-10-04 07:36:58 +02002246 /* now it is time to check that we have a full line,
2247 * remove the trailing \n and possibly \r, then cut the
2248 * line.
2249 */
2250 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002251 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002252 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002253 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002254 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002255
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002256 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002257 len--;
2258
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002259 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002260
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002261 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002262 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002263 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002264 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002265 continue;
2266 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002267 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002268 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002269 else if (strcmp(trash.str, "help") == 0 ||
2270 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002271 appctx->ctx.cli.msg = stats_sock_usage_msg;
2272 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002273 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002274 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002275 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002276 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002277 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002278 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002279 /* if prompt is disabled, print help on empty lines,
2280 * so that the user at least knows how to enable
2281 * prompt and find help.
2282 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002283 appctx->ctx.cli.msg = stats_sock_usage_msg;
2284 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002285 }
2286
2287 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002288 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002289 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002290 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002291 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002292 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002293 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002294 continue;
2295 }
2296
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002297 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002298 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002299 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2300 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002301 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002302 case STAT_CLI_PRINT_FREE:
2303 if (bi_putstr(si->ib, appctx->ctx.cli.err) != -1) {
2304 free(appctx->ctx.cli.err);
2305 appctx->st0 = STAT_CLI_PROMPT;
2306 }
2307 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002308 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002309 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002310 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002311 break;
2312 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002313 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002314 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002315 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002316 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002317 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002318 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002319 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002320 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002321 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002322 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002323 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002324 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002325 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002326 if (stats_table_request(si, appctx->st0))
2327 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002328 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002329 case STAT_CLI_O_PATS:
2330 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002331 appctx->st0 = STAT_CLI_PROMPT;
2332 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002333 case STAT_CLI_O_PAT:
2334 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002335 appctx->st0 = STAT_CLI_PROMPT;
2336 break;
2337 case STAT_CLI_O_MLOOK:
2338 if (stats_map_lookup(si))
2339 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002340 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002341 case STAT_CLI_O_POOLS:
2342 if (stats_dump_pools_to_buffer(si))
2343 appctx->st0 = STAT_CLI_PROMPT;
2344 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002345 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002346 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002347 break;
2348 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002349
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002350 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002351 if (appctx->st0 == STAT_CLI_PROMPT) {
2352 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2353 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002354 }
2355
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002356 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002357 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002358 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002359
2360 /* Now we close the output if one of the writers did so,
2361 * or if we're not in interactive mode and the request
2362 * buffer is empty. This still allows pipelined requests
2363 * to be sent in non-interactive mode.
2364 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002365 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2366 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002367 continue;
2368 }
2369
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002370 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002371 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002372 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002373 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002374
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002375 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002376 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2377 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002378 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002379 * and nothing more to consume. This is comparable to a broken pipe, so
2380 * we forward the close to the request side so that it flows upstream to
2381 * the client.
2382 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002383 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002384 }
2385
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002386 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002387 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2388 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2389 /* We have no more processing to do, and nothing more to send, and
2390 * the client side has closed. So we'll forward this state downstream
2391 * on the response buffer.
2392 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002393 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002394 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002395 }
2396
2397 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002398 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002399
2400 /* we don't want to expire timeouts while we're processing requests */
2401 si->ib->rex = TICK_ETERNITY;
2402 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002403
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002404 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002405 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 +02002406 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002407 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 +02002408
2409 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2410 /* check that we have released everything then unregister */
2411 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002412 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002413}
2414
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002415/* This function dumps information onto the stream interface's read buffer.
2416 * It returns 0 as long as it does not complete, non-zero upon completion.
2417 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002418 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002419static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002420{
2421 unsigned int up = (now.tv_sec - start_date.tv_sec);
2422
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002423#ifdef USE_OPENSSL
2424 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2425 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2426 int ssl_reuse = 0;
2427
2428 if (ssl_key_rate < ssl_sess_rate) {
2429 /* count the ssl reuse ratio and avoid overflows in both directions */
2430 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2431 }
2432#endif
2433
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002434 chunk_printf(&trash,
2435 "Name: " PRODUCT_NAME "\n"
2436 "Version: " HAPROXY_VERSION "\n"
2437 "Release_date: " HAPROXY_DATE "\n"
2438 "Nbproc: %d\n"
2439 "Process_num: %d\n"
2440 "Pid: %d\n"
2441 "Uptime: %dd %dh%02dm%02ds\n"
2442 "Uptime_sec: %d\n"
2443 "Memmax_MB: %d\n"
2444 "Ulimit-n: %d\n"
2445 "Maxsock: %d\n"
2446 "Maxconn: %d\n"
2447 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002448 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002449 "CumConns: %d\n"
2450 "CumReq: %d\n"
2451#ifdef USE_OPENSSL
2452 "MaxSslConns: %d\n"
2453 "CurrSslConns: %d\n"
2454 "CumSslConns: %d\n"
2455#endif
2456 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002457 "PipesUsed: %d\n"
2458 "PipesFree: %d\n"
2459 "ConnRate: %d\n"
2460 "ConnRateLimit: %d\n"
2461 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002462 "SessRate: %d\n"
2463 "SessRateLimit: %d\n"
2464 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002465#ifdef USE_OPENSSL
2466 "SslRate: %d\n"
2467 "SslRateLimit: %d\n"
2468 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002469 "SslFrontendKeyRate: %d\n"
2470 "SslFrontendMaxKeyRate: %d\n"
2471 "SslFrontendSessionReuse_pct: %d\n"
2472 "SslBackendKeyRate: %d\n"
2473 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002474 "SslCacheLookups: %u\n"
2475 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002476#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002477 "CompressBpsIn: %u\n"
2478 "CompressBpsOut: %u\n"
2479 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002480#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002481 "ZlibMemUsage: %ld\n"
2482 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002483#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002484 "Tasks: %d\n"
2485 "Run_queue: %d\n"
2486 "Idle_pct: %d\n"
2487 "node: %s\n"
2488 "description: %s\n"
2489 "",
2490 global.nbproc,
2491 relative_pid,
2492 pid,
2493 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2494 up,
2495 global.rlimit_memmax,
2496 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002497 global.maxsock, global.maxconn, global.hardmaxconn,
2498 actconn, totalconn, global.req_count,
2499#ifdef USE_OPENSSL
2500 global.maxsslconn, sslconns, totalsslconns,
2501#endif
2502 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002503 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002504 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002505#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002506 ssl_sess_rate, global.ssl_lim, global.ssl_max,
2507 ssl_key_rate, global.ssl_fe_keys_max,
2508 ssl_reuse,
2509 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02002510 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002511#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002512 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2513 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002514#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002515 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002516#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002517 nb_tasks_cur, run_queue_cur, idle_pct,
2518 global.node, global.desc ? global.desc : ""
2519 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002520
2521 if (bi_putchk(si->ib, &trash) == -1)
2522 return 0;
2523
2524 return 1;
2525}
2526
Willy Tarreau12833bb2014-01-28 16:49:56 +01002527/* This function dumps memory usage information onto the stream interface's
2528 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2529 * completion. No state is used.
2530 */
2531static int stats_dump_pools_to_buffer(struct stream_interface *si)
2532{
2533 dump_pools_to_trash();
2534 if (bi_putchk(si->ib, &trash) == -1)
2535 return 0;
2536 return 1;
2537}
2538
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002539/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2540 * the state from stream interface <si>. The caller is responsible for clearing
2541 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002542 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002543static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002544{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002545 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002546 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002547
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002548 if (!(px->cap & PR_CAP_FE))
2549 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002550
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002551 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002552 return 0;
2553
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002554 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002555 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002556 /* name, queue */
2557 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002558
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002559 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002560 /* Column sub-heading for Enable or Disable server */
2561 chunk_appendf(&trash, "<td></td>");
2562 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002563
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002564 chunk_appendf(&trash,
2565 "<td class=ac>"
2566 "<a name=\"%s/Frontend\"></a>"
2567 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2568 "<td colspan=3></td>"
2569 "",
2570 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002571
Willy Tarreau466c9b52012-12-23 02:25:03 +01002572 chunk_appendf(&trash,
2573 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002574 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002575 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2576 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2577 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002578 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2579 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2580 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002581
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002582 if (px->mode == PR_MODE_HTTP)
2583 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002584 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002585 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002586
2587 chunk_appendf(&trash,
2588 "</table></div></u></td>"
2589 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002590 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002591 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2592 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2593 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002594 U2H(px->fe_counters.sps_max),
2595 U2H(px->fe_counters.cps_max),
2596 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002597
2598 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002599 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002600 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002601 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002602
2603 chunk_appendf(&trash,
2604 "</table></div></u></td>"
2605 /* sessions rate : limit */
2606 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002607 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002608
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002609 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002610 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002611 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002612 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002613 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2614 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002615 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002616 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2617 U2H(px->fe_counters.cum_sess),
2618 U2H(px->fe_counters.cum_conn),
2619 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002620
Willy Tarreau466c9b52012-12-23 02:25:03 +01002621 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002622 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002623 chunk_appendf(&trash,
2624 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2625 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2626 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2627 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2628 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2629 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2630 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2631 "<tr><th>- other responses:</th><td>%s</td></tr>"
2632 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2633 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002634 U2H(px->fe_counters.p.http.cum_req),
2635 U2H(px->fe_counters.p.http.rsp[1]),
2636 U2H(px->fe_counters.p.http.rsp[2]),
2637 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002638 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002639 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002640 U2H(px->fe_counters.p.http.rsp[3]),
2641 U2H(px->fe_counters.p.http.rsp[4]),
2642 U2H(px->fe_counters.p.http.rsp[5]),
2643 U2H(px->fe_counters.p.http.rsp[0]),
2644 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002645 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002646
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002647 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002648 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002649 /* sessions: lbtot, lastsess */
2650 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002651 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002652 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002653 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002654 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002655
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002656 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002657 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002658 "<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 +01002659 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002660 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002661 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2662 px->fe_counters.comp_in ?
2663 (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 +01002664 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002665
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002666 chunk_appendf(&trash,
2667 /* denied: req, resp */
2668 "<td>%s</td><td>%s</td>"
2669 /* errors : request, connect, response */
2670 "<td>%s</td><td></td><td></td>"
2671 /* warnings: retries, redispatches */
2672 "<td></td><td></td>"
2673 /* server status : reflect frontend status */
2674 "<td class=ac>%s</td>"
2675 /* rest of server: nothing */
2676 "<td class=ac colspan=8></td></tr>"
2677 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002678 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2679 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002680 px->state == PR_STREADY ? "OPEN" :
2681 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002682 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002683 else { /* CSV mode */
2684 chunk_appendf(&trash,
2685 /* pxid, name, queue cur, queue max, */
2686 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002687 /* sessions : current, max, limit, total */
2688 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002689 /* bytes : in, out */
2690 "%lld,%lld,"
2691 /* denied: req, resp */
2692 "%lld,%lld,"
2693 /* errors : request, connect, response */
2694 "%lld,,,"
2695 /* warnings: retries, redispatches */
2696 ",,"
2697 /* server status : reflect frontend status */
2698 "%s,"
2699 /* rest of server: nothing */
2700 ",,,,,,,,"
2701 /* pid, iid, sid, throttle, lbtot, tracked, type */
2702 "%d,%d,0,,,,%d,"
2703 /* rate, rate_lim, rate_max */
2704 "%u,%u,%u,"
2705 /* check_status, check_code, check_duration */
2706 ",,,",
2707 px->id,
2708 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2709 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2710 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2711 px->fe_counters.failed_req,
2712 px->state == PR_STREADY ? "OPEN" :
2713 px->state == PR_STFULL ? "FULL" : "STOP",
2714 relative_pid, px->uuid, STATS_TYPE_FE,
2715 read_freq_ctr(&px->fe_sess_per_sec),
2716 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002717
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002718 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2719 if (px->mode == PR_MODE_HTTP) {
2720 for (i=1; i<6; i++)
2721 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2722 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2723 }
2724 else
2725 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002726
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002727 /* failed health analyses */
2728 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002729
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002730 /* requests : req_rate, req_rate_max, req_tot, */
2731 chunk_appendf(&trash, "%u,%u,%lld,",
2732 read_freq_ctr(&px->fe_req_per_sec),
2733 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2734
2735 /* errors: cli_aborts, srv_aborts */
2736 chunk_appendf(&trash, ",,");
2737
2738 /* compression: in, out, bypassed */
2739 chunk_appendf(&trash, "%lld,%lld,%lld,",
2740 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2741
2742 /* compression: comp_rsp */
2743 chunk_appendf(&trash, "%lld,",
2744 px->fe_counters.p.http.comp_rsp);
2745
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002746 /* lastsess */
2747 chunk_appendf(&trash, ",");
2748
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002749 /* finish with EOL */
2750 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002751 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002752 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002753}
2754
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002755/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2756 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2757 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2758 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002759 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002760static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002761{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002762 struct appctx *appctx = __objt_appctx(si->end);
2763
2764 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002765 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002766 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002767 /* Column sub-heading for Enable or Disable server */
2768 chunk_appendf(&trash, "<td></td>");
2769 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002770 chunk_appendf(&trash,
2771 /* frontend name, listener name */
2772 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2773 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2774 "",
2775 px->id, l->name,
2776 (flags & ST_SHLGNDS)?"<u>":"",
2777 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002778
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002779 if (flags & ST_SHLGNDS) {
2780 char str[INET6_ADDRSTRLEN];
2781 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002782
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002783 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002784
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002785 port = get_host_port(&l->addr);
2786 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2787 case AF_INET:
2788 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2789 break;
2790 case AF_INET6:
2791 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2792 break;
2793 case AF_UNIX:
2794 chunk_appendf(&trash, "unix, ");
2795 break;
2796 case -1:
2797 chunk_appendf(&trash, "(%s), ", strerror(errno));
2798 break;
2799 }
Willy Tarreau91861262007-10-17 17:06:05 +02002800
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002801 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002802 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002803 }
Willy Tarreau91861262007-10-17 17:06:05 +02002804
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002805 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002806 /* queue */
2807 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002808 /* sessions rate: current, max, limit */
2809 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002810 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002811 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002812 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002813 /* bytes: in, out */
2814 "<td>%s</td><td>%s</td>"
2815 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002816 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002817 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2818 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002819
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002820 chunk_appendf(&trash,
2821 /* denied: req, resp */
2822 "<td>%s</td><td>%s</td>"
2823 /* errors: request, connect, response */
2824 "<td>%s</td><td></td><td></td>"
2825 /* warnings: retries, redispatches */
2826 "<td></td><td></td>"
2827 /* server status: reflect listener status */
2828 "<td class=ac>%s</td>"
2829 /* rest of server: nothing */
2830 "<td class=ac colspan=8></td></tr>"
2831 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002832 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2833 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002834 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2835 }
2836 else { /* CSV mode */
2837 chunk_appendf(&trash,
2838 /* pxid, name, queue cur, queue max, */
2839 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002840 /* sessions: current, max, limit, total */
2841 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002842 /* bytes: in, out */
2843 "%lld,%lld,"
2844 /* denied: req, resp */
2845 "%lld,%lld,"
2846 /* errors: request, connect, response */
2847 "%lld,,,"
2848 /* warnings: retries, redispatches */
2849 ",,"
2850 /* server status: reflect listener status */
2851 "%s,"
2852 /* rest of server: nothing */
2853 ",,,,,,,,"
2854 /* pid, iid, sid, throttle, lbtot, tracked, type */
2855 "%d,%d,%d,,,,%d,"
2856 /* rate, rate_lim, rate_max */
2857 ",,,"
2858 /* check_status, check_code, check_duration */
2859 ",,,"
2860 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2861 ",,,,,,"
2862 /* failed health analyses */
2863 ","
2864 /* requests : req_rate, req_rate_max, req_tot, */
2865 ",,,"
2866 /* errors: cli_aborts, srv_aborts */
2867 ",,"
2868 /* compression: in, out, bypassed, comp_rsp */
2869 ",,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002870 /* lastsess */
2871 ","
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002872 "\n",
2873 px->id, l->name,
2874 l->nbconn, l->counters->conn_max,
2875 l->maxconn, l->counters->cum_conn,
2876 l->counters->bytes_in, l->counters->bytes_out,
2877 l->counters->denied_req, l->counters->denied_resp,
2878 l->counters->failed_req,
2879 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2880 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2881 }
2882 return 1;
2883}
Willy Tarreau91861262007-10-17 17:06:05 +02002884
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002885/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2886 * from stream interface <si>, stats flags <flags>, and server state <state>.
2887 * The caller is responsible for clearing the trash if needed. Returns non-zero
2888 * if it emits anything, zero otherwise. The <state> parameter can take the
Willy Tarreau9638efa2014-05-23 11:19:57 +02002889 * following values : 0=DOWN, 1=DOWN(agent) 2=going up, 3=going down, 4=UP, 5,6=NOLB,
2890 * 7,8=DRAIN, 9=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002891 */
2892static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2893{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002894 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002895 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002896 char str[INET6_ADDRSTRLEN];
2897 struct chunk src;
2898 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002899
Willy Tarreau32091232014-05-16 13:52:00 +02002900 /* we have "via" which is the tracked server as described in the configuration,
2901 * and "ref" which is the checked server and the end of the chain.
2902 */
2903 via = sv->track ? sv->track : sv;
2904 ref = via;
2905 while (ref->track)
2906 ref = ref->track;
2907
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002908 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02002909 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002910 "DOWN",
Willy Tarreau9638efa2014-05-23 11:19:57 +02002911 "DOWN (agent)",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002912 "DN %d/%d &uarr;",
2913 "UP %d/%d &darr;",
2914 "UP",
2915 "NOLB %d/%d &darr;",
2916 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002917 "DRAIN %d/%d &darr;",
2918 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002919 "<i>no check</i>"
2920 };
Willy Tarreau91861262007-10-17 17:06:05 +02002921
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002922 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002923 chunk_appendf(&trash, "<tr class=\"maintain\">");
2924 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002925 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002926 "<tr class=\"%s%d\">",
Willy Tarreauc93cd162014-05-13 15:54:22 +02002927 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002928
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002929 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002930 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002931 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2932 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002933
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002934 chunk_appendf(&trash,
2935 "<td class=ac><a name=\"%s/%s\"></a>%s"
2936 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2937 "",
2938 px->id, sv->id,
2939 (flags & ST_SHLGNDS) ? "<u>" : "",
2940 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002941
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002942 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002943 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002944
2945 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2946 case AF_INET:
2947 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2948 break;
2949 case AF_INET6:
2950 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2951 break;
2952 case AF_UNIX:
2953 chunk_appendf(&trash, "unix, ");
2954 break;
2955 case -1:
2956 chunk_appendf(&trash, "(%s), ", strerror(errno));
2957 break;
2958 default: /* address family not supported */
2959 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002960 }
Willy Tarreau91861262007-10-17 17:06:05 +02002961
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002962 /* id */
2963 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002964
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002965 /* cookie */
2966 if (sv->cookie) {
2967 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002968
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002969 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2970 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002971
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002972 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002973 }
2974
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002975 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002976 }
Willy Tarreau91861262007-10-17 17:06:05 +02002977
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002978 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002979 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002980 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002981 /* sessions rate : current, max, limit */
2982 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002983 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002984 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002985 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2986 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002987
Willy Tarreau466c9b52012-12-23 02:25:03 +01002988
2989 chunk_appendf(&trash,
2990 /* sessions: current, max, limit, total */
2991 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002992 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002993 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2994 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002995 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2996 U2H(sv->counters.cum_sess),
2997 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002998
Willy Tarreau466c9b52012-12-23 02:25:03 +01002999 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3000 if (px->mode == PR_MODE_HTTP) {
3001 unsigned long long tot;
3002 for (tot = i = 0; i < 6; i++)
3003 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003004
Willy Tarreau466c9b52012-12-23 02:25:03 +01003005 chunk_appendf(&trash,
3006 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3007 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3008 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3009 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3010 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3011 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3012 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3013 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003014 U2H(tot),
3015 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3016 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3017 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3018 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3019 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3020 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 +02003021 }
Willy Tarreau91861262007-10-17 17:06:05 +02003022
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003023 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003024 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003025 /* sessions: lbtot, last */
3026 "<td>%s</td><td>%s</td>",
3027 U2H(sv->counters.cum_lbconn),
3028 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003029
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003030 chunk_appendf(&trash,
3031 /* bytes : in, out */
3032 "<td>%s</td><td>%s</td>"
3033 /* denied: req, resp */
3034 "<td></td><td>%s</td>"
3035 /* errors : request, connect */
3036 "<td></td><td>%s</td>"
3037 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003038 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003039 /* warnings: retries, redispatches */
3040 "<td>%lld</td><td>%lld</td>"
3041 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003042 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3043 U2H(sv->counters.failed_secu),
3044 U2H(sv->counters.failed_conns),
3045 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003046 sv->counters.cli_aborts,
3047 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003048 sv->counters.retries, sv->counters.redispatches);
3049
3050 /* status, lest check */
3051 chunk_appendf(&trash, "<td class=ac>");
3052
Willy Tarreau20125212014-05-13 19:44:56 +02003053 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003054 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3055 chunk_appendf(&trash, "MAINT");
3056 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003057 else if ((ref->agent.state & CHK_ST_ENABLED) && (ref->state == SRV_ST_STOPPED)) {
3058 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003059 /* DOWN (agent) */
3060 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 +02003061 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003062 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003063 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3064 chunk_appendf(&trash,
3065 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003066 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3067 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003068 }
Willy Tarreau91861262007-10-17 17:06:05 +02003069
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003070 if ((sv->state == SRV_ST_STOPPED) &&
3071 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3072 chunk_appendf(&trash,
3073 "</td><td class=ac><u> %s%s",
3074 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3075 get_check_status_info(sv->agent.status));
3076
3077 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3078 chunk_appendf(&trash, "/%d", sv->agent.code);
3079
3080 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3081 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3082
3083 chunk_appendf(&trash, "<div class=tips>%s",
3084 get_check_status_description(sv->agent.status));
3085 if (*sv->agent.desc) {
3086 chunk_appendf(&trash, ": ");
3087 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3088 chunk_htmlencode(&trash, &src);
3089 }
3090 chunk_appendf(&trash, "</div></u>");
3091 }
3092 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003093 chunk_appendf(&trash,
3094 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003095 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003096 get_check_status_info(sv->check.status));
3097
3098 if (sv->check.status >= HCHK_STATUS_L57DATA)
3099 chunk_appendf(&trash, "/%d", sv->check.code);
3100
3101 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003102 chunk_appendf(&trash, " in %lums", sv->check.duration);
3103
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003104 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003105 get_check_status_description(sv->check.status));
3106 if (*sv->check.desc) {
3107 chunk_appendf(&trash, ": ");
3108 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3109 chunk_htmlencode(&trash, &src);
3110 }
3111 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003112 }
3113 else
3114 chunk_appendf(&trash, "</td><td>");
3115
3116 chunk_appendf(&trash,
3117 /* weight */
3118 "</td><td class=ac>%d</td>"
3119 /* act, bck */
3120 "<td class=ac>%s</td><td class=ac>%s</td>"
3121 "",
3122 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003123 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3124 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003125
3126 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003127 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003128 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003129
3130 if (ref->observe)
3131 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3132
3133 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003134 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003135 "<td>%lld</td><td>%s</td>"
3136 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003137 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003138 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3139 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003140 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3141 /* tracking a server */
3142 chunk_appendf(&trash,
3143 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003144 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003145 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003146 else
3147 chunk_appendf(&trash, "<td colspan=3></td>");
3148
3149 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003150 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003151 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003152 else
3153 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3154 }
3155 else { /* CSV mode */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003156 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003157 "DOWN,",
Willy Tarreau9638efa2014-05-23 11:19:57 +02003158 "DOWN (agent),",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003159 "DOWN %d/%d,",
3160 "UP %d/%d,",
3161 "UP,",
3162 "NOLB %d/%d,",
3163 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09003164 "DRAIN %d/%d,",
3165 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003166 "no check,"
3167 };
3168
3169 chunk_appendf(&trash,
3170 /* pxid, name */
3171 "%s,%s,"
3172 /* queue : current, max */
3173 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003174 /* sessions : current, max, limit, total */
3175 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003176 /* bytes : in, out */
3177 "%lld,%lld,"
3178 /* denied: req, resp */
3179 ",%lld,"
3180 /* errors : request, connect, response */
3181 ",%lld,%lld,"
3182 /* warnings: retries, redispatches */
3183 "%lld,%lld,"
3184 "",
3185 px->id, sv->id,
3186 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003187 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003188 sv->counters.bytes_in, sv->counters.bytes_out,
3189 sv->counters.failed_secu,
3190 sv->counters.failed_conns, sv->counters.failed_resp,
3191 sv->counters.retries, sv->counters.redispatches);
3192
3193 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003194 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003195 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003196 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003197 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003198 else
3199 chunk_appendf(&trash,
3200 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003201 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3202 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003203
3204 chunk_appendf(&trash,
3205 /* weight, active, backup */
3206 "%d,%d,%d,"
3207 "",
3208 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003209 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3210 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003211
3212 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003213 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003214 chunk_appendf(&trash,
3215 "%lld,%lld,%d,%d,",
3216 sv->counters.failed_checks, sv->counters.down_trans,
3217 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3218 else
3219 chunk_appendf(&trash, ",,,,");
3220
3221 /* queue limit, pid, iid, sid, */
3222 chunk_appendf(&trash,
3223 "%s,"
3224 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003225 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003226 relative_pid, px->uuid, sv->puid);
3227
3228 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003229 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003230 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003231
3232 /* sessions: lbtot */
3233 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3234
3235 /* tracked */
3236 if (sv->track)
3237 chunk_appendf(&trash, "%s/%s,",
3238 sv->track->proxy->id, sv->track->id);
3239 else
3240 chunk_appendf(&trash, ",");
3241
3242 /* type */
3243 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3244
3245 /* rate */
3246 chunk_appendf(&trash, "%u,,%u,",
3247 read_freq_ctr(&sv->sess_per_sec),
3248 sv->counters.sps_max);
3249
Willy Tarreauff5ae352013-12-11 20:36:34 +01003250 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003251 /* check_status */
3252 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3253
3254 /* check_code */
3255 if (sv->check.status >= HCHK_STATUS_L57DATA)
3256 chunk_appendf(&trash, "%u,", sv->check.code);
3257 else
3258 chunk_appendf(&trash, ",");
3259
3260 /* check_duration */
3261 if (sv->check.status >= HCHK_STATUS_CHECKED)
3262 chunk_appendf(&trash, "%lu,", sv->check.duration);
3263 else
3264 chunk_appendf(&trash, ",");
3265
3266 }
3267 else
3268 chunk_appendf(&trash, ",,,");
3269
3270 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3271 if (px->mode == PR_MODE_HTTP) {
3272 for (i=1; i<6; i++)
3273 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3274
3275 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3276 }
3277 else
3278 chunk_appendf(&trash, ",,,,,,");
3279
3280 /* failed health analyses */
3281 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3282
3283 /* requests : req_rate, req_rate_max, req_tot, */
3284 chunk_appendf(&trash, ",,,");
3285
3286 /* errors: cli_aborts, srv_aborts */
3287 chunk_appendf(&trash, "%lld,%lld,",
3288 sv->counters.cli_aborts, sv->counters.srv_aborts);
3289
3290 /* compression: in, out, bypassed, comp_rsp */
3291 chunk_appendf(&trash, ",,,,");
3292
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003293 /* lastsess */
3294 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3295
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003296 /* finish with EOL */
3297 chunk_appendf(&trash, "\n");
3298 }
3299 return 1;
3300}
3301
3302/* Dumps a line for backend <px> to the trash for and uses the state from stream
3303 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3304 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3305 */
3306static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3307{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003308 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003309 struct chunk src;
3310 int i;
3311
3312 if (!(px->cap & PR_CAP_BE))
3313 return 0;
3314
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003315 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003316 return 0;
3317
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003318 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003319 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003320 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003321 /* Column sub-heading for Enable or Disable server */
3322 chunk_appendf(&trash, "<td></td>");
3323 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003324 chunk_appendf(&trash,
3325 "<td class=ac>"
3326 /* name */
3327 "%s<a name=\"%s/Backend\"></a>"
3328 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3329 "",
3330 (flags & ST_SHLGNDS)?"<u>":"",
3331 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332
3333 if (flags & ST_SHLGNDS) {
3334 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003335 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003336 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3337
3338 /* cookie */
3339 if (px->cookie_name) {
3340 chunk_appendf(&trash, ", cookie: '");
3341 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3342 chunk_htmlencode(&trash, &src);
3343 chunk_appendf(&trash, "'");
3344 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003345 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003346 }
3347
3348 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003349 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003350 /* queue : current, max */
3351 "<td>%s</td><td>%s</td><td></td>"
3352 /* sessions rate : current, max, limit */
3353 "<td>%s</td><td>%s</td><td></td>"
3354 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003355 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003356 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3357 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003358
3359 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003360 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003361 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003362 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003363 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003364 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003365 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3366 U2H(px->be_counters.cum_conn),
3367 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003368
Willy Tarreau466c9b52012-12-23 02:25:03 +01003369 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003370 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003371 chunk_appendf(&trash,
3372 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3373 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3374 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3375 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3376 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3377 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3378 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3379 "<tr><th>- other responses:</th><td>%s</td></tr>"
3380 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3381 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003382 U2H(px->be_counters.p.http.cum_req),
3383 U2H(px->be_counters.p.http.rsp[1]),
3384 U2H(px->be_counters.p.http.rsp[2]),
3385 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003386 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003387 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003388 U2H(px->be_counters.p.http.rsp[3]),
3389 U2H(px->be_counters.p.http.rsp[4]),
3390 U2H(px->be_counters.p.http.rsp[5]),
3391 U2H(px->be_counters.p.http.rsp[0]),
3392 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003393 }
3394
3395 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003396 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003397 /* sessions: lbtot, last */
3398 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003399 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003400 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003401 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003402 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003403 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003404 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003405
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003406 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003407 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003408 "<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 +01003409 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003410 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003411 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3412 px->be_counters.comp_in ?
3413 (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 +01003414 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3415
3416 chunk_appendf(&trash,
3417 /* denied: req, resp */
3418 "<td>%s</td><td>%s</td>"
3419 /* errors : request, connect */
3420 "<td></td><td>%s</td>"
3421 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003422 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003423 /* warnings: retries, redispatches */
3424 "<td>%lld</td><td>%lld</td>"
3425 /* backend status: reflect backend status (up/down): we display UP
3426 * if the backend has known working servers or if it has no server at
3427 * all (eg: for stats). Then we display the total weight, number of
3428 * active and backups. */
3429 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3430 "<td class=ac>%d</td><td class=ac>%d</td>"
3431 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003432 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3433 U2H(px->be_counters.failed_conns),
3434 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003435 px->be_counters.cli_aborts,
3436 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003437 px->be_counters.retries, px->be_counters.redispatches,
3438 human_time(now.tv_sec - px->last_change, 1),
3439 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3440 "<font color=\"red\"><b>DOWN</b></font>",
3441 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3442 px->srv_act, px->srv_bck);
3443
3444 chunk_appendf(&trash,
3445 /* rest of backend: nothing, down transitions, total downtime, throttle */
3446 "<td class=ac>&nbsp;</td><td>%d</td>"
3447 "<td>%s</td>"
3448 "<td></td>"
3449 "</tr>",
3450 px->down_trans,
3451 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3452 }
3453 else { /* CSV mode */
3454 chunk_appendf(&trash,
3455 /* pxid, name */
3456 "%s,BACKEND,"
3457 /* queue : current, max */
3458 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003459 /* sessions : current, max, limit, total */
3460 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003461 /* bytes : in, out */
3462 "%lld,%lld,"
3463 /* denied: req, resp */
3464 "%lld,%lld,"
3465 /* errors : request, connect, response */
3466 ",%lld,%lld,"
3467 /* warnings: retries, redispatches */
3468 "%lld,%lld,"
3469 /* backend status: reflect backend status (up/down): we display UP
3470 * if the backend has known working servers or if it has no server at
3471 * all (eg: for stats). Then we display the total weight, number of
3472 * active and backups. */
3473 "%s,"
3474 "%d,%d,%d,"
3475 /* rest of backend: nothing, down transitions, last change, total downtime */
3476 ",%d,%d,%d,,"
3477 /* pid, iid, sid, throttle, lbtot, tracked, type */
3478 "%d,%d,0,,%lld,,%d,"
3479 /* rate, rate_lim, rate_max, */
3480 "%u,,%u,"
3481 /* check_status, check_code, check_duration */
3482 ",,,",
3483 px->id,
3484 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3485 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3486 px->be_counters.bytes_in, px->be_counters.bytes_out,
3487 px->be_counters.denied_req, px->be_counters.denied_resp,
3488 px->be_counters.failed_conns, px->be_counters.failed_resp,
3489 px->be_counters.retries, px->be_counters.redispatches,
3490 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3491 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3492 px->srv_act, px->srv_bck,
3493 px->down_trans, (int)(now.tv_sec - px->last_change),
3494 px->srv?be_downtime(px):0,
3495 relative_pid, px->uuid,
3496 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3497 read_freq_ctr(&px->be_sess_per_sec),
3498 px->be_counters.sps_max);
3499
3500 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3501 if (px->mode == PR_MODE_HTTP) {
3502 for (i=1; i<6; i++)
3503 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3504 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3505 }
3506 else
3507 chunk_appendf(&trash, ",,,,,,");
3508
3509 /* failed health analyses */
3510 chunk_appendf(&trash, ",");
3511
3512 /* requests : req_rate, req_rate_max, req_tot, */
3513 chunk_appendf(&trash, ",,,");
3514
3515 /* errors: cli_aborts, srv_aborts */
3516 chunk_appendf(&trash, "%lld,%lld,",
3517 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3518
3519 /* compression: in, out, bypassed */
3520 chunk_appendf(&trash, "%lld,%lld,%lld,",
3521 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3522
3523 /* compression: comp_rsp */
3524 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3525
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003526 /* lastsess */
3527 chunk_appendf(&trash, "%d,", be_lastsession(px));
3528
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003529 /* finish with EOL */
3530 chunk_appendf(&trash, "\n");
3531 }
3532 return 1;
3533}
3534
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003535/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003536 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003537 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003538 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003539static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003540{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003541 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003542 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3543
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003544 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003545 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003546
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003547 /* 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 +02003548 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003549 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003550 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003551 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3552 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003553 }
3554
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003555 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003556 "<form action=\"%s%s%s%s\" method=\"post\">",
3557 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003558 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3559 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003560 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003561 }
3562
3563 /* print a new table */
3564 chunk_appendf(&trash,
3565 "<table class=\"tbl\" width=\"100%%\">\n"
3566 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003567 "<th class=\"pxname\" width=\"10%%\">");
3568
3569 chunk_appendf(&trash,
3570 "<a name=\"%s\"></a>%s"
3571 "<a class=px href=\"#%s\">%s</a>",
3572 px->id,
3573 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3574 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003575
3576 if (uri->flags & ST_SHLGNDS) {
3577 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003578 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003579 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3580 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003581 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003582 }
3583
3584 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003585 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003586 "<th class=\"%s\" width=\"90%%\">%s</th>"
3587 "</tr>\n"
3588 "</table>\n"
3589 "<table class=\"tbl\" width=\"100%%\">\n"
3590 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003591 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3592 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3593
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003594 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003595 /* Column heading for Enable or Disable server */
3596 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003597 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003598
3599 chunk_appendf(&trash,
3600 "<th rowspan=2></th>"
3601 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003602 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003603 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3604 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3605 "<th colspan=9>Server</th>"
3606 "</tr>\n"
3607 "<tr class=\"titre\">"
3608 "<th>Cur</th><th>Max</th><th>Limit</th>"
3609 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003610 "<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 +01003611 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3612 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3613 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3614 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3615 "<th>Thrtle</th>\n"
3616 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003617}
3618
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003619/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003620 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003621 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003622static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003623{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003624 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003625 chunk_appendf(&trash, "</table>");
3626
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003627 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003628 /* close the form used to enable/disable this proxy servers */
3629 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003630 "Choose the action to perform on the checked servers : "
3631 "<select name=action>"
3632 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003633 "<option value=\"ready\">Set state to READY</option>"
3634 "<option value=\"drain\">Set state to DRAIN</option>"
3635 "<option value=\"maint\">set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02003636 "<option value=\"dhlth\">Health: disable checks</option>"
3637 "<option value=\"ehlth\">Health: enable checks</option>"
3638 "<option value=\"hrunn\">Health: force UP</option>"
3639 "<option value=\"hnolb\">Health: force NOLB</option>"
3640 "<option value=\"hdown\">Health: force DOWN</option>"
3641 "<option value=\"dagent\">Agent: disable checks</option>"
3642 "<option value=\"eagent\">Agent: enable checks</option>"
3643 "<option value=\"arunn\">Agent: force UP</option>"
3644 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003645 "<option value=\"shutdown\">Kill Sessions</option>"
3646 "</select>"
3647 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3648 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3649 "</form>",
3650 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003651 }
3652
3653 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003654}
Willy Tarreau91861262007-10-17 17:06:05 +02003655
3656/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003657 * Dumps statistics for a proxy. The output is sent to the stream interface's
3658 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3659 * buffer space, or non-zero if everything completed. This function is used
3660 * both by the CLI and the HTTP entry points, and is able to dump the output
3661 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003662 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003663static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003664{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003665 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003666 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003667 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003668 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003669 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003670
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003671 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003672
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003673 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003674 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003675 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003676 if (uri && uri->scope) {
3677 /* we have a limited scope, we have to check the proxy name */
3678 struct stat_scope *scope;
3679 int len;
3680
3681 len = strlen(px->id);
3682 scope = uri->scope;
3683
3684 while (scope) {
3685 /* match exact proxy name */
3686 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3687 break;
3688
3689 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003690 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003691 break;
3692 scope = scope->next;
3693 }
3694
3695 /* proxy name not found : don't dump anything */
3696 if (scope == NULL)
3697 return 1;
3698 }
3699
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003700 /* if the user has requested a limited output and the proxy
3701 * name does not match, skip it.
3702 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003703 if (appctx->ctx.stats.scope_len &&
3704 strnistr(px->id, strlen(px->id), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003705 return 1;
3706
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003707 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3708 (appctx->ctx.stats.iid != -1) &&
3709 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003710 return 1;
3711
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003712 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003713 /* fall through */
3714
Willy Tarreau295a8372011-03-10 11:25:07 +01003715 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003716 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003717 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003718 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003719 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003720 }
Willy Tarreau91861262007-10-17 17:06:05 +02003721
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003722 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003723 /* fall through */
3724
Willy Tarreau295a8372011-03-10 11:25:07 +01003725 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003726 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003727 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003728 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003729 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003730
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003731 appctx->ctx.stats.l = px->conf.listeners.n;
3732 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003733 /* fall through */
3734
Willy Tarreau295a8372011-03-10 11:25:07 +01003735 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003736 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003737 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003738 if (buffer_almost_full(rep->buf)) {
3739 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003740 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003741 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003742
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003743 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003744 if (!l->counters)
3745 continue;
3746
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003747 if (appctx->ctx.stats.flags & STAT_BOUND) {
3748 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003749 break;
3750
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003751 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003752 continue;
3753 }
3754
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003755 /* print the frontend */
3756 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3757 if (bi_putchk(rep, &trash) == -1)
3758 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003759 }
3760
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003761 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3762 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003763 /* fall through */
3764
Willy Tarreau295a8372011-03-10 11:25:07 +01003765 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003766 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003767 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02003768 int sv_state;
Willy Tarreau91861262007-10-17 17:06:05 +02003769
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003770 if (buffer_almost_full(rep->buf)) {
3771 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003772 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003773 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003774
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003775 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003776
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003777 if (appctx->ctx.stats.flags & STAT_BOUND) {
3778 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003779 break;
3780
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003781 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003782 continue;
3783 }
3784
Willy Tarreau32091232014-05-16 13:52:00 +02003785 svs = sv;
3786 while (svs->track)
3787 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003788
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003789 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
3790 /* server is UP. The possibilities are :
Willy Tarreau9638efa2014-05-23 11:19:57 +02003791 * - UP, draining, going down => state = 7
3792 * - UP, going down => state = 3
3793 * - UP, draining => state = 8
3794 * - UP, checked => state = 4
3795 * - UP, not checked nor tracked => state = 9
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003796 */
3797
3798 if ((svs->check.state & CHK_ST_ENABLED) &&
3799 (svs->check.health < svs->check.rise + svs->check.fall - 1))
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003800 sv_state = 3;
Willy Tarreau9638efa2014-05-23 11:19:57 +02003801 else
3802 sv_state = 4;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003803
Willy Tarreauefe28222014-05-21 17:13:13 +02003804 if (server_is_draining(sv))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003805 sv_state += 4;
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003806
Willy Tarreau9638efa2014-05-23 11:19:57 +02003807 if (sv_state == 4 && !(svs->check.state & CHK_ST_ENABLED))
3808 sv_state = 9; /* unchecked UP */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003809 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003810 else if (sv->state == SRV_ST_STOPPING) {
3811 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
3812 (svs->check.health == svs->check.rise + svs->check.fall - 1))
Willy Tarreau9638efa2014-05-23 11:19:57 +02003813 sv_state = 6; /* NOLB */
Willy Tarreau91861262007-10-17 17:06:05 +02003814 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003815 sv_state = 5; /* NOLB going down */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003816 }
3817 else { /* stopped */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003818 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health)
3819 sv_state = 1; /* DOWN (agent) */
3820 else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003821 sv_state = 0; /* DOWN */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003822 else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED))
3823 sv_state = 2; /* going up */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003824 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003825 sv_state = 0; /* DOWN, unchecked */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003826 }
Willy Tarreau91861262007-10-17 17:06:05 +02003827
Willy Tarreau9638efa2014-05-23 11:19:57 +02003828 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003829 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003830 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003831 continue;
3832 }
3833
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003834 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3835 if (bi_putchk(rep, &trash) == -1)
3836 return 0;
3837 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003838
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003839 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003840 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003841
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003842 case STAT_PX_ST_BE:
3843 /* print the backend */
3844 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3845 if (bi_putchk(rep, &trash) == -1)
3846 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003847
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003848 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003849 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003850
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003851 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003852 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003853 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003854 if (bi_putchk(rep, &trash) == -1)
3855 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003856 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003857
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003858 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003859 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003860
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003861 case STAT_PX_ST_FIN:
3862 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003863
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003864 default:
3865 /* unknown state, we should put an abort() here ! */
3866 return 1;
3867 }
3868}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003869
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003870/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3871 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003872 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003873static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003874{
3875 /* WARNING! This must fit in the first buffer !!! */
3876 chunk_appendf(&trash,
3877 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3878 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3879 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3880 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3881 "<style type=\"text/css\"><!--\n"
3882 "body {"
3883 " font-family: arial, helvetica, sans-serif;"
3884 " font-size: 12px;"
3885 " font-weight: normal;"
3886 " color: black;"
3887 " background: white;"
3888 "}\n"
3889 "th,td {"
3890 " font-size: 10px;"
3891 "}\n"
3892 "h1 {"
3893 " font-size: x-large;"
3894 " margin-bottom: 0.5em;"
3895 "}\n"
3896 "h2 {"
3897 " font-family: helvetica, arial;"
3898 " font-size: x-large;"
3899 " font-weight: bold;"
3900 " font-style: italic;"
3901 " color: #6020a0;"
3902 " margin-top: 0em;"
3903 " margin-bottom: 0em;"
3904 "}\n"
3905 "h3 {"
3906 " font-family: helvetica, arial;"
3907 " font-size: 16px;"
3908 " font-weight: bold;"
3909 " color: #b00040;"
3910 " background: #e8e8d0;"
3911 " margin-top: 0em;"
3912 " margin-bottom: 0em;"
3913 "}\n"
3914 "li {"
3915 " margin-top: 0.25em;"
3916 " margin-right: 2em;"
3917 "}\n"
3918 ".hr {margin-top: 0.25em;"
3919 " border-color: black;"
3920 " border-bottom-style: solid;"
3921 "}\n"
3922 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3923 ".total {background: #20D0D0;color: #ffff80;}\n"
3924 ".frontend {background: #e8e8d0;}\n"
3925 ".socket {background: #d0d0d0;}\n"
3926 ".backend {background: #e8e8d0;}\n"
3927 ".active0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02003928 ".active1 {background: #ff9090;}\n"
3929 ".active2 {background: #ffd020;}\n"
3930 ".active3 {background: #ffffa0;}\n"
3931 ".active4 {background: #c0ffc0;}\n"
3932 ".active5 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
3933 ".active6 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3934 ".active7 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3935 ".active8 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
3936 ".active9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003937 ".backup0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02003938 ".backup1 {background: #ff9090;}\n"
3939 ".backup2 {background: #ff80ff;}\n"
3940 ".backup3 {background: #c060ff;}\n"
3941 ".backup4 {background: #b0d0ff;}\n"
3942 ".backup5 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3943 ".backup6 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
3944 ".backup7 {background: #c060ff;}\n"
3945 ".backup8 {background: #cc9900;}\n"
3946 ".backup9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003947 ".maintain {background: #c07820;}\n"
3948 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3949 "\n"
3950 "a.px:link {color: #ffff40; text-decoration: none;}"
3951 "a.px:visited {color: #ffff40; text-decoration: none;}"
3952 "a.px:hover {color: #ffffff; text-decoration: none;}"
3953 "a.lfsb:link {color: #000000; text-decoration: none;}"
3954 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3955 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3956 "\n"
3957 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3958 "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"
3959 "table.tbl td.ac { text-align: center;}\n"
3960 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3961 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3962 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3963 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3964 "\n"
3965 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3966 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3967 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003968 "table.det { border-collapse: collapse; border-style: none; }\n"
3969 "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 +01003970 "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 +01003971 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003972 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003973 " display:block;\n"
3974 " visibility:hidden;\n"
3975 " z-index:2147483647;\n"
3976 " position:absolute;\n"
3977 " padding:2px 4px 3px;\n"
3978 " background:#f0f060; color:#000000;\n"
3979 " border:1px solid #7040c0;\n"
3980 " white-space:nowrap;\n"
3981 " font-style:normal;font-size:11px;font-weight:normal;\n"
3982 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3983 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3984 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003985 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003986 "-->\n"
3987 "</style></head>\n",
3988 (uri->flags & ST_SHNODE) ? " on " : "",
3989 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3990 );
3991}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003992
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003993/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003994 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003995 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003996 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003997static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003998{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003999 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004000 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004001 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004002
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004003 /* WARNING! this has to fit the first packet too.
4004 * We are around 3.5 kB, add adding entries will
4005 * become tricky if we want to support 4kB buffers !
4006 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004007 chunk_appendf(&trash,
4008 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4009 PRODUCT_NAME "%s</a></h1>\n"
4010 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4011 "<hr width=\"100%%\" class=\"hr\">\n"
4012 "<h3>&gt; General process information</h3>\n"
4013 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4014 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4015 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4016 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4017 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4018 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4019 "Running tasks: %d/%d; idle = %d %%<br>\n"
4020 "</td><td align=\"center\" nowrap>\n"
4021 "<table class=\"lgd\"><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004022 "<td class=\"active4\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4023 "<td class=\"backup4\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004024 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004025 "<td class=\"active3\"></td><td class=\"noborder\">active UP, going down </td>"
4026 "<td class=\"backup3\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004027 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004028 "<td class=\"active2\"></td><td class=\"noborder\">active DOWN, going up </td>"
4029 "<td class=\"backup2\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004030 "</tr><tr>\n"
4031 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004032 "<td class=\"active9\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004033 "</tr><tr>\n"
4034 "<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 -07004035 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02004036 "<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 +01004037 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004038 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004039 "</td>"
4040 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4041 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4042 "",
4043 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4044 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4045 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4046 (uri->flags & ST_SHDESC) ? ": " : "",
4047 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4048 pid, relative_pid, global.nbproc,
4049 up / 86400, (up % 86400) / 3600,
4050 (up % 3600) / 60, (up % 60),
4051 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4052 global.rlimit_memmax ? " MB" : "",
4053 global.rlimit_nofile,
4054 global.maxsock, global.maxconn, global.maxpipes,
4055 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4056 run_queue_cur, nb_tasks_cur, idle_pct
4057 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004058
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004059 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4060 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4061 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004062
4063 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02004064 "<li><form method=\"GET\" action=\"%s%s%s\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004065 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004066 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4067 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
4068 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004069 STAT_SCOPE_TXT_MAXLEN);
4070
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004071 /* 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 +02004072 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004073 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004074 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004075 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4076 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004077 }
4078
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004079 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004080 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004081 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004082 uri->uri_prefix,
4083 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004084 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004085 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004086 else
4087 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004088 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004089 uri->uri_prefix,
4090 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004091 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004092 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004093
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004094 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004095 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004096 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004097 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004098 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004099 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004100 "",
4101 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004102 else
4103 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004104 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004105 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004106 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004107 ";norefresh",
4108 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004109 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004110
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004111 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004112 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004113 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004114 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4115 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004116 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004117
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004118 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004119 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004120 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004121 (uri->refresh > 0) ? ";norefresh" : "",
4122 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004123
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004124 chunk_appendf(&trash,
4125 "</ul></td>"
4126 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4127 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4128 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4129 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4130 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4131 "</ul>"
4132 "</td>"
4133 "</tr></table>\n"
4134 ""
4135 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004136
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004137 if (appctx->ctx.stats.st_code) {
4138 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004139 case STAT_STATUS_DONE:
4140 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004141 "<p><div class=active4>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004142 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004143 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004144 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004145 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4146 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004147 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004148 break;
4149 case STAT_STATUS_NONE:
4150 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004151 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004152 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004153 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004154 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004155 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4156 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004157 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004158 break;
4159 case STAT_STATUS_PART:
4160 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004161 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004162 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004163 "Action partially processed.<br>"
4164 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004165 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004166 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4167 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004168 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004169 break;
4170 case STAT_STATUS_ERRP:
4171 chunk_appendf(&trash,
4172 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004173 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004174 "Action not processed because of invalid parameters."
4175 "<ul>"
4176 "<li>The action is maybe unknown.</li>"
4177 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4178 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4179 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004180 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004181 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4182 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004183 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004184 break;
4185 case STAT_STATUS_EXCD:
4186 chunk_appendf(&trash,
4187 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004188 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004189 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4190 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004191 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004192 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4193 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004194 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004195 break;
4196 case STAT_STATUS_DENY:
4197 chunk_appendf(&trash,
4198 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004199 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004200 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004201 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004202 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4203 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004204 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004205 break;
4206 default:
4207 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004208 "<p><div class=active9>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004209 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004210 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004211 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004212 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4213 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004214 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004215 }
4216 chunk_appendf(&trash, "<p>\n");
4217 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004218}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004219
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004220/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4221 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004222 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004223static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004224{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004225 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004226}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004227
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004228/* This function dumps statistics onto the stream interface's read buffer in
4229 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004230 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4231 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
4232 * and the session must be closed, or -1 in case of any error. This function is
4233 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004234 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004235static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004236{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004237 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004238 struct channel *rep = si->ib;
4239 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004240
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004241 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004242
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004243 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004244 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004245 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004246 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004247
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004248 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004249 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004250 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004251 else
4252 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004253
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004254 if (bi_putchk(rep, &trash) == -1)
4255 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004256
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004257 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004258 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004259
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004260 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004261 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004262 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004263 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004264 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004265 }
Willy Tarreau91861262007-10-17 17:06:05 +02004266
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004267 appctx->ctx.stats.px = proxy;
4268 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4269 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004270 /* fall through */
4271
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004272 case STAT_ST_LIST:
4273 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004274 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004275 if (buffer_almost_full(rep->buf)) {
4276 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004277 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004278 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004279
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004280 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004281 /* skip the disabled proxies, global frontend and non-networked ones */
4282 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004283 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004284 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004285
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004286 appctx->ctx.stats.px = px->next;
4287 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004288 }
4289 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004290
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004291 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004292 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004293
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004294 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004295 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004296 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004297 if (bi_putchk(rep, &trash) == -1)
4298 return 0;
4299 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004300
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004301 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004302 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004303
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004304 case STAT_ST_FIN:
4305 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004306
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004307 default:
4308 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004309 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004310 return -1;
4311 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004312}
Willy Tarreauae526782010-03-04 20:34:23 +01004313
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004314/* We reached the stats page through a POST request. The appctx is
4315 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004316 * Parse the posted data and enable/disable servers if necessary.
4317 * Returns 1 if request was parsed or zero if it needs more data.
4318 */
4319static int stats_process_http_post(struct stream_interface *si)
4320{
4321 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004322 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004323
4324 struct proxy *px = NULL;
4325 struct server *sv = NULL;
4326
4327 char key[LINESIZE];
4328 int action = ST_ADM_ACTION_NONE;
4329 int reprocess = 0;
4330
4331 int total_servers = 0;
4332 int altered_servers = 0;
4333
4334 char *first_param, *cur_param, *next_param, *end_params;
4335 char *st_cur_param = NULL;
4336 char *st_next_param = NULL;
4337
4338 struct chunk *temp;
4339 int reql;
4340
4341 temp = get_trash_chunk();
4342 if (temp->size < s->txn.req.body_len) {
4343 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004344 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004345 goto out;
4346 }
4347
4348 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4349 if (reql <= 0) {
4350 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004351 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004352 return 0;
4353 }
4354
4355 first_param = temp->str;
4356 end_params = temp->str + reql;
4357 cur_param = next_param = end_params;
4358 *end_params = '\0';
4359
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004360 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004361
4362 /*
4363 * Parse the parameters in reverse order to only store the last value.
4364 * From the html form, the backend and the action are at the end.
4365 */
4366 while (cur_param > first_param) {
4367 char *value;
4368 int poffset, plen;
4369
4370 cur_param--;
4371
4372 if ((*cur_param == '&') || (cur_param == first_param)) {
4373 reprocess_servers:
4374 /* Parse the key */
4375 poffset = (cur_param != first_param ? 1 : 0);
4376 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4377 if ((plen > 0) && (plen <= sizeof(key))) {
4378 strncpy(key, cur_param + poffset, plen);
4379 key[plen - 1] = '\0';
4380 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004381 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004382 goto out;
4383 }
4384
4385 /* Parse the value */
4386 value = key;
4387 while (*value != '\0' && *value != '=') {
4388 value++;
4389 }
4390 if (*value == '=') {
4391 /* Ok, a value is found, we can mark the end of the key */
4392 *value++ = '\0';
4393 }
4394 if (url_decode(key) < 0 || url_decode(value) < 0)
4395 break;
4396
4397 /* Now we can check the key to see what to do */
4398 if (!px && (strcmp(key, "b") == 0)) {
4399 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4400 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004401 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004402 goto out;
4403 }
4404 }
4405 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004406 if (strcmp(value, "ready") == 0) {
4407 action = ST_ADM_ACTION_READY;
4408 }
4409 else if (strcmp(value, "drain") == 0) {
4410 action = ST_ADM_ACTION_DRAIN;
4411 }
4412 else if (strcmp(value, "maint") == 0) {
4413 action = ST_ADM_ACTION_MAINT;
4414 }
4415 else if (strcmp(value, "shutdown") == 0) {
4416 action = ST_ADM_ACTION_SHUTDOWN;
4417 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02004418 else if (strcmp(value, "dhlth") == 0) {
4419 action = ST_ADM_ACTION_DHLTH;
4420 }
4421 else if (strcmp(value, "ehlth") == 0) {
4422 action = ST_ADM_ACTION_EHLTH;
4423 }
4424 else if (strcmp(value, "hrunn") == 0) {
4425 action = ST_ADM_ACTION_HRUNN;
4426 }
4427 else if (strcmp(value, "hnolb") == 0) {
4428 action = ST_ADM_ACTION_HNOLB;
4429 }
4430 else if (strcmp(value, "hdown") == 0) {
4431 action = ST_ADM_ACTION_HDOWN;
4432 }
4433 else if (strcmp(value, "dagent") == 0) {
4434 action = ST_ADM_ACTION_DAGENT;
4435 }
4436 else if (strcmp(value, "eagent") == 0) {
4437 action = ST_ADM_ACTION_EAGENT;
4438 }
4439 else if (strcmp(value, "arunn") == 0) {
4440 action = ST_ADM_ACTION_ARUNN;
4441 }
4442 else if (strcmp(value, "adown") == 0) {
4443 action = ST_ADM_ACTION_ADOWN;
4444 }
Willy Tarreaued7df902014-05-22 18:04:49 +02004445 /* else these are the old supported methods */
4446 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004447 action = ST_ADM_ACTION_DISABLE;
4448 }
4449 else if (strcmp(value, "enable") == 0) {
4450 action = ST_ADM_ACTION_ENABLE;
4451 }
4452 else if (strcmp(value, "stop") == 0) {
4453 action = ST_ADM_ACTION_STOP;
4454 }
4455 else if (strcmp(value, "start") == 0) {
4456 action = ST_ADM_ACTION_START;
4457 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004458 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004459 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004460 goto out;
4461 }
4462 }
4463 else if (strcmp(key, "s") == 0) {
4464 if (!(px && action)) {
4465 /*
4466 * Indicates that we'll need to reprocess the parameters
4467 * as soon as backend and action are known
4468 */
4469 if (!reprocess) {
4470 st_cur_param = cur_param;
4471 st_next_param = next_param;
4472 }
4473 reprocess = 1;
4474 }
4475 else if ((sv = findserver(px, value)) != NULL) {
4476 switch (action) {
4477 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004478 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004479 altered_servers++;
4480 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004481 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004482 }
4483 break;
4484 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004485 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004486 altered_servers++;
4487 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004488 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004489 }
4490 break;
4491 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004492 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4493 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4494 altered_servers++;
4495 total_servers++;
4496 }
4497 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004498 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004499 if (sv->admin & SRV_ADMF_FDRAIN) {
4500 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4501 altered_servers++;
4502 total_servers++;
4503 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004504 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02004505 case ST_ADM_ACTION_DHLTH:
4506 if (sv->check.state & CHK_ST_CONFIGURED) {
4507 sv->check.state &= ~CHK_ST_ENABLED;
4508 altered_servers++;
4509 total_servers++;
4510 }
4511 break;
4512 case ST_ADM_ACTION_EHLTH:
4513 if (sv->check.state & CHK_ST_CONFIGURED) {
4514 sv->check.state |= CHK_ST_ENABLED;
4515 altered_servers++;
4516 total_servers++;
4517 }
4518 break;
4519 case ST_ADM_ACTION_HRUNN:
4520 if (!(sv->track)) {
4521 sv->check.health = sv->check.rise + sv->check.fall - 1;
4522 srv_set_running(sv, "changed from Web interface");
4523 altered_servers++;
4524 total_servers++;
4525 }
4526 break;
4527 case ST_ADM_ACTION_HNOLB:
4528 if (!(sv->track)) {
4529 sv->check.health = sv->check.rise + sv->check.fall - 1;
4530 srv_set_stopping(sv, "changed from Web interface");
4531 altered_servers++;
4532 total_servers++;
4533 }
4534 break;
4535 case ST_ADM_ACTION_HDOWN:
4536 if (!(sv->track)) {
4537 sv->check.health = 0;
4538 srv_set_stopped(sv, "changed from Web interface");
4539 altered_servers++;
4540 total_servers++;
4541 }
4542 break;
4543 case ST_ADM_ACTION_DAGENT:
4544 if (sv->agent.state & CHK_ST_CONFIGURED) {
4545 sv->agent.state &= ~CHK_ST_ENABLED;
4546 altered_servers++;
4547 total_servers++;
4548 }
4549 break;
4550 case ST_ADM_ACTION_EAGENT:
4551 if (sv->agent.state & CHK_ST_CONFIGURED) {
4552 sv->agent.state |= CHK_ST_ENABLED;
4553 altered_servers++;
4554 total_servers++;
4555 }
4556 break;
4557 case ST_ADM_ACTION_ARUNN:
4558 if (sv->agent.state & CHK_ST_ENABLED) {
4559 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4560 srv_set_running(sv, "changed from Web interface");
4561 altered_servers++;
4562 total_servers++;
4563 }
4564 break;
4565 case ST_ADM_ACTION_ADOWN:
4566 if (sv->agent.state & CHK_ST_ENABLED) {
4567 sv->agent.health = 0;
4568 srv_set_stopped(sv, "changed from Web interface");
4569 altered_servers++;
4570 total_servers++;
4571 }
4572 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004573 case ST_ADM_ACTION_READY:
4574 srv_adm_set_ready(sv);
4575 altered_servers++;
4576 total_servers++;
4577 break;
4578 case ST_ADM_ACTION_DRAIN:
4579 srv_adm_set_drain(sv);
4580 altered_servers++;
4581 total_servers++;
4582 break;
4583 case ST_ADM_ACTION_MAINT:
4584 srv_adm_set_maint(sv);
4585 altered_servers++;
4586 total_servers++;
4587 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004588 case ST_ADM_ACTION_SHUTDOWN:
4589 if (px->state != PR_STSTOPPED) {
4590 struct session *sess, *sess_bck;
4591
4592 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4593 if (sess->srv_conn == sv)
4594 session_shutdown(sess, SN_ERR_KILLED);
4595
4596 altered_servers++;
4597 total_servers++;
4598 }
4599 break;
4600 }
4601 } else {
4602 /* the server name is unknown or ambiguous (duplicate names) */
4603 total_servers++;
4604 }
4605 }
4606 if (reprocess && px && action) {
4607 /* Now, we know the backend and the action chosen by the user.
4608 * We can safely restart from the first server parameter
4609 * to reprocess them
4610 */
4611 cur_param = st_cur_param;
4612 next_param = st_next_param;
4613 reprocess = 0;
4614 goto reprocess_servers;
4615 }
4616
4617 next_param = cur_param;
4618 }
4619 }
4620
4621 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004622 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004623 }
4624 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004625 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004626 }
4627 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004628 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004629 }
4630 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004631 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004632 }
4633 out:
4634 return 1;
4635}
4636
4637
4638static int stats_send_http_headers(struct stream_interface *si)
4639{
4640 struct session *s = session_from_task(si->owner);
4641 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004642 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004643
4644 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004645 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004646 "Cache-Control: no-cache\r\n"
4647 "Connection: close\r\n"
4648 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004649 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004650
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004651 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004652 chunk_appendf(&trash, "Refresh: %d\r\n",
4653 uri->refresh);
4654
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004655 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4656
4657 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4658 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4659 else
4660 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004661
4662 s->txn.status = 200;
4663 s->logs.tv_request = now;
4664
4665 if (bi_putchk(si->ib, &trash) == -1)
4666 return 0;
4667
4668 return 1;
4669}
4670
4671static int stats_send_http_redirect(struct stream_interface *si)
4672{
4673 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4674 struct session *s = session_from_task(si->owner);
4675 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004676 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004677
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004678 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004679 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004680 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004681 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004682 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4683 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004684 }
4685
4686 /* We don't want to land on the posted stats page because a refresh will
4687 * repost the data. We don't want this to happen on accident so we redirect
4688 * the browse to the stats page with a GET.
4689 */
4690 chunk_printf(&trash,
4691 "HTTP/1.1 303 See Other\r\n"
4692 "Cache-Control: no-cache\r\n"
4693 "Content-Type: text/plain\r\n"
4694 "Connection: close\r\n"
4695 "Location: %s;st=%s%s%s%s\r\n"
4696 "\r\n",
4697 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004698 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4699 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4700 stat_status_codes[appctx->ctx.stats.st_code]) ?
4701 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004702 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004703 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4704 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004705 scope_txt);
4706
4707 s->txn.status = 303;
4708 s->logs.tv_request = now;
4709
4710 if (bi_putchk(si->ib, &trash) == -1)
4711 return 0;
4712
4713 return 1;
4714}
4715
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004716/* This I/O handler runs as an applet embedded in a stream interface. It is
4717 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004718 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004719 * automatically unregisters itself once transfer is complete.
4720 */
4721static void http_stats_io_handler(struct stream_interface *si)
4722{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004723 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004724 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004725 struct channel *req = si->ob;
4726 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004727
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004728 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4729 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004730
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004731 /* check that the output is not closed */
4732 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004733 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004734
Willy Tarreau347a35d2013-11-22 17:51:09 +01004735 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004736 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004737 if (stats_send_http_headers(si)) {
4738 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004739 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004740 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004741 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004742 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004743 }
4744
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004745 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004746 unsigned int prev_len = si->ib->buf->i;
4747 unsigned int data_len;
4748 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004749 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004750
4751 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4752 /* One difficulty we're facing is that we must prevent
4753 * the input data from being automatically forwarded to
4754 * the output area. For this, we temporarily disable
4755 * forwarding on the channel.
4756 */
4757 last_fwd = si->ib->to_forward;
4758 si->ib->to_forward = 0;
4759 chunk_printf(&trash, "\r\n000000\r\n");
4760 if (bi_putchk(si->ib, &trash) == -1) {
4761 si->ib->to_forward = last_fwd;
4762 goto fail;
4763 }
4764 }
4765
4766 data_len = si->ib->buf->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004767 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004768 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004769
4770 last_len = si->ib->buf->i;
4771
4772 /* Now we must either adjust or remove the chunk size. This is
4773 * not easy because the chunk size might wrap at the end of the
4774 * buffer, so we pretend we have nothing in the buffer, we write
4775 * the size, then restore the buffer's contents. Note that we can
4776 * only do that because no forwarding is scheduled on the stats
4777 * applet.
4778 */
4779 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4780 si->ib->total -= (last_len - prev_len);
4781 si->ib->buf->i -= (last_len - prev_len);
4782
4783 if (last_len != data_len) {
4784 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
4785 bi_putchk(si->ib, &trash);
4786
4787 si->ib->total += (last_len - data_len);
4788 si->ib->buf->i += (last_len - data_len);
4789 }
4790 /* now re-enable forwarding */
4791 channel_forward(si->ib, last_fwd);
4792 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004793 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004794
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004795 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004796 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004797 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004798 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004799 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004800 }
4801
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004802 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004803 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004804 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004805 }
4806
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004807 if (appctx->st0 == STAT_HTTP_DONE) {
4808 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4809 chunk_printf(&trash, "\r\n0\r\n\r\n");
4810 if (bi_putchk(si->ib, &trash) == -1)
4811 goto fail;
4812 }
4813 /* eat the whole request */
4814 bo_skip(si->ob, si->ob->buf->o);
4815 res->flags |= CF_READ_NULL;
4816 si_shutr(si);
4817 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004818
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004819 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4820 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004821
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004822 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004823 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4824 si_shutr(si);
4825 res->flags |= CF_READ_NULL;
4826 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004827 }
Willy Tarreau91861262007-10-17 17:06:05 +02004828
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004829 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004830 /* update all other flags and resync with the other side */
4831 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004832
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004833 /* we don't want to expire timeouts while we're processing requests */
4834 si->ib->rex = TICK_ETERNITY;
4835 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004836
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004837 out:
4838 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4839 /* check that we have released everything then unregister */
4840 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004841 }
4842}
4843
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004844
Willy Tarreau909d5172012-11-26 03:04:41 +01004845static inline const char *get_conn_ctrl_name(const struct connection *conn)
4846{
Willy Tarreau3c728722014-01-23 13:50:42 +01004847 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004848 return "NONE";
4849 return conn->ctrl->name;
4850}
4851
4852static inline const char *get_conn_xprt_name(const struct connection *conn)
4853{
4854 static char ptr[17];
4855
Willy Tarreauaad69382014-01-23 14:21:42 +01004856 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004857 return "NONE";
4858
4859 if (conn->xprt == &raw_sock)
4860 return "RAW";
4861
4862#ifdef USE_OPENSSL
4863 if (conn->xprt == &ssl_sock)
4864 return "SSL";
4865#endif
4866 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4867 return ptr;
4868}
4869
4870static inline const char *get_conn_data_name(const struct connection *conn)
4871{
4872 static char ptr[17];
4873
4874 if (!conn->data)
4875 return "NONE";
4876
4877 if (conn->data == &sess_conn_cb)
4878 return "SESS";
4879
4880 if (conn->data == &si_conn_cb)
4881 return "STRM";
4882
4883 if (conn->data == &check_conn_cb)
4884 return "CHCK";
4885
4886 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4887 return ptr;
4888}
4889
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004890/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004891 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004892 * 0 if the output buffer is full and it needs to be called again, otherwise
4893 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004894 */
Willy Tarreau76153662012-11-26 01:16:39 +01004895static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004896{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004897 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004898 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004899 extern const char *monthname[12];
4900 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004901 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004902 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004903
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004904 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004905
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004906 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004907 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004908 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4909 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004910 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004911 appctx->ctx.sess.uid = 0;
4912 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004913 return 1;
4914 }
4915
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004916 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004917 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004918 appctx->ctx.sess.uid = sess->uniq_id;
4919 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004920 /* fall through */
4921
4922 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004923 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004924 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004925 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004926 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004927 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4928 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004929 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004930 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004931
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004932 conn = objt_conn(sess->si[0].end);
4933 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004934 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004935 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004936 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004937 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004938 break;
4939 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004940 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004941 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004942 default:
4943 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004944 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004945 break;
4946 }
4947
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004948 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004949 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004950 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004951
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004952 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004953 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004954 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4955 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4956 sess->listener ? sess->listener->luid : 0);
4957
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004958 if (conn)
4959 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004960
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004961 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004962 case AF_INET:
4963 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004964 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004965 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004966 break;
4967 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004968 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004969 break;
4970 default:
4971 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004972 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004973 break;
4974 }
4975
Willy Tarreau50943332011-09-02 17:33:05 +02004976 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004977 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004978 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004979 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004980 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4981 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004982 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004983
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004984 conn = objt_conn(sess->si[1].end);
4985 if (conn)
4986 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004987
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004988 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004989 case AF_INET:
4990 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004991 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004992 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004993 break;
4994 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004995 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004996 break;
4997 default:
4998 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004999 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005000 break;
5001 }
5002
5003 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005004 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005005 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005006 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5007 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005008 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005009 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005010
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005011 if (conn)
5012 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005013
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005014 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005015 case AF_INET:
5016 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005017 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005018 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005019 break;
5020 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005021 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005022 break;
5023 default:
5024 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005025 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005026 break;
5027 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005028
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005029 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005030 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005031 sess->task,
5032 sess->task->state,
5033 sess->task->nice, sess->task->calls,
5034 sess->task->expire ?
5035 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5036 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5037 TICKS_TO_MS(1000)) : "<NEVER>",
5038 task_in_rq(sess->task) ? ", running" : "");
5039
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005040 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005041 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005042 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5043
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005044 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005045 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
5046 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
5047 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
5048
5049 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005050 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005051 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005052 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005053 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005054 obj_type_name(sess->si[0].end),
5055 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005056 sess->si[0].exp ?
5057 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5058 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5059 TICKS_TO_MS(1000)) : "<NEVER>",
5060 sess->si[0].err_type);
5061
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005062 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005063 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005064 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005065 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005066 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005067 obj_type_name(sess->si[1].end),
5068 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005069 sess->si[1].exp ?
5070 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5071 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5072 TICKS_TO_MS(1000)) : "<NEVER>",
5073 sess->si[1].err_type);
5074
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005075 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005076 chunk_appendf(&trash,
5077 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005078 conn,
5079 get_conn_ctrl_name(conn),
5080 get_conn_xprt_name(conn),
5081 get_conn_data_name(conn),
5082 obj_type_name(conn->target),
5083 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005084
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005085 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005086 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005087 conn->flags,
5088 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005089 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005090 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005091 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005092 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005093 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5094 chunk_appendf(&trash,
5095 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5096 tmpctx,
5097 tmpctx->st0,
5098 tmpctx->st1,
5099 tmpctx->st2,
5100 tmpctx->applet->name);
5101 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005102
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005103 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005104 chunk_appendf(&trash,
5105 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005106 conn,
5107 get_conn_ctrl_name(conn),
5108 get_conn_xprt_name(conn),
5109 get_conn_data_name(conn),
5110 obj_type_name(conn->target),
5111 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005112
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005113 chunk_appendf(&trash,
5114 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005115 conn->flags,
5116 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005117 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005118 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005119 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005120 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005121 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5122 chunk_appendf(&trash,
5123 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5124 tmpctx,
5125 tmpctx->st0,
5126 tmpctx->st1,
5127 tmpctx->st2,
5128 tmpctx->applet->name);
5129 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005130
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005131 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005132 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005133 " an_exp=%s",
5134 sess->req,
5135 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005136 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01005137 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005138 sess->req->analyse_exp ?
5139 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
5140 TICKS_TO_MS(1000)) : "<NEVER>");
5141
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005142 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005143 " rex=%s",
5144 sess->req->rex ?
5145 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
5146 TICKS_TO_MS(1000)) : "<NEVER>");
5147
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005148 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005149 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005150 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005151 sess->req->wex ?
5152 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
5153 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01005154 sess->req->buf,
5155 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005156 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01005157 sess->txn.req.next, sess->req->buf->i,
5158 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005159
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005160 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005161 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005162 " an_exp=%s",
5163 sess->rep,
5164 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005165 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01005166 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005167 sess->rep->analyse_exp ?
5168 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
5169 TICKS_TO_MS(1000)) : "<NEVER>");
5170
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005171 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005172 " rex=%s",
5173 sess->rep->rex ?
5174 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
5175 TICKS_TO_MS(1000)) : "<NEVER>");
5176
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005177 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005178 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005179 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005180 sess->rep->wex ?
5181 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
5182 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01005183 sess->rep->buf,
5184 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005185 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01005186 sess->txn.rsp.next, sess->rep->buf->i,
5187 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005188
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005189 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005190 return 0;
5191
5192 /* use other states to dump the contents */
5193 }
5194 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005195 appctx->ctx.sess.uid = 0;
5196 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005197 return 1;
5198}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005199
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005200static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005201{
5202 struct appctx *appctx = __objt_appctx(si->end);
5203
5204 switch (appctx->st2) {
5205 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005206 /* Display the column headers. If the message cannot be sent,
5207 * quit the fucntion with returning 0. The function is called
5208 * later and restart at the state "STAT_ST_INIT".
5209 */
5210 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005211 chunk_appendf(&trash, "# id (file) description\n");
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005212 if (bi_putchk(si->ib, &trash) == -1)
5213 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005214
5215 /* Now, we start the browsing of the references lists.
5216 * Note that the following call to LIST_ELEM return bad pointer. The only
5217 * avalaible field of this pointer is <list>. It is used with the function
5218 * pat_list_get_next() for retruning the first avalaible entry
5219 */
5220 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5221 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5222 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005223 appctx->st2 = STAT_ST_LIST;
5224 /* fall through */
5225
5226 case STAT_ST_LIST:
5227 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005228 chunk_reset(&trash);
5229
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005230 /* Build messages. If the reference is used by another category than
5231 * the listed categorie, display the information in the massage.
5232 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005233 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005234 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5235 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005236
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005237 if (bi_putchk(si->ib, &trash) == -1) {
5238 /* let's try again later from this session. We add ourselves into
5239 * this session's users so that it can remove us upon termination.
5240 */
5241 return 0;
5242 }
5243
5244 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005245 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5246 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005247 }
5248
5249 appctx->st2 = STAT_ST_FIN;
5250 /* fall through */
5251
5252 default:
5253 appctx->st2 = STAT_ST_FIN;
5254 return 1;
5255 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005256 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005257}
5258
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005259static int stats_map_lookup(struct stream_interface *si)
5260{
5261 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005262 struct sample sample;
5263 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005264 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005265
5266 switch (appctx->st2) {
5267 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005268 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005269 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005270 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005271 appctx->st2 = STAT_ST_LIST;
5272 /* fall through */
5273
5274 case STAT_ST_LIST:
5275 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005276 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005277 /* initialise chunk to build new message */
5278 chunk_reset(&trash);
5279
5280 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005281 sample.type = SMP_T_STR;
5282 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005283 sample.data.str.len = appctx->ctx.map.chunk.len;
5284 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005285 if (appctx->ctx.map.expr->pat_head->match &&
5286 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005287 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5288 else
5289 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005290
5291 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005292 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5293 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5294 break;
5295 if (match_method >= PAT_MATCH_NUM)
5296 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5297 else
5298 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005299
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005300 /* case sensitive */
5301 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5302 chunk_appendf(&trash, ", case=insensitive");
5303 else
5304 chunk_appendf(&trash, ", case=sensitive");
5305
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005306 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005307 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005308 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5309 chunk_appendf(&trash, ", found=no");
5310 else
5311 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005312 }
5313
5314 /* Display match and match info */
5315 else {
5316 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005317 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5318 chunk_appendf(&trash, ", found=yes");
5319 else
5320 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005321
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005322 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005323 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005324 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005325 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005326 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005327
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005328 /* display pattern */
5329 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5330 if (pat->ref && pat->ref->pattern)
5331 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5332 else
5333 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005334 }
5335 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005336 if (pat->ref && pat->ref->pattern)
5337 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5338 else
5339 chunk_appendf(&trash, ", pattern=unknown");
5340 }
5341
5342 /* display return value */
5343 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5344 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005345 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005346 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005347 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005348 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005349 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005350 }
5351
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005352 chunk_appendf(&trash, "\n");
5353
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005354 /* display response */
5355 if (bi_putchk(si->ib, &trash) == -1) {
5356 /* let's try again later from this session. We add ourselves into
5357 * this session's users so that it can remove us upon termination.
5358 */
5359 return 0;
5360 }
5361
5362 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005363 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5364 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005365 }
5366
5367 appctx->st2 = STAT_ST_FIN;
5368 /* fall through */
5369
5370 default:
5371 appctx->st2 = STAT_ST_FIN;
5372 free(appctx->ctx.map.chunk.str);
5373 return 1;
5374 }
5375}
5376
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005377static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005378{
5379 struct appctx *appctx = __objt_appctx(si->end);
5380
5381 switch (appctx->st2) {
5382
5383 case STAT_ST_INIT:
5384 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005385 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5386 struct pat_ref_elt *, list);
5387 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5388 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005389 appctx->st2 = STAT_ST_LIST;
5390 /* fall through */
5391
5392 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005393 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005394 chunk_reset(&trash);
5395
5396 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005397 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005398 chunk_appendf(&trash, "%p %s %s\n",
5399 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5400 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005401 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005402 chunk_appendf(&trash, "%p %s\n",
5403 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005404
5405 if (bi_putchk(si->ib, &trash) == -1) {
5406 /* let's try again later from this session. We add ourselves into
5407 * this session's users so that it can remove us upon termination.
5408 */
5409 return 0;
5410 }
5411
5412 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005413 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5414 struct pat_ref_elt *, list);
5415 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005416 break;
5417 }
5418
5419 appctx->st2 = STAT_ST_FIN;
5420 /* fall through */
5421
5422 default:
5423 appctx->st2 = STAT_ST_FIN;
5424 return 1;
5425 }
5426}
5427
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005428/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005429 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005430 * to be called again, otherwise non-zero. It is designed to be called
5431 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005432 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005433static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005434{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005435 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005436 struct connection *conn;
5437
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005438 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005439 /* If we're forced to shut down, we might have to remove our
5440 * reference to the last session being dumped.
5441 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005442 if (appctx->st2 == STAT_ST_LIST) {
5443 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5444 LIST_DEL(&appctx->ctx.sess.bref.users);
5445 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005446 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005447 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005448 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005449 }
5450
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005451 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005452
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005453 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005454 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005455 /* the function had not been called yet, let's prepare the
5456 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005457 * pointer to the first in the global list. When a target
5458 * session is being destroyed, it is responsible for updating
5459 * this pointer. We know we have reached the end when this
5460 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005461 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005462 LIST_INIT(&appctx->ctx.sess.bref.users);
5463 appctx->ctx.sess.bref.ref = sessions.n;
5464 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005465 /* fall through */
5466
Willy Tarreau295a8372011-03-10 11:25:07 +01005467 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005468 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005469 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5470 LIST_DEL(&appctx->ctx.sess.bref.users);
5471 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005472 }
5473
5474 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005475 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005476 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005477 struct session *curr_sess;
5478
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005479 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005480
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005481 if (appctx->ctx.sess.target) {
5482 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005483 goto next_sess;
5484
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005485 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005486 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005487 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005488 return 0;
5489
5490 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005491 LIST_DEL(&appctx->ctx.sess.bref.users);
5492 LIST_INIT(&appctx->ctx.sess.bref.users);
5493 if (appctx->ctx.sess.target != (void *)-1) {
5494 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005495 break;
5496 }
5497 else
5498 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005499 }
5500
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005501 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005502 "%p: proto=%s",
5503 curr_sess,
5504 curr_sess->listener->proto->name);
5505
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005506
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005507 conn = objt_conn(curr_sess->si[0].end);
5508 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005509 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005510 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005511 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005512 " src=%s:%d fe=%s be=%s srv=%s",
5513 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005514 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005515 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005516 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005517 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005518 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005519 break;
5520 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005521 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005522 " src=unix:%d fe=%s be=%s srv=%s",
5523 curr_sess->listener->luid,
5524 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005525 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005526 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005527 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005528 break;
5529 }
5530
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005531 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005532 " ts=%02x age=%s calls=%d",
5533 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005534 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5535 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005536
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005537 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005538 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005539 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005540 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005541 curr_sess->req->analysers,
5542 curr_sess->req->rex ?
5543 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5544 TICKS_TO_MS(1000)) : "");
5545
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005546 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005547 ",wx=%s",
5548 curr_sess->req->wex ?
5549 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5550 TICKS_TO_MS(1000)) : "");
5551
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005552 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005553 ",ax=%s]",
5554 curr_sess->req->analyse_exp ?
5555 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5556 TICKS_TO_MS(1000)) : "");
5557
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005558 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005559 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005560 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005561 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005562 curr_sess->rep->analysers,
5563 curr_sess->rep->rex ?
5564 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5565 TICKS_TO_MS(1000)) : "");
5566
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005567 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005568 ",wx=%s",
5569 curr_sess->rep->wex ?
5570 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5571 TICKS_TO_MS(1000)) : "");
5572
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005573 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005574 ",ax=%s]",
5575 curr_sess->rep->analyse_exp ?
5576 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5577 TICKS_TO_MS(1000)) : "");
5578
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005579 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005580 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005581 " s0=[%d,%1xh,fd=%d,ex=%s]",
5582 curr_sess->si[0].state,
5583 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005584 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005585 curr_sess->si[0].exp ?
5586 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5587 TICKS_TO_MS(1000)) : "");
5588
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005589 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005590 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005591 " s1=[%d,%1xh,fd=%d,ex=%s]",
5592 curr_sess->si[1].state,
5593 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005594 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005595 curr_sess->si[1].exp ?
5596 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5597 TICKS_TO_MS(1000)) : "");
5598
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005599 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005600 " exp=%s",
5601 curr_sess->task->expire ?
5602 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5603 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005604 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005605 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005606
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005607 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005608
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005609 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005610 /* let's try again later from this session. We add ourselves into
5611 * this session's users so that it can remove us upon termination.
5612 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005613 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005614 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005615 }
5616
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005617 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005618 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005619 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005620
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005621 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005622 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005623 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005624 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005625 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005626 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005627
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005628 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005629 return 0;
5630
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005631 appctx->ctx.sess.target = NULL;
5632 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005633 return 1;
5634 }
5635
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005636 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005637 /* fall through */
5638
5639 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005640 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005641 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005642 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005643}
5644
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005645/* This is called when the stream interface is closed. For instance, upon an
5646 * external abort, we won't call the i/o handler anymore so we may need to
5647 * remove back references to the session currently being dumped.
5648 */
Simon Horman74d88312013-02-12 10:45:50 +09005649static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005650{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005651 struct appctx *appctx = __objt_appctx(si->end);
5652
5653 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5654 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5655 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005656 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005657 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5658 free(appctx->ctx.cli.err);
5659 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005660 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5661 free(appctx->ctx.map.chunk.str);
5662 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005663}
5664
Willy Tarreau20e99322013-04-13 09:22:25 +02005665/* This function is used to either dump tables states (when action is set
5666 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005667 * It returns 0 if the output buffer is full and it needs to be called
5668 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005669 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005670static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005671{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005672 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005673 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005674 struct ebmb_node *eb;
5675 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005676 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005677 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005678
5679 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005680 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005681 * - STAT_ST_INIT : the first call
5682 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005683 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005684 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005685 * and the entry pointer points to the next entry to be dumped,
5686 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005687 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005688 * data though.
5689 */
5690
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005691 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005692 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005693 if (appctx->st2 == STAT_ST_LIST) {
5694 appctx->ctx.table.entry->ref_cnt--;
5695 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005696 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005697 return 1;
5698 }
5699
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005700 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005701
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005702 while (appctx->st2 != STAT_ST_FIN) {
5703 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005704 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005705 appctx->ctx.table.proxy = appctx->ctx.table.target;
5706 if (!appctx->ctx.table.proxy)
5707 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005708
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005709 appctx->ctx.table.entry = NULL;
5710 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005711 break;
5712
Willy Tarreau295a8372011-03-10 11:25:07 +01005713 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005714 if (!appctx->ctx.table.proxy ||
5715 (appctx->ctx.table.target &&
5716 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5717 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005718 break;
5719 }
5720
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005721 if (appctx->ctx.table.proxy->table.size) {
5722 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5723 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005724 return 0;
5725
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005726 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005727 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005728 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005729 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005730 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005731 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5732 appctx->ctx.table.entry->ref_cnt++;
5733 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005734 break;
5735 }
5736 }
5737 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005738 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005739 break;
5740
Willy Tarreau295a8372011-03-10 11:25:07 +01005741 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005742 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005743
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005744 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005745 /* we're filtering on some data contents */
5746 void *ptr;
5747 long long data;
5748
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005749 dt = appctx->ctx.table.data_type;
5750 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5751 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005752 dt);
5753
5754 data = 0;
5755 switch (stktable_data_types[dt].std_type) {
5756 case STD_T_SINT:
5757 data = stktable_data_cast(ptr, std_t_sint);
5758 break;
5759 case STD_T_UINT:
5760 data = stktable_data_cast(ptr, std_t_uint);
5761 break;
5762 case STD_T_ULL:
5763 data = stktable_data_cast(ptr, std_t_ull);
5764 break;
5765 case STD_T_FRQP:
5766 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005767 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005768 break;
5769 }
5770
5771 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005772 if ((data < appctx->ctx.table.value &&
5773 (appctx->ctx.table.data_op == STD_OP_EQ ||
5774 appctx->ctx.table.data_op == STD_OP_GT ||
5775 appctx->ctx.table.data_op == STD_OP_GE)) ||
5776 (data == appctx->ctx.table.value &&
5777 (appctx->ctx.table.data_op == STD_OP_NE ||
5778 appctx->ctx.table.data_op == STD_OP_GT ||
5779 appctx->ctx.table.data_op == STD_OP_LT)) ||
5780 (data > appctx->ctx.table.value &&
5781 (appctx->ctx.table.data_op == STD_OP_EQ ||
5782 appctx->ctx.table.data_op == STD_OP_LT ||
5783 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005784 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005785 }
5786
Simon Hormanc88b8872011-06-15 15:18:49 +09005787 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005788 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5789 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005790 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005791
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005792 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005793
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005794 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005795 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005796 struct stksess *old = appctx->ctx.table.entry;
5797 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005798 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005799 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5800 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5801 stksess_kill(&appctx->ctx.table.proxy->table, old);
5802 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005803 break;
5804 }
5805
Simon Hormanc88b8872011-06-15 15:18:49 +09005806
5807 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005808 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5809 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5810 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005811
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005812 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
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_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005817 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005818 break;
5819 }
5820 }
5821 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005822}
5823
Willy Tarreaud426a182010-03-05 14:58:26 +01005824/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005825 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5826 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5827 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5828 * lines are respected within the limit of 70 output chars. Lines that are
5829 * continuation of a previous truncated line begin with "+" instead of " "
5830 * after the offset. The new pointer is returned.
5831 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005832static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5833 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005834{
5835 int end;
5836 unsigned char c;
5837
5838 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005839 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005840 return ptr;
5841
Willy Tarreau77804732012-10-29 16:14:26 +01005842 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005843
Willy Tarreaud426a182010-03-05 14:58:26 +01005844 while (ptr < len && ptr < bsize) {
5845 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005846 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005847 if (out->len > end - 2)
5848 break;
5849 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005850 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005851 if (out->len > end - 3)
5852 break;
5853 out->str[out->len++] = '\\';
5854 switch (c) {
5855 case '\t': c = 't'; break;
5856 case '\n': c = 'n'; break;
5857 case '\r': c = 'r'; break;
5858 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005859 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005860 }
5861 out->str[out->len++] = c;
5862 } else {
5863 if (out->len > end - 5)
5864 break;
5865 out->str[out->len++] = '\\';
5866 out->str[out->len++] = 'x';
5867 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5868 out->str[out->len++] = hextab[c & 0xF];
5869 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005870 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005871 /* we had a line break, let's return now */
5872 out->str[out->len++] = '\n';
5873 *line = ptr;
5874 return ptr;
5875 }
5876 }
5877 /* we have an incomplete line, we return it as-is */
5878 out->str[out->len++] = '\n';
5879 return ptr;
5880}
5881
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005882/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005883 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005884 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005885 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005886static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005887{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005888 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005889 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005890
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005891 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005892 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005893
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005894 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005895
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005896 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005897 /* the function had not been called yet, let's prepare the
5898 * buffer for a response.
5899 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005900 struct tm tm;
5901
5902 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005903 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005904 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5905 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5906 error_snapshot_id);
5907
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005908 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005909 /* Socket buffer full. Let's try again later from the same point */
5910 return 0;
5911 }
5912
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005913 appctx->ctx.errors.px = proxy;
5914 appctx->ctx.errors.buf = 0;
5915 appctx->ctx.errors.bol = 0;
5916 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005917 }
5918
5919 /* we have two inner loops here, one for the proxy, the other one for
5920 * the buffer.
5921 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005922 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005923 struct error_snapshot *es;
5924
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005925 if (appctx->ctx.errors.buf == 0)
5926 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005927 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005928 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005929
5930 if (!es->when.tv_sec)
5931 goto next;
5932
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005933 if (appctx->ctx.errors.iid >= 0 &&
5934 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5935 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005936 goto next;
5937
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005938 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005939 /* just print headers now */
5940
5941 char pn[INET6_ADDRSTRLEN];
5942 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005943 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005944
5945 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005946 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005947 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005948 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005949
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005950 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5951 case AF_INET:
5952 case AF_INET6:
5953 port = get_host_port(&es->src);
5954 break;
5955 default:
5956 port = 0;
5957 }
5958
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005959 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005960 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005961 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005962 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005963 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005964 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005965 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5966 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005967 break;
5968 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005969 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005970 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005971 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005972 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005973 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005974 break;
5975 }
5976
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005977 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005978 ", server %s (#%d), event #%u\n"
5979 " src %s:%d, session #%d, session flags 0x%08x\n"
5980 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5981 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5982 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5983 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5984 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5985 es->ev_id,
5986 pn, port, es->sid, es->s_flags,
5987 es->state, es->m_flags, es->t_flags,
5988 es->m_clen, es->m_blen,
5989 es->b_flags, es->b_out, es->b_tot,
5990 es->len, es->b_wrap, es->pos);
5991
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005992 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005993 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005994 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005995 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005996 appctx->ctx.errors.ptr = 0;
5997 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005998 }
5999
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006000 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006001 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006002 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006003 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006004 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02006005 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006006 goto next;
6007 }
6008
6009 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006010 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006011 int newptr;
6012 int newline;
6013
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006014 newline = appctx->ctx.errors.bol;
6015 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6016 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006017 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006018
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006019 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006020 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02006021 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006022 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006023 appctx->ctx.errors.ptr = newptr;
6024 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006025 };
6026 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006027 appctx->ctx.errors.bol = 0;
6028 appctx->ctx.errors.ptr = -1;
6029 appctx->ctx.errors.buf++;
6030 if (appctx->ctx.errors.buf > 1) {
6031 appctx->ctx.errors.buf = 0;
6032 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006033 }
6034 }
6035
6036 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006037 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006038}
6039
Willy Tarreaud5781202012-09-22 19:32:35 +02006040/* parse the "level" argument on the bind lines */
6041static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6042{
6043 if (!*args[cur_arg + 1]) {
6044 memprintf(err, "'%s' : missing level", args[cur_arg]);
6045 return ERR_ALERT | ERR_FATAL;
6046 }
6047
6048 if (!strcmp(args[cur_arg+1], "user"))
6049 conf->level = ACCESS_LVL_USER;
6050 else if (!strcmp(args[cur_arg+1], "operator"))
6051 conf->level = ACCESS_LVL_OPER;
6052 else if (!strcmp(args[cur_arg+1], "admin"))
6053 conf->level = ACCESS_LVL_ADMIN;
6054 else {
6055 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6056 args[cur_arg], args[cur_arg+1]);
6057 return ERR_ALERT | ERR_FATAL;
6058 }
6059
6060 return 0;
6061}
6062
Willy Tarreaub24281b2011-02-13 13:16:36 +01006063struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006064 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006065 .name = "<STATS>", /* used for logging */
6066 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006067 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006068};
6069
Simon Horman9bd2c732011-06-15 15:18:44 +09006070static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006071 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006072 .name = "<CLI>", /* used for logging */
6073 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006074 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006075};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006076
Willy Tarreaudc13c112013-06-21 23:16:39 +02006077static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006078 { CFG_GLOBAL, "stats", stats_parse_global },
6079 { 0, NULL, NULL },
6080}};
6081
Willy Tarreaud5781202012-09-22 19:32:35 +02006082static struct bind_kw_list bind_kws = { "STAT", { }, {
6083 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6084 { NULL, NULL, 0 },
6085}};
6086
Willy Tarreau10522fd2008-07-09 20:12:41 +02006087__attribute__((constructor))
6088static void __dumpstats_module_init(void)
6089{
6090 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02006091 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02006092}
6093
Willy Tarreau91861262007-10-17 17:06:05 +02006094/*
6095 * Local variables:
6096 * c-indent-level: 8
6097 * c-basic-offset: 8
6098 * End:
6099 */