blob: 02bff56b73466191edc82aab3f0fea221903d181 [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,
92 ST_ADM_ACTION_READY,
93 ST_ADM_ACTION_DRAIN,
94 ST_ADM_ACTION_MAINT,
95 ST_ADM_ACTION_SHUTDOWN,
96 /* these are the ancient actions, still available for compatibility */
97 ST_ADM_ACTION_DISABLE,
98 ST_ADM_ACTION_ENABLE,
99 ST_ADM_ACTION_STOP,
100 ST_ADM_ACTION_START,
101};
102
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100103static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100104static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +0100105static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900106static int stats_dump_sess_to_buffer(struct stream_interface *si);
107static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100108static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100109static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
110static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100111static int stats_pats_list(struct stream_interface *si);
112static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100113static int stats_map_lookup(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +0900114
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100115/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100116 * cli_io_handler()
117 * -> stats_dump_sess_to_buffer() // "show sess"
118 * -> stats_dump_errors_to_buffer() // "show errors"
119 * -> stats_dump_info_to_buffer() // "show info"
120 * -> stats_dump_stat_to_buffer() // "show stat"
121 * -> stats_dump_csv_header()
122 * -> stats_dump_proxy_to_buffer()
123 * -> stats_dump_fe_stats()
124 * -> stats_dump_li_stats()
125 * -> stats_dump_sv_stats()
126 * -> stats_dump_be_stats()
127 *
128 * http_stats_io_handler()
129 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
130 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
131 * -> stats_dump_html_head() // emits the HTML headers
132 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
133 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
134 * -> stats_dump_html_px_hdr()
135 * -> stats_dump_fe_stats()
136 * -> stats_dump_li_stats()
137 * -> stats_dump_sv_stats()
138 * -> stats_dump_be_stats()
139 * -> stats_dump_html_px_end()
140 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100141 */
142
Simon Horman9bd2c732011-06-15 15:18:44 +0900143static struct si_applet cli_applet;
144
145static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200146 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200147 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200148 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200149 " help : this message\n"
150 " prompt : toggle interactive mode with prompt\n"
151 " quit : disconnect\n"
152 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100153 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200154 " show stat : report counters for each proxy and server\n"
155 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100156 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200157 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200158 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200159 " set weight : change a server's weight\n"
Willy Tarreau2a4b70f2014-05-22 18:42:35 +0200160 " set server : change a server's state or weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200161 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100162 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200163 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200164 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200165 " disable : put a server or frontend in maintenance mode\n"
166 " enable : re-enable a server or frontend which is in maintenance mode\n"
167 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100168 " show acl [id] : report avalaible acls or dump an acl's contents\n"
169 " get acl : reports the patterns matching a sample for an ACL\n"
170 " add acl : add acl entry\n"
171 " del acl : delete acl entry\n"
172 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100173 " show map [id] : report avalaible maps or dump a map's contents\n"
174 " get map : reports the keys and values matching a sample for a map\n"
175 " set map : modify map entry\n"
176 " add map : add map entry\n"
177 " del map : delete map entry\n"
178 " clear map <id> : clear the content of this map\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200179 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200180
Simon Horman9bd2c732011-06-15 15:18:44 +0900181static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200182 "Permission denied\n"
183 "";
184
Willy Tarreau295a8372011-03-10 11:25:07 +0100185/* data transmission states for the stats responses */
186enum {
187 STAT_ST_INIT = 0,
188 STAT_ST_HEAD,
189 STAT_ST_INFO,
190 STAT_ST_LIST,
191 STAT_ST_END,
192 STAT_ST_FIN,
193};
194
195/* data transmission states for the stats responses inside a proxy */
196enum {
197 STAT_PX_ST_INIT = 0,
198 STAT_PX_ST_TH,
199 STAT_PX_ST_FE,
200 STAT_PX_ST_LI,
201 STAT_PX_ST_SV,
202 STAT_PX_ST_BE,
203 STAT_PX_ST_END,
204 STAT_PX_ST_FIN,
205};
206
Cyril Bonté19979e12012-04-04 12:57:21 +0200207extern const char *stat_status_codes[];
208
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200209/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200210 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200211 * needs to be closed and ignored, or a negative value upon critical failure.
212 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900213static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200214{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100215 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100216 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200217
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200218 tv_zero(&s->logs.tv_request);
219 s->logs.t_queue = 0;
220 s->logs.t_connect = 0;
221 s->logs.t_data = 0;
222 s->logs.t_close = 0;
223 s->logs.bytes_in = s->logs.bytes_out = 0;
224 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
225 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200226
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200227 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200228
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200229 if (s->listener->timeout) {
230 s->req->rto = *s->listener->timeout;
231 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200232 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200233 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200234}
235
Willy Tarreau07e9e642010-08-17 21:48:17 +0200236/* allocate a new stats frontend named <name>, and return it
237 * (or NULL in case of lack of memory).
238 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200239static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200240{
241 struct proxy *fe;
242
243 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
244 if (!fe)
245 return NULL;
246
Willy Tarreau237250c2011-07-29 01:49:03 +0200247 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200248 fe->next = proxy;
249 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200250 fe->last_change = now.tv_sec;
251 fe->id = strdup("GLOBAL");
252 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200253 fe->maxconn = 10; /* default to 10 concurrent connections */
254 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200255 fe->conf.file = strdup(file);
256 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200257 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200258
259 /* the stats frontend is the only one able to assign ID #0 */
260 fe->conf.id.key = fe->uuid = 0;
261 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200262 return fe;
263}
264
Willy Tarreaufbee7132007-10-18 13:53:22 +0200265/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200266 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
267 * error message into the <err> buffer which will be preallocated. The trailing
268 * '\n' must not be written. The function must be called with <args> pointing to
269 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200270 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200271static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200272 struct proxy *defpx, const char *file, int line,
273 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200274{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200275 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200276 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200277
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200278 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200279 int cur_arg;
280
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200281 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200282 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 +0200283 return -1;
284 }
285
Willy Tarreau89a63132009-08-16 17:41:45 +0200286 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200287 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200288 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200289 return -1;
290 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200291 }
292
Willy Tarreau4348fad2012-09-20 16:48:07 +0200293 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200294 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200295
Willy Tarreauc53d4222012-09-20 20:19:28 +0200296 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
297 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
298 file, line, args[0], args[1], err && *err ? *err : "error");
299 return -1;
300 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200301
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200302 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200303 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200304 static int bind_dumped;
305 struct bind_kw *kw;
306
307 kw = bind_find_kw(args[cur_arg]);
308 if (kw) {
309 if (!kw->parse) {
310 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
311 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200312 return -1;
313 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200314
315 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
316 if (err && *err)
317 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
318 else
319 memprintf(err, "'%s %s' : error encountered while processing '%s'",
320 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200321 return -1;
322 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200323
324 cur_arg += 1 + kw->skip;
325 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200326 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200327
328 if (!bind_dumped) {
329 bind_dump_kws(err);
330 indent_msg(err, 4);
331 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200332 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200333
334 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
335 args[0], args[1], args[cur_arg],
336 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
337 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200338 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100339
Willy Tarreauc53d4222012-09-20 20:19:28 +0200340 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
341 l->maxconn = global.stats_fe->maxconn;
342 l->backlog = global.stats_fe->backlog;
343 l->timeout = &global.stats_fe->timeout.client;
344 l->accept = session_accept;
345 l->handler = process_session;
346 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
347 l->nice = -64; /* we want to boost priority for local stats */
348 global.maxsock += l->maxconn;
349 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200350 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200351 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100352 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200353 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100354
355 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200356 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100357 return -1;
358 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200359
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100360 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200361 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200362 return -1;
363 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200364 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200365 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200366 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200367 return -1;
368 }
369 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200370 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200371 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200372 else if (!strcmp(args[1], "maxconn")) {
373 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200374
375 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200376 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200377 return -1;
378 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200379
380 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200381 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200382 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200383 return -1;
384 }
385 }
386 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200387 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200388 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
389 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100390 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200391
Willy Tarreau91319572013-04-20 09:48:50 +0200392 if (!global.stats_fe) {
393 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
394 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
395 return -1;
396 }
397 }
398
Willy Tarreau35b7b162012-10-22 23:17:18 +0200399 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100400 unsigned int low, high;
401
Willy Tarreau35b7b162012-10-22 23:17:18 +0200402 if (strcmp(args[cur_arg], "all") == 0) {
403 set = 0;
404 break;
405 }
406 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100407 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200408 }
409 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100410 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200411 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100412 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100413 char *dash = strchr(args[cur_arg], '-');
414
415 low = high = str2uic(args[cur_arg]);
416 if (dash)
417 high = str2uic(dash + 1);
418
419 if (high < low) {
420 unsigned int swap = low;
421 low = high;
422 high = swap;
423 }
424
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100425 if (low < 1 || high > LONGBITS) {
426 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
427 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200428 return -1;
429 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100430 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100431 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100432 }
433 else {
434 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100435 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
436 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100437 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200438 }
439 cur_arg++;
440 }
441 global.stats_fe->bind_proc = set;
442 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200443 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200444 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200445 return -1;
446 }
447 return 0;
448}
449
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100450/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
451 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100452 * NOTE: Some tools happen to rely on the field position instead of its name,
453 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100454 */
455static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100456{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100457 chunk_appendf(&trash,
458 "# pxname,svname,"
459 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100460 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100461 "bin,bout,"
462 "dreq,dresp,"
463 "ereq,econ,eresp,"
464 "wretr,wredis,"
465 "status,weight,act,bck,"
466 "chkfail,chkdown,lastchg,downtime,qlimit,"
467 "pid,iid,sid,throttle,lbtot,tracked,type,"
468 "rate,rate_lim,rate_max,"
469 "check_status,check_code,check_duration,"
470 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
471 "req_rate,req_rate_max,req_tot,"
472 "cli_abrt,srv_abrt,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100473 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100474 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100475}
476
Simon Hormand9366582011-06-15 15:18:45 +0900477/* print a string of text buffer to <out>. The format is :
478 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
479 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
480 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
481 */
482static int dump_text(struct chunk *out, const char *buf, int bsize)
483{
484 unsigned char c;
485 int ptr = 0;
486
487 while (buf[ptr] && ptr < bsize) {
488 c = buf[ptr];
489 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
490 if (out->len > out->size - 1)
491 break;
492 out->str[out->len++] = c;
493 }
494 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
495 if (out->len > out->size - 2)
496 break;
497 out->str[out->len++] = '\\';
498 switch (c) {
499 case ' ': c = ' '; break;
500 case '\t': c = 't'; break;
501 case '\n': c = 'n'; break;
502 case '\r': c = 'r'; break;
503 case '\e': c = 'e'; break;
504 case '\\': c = '\\'; break;
505 }
506 out->str[out->len++] = c;
507 }
508 else {
509 if (out->len > out->size - 4)
510 break;
511 out->str[out->len++] = '\\';
512 out->str[out->len++] = 'x';
513 out->str[out->len++] = hextab[(c >> 4) & 0xF];
514 out->str[out->len++] = hextab[c & 0xF];
515 }
516 ptr++;
517 }
518
519 return ptr;
520}
521
522/* print a buffer in hexa.
523 * Print stopped if <bsize> is reached, or if no more place in the chunk.
524 */
525static int dump_binary(struct chunk *out, const char *buf, int bsize)
526{
527 unsigned char c;
528 int ptr = 0;
529
530 while (ptr < bsize) {
531 c = buf[ptr];
532
533 if (out->len > out->size - 2)
534 break;
535 out->str[out->len++] = hextab[(c >> 4) & 0xF];
536 out->str[out->len++] = hextab[c & 0xF];
537
538 ptr++;
539 }
540 return ptr;
541}
542
543/* Dump the status of a table to a stream interface's
544 * read buffer. It returns 0 if the output buffer is full
545 * and needs to be called again, otherwise non-zero.
546 */
547static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
548 struct proxy *proxy, struct proxy *target)
549{
Willy Tarreau306f8302013-07-08 15:53:06 +0200550 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900551
Willy Tarreau77804732012-10-29 16:14:26 +0100552 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900553 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
554
555 /* any other information should be dumped here */
556
Willy Tarreau290e63a2012-09-20 18:07:14 +0200557 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100558 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900559
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200560 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900561 return 0;
562
563 return 1;
564}
565
566/* Dump the a table entry to a stream interface's
567 * read buffer. It returns 0 if the output buffer is full
568 * and needs to be called again, otherwise non-zero.
569 */
570static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
571 struct proxy *proxy, struct stksess *entry)
572{
573 int dt;
574
Willy Tarreau77804732012-10-29 16:14:26 +0100575 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900576
577 if (proxy->table.type == STKTABLE_TYPE_IP) {
578 char addr[INET_ADDRSTRLEN];
579 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100580 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900581 }
582 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
583 char addr[INET6_ADDRSTRLEN];
584 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100585 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900586 }
587 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100588 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900589 }
590 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100591 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900592 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
593 }
594 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100595 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900596 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
597 }
598
Willy Tarreau77804732012-10-29 16:14:26 +0100599 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900600
601 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
602 void *ptr;
603
604 if (proxy->table.data_ofs[dt] == 0)
605 continue;
606 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100607 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900608 else
Willy Tarreau77804732012-10-29 16:14:26 +0100609 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900610
611 ptr = stktable_data_ptr(&proxy->table, entry, dt);
612 switch (stktable_data_types[dt].std_type) {
613 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100614 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900615 break;
616 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100617 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900618 break;
619 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100620 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900621 break;
622 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100623 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900624 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
625 proxy->table.data_arg[dt].u));
626 break;
627 }
628 }
Willy Tarreau77804732012-10-29 16:14:26 +0100629 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900630
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200631 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900632 return 0;
633
634 return 1;
635}
636
Willy Tarreaudec98142012-06-06 23:37:08 +0200637static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900638{
Willy Tarreau306f8302013-07-08 15:53:06 +0200639 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100640 struct appctx *appctx = __objt_appctx(si->end);
641 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900642 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900643 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900644 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200645 long long value;
646 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200647 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200648 void *ptr;
649 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900650
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100651 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900652
653 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100654 appctx->ctx.cli.msg = "Key value expected\n";
655 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900656 return;
657 }
658
Simon Hormanc5b89f62011-06-15 15:18:50 +0900659 switch (px->table.type) {
660 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900661 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100662 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900663 break;
664 case STKTABLE_TYPE_IPV6:
665 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100666 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900667 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900668 case STKTABLE_TYPE_INTEGER:
669 {
670 char *endptr;
671 unsigned long val;
672 errno = 0;
673 val = strtoul(args[4], &endptr, 10);
674 if ((errno == ERANGE && val == ULONG_MAX) ||
675 (errno != 0 && val == 0) || endptr == args[4] ||
676 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100677 appctx->ctx.cli.msg = "Invalid key\n";
678 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900679 return;
680 }
681 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100682 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900683 break;
684 }
685 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900686 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100687 static_table_key->key = args[4];
688 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900689 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900690 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200691 switch (action) {
692 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100693 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 +0200694 break;
695 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100696 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 +0200697 break;
698 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100699 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200700 break;
701 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100702 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900703 return;
704 }
705
706 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200707 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100708 appctx->ctx.cli.msg = stats_permission_denied_msg;
709 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900710 return;
711 }
712
Willy Tarreau07115412012-10-29 21:56:59 +0100713 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900714
Willy Tarreaudec98142012-06-06 23:37:08 +0200715 switch (action) {
716 case STAT_CLI_O_TAB:
717 if (!ts)
718 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100719 chunk_reset(&trash);
720 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900721 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100722 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900723 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200724
725 case STAT_CLI_O_CLR:
726 if (!ts)
727 return;
728 if (ts->ref_cnt) {
729 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100730 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
731 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200732 return;
733 }
734 stksess_kill(&px->table, ts);
735 break;
Simon Horman17bce342011-06-15 15:18:47 +0900736
Willy Tarreau654694e2012-06-07 01:03:16 +0200737 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200738 if (ts)
739 stktable_touch(&px->table, ts, 1);
740 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100741 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200742 if (!ts) {
743 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100744 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
745 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200746 return;
747 }
748 stktable_store(&px->table, ts, 1);
749 }
750
Willy Tarreau47060b62013-08-01 21:11:42 +0200751 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
752 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100753 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
754 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200755 return;
756 }
757
758 data_type = stktable_get_data_type(args[cur_arg] + 5);
759 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100760 appctx->ctx.cli.msg = "Unknown data type\n";
761 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200762 return;
763 }
764
765 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100766 appctx->ctx.cli.msg = "Data type not stored in this table\n";
767 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200768 return;
769 }
770
771 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100772 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
773 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200774 return;
775 }
776
777 ptr = stktable_data_ptr(&px->table, ts, data_type);
778
779 switch (stktable_data_types[data_type].std_type) {
780 case STD_T_SINT:
781 stktable_data_cast(ptr, std_t_sint) = value;
782 break;
783 case STD_T_UINT:
784 stktable_data_cast(ptr, std_t_uint) = value;
785 break;
786 case STD_T_ULL:
787 stktable_data_cast(ptr, std_t_ull) = value;
788 break;
789 case STD_T_FRQP:
790 /* We set both the current and previous values. That way
791 * the reported frequency is stable during all the period
792 * then slowly fades out. This allows external tools to
793 * push measures without having to update them too often.
794 */
795 frqp = &stktable_data_cast(ptr, std_t_frqp);
796 frqp->curr_tick = now_ms;
797 frqp->prev_ctr = 0;
798 frqp->curr_ctr = value;
799 break;
800 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200801 }
802 break;
803
Willy Tarreaudec98142012-06-06 23:37:08 +0200804 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100805 appctx->ctx.cli.msg = "Unknown action\n";
806 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200807 break;
Simon Horman121f3052011-06-15 15:18:46 +0900808 }
Simon Horman121f3052011-06-15 15:18:46 +0900809}
810
Willy Tarreau654694e2012-06-07 01:03:16 +0200811static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900812{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100813 struct appctx *appctx = __objt_appctx(si->end);
814
Willy Tarreau04b3a192013-04-13 09:41:37 +0200815 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100816 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
817 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200818 return;
819 }
820
Simon Hormand5b9fd92011-06-15 15:18:48 +0900821 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100822 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
823 if (appctx->ctx.table.data_type < 0) {
824 appctx->ctx.cli.msg = "Unknown data type\n";
825 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900826 return;
827 }
828
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100829 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
830 appctx->ctx.cli.msg = "Data type not stored in this table\n";
831 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900832 return;
833 }
834
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100835 appctx->ctx.table.data_op = get_std_op(args[4]);
836 if (appctx->ctx.table.data_op < 0) {
837 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
838 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900839 return;
840 }
841
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100842 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
843 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
844 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900845 return;
846 }
847}
848
Willy Tarreaudec98142012-06-06 23:37:08 +0200849static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900850{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100851 struct appctx *appctx = __objt_appctx(si->end);
852
853 appctx->ctx.table.data_type = -1;
854 appctx->st2 = STAT_ST_INIT;
855 appctx->ctx.table.target = NULL;
856 appctx->ctx.table.proxy = NULL;
857 appctx->ctx.table.entry = NULL;
858 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900859
860 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100861 appctx->ctx.table.target = find_stktable(args[2]);
862 if (!appctx->ctx.table.target) {
863 appctx->ctx.cli.msg = "No such table\n";
864 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900865 return;
866 }
867 }
868 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200869 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900870 goto err_args;
871 return;
872 }
873
874 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200875 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900876 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200877 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900878 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900879 goto err_args;
880
881 return;
882
883err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200884 switch (action) {
885 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100886 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 +0200887 break;
888 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100889 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 +0200890 break;
891 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100892 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200893 break;
894 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100895 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900896}
897
Willy Tarreau532a4502011-09-07 22:37:44 +0200898/* Expects to find a frontend named <arg> and returns it, otherwise displays various
899 * adequate error messages and returns NULL. This function also expects the session
900 * level to be admin.
901 */
902static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
903{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100904 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200905 struct proxy *px;
906
Willy Tarreau290e63a2012-09-20 18:07:14 +0200907 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100908 appctx->ctx.cli.msg = stats_permission_denied_msg;
909 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200910 return NULL;
911 }
912
913 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100914 appctx->ctx.cli.msg = "A frontend name is expected.\n";
915 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200916 return NULL;
917 }
918
919 px = findproxy(arg, PR_CAP_FE);
920 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100921 appctx->ctx.cli.msg = "No such frontend.\n";
922 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200923 return NULL;
924 }
925 return px;
926}
927
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200928/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
929 * and returns the pointer to the server. Otherwise, display adequate error messages
930 * and returns NULL. This function also expects the session level to be admin. Note:
931 * the <arg> is modified to remove the '/'.
932 */
933static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
934{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100935 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200936 struct proxy *px;
937 struct server *sv;
938 char *line;
939
Willy Tarreau290e63a2012-09-20 18:07:14 +0200940 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100941 appctx->ctx.cli.msg = stats_permission_denied_msg;
942 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200943 return NULL;
944 }
945
946 /* split "backend/server" and make <line> point to server */
947 for (line = arg; *line; line++)
948 if (*line == '/') {
949 *line++ = '\0';
950 break;
951 }
952
953 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100954 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
955 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200956 return NULL;
957 }
958
959 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100960 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
961 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200962 return NULL;
963 }
964
965 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100966 appctx->ctx.cli.msg = "Proxy is disabled.\n";
967 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200968 return NULL;
969 }
970
971 return sv;
972}
973
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100974/* This function is used with map and acl management. It permits to browse
975 * each reference. The variable <getnext> must contain the current node,
976 * <end> point to the root node and the <flags> permit to filter required
977 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100978 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100979static inline
980struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
981 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100982{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100983 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100984
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100985 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100986
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100987 /* Get next list entry. */
988 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100989
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100990 /* If the entry is the last of the list, return NULL. */
991 if (&ref->list == end)
992 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100993
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100994 /* If the entry match the flag, return it. */
995 if (ref->flags & flags)
996 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100997 }
998}
999
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001000static inline
1001struct pat_ref *pat_ref_lookup_ref(const char *reference)
1002{
1003 int id;
1004 char *error;
1005
1006 /* If the reference starts by a '#', this is numeric id. */
1007 if (reference[0] == '#') {
1008 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1009 id = strtol(reference + 1, &error, 10);
1010 if (*error != '\0')
1011 return NULL;
1012
1013 /* Perform the unique id lookup. */
1014 return pat_ref_lookupid(id);
1015 }
1016
1017 /* Perform the string lookup. */
1018 return pat_ref_lookup(reference);
1019}
1020
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001021/* This function is used with map and acl management. It permits to browse
1022 * each reference.
1023 */
1024static inline
1025struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1026{
1027 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001028 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1029 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001030 return NULL;
1031 return expr;
1032}
1033
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001034/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001035 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001036 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001037 * designating the function which will have to process the request, which can
1038 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001039 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001040static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001041{
Willy Tarreau306f8302013-07-08 15:53:06 +02001042 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001043 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001044 char *args[MAX_STATS_ARGS + 1];
1045 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001046 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001047
1048 while (isspace((unsigned char)*line))
1049 line++;
1050
1051 arg = 0;
1052 args[arg] = line;
1053
1054 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001055 if (*line == '\\') {
1056 line++;
1057 if (*line == '\0')
1058 break;
1059 }
1060 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001061 *line++ = '\0';
1062
1063 while (isspace((unsigned char)*line))
1064 line++;
1065
1066 args[++arg] = line;
1067 continue;
1068 }
1069
1070 line++;
1071 }
1072
1073 while (++arg <= MAX_STATS_ARGS)
1074 args[arg] = line;
1075
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001076 /* remove \ */
1077 arg = 0;
1078 while (*args[arg] != '\0') {
1079 j = 0;
1080 for (i=0; args[arg][i] != '\0'; i++) {
1081 if (args[arg][i] == '\\')
1082 continue;
1083 args[arg][j] = args[arg][i];
1084 j++;
1085 }
1086 args[arg][j] = '\0';
1087 arg++;
1088 }
1089
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001090 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001091 if (strcmp(args[0], "show") == 0) {
1092 if (strcmp(args[1], "stat") == 0) {
1093 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001094 appctx->ctx.stats.flags |= STAT_BOUND;
1095 appctx->ctx.stats.iid = atoi(args[2]);
1096 appctx->ctx.stats.type = atoi(args[3]);
1097 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001098 }
1099
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001100 appctx->st2 = STAT_ST_INIT;
1101 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001102 }
1103 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001104 appctx->st2 = STAT_ST_INIT;
1105 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001106 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001107 else if (strcmp(args[1], "pools") == 0) {
1108 appctx->st2 = STAT_ST_INIT;
1109 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1110 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001111 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001112 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001113 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001114 appctx->ctx.cli.msg = stats_permission_denied_msg;
1115 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001116 return 1;
1117 }
Willy Tarreau76153662012-11-26 01:16:39 +01001118 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001119 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001120 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001121 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001122 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001123 appctx->ctx.sess.target = NULL;
1124 appctx->ctx.sess.section = 0; /* start with session status */
1125 appctx->ctx.sess.pos = 0;
1126 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001127 }
1128 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001129 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001130 appctx->ctx.cli.msg = stats_permission_denied_msg;
1131 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001132 return 1;
1133 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001134 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001135 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001136 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001137 appctx->ctx.errors.iid = -1;
1138 appctx->ctx.errors.px = NULL;
1139 appctx->st2 = STAT_ST_INIT;
1140 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001141 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001142 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001143 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001144 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001145 else if (strcmp(args[1], "map") == 0 ||
1146 strcmp(args[1], "acl") == 0) {
1147
1148 /* Set ACL or MAP flags. */
1149 if (args[1][0] == 'm')
1150 appctx->ctx.map.display_flags = PAT_REF_MAP;
1151 else
1152 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001153
1154 /* no parameter: display all map avalaible */
1155 if (!*args[2]) {
1156 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001157 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001158 return 1;
1159 }
1160
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001161 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001162 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001163 if (!appctx->ctx.map.ref ||
1164 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1165 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001166 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001167 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001168 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001169 appctx->st0 = STAT_CLI_PRINT;
1170 return 1;
1171 }
1172 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001173 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001174 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001175 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001176 return 0;
1177 }
1178 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001179 else if (strcmp(args[0], "clear") == 0) {
1180 if (strcmp(args[1], "counters") == 0) {
1181 struct proxy *px;
1182 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001183 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001184 int clrall = 0;
1185
1186 if (strcmp(args[2], "all") == 0)
1187 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001188
Willy Tarreau6162db22009-10-10 17:13:00 +02001189 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001190 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1191 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001192 appctx->ctx.cli.msg = stats_permission_denied_msg;
1193 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001194 return 1;
1195 }
1196
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001197 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001198 if (clrall) {
1199 memset(&px->be_counters, 0, sizeof(px->be_counters));
1200 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1201 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001202 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001203 px->be_counters.conn_max = 0;
1204 px->be_counters.p.http.rps_max = 0;
1205 px->be_counters.sps_max = 0;
1206 px->be_counters.cps_max = 0;
1207 px->be_counters.nbpend_max = 0;
1208
1209 px->fe_counters.conn_max = 0;
1210 px->fe_counters.p.http.rps_max = 0;
1211 px->fe_counters.sps_max = 0;
1212 px->fe_counters.cps_max = 0;
1213 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001214 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001215
1216 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001217 if (clrall)
1218 memset(&sv->counters, 0, sizeof(sv->counters));
1219 else {
1220 sv->counters.cur_sess_max = 0;
1221 sv->counters.nbpend_max = 0;
1222 sv->counters.sps_max = 0;
1223 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001224
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001225 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001226 if (li->counters) {
1227 if (clrall)
1228 memset(li->counters, 0, sizeof(*li->counters));
1229 else
1230 li->counters->conn_max = 0;
1231 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001232 }
1233
Willy Tarreau81c25d02011-09-07 15:17:21 +02001234 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001235 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001236 return 1;
1237 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001238 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001239 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001240 /* end of processing */
1241 return 1;
1242 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001243 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1244 /* Set ACL or MAP flags. */
1245 if (args[1][0] == 'm')
1246 appctx->ctx.map.display_flags = PAT_REF_MAP;
1247 else
1248 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001249
1250 /* no parameter */
1251 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001252 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1253 appctx->ctx.cli.msg = "Missing map identifier.\n";
1254 else
1255 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001256 appctx->st0 = STAT_CLI_PRINT;
1257 return 1;
1258 }
1259
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001260 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001261 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001262 if (!appctx->ctx.map.ref ||
1263 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1264 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001265 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001266 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001267 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001268 appctx->st0 = STAT_CLI_PRINT;
1269 return 1;
1270 }
1271
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001272 /* Clear all. */
1273 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001274
1275 /* return response */
1276 appctx->ctx.cli.msg = "Done.\n";
1277 appctx->st0 = STAT_CLI_PRINT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001278 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001279 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001280 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001281 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001282 return 0;
1283 }
1284 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001285 else if (strcmp(args[0], "get") == 0) {
1286 if (strcmp(args[1], "weight") == 0) {
1287 struct proxy *px;
1288 struct server *sv;
1289
1290 /* split "backend/server" and make <line> point to server */
1291 for (line = args[2]; *line; line++)
1292 if (*line == '/') {
1293 *line++ = '\0';
1294 break;
1295 }
1296
1297 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001298 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1299 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001300 return 1;
1301 }
1302
1303 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001304 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1305 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001306 return 1;
1307 }
1308
1309 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001310 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1311 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001312 return 1;
1313 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001314 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1315 /* Set flags. */
1316 if (args[1][0] == 'm')
1317 appctx->ctx.map.display_flags = PAT_REF_MAP;
1318 else
1319 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001320
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001321 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001322 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001323 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1324 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1325 else
1326 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001327 appctx->st0 = STAT_CLI_PRINT;
1328 return 1;
1329 }
1330
1331 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001332 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001333 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001334 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001335 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001336 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001337 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001338 appctx->st0 = STAT_CLI_PRINT;
1339 return 1;
1340 }
1341
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001342 /* copy input string. The string must be allocated because
1343 * it may be used over multiple iterations. It's released
1344 * at the end and upon abort anyway.
1345 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001346 appctx->ctx.map.chunk.len = strlen(args[3]);
1347 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1348 appctx->ctx.map.chunk.str = strdup(args[3]);
1349 if (!appctx->ctx.map.chunk.str) {
1350 appctx->ctx.cli.msg = "Out of memory error.\n";
1351 appctx->st0 = STAT_CLI_PRINT;
1352 return 1;
1353 }
1354
1355 /* prepare response */
1356 appctx->st2 = STAT_ST_INIT;
1357 appctx->st0 = STAT_CLI_O_MLOOK;
1358 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001359 else { /* not "get weight" */
1360 return 0;
1361 }
1362 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001363 else if (strcmp(args[0], "set") == 0) {
1364 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001365 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001366 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001367
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001368 sv = expect_server_admin(s, si, args[2]);
1369 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001370 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001371
Simon Horman7d09b9a2013-02-12 10:45:51 +09001372 warning = server_parse_weight_change_request(sv, args[3]);
1373 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001374 appctx->ctx.cli.msg = warning;
1375 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001376 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001377 return 1;
1378 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001379 else if (strcmp(args[1], "server") == 0) {
1380 struct server *sv;
1381 const char *warning;
1382
1383 sv = expect_server_admin(s, si, args[2]);
1384 if (!sv)
1385 return 1;
1386
1387 if (strcmp(args[3], "weight") == 0) {
1388 warning = server_parse_weight_change_request(sv, args[4]);
1389 if (warning) {
1390 appctx->ctx.cli.msg = warning;
1391 appctx->st0 = STAT_CLI_PRINT;
1392 }
1393 }
1394 else if (strcmp(args[3], "state") == 0) {
1395 if (strcmp(args[4], "ready") == 0)
1396 srv_adm_set_ready(sv);
1397 else if (strcmp(args[4], "drain") == 0)
1398 srv_adm_set_drain(sv);
1399 else if (strcmp(args[4], "maint") == 0)
1400 srv_adm_set_maint(sv);
1401 else {
1402 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1403 appctx->st0 = STAT_CLI_PRINT;
1404 }
1405 }
1406 else if (strcmp(args[3], "health") == 0) {
1407 if (sv->track) {
1408 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1409 appctx->st0 = STAT_CLI_PRINT;
1410 }
1411 else if (strcmp(args[4], "up") == 0) {
1412 sv->check.health = sv->check.rise + sv->check.fall - 1;
1413 srv_set_running(sv, "changed from CLI");
1414 }
1415 else if (strcmp(args[4], "stopping") == 0) {
1416 sv->check.health = sv->check.rise + sv->check.fall - 1;
1417 srv_set_stopping(sv, "changed from CLI");
1418 }
1419 else if (strcmp(args[4], "down") == 0) {
1420 sv->check.health = 0;
1421 srv_set_stopped(sv, "changed from CLI");
1422 }
1423 else {
1424 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1425 appctx->st0 = STAT_CLI_PRINT;
1426 }
1427 }
1428 else if (strcmp(args[3], "agent") == 0) {
1429 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1430 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1431 appctx->st0 = STAT_CLI_PRINT;
1432 }
1433 else if (strcmp(args[4], "up") == 0) {
1434 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1435 srv_set_running(sv, "changed from CLI");
1436 }
1437 else if (strcmp(args[4], "down") == 0) {
1438 sv->agent.health = 0;
1439 srv_set_stopped(sv, "changed from CLI");
1440 }
1441 else {
1442 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1443 appctx->st0 = STAT_CLI_PRINT;
1444 }
1445 }
1446 else {
1447 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state' and 'weight'.\n";
1448 appctx->st0 = STAT_CLI_PRINT;
1449 }
1450 return 1;
1451 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001452 else if (strcmp(args[1], "timeout") == 0) {
1453 if (strcmp(args[2], "cli") == 0) {
1454 unsigned timeout;
1455 const char *res;
1456
1457 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001458 appctx->ctx.cli.msg = "Expects an integer value.\n";
1459 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001460 return 1;
1461 }
1462
1463 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1464 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001465 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1466 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001467 return 1;
1468 }
1469
1470 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1471 return 1;
1472 }
1473 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001474 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1475 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001476 return 1;
1477 }
1478 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001479 else if (strcmp(args[1], "maxconn") == 0) {
1480 if (strcmp(args[2], "frontend") == 0) {
1481 struct proxy *px;
1482 struct listener *l;
1483 int v;
1484
Willy Tarreau532a4502011-09-07 22:37:44 +02001485 px = expect_frontend_admin(s, si, args[3]);
1486 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001487 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001488
1489 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001490 appctx->ctx.cli.msg = "Integer value expected.\n";
1491 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001492 return 1;
1493 }
1494
1495 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001496 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001497 appctx->ctx.cli.msg = "Value out of range.\n";
1498 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001499 return 1;
1500 }
1501
1502 /* OK, the value is fine, so we assign it to the proxy and to all of
1503 * its listeners. The blocked ones will be dequeued.
1504 */
1505 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001506 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001507 l->maxconn = v;
1508 if (l->state == LI_FULL)
1509 resume_listener(l);
1510 }
1511
1512 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1513 dequeue_all_listeners(&s->fe->listener_queue);
1514
1515 return 1;
1516 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001517 else if (strcmp(args[2], "global") == 0) {
1518 int v;
1519
Willy Tarreau290e63a2012-09-20 18:07:14 +02001520 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001521 appctx->ctx.cli.msg = stats_permission_denied_msg;
1522 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001523 return 1;
1524 }
1525
1526 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001527 appctx->ctx.cli.msg = "Expects an integer value.\n";
1528 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001529 return 1;
1530 }
1531
1532 v = atoi(args[3]);
1533 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001534 appctx->ctx.cli.msg = "Value out of range.\n";
1535 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001536 return 1;
1537 }
1538
1539 /* check for unlimited values */
1540 if (v <= 0)
1541 v = global.hardmaxconn;
1542
1543 global.maxconn = v;
1544
1545 /* Dequeues all of the listeners waiting for a resource */
1546 if (!LIST_ISEMPTY(&global_listener_queue))
1547 dequeue_all_listeners(&global_listener_queue);
1548
1549 return 1;
1550 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001551 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001552 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1553 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001554 return 1;
1555 }
1556 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001557 else if (strcmp(args[1], "rate-limit") == 0) {
1558 if (strcmp(args[2], "connections") == 0) {
1559 if (strcmp(args[3], "global") == 0) {
1560 int v;
1561
Willy Tarreau290e63a2012-09-20 18:07:14 +02001562 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001563 appctx->ctx.cli.msg = stats_permission_denied_msg;
1564 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001565 return 1;
1566 }
1567
1568 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001569 appctx->ctx.cli.msg = "Expects an integer value.\n";
1570 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001571 return 1;
1572 }
1573
1574 v = atoi(args[4]);
1575 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001576 appctx->ctx.cli.msg = "Value out of range.\n";
1577 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001578 return 1;
1579 }
1580
1581 global.cps_lim = v;
1582
1583 /* Dequeues all of the listeners waiting for a resource */
1584 if (!LIST_ISEMPTY(&global_listener_queue))
1585 dequeue_all_listeners(&global_listener_queue);
1586
1587 return 1;
1588 }
1589 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001590 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1591 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001592 return 1;
1593 }
1594 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001595 else if (strcmp(args[2], "sessions") == 0) {
1596 if (strcmp(args[3], "global") == 0) {
1597 int v;
1598
1599 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1600 appctx->ctx.cli.msg = stats_permission_denied_msg;
1601 appctx->st0 = STAT_CLI_PRINT;
1602 return 1;
1603 }
1604
1605 if (!*args[4]) {
1606 appctx->ctx.cli.msg = "Expects an integer value.\n";
1607 appctx->st0 = STAT_CLI_PRINT;
1608 return 1;
1609 }
1610
1611 v = atoi(args[4]);
1612 if (v < 0) {
1613 appctx->ctx.cli.msg = "Value out of range.\n";
1614 appctx->st0 = STAT_CLI_PRINT;
1615 return 1;
1616 }
1617
1618 global.sps_lim = v;
1619
1620 /* Dequeues all of the listeners waiting for a resource */
1621 if (!LIST_ISEMPTY(&global_listener_queue))
1622 dequeue_all_listeners(&global_listener_queue);
1623
1624 return 1;
1625 }
1626 else {
1627 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1628 appctx->st0 = STAT_CLI_PRINT;
1629 return 1;
1630 }
1631 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001632#ifdef USE_OPENSSL
1633 else if (strcmp(args[2], "ssl-sessions") == 0) {
1634 if (strcmp(args[3], "global") == 0) {
1635 int v;
1636
1637 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1638 appctx->ctx.cli.msg = stats_permission_denied_msg;
1639 appctx->st0 = STAT_CLI_PRINT;
1640 return 1;
1641 }
1642
1643 if (!*args[4]) {
1644 appctx->ctx.cli.msg = "Expects an integer value.\n";
1645 appctx->st0 = STAT_CLI_PRINT;
1646 return 1;
1647 }
1648
1649 v = atoi(args[4]);
1650 if (v < 0) {
1651 appctx->ctx.cli.msg = "Value out of range.\n";
1652 appctx->st0 = STAT_CLI_PRINT;
1653 return 1;
1654 }
1655
1656 global.ssl_lim = v;
1657
1658 /* Dequeues all of the listeners waiting for a resource */
1659 if (!LIST_ISEMPTY(&global_listener_queue))
1660 dequeue_all_listeners(&global_listener_queue);
1661
1662 return 1;
1663 }
1664 else {
1665 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1666 appctx->st0 = STAT_CLI_PRINT;
1667 return 1;
1668 }
1669 }
1670#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001671 else if (strcmp(args[2], "http-compression") == 0) {
1672 if (strcmp(args[3], "global") == 0) {
1673 int v;
1674
Willy Tarreau85d47f92012-11-21 00:29:50 +01001675 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001676 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1677 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001678 return 1;
1679 }
1680
William Lallemandd85f9172012-11-09 17:05:39 +01001681 v = atoi(args[4]);
1682 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1683 }
1684 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001685 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1686 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001687 return 1;
1688 }
1689 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001690 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001691 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001692 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001693 return 1;
1694 }
1695 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001696 else if (strcmp(args[1], "table") == 0) {
1697 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1698 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001699 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001700 char *err;
1701
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001702 /* Set flags. */
1703 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001704
1705 /* Expect three parameters: map name, key and new value. */
1706 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001707 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001708 appctx->st0 = STAT_CLI_PRINT;
1709 return 1;
1710 }
1711
1712 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001713 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001714 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001715 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001716 appctx->st0 = STAT_CLI_PRINT;
1717 return 1;
1718 }
1719
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001720 /* If the entry identifier start with a '#', it is considered as
1721 * pointer id
1722 */
1723 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1724 struct pat_ref_elt *ref;
1725 long long int conv;
1726 char *error;
1727
1728 /* Convert argument to integer value. */
1729 conv = strtoll(&args[3][1], &error, 16);
1730 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001731 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001732 appctx->st0 = STAT_CLI_PRINT;
1733 return 1;
1734 }
1735
1736 /* Convert and check integer to pointer. */
1737 ref = (struct pat_ref_elt *)(long)conv;
1738 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001739 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001740 appctx->st0 = STAT_CLI_PRINT;
1741 return 1;
1742 }
1743
1744 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001745 err = NULL;
1746 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1747 if (err)
1748 memprintf(&err, "%s.\n", err);
1749 appctx->ctx.cli.err = err;
1750 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001751 return 1;
1752 }
1753 }
1754 else {
1755 /* Else, use the entry identifier as pattern
1756 * string, and update the value.
1757 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001758 err = NULL;
1759 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1760 if (err)
1761 memprintf(&err, "%s.\n", err);
1762 appctx->ctx.cli.err = err;
1763 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001764 return 1;
1765 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001766 }
1767
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001768 /* The set is done, send message. */
1769 appctx->ctx.cli.msg = "Done.\n";
1770 appctx->st0 = STAT_CLI_PRINT;
1771 return 1;
1772 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001773 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001774 return 0;
1775 }
1776 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001777 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001778 if (strcmp(args[1], "agent") == 0) {
1779 struct server *sv;
1780
1781 sv = expect_server_admin(s, si, args[2]);
1782 if (!sv)
1783 return 1;
1784
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001785 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1786 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1787 appctx->st0 = STAT_CLI_PRINT;
1788 return 1;
1789 }
1790
1791 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001792 return 1;
1793 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001794 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001795 struct server *sv;
1796
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001797 sv = expect_server_admin(s, si, args[2]);
1798 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001799 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001800
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001801 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001802 return 1;
1803 }
1804 else if (strcmp(args[1], "frontend") == 0) {
1805 struct proxy *px;
1806
1807 px = expect_frontend_admin(s, si, args[2]);
1808 if (!px)
1809 return 1;
1810
1811 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001812 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1813 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001814 return 1;
1815 }
1816
1817 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001818 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1819 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001820 return 1;
1821 }
1822
1823 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001824 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1825 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001826 return 1;
1827 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001828 return 1;
1829 }
1830 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001831 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1832 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001833 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001834 }
1835 }
1836 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001837 if (strcmp(args[1], "agent") == 0) {
1838 struct server *sv;
1839
1840 sv = expect_server_admin(s, si, args[2]);
1841 if (!sv)
1842 return 1;
1843
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001844 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001845 return 1;
1846 }
1847 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001848 struct server *sv;
1849
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001850 sv = expect_server_admin(s, si, args[2]);
1851 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001852 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001853
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001854 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02001855 return 1;
1856 }
1857 else if (strcmp(args[1], "frontend") == 0) {
1858 struct proxy *px;
1859
1860 px = expect_frontend_admin(s, si, args[2]);
1861 if (!px)
1862 return 1;
1863
1864 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001865 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1866 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001867 return 1;
1868 }
1869
1870 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001871 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1872 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001873 return 1;
1874 }
1875
1876 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001877 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1878 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001879 return 1;
1880 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001881 return 1;
1882 }
1883 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001884 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1885 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001886 return 1;
1887 }
1888 }
1889 else if (strcmp(args[0], "shutdown") == 0) {
1890 if (strcmp(args[1], "frontend") == 0) {
1891 struct proxy *px;
1892
1893 px = expect_frontend_admin(s, si, args[2]);
1894 if (!px)
1895 return 1;
1896
1897 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001898 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1899 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001900 return 1;
1901 }
1902
1903 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1904 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1905 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1906 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1907 stop_proxy(px);
1908 return 1;
1909 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001910 else if (strcmp(args[1], "session") == 0) {
1911 struct session *sess, *ptr;
1912
Willy Tarreau290e63a2012-09-20 18:07:14 +02001913 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001914 appctx->ctx.cli.msg = stats_permission_denied_msg;
1915 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001916 return 1;
1917 }
1918
1919 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001920 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1921 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001922 return 1;
1923 }
1924
1925 ptr = (void *)strtoul(args[2], NULL, 0);
1926
1927 /* first, look for the requested session in the session table */
1928 list_for_each_entry(sess, &sessions, list) {
1929 if (sess == ptr)
1930 break;
1931 }
1932
1933 /* do we have the session ? */
1934 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001935 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1936 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001937 return 1;
1938 }
1939
1940 session_shutdown(sess, SN_ERR_KILLED);
1941 return 1;
1942 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001943 else if (strcmp(args[1], "sessions") == 0) {
1944 if (strcmp(args[2], "server") == 0) {
1945 struct server *sv;
1946 struct session *sess, *sess_bck;
1947
1948 sv = expect_server_admin(s, si, args[3]);
1949 if (!sv)
1950 return 1;
1951
1952 /* kill all the session that are on this server */
1953 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1954 if (sess->srv_conn == sv)
1955 session_shutdown(sess, SN_ERR_KILLED);
1956
1957 return 1;
1958 }
1959 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001960 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1961 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001962 return 1;
1963 }
1964 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001965 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001966 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1967 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001968 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001969 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001970 }
1971 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001972 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1973 if (args[1][0] == 'm')
1974 appctx->ctx.map.display_flags = PAT_REF_MAP;
1975 else
1976 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001977
1978 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001979 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1980 if (!*args[2] || !*args[3]) {
1981 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
1982 appctx->st0 = STAT_CLI_PRINT;
1983 return 1;
1984 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001985 }
1986
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001987 else {
1988 if (!*args[2] || !*args[3]) {
1989 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
1990 appctx->st0 = STAT_CLI_PRINT;
1991 return 1;
1992 }
1993 }
1994
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001995 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001996 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001997 if (!appctx->ctx.map.ref ||
1998 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001999 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002000 appctx->st0 = STAT_CLI_PRINT;
2001 return 1;
2002 }
2003
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002004 /* If the entry identifier start with a '#', it is considered as
2005 * pointer id
2006 */
2007 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2008 struct pat_ref_elt *ref;
2009 long long int conv;
2010 char *error;
2011
2012 /* Convert argument to integer value. */
2013 conv = strtoll(&args[3][1], &error, 16);
2014 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002015 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002016 appctx->st0 = STAT_CLI_PRINT;
2017 return 1;
2018 }
2019
2020 /* Convert and check integer to pointer. */
2021 ref = (struct pat_ref_elt *)(long)conv;
2022 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002023 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002024 appctx->st0 = STAT_CLI_PRINT;
2025 return 1;
2026 }
2027
2028 /* Try to delete the entry. */
2029 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2030 /* The entry is not found, send message. */
2031 appctx->ctx.cli.msg = "Key not found.\n";
2032 appctx->st0 = STAT_CLI_PRINT;
2033 return 1;
2034 }
2035 }
2036 else {
2037 /* Else, use the entry identifier as pattern
2038 * string and try to delete the entry.
2039 */
2040 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2041 /* The entry is not found, send message. */
2042 appctx->ctx.cli.msg = "Key not found.\n";
2043 appctx->st0 = STAT_CLI_PRINT;
2044 return 1;
2045 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002046 }
2047
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002048 /* The deletion is done, send message. */
2049 appctx->ctx.cli.msg = "Done.\n";
2050 appctx->st0 = STAT_CLI_PRINT;
2051 return 1;
2052 }
2053 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002054 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002055 appctx->st0 = STAT_CLI_PRINT;
2056 return 1;
2057 }
2058 }
2059 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002060 if (strcmp(args[1], "map") == 0 ||
2061 strcmp(args[1], "acl") == 0) {
2062 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002063 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002064
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002065 /* Set flags. */
2066 if (args[1][0] == 'm')
2067 appctx->ctx.map.display_flags = PAT_REF_MAP;
2068 else
2069 appctx->ctx.map.display_flags = PAT_REF_ACL;
2070
2071 /* If the keywork is "map", we expect three parameters, if it
2072 * is "acl", we expect only two parameters
2073 */
2074 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2075 if (!*args[2] || !*args[3] || !*args[4]) {
2076 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2077 appctx->st0 = STAT_CLI_PRINT;
2078 return 1;
2079 }
2080 }
2081 else {
2082 if (!*args[2] || !*args[3]) {
2083 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2084 appctx->st0 = STAT_CLI_PRINT;
2085 return 1;
2086 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002087 }
2088
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002089 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002090 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002091 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002092 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002093 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002094 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002095 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002096 appctx->st0 = STAT_CLI_PRINT;
2097 return 1;
2098 }
2099
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002100 /* The command "add acl" is prohibited if the reference
2101 * use samples.
2102 */
2103 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2104 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2105 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2106 "You must use the command 'add map' to add values.\n";
2107 appctx->st0 = STAT_CLI_PRINT;
2108 return 1;
2109 }
2110
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002111 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002112 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002113 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002114 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002115 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002116 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002117 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002118 if (err)
2119 memprintf(&err, "%s.\n", err);
2120 appctx->ctx.cli.err = err;
2121 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002122 return 1;
2123 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002124
2125 /* The add is done, send message. */
2126 appctx->ctx.cli.msg = "Done.\n";
2127 appctx->st0 = STAT_CLI_PRINT;
2128 return 1;
2129 }
2130 else { /* unknown "del" parameter */
2131 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2132 appctx->st0 = STAT_CLI_PRINT;
2133 return 1;
2134 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002135 }
2136 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002137 return 0;
2138 }
2139 return 1;
2140}
2141
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002142/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002143 * used to processes I/O from/to the stats unix socket. The system relies on a
2144 * state machine handling requests and various responses. We read a request,
2145 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002146 * Then we can read again. The state is stored in appctx->st0 and is one of the
2147 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002148 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002149 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002150static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002151{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002152 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002153 struct channel *req = si->ob;
2154 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002155 int reql;
2156 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002157
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002158 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2159 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002160
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002161 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002162 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002163 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002164 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2165 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002166 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002167 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002168 /* Let's close for real now. We just close the request
2169 * side, the conditions below will complete if needed.
2170 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002171 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002172 break;
2173 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002174 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002175 /* ensure we have some output room left in the event we
2176 * would want to return some info right after parsing.
2177 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002178 if (buffer_almost_full(si->ib->buf)) {
2179 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002180 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002181 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002182
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002183 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002184 if (reql <= 0) { /* closed or EOL not found */
2185 if (reql == 0)
2186 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002187 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002188 continue;
2189 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002190
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002191 /* seek for a possible semi-colon. If we find one, we
2192 * replace it with an LF and skip only this part.
2193 */
2194 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002195 if (trash.str[len] == ';') {
2196 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002197 reql = len + 1;
2198 break;
2199 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002200
Willy Tarreau816fc222009-10-04 07:36:58 +02002201 /* now it is time to check that we have a full line,
2202 * remove the trailing \n and possibly \r, then cut the
2203 * line.
2204 */
2205 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002206 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002207 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002208 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002209 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002210
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002211 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002212 len--;
2213
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002214 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002215
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002216 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002217 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002218 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002219 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002220 continue;
2221 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002222 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002223 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002224 else if (strcmp(trash.str, "help") == 0 ||
2225 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002226 appctx->ctx.cli.msg = stats_sock_usage_msg;
2227 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002228 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002229 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002230 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002231 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002232 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002233 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002234 /* if prompt is disabled, print help on empty lines,
2235 * so that the user at least knows how to enable
2236 * prompt and find help.
2237 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002238 appctx->ctx.cli.msg = stats_sock_usage_msg;
2239 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002240 }
2241
2242 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002243 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002244 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002245 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002246 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002247 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002248 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002249 continue;
2250 }
2251
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002252 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002253 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002254 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2255 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002256 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002257 case STAT_CLI_PRINT_FREE:
2258 if (bi_putstr(si->ib, appctx->ctx.cli.err) != -1) {
2259 free(appctx->ctx.cli.err);
2260 appctx->st0 = STAT_CLI_PROMPT;
2261 }
2262 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002263 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002264 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002265 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002266 break;
2267 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002268 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002269 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002270 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002271 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002272 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002273 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002274 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002275 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002276 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002277 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002278 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002279 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002280 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002281 if (stats_table_request(si, appctx->st0))
2282 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002283 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002284 case STAT_CLI_O_PATS:
2285 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002286 appctx->st0 = STAT_CLI_PROMPT;
2287 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002288 case STAT_CLI_O_PAT:
2289 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002290 appctx->st0 = STAT_CLI_PROMPT;
2291 break;
2292 case STAT_CLI_O_MLOOK:
2293 if (stats_map_lookup(si))
2294 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002295 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002296 case STAT_CLI_O_POOLS:
2297 if (stats_dump_pools_to_buffer(si))
2298 appctx->st0 = STAT_CLI_PROMPT;
2299 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002300 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002301 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002302 break;
2303 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002304
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002305 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002306 if (appctx->st0 == STAT_CLI_PROMPT) {
2307 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2308 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002309 }
2310
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002311 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002312 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002313 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002314
2315 /* Now we close the output if one of the writers did so,
2316 * or if we're not in interactive mode and the request
2317 * buffer is empty. This still allows pipelined requests
2318 * to be sent in non-interactive mode.
2319 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002320 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2321 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002322 continue;
2323 }
2324
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002325 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002326 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002327 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002328 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002329
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002330 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002331 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2332 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002333 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002334 * and nothing more to consume. This is comparable to a broken pipe, so
2335 * we forward the close to the request side so that it flows upstream to
2336 * the client.
2337 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002338 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002339 }
2340
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002341 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002342 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2343 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2344 /* We have no more processing to do, and nothing more to send, and
2345 * the client side has closed. So we'll forward this state downstream
2346 * on the response buffer.
2347 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002348 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002349 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002350 }
2351
2352 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002353 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002354
2355 /* we don't want to expire timeouts while we're processing requests */
2356 si->ib->rex = TICK_ETERNITY;
2357 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002358
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002359 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002360 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 +02002361 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002362 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 +02002363
2364 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2365 /* check that we have released everything then unregister */
2366 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002367 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002368}
2369
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002370/* This function dumps information onto the stream interface's read buffer.
2371 * It returns 0 as long as it does not complete, non-zero upon completion.
2372 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002373 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002374static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002375{
2376 unsigned int up = (now.tv_sec - start_date.tv_sec);
2377
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002378 chunk_printf(&trash,
2379 "Name: " PRODUCT_NAME "\n"
2380 "Version: " HAPROXY_VERSION "\n"
2381 "Release_date: " HAPROXY_DATE "\n"
2382 "Nbproc: %d\n"
2383 "Process_num: %d\n"
2384 "Pid: %d\n"
2385 "Uptime: %dd %dh%02dm%02ds\n"
2386 "Uptime_sec: %d\n"
2387 "Memmax_MB: %d\n"
2388 "Ulimit-n: %d\n"
2389 "Maxsock: %d\n"
2390 "Maxconn: %d\n"
2391 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002392 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002393 "CumConns: %d\n"
2394 "CumReq: %d\n"
2395#ifdef USE_OPENSSL
2396 "MaxSslConns: %d\n"
2397 "CurrSslConns: %d\n"
2398 "CumSslConns: %d\n"
2399#endif
2400 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002401 "PipesUsed: %d\n"
2402 "PipesFree: %d\n"
2403 "ConnRate: %d\n"
2404 "ConnRateLimit: %d\n"
2405 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002406 "SessRate: %d\n"
2407 "SessRateLimit: %d\n"
2408 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002409#ifdef USE_OPENSSL
2410 "SslRate: %d\n"
2411 "SslRateLimit: %d\n"
2412 "MaxSslRate: %d\n"
2413#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002414 "CompressBpsIn: %u\n"
2415 "CompressBpsOut: %u\n"
2416 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002417#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002418 "ZlibMemUsage: %ld\n"
2419 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002420#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002421 "Tasks: %d\n"
2422 "Run_queue: %d\n"
2423 "Idle_pct: %d\n"
2424 "node: %s\n"
2425 "description: %s\n"
2426 "",
2427 global.nbproc,
2428 relative_pid,
2429 pid,
2430 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2431 up,
2432 global.rlimit_memmax,
2433 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002434 global.maxsock, global.maxconn, global.hardmaxconn,
2435 actconn, totalconn, global.req_count,
2436#ifdef USE_OPENSSL
2437 global.maxsslconn, sslconns, totalsslconns,
2438#endif
2439 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002440 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002441 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002442#ifdef USE_OPENSSL
2443 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2444#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002445 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2446 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002447#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002448 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002449#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002450 nb_tasks_cur, run_queue_cur, idle_pct,
2451 global.node, global.desc ? global.desc : ""
2452 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002453
2454 if (bi_putchk(si->ib, &trash) == -1)
2455 return 0;
2456
2457 return 1;
2458}
2459
Willy Tarreau12833bb2014-01-28 16:49:56 +01002460/* This function dumps memory usage information onto the stream interface's
2461 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2462 * completion. No state is used.
2463 */
2464static int stats_dump_pools_to_buffer(struct stream_interface *si)
2465{
2466 dump_pools_to_trash();
2467 if (bi_putchk(si->ib, &trash) == -1)
2468 return 0;
2469 return 1;
2470}
2471
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002472/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2473 * the state from stream interface <si>. The caller is responsible for clearing
2474 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002475 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002476static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002477{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002478 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002479 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002480
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002481 if (!(px->cap & PR_CAP_FE))
2482 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002483
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002484 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002485 return 0;
2486
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002487 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002488 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002489 /* name, queue */
2490 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002491
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002492 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002493 /* Column sub-heading for Enable or Disable server */
2494 chunk_appendf(&trash, "<td></td>");
2495 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002496
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002497 chunk_appendf(&trash,
2498 "<td class=ac>"
2499 "<a name=\"%s/Frontend\"></a>"
2500 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2501 "<td colspan=3></td>"
2502 "",
2503 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002504
Willy Tarreau466c9b52012-12-23 02:25:03 +01002505 chunk_appendf(&trash,
2506 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002507 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002508 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2509 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2510 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002511 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2512 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2513 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002514
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002515 if (px->mode == PR_MODE_HTTP)
2516 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002517 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002518 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002519
2520 chunk_appendf(&trash,
2521 "</table></div></u></td>"
2522 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002523 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002524 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2525 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2526 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002527 U2H(px->fe_counters.sps_max),
2528 U2H(px->fe_counters.cps_max),
2529 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002530
2531 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002532 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002533 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002534 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002535
2536 chunk_appendf(&trash,
2537 "</table></div></u></td>"
2538 /* sessions rate : limit */
2539 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002540 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002541
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002542 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002543 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002544 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002545 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002546 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2547 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002548 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002549 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2550 U2H(px->fe_counters.cum_sess),
2551 U2H(px->fe_counters.cum_conn),
2552 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002553
Willy Tarreau466c9b52012-12-23 02:25:03 +01002554 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002555 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002556 chunk_appendf(&trash,
2557 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2558 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2559 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2560 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2561 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2562 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2563 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2564 "<tr><th>- other responses:</th><td>%s</td></tr>"
2565 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2566 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002567 U2H(px->fe_counters.p.http.cum_req),
2568 U2H(px->fe_counters.p.http.rsp[1]),
2569 U2H(px->fe_counters.p.http.rsp[2]),
2570 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002571 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002572 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002573 U2H(px->fe_counters.p.http.rsp[3]),
2574 U2H(px->fe_counters.p.http.rsp[4]),
2575 U2H(px->fe_counters.p.http.rsp[5]),
2576 U2H(px->fe_counters.p.http.rsp[0]),
2577 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002578 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002579
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002580 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002581 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002582 /* sessions: lbtot, lastsess */
2583 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002584 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002585 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002586 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002587 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002588
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002589 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002590 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002591 "<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 +01002592 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002593 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002594 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2595 px->fe_counters.comp_in ?
2596 (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 +01002597 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002598
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002599 chunk_appendf(&trash,
2600 /* denied: req, resp */
2601 "<td>%s</td><td>%s</td>"
2602 /* errors : request, connect, response */
2603 "<td>%s</td><td></td><td></td>"
2604 /* warnings: retries, redispatches */
2605 "<td></td><td></td>"
2606 /* server status : reflect frontend status */
2607 "<td class=ac>%s</td>"
2608 /* rest of server: nothing */
2609 "<td class=ac colspan=8></td></tr>"
2610 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002611 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2612 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002613 px->state == PR_STREADY ? "OPEN" :
2614 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002615 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002616 else { /* CSV mode */
2617 chunk_appendf(&trash,
2618 /* pxid, name, queue cur, queue max, */
2619 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002620 /* sessions : current, max, limit, total */
2621 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002622 /* bytes : in, out */
2623 "%lld,%lld,"
2624 /* denied: req, resp */
2625 "%lld,%lld,"
2626 /* errors : request, connect, response */
2627 "%lld,,,"
2628 /* warnings: retries, redispatches */
2629 ",,"
2630 /* server status : reflect frontend status */
2631 "%s,"
2632 /* rest of server: nothing */
2633 ",,,,,,,,"
2634 /* pid, iid, sid, throttle, lbtot, tracked, type */
2635 "%d,%d,0,,,,%d,"
2636 /* rate, rate_lim, rate_max */
2637 "%u,%u,%u,"
2638 /* check_status, check_code, check_duration */
2639 ",,,",
2640 px->id,
2641 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2642 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2643 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2644 px->fe_counters.failed_req,
2645 px->state == PR_STREADY ? "OPEN" :
2646 px->state == PR_STFULL ? "FULL" : "STOP",
2647 relative_pid, px->uuid, STATS_TYPE_FE,
2648 read_freq_ctr(&px->fe_sess_per_sec),
2649 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002650
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002651 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2652 if (px->mode == PR_MODE_HTTP) {
2653 for (i=1; i<6; i++)
2654 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2655 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2656 }
2657 else
2658 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002659
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002660 /* failed health analyses */
2661 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002662
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002663 /* requests : req_rate, req_rate_max, req_tot, */
2664 chunk_appendf(&trash, "%u,%u,%lld,",
2665 read_freq_ctr(&px->fe_req_per_sec),
2666 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2667
2668 /* errors: cli_aborts, srv_aborts */
2669 chunk_appendf(&trash, ",,");
2670
2671 /* compression: in, out, bypassed */
2672 chunk_appendf(&trash, "%lld,%lld,%lld,",
2673 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2674
2675 /* compression: comp_rsp */
2676 chunk_appendf(&trash, "%lld,",
2677 px->fe_counters.p.http.comp_rsp);
2678
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002679 /* lastsess */
2680 chunk_appendf(&trash, ",");
2681
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002682 /* finish with EOL */
2683 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002684 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002685 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002686}
2687
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002688/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2689 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2690 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2691 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002692 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002693static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002694{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002695 struct appctx *appctx = __objt_appctx(si->end);
2696
2697 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002698 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002699 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002700 /* Column sub-heading for Enable or Disable server */
2701 chunk_appendf(&trash, "<td></td>");
2702 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002703 chunk_appendf(&trash,
2704 /* frontend name, listener name */
2705 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2706 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2707 "",
2708 px->id, l->name,
2709 (flags & ST_SHLGNDS)?"<u>":"",
2710 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002711
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002712 if (flags & ST_SHLGNDS) {
2713 char str[INET6_ADDRSTRLEN];
2714 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002715
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002716 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002717
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002718 port = get_host_port(&l->addr);
2719 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2720 case AF_INET:
2721 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2722 break;
2723 case AF_INET6:
2724 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2725 break;
2726 case AF_UNIX:
2727 chunk_appendf(&trash, "unix, ");
2728 break;
2729 case -1:
2730 chunk_appendf(&trash, "(%s), ", strerror(errno));
2731 break;
2732 }
Willy Tarreau91861262007-10-17 17:06:05 +02002733
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002734 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002735 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002736 }
Willy Tarreau91861262007-10-17 17:06:05 +02002737
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002738 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002739 /* queue */
2740 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002741 /* sessions rate: current, max, limit */
2742 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002743 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002744 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002745 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002746 /* bytes: in, out */
2747 "<td>%s</td><td>%s</td>"
2748 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002749 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002750 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2751 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002752
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002753 chunk_appendf(&trash,
2754 /* denied: req, resp */
2755 "<td>%s</td><td>%s</td>"
2756 /* errors: request, connect, response */
2757 "<td>%s</td><td></td><td></td>"
2758 /* warnings: retries, redispatches */
2759 "<td></td><td></td>"
2760 /* server status: reflect listener status */
2761 "<td class=ac>%s</td>"
2762 /* rest of server: nothing */
2763 "<td class=ac colspan=8></td></tr>"
2764 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002765 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2766 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002767 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2768 }
2769 else { /* CSV mode */
2770 chunk_appendf(&trash,
2771 /* pxid, name, queue cur, queue max, */
2772 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002773 /* sessions: current, max, limit, total */
2774 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002775 /* bytes: in, out */
2776 "%lld,%lld,"
2777 /* denied: req, resp */
2778 "%lld,%lld,"
2779 /* errors: request, connect, response */
2780 "%lld,,,"
2781 /* warnings: retries, redispatches */
2782 ",,"
2783 /* server status: reflect listener status */
2784 "%s,"
2785 /* rest of server: nothing */
2786 ",,,,,,,,"
2787 /* pid, iid, sid, throttle, lbtot, tracked, type */
2788 "%d,%d,%d,,,,%d,"
2789 /* rate, rate_lim, rate_max */
2790 ",,,"
2791 /* check_status, check_code, check_duration */
2792 ",,,"
2793 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2794 ",,,,,,"
2795 /* failed health analyses */
2796 ","
2797 /* requests : req_rate, req_rate_max, req_tot, */
2798 ",,,"
2799 /* errors: cli_aborts, srv_aborts */
2800 ",,"
2801 /* compression: in, out, bypassed, comp_rsp */
2802 ",,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002803 /* lastsess */
2804 ","
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002805 "\n",
2806 px->id, l->name,
2807 l->nbconn, l->counters->conn_max,
2808 l->maxconn, l->counters->cum_conn,
2809 l->counters->bytes_in, l->counters->bytes_out,
2810 l->counters->denied_req, l->counters->denied_resp,
2811 l->counters->failed_req,
2812 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2813 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2814 }
2815 return 1;
2816}
Willy Tarreau91861262007-10-17 17:06:05 +02002817
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002818/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2819 * from stream interface <si>, stats flags <flags>, and server state <state>.
2820 * The caller is responsible for clearing the trash if needed. Returns non-zero
2821 * if it emits anything, zero otherwise. The <state> parameter can take the
Willy Tarreau9638efa2014-05-23 11:19:57 +02002822 * following values : 0=DOWN, 1=DOWN(agent) 2=going up, 3=going down, 4=UP, 5,6=NOLB,
2823 * 7,8=DRAIN, 9=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002824 */
2825static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2826{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002827 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002828 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002829 char str[INET6_ADDRSTRLEN];
2830 struct chunk src;
2831 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002832
Willy Tarreau32091232014-05-16 13:52:00 +02002833 /* we have "via" which is the tracked server as described in the configuration,
2834 * and "ref" which is the checked server and the end of the chain.
2835 */
2836 via = sv->track ? sv->track : sv;
2837 ref = via;
2838 while (ref->track)
2839 ref = ref->track;
2840
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002841 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02002842 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002843 "DOWN",
Willy Tarreau9638efa2014-05-23 11:19:57 +02002844 "DOWN (agent)",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002845 "DN %d/%d &uarr;",
2846 "UP %d/%d &darr;",
2847 "UP",
2848 "NOLB %d/%d &darr;",
2849 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002850 "DRAIN %d/%d &darr;",
2851 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002852 "<i>no check</i>"
2853 };
Willy Tarreau91861262007-10-17 17:06:05 +02002854
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002855 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002856 chunk_appendf(&trash, "<tr class=\"maintain\">");
2857 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002858 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002859 "<tr class=\"%s%d\">",
Willy Tarreauc93cd162014-05-13 15:54:22 +02002860 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002861
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002862 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002863 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002864 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2865 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002866
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002867 chunk_appendf(&trash,
2868 "<td class=ac><a name=\"%s/%s\"></a>%s"
2869 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2870 "",
2871 px->id, sv->id,
2872 (flags & ST_SHLGNDS) ? "<u>" : "",
2873 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002874
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002875 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002876 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002877
2878 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2879 case AF_INET:
2880 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2881 break;
2882 case AF_INET6:
2883 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2884 break;
2885 case AF_UNIX:
2886 chunk_appendf(&trash, "unix, ");
2887 break;
2888 case -1:
2889 chunk_appendf(&trash, "(%s), ", strerror(errno));
2890 break;
2891 default: /* address family not supported */
2892 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002893 }
Willy Tarreau91861262007-10-17 17:06:05 +02002894
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002895 /* id */
2896 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002897
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002898 /* cookie */
2899 if (sv->cookie) {
2900 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002901
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002902 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2903 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002904
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002905 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002906 }
2907
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002908 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002909 }
Willy Tarreau91861262007-10-17 17:06:05 +02002910
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002911 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002912 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002913 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002914 /* sessions rate : current, max, limit */
2915 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002916 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002917 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002918 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2919 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002920
Willy Tarreau466c9b52012-12-23 02:25:03 +01002921
2922 chunk_appendf(&trash,
2923 /* sessions: current, max, limit, total */
2924 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002925 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002926 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2927 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002928 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2929 U2H(sv->counters.cum_sess),
2930 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002931
Willy Tarreau466c9b52012-12-23 02:25:03 +01002932 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2933 if (px->mode == PR_MODE_HTTP) {
2934 unsigned long long tot;
2935 for (tot = i = 0; i < 6; i++)
2936 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002937
Willy Tarreau466c9b52012-12-23 02:25:03 +01002938 chunk_appendf(&trash,
2939 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2940 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2941 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2942 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2943 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2944 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2945 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2946 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002947 U2H(tot),
2948 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2949 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2950 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2951 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2952 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2953 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 +02002954 }
Willy Tarreau91861262007-10-17 17:06:05 +02002955
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002956 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002957 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002958 /* sessions: lbtot, last */
2959 "<td>%s</td><td>%s</td>",
2960 U2H(sv->counters.cum_lbconn),
2961 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02002962
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002963 chunk_appendf(&trash,
2964 /* bytes : in, out */
2965 "<td>%s</td><td>%s</td>"
2966 /* denied: req, resp */
2967 "<td></td><td>%s</td>"
2968 /* errors : request, connect */
2969 "<td></td><td>%s</td>"
2970 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002971 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002972 /* warnings: retries, redispatches */
2973 "<td>%lld</td><td>%lld</td>"
2974 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002975 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2976 U2H(sv->counters.failed_secu),
2977 U2H(sv->counters.failed_conns),
2978 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002979 sv->counters.cli_aborts,
2980 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002981 sv->counters.retries, sv->counters.redispatches);
2982
2983 /* status, lest check */
2984 chunk_appendf(&trash, "<td class=ac>");
2985
Willy Tarreau20125212014-05-13 19:44:56 +02002986 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002987 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2988 chunk_appendf(&trash, "MAINT");
2989 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002990 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002991 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2992 chunk_appendf(&trash,
2993 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02002994 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2995 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002996 }
Willy Tarreau91861262007-10-17 17:06:05 +02002997
Willy Tarreauff5ae352013-12-11 20:36:34 +01002998 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002999 chunk_appendf(&trash,
3000 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003001 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003002 get_check_status_info(sv->check.status));
3003
3004 if (sv->check.status >= HCHK_STATUS_L57DATA)
3005 chunk_appendf(&trash, "/%d", sv->check.code);
3006
3007 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003008 chunk_appendf(&trash, " in %lums", sv->check.duration);
3009
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003010 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003011 get_check_status_description(sv->check.status));
3012 if (*sv->check.desc) {
3013 chunk_appendf(&trash, ": ");
3014 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3015 chunk_htmlencode(&trash, &src);
3016 }
3017 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003018 }
3019 else
3020 chunk_appendf(&trash, "</td><td>");
3021
3022 chunk_appendf(&trash,
3023 /* weight */
3024 "</td><td class=ac>%d</td>"
3025 /* act, bck */
3026 "<td class=ac>%s</td><td class=ac>%s</td>"
3027 "",
3028 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003029 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3030 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003031
3032 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003033 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003034 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003035
3036 if (ref->observe)
3037 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3038
3039 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003040 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003041 "<td>%lld</td><td>%s</td>"
3042 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003043 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003044 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3045 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003046 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3047 /* tracking a server */
3048 chunk_appendf(&trash,
3049 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003050 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003051 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003052 else
3053 chunk_appendf(&trash, "<td colspan=3></td>");
3054
3055 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003056 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003057 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003058 else
3059 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3060 }
3061 else { /* CSV mode */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003062 static char *srv_hlt_st[10] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003063 "DOWN,",
Willy Tarreau9638efa2014-05-23 11:19:57 +02003064 "DOWN (agent),",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003065 "DOWN %d/%d,",
3066 "UP %d/%d,",
3067 "UP,",
3068 "NOLB %d/%d,",
3069 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09003070 "DRAIN %d/%d,",
3071 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003072 "no check,"
3073 };
3074
3075 chunk_appendf(&trash,
3076 /* pxid, name */
3077 "%s,%s,"
3078 /* queue : current, max */
3079 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003080 /* sessions : current, max, limit, total */
3081 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003082 /* bytes : in, out */
3083 "%lld,%lld,"
3084 /* denied: req, resp */
3085 ",%lld,"
3086 /* errors : request, connect, response */
3087 ",%lld,%lld,"
3088 /* warnings: retries, redispatches */
3089 "%lld,%lld,"
3090 "",
3091 px->id, sv->id,
3092 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003093 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003094 sv->counters.bytes_in, sv->counters.bytes_out,
3095 sv->counters.failed_secu,
3096 sv->counters.failed_conns, sv->counters.failed_resp,
3097 sv->counters.retries, sv->counters.redispatches);
3098
3099 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003100 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003101 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003102 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003103 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003104 else
3105 chunk_appendf(&trash,
3106 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003107 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3108 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003109
3110 chunk_appendf(&trash,
3111 /* weight, active, backup */
3112 "%d,%d,%d,"
3113 "",
3114 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003115 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3116 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003117
3118 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003119 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003120 chunk_appendf(&trash,
3121 "%lld,%lld,%d,%d,",
3122 sv->counters.failed_checks, sv->counters.down_trans,
3123 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3124 else
3125 chunk_appendf(&trash, ",,,,");
3126
3127 /* queue limit, pid, iid, sid, */
3128 chunk_appendf(&trash,
3129 "%s,"
3130 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003131 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003132 relative_pid, px->uuid, sv->puid);
3133
3134 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003135 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003136 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003137
3138 /* sessions: lbtot */
3139 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3140
3141 /* tracked */
3142 if (sv->track)
3143 chunk_appendf(&trash, "%s/%s,",
3144 sv->track->proxy->id, sv->track->id);
3145 else
3146 chunk_appendf(&trash, ",");
3147
3148 /* type */
3149 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3150
3151 /* rate */
3152 chunk_appendf(&trash, "%u,,%u,",
3153 read_freq_ctr(&sv->sess_per_sec),
3154 sv->counters.sps_max);
3155
Willy Tarreauff5ae352013-12-11 20:36:34 +01003156 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003157 /* check_status */
3158 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3159
3160 /* check_code */
3161 if (sv->check.status >= HCHK_STATUS_L57DATA)
3162 chunk_appendf(&trash, "%u,", sv->check.code);
3163 else
3164 chunk_appendf(&trash, ",");
3165
3166 /* check_duration */
3167 if (sv->check.status >= HCHK_STATUS_CHECKED)
3168 chunk_appendf(&trash, "%lu,", sv->check.duration);
3169 else
3170 chunk_appendf(&trash, ",");
3171
3172 }
3173 else
3174 chunk_appendf(&trash, ",,,");
3175
3176 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3177 if (px->mode == PR_MODE_HTTP) {
3178 for (i=1; i<6; i++)
3179 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3180
3181 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3182 }
3183 else
3184 chunk_appendf(&trash, ",,,,,,");
3185
3186 /* failed health analyses */
3187 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3188
3189 /* requests : req_rate, req_rate_max, req_tot, */
3190 chunk_appendf(&trash, ",,,");
3191
3192 /* errors: cli_aborts, srv_aborts */
3193 chunk_appendf(&trash, "%lld,%lld,",
3194 sv->counters.cli_aborts, sv->counters.srv_aborts);
3195
3196 /* compression: in, out, bypassed, comp_rsp */
3197 chunk_appendf(&trash, ",,,,");
3198
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003199 /* lastsess */
3200 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3201
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003202 /* finish with EOL */
3203 chunk_appendf(&trash, "\n");
3204 }
3205 return 1;
3206}
3207
3208/* Dumps a line for backend <px> to the trash for and uses the state from stream
3209 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3210 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3211 */
3212static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3213{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003214 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003215 struct chunk src;
3216 int i;
3217
3218 if (!(px->cap & PR_CAP_BE))
3219 return 0;
3220
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003221 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003222 return 0;
3223
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003224 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003225 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003226 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003227 /* Column sub-heading for Enable or Disable server */
3228 chunk_appendf(&trash, "<td></td>");
3229 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003230 chunk_appendf(&trash,
3231 "<td class=ac>"
3232 /* name */
3233 "%s<a name=\"%s/Backend\"></a>"
3234 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3235 "",
3236 (flags & ST_SHLGNDS)?"<u>":"",
3237 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003238
3239 if (flags & ST_SHLGNDS) {
3240 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003241 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003242 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3243
3244 /* cookie */
3245 if (px->cookie_name) {
3246 chunk_appendf(&trash, ", cookie: '");
3247 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3248 chunk_htmlencode(&trash, &src);
3249 chunk_appendf(&trash, "'");
3250 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003251 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003252 }
3253
3254 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003255 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003256 /* queue : current, max */
3257 "<td>%s</td><td>%s</td><td></td>"
3258 /* sessions rate : current, max, limit */
3259 "<td>%s</td><td>%s</td><td></td>"
3260 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003261 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003262 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3263 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003264
3265 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003266 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003267 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003268 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003269 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003270 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003271 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3272 U2H(px->be_counters.cum_conn),
3273 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003274
Willy Tarreau466c9b52012-12-23 02:25:03 +01003275 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003276 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003277 chunk_appendf(&trash,
3278 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3279 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3280 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3281 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3282 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3283 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3284 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3285 "<tr><th>- other responses:</th><td>%s</td></tr>"
3286 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3287 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003288 U2H(px->be_counters.p.http.cum_req),
3289 U2H(px->be_counters.p.http.rsp[1]),
3290 U2H(px->be_counters.p.http.rsp[2]),
3291 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003292 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003293 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003294 U2H(px->be_counters.p.http.rsp[3]),
3295 U2H(px->be_counters.p.http.rsp[4]),
3296 U2H(px->be_counters.p.http.rsp[5]),
3297 U2H(px->be_counters.p.http.rsp[0]),
3298 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003299 }
3300
3301 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003302 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003303 /* sessions: lbtot, last */
3304 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003305 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003306 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003307 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003308 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003309 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003310 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003311
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003312 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003313 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003314 "<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 +01003315 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003316 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003317 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3318 px->be_counters.comp_in ?
3319 (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 +01003320 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3321
3322 chunk_appendf(&trash,
3323 /* denied: req, resp */
3324 "<td>%s</td><td>%s</td>"
3325 /* errors : request, connect */
3326 "<td></td><td>%s</td>"
3327 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003328 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003329 /* warnings: retries, redispatches */
3330 "<td>%lld</td><td>%lld</td>"
3331 /* backend status: reflect backend status (up/down): we display UP
3332 * if the backend has known working servers or if it has no server at
3333 * all (eg: for stats). Then we display the total weight, number of
3334 * active and backups. */
3335 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3336 "<td class=ac>%d</td><td class=ac>%d</td>"
3337 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003338 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3339 U2H(px->be_counters.failed_conns),
3340 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003341 px->be_counters.cli_aborts,
3342 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003343 px->be_counters.retries, px->be_counters.redispatches,
3344 human_time(now.tv_sec - px->last_change, 1),
3345 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3346 "<font color=\"red\"><b>DOWN</b></font>",
3347 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3348 px->srv_act, px->srv_bck);
3349
3350 chunk_appendf(&trash,
3351 /* rest of backend: nothing, down transitions, total downtime, throttle */
3352 "<td class=ac>&nbsp;</td><td>%d</td>"
3353 "<td>%s</td>"
3354 "<td></td>"
3355 "</tr>",
3356 px->down_trans,
3357 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3358 }
3359 else { /* CSV mode */
3360 chunk_appendf(&trash,
3361 /* pxid, name */
3362 "%s,BACKEND,"
3363 /* queue : current, max */
3364 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003365 /* sessions : current, max, limit, total */
3366 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003367 /* bytes : in, out */
3368 "%lld,%lld,"
3369 /* denied: req, resp */
3370 "%lld,%lld,"
3371 /* errors : request, connect, response */
3372 ",%lld,%lld,"
3373 /* warnings: retries, redispatches */
3374 "%lld,%lld,"
3375 /* backend status: reflect backend status (up/down): we display UP
3376 * if the backend has known working servers or if it has no server at
3377 * all (eg: for stats). Then we display the total weight, number of
3378 * active and backups. */
3379 "%s,"
3380 "%d,%d,%d,"
3381 /* rest of backend: nothing, down transitions, last change, total downtime */
3382 ",%d,%d,%d,,"
3383 /* pid, iid, sid, throttle, lbtot, tracked, type */
3384 "%d,%d,0,,%lld,,%d,"
3385 /* rate, rate_lim, rate_max, */
3386 "%u,,%u,"
3387 /* check_status, check_code, check_duration */
3388 ",,,",
3389 px->id,
3390 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3391 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3392 px->be_counters.bytes_in, px->be_counters.bytes_out,
3393 px->be_counters.denied_req, px->be_counters.denied_resp,
3394 px->be_counters.failed_conns, px->be_counters.failed_resp,
3395 px->be_counters.retries, px->be_counters.redispatches,
3396 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3397 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3398 px->srv_act, px->srv_bck,
3399 px->down_trans, (int)(now.tv_sec - px->last_change),
3400 px->srv?be_downtime(px):0,
3401 relative_pid, px->uuid,
3402 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3403 read_freq_ctr(&px->be_sess_per_sec),
3404 px->be_counters.sps_max);
3405
3406 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3407 if (px->mode == PR_MODE_HTTP) {
3408 for (i=1; i<6; i++)
3409 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3410 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3411 }
3412 else
3413 chunk_appendf(&trash, ",,,,,,");
3414
3415 /* failed health analyses */
3416 chunk_appendf(&trash, ",");
3417
3418 /* requests : req_rate, req_rate_max, req_tot, */
3419 chunk_appendf(&trash, ",,,");
3420
3421 /* errors: cli_aborts, srv_aborts */
3422 chunk_appendf(&trash, "%lld,%lld,",
3423 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3424
3425 /* compression: in, out, bypassed */
3426 chunk_appendf(&trash, "%lld,%lld,%lld,",
3427 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3428
3429 /* compression: comp_rsp */
3430 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3431
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003432 /* lastsess */
3433 chunk_appendf(&trash, "%d,", be_lastsession(px));
3434
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003435 /* finish with EOL */
3436 chunk_appendf(&trash, "\n");
3437 }
3438 return 1;
3439}
3440
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003441/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003442 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003443 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003444 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003445static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003446{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003447 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003448 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3449
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003450 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003451 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003452
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003453 /* 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 +02003454 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003455 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003456 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003457 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3458 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003459 }
3460
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003461 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003462 "<form action=\"%s%s%s%s\" method=\"post\">",
3463 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003464 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3465 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003466 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003467 }
3468
3469 /* print a new table */
3470 chunk_appendf(&trash,
3471 "<table class=\"tbl\" width=\"100%%\">\n"
3472 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003473 "<th class=\"pxname\" width=\"10%%\">");
3474
3475 chunk_appendf(&trash,
3476 "<a name=\"%s\"></a>%s"
3477 "<a class=px href=\"#%s\">%s</a>",
3478 px->id,
3479 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3480 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003481
3482 if (uri->flags & ST_SHLGNDS) {
3483 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003484 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003485 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3486 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003487 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003488 }
3489
3490 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003491 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003492 "<th class=\"%s\" width=\"90%%\">%s</th>"
3493 "</tr>\n"
3494 "</table>\n"
3495 "<table class=\"tbl\" width=\"100%%\">\n"
3496 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003497 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3498 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3499
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003500 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003501 /* Column heading for Enable or Disable server */
3502 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003503 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003504
3505 chunk_appendf(&trash,
3506 "<th rowspan=2></th>"
3507 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003508 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003509 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3510 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3511 "<th colspan=9>Server</th>"
3512 "</tr>\n"
3513 "<tr class=\"titre\">"
3514 "<th>Cur</th><th>Max</th><th>Limit</th>"
3515 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003516 "<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 +01003517 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3518 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3519 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3520 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3521 "<th>Thrtle</th>\n"
3522 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003523}
3524
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003525/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003526 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003527 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003528static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003529{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003530 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003531 chunk_appendf(&trash, "</table>");
3532
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003533 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003534 /* close the form used to enable/disable this proxy servers */
3535 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003536 "Choose the action to perform on the checked servers : "
3537 "<select name=action>"
3538 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02003539 "<option value=\"ready\">Set state to READY</option>"
3540 "<option value=\"drain\">Set state to DRAIN</option>"
3541 "<option value=\"maint\">set state to MAINT</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003542 "<option value=\"shutdown\">Kill Sessions</option>"
3543 "</select>"
3544 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3545 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3546 "</form>",
3547 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003548 }
3549
3550 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003551}
Willy Tarreau91861262007-10-17 17:06:05 +02003552
3553/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003554 * Dumps statistics for a proxy. The output is sent to the stream interface's
3555 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3556 * buffer space, or non-zero if everything completed. This function is used
3557 * both by the CLI and the HTTP entry points, and is able to dump the output
3558 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003559 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003560static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003561{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003562 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003563 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003564 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003565 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003566 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003567
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003568 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003569
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003570 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003571 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003572 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003573 if (uri && uri->scope) {
3574 /* we have a limited scope, we have to check the proxy name */
3575 struct stat_scope *scope;
3576 int len;
3577
3578 len = strlen(px->id);
3579 scope = uri->scope;
3580
3581 while (scope) {
3582 /* match exact proxy name */
3583 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3584 break;
3585
3586 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003587 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003588 break;
3589 scope = scope->next;
3590 }
3591
3592 /* proxy name not found : don't dump anything */
3593 if (scope == NULL)
3594 return 1;
3595 }
3596
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003597 /* if the user has requested a limited output and the proxy
3598 * name does not match, skip it.
3599 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003600 if (appctx->ctx.stats.scope_len &&
3601 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 +02003602 return 1;
3603
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003604 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3605 (appctx->ctx.stats.iid != -1) &&
3606 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003607 return 1;
3608
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003609 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003610 /* fall through */
3611
Willy Tarreau295a8372011-03-10 11:25:07 +01003612 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003613 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003614 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003615 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003616 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003617 }
Willy Tarreau91861262007-10-17 17:06:05 +02003618
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003619 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003620 /* fall through */
3621
Willy Tarreau295a8372011-03-10 11:25:07 +01003622 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003623 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003624 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003625 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003626 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003627
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003628 appctx->ctx.stats.l = px->conf.listeners.n;
3629 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003630 /* fall through */
3631
Willy Tarreau295a8372011-03-10 11:25:07 +01003632 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003633 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003634 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003635 if (buffer_almost_full(rep->buf)) {
3636 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003637 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003638 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003639
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003640 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003641 if (!l->counters)
3642 continue;
3643
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003644 if (appctx->ctx.stats.flags & STAT_BOUND) {
3645 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003646 break;
3647
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003648 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003649 continue;
3650 }
3651
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003652 /* print the frontend */
3653 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3654 if (bi_putchk(rep, &trash) == -1)
3655 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003656 }
3657
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003658 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3659 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003660 /* fall through */
3661
Willy Tarreau295a8372011-03-10 11:25:07 +01003662 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003663 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003664 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau9638efa2014-05-23 11:19:57 +02003665 int sv_state;
Willy Tarreau91861262007-10-17 17:06:05 +02003666
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003667 if (buffer_almost_full(rep->buf)) {
3668 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003669 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003670 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003671
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003672 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003673
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003674 if (appctx->ctx.stats.flags & STAT_BOUND) {
3675 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003676 break;
3677
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003678 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003679 continue;
3680 }
3681
Willy Tarreau32091232014-05-16 13:52:00 +02003682 svs = sv;
3683 while (svs->track)
3684 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003685
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003686 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
3687 /* server is UP. The possibilities are :
Willy Tarreau9638efa2014-05-23 11:19:57 +02003688 * - UP, draining, going down => state = 7
3689 * - UP, going down => state = 3
3690 * - UP, draining => state = 8
3691 * - UP, checked => state = 4
3692 * - UP, not checked nor tracked => state = 9
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003693 */
3694
3695 if ((svs->check.state & CHK_ST_ENABLED) &&
3696 (svs->check.health < svs->check.rise + svs->check.fall - 1))
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003697 sv_state = 3;
Willy Tarreau9638efa2014-05-23 11:19:57 +02003698 else
3699 sv_state = 4;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003700
Willy Tarreauefe28222014-05-21 17:13:13 +02003701 if (server_is_draining(sv))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003702 sv_state += 4;
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003703
Willy Tarreau9638efa2014-05-23 11:19:57 +02003704 if (sv_state == 4 && !(svs->check.state & CHK_ST_ENABLED))
3705 sv_state = 9; /* unchecked UP */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003706 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003707 else if (sv->state == SRV_ST_STOPPING) {
3708 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
3709 (svs->check.health == svs->check.rise + svs->check.fall - 1))
Willy Tarreau9638efa2014-05-23 11:19:57 +02003710 sv_state = 6; /* NOLB */
Willy Tarreau91861262007-10-17 17:06:05 +02003711 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003712 sv_state = 5; /* NOLB going down */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003713 }
3714 else { /* stopped */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003715 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health)
3716 sv_state = 1; /* DOWN (agent) */
3717 else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003718 sv_state = 0; /* DOWN */
Willy Tarreau9638efa2014-05-23 11:19:57 +02003719 else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED))
3720 sv_state = 2; /* going up */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003721 else
Willy Tarreau9638efa2014-05-23 11:19:57 +02003722 sv_state = 0; /* DOWN, unchecked */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003723 }
Willy Tarreau91861262007-10-17 17:06:05 +02003724
Willy Tarreau9638efa2014-05-23 11:19:57 +02003725 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003726 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003727 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003728 continue;
3729 }
3730
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003731 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3732 if (bi_putchk(rep, &trash) == -1)
3733 return 0;
3734 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003735
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003736 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003737 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003738
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003739 case STAT_PX_ST_BE:
3740 /* print the backend */
3741 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3742 if (bi_putchk(rep, &trash) == -1)
3743 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003744
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003745 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003746 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003747
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003748 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003749 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003750 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003751 if (bi_putchk(rep, &trash) == -1)
3752 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003753 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003754
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003755 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003756 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003757
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003758 case STAT_PX_ST_FIN:
3759 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003760
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003761 default:
3762 /* unknown state, we should put an abort() here ! */
3763 return 1;
3764 }
3765}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003766
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003767/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3768 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003769 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003770static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003771{
3772 /* WARNING! This must fit in the first buffer !!! */
3773 chunk_appendf(&trash,
3774 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3775 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3776 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3777 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3778 "<style type=\"text/css\"><!--\n"
3779 "body {"
3780 " font-family: arial, helvetica, sans-serif;"
3781 " font-size: 12px;"
3782 " font-weight: normal;"
3783 " color: black;"
3784 " background: white;"
3785 "}\n"
3786 "th,td {"
3787 " font-size: 10px;"
3788 "}\n"
3789 "h1 {"
3790 " font-size: x-large;"
3791 " margin-bottom: 0.5em;"
3792 "}\n"
3793 "h2 {"
3794 " font-family: helvetica, arial;"
3795 " font-size: x-large;"
3796 " font-weight: bold;"
3797 " font-style: italic;"
3798 " color: #6020a0;"
3799 " margin-top: 0em;"
3800 " margin-bottom: 0em;"
3801 "}\n"
3802 "h3 {"
3803 " font-family: helvetica, arial;"
3804 " font-size: 16px;"
3805 " font-weight: bold;"
3806 " color: #b00040;"
3807 " background: #e8e8d0;"
3808 " margin-top: 0em;"
3809 " margin-bottom: 0em;"
3810 "}\n"
3811 "li {"
3812 " margin-top: 0.25em;"
3813 " margin-right: 2em;"
3814 "}\n"
3815 ".hr {margin-top: 0.25em;"
3816 " border-color: black;"
3817 " border-bottom-style: solid;"
3818 "}\n"
3819 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3820 ".total {background: #20D0D0;color: #ffff80;}\n"
3821 ".frontend {background: #e8e8d0;}\n"
3822 ".socket {background: #d0d0d0;}\n"
3823 ".backend {background: #e8e8d0;}\n"
3824 ".active0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02003825 ".active1 {background: #ff9090;}\n"
3826 ".active2 {background: #ffd020;}\n"
3827 ".active3 {background: #ffffa0;}\n"
3828 ".active4 {background: #c0ffc0;}\n"
3829 ".active5 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
3830 ".active6 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3831 ".active7 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3832 ".active8 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
3833 ".active9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003834 ".backup0 {background: #ff9090;}\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02003835 ".backup1 {background: #ff9090;}\n"
3836 ".backup2 {background: #ff80ff;}\n"
3837 ".backup3 {background: #c060ff;}\n"
3838 ".backup4 {background: #b0d0ff;}\n"
3839 ".backup5 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3840 ".backup6 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
3841 ".backup7 {background: #c060ff;}\n"
3842 ".backup8 {background: #cc9900;}\n"
3843 ".backup9 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003844 ".maintain {background: #c07820;}\n"
3845 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3846 "\n"
3847 "a.px:link {color: #ffff40; text-decoration: none;}"
3848 "a.px:visited {color: #ffff40; text-decoration: none;}"
3849 "a.px:hover {color: #ffffff; text-decoration: none;}"
3850 "a.lfsb:link {color: #000000; text-decoration: none;}"
3851 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3852 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3853 "\n"
3854 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3855 "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"
3856 "table.tbl td.ac { text-align: center;}\n"
3857 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3858 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3859 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3860 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3861 "\n"
3862 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3863 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3864 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003865 "table.det { border-collapse: collapse; border-style: none; }\n"
3866 "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 +01003867 "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 +01003868 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003869 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003870 " display:block;\n"
3871 " visibility:hidden;\n"
3872 " z-index:2147483647;\n"
3873 " position:absolute;\n"
3874 " padding:2px 4px 3px;\n"
3875 " background:#f0f060; color:#000000;\n"
3876 " border:1px solid #7040c0;\n"
3877 " white-space:nowrap;\n"
3878 " font-style:normal;font-size:11px;font-weight:normal;\n"
3879 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3880 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3881 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003882 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003883 "-->\n"
3884 "</style></head>\n",
3885 (uri->flags & ST_SHNODE) ? " on " : "",
3886 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3887 );
3888}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003889
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003890/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003891 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003892 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003893 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003894static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003895{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003896 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003897 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003898 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003899
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003900 /* WARNING! this has to fit the first packet too.
3901 * We are around 3.5 kB, add adding entries will
3902 * become tricky if we want to support 4kB buffers !
3903 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003904 chunk_appendf(&trash,
3905 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3906 PRODUCT_NAME "%s</a></h1>\n"
3907 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3908 "<hr width=\"100%%\" class=\"hr\">\n"
3909 "<h3>&gt; General process information</h3>\n"
3910 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3911 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3912 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3913 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3914 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3915 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3916 "Running tasks: %d/%d; idle = %d %%<br>\n"
3917 "</td><td align=\"center\" nowrap>\n"
3918 "<table class=\"lgd\"><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02003919 "<td class=\"active4\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3920 "<td class=\"backup4\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003921 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02003922 "<td class=\"active3\"></td><td class=\"noborder\">active UP, going down </td>"
3923 "<td class=\"backup3\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003924 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02003925 "<td class=\"active2\"></td><td class=\"noborder\">active DOWN, going up </td>"
3926 "<td class=\"backup2\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003927 "</tr><tr>\n"
3928 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Willy Tarreau9638efa2014-05-23 11:19:57 +02003929 "<td class=\"active9\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003930 "</tr><tr>\n"
3931 "<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 -07003932 "</tr><tr>\n"
Willy Tarreau9638efa2014-05-23 11:19:57 +02003933 "<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 +01003934 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003935 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003936 "</td>"
3937 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3938 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3939 "",
3940 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3941 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3942 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3943 (uri->flags & ST_SHDESC) ? ": " : "",
3944 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3945 pid, relative_pid, global.nbproc,
3946 up / 86400, (up % 86400) / 3600,
3947 (up % 3600) / 60, (up % 60),
3948 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3949 global.rlimit_memmax ? " MB" : "",
3950 global.rlimit_nofile,
3951 global.maxsock, global.maxconn, global.maxpipes,
3952 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3953 run_queue_cur, nb_tasks_cur, idle_pct
3954 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003955
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003956 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3957 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3958 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003959
3960 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003961 "<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 +02003962 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003963 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3964 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3965 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003966 STAT_SCOPE_TXT_MAXLEN);
3967
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003968 /* 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 +02003969 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003970 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003971 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003972 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3973 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003974 }
3975
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003976 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003977 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003978 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003979 uri->uri_prefix,
3980 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003981 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003982 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003983 else
3984 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003985 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003986 uri->uri_prefix,
3987 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003988 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003989 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003990
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003991 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003992 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003993 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003994 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003995 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003996 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003997 "",
3998 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003999 else
4000 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004001 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004002 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004003 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004004 ";norefresh",
4005 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004006 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004007
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004008 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004009 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004010 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004011 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4012 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004013 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004014
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004015 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004016 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004017 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004018 (uri->refresh > 0) ? ";norefresh" : "",
4019 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004020
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004021 chunk_appendf(&trash,
4022 "</ul></td>"
4023 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4024 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4025 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4026 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4027 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4028 "</ul>"
4029 "</td>"
4030 "</tr></table>\n"
4031 ""
4032 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004033
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004034 if (appctx->ctx.stats.st_code) {
4035 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004036 case STAT_STATUS_DONE:
4037 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004038 "<p><div class=active4>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004039 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004040 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004041 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004042 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4043 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004044 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004045 break;
4046 case STAT_STATUS_NONE:
4047 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004048 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004049 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004050 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004051 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004052 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4053 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004054 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004055 break;
4056 case STAT_STATUS_PART:
4057 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004058 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004059 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004060 "Action partially processed.<br>"
4061 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004062 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004063 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4064 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004065 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004066 break;
4067 case STAT_STATUS_ERRP:
4068 chunk_appendf(&trash,
4069 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004070 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004071 "Action not processed because of invalid parameters."
4072 "<ul>"
4073 "<li>The action is maybe unknown.</li>"
4074 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4075 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4076 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004077 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004078 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4079 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004080 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004081 break;
4082 case STAT_STATUS_EXCD:
4083 chunk_appendf(&trash,
4084 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004085 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004086 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4087 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004088 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004089 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4090 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004091 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004092 break;
4093 case STAT_STATUS_DENY:
4094 chunk_appendf(&trash,
4095 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004096 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004097 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004098 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004099 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4100 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004101 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004102 break;
4103 default:
4104 chunk_appendf(&trash,
Willy Tarreau9638efa2014-05-23 11:19:57 +02004105 "<p><div class=active9>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004106 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004107 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004108 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004109 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4110 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004111 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004112 }
4113 chunk_appendf(&trash, "<p>\n");
4114 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004115}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004116
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004117/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4118 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004119 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004120static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004121{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004122 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004123}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004124
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004125/* This function dumps statistics onto the stream interface's read buffer in
4126 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004127 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4128 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
4129 * and the session must be closed, or -1 in case of any error. This function is
4130 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004131 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004132static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004133{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004134 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004135 struct channel *rep = si->ib;
4136 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004137
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004138 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004139
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004140 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004141 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004142 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004143 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004144
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004145 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004146 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004147 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004148 else
4149 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004150
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004151 if (bi_putchk(rep, &trash) == -1)
4152 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004153
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004154 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004155 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004156
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004157 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004158 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004159 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004160 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004161 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004162 }
Willy Tarreau91861262007-10-17 17:06:05 +02004163
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004164 appctx->ctx.stats.px = proxy;
4165 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4166 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004167 /* fall through */
4168
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004169 case STAT_ST_LIST:
4170 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004171 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004172 if (buffer_almost_full(rep->buf)) {
4173 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004174 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004175 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004176
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004177 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004178 /* skip the disabled proxies, global frontend and non-networked ones */
4179 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004180 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004181 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004182
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004183 appctx->ctx.stats.px = px->next;
4184 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004185 }
4186 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004187
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004188 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004189 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004190
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004191 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004192 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004193 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004194 if (bi_putchk(rep, &trash) == -1)
4195 return 0;
4196 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004197
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004198 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004199 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004200
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004201 case STAT_ST_FIN:
4202 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004203
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004204 default:
4205 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004206 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004207 return -1;
4208 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004209}
Willy Tarreauae526782010-03-04 20:34:23 +01004210
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004211/* We reached the stats page through a POST request. The appctx is
4212 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004213 * Parse the posted data and enable/disable servers if necessary.
4214 * Returns 1 if request was parsed or zero if it needs more data.
4215 */
4216static int stats_process_http_post(struct stream_interface *si)
4217{
4218 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004219 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004220
4221 struct proxy *px = NULL;
4222 struct server *sv = NULL;
4223
4224 char key[LINESIZE];
4225 int action = ST_ADM_ACTION_NONE;
4226 int reprocess = 0;
4227
4228 int total_servers = 0;
4229 int altered_servers = 0;
4230
4231 char *first_param, *cur_param, *next_param, *end_params;
4232 char *st_cur_param = NULL;
4233 char *st_next_param = NULL;
4234
4235 struct chunk *temp;
4236 int reql;
4237
4238 temp = get_trash_chunk();
4239 if (temp->size < s->txn.req.body_len) {
4240 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004241 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004242 goto out;
4243 }
4244
4245 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4246 if (reql <= 0) {
4247 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004248 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004249 return 0;
4250 }
4251
4252 first_param = temp->str;
4253 end_params = temp->str + reql;
4254 cur_param = next_param = end_params;
4255 *end_params = '\0';
4256
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004257 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004258
4259 /*
4260 * Parse the parameters in reverse order to only store the last value.
4261 * From the html form, the backend and the action are at the end.
4262 */
4263 while (cur_param > first_param) {
4264 char *value;
4265 int poffset, plen;
4266
4267 cur_param--;
4268
4269 if ((*cur_param == '&') || (cur_param == first_param)) {
4270 reprocess_servers:
4271 /* Parse the key */
4272 poffset = (cur_param != first_param ? 1 : 0);
4273 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4274 if ((plen > 0) && (plen <= sizeof(key))) {
4275 strncpy(key, cur_param + poffset, plen);
4276 key[plen - 1] = '\0';
4277 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004278 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004279 goto out;
4280 }
4281
4282 /* Parse the value */
4283 value = key;
4284 while (*value != '\0' && *value != '=') {
4285 value++;
4286 }
4287 if (*value == '=') {
4288 /* Ok, a value is found, we can mark the end of the key */
4289 *value++ = '\0';
4290 }
4291 if (url_decode(key) < 0 || url_decode(value) < 0)
4292 break;
4293
4294 /* Now we can check the key to see what to do */
4295 if (!px && (strcmp(key, "b") == 0)) {
4296 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4297 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004298 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004299 goto out;
4300 }
4301 }
4302 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02004303 if (strcmp(value, "ready") == 0) {
4304 action = ST_ADM_ACTION_READY;
4305 }
4306 else if (strcmp(value, "drain") == 0) {
4307 action = ST_ADM_ACTION_DRAIN;
4308 }
4309 else if (strcmp(value, "maint") == 0) {
4310 action = ST_ADM_ACTION_MAINT;
4311 }
4312 else if (strcmp(value, "shutdown") == 0) {
4313 action = ST_ADM_ACTION_SHUTDOWN;
4314 }
4315 /* else these are the old supported methods */
4316 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004317 action = ST_ADM_ACTION_DISABLE;
4318 }
4319 else if (strcmp(value, "enable") == 0) {
4320 action = ST_ADM_ACTION_ENABLE;
4321 }
4322 else if (strcmp(value, "stop") == 0) {
4323 action = ST_ADM_ACTION_STOP;
4324 }
4325 else if (strcmp(value, "start") == 0) {
4326 action = ST_ADM_ACTION_START;
4327 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004328 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004329 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004330 goto out;
4331 }
4332 }
4333 else if (strcmp(key, "s") == 0) {
4334 if (!(px && action)) {
4335 /*
4336 * Indicates that we'll need to reprocess the parameters
4337 * as soon as backend and action are known
4338 */
4339 if (!reprocess) {
4340 st_cur_param = cur_param;
4341 st_next_param = next_param;
4342 }
4343 reprocess = 1;
4344 }
4345 else if ((sv = findserver(px, value)) != NULL) {
4346 switch (action) {
4347 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004348 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004349 altered_servers++;
4350 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004351 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004352 }
4353 break;
4354 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004355 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004356 altered_servers++;
4357 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004358 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004359 }
4360 break;
4361 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004362 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
4363 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
4364 altered_servers++;
4365 total_servers++;
4366 }
4367 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004368 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02004369 if (sv->admin & SRV_ADMF_FDRAIN) {
4370 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
4371 altered_servers++;
4372 total_servers++;
4373 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004374 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02004375 case ST_ADM_ACTION_READY:
4376 srv_adm_set_ready(sv);
4377 altered_servers++;
4378 total_servers++;
4379 break;
4380 case ST_ADM_ACTION_DRAIN:
4381 srv_adm_set_drain(sv);
4382 altered_servers++;
4383 total_servers++;
4384 break;
4385 case ST_ADM_ACTION_MAINT:
4386 srv_adm_set_maint(sv);
4387 altered_servers++;
4388 total_servers++;
4389 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004390 case ST_ADM_ACTION_SHUTDOWN:
4391 if (px->state != PR_STSTOPPED) {
4392 struct session *sess, *sess_bck;
4393
4394 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4395 if (sess->srv_conn == sv)
4396 session_shutdown(sess, SN_ERR_KILLED);
4397
4398 altered_servers++;
4399 total_servers++;
4400 }
4401 break;
4402 }
4403 } else {
4404 /* the server name is unknown or ambiguous (duplicate names) */
4405 total_servers++;
4406 }
4407 }
4408 if (reprocess && px && action) {
4409 /* Now, we know the backend and the action chosen by the user.
4410 * We can safely restart from the first server parameter
4411 * to reprocess them
4412 */
4413 cur_param = st_cur_param;
4414 next_param = st_next_param;
4415 reprocess = 0;
4416 goto reprocess_servers;
4417 }
4418
4419 next_param = cur_param;
4420 }
4421 }
4422
4423 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004424 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004425 }
4426 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004427 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004428 }
4429 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004430 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004431 }
4432 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004433 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004434 }
4435 out:
4436 return 1;
4437}
4438
4439
4440static int stats_send_http_headers(struct stream_interface *si)
4441{
4442 struct session *s = session_from_task(si->owner);
4443 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004444 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004445
4446 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004447 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004448 "Cache-Control: no-cache\r\n"
4449 "Connection: close\r\n"
4450 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004451 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004452
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004453 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004454 chunk_appendf(&trash, "Refresh: %d\r\n",
4455 uri->refresh);
4456
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004457 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4458
4459 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4460 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4461 else
4462 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004463
4464 s->txn.status = 200;
4465 s->logs.tv_request = now;
4466
4467 if (bi_putchk(si->ib, &trash) == -1)
4468 return 0;
4469
4470 return 1;
4471}
4472
4473static int stats_send_http_redirect(struct stream_interface *si)
4474{
4475 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4476 struct session *s = session_from_task(si->owner);
4477 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004478 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004479
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004480 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004481 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004482 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004483 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004484 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4485 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004486 }
4487
4488 /* We don't want to land on the posted stats page because a refresh will
4489 * repost the data. We don't want this to happen on accident so we redirect
4490 * the browse to the stats page with a GET.
4491 */
4492 chunk_printf(&trash,
4493 "HTTP/1.1 303 See Other\r\n"
4494 "Cache-Control: no-cache\r\n"
4495 "Content-Type: text/plain\r\n"
4496 "Connection: close\r\n"
4497 "Location: %s;st=%s%s%s%s\r\n"
4498 "\r\n",
4499 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004500 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4501 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4502 stat_status_codes[appctx->ctx.stats.st_code]) ?
4503 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004504 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004505 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4506 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004507 scope_txt);
4508
4509 s->txn.status = 303;
4510 s->logs.tv_request = now;
4511
4512 if (bi_putchk(si->ib, &trash) == -1)
4513 return 0;
4514
4515 return 1;
4516}
4517
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004518/* This I/O handler runs as an applet embedded in a stream interface. It is
4519 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004520 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004521 * automatically unregisters itself once transfer is complete.
4522 */
4523static void http_stats_io_handler(struct stream_interface *si)
4524{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004525 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004526 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004527 struct channel *req = si->ob;
4528 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004529
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004530 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4531 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004532
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004533 /* check that the output is not closed */
4534 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004535 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004536
Willy Tarreau347a35d2013-11-22 17:51:09 +01004537 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004538 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004539 if (stats_send_http_headers(si)) {
4540 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004541 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004542 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004543 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004544 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004545 }
4546
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004547 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004548 unsigned int prev_len = si->ib->buf->i;
4549 unsigned int data_len;
4550 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004551 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004552
4553 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4554 /* One difficulty we're facing is that we must prevent
4555 * the input data from being automatically forwarded to
4556 * the output area. For this, we temporarily disable
4557 * forwarding on the channel.
4558 */
4559 last_fwd = si->ib->to_forward;
4560 si->ib->to_forward = 0;
4561 chunk_printf(&trash, "\r\n000000\r\n");
4562 if (bi_putchk(si->ib, &trash) == -1) {
4563 si->ib->to_forward = last_fwd;
4564 goto fail;
4565 }
4566 }
4567
4568 data_len = si->ib->buf->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004569 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004570 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004571
4572 last_len = si->ib->buf->i;
4573
4574 /* Now we must either adjust or remove the chunk size. This is
4575 * not easy because the chunk size might wrap at the end of the
4576 * buffer, so we pretend we have nothing in the buffer, we write
4577 * the size, then restore the buffer's contents. Note that we can
4578 * only do that because no forwarding is scheduled on the stats
4579 * applet.
4580 */
4581 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4582 si->ib->total -= (last_len - prev_len);
4583 si->ib->buf->i -= (last_len - prev_len);
4584
4585 if (last_len != data_len) {
4586 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
4587 bi_putchk(si->ib, &trash);
4588
4589 si->ib->total += (last_len - data_len);
4590 si->ib->buf->i += (last_len - data_len);
4591 }
4592 /* now re-enable forwarding */
4593 channel_forward(si->ib, last_fwd);
4594 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004595 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004596
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004597 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004598 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004599 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004600 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004601 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004602 }
4603
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004604 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004605 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004606 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004607 }
4608
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004609 if (appctx->st0 == STAT_HTTP_DONE) {
4610 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4611 chunk_printf(&trash, "\r\n0\r\n\r\n");
4612 if (bi_putchk(si->ib, &trash) == -1)
4613 goto fail;
4614 }
4615 /* eat the whole request */
4616 bo_skip(si->ob, si->ob->buf->o);
4617 res->flags |= CF_READ_NULL;
4618 si_shutr(si);
4619 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004620
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004621 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4622 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004623
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004624 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004625 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4626 si_shutr(si);
4627 res->flags |= CF_READ_NULL;
4628 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004629 }
Willy Tarreau91861262007-10-17 17:06:05 +02004630
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004631 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004632 /* update all other flags and resync with the other side */
4633 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004634
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004635 /* we don't want to expire timeouts while we're processing requests */
4636 si->ib->rex = TICK_ETERNITY;
4637 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004638
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004639 out:
4640 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4641 /* check that we have released everything then unregister */
4642 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004643 }
4644}
4645
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004646
Willy Tarreau909d5172012-11-26 03:04:41 +01004647static inline const char *get_conn_ctrl_name(const struct connection *conn)
4648{
Willy Tarreau3c728722014-01-23 13:50:42 +01004649 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004650 return "NONE";
4651 return conn->ctrl->name;
4652}
4653
4654static inline const char *get_conn_xprt_name(const struct connection *conn)
4655{
4656 static char ptr[17];
4657
Willy Tarreauaad69382014-01-23 14:21:42 +01004658 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004659 return "NONE";
4660
4661 if (conn->xprt == &raw_sock)
4662 return "RAW";
4663
4664#ifdef USE_OPENSSL
4665 if (conn->xprt == &ssl_sock)
4666 return "SSL";
4667#endif
4668 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4669 return ptr;
4670}
4671
4672static inline const char *get_conn_data_name(const struct connection *conn)
4673{
4674 static char ptr[17];
4675
4676 if (!conn->data)
4677 return "NONE";
4678
4679 if (conn->data == &sess_conn_cb)
4680 return "SESS";
4681
4682 if (conn->data == &si_conn_cb)
4683 return "STRM";
4684
4685 if (conn->data == &check_conn_cb)
4686 return "CHCK";
4687
4688 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4689 return ptr;
4690}
4691
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004692/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004693 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004694 * 0 if the output buffer is full and it needs to be called again, otherwise
4695 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004696 */
Willy Tarreau76153662012-11-26 01:16:39 +01004697static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004698{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004699 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004700 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004701 extern const char *monthname[12];
4702 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004703 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004704 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004705
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004706 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004707
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004708 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004709 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004710 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4711 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004712 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004713 appctx->ctx.sess.uid = 0;
4714 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004715 return 1;
4716 }
4717
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004718 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004719 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004720 appctx->ctx.sess.uid = sess->uniq_id;
4721 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004722 /* fall through */
4723
4724 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004725 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004726 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004727 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004728 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004729 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4730 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004731 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004732 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004733
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004734 conn = objt_conn(sess->si[0].end);
4735 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004736 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004737 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004738 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004739 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004740 break;
4741 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004742 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004743 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004744 default:
4745 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004746 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004747 break;
4748 }
4749
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004750 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004751 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004752 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004753
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004754 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004755 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004756 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4757 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4758 sess->listener ? sess->listener->luid : 0);
4759
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004760 if (conn)
4761 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004762
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004763 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004764 case AF_INET:
4765 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004766 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004767 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004768 break;
4769 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004770 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004771 break;
4772 default:
4773 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004774 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004775 break;
4776 }
4777
Willy Tarreau50943332011-09-02 17:33:05 +02004778 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004779 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004780 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004781 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004782 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4783 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004784 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004785
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004786 conn = objt_conn(sess->si[1].end);
4787 if (conn)
4788 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004789
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004790 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004791 case AF_INET:
4792 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004793 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004794 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004795 break;
4796 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004797 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004798 break;
4799 default:
4800 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004801 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004802 break;
4803 }
4804
4805 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004806 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004807 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004808 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4809 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004810 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004811 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004812
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004813 if (conn)
4814 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004815
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004816 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004817 case AF_INET:
4818 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004819 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004820 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004821 break;
4822 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004823 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004824 break;
4825 default:
4826 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004827 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004828 break;
4829 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004830
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004831 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004832 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004833 sess->task,
4834 sess->task->state,
4835 sess->task->nice, sess->task->calls,
4836 sess->task->expire ?
4837 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4838 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4839 TICKS_TO_MS(1000)) : "<NEVER>",
4840 task_in_rq(sess->task) ? ", running" : "");
4841
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004842 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004843 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004844 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4845
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004846 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004847 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4848 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4849 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4850
4851 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004852 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004853 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004854 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004855 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004856 obj_type_name(sess->si[0].end),
4857 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004858 sess->si[0].exp ?
4859 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4860 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4861 TICKS_TO_MS(1000)) : "<NEVER>",
4862 sess->si[0].err_type);
4863
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004864 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004865 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004866 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004867 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004868 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004869 obj_type_name(sess->si[1].end),
4870 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004871 sess->si[1].exp ?
4872 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4873 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4874 TICKS_TO_MS(1000)) : "<NEVER>",
4875 sess->si[1].err_type);
4876
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004877 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004878 chunk_appendf(&trash,
4879 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004880 conn,
4881 get_conn_ctrl_name(conn),
4882 get_conn_xprt_name(conn),
4883 get_conn_data_name(conn),
4884 obj_type_name(conn->target),
4885 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004886
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004887 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004888 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004889 conn->flags,
4890 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004891 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004892 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004893 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004894 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004895 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4896 chunk_appendf(&trash,
4897 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4898 tmpctx,
4899 tmpctx->st0,
4900 tmpctx->st1,
4901 tmpctx->st2,
4902 tmpctx->applet->name);
4903 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004904
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004905 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004906 chunk_appendf(&trash,
4907 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004908 conn,
4909 get_conn_ctrl_name(conn),
4910 get_conn_xprt_name(conn),
4911 get_conn_data_name(conn),
4912 obj_type_name(conn->target),
4913 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004914
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004915 chunk_appendf(&trash,
4916 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004917 conn->flags,
4918 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004919 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004920 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004921 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004922 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004923 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4924 chunk_appendf(&trash,
4925 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4926 tmpctx,
4927 tmpctx->st0,
4928 tmpctx->st1,
4929 tmpctx->st2,
4930 tmpctx->applet->name);
4931 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004932
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004933 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004934 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004935 " an_exp=%s",
4936 sess->req,
4937 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004938 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004939 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004940 sess->req->analyse_exp ?
4941 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4942 TICKS_TO_MS(1000)) : "<NEVER>");
4943
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004944 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004945 " rex=%s",
4946 sess->req->rex ?
4947 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4948 TICKS_TO_MS(1000)) : "<NEVER>");
4949
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004950 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004951 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004952 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004953 sess->req->wex ?
4954 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4955 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004956 sess->req->buf,
4957 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004958 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004959 sess->txn.req.next, sess->req->buf->i,
4960 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004961
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004962 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004963 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004964 " an_exp=%s",
4965 sess->rep,
4966 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004967 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004968 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004969 sess->rep->analyse_exp ?
4970 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4971 TICKS_TO_MS(1000)) : "<NEVER>");
4972
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004973 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004974 " rex=%s",
4975 sess->rep->rex ?
4976 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4977 TICKS_TO_MS(1000)) : "<NEVER>");
4978
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004979 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004980 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004981 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004982 sess->rep->wex ?
4983 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4984 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004985 sess->rep->buf,
4986 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004987 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004988 sess->txn.rsp.next, sess->rep->buf->i,
4989 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004990
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004991 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004992 return 0;
4993
4994 /* use other states to dump the contents */
4995 }
4996 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004997 appctx->ctx.sess.uid = 0;
4998 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004999 return 1;
5000}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005001
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005002static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005003{
5004 struct appctx *appctx = __objt_appctx(si->end);
5005
5006 switch (appctx->st2) {
5007 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005008 /* Display the column headers. If the message cannot be sent,
5009 * quit the fucntion with returning 0. The function is called
5010 * later and restart at the state "STAT_ST_INIT".
5011 */
5012 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01005013 chunk_appendf(&trash, "# id (file) description\n");
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005014 if (bi_putchk(si->ib, &trash) == -1)
5015 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005016
5017 /* Now, we start the browsing of the references lists.
5018 * Note that the following call to LIST_ELEM return bad pointer. The only
5019 * avalaible field of this pointer is <list>. It is used with the function
5020 * pat_list_get_next() for retruning the first avalaible entry
5021 */
5022 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
5023 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5024 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005025 appctx->st2 = STAT_ST_LIST;
5026 /* fall through */
5027
5028 case STAT_ST_LIST:
5029 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005030 chunk_reset(&trash);
5031
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005032 /* Build messages. If the reference is used by another category than
5033 * the listed categorie, display the information in the massage.
5034 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01005035 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01005036 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
5037 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01005038
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005039 if (bi_putchk(si->ib, &trash) == -1) {
5040 /* let's try again later from this session. We add ourselves into
5041 * this session's users so that it can remove us upon termination.
5042 */
5043 return 0;
5044 }
5045
5046 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005047 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
5048 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005049 }
5050
5051 appctx->st2 = STAT_ST_FIN;
5052 /* fall through */
5053
5054 default:
5055 appctx->st2 = STAT_ST_FIN;
5056 return 1;
5057 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005058 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005059}
5060
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005061static int stats_map_lookup(struct stream_interface *si)
5062{
5063 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005064 struct sample sample;
5065 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005066 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005067
5068 switch (appctx->st2) {
5069 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005070 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01005071 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005072 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005073 appctx->st2 = STAT_ST_LIST;
5074 /* fall through */
5075
5076 case STAT_ST_LIST:
5077 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005078 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005079 /* initialise chunk to build new message */
5080 chunk_reset(&trash);
5081
5082 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005083 sample.type = SMP_T_STR;
5084 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005085 sample.data.str.len = appctx->ctx.map.chunk.len;
5086 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01005087 if (appctx->ctx.map.expr->pat_head->match &&
5088 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005089 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
5090 else
5091 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005092
5093 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005094 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
5095 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
5096 break;
5097 if (match_method >= PAT_MATCH_NUM)
5098 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
5099 else
5100 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005101
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005102 /* case sensitive */
5103 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
5104 chunk_appendf(&trash, ", case=insensitive");
5105 else
5106 chunk_appendf(&trash, ", case=sensitive");
5107
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005108 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005109 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005110 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5111 chunk_appendf(&trash, ", found=no");
5112 else
5113 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005114 }
5115
5116 /* Display match and match info */
5117 else {
5118 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005119 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
5120 chunk_appendf(&trash, ", found=yes");
5121 else
5122 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005123
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005124 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005125 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005126 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005127 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005128 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005129
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005130 /* display pattern */
5131 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5132 if (pat->ref && pat->ref->pattern)
5133 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5134 else
5135 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005136 }
5137 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005138 if (pat->ref && pat->ref->pattern)
5139 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5140 else
5141 chunk_appendf(&trash, ", pattern=unknown");
5142 }
5143
5144 /* display return value */
5145 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5146 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005147 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005148 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005149 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005150 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005151 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005152 }
5153
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005154 chunk_appendf(&trash, "\n");
5155
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005156 /* display response */
5157 if (bi_putchk(si->ib, &trash) == -1) {
5158 /* let's try again later from this session. We add ourselves into
5159 * this session's users so that it can remove us upon termination.
5160 */
5161 return 0;
5162 }
5163
5164 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005165 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5166 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005167 }
5168
5169 appctx->st2 = STAT_ST_FIN;
5170 /* fall through */
5171
5172 default:
5173 appctx->st2 = STAT_ST_FIN;
5174 free(appctx->ctx.map.chunk.str);
5175 return 1;
5176 }
5177}
5178
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005179static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005180{
5181 struct appctx *appctx = __objt_appctx(si->end);
5182
5183 switch (appctx->st2) {
5184
5185 case STAT_ST_INIT:
5186 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005187 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5188 struct pat_ref_elt *, list);
5189 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5190 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005191 appctx->st2 = STAT_ST_LIST;
5192 /* fall through */
5193
5194 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005195 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005196 chunk_reset(&trash);
5197
5198 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005199 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005200 chunk_appendf(&trash, "%p %s %s\n",
5201 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5202 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005203 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005204 chunk_appendf(&trash, "%p %s\n",
5205 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005206
5207 if (bi_putchk(si->ib, &trash) == -1) {
5208 /* let's try again later from this session. We add ourselves into
5209 * this session's users so that it can remove us upon termination.
5210 */
5211 return 0;
5212 }
5213
5214 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005215 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5216 struct pat_ref_elt *, list);
5217 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005218 break;
5219 }
5220
5221 appctx->st2 = STAT_ST_FIN;
5222 /* fall through */
5223
5224 default:
5225 appctx->st2 = STAT_ST_FIN;
5226 return 1;
5227 }
5228}
5229
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005230/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005231 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005232 * to be called again, otherwise non-zero. It is designed to be called
5233 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005234 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005235static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005236{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005237 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005238 struct connection *conn;
5239
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005240 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005241 /* If we're forced to shut down, we might have to remove our
5242 * reference to the last session being dumped.
5243 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005244 if (appctx->st2 == STAT_ST_LIST) {
5245 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5246 LIST_DEL(&appctx->ctx.sess.bref.users);
5247 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005248 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005249 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005250 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005251 }
5252
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005253 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005254
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005255 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005256 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005257 /* the function had not been called yet, let's prepare the
5258 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005259 * pointer to the first in the global list. When a target
5260 * session is being destroyed, it is responsible for updating
5261 * this pointer. We know we have reached the end when this
5262 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005263 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005264 LIST_INIT(&appctx->ctx.sess.bref.users);
5265 appctx->ctx.sess.bref.ref = sessions.n;
5266 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005267 /* fall through */
5268
Willy Tarreau295a8372011-03-10 11:25:07 +01005269 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005270 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005271 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5272 LIST_DEL(&appctx->ctx.sess.bref.users);
5273 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005274 }
5275
5276 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005277 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005278 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005279 struct session *curr_sess;
5280
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005281 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005282
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005283 if (appctx->ctx.sess.target) {
5284 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005285 goto next_sess;
5286
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005287 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005288 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005289 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005290 return 0;
5291
5292 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005293 LIST_DEL(&appctx->ctx.sess.bref.users);
5294 LIST_INIT(&appctx->ctx.sess.bref.users);
5295 if (appctx->ctx.sess.target != (void *)-1) {
5296 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005297 break;
5298 }
5299 else
5300 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005301 }
5302
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005303 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005304 "%p: proto=%s",
5305 curr_sess,
5306 curr_sess->listener->proto->name);
5307
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005308
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005309 conn = objt_conn(curr_sess->si[0].end);
5310 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005311 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005312 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005313 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005314 " src=%s:%d fe=%s be=%s srv=%s",
5315 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005316 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005317 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005318 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005319 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005320 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005321 break;
5322 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005323 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005324 " src=unix:%d fe=%s be=%s srv=%s",
5325 curr_sess->listener->luid,
5326 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005327 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005328 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005329 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005330 break;
5331 }
5332
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005333 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005334 " ts=%02x age=%s calls=%d",
5335 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005336 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5337 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005338
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005339 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005340 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005341 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005342 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005343 curr_sess->req->analysers,
5344 curr_sess->req->rex ?
5345 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5346 TICKS_TO_MS(1000)) : "");
5347
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005348 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005349 ",wx=%s",
5350 curr_sess->req->wex ?
5351 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5352 TICKS_TO_MS(1000)) : "");
5353
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005354 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005355 ",ax=%s]",
5356 curr_sess->req->analyse_exp ?
5357 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5358 TICKS_TO_MS(1000)) : "");
5359
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005360 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005361 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005362 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005363 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005364 curr_sess->rep->analysers,
5365 curr_sess->rep->rex ?
5366 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5367 TICKS_TO_MS(1000)) : "");
5368
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005369 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005370 ",wx=%s",
5371 curr_sess->rep->wex ?
5372 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5373 TICKS_TO_MS(1000)) : "");
5374
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005375 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005376 ",ax=%s]",
5377 curr_sess->rep->analyse_exp ?
5378 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5379 TICKS_TO_MS(1000)) : "");
5380
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005381 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005382 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005383 " s0=[%d,%1xh,fd=%d,ex=%s]",
5384 curr_sess->si[0].state,
5385 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005386 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005387 curr_sess->si[0].exp ?
5388 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5389 TICKS_TO_MS(1000)) : "");
5390
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005391 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005392 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005393 " s1=[%d,%1xh,fd=%d,ex=%s]",
5394 curr_sess->si[1].state,
5395 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005396 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005397 curr_sess->si[1].exp ?
5398 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5399 TICKS_TO_MS(1000)) : "");
5400
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005401 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005402 " exp=%s",
5403 curr_sess->task->expire ?
5404 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5405 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005406 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005407 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005408
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005409 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005410
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005411 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005412 /* let's try again later from this session. We add ourselves into
5413 * this session's users so that it can remove us upon termination.
5414 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005415 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005416 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005417 }
5418
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005419 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005420 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005421 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005422
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005423 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005424 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005425 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005426 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005427 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005428 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005429
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005430 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005431 return 0;
5432
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005433 appctx->ctx.sess.target = NULL;
5434 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005435 return 1;
5436 }
5437
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005438 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005439 /* fall through */
5440
5441 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005442 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005443 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005444 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005445}
5446
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005447/* This is called when the stream interface is closed. For instance, upon an
5448 * external abort, we won't call the i/o handler anymore so we may need to
5449 * remove back references to the session currently being dumped.
5450 */
Simon Horman74d88312013-02-12 10:45:50 +09005451static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005452{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005453 struct appctx *appctx = __objt_appctx(si->end);
5454
5455 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5456 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5457 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005458 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005459 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5460 free(appctx->ctx.cli.err);
5461 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005462 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5463 free(appctx->ctx.map.chunk.str);
5464 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005465}
5466
Willy Tarreau20e99322013-04-13 09:22:25 +02005467/* This function is used to either dump tables states (when action is set
5468 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005469 * It returns 0 if the output buffer is full and it needs to be called
5470 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005471 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005472static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005473{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005474 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005475 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005476 struct ebmb_node *eb;
5477 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005478 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005479 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005480
5481 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005482 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005483 * - STAT_ST_INIT : the first call
5484 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005485 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005486 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005487 * and the entry pointer points to the next entry to be dumped,
5488 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005489 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005490 * data though.
5491 */
5492
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005493 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005494 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005495 if (appctx->st2 == STAT_ST_LIST) {
5496 appctx->ctx.table.entry->ref_cnt--;
5497 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005498 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005499 return 1;
5500 }
5501
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005502 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005503
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005504 while (appctx->st2 != STAT_ST_FIN) {
5505 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005506 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005507 appctx->ctx.table.proxy = appctx->ctx.table.target;
5508 if (!appctx->ctx.table.proxy)
5509 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005510
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005511 appctx->ctx.table.entry = NULL;
5512 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005513 break;
5514
Willy Tarreau295a8372011-03-10 11:25:07 +01005515 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005516 if (!appctx->ctx.table.proxy ||
5517 (appctx->ctx.table.target &&
5518 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5519 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005520 break;
5521 }
5522
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005523 if (appctx->ctx.table.proxy->table.size) {
5524 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5525 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005526 return 0;
5527
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005528 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005529 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005530 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005531 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005532 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005533 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5534 appctx->ctx.table.entry->ref_cnt++;
5535 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005536 break;
5537 }
5538 }
5539 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005540 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005541 break;
5542
Willy Tarreau295a8372011-03-10 11:25:07 +01005543 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005544 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005545
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005546 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005547 /* we're filtering on some data contents */
5548 void *ptr;
5549 long long data;
5550
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005551 dt = appctx->ctx.table.data_type;
5552 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5553 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005554 dt);
5555
5556 data = 0;
5557 switch (stktable_data_types[dt].std_type) {
5558 case STD_T_SINT:
5559 data = stktable_data_cast(ptr, std_t_sint);
5560 break;
5561 case STD_T_UINT:
5562 data = stktable_data_cast(ptr, std_t_uint);
5563 break;
5564 case STD_T_ULL:
5565 data = stktable_data_cast(ptr, std_t_ull);
5566 break;
5567 case STD_T_FRQP:
5568 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005569 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005570 break;
5571 }
5572
5573 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005574 if ((data < appctx->ctx.table.value &&
5575 (appctx->ctx.table.data_op == STD_OP_EQ ||
5576 appctx->ctx.table.data_op == STD_OP_GT ||
5577 appctx->ctx.table.data_op == STD_OP_GE)) ||
5578 (data == appctx->ctx.table.value &&
5579 (appctx->ctx.table.data_op == STD_OP_NE ||
5580 appctx->ctx.table.data_op == STD_OP_GT ||
5581 appctx->ctx.table.data_op == STD_OP_LT)) ||
5582 (data > appctx->ctx.table.value &&
5583 (appctx->ctx.table.data_op == STD_OP_EQ ||
5584 appctx->ctx.table.data_op == STD_OP_LT ||
5585 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005586 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005587 }
5588
Simon Hormanc88b8872011-06-15 15:18:49 +09005589 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005590 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5591 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005592 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005593
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005594 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005595
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005596 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005597 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005598 struct stksess *old = appctx->ctx.table.entry;
5599 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005600 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005601 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5602 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5603 stksess_kill(&appctx->ctx.table.proxy->table, old);
5604 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005605 break;
5606 }
5607
Simon Hormanc88b8872011-06-15 15:18:49 +09005608
5609 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005610 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5611 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5612 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005613
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005614 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5615 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005616 break;
5617
Willy Tarreau295a8372011-03-10 11:25:07 +01005618 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005619 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005620 break;
5621 }
5622 }
5623 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005624}
5625
Willy Tarreaud426a182010-03-05 14:58:26 +01005626/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005627 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5628 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5629 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5630 * lines are respected within the limit of 70 output chars. Lines that are
5631 * continuation of a previous truncated line begin with "+" instead of " "
5632 * after the offset. The new pointer is returned.
5633 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005634static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5635 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005636{
5637 int end;
5638 unsigned char c;
5639
5640 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005641 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005642 return ptr;
5643
Willy Tarreau77804732012-10-29 16:14:26 +01005644 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005645
Willy Tarreaud426a182010-03-05 14:58:26 +01005646 while (ptr < len && ptr < bsize) {
5647 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005648 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005649 if (out->len > end - 2)
5650 break;
5651 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005652 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005653 if (out->len > end - 3)
5654 break;
5655 out->str[out->len++] = '\\';
5656 switch (c) {
5657 case '\t': c = 't'; break;
5658 case '\n': c = 'n'; break;
5659 case '\r': c = 'r'; break;
5660 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005661 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005662 }
5663 out->str[out->len++] = c;
5664 } else {
5665 if (out->len > end - 5)
5666 break;
5667 out->str[out->len++] = '\\';
5668 out->str[out->len++] = 'x';
5669 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5670 out->str[out->len++] = hextab[c & 0xF];
5671 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005672 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005673 /* we had a line break, let's return now */
5674 out->str[out->len++] = '\n';
5675 *line = ptr;
5676 return ptr;
5677 }
5678 }
5679 /* we have an incomplete line, we return it as-is */
5680 out->str[out->len++] = '\n';
5681 return ptr;
5682}
5683
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005684/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005685 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005686 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005687 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005688static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005689{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005690 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005691 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005692
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005693 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005694 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005695
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005696 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005697
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005698 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005699 /* the function had not been called yet, let's prepare the
5700 * buffer for a response.
5701 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005702 struct tm tm;
5703
5704 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005705 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005706 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5707 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5708 error_snapshot_id);
5709
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005710 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005711 /* Socket buffer full. Let's try again later from the same point */
5712 return 0;
5713 }
5714
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005715 appctx->ctx.errors.px = proxy;
5716 appctx->ctx.errors.buf = 0;
5717 appctx->ctx.errors.bol = 0;
5718 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005719 }
5720
5721 /* we have two inner loops here, one for the proxy, the other one for
5722 * the buffer.
5723 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005724 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005725 struct error_snapshot *es;
5726
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005727 if (appctx->ctx.errors.buf == 0)
5728 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005729 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005730 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005731
5732 if (!es->when.tv_sec)
5733 goto next;
5734
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005735 if (appctx->ctx.errors.iid >= 0 &&
5736 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5737 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005738 goto next;
5739
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005740 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005741 /* just print headers now */
5742
5743 char pn[INET6_ADDRSTRLEN];
5744 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005745 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005746
5747 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005748 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005749 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005750 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005751
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005752 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5753 case AF_INET:
5754 case AF_INET6:
5755 port = get_host_port(&es->src);
5756 break;
5757 default:
5758 port = 0;
5759 }
5760
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005761 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005762 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005763 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005764 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005765 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005766 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005767 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5768 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005769 break;
5770 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005771 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005772 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005773 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005774 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005775 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005776 break;
5777 }
5778
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005779 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005780 ", server %s (#%d), event #%u\n"
5781 " src %s:%d, session #%d, session flags 0x%08x\n"
5782 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5783 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5784 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5785 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5786 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5787 es->ev_id,
5788 pn, port, es->sid, es->s_flags,
5789 es->state, es->m_flags, es->t_flags,
5790 es->m_clen, es->m_blen,
5791 es->b_flags, es->b_out, es->b_tot,
5792 es->len, es->b_wrap, es->pos);
5793
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005794 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005795 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005796 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005797 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005798 appctx->ctx.errors.ptr = 0;
5799 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005800 }
5801
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005802 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005803 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005804 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005805 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005806 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005807 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005808 goto next;
5809 }
5810
5811 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005812 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005813 int newptr;
5814 int newline;
5815
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005816 newline = appctx->ctx.errors.bol;
5817 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5818 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005819 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005820
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005821 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005822 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005823 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005824 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005825 appctx->ctx.errors.ptr = newptr;
5826 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005827 };
5828 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005829 appctx->ctx.errors.bol = 0;
5830 appctx->ctx.errors.ptr = -1;
5831 appctx->ctx.errors.buf++;
5832 if (appctx->ctx.errors.buf > 1) {
5833 appctx->ctx.errors.buf = 0;
5834 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005835 }
5836 }
5837
5838 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005839 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005840}
5841
Willy Tarreaud5781202012-09-22 19:32:35 +02005842/* parse the "level" argument on the bind lines */
5843static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5844{
5845 if (!*args[cur_arg + 1]) {
5846 memprintf(err, "'%s' : missing level", args[cur_arg]);
5847 return ERR_ALERT | ERR_FATAL;
5848 }
5849
5850 if (!strcmp(args[cur_arg+1], "user"))
5851 conf->level = ACCESS_LVL_USER;
5852 else if (!strcmp(args[cur_arg+1], "operator"))
5853 conf->level = ACCESS_LVL_OPER;
5854 else if (!strcmp(args[cur_arg+1], "admin"))
5855 conf->level = ACCESS_LVL_ADMIN;
5856 else {
5857 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5858 args[cur_arg], args[cur_arg+1]);
5859 return ERR_ALERT | ERR_FATAL;
5860 }
5861
5862 return 0;
5863}
5864
Willy Tarreaub24281b2011-02-13 13:16:36 +01005865struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005866 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005867 .name = "<STATS>", /* used for logging */
5868 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005869 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005870};
5871
Simon Horman9bd2c732011-06-15 15:18:44 +09005872static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005873 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005874 .name = "<CLI>", /* used for logging */
5875 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005876 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005877};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005878
Willy Tarreaudc13c112013-06-21 23:16:39 +02005879static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005880 { CFG_GLOBAL, "stats", stats_parse_global },
5881 { 0, NULL, NULL },
5882}};
5883
Willy Tarreaud5781202012-09-22 19:32:35 +02005884static struct bind_kw_list bind_kws = { "STAT", { }, {
5885 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5886 { NULL, NULL, 0 },
5887}};
5888
Willy Tarreau10522fd2008-07-09 20:12:41 +02005889__attribute__((constructor))
5890static void __dumpstats_module_init(void)
5891{
5892 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005893 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005894}
5895
Willy Tarreau91861262007-10-17 17:06:05 +02005896/*
5897 * Local variables:
5898 * c-indent-level: 8
5899 * c-basic-offset: 8
5900 * End:
5901 */