blob: 57a9fc51fb000387c92a87b43759b4af56e6d48c [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 */
75 STAT_CLI_O_INFO, /* dump info */
76 STAT_CLI_O_SESS, /* dump sessions */
77 STAT_CLI_O_ERR, /* dump errors */
78 STAT_CLI_O_TAB, /* dump tables */
79 STAT_CLI_O_CLR, /* clear tables */
80 STAT_CLI_O_SET, /* set entries in tables */
81 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010082 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
83 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010084 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010085 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010086};
87
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010088static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +010089static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010090static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090091static int stats_dump_sess_to_buffer(struct stream_interface *si);
92static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010093static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010094static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
95static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010096static int stats_pats_list(struct stream_interface *si);
97static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010098static int stats_map_lookup(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +090099
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100100/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100101 * cli_io_handler()
102 * -> stats_dump_sess_to_buffer() // "show sess"
103 * -> stats_dump_errors_to_buffer() // "show errors"
104 * -> stats_dump_info_to_buffer() // "show info"
105 * -> stats_dump_stat_to_buffer() // "show stat"
106 * -> stats_dump_csv_header()
107 * -> stats_dump_proxy_to_buffer()
108 * -> stats_dump_fe_stats()
109 * -> stats_dump_li_stats()
110 * -> stats_dump_sv_stats()
111 * -> stats_dump_be_stats()
112 *
113 * http_stats_io_handler()
114 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
115 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
116 * -> stats_dump_html_head() // emits the HTML headers
117 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
118 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
119 * -> stats_dump_html_px_hdr()
120 * -> stats_dump_fe_stats()
121 * -> stats_dump_li_stats()
122 * -> stats_dump_sv_stats()
123 * -> stats_dump_be_stats()
124 * -> stats_dump_html_px_end()
125 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100126 */
127
Simon Horman9bd2c732011-06-15 15:18:44 +0900128static struct si_applet cli_applet;
129
130static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200131 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200132 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200133 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200134 " help : this message\n"
135 " prompt : toggle interactive mode with prompt\n"
136 " quit : disconnect\n"
137 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100138 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200139 " show stat : report counters for each proxy and server\n"
140 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100141 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200142 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200143 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200144 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200145 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100146 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200147 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200148 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200149 " disable : put a server or frontend in maintenance mode\n"
150 " enable : re-enable a server or frontend which is in maintenance mode\n"
151 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100152 " show acl [id] : report avalaible acls or dump an acl's contents\n"
153 " get acl : reports the patterns matching a sample for an ACL\n"
154 " add acl : add acl entry\n"
155 " del acl : delete acl entry\n"
156 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100157 " show map [id] : report avalaible maps or dump a map's contents\n"
158 " get map : reports the keys and values matching a sample for a map\n"
159 " set map : modify map entry\n"
160 " add map : add map entry\n"
161 " del map : delete map entry\n"
162 " clear map <id> : clear the content of this map\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200163 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200164
Simon Horman9bd2c732011-06-15 15:18:44 +0900165static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200166 "Permission denied\n"
167 "";
168
Willy Tarreau295a8372011-03-10 11:25:07 +0100169/* data transmission states for the stats responses */
170enum {
171 STAT_ST_INIT = 0,
172 STAT_ST_HEAD,
173 STAT_ST_INFO,
174 STAT_ST_LIST,
175 STAT_ST_END,
176 STAT_ST_FIN,
177};
178
179/* data transmission states for the stats responses inside a proxy */
180enum {
181 STAT_PX_ST_INIT = 0,
182 STAT_PX_ST_TH,
183 STAT_PX_ST_FE,
184 STAT_PX_ST_LI,
185 STAT_PX_ST_SV,
186 STAT_PX_ST_BE,
187 STAT_PX_ST_END,
188 STAT_PX_ST_FIN,
189};
190
Cyril Bonté19979e12012-04-04 12:57:21 +0200191extern const char *stat_status_codes[];
192
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200193/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200194 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200195 * needs to be closed and ignored, or a negative value upon critical failure.
196 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900197static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200198{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100199 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100200 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200201
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200202 tv_zero(&s->logs.tv_request);
203 s->logs.t_queue = 0;
204 s->logs.t_connect = 0;
205 s->logs.t_data = 0;
206 s->logs.t_close = 0;
207 s->logs.bytes_in = s->logs.bytes_out = 0;
208 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
209 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200210
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200211 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200212
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200213 if (s->listener->timeout) {
214 s->req->rto = *s->listener->timeout;
215 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200216 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200217 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200218}
219
Willy Tarreau07e9e642010-08-17 21:48:17 +0200220/* allocate a new stats frontend named <name>, and return it
221 * (or NULL in case of lack of memory).
222 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200223static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200224{
225 struct proxy *fe;
226
227 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
228 if (!fe)
229 return NULL;
230
Willy Tarreau237250c2011-07-29 01:49:03 +0200231 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200232 fe->next = proxy;
233 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200234 fe->last_change = now.tv_sec;
235 fe->id = strdup("GLOBAL");
236 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200237 fe->maxconn = 10; /* default to 10 concurrent connections */
238 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200239 fe->conf.file = strdup(file);
240 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200241 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200242
243 /* the stats frontend is the only one able to assign ID #0 */
244 fe->conf.id.key = fe->uuid = 0;
245 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200246 return fe;
247}
248
Willy Tarreaufbee7132007-10-18 13:53:22 +0200249/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200250 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
251 * error message into the <err> buffer which will be preallocated. The trailing
252 * '\n' must not be written. The function must be called with <args> pointing to
253 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200254 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200255static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200256 struct proxy *defpx, const char *file, int line,
257 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200258{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200259 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200260 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200261
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200262 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200263 int cur_arg;
264
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200265 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200266 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 +0200267 return -1;
268 }
269
Willy Tarreau89a63132009-08-16 17:41:45 +0200270 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200271 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200272 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200273 return -1;
274 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200275 }
276
Willy Tarreau4348fad2012-09-20 16:48:07 +0200277 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200278 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200279
Willy Tarreauc53d4222012-09-20 20:19:28 +0200280 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
281 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
282 file, line, args[0], args[1], err && *err ? *err : "error");
283 return -1;
284 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200285
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200286 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200287 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200288 static int bind_dumped;
289 struct bind_kw *kw;
290
291 kw = bind_find_kw(args[cur_arg]);
292 if (kw) {
293 if (!kw->parse) {
294 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
295 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200296 return -1;
297 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200298
299 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
300 if (err && *err)
301 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
302 else
303 memprintf(err, "'%s %s' : error encountered while processing '%s'",
304 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200305 return -1;
306 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200307
308 cur_arg += 1 + kw->skip;
309 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200310 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200311
312 if (!bind_dumped) {
313 bind_dump_kws(err);
314 indent_msg(err, 4);
315 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200316 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200317
318 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
319 args[0], args[1], args[cur_arg],
320 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
321 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200322 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100323
Willy Tarreauc53d4222012-09-20 20:19:28 +0200324 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
325 l->maxconn = global.stats_fe->maxconn;
326 l->backlog = global.stats_fe->backlog;
327 l->timeout = &global.stats_fe->timeout.client;
328 l->accept = session_accept;
329 l->handler = process_session;
330 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
331 l->nice = -64; /* we want to boost priority for local stats */
332 global.maxsock += l->maxconn;
333 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200334 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200335 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100336 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200337 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100338
339 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200340 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100341 return -1;
342 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200343
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100344 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200345 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200346 return -1;
347 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200348 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200349 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200350 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200351 return -1;
352 }
353 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200354 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200355 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200356 else if (!strcmp(args[1], "maxconn")) {
357 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200358
359 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200360 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200361 return -1;
362 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200363
364 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 Tarreauc2adf8b2011-09-07 12:13:34 +0200367 return -1;
368 }
369 }
370 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200371 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200372 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
373 int cur_arg = 2;
374 unsigned int set = 0;
375
Willy Tarreau91319572013-04-20 09:48:50 +0200376 if (!global.stats_fe) {
377 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
378 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
379 return -1;
380 }
381 }
382
Willy Tarreau35b7b162012-10-22 23:17:18 +0200383 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100384 unsigned int low, high;
385
Willy Tarreau35b7b162012-10-22 23:17:18 +0200386 if (strcmp(args[cur_arg], "all") == 0) {
387 set = 0;
388 break;
389 }
390 else if (strcmp(args[cur_arg], "odd") == 0) {
391 set |= 0x55555555;
392 }
393 else if (strcmp(args[cur_arg], "even") == 0) {
394 set |= 0xAAAAAAAA;
395 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100396 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100397 char *dash = strchr(args[cur_arg], '-');
398
399 low = high = str2uic(args[cur_arg]);
400 if (dash)
401 high = str2uic(dash + 1);
402
403 if (high < low) {
404 unsigned int swap = low;
405 low = high;
406 high = swap;
407 }
408
409 if (low < 1 || high > 32) {
410 memprintf(err, "'%s %s' supports process numbers from 1 to 32.\n",
411 args[0], args[1]);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200412 return -1;
413 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100414
415 while (low <= high)
416 set |= 1 << (low++ - 1);
417 }
418 else {
419 memprintf(err,
420 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to 32.\n",
421 args[0], args[1]);
422 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200423 }
424 cur_arg++;
425 }
426 global.stats_fe->bind_proc = set;
427 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200428 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200429 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200430 return -1;
431 }
432 return 0;
433}
434
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100435/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
436 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100437 * NOTE: Some tools happen to rely on the field position instead of its name,
438 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100439 */
440static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100441{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100442 chunk_appendf(&trash,
443 "# pxname,svname,"
444 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100445 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100446 "bin,bout,"
447 "dreq,dresp,"
448 "ereq,econ,eresp,"
449 "wretr,wredis,"
450 "status,weight,act,bck,"
451 "chkfail,chkdown,lastchg,downtime,qlimit,"
452 "pid,iid,sid,throttle,lbtot,tracked,type,"
453 "rate,rate_lim,rate_max,"
454 "check_status,check_code,check_duration,"
455 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
456 "req_rate,req_rate_max,req_tot,"
457 "cli_abrt,srv_abrt,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100458 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100459 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100460}
461
Simon Hormand9366582011-06-15 15:18:45 +0900462/* print a string of text buffer to <out>. The format is :
463 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
464 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
465 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
466 */
467static int dump_text(struct chunk *out, const char *buf, int bsize)
468{
469 unsigned char c;
470 int ptr = 0;
471
472 while (buf[ptr] && ptr < bsize) {
473 c = buf[ptr];
474 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
475 if (out->len > out->size - 1)
476 break;
477 out->str[out->len++] = c;
478 }
479 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
480 if (out->len > out->size - 2)
481 break;
482 out->str[out->len++] = '\\';
483 switch (c) {
484 case ' ': c = ' '; break;
485 case '\t': c = 't'; break;
486 case '\n': c = 'n'; break;
487 case '\r': c = 'r'; break;
488 case '\e': c = 'e'; break;
489 case '\\': c = '\\'; break;
490 }
491 out->str[out->len++] = c;
492 }
493 else {
494 if (out->len > out->size - 4)
495 break;
496 out->str[out->len++] = '\\';
497 out->str[out->len++] = 'x';
498 out->str[out->len++] = hextab[(c >> 4) & 0xF];
499 out->str[out->len++] = hextab[c & 0xF];
500 }
501 ptr++;
502 }
503
504 return ptr;
505}
506
507/* print a buffer in hexa.
508 * Print stopped if <bsize> is reached, or if no more place in the chunk.
509 */
510static int dump_binary(struct chunk *out, const char *buf, int bsize)
511{
512 unsigned char c;
513 int ptr = 0;
514
515 while (ptr < bsize) {
516 c = buf[ptr];
517
518 if (out->len > out->size - 2)
519 break;
520 out->str[out->len++] = hextab[(c >> 4) & 0xF];
521 out->str[out->len++] = hextab[c & 0xF];
522
523 ptr++;
524 }
525 return ptr;
526}
527
528/* Dump the status of a table to a stream interface's
529 * read buffer. It returns 0 if the output buffer is full
530 * and needs to be called again, otherwise non-zero.
531 */
532static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
533 struct proxy *proxy, struct proxy *target)
534{
Willy Tarreau306f8302013-07-08 15:53:06 +0200535 struct session *s = session_from_task(si->owner);
Simon Hormand9366582011-06-15 15:18:45 +0900536
Willy Tarreau77804732012-10-29 16:14:26 +0100537 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900538 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
539
540 /* any other information should be dumped here */
541
Willy Tarreau290e63a2012-09-20 18:07:14 +0200542 if (target && s->listener->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100543 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900544
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200545 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900546 return 0;
547
548 return 1;
549}
550
551/* Dump the a table entry to a stream interface's
552 * read buffer. It returns 0 if the output buffer is full
553 * and needs to be called again, otherwise non-zero.
554 */
555static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
556 struct proxy *proxy, struct stksess *entry)
557{
558 int dt;
559
Willy Tarreau77804732012-10-29 16:14:26 +0100560 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900561
562 if (proxy->table.type == STKTABLE_TYPE_IP) {
563 char addr[INET_ADDRSTRLEN];
564 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100565 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900566 }
567 else if (proxy->table.type == STKTABLE_TYPE_IPV6) {
568 char addr[INET6_ADDRSTRLEN];
569 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100570 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900571 }
572 else if (proxy->table.type == STKTABLE_TYPE_INTEGER) {
Willy Tarreau77804732012-10-29 16:14:26 +0100573 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900574 }
575 else if (proxy->table.type == STKTABLE_TYPE_STRING) {
Willy Tarreau77804732012-10-29 16:14:26 +0100576 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900577 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
578 }
579 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100580 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900581 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
582 }
583
Willy Tarreau77804732012-10-29 16:14:26 +0100584 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900585
586 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
587 void *ptr;
588
589 if (proxy->table.data_ofs[dt] == 0)
590 continue;
591 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100592 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900593 else
Willy Tarreau77804732012-10-29 16:14:26 +0100594 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900595
596 ptr = stktable_data_ptr(&proxy->table, entry, dt);
597 switch (stktable_data_types[dt].std_type) {
598 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100599 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900600 break;
601 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100602 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900603 break;
604 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100605 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900606 break;
607 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100608 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900609 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
610 proxy->table.data_arg[dt].u));
611 break;
612 }
613 }
Willy Tarreau77804732012-10-29 16:14:26 +0100614 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900615
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200616 if (bi_putchk(si->ib, msg) == -1)
Simon Hormand9366582011-06-15 15:18:45 +0900617 return 0;
618
619 return 1;
620}
621
Willy Tarreaudec98142012-06-06 23:37:08 +0200622static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900623{
Willy Tarreau306f8302013-07-08 15:53:06 +0200624 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100625 struct appctx *appctx = __objt_appctx(si->end);
626 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900627 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900628 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900629 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200630 long long value;
631 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200632 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200633 void *ptr;
634 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900635
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100636 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900637
638 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100639 appctx->ctx.cli.msg = "Key value expected\n";
640 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900641 return;
642 }
643
Simon Hormanc5b89f62011-06-15 15:18:50 +0900644 switch (px->table.type) {
645 case STKTABLE_TYPE_IP:
Simon Hormancec9a222011-06-15 15:18:51 +0900646 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100647 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900648 break;
649 case STKTABLE_TYPE_IPV6:
650 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100651 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900652 break;
Simon Hormancec9a222011-06-15 15:18:51 +0900653 case STKTABLE_TYPE_INTEGER:
654 {
655 char *endptr;
656 unsigned long val;
657 errno = 0;
658 val = strtoul(args[4], &endptr, 10);
659 if ((errno == ERANGE && val == ULONG_MAX) ||
660 (errno != 0 && val == 0) || endptr == args[4] ||
661 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100662 appctx->ctx.cli.msg = "Invalid key\n";
663 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900664 return;
665 }
666 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100667 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900668 break;
669 }
670 break;
Simon Horman619e3cc2011-06-15 15:18:52 +0900671 case STKTABLE_TYPE_STRING:
Willy Tarreau07115412012-10-29 21:56:59 +0100672 static_table_key->key = args[4];
673 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900674 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900675 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200676 switch (action) {
677 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100678 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 +0200679 break;
680 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100681 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 +0200682 break;
683 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100684 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200685 break;
686 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100687 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900688 return;
689 }
690
691 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +0200692 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100693 appctx->ctx.cli.msg = stats_permission_denied_msg;
694 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900695 return;
696 }
697
Willy Tarreau07115412012-10-29 21:56:59 +0100698 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900699
Willy Tarreaudec98142012-06-06 23:37:08 +0200700 switch (action) {
701 case STAT_CLI_O_TAB:
702 if (!ts)
703 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100704 chunk_reset(&trash);
705 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900706 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100707 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900708 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200709
710 case STAT_CLI_O_CLR:
711 if (!ts)
712 return;
713 if (ts->ref_cnt) {
714 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100715 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
716 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200717 return;
718 }
719 stksess_kill(&px->table, ts);
720 break;
Simon Horman17bce342011-06-15 15:18:47 +0900721
Willy Tarreau654694e2012-06-07 01:03:16 +0200722 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200723 if (ts)
724 stktable_touch(&px->table, ts, 1);
725 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100726 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200727 if (!ts) {
728 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100729 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
730 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200731 return;
732 }
733 stktable_store(&px->table, ts, 1);
734 }
735
Willy Tarreau47060b62013-08-01 21:11:42 +0200736 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
737 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100738 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
739 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200740 return;
741 }
742
743 data_type = stktable_get_data_type(args[cur_arg] + 5);
744 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100745 appctx->ctx.cli.msg = "Unknown data type\n";
746 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200747 return;
748 }
749
750 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100751 appctx->ctx.cli.msg = "Data type not stored in this table\n";
752 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200753 return;
754 }
755
756 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100757 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
758 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200759 return;
760 }
761
762 ptr = stktable_data_ptr(&px->table, ts, data_type);
763
764 switch (stktable_data_types[data_type].std_type) {
765 case STD_T_SINT:
766 stktable_data_cast(ptr, std_t_sint) = value;
767 break;
768 case STD_T_UINT:
769 stktable_data_cast(ptr, std_t_uint) = value;
770 break;
771 case STD_T_ULL:
772 stktable_data_cast(ptr, std_t_ull) = value;
773 break;
774 case STD_T_FRQP:
775 /* We set both the current and previous values. That way
776 * the reported frequency is stable during all the period
777 * then slowly fades out. This allows external tools to
778 * push measures without having to update them too often.
779 */
780 frqp = &stktable_data_cast(ptr, std_t_frqp);
781 frqp->curr_tick = now_ms;
782 frqp->prev_ctr = 0;
783 frqp->curr_ctr = value;
784 break;
785 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200786 }
787 break;
788
Willy Tarreaudec98142012-06-06 23:37:08 +0200789 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100790 appctx->ctx.cli.msg = "Unknown action\n";
791 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200792 break;
Simon Horman121f3052011-06-15 15:18:46 +0900793 }
Simon Horman121f3052011-06-15 15:18:46 +0900794}
795
Willy Tarreau654694e2012-06-07 01:03:16 +0200796static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900797{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100798 struct appctx *appctx = __objt_appctx(si->end);
799
Willy Tarreau04b3a192013-04-13 09:41:37 +0200800 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100801 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
802 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200803 return;
804 }
805
Simon Hormand5b9fd92011-06-15 15:18:48 +0900806 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100807 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
808 if (appctx->ctx.table.data_type < 0) {
809 appctx->ctx.cli.msg = "Unknown data type\n";
810 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900811 return;
812 }
813
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100814 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
815 appctx->ctx.cli.msg = "Data type not stored in this table\n";
816 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900817 return;
818 }
819
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100820 appctx->ctx.table.data_op = get_std_op(args[4]);
821 if (appctx->ctx.table.data_op < 0) {
822 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
823 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900824 return;
825 }
826
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100827 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
828 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
829 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900830 return;
831 }
832}
833
Willy Tarreaudec98142012-06-06 23:37:08 +0200834static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900835{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100836 struct appctx *appctx = __objt_appctx(si->end);
837
838 appctx->ctx.table.data_type = -1;
839 appctx->st2 = STAT_ST_INIT;
840 appctx->ctx.table.target = NULL;
841 appctx->ctx.table.proxy = NULL;
842 appctx->ctx.table.entry = NULL;
843 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900844
845 if (*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100846 appctx->ctx.table.target = find_stktable(args[2]);
847 if (!appctx->ctx.table.target) {
848 appctx->ctx.cli.msg = "No such table\n";
849 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900850 return;
851 }
852 }
853 else {
Willy Tarreaudec98142012-06-06 23:37:08 +0200854 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900855 goto err_args;
856 return;
857 }
858
859 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +0200860 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900861 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +0200862 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +0900863 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +0900864 goto err_args;
865
866 return;
867
868err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +0200869 switch (action) {
870 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100871 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 +0200872 break;
873 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100874 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 +0200875 break;
876 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100877 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200878 break;
879 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100880 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900881}
882
Willy Tarreau532a4502011-09-07 22:37:44 +0200883/* Expects to find a frontend named <arg> and returns it, otherwise displays various
884 * adequate error messages and returns NULL. This function also expects the session
885 * level to be admin.
886 */
887static struct proxy *expect_frontend_admin(struct session *s, struct stream_interface *si, const char *arg)
888{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100889 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +0200890 struct proxy *px;
891
Willy Tarreau290e63a2012-09-20 18:07:14 +0200892 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100893 appctx->ctx.cli.msg = stats_permission_denied_msg;
894 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200895 return NULL;
896 }
897
898 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100899 appctx->ctx.cli.msg = "A frontend name is expected.\n";
900 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200901 return NULL;
902 }
903
904 px = findproxy(arg, PR_CAP_FE);
905 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100906 appctx->ctx.cli.msg = "No such frontend.\n";
907 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +0200908 return NULL;
909 }
910 return px;
911}
912
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200913/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
914 * and returns the pointer to the server. Otherwise, display adequate error messages
915 * and returns NULL. This function also expects the session level to be admin. Note:
916 * the <arg> is modified to remove the '/'.
917 */
918static struct server *expect_server_admin(struct session *s, struct stream_interface *si, char *arg)
919{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100920 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200921 struct proxy *px;
922 struct server *sv;
923 char *line;
924
Willy Tarreau290e63a2012-09-20 18:07:14 +0200925 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100926 appctx->ctx.cli.msg = stats_permission_denied_msg;
927 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200928 return NULL;
929 }
930
931 /* split "backend/server" and make <line> point to server */
932 for (line = arg; *line; line++)
933 if (*line == '/') {
934 *line++ = '\0';
935 break;
936 }
937
938 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100939 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
940 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200941 return NULL;
942 }
943
944 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100945 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
946 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200947 return NULL;
948 }
949
950 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100951 appctx->ctx.cli.msg = "Proxy is disabled.\n";
952 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +0200953 return NULL;
954 }
955
956 return sv;
957}
958
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100959/* This function is used with map and acl management. It permits to browse
960 * each reference. The variable <getnext> must contain the current node,
961 * <end> point to the root node and the <flags> permit to filter required
962 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100963 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100964static inline
965struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
966 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100967{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100968 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100969
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100970 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100971
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100972 /* Get next list entry. */
973 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100974
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100975 /* If the entry is the last of the list, return NULL. */
976 if (&ref->list == end)
977 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100978
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100979 /* If the entry match the flag, return it. */
980 if (ref->flags & flags)
981 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100982 }
983}
984
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100985/* This function is used with map and acl management. It permits to browse
986 * each reference.
987 */
988static inline
989struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
990{
991 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +0100992 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
993 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100994 return NULL;
995 return expr;
996}
997
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200998/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +0200999 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001000 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001001 * designating the function which will have to process the request, which can
1002 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001003 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001004static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001005{
Willy Tarreau306f8302013-07-08 15:53:06 +02001006 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001007 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001008 char *args[MAX_STATS_ARGS + 1];
1009 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001010 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001011
1012 while (isspace((unsigned char)*line))
1013 line++;
1014
1015 arg = 0;
1016 args[arg] = line;
1017
1018 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001019 if (*line == '\\') {
1020 line++;
1021 if (*line == '\0')
1022 break;
1023 }
1024 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001025 *line++ = '\0';
1026
1027 while (isspace((unsigned char)*line))
1028 line++;
1029
1030 args[++arg] = line;
1031 continue;
1032 }
1033
1034 line++;
1035 }
1036
1037 while (++arg <= MAX_STATS_ARGS)
1038 args[arg] = line;
1039
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001040 /* remove \ */
1041 arg = 0;
1042 while (*args[arg] != '\0') {
1043 j = 0;
1044 for (i=0; args[arg][i] != '\0'; i++) {
1045 if (args[arg][i] == '\\')
1046 continue;
1047 args[arg][j] = args[arg][i];
1048 j++;
1049 }
1050 args[arg][j] = '\0';
1051 arg++;
1052 }
1053
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001054 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001055 if (strcmp(args[0], "show") == 0) {
1056 if (strcmp(args[1], "stat") == 0) {
1057 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001058 appctx->ctx.stats.flags |= STAT_BOUND;
1059 appctx->ctx.stats.iid = atoi(args[2]);
1060 appctx->ctx.stats.type = atoi(args[3]);
1061 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001062 }
1063
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001064 appctx->st2 = STAT_ST_INIT;
1065 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001066 }
1067 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001068 appctx->st2 = STAT_ST_INIT;
1069 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001070 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001071 else if (strcmp(args[1], "pools") == 0) {
1072 appctx->st2 = STAT_ST_INIT;
1073 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1074 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001075 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001076 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001077 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001078 appctx->ctx.cli.msg = stats_permission_denied_msg;
1079 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001080 return 1;
1081 }
Willy Tarreau76153662012-11-26 01:16:39 +01001082 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001083 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001084 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001085 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001086 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001087 appctx->ctx.sess.target = NULL;
1088 appctx->ctx.sess.section = 0; /* start with session status */
1089 appctx->ctx.sess.pos = 0;
1090 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001091 }
1092 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001093 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001094 appctx->ctx.cli.msg = stats_permission_denied_msg;
1095 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001096 return 1;
1097 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001098 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001099 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001100 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001101 appctx->ctx.errors.iid = -1;
1102 appctx->ctx.errors.px = NULL;
1103 appctx->st2 = STAT_ST_INIT;
1104 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001105 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001106 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001107 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001108 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001109 else if (strcmp(args[1], "map") == 0 ||
1110 strcmp(args[1], "acl") == 0) {
1111
1112 /* Set ACL or MAP flags. */
1113 if (args[1][0] == 'm')
1114 appctx->ctx.map.display_flags = PAT_REF_MAP;
1115 else
1116 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001117
1118 /* no parameter: display all map avalaible */
1119 if (!*args[2]) {
1120 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001121 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001122 return 1;
1123 }
1124
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001125 /* lookup into the refs and check the map flag */
1126 appctx->ctx.map.ref = pat_ref_lookup(args[2]);
1127 if (!appctx->ctx.map.ref ||
1128 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1129 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1130 appctx->ctx.cli.msg = "Unknown map identifier. Please use <name>.\n";
1131 else
1132 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001133 appctx->st0 = STAT_CLI_PRINT;
1134 return 1;
1135 }
1136 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001137 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001138 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001139 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001140 return 0;
1141 }
1142 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001143 else if (strcmp(args[0], "clear") == 0) {
1144 if (strcmp(args[1], "counters") == 0) {
1145 struct proxy *px;
1146 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001147 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001148 int clrall = 0;
1149
1150 if (strcmp(args[2], "all") == 0)
1151 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001152
Willy Tarreau6162db22009-10-10 17:13:00 +02001153 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001154 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1155 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001156 appctx->ctx.cli.msg = stats_permission_denied_msg;
1157 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001158 return 1;
1159 }
1160
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001161 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001162 if (clrall) {
1163 memset(&px->be_counters, 0, sizeof(px->be_counters));
1164 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1165 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001166 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001167 px->be_counters.conn_max = 0;
1168 px->be_counters.p.http.rps_max = 0;
1169 px->be_counters.sps_max = 0;
1170 px->be_counters.cps_max = 0;
1171 px->be_counters.nbpend_max = 0;
1172
1173 px->fe_counters.conn_max = 0;
1174 px->fe_counters.p.http.rps_max = 0;
1175 px->fe_counters.sps_max = 0;
1176 px->fe_counters.cps_max = 0;
1177 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001178 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001179
1180 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001181 if (clrall)
1182 memset(&sv->counters, 0, sizeof(sv->counters));
1183 else {
1184 sv->counters.cur_sess_max = 0;
1185 sv->counters.nbpend_max = 0;
1186 sv->counters.sps_max = 0;
1187 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001188
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001189 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001190 if (li->counters) {
1191 if (clrall)
1192 memset(li->counters, 0, sizeof(*li->counters));
1193 else
1194 li->counters->conn_max = 0;
1195 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001196 }
1197
Willy Tarreau81c25d02011-09-07 15:17:21 +02001198 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001199 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001200 return 1;
1201 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001202 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001203 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001204 /* end of processing */
1205 return 1;
1206 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001207 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1208 /* Set ACL or MAP flags. */
1209 if (args[1][0] == 'm')
1210 appctx->ctx.map.display_flags = PAT_REF_MAP;
1211 else
1212 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001213
1214 /* no parameter */
1215 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001216 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1217 appctx->ctx.cli.msg = "Missing map identifier.\n";
1218 else
1219 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001220 appctx->st0 = STAT_CLI_PRINT;
1221 return 1;
1222 }
1223
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001224 /* lookup into the refs and check the map flag */
1225 appctx->ctx.map.ref = pat_ref_lookup(args[2]);
1226 if (!appctx->ctx.map.ref ||
1227 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1228 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1229 appctx->ctx.cli.msg = "Unknown map identifier. Please use <name>.\n";
1230 else
1231 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001232 appctx->st0 = STAT_CLI_PRINT;
1233 return 1;
1234 }
1235
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001236 /* Clear all. */
1237 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001238
1239 /* return response */
1240 appctx->ctx.cli.msg = "Done.\n";
1241 appctx->st0 = STAT_CLI_PRINT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001242 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001243 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001244 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001245 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001246 return 0;
1247 }
1248 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001249 else if (strcmp(args[0], "get") == 0) {
1250 if (strcmp(args[1], "weight") == 0) {
1251 struct proxy *px;
1252 struct server *sv;
1253
1254 /* split "backend/server" and make <line> point to server */
1255 for (line = args[2]; *line; line++)
1256 if (*line == '/') {
1257 *line++ = '\0';
1258 break;
1259 }
1260
1261 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001262 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1263 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001264 return 1;
1265 }
1266
1267 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001268 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1269 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001270 return 1;
1271 }
1272
1273 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001274 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1275 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001276 return 1;
1277 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001278 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1279 /* Set flags. */
1280 if (args[1][0] == 'm')
1281 appctx->ctx.map.display_flags = PAT_REF_MAP;
1282 else
1283 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001284
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001285 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001286 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001287 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1288 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1289 else
1290 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001291 appctx->st0 = STAT_CLI_PRINT;
1292 return 1;
1293 }
1294
1295 /* lookup into the maps */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001296 appctx->ctx.map.ref = pat_ref_lookup(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001297 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001298 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1299 appctx->ctx.cli.msg = "Unknown map identifier. Please use <name>.\n";
1300 else
1301 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001302 appctx->st0 = STAT_CLI_PRINT;
1303 return 1;
1304 }
1305
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001306 /* copy input string. The string must be allocated because
1307 * it may be used over multiple iterations. It's released
1308 * at the end and upon abort anyway.
1309 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001310 appctx->ctx.map.chunk.len = strlen(args[3]);
1311 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1312 appctx->ctx.map.chunk.str = strdup(args[3]);
1313 if (!appctx->ctx.map.chunk.str) {
1314 appctx->ctx.cli.msg = "Out of memory error.\n";
1315 appctx->st0 = STAT_CLI_PRINT;
1316 return 1;
1317 }
1318
1319 /* prepare response */
1320 appctx->st2 = STAT_ST_INIT;
1321 appctx->st0 = STAT_CLI_O_MLOOK;
1322 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001323 else { /* not "get weight" */
1324 return 0;
1325 }
1326 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001327 else if (strcmp(args[0], "set") == 0) {
1328 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001329 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001330 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001331
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001332 sv = expect_server_admin(s, si, args[2]);
1333 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001334 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001335
Simon Horman7d09b9a2013-02-12 10:45:51 +09001336 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001337 /*
1338 * The user-weight may now be zero and thus
1339 * the server considered to be draining.
1340 * Update the server's drain state as necessary.
1341 */
1342 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001343 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001344 appctx->ctx.cli.msg = warning;
1345 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001346 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001347 return 1;
1348 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001349 else if (strcmp(args[1], "timeout") == 0) {
1350 if (strcmp(args[2], "cli") == 0) {
1351 unsigned timeout;
1352 const char *res;
1353
1354 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001355 appctx->ctx.cli.msg = "Expects an integer value.\n";
1356 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001357 return 1;
1358 }
1359
1360 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1361 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001362 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1363 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001364 return 1;
1365 }
1366
1367 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1368 return 1;
1369 }
1370 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001371 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1372 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001373 return 1;
1374 }
1375 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001376 else if (strcmp(args[1], "maxconn") == 0) {
1377 if (strcmp(args[2], "frontend") == 0) {
1378 struct proxy *px;
1379 struct listener *l;
1380 int v;
1381
Willy Tarreau532a4502011-09-07 22:37:44 +02001382 px = expect_frontend_admin(s, si, args[3]);
1383 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001384 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001385
1386 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001387 appctx->ctx.cli.msg = "Integer value expected.\n";
1388 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001389 return 1;
1390 }
1391
1392 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001393 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001394 appctx->ctx.cli.msg = "Value out of range.\n";
1395 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001396 return 1;
1397 }
1398
1399 /* OK, the value is fine, so we assign it to the proxy and to all of
1400 * its listeners. The blocked ones will be dequeued.
1401 */
1402 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001403 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001404 l->maxconn = v;
1405 if (l->state == LI_FULL)
1406 resume_listener(l);
1407 }
1408
1409 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1410 dequeue_all_listeners(&s->fe->listener_queue);
1411
1412 return 1;
1413 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001414 else if (strcmp(args[2], "global") == 0) {
1415 int v;
1416
Willy Tarreau290e63a2012-09-20 18:07:14 +02001417 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001418 appctx->ctx.cli.msg = stats_permission_denied_msg;
1419 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001420 return 1;
1421 }
1422
1423 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001424 appctx->ctx.cli.msg = "Expects an integer value.\n";
1425 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001426 return 1;
1427 }
1428
1429 v = atoi(args[3]);
1430 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001431 appctx->ctx.cli.msg = "Value out of range.\n";
1432 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001433 return 1;
1434 }
1435
1436 /* check for unlimited values */
1437 if (v <= 0)
1438 v = global.hardmaxconn;
1439
1440 global.maxconn = v;
1441
1442 /* Dequeues all of the listeners waiting for a resource */
1443 if (!LIST_ISEMPTY(&global_listener_queue))
1444 dequeue_all_listeners(&global_listener_queue);
1445
1446 return 1;
1447 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001448 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001449 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1450 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001451 return 1;
1452 }
1453 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001454 else if (strcmp(args[1], "rate-limit") == 0) {
1455 if (strcmp(args[2], "connections") == 0) {
1456 if (strcmp(args[3], "global") == 0) {
1457 int v;
1458
Willy Tarreau290e63a2012-09-20 18:07:14 +02001459 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001460 appctx->ctx.cli.msg = stats_permission_denied_msg;
1461 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001462 return 1;
1463 }
1464
1465 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001466 appctx->ctx.cli.msg = "Expects an integer value.\n";
1467 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001468 return 1;
1469 }
1470
1471 v = atoi(args[4]);
1472 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001473 appctx->ctx.cli.msg = "Value out of range.\n";
1474 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001475 return 1;
1476 }
1477
1478 global.cps_lim = v;
1479
1480 /* Dequeues all of the listeners waiting for a resource */
1481 if (!LIST_ISEMPTY(&global_listener_queue))
1482 dequeue_all_listeners(&global_listener_queue);
1483
1484 return 1;
1485 }
1486 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001487 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1488 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001489 return 1;
1490 }
1491 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001492 else if (strcmp(args[2], "sessions") == 0) {
1493 if (strcmp(args[3], "global") == 0) {
1494 int v;
1495
1496 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1497 appctx->ctx.cli.msg = stats_permission_denied_msg;
1498 appctx->st0 = STAT_CLI_PRINT;
1499 return 1;
1500 }
1501
1502 if (!*args[4]) {
1503 appctx->ctx.cli.msg = "Expects an integer value.\n";
1504 appctx->st0 = STAT_CLI_PRINT;
1505 return 1;
1506 }
1507
1508 v = atoi(args[4]);
1509 if (v < 0) {
1510 appctx->ctx.cli.msg = "Value out of range.\n";
1511 appctx->st0 = STAT_CLI_PRINT;
1512 return 1;
1513 }
1514
1515 global.sps_lim = v;
1516
1517 /* Dequeues all of the listeners waiting for a resource */
1518 if (!LIST_ISEMPTY(&global_listener_queue))
1519 dequeue_all_listeners(&global_listener_queue);
1520
1521 return 1;
1522 }
1523 else {
1524 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1525 appctx->st0 = STAT_CLI_PRINT;
1526 return 1;
1527 }
1528 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001529#ifdef USE_OPENSSL
1530 else if (strcmp(args[2], "ssl-sessions") == 0) {
1531 if (strcmp(args[3], "global") == 0) {
1532 int v;
1533
1534 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1535 appctx->ctx.cli.msg = stats_permission_denied_msg;
1536 appctx->st0 = STAT_CLI_PRINT;
1537 return 1;
1538 }
1539
1540 if (!*args[4]) {
1541 appctx->ctx.cli.msg = "Expects an integer value.\n";
1542 appctx->st0 = STAT_CLI_PRINT;
1543 return 1;
1544 }
1545
1546 v = atoi(args[4]);
1547 if (v < 0) {
1548 appctx->ctx.cli.msg = "Value out of range.\n";
1549 appctx->st0 = STAT_CLI_PRINT;
1550 return 1;
1551 }
1552
1553 global.ssl_lim = v;
1554
1555 /* Dequeues all of the listeners waiting for a resource */
1556 if (!LIST_ISEMPTY(&global_listener_queue))
1557 dequeue_all_listeners(&global_listener_queue);
1558
1559 return 1;
1560 }
1561 else {
1562 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1563 appctx->st0 = STAT_CLI_PRINT;
1564 return 1;
1565 }
1566 }
1567#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001568 else if (strcmp(args[2], "http-compression") == 0) {
1569 if (strcmp(args[3], "global") == 0) {
1570 int v;
1571
Willy Tarreau85d47f92012-11-21 00:29:50 +01001572 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001573 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1574 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001575 return 1;
1576 }
1577
William Lallemandd85f9172012-11-09 17:05:39 +01001578 v = atoi(args[4]);
1579 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1580 }
1581 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001582 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1583 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001584 return 1;
1585 }
1586 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001587 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001588 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001589 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001590 return 1;
1591 }
1592 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001593 else if (strcmp(args[1], "table") == 0) {
1594 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1595 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001596 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001597 /* Set flags. */
1598 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001599
1600 /* Expect three parameters: map name, key and new value. */
1601 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001602 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001603 appctx->st0 = STAT_CLI_PRINT;
1604 return 1;
1605 }
1606
1607 /* Lookup the reference in the maps. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001608 appctx->ctx.map.ref = pat_ref_lookup(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001609 if (!appctx->ctx.map.ref) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001610 appctx->ctx.cli.msg = "Unknown map identifier. Please use <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001611 appctx->st0 = STAT_CLI_PRINT;
1612 return 1;
1613 }
1614
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001615 /* Update the value. */
1616 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4])) {
1617 appctx->ctx.cli.msg = "Pattern not found.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001618 appctx->st0 = STAT_CLI_PRINT;
1619 return 1;
1620 }
1621
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001622 /* The set is done, send message. */
1623 appctx->ctx.cli.msg = "Done.\n";
1624 appctx->st0 = STAT_CLI_PRINT;
1625 return 1;
1626 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001627 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001628 return 0;
1629 }
1630 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001631 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001632 if (strcmp(args[1], "agent") == 0) {
1633 struct server *sv;
1634
1635 sv = expect_server_admin(s, si, args[2]);
1636 if (!sv)
1637 return 1;
1638
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001639 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1640 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1641 appctx->st0 = STAT_CLI_PRINT;
1642 return 1;
1643 }
1644
1645 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001646 return 1;
1647 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001648 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001649 struct server *sv;
1650
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001651 sv = expect_server_admin(s, si, args[2]);
1652 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001653 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001654
Cyril Bontécd19e512010-01-31 22:34:03 +01001655 if (sv->state & SRV_MAINTAIN) {
1656 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001657 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001658 /* If this server tracks the status of another one,
1659 * we must restore the good status.
1660 */
Willy Tarreau44267702011-10-28 15:35:33 +02001661 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001662 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001663 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001664 } else {
1665 sv->state &= ~SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001666 sv->check.state &= ~CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001667 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001668 }
1669 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001670 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001671 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001672 }
1673 }
1674
Willy Tarreau532a4502011-09-07 22:37:44 +02001675 return 1;
1676 }
1677 else if (strcmp(args[1], "frontend") == 0) {
1678 struct proxy *px;
1679
1680 px = expect_frontend_admin(s, si, args[2]);
1681 if (!px)
1682 return 1;
1683
1684 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001685 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1686 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001687 return 1;
1688 }
1689
1690 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001691 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1692 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001693 return 1;
1694 }
1695
1696 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001697 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1698 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001699 return 1;
1700 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001701 return 1;
1702 }
1703 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001704 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1705 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001706 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001707 }
1708 }
1709 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001710 if (strcmp(args[1], "agent") == 0) {
1711 struct server *sv;
1712
1713 sv = expect_server_admin(s, si, args[2]);
1714 if (!sv)
1715 return 1;
1716
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001717 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001718 return 1;
1719 }
1720 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001721 struct server *sv;
1722
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001723 sv = expect_server_admin(s, si, args[2]);
1724 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001725 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001726
Cyril Bontécd19e512010-01-31 22:34:03 +01001727 if (! (sv->state & SRV_MAINTAIN)) {
1728 /* Not already in maintenance, we can change the server state */
1729 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001730 sv->check.state |= CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001731 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001732 }
1733
Willy Tarreau532a4502011-09-07 22:37:44 +02001734 return 1;
1735 }
1736 else if (strcmp(args[1], "frontend") == 0) {
1737 struct proxy *px;
1738
1739 px = expect_frontend_admin(s, si, args[2]);
1740 if (!px)
1741 return 1;
1742
1743 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001744 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1745 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001746 return 1;
1747 }
1748
1749 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001750 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1751 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001752 return 1;
1753 }
1754
1755 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001756 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1757 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001758 return 1;
1759 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001760 return 1;
1761 }
1762 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001763 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1764 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001765 return 1;
1766 }
1767 }
1768 else if (strcmp(args[0], "shutdown") == 0) {
1769 if (strcmp(args[1], "frontend") == 0) {
1770 struct proxy *px;
1771
1772 px = expect_frontend_admin(s, si, args[2]);
1773 if (!px)
1774 return 1;
1775
1776 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001777 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1778 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001779 return 1;
1780 }
1781
1782 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1783 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1784 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1785 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1786 stop_proxy(px);
1787 return 1;
1788 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001789 else if (strcmp(args[1], "session") == 0) {
1790 struct session *sess, *ptr;
1791
Willy Tarreau290e63a2012-09-20 18:07:14 +02001792 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001793 appctx->ctx.cli.msg = stats_permission_denied_msg;
1794 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001795 return 1;
1796 }
1797
1798 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001799 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1800 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001801 return 1;
1802 }
1803
1804 ptr = (void *)strtoul(args[2], NULL, 0);
1805
1806 /* first, look for the requested session in the session table */
1807 list_for_each_entry(sess, &sessions, list) {
1808 if (sess == ptr)
1809 break;
1810 }
1811
1812 /* do we have the session ? */
1813 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001814 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1815 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001816 return 1;
1817 }
1818
1819 session_shutdown(sess, SN_ERR_KILLED);
1820 return 1;
1821 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001822 else if (strcmp(args[1], "sessions") == 0) {
1823 if (strcmp(args[2], "server") == 0) {
1824 struct server *sv;
1825 struct session *sess, *sess_bck;
1826
1827 sv = expect_server_admin(s, si, args[3]);
1828 if (!sv)
1829 return 1;
1830
1831 /* kill all the session that are on this server */
1832 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1833 if (sess->srv_conn == sv)
1834 session_shutdown(sess, SN_ERR_KILLED);
1835
1836 return 1;
1837 }
1838 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001839 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1840 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001841 return 1;
1842 }
1843 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001844 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001845 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1846 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001847 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001848 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001849 }
1850 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001851 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1852 if (args[1][0] == 'm')
1853 appctx->ctx.map.display_flags = PAT_REF_MAP;
1854 else
1855 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001856
1857 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001858 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1859 if (!*args[2] || !*args[3]) {
1860 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
1861 appctx->st0 = STAT_CLI_PRINT;
1862 return 1;
1863 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001864 }
1865
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001866 else {
1867 if (!*args[2] || !*args[3]) {
1868 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
1869 appctx->st0 = STAT_CLI_PRINT;
1870 return 1;
1871 }
1872 }
1873
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001874 /* Lookup the reference in the maps. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001875 appctx->ctx.map.ref = pat_ref_lookup(args[2]);
1876 if (!appctx->ctx.map.ref ||
1877 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001878 appctx->ctx.cli.msg = "Unknown map identifier. Please use <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001879 appctx->st0 = STAT_CLI_PRINT;
1880 return 1;
1881 }
1882
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001883 /* Try to delete the entry. */
1884 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
1885 /* The entry is not found, send message. */
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001886 appctx->ctx.cli.msg = "Key not found.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001887 appctx->st0 = STAT_CLI_PRINT;
1888 return 1;
1889 }
1890
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001891 /* The deletion is done, send message. */
1892 appctx->ctx.cli.msg = "Done.\n";
1893 appctx->st0 = STAT_CLI_PRINT;
1894 return 1;
1895 }
1896 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001897 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001898 appctx->st0 = STAT_CLI_PRINT;
1899 return 1;
1900 }
1901 }
1902 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001903 if (strcmp(args[1], "map") == 0 ||
1904 strcmp(args[1], "acl") == 0) {
1905 int ret;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001906
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001907 /* Set flags. */
1908 if (args[1][0] == 'm')
1909 appctx->ctx.map.display_flags = PAT_REF_MAP;
1910 else
1911 appctx->ctx.map.display_flags = PAT_REF_ACL;
1912
1913 /* If the keywork is "map", we expect three parameters, if it
1914 * is "acl", we expect only two parameters
1915 */
1916 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1917 if (!*args[2] || !*args[3] || !*args[4]) {
1918 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
1919 appctx->st0 = STAT_CLI_PRINT;
1920 return 1;
1921 }
1922 }
1923 else {
1924 if (!*args[2] || !*args[3]) {
1925 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
1926 appctx->st0 = STAT_CLI_PRINT;
1927 return 1;
1928 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001929 }
1930
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001931 /* Lookup for the reference. */
1932 appctx->ctx.map.ref = pat_ref_lookup(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001933 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001934 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1935 appctx->ctx.cli.msg = "Unknown map identifier. Please use <name>.\n";
1936 else
1937 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001938 appctx->st0 = STAT_CLI_PRINT;
1939 return 1;
1940 }
1941
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001942 /* Add value. */
1943 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1944 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], NULL);
1945 else
1946 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, NULL);
1947 if (!ret) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001948 appctx->ctx.cli.msg = "Out of memory error.\n";
1949 appctx->st0 = STAT_CLI_PRINT;
1950 return 1;
1951 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001952
1953 /* The add is done, send message. */
1954 appctx->ctx.cli.msg = "Done.\n";
1955 appctx->st0 = STAT_CLI_PRINT;
1956 return 1;
1957 }
1958 else { /* unknown "del" parameter */
1959 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
1960 appctx->st0 = STAT_CLI_PRINT;
1961 return 1;
1962 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001963 }
1964 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001965 return 0;
1966 }
1967 return 1;
1968}
1969
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001970/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001971 * used to processes I/O from/to the stats unix socket. The system relies on a
1972 * state machine handling requests and various responses. We read a request,
1973 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001974 * Then we can read again. The state is stored in appctx->st0 and is one of the
1975 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001976 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001977 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01001978static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001979{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001980 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02001981 struct channel *req = si->ob;
1982 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001983 int reql;
1984 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001985
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001986 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1987 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001988
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001989 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001990 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001991 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001992 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
1993 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001994 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001995 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001996 /* Let's close for real now. We just close the request
1997 * side, the conditions below will complete if needed.
1998 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001999 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002000 break;
2001 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002002 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002003 /* ensure we have some output room left in the event we
2004 * would want to return some info right after parsing.
2005 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002006 if (buffer_almost_full(si->ib->buf)) {
2007 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002008 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002009 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002010
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002011 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002012 if (reql <= 0) { /* closed or EOL not found */
2013 if (reql == 0)
2014 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002015 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002016 continue;
2017 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002018
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002019 /* seek for a possible semi-colon. If we find one, we
2020 * replace it with an LF and skip only this part.
2021 */
2022 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002023 if (trash.str[len] == ';') {
2024 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002025 reql = len + 1;
2026 break;
2027 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002028
Willy Tarreau816fc222009-10-04 07:36:58 +02002029 /* now it is time to check that we have a full line,
2030 * remove the trailing \n and possibly \r, then cut the
2031 * line.
2032 */
2033 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002034 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002035 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002036 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002037 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002038
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002039 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002040 len--;
2041
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002042 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002043
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002044 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002045 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002046 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002047 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002048 continue;
2049 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002050 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002051 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002052 else if (strcmp(trash.str, "help") == 0 ||
2053 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002054 appctx->ctx.cli.msg = stats_sock_usage_msg;
2055 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002056 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002057 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002058 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002059 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002060 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002061 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002062 /* if prompt is disabled, print help on empty lines,
2063 * so that the user at least knows how to enable
2064 * prompt and find help.
2065 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002066 appctx->ctx.cli.msg = stats_sock_usage_msg;
2067 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002068 }
2069
2070 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002071 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002072 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002073 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002074 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002075 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002076 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002077 continue;
2078 }
2079
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002080 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002081 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002082 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2083 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002084 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002085 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002086 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002087 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002088 break;
2089 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002090 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002091 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002092 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002093 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002094 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002095 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002096 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002097 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002098 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002099 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002100 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002101 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002102 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002103 if (stats_table_request(si, appctx->st0))
2104 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002105 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002106 case STAT_CLI_O_PATS:
2107 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002108 appctx->st0 = STAT_CLI_PROMPT;
2109 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002110 case STAT_CLI_O_PAT:
2111 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002112 appctx->st0 = STAT_CLI_PROMPT;
2113 break;
2114 case STAT_CLI_O_MLOOK:
2115 if (stats_map_lookup(si))
2116 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002117 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002118 case STAT_CLI_O_POOLS:
2119 if (stats_dump_pools_to_buffer(si))
2120 appctx->st0 = STAT_CLI_PROMPT;
2121 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002122 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002123 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002124 break;
2125 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002126
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002127 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002128 if (appctx->st0 == STAT_CLI_PROMPT) {
2129 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2130 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002131 }
2132
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002133 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002134 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002135 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002136
2137 /* Now we close the output if one of the writers did so,
2138 * or if we're not in interactive mode and the request
2139 * buffer is empty. This still allows pipelined requests
2140 * to be sent in non-interactive mode.
2141 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002142 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2143 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002144 continue;
2145 }
2146
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002147 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002148 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002149 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002150 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002151
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002152 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002153 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2154 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002155 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002156 * and nothing more to consume. This is comparable to a broken pipe, so
2157 * we forward the close to the request side so that it flows upstream to
2158 * the client.
2159 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002160 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002161 }
2162
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002163 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002164 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2165 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2166 /* We have no more processing to do, and nothing more to send, and
2167 * the client side has closed. So we'll forward this state downstream
2168 * on the response buffer.
2169 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002170 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002171 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002172 }
2173
2174 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002175 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002176
2177 /* we don't want to expire timeouts while we're processing requests */
2178 si->ib->rex = TICK_ETERNITY;
2179 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002180
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002181 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002182 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 +02002183 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002184 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 +02002185
2186 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2187 /* check that we have released everything then unregister */
2188 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002189 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002190}
2191
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002192/* This function dumps information onto the stream interface's read buffer.
2193 * It returns 0 as long as it does not complete, non-zero upon completion.
2194 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002195 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002196static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002197{
2198 unsigned int up = (now.tv_sec - start_date.tv_sec);
2199
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002200 chunk_printf(&trash,
2201 "Name: " PRODUCT_NAME "\n"
2202 "Version: " HAPROXY_VERSION "\n"
2203 "Release_date: " HAPROXY_DATE "\n"
2204 "Nbproc: %d\n"
2205 "Process_num: %d\n"
2206 "Pid: %d\n"
2207 "Uptime: %dd %dh%02dm%02ds\n"
2208 "Uptime_sec: %d\n"
2209 "Memmax_MB: %d\n"
2210 "Ulimit-n: %d\n"
2211 "Maxsock: %d\n"
2212 "Maxconn: %d\n"
2213 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002214 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002215 "CumConns: %d\n"
2216 "CumReq: %d\n"
2217#ifdef USE_OPENSSL
2218 "MaxSslConns: %d\n"
2219 "CurrSslConns: %d\n"
2220 "CumSslConns: %d\n"
2221#endif
2222 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002223 "PipesUsed: %d\n"
2224 "PipesFree: %d\n"
2225 "ConnRate: %d\n"
2226 "ConnRateLimit: %d\n"
2227 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002228 "SessRate: %d\n"
2229 "SessRateLimit: %d\n"
2230 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002231#ifdef USE_OPENSSL
2232 "SslRate: %d\n"
2233 "SslRateLimit: %d\n"
2234 "MaxSslRate: %d\n"
2235#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002236 "CompressBpsIn: %u\n"
2237 "CompressBpsOut: %u\n"
2238 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002239#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002240 "ZlibMemUsage: %ld\n"
2241 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002242#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002243 "Tasks: %d\n"
2244 "Run_queue: %d\n"
2245 "Idle_pct: %d\n"
2246 "node: %s\n"
2247 "description: %s\n"
2248 "",
2249 global.nbproc,
2250 relative_pid,
2251 pid,
2252 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2253 up,
2254 global.rlimit_memmax,
2255 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002256 global.maxsock, global.maxconn, global.hardmaxconn,
2257 actconn, totalconn, global.req_count,
2258#ifdef USE_OPENSSL
2259 global.maxsslconn, sslconns, totalsslconns,
2260#endif
2261 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002262 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002263 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002264#ifdef USE_OPENSSL
2265 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2266#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002267 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2268 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002269#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002270 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002271#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002272 nb_tasks_cur, run_queue_cur, idle_pct,
2273 global.node, global.desc ? global.desc : ""
2274 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002275
2276 if (bi_putchk(si->ib, &trash) == -1)
2277 return 0;
2278
2279 return 1;
2280}
2281
Willy Tarreau12833bb2014-01-28 16:49:56 +01002282/* This function dumps memory usage information onto the stream interface's
2283 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2284 * completion. No state is used.
2285 */
2286static int stats_dump_pools_to_buffer(struct stream_interface *si)
2287{
2288 dump_pools_to_trash();
2289 if (bi_putchk(si->ib, &trash) == -1)
2290 return 0;
2291 return 1;
2292}
2293
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002294/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2295 * the state from stream interface <si>. The caller is responsible for clearing
2296 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002297 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002298static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002299{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002300 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002301 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002302
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002303 if (!(px->cap & PR_CAP_FE))
2304 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002305
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002306 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002307 return 0;
2308
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002309 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002310 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002311 /* name, queue */
2312 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002313
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002314 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002315 /* Column sub-heading for Enable or Disable server */
2316 chunk_appendf(&trash, "<td></td>");
2317 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002318
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002319 chunk_appendf(&trash,
2320 "<td class=ac>"
2321 "<a name=\"%s/Frontend\"></a>"
2322 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2323 "<td colspan=3></td>"
2324 "",
2325 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002326
Willy Tarreau466c9b52012-12-23 02:25:03 +01002327 chunk_appendf(&trash,
2328 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002329 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002330 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2331 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2332 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002333 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2334 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2335 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002336
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002337 if (px->mode == PR_MODE_HTTP)
2338 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002339 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002340 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002341
2342 chunk_appendf(&trash,
2343 "</table></div></u></td>"
2344 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002345 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002346 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2347 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2348 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002349 U2H(px->fe_counters.sps_max),
2350 U2H(px->fe_counters.cps_max),
2351 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002352
2353 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002354 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002355 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002356 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002357
2358 chunk_appendf(&trash,
2359 "</table></div></u></td>"
2360 /* sessions rate : limit */
2361 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002362 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002363
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002364 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002365 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002366 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002367 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002368 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2369 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002370 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002371 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2372 U2H(px->fe_counters.cum_sess),
2373 U2H(px->fe_counters.cum_conn),
2374 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002375
Willy Tarreau466c9b52012-12-23 02:25:03 +01002376 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002377 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002378 chunk_appendf(&trash,
2379 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2380 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2381 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2382 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2383 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2384 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2385 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2386 "<tr><th>- other responses:</th><td>%s</td></tr>"
2387 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2388 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002389 U2H(px->fe_counters.p.http.cum_req),
2390 U2H(px->fe_counters.p.http.rsp[1]),
2391 U2H(px->fe_counters.p.http.rsp[2]),
2392 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002393 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002394 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002395 U2H(px->fe_counters.p.http.rsp[3]),
2396 U2H(px->fe_counters.p.http.rsp[4]),
2397 U2H(px->fe_counters.p.http.rsp[5]),
2398 U2H(px->fe_counters.p.http.rsp[0]),
2399 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002400 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002401
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002402 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002403 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002404 /* sessions: lbtot, lastsess */
2405 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002406 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002407 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002408 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002409 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002410
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002411 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002412 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002413 "<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 +01002414 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002415 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002416 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2417 px->fe_counters.comp_in ?
2418 (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 +01002419 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002420
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002421 chunk_appendf(&trash,
2422 /* denied: req, resp */
2423 "<td>%s</td><td>%s</td>"
2424 /* errors : request, connect, response */
2425 "<td>%s</td><td></td><td></td>"
2426 /* warnings: retries, redispatches */
2427 "<td></td><td></td>"
2428 /* server status : reflect frontend status */
2429 "<td class=ac>%s</td>"
2430 /* rest of server: nothing */
2431 "<td class=ac colspan=8></td></tr>"
2432 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002433 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2434 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002435 px->state == PR_STREADY ? "OPEN" :
2436 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002437 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002438 else { /* CSV mode */
2439 chunk_appendf(&trash,
2440 /* pxid, name, queue cur, queue max, */
2441 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002442 /* sessions : current, max, limit, total */
2443 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002444 /* bytes : in, out */
2445 "%lld,%lld,"
2446 /* denied: req, resp */
2447 "%lld,%lld,"
2448 /* errors : request, connect, response */
2449 "%lld,,,"
2450 /* warnings: retries, redispatches */
2451 ",,"
2452 /* server status : reflect frontend status */
2453 "%s,"
2454 /* rest of server: nothing */
2455 ",,,,,,,,"
2456 /* pid, iid, sid, throttle, lbtot, tracked, type */
2457 "%d,%d,0,,,,%d,"
2458 /* rate, rate_lim, rate_max */
2459 "%u,%u,%u,"
2460 /* check_status, check_code, check_duration */
2461 ",,,",
2462 px->id,
2463 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2464 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2465 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2466 px->fe_counters.failed_req,
2467 px->state == PR_STREADY ? "OPEN" :
2468 px->state == PR_STFULL ? "FULL" : "STOP",
2469 relative_pid, px->uuid, STATS_TYPE_FE,
2470 read_freq_ctr(&px->fe_sess_per_sec),
2471 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002472
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002473 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2474 if (px->mode == PR_MODE_HTTP) {
2475 for (i=1; i<6; i++)
2476 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2477 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2478 }
2479 else
2480 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002481
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002482 /* failed health analyses */
2483 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002484
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002485 /* requests : req_rate, req_rate_max, req_tot, */
2486 chunk_appendf(&trash, "%u,%u,%lld,",
2487 read_freq_ctr(&px->fe_req_per_sec),
2488 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2489
2490 /* errors: cli_aborts, srv_aborts */
2491 chunk_appendf(&trash, ",,");
2492
2493 /* compression: in, out, bypassed */
2494 chunk_appendf(&trash, "%lld,%lld,%lld,",
2495 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2496
2497 /* compression: comp_rsp */
2498 chunk_appendf(&trash, "%lld,",
2499 px->fe_counters.p.http.comp_rsp);
2500
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002501 /* lastsess */
2502 chunk_appendf(&trash, ",");
2503
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002504 /* finish with EOL */
2505 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002506 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002507 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002508}
2509
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002510/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2511 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2512 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2513 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002514 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002515static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002516{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002517 struct appctx *appctx = __objt_appctx(si->end);
2518
2519 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002520 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002521 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002522 /* Column sub-heading for Enable or Disable server */
2523 chunk_appendf(&trash, "<td></td>");
2524 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002525 chunk_appendf(&trash,
2526 /* frontend name, listener name */
2527 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2528 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2529 "",
2530 px->id, l->name,
2531 (flags & ST_SHLGNDS)?"<u>":"",
2532 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002533
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002534 if (flags & ST_SHLGNDS) {
2535 char str[INET6_ADDRSTRLEN];
2536 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002537
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002538 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002539
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002540 port = get_host_port(&l->addr);
2541 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2542 case AF_INET:
2543 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2544 break;
2545 case AF_INET6:
2546 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2547 break;
2548 case AF_UNIX:
2549 chunk_appendf(&trash, "unix, ");
2550 break;
2551 case -1:
2552 chunk_appendf(&trash, "(%s), ", strerror(errno));
2553 break;
2554 }
Willy Tarreau91861262007-10-17 17:06:05 +02002555
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002556 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002557 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002558 }
Willy Tarreau91861262007-10-17 17:06:05 +02002559
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002560 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002561 /* queue */
2562 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002563 /* sessions rate: current, max, limit */
2564 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002565 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002566 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002567 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002568 /* bytes: in, out */
2569 "<td>%s</td><td>%s</td>"
2570 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002571 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002572 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2573 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002574
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002575 chunk_appendf(&trash,
2576 /* denied: req, resp */
2577 "<td>%s</td><td>%s</td>"
2578 /* errors: request, connect, response */
2579 "<td>%s</td><td></td><td></td>"
2580 /* warnings: retries, redispatches */
2581 "<td></td><td></td>"
2582 /* server status: reflect listener status */
2583 "<td class=ac>%s</td>"
2584 /* rest of server: nothing */
2585 "<td class=ac colspan=8></td></tr>"
2586 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002587 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2588 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002589 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2590 }
2591 else { /* CSV mode */
2592 chunk_appendf(&trash,
2593 /* pxid, name, queue cur, queue max, */
2594 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002595 /* sessions: current, max, limit, total */
2596 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002597 /* bytes: in, out */
2598 "%lld,%lld,"
2599 /* denied: req, resp */
2600 "%lld,%lld,"
2601 /* errors: request, connect, response */
2602 "%lld,,,"
2603 /* warnings: retries, redispatches */
2604 ",,"
2605 /* server status: reflect listener status */
2606 "%s,"
2607 /* rest of server: nothing */
2608 ",,,,,,,,"
2609 /* pid, iid, sid, throttle, lbtot, tracked, type */
2610 "%d,%d,%d,,,,%d,"
2611 /* rate, rate_lim, rate_max */
2612 ",,,"
2613 /* check_status, check_code, check_duration */
2614 ",,,"
2615 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2616 ",,,,,,"
2617 /* failed health analyses */
2618 ","
2619 /* requests : req_rate, req_rate_max, req_tot, */
2620 ",,,"
2621 /* errors: cli_aborts, srv_aborts */
2622 ",,"
2623 /* compression: in, out, bypassed, comp_rsp */
2624 ",,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002625 /* lastsess */
2626 ","
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002627 "\n",
2628 px->id, l->name,
2629 l->nbconn, l->counters->conn_max,
2630 l->maxconn, l->counters->cum_conn,
2631 l->counters->bytes_in, l->counters->bytes_out,
2632 l->counters->denied_req, l->counters->denied_resp,
2633 l->counters->failed_req,
2634 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2635 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2636 }
2637 return 1;
2638}
Willy Tarreau91861262007-10-17 17:06:05 +02002639
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002640/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2641 * from stream interface <si>, stats flags <flags>, and server state <state>.
2642 * The caller is responsible for clearing the trash if needed. Returns non-zero
2643 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002644 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2645 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002646 */
2647static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2648{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002649 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002650 struct server *ref = sv->track ? sv->track : sv;
2651 char str[INET6_ADDRSTRLEN];
2652 struct chunk src;
2653 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002654
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002655 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002656 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002657 "DOWN",
2658 "DN %d/%d &uarr;",
2659 "UP %d/%d &darr;",
2660 "UP",
2661 "NOLB %d/%d &darr;",
2662 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002663 "DRAIN %d/%d &darr;",
2664 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002665 "<i>no check</i>"
2666 };
Willy Tarreau91861262007-10-17 17:06:05 +02002667
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002668 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2669 chunk_appendf(&trash, "<tr class=\"maintain\">");
2670 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002671 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002672 "<tr class=\"%s%d\">",
2673 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002674
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002675 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002676 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002677 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2678 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002679
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002680 chunk_appendf(&trash,
2681 "<td class=ac><a name=\"%s/%s\"></a>%s"
2682 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2683 "",
2684 px->id, sv->id,
2685 (flags & ST_SHLGNDS) ? "<u>" : "",
2686 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002687
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002688 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002689 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002690
2691 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2692 case AF_INET:
2693 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2694 break;
2695 case AF_INET6:
2696 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2697 break;
2698 case AF_UNIX:
2699 chunk_appendf(&trash, "unix, ");
2700 break;
2701 case -1:
2702 chunk_appendf(&trash, "(%s), ", strerror(errno));
2703 break;
2704 default: /* address family not supported */
2705 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002706 }
Willy Tarreau91861262007-10-17 17:06:05 +02002707
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002708 /* id */
2709 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002710
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002711 /* cookie */
2712 if (sv->cookie) {
2713 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002714
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002715 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2716 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002717
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002718 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002719 }
2720
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002721 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002722 }
Willy Tarreau91861262007-10-17 17:06:05 +02002723
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002724 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002725 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002726 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002727 /* sessions rate : current, max, limit */
2728 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002729 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002730 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002731 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2732 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002733
Willy Tarreau466c9b52012-12-23 02:25:03 +01002734
2735 chunk_appendf(&trash,
2736 /* sessions: current, max, limit, total */
2737 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002738 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002739 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2740 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002741 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2742 U2H(sv->counters.cum_sess),
2743 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002744
Willy Tarreau466c9b52012-12-23 02:25:03 +01002745 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2746 if (px->mode == PR_MODE_HTTP) {
2747 unsigned long long tot;
2748 for (tot = i = 0; i < 6; i++)
2749 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002750
Willy Tarreau466c9b52012-12-23 02:25:03 +01002751 chunk_appendf(&trash,
2752 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2753 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2754 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2755 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2756 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2757 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2758 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2759 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002760 U2H(tot),
2761 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2762 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2763 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2764 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2765 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2766 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 +02002767 }
Willy Tarreau91861262007-10-17 17:06:05 +02002768
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002769 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002770 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002771 /* sessions: lbtot, last */
2772 "<td>%s</td><td>%s</td>",
2773 U2H(sv->counters.cum_lbconn),
2774 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02002775
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002776 chunk_appendf(&trash,
2777 /* bytes : in, out */
2778 "<td>%s</td><td>%s</td>"
2779 /* denied: req, resp */
2780 "<td></td><td>%s</td>"
2781 /* errors : request, connect */
2782 "<td></td><td>%s</td>"
2783 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002784 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002785 /* warnings: retries, redispatches */
2786 "<td>%lld</td><td>%lld</td>"
2787 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002788 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2789 U2H(sv->counters.failed_secu),
2790 U2H(sv->counters.failed_conns),
2791 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002792 sv->counters.cli_aborts,
2793 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002794 sv->counters.retries, sv->counters.redispatches);
2795
2796 /* status, lest check */
2797 chunk_appendf(&trash, "<td class=ac>");
2798
2799 if (sv->state & SRV_MAINTAIN) {
2800 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2801 chunk_appendf(&trash, "MAINT");
2802 }
2803 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2804 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2805 chunk_appendf(&trash, "MAINT(via)");
2806 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002807 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002808 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2809 chunk_appendf(&trash,
2810 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002811 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2812 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002813 }
Willy Tarreau91861262007-10-17 17:06:05 +02002814
Willy Tarreauff5ae352013-12-11 20:36:34 +01002815 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002816 chunk_appendf(&trash,
2817 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002818 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002819 get_check_status_info(sv->check.status));
2820
2821 if (sv->check.status >= HCHK_STATUS_L57DATA)
2822 chunk_appendf(&trash, "/%d", sv->check.code);
2823
2824 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002825 chunk_appendf(&trash, " in %lums", sv->check.duration);
2826
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002827 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002828 get_check_status_description(sv->check.status));
2829 if (*sv->check.desc) {
2830 chunk_appendf(&trash, ": ");
2831 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2832 chunk_htmlencode(&trash, &src);
2833 }
2834 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002835 }
2836 else
2837 chunk_appendf(&trash, "</td><td>");
2838
2839 chunk_appendf(&trash,
2840 /* weight */
2841 "</td><td class=ac>%d</td>"
2842 /* act, bck */
2843 "<td class=ac>%s</td><td class=ac>%s</td>"
2844 "",
2845 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2846 (sv->state & SRV_BACKUP) ? "-" : "Y",
2847 (sv->state & SRV_BACKUP) ? "Y" : "-");
2848
2849 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002850 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002851 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002852
2853 if (ref->observe)
2854 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2855
2856 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002857 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002858 "<td>%lld</td><td>%s</td>"
2859 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002860 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002861 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2862 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002863 else if (sv != ref) {
2864 if (sv->state & SRV_MAINTAIN)
2865 chunk_appendf(&trash,
2866 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2867 ref->proxy->id, ref->id);
2868 else
2869 chunk_appendf(&trash,
2870 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2871 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2872 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002873 else
2874 chunk_appendf(&trash, "<td colspan=3></td>");
2875
2876 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002877 if (sv->state & SRV_WARMINGUP)
2878 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002879 else
2880 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2881 }
2882 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002883 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002884 "DOWN,",
2885 "DOWN %d/%d,",
2886 "UP %d/%d,",
2887 "UP,",
2888 "NOLB %d/%d,",
2889 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002890 "DRAIN %d/%d,",
2891 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002892 "no check,"
2893 };
2894
2895 chunk_appendf(&trash,
2896 /* pxid, name */
2897 "%s,%s,"
2898 /* queue : current, max */
2899 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002900 /* sessions : current, max, limit, total */
2901 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002902 /* bytes : in, out */
2903 "%lld,%lld,"
2904 /* denied: req, resp */
2905 ",%lld,"
2906 /* errors : request, connect, response */
2907 ",%lld,%lld,"
2908 /* warnings: retries, redispatches */
2909 "%lld,%lld,"
2910 "",
2911 px->id, sv->id,
2912 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002913 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002914 sv->counters.bytes_in, sv->counters.bytes_out,
2915 sv->counters.failed_secu,
2916 sv->counters.failed_conns, sv->counters.failed_resp,
2917 sv->counters.retries, sv->counters.redispatches);
2918
2919 /* status */
2920 if (sv->state & SRV_MAINTAIN)
2921 chunk_appendf(&trash, "MAINT,");
2922 else if (ref != sv && ref->state & SRV_MAINTAIN)
2923 chunk_appendf(&trash, "MAINT(via),");
2924 else
2925 chunk_appendf(&trash,
2926 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002927 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2928 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002929
2930 chunk_appendf(&trash,
2931 /* weight, active, backup */
2932 "%d,%d,%d,"
2933 "",
2934 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2935 (sv->state & SRV_BACKUP) ? 0 : 1,
2936 (sv->state & SRV_BACKUP) ? 1 : 0);
2937
2938 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002939 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002940 chunk_appendf(&trash,
2941 "%lld,%lld,%d,%d,",
2942 sv->counters.failed_checks, sv->counters.down_trans,
2943 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
2944 else
2945 chunk_appendf(&trash, ",,,,");
2946
2947 /* queue limit, pid, iid, sid, */
2948 chunk_appendf(&trash,
2949 "%s,"
2950 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002951 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002952 relative_pid, px->uuid, sv->puid);
2953
2954 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002955 if (sv->state & SRV_WARMINGUP)
2956 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002957
2958 /* sessions: lbtot */
2959 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
2960
2961 /* tracked */
2962 if (sv->track)
2963 chunk_appendf(&trash, "%s/%s,",
2964 sv->track->proxy->id, sv->track->id);
2965 else
2966 chunk_appendf(&trash, ",");
2967
2968 /* type */
2969 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
2970
2971 /* rate */
2972 chunk_appendf(&trash, "%u,,%u,",
2973 read_freq_ctr(&sv->sess_per_sec),
2974 sv->counters.sps_max);
2975
Willy Tarreauff5ae352013-12-11 20:36:34 +01002976 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002977 /* check_status */
2978 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
2979
2980 /* check_code */
2981 if (sv->check.status >= HCHK_STATUS_L57DATA)
2982 chunk_appendf(&trash, "%u,", sv->check.code);
2983 else
2984 chunk_appendf(&trash, ",");
2985
2986 /* check_duration */
2987 if (sv->check.status >= HCHK_STATUS_CHECKED)
2988 chunk_appendf(&trash, "%lu,", sv->check.duration);
2989 else
2990 chunk_appendf(&trash, ",");
2991
2992 }
2993 else
2994 chunk_appendf(&trash, ",,,");
2995
2996 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2997 if (px->mode == PR_MODE_HTTP) {
2998 for (i=1; i<6; i++)
2999 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3000
3001 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3002 }
3003 else
3004 chunk_appendf(&trash, ",,,,,,");
3005
3006 /* failed health analyses */
3007 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3008
3009 /* requests : req_rate, req_rate_max, req_tot, */
3010 chunk_appendf(&trash, ",,,");
3011
3012 /* errors: cli_aborts, srv_aborts */
3013 chunk_appendf(&trash, "%lld,%lld,",
3014 sv->counters.cli_aborts, sv->counters.srv_aborts);
3015
3016 /* compression: in, out, bypassed, comp_rsp */
3017 chunk_appendf(&trash, ",,,,");
3018
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003019 /* lastsess */
3020 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3021
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003022 /* finish with EOL */
3023 chunk_appendf(&trash, "\n");
3024 }
3025 return 1;
3026}
3027
3028/* Dumps a line for backend <px> to the trash for and uses the state from stream
3029 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3030 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3031 */
3032static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3033{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003034 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003035 struct chunk src;
3036 int i;
3037
3038 if (!(px->cap & PR_CAP_BE))
3039 return 0;
3040
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003041 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003042 return 0;
3043
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003044 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003045 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003046 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003047 /* Column sub-heading for Enable or Disable server */
3048 chunk_appendf(&trash, "<td></td>");
3049 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003050 chunk_appendf(&trash,
3051 "<td class=ac>"
3052 /* name */
3053 "%s<a name=\"%s/Backend\"></a>"
3054 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3055 "",
3056 (flags & ST_SHLGNDS)?"<u>":"",
3057 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003058
3059 if (flags & ST_SHLGNDS) {
3060 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003061 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003062 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3063
3064 /* cookie */
3065 if (px->cookie_name) {
3066 chunk_appendf(&trash, ", cookie: '");
3067 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3068 chunk_htmlencode(&trash, &src);
3069 chunk_appendf(&trash, "'");
3070 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003071 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003072 }
3073
3074 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003075 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003076 /* queue : current, max */
3077 "<td>%s</td><td>%s</td><td></td>"
3078 /* sessions rate : current, max, limit */
3079 "<td>%s</td><td>%s</td><td></td>"
3080 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003081 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003082 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3083 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003084
3085 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003086 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003087 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003088 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003089 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003090 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003091 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3092 U2H(px->be_counters.cum_conn),
3093 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003094
Willy Tarreau466c9b52012-12-23 02:25:03 +01003095 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003096 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003097 chunk_appendf(&trash,
3098 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3099 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3100 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3101 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3102 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3103 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3104 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3105 "<tr><th>- other responses:</th><td>%s</td></tr>"
3106 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3107 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003108 U2H(px->be_counters.p.http.cum_req),
3109 U2H(px->be_counters.p.http.rsp[1]),
3110 U2H(px->be_counters.p.http.rsp[2]),
3111 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003112 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003113 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003114 U2H(px->be_counters.p.http.rsp[3]),
3115 U2H(px->be_counters.p.http.rsp[4]),
3116 U2H(px->be_counters.p.http.rsp[5]),
3117 U2H(px->be_counters.p.http.rsp[0]),
3118 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003119 }
3120
3121 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003122 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003123 /* sessions: lbtot, last */
3124 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003125 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003126 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003127 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003128 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003129 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003130 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003131
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003132 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003133 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003134 "<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 +01003135 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003136 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003137 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3138 px->be_counters.comp_in ?
3139 (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 +01003140 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3141
3142 chunk_appendf(&trash,
3143 /* denied: req, resp */
3144 "<td>%s</td><td>%s</td>"
3145 /* errors : request, connect */
3146 "<td></td><td>%s</td>"
3147 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003148 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003149 /* warnings: retries, redispatches */
3150 "<td>%lld</td><td>%lld</td>"
3151 /* backend status: reflect backend status (up/down): we display UP
3152 * if the backend has known working servers or if it has no server at
3153 * all (eg: for stats). Then we display the total weight, number of
3154 * active and backups. */
3155 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3156 "<td class=ac>%d</td><td class=ac>%d</td>"
3157 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003158 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3159 U2H(px->be_counters.failed_conns),
3160 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003161 px->be_counters.cli_aborts,
3162 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003163 px->be_counters.retries, px->be_counters.redispatches,
3164 human_time(now.tv_sec - px->last_change, 1),
3165 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3166 "<font color=\"red\"><b>DOWN</b></font>",
3167 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3168 px->srv_act, px->srv_bck);
3169
3170 chunk_appendf(&trash,
3171 /* rest of backend: nothing, down transitions, total downtime, throttle */
3172 "<td class=ac>&nbsp;</td><td>%d</td>"
3173 "<td>%s</td>"
3174 "<td></td>"
3175 "</tr>",
3176 px->down_trans,
3177 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3178 }
3179 else { /* CSV mode */
3180 chunk_appendf(&trash,
3181 /* pxid, name */
3182 "%s,BACKEND,"
3183 /* queue : current, max */
3184 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003185 /* sessions : current, max, limit, total */
3186 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003187 /* bytes : in, out */
3188 "%lld,%lld,"
3189 /* denied: req, resp */
3190 "%lld,%lld,"
3191 /* errors : request, connect, response */
3192 ",%lld,%lld,"
3193 /* warnings: retries, redispatches */
3194 "%lld,%lld,"
3195 /* backend status: reflect backend status (up/down): we display UP
3196 * if the backend has known working servers or if it has no server at
3197 * all (eg: for stats). Then we display the total weight, number of
3198 * active and backups. */
3199 "%s,"
3200 "%d,%d,%d,"
3201 /* rest of backend: nothing, down transitions, last change, total downtime */
3202 ",%d,%d,%d,,"
3203 /* pid, iid, sid, throttle, lbtot, tracked, type */
3204 "%d,%d,0,,%lld,,%d,"
3205 /* rate, rate_lim, rate_max, */
3206 "%u,,%u,"
3207 /* check_status, check_code, check_duration */
3208 ",,,",
3209 px->id,
3210 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3211 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3212 px->be_counters.bytes_in, px->be_counters.bytes_out,
3213 px->be_counters.denied_req, px->be_counters.denied_resp,
3214 px->be_counters.failed_conns, px->be_counters.failed_resp,
3215 px->be_counters.retries, px->be_counters.redispatches,
3216 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3217 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3218 px->srv_act, px->srv_bck,
3219 px->down_trans, (int)(now.tv_sec - px->last_change),
3220 px->srv?be_downtime(px):0,
3221 relative_pid, px->uuid,
3222 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3223 read_freq_ctr(&px->be_sess_per_sec),
3224 px->be_counters.sps_max);
3225
3226 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3227 if (px->mode == PR_MODE_HTTP) {
3228 for (i=1; i<6; i++)
3229 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3230 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3231 }
3232 else
3233 chunk_appendf(&trash, ",,,,,,");
3234
3235 /* failed health analyses */
3236 chunk_appendf(&trash, ",");
3237
3238 /* requests : req_rate, req_rate_max, req_tot, */
3239 chunk_appendf(&trash, ",,,");
3240
3241 /* errors: cli_aborts, srv_aborts */
3242 chunk_appendf(&trash, "%lld,%lld,",
3243 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3244
3245 /* compression: in, out, bypassed */
3246 chunk_appendf(&trash, "%lld,%lld,%lld,",
3247 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3248
3249 /* compression: comp_rsp */
3250 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3251
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003252 /* lastsess */
3253 chunk_appendf(&trash, "%d,", be_lastsession(px));
3254
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003255 /* finish with EOL */
3256 chunk_appendf(&trash, "\n");
3257 }
3258 return 1;
3259}
3260
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003261/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003262 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003263 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003264 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003265static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003266{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003267 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003268 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3269
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003270 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003271 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003272
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003273 /* 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 +02003274 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003275 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003276 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003277 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3278 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003279 }
3280
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003281 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003282 "<form action=\"%s%s%s%s\" method=\"post\">",
3283 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003284 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3285 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003286 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003287 }
3288
3289 /* print a new table */
3290 chunk_appendf(&trash,
3291 "<table class=\"tbl\" width=\"100%%\">\n"
3292 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003293 "<th class=\"pxname\" width=\"10%%\">");
3294
3295 chunk_appendf(&trash,
3296 "<a name=\"%s\"></a>%s"
3297 "<a class=px href=\"#%s\">%s</a>",
3298 px->id,
3299 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3300 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003301
3302 if (uri->flags & ST_SHLGNDS) {
3303 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003304 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003305 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3306 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003307 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003308 }
3309
3310 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003311 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003312 "<th class=\"%s\" width=\"90%%\">%s</th>"
3313 "</tr>\n"
3314 "</table>\n"
3315 "<table class=\"tbl\" width=\"100%%\">\n"
3316 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003317 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3318 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3319
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003320 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003321 /* Column heading for Enable or Disable server */
3322 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003323 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003324
3325 chunk_appendf(&trash,
3326 "<th rowspan=2></th>"
3327 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003328 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003329 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3330 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3331 "<th colspan=9>Server</th>"
3332 "</tr>\n"
3333 "<tr class=\"titre\">"
3334 "<th>Cur</th><th>Max</th><th>Limit</th>"
3335 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003336 "<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 +01003337 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3338 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3339 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3340 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3341 "<th>Thrtle</th>\n"
3342 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003343}
3344
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003345/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003346 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003347 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003348static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003349{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003350 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003351 chunk_appendf(&trash, "</table>");
3352
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003353 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003354 /* close the form used to enable/disable this proxy servers */
3355 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003356 "Choose the action to perform on the checked servers : "
3357 "<select name=action>"
3358 "<option value=\"\"></option>"
3359 "<option value=\"disable\">Disable</option>"
3360 "<option value=\"enable\">Enable</option>"
3361 "<option value=\"stop\">Soft Stop</option>"
3362 "<option value=\"start\">Soft Start</option>"
3363 "<option value=\"shutdown\">Kill Sessions</option>"
3364 "</select>"
3365 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3366 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3367 "</form>",
3368 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369 }
3370
3371 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003372}
Willy Tarreau91861262007-10-17 17:06:05 +02003373
3374/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003375 * Dumps statistics for a proxy. The output is sent to the stream interface's
3376 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3377 * buffer space, or non-zero if everything completed. This function is used
3378 * both by the CLI and the HTTP entry points, and is able to dump the output
3379 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003380 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003381static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003382{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003383 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003384 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003385 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003386 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003387 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003388
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003389 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003390
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003391 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003392 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003393 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003394 if (uri && uri->scope) {
3395 /* we have a limited scope, we have to check the proxy name */
3396 struct stat_scope *scope;
3397 int len;
3398
3399 len = strlen(px->id);
3400 scope = uri->scope;
3401
3402 while (scope) {
3403 /* match exact proxy name */
3404 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3405 break;
3406
3407 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003408 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003409 break;
3410 scope = scope->next;
3411 }
3412
3413 /* proxy name not found : don't dump anything */
3414 if (scope == NULL)
3415 return 1;
3416 }
3417
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003418 /* if the user has requested a limited output and the proxy
3419 * name does not match, skip it.
3420 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003421 if (appctx->ctx.stats.scope_len &&
3422 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 +02003423 return 1;
3424
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003425 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3426 (appctx->ctx.stats.iid != -1) &&
3427 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003428 return 1;
3429
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003430 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003431 /* fall through */
3432
Willy Tarreau295a8372011-03-10 11:25:07 +01003433 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003434 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003435 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003436 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003437 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003438 }
Willy Tarreau91861262007-10-17 17:06:05 +02003439
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003440 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003441 /* fall through */
3442
Willy Tarreau295a8372011-03-10 11:25:07 +01003443 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003444 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003445 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003446 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003447 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003448
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003449 appctx->ctx.stats.l = px->conf.listeners.n;
3450 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003451 /* fall through */
3452
Willy Tarreau295a8372011-03-10 11:25:07 +01003453 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003454 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003455 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003456 if (buffer_almost_full(rep->buf)) {
3457 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003458 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003459 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003460
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003461 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003462 if (!l->counters)
3463 continue;
3464
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003465 if (appctx->ctx.stats.flags & STAT_BOUND) {
3466 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003467 break;
3468
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003469 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003470 continue;
3471 }
3472
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003473 /* print the frontend */
3474 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3475 if (bi_putchk(rep, &trash) == -1)
3476 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003477 }
3478
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003479 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3480 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003481 /* fall through */
3482
Willy Tarreau295a8372011-03-10 11:25:07 +01003483 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003484 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003485 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003486 int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB, 6=unchecked */
Willy Tarreau91861262007-10-17 17:06:05 +02003487
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003488 if (buffer_almost_full(rep->buf)) {
3489 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003490 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003491 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003493 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003494
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003495 if (appctx->ctx.stats.flags & STAT_BOUND) {
3496 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003497 break;
3498
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003499 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003500 continue;
3501 }
3502
Willy Tarreau44267702011-10-28 15:35:33 +02003503 if (sv->track)
3504 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003505 else
3506 svs = sv;
3507
Willy Tarreau91861262007-10-17 17:06:05 +02003508 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003509 if (!(svs->check.state & CHK_ST_ENABLED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003510 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003511 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003512 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003513 sv_state = 3; /* UP */
3514 else
3515 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003516
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003517 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003518 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003519 else if (svs->state & SRV_GOINGDOWN)
3520 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003521 }
Willy Tarreau91861262007-10-17 17:06:05 +02003522 else
Simon Horman125d0992013-02-24 17:23:38 +09003523 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003524 sv_state = 1; /* going up */
3525 else
3526 sv_state = 0; /* DOWN */
3527
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003528 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003529 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003530 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003531 continue;
3532 }
3533
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003534 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3535 if (bi_putchk(rep, &trash) == -1)
3536 return 0;
3537 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003538
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003539 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003540 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003541
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003542 case STAT_PX_ST_BE:
3543 /* print the backend */
3544 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3545 if (bi_putchk(rep, &trash) == -1)
3546 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003547
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003548 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003549 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003550
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003551 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003552 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003553 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003554 if (bi_putchk(rep, &trash) == -1)
3555 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003556 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003557
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003558 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003559 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003560
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003561 case STAT_PX_ST_FIN:
3562 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003563
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003564 default:
3565 /* unknown state, we should put an abort() here ! */
3566 return 1;
3567 }
3568}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003569
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003570/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3571 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003572 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003573static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003574{
3575 /* WARNING! This must fit in the first buffer !!! */
3576 chunk_appendf(&trash,
3577 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3578 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3579 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3580 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3581 "<style type=\"text/css\"><!--\n"
3582 "body {"
3583 " font-family: arial, helvetica, sans-serif;"
3584 " font-size: 12px;"
3585 " font-weight: normal;"
3586 " color: black;"
3587 " background: white;"
3588 "}\n"
3589 "th,td {"
3590 " font-size: 10px;"
3591 "}\n"
3592 "h1 {"
3593 " font-size: x-large;"
3594 " margin-bottom: 0.5em;"
3595 "}\n"
3596 "h2 {"
3597 " font-family: helvetica, arial;"
3598 " font-size: x-large;"
3599 " font-weight: bold;"
3600 " font-style: italic;"
3601 " color: #6020a0;"
3602 " margin-top: 0em;"
3603 " margin-bottom: 0em;"
3604 "}\n"
3605 "h3 {"
3606 " font-family: helvetica, arial;"
3607 " font-size: 16px;"
3608 " font-weight: bold;"
3609 " color: #b00040;"
3610 " background: #e8e8d0;"
3611 " margin-top: 0em;"
3612 " margin-bottom: 0em;"
3613 "}\n"
3614 "li {"
3615 " margin-top: 0.25em;"
3616 " margin-right: 2em;"
3617 "}\n"
3618 ".hr {margin-top: 0.25em;"
3619 " border-color: black;"
3620 " border-bottom-style: solid;"
3621 "}\n"
3622 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3623 ".total {background: #20D0D0;color: #ffff80;}\n"
3624 ".frontend {background: #e8e8d0;}\n"
3625 ".socket {background: #d0d0d0;}\n"
3626 ".backend {background: #e8e8d0;}\n"
3627 ".active0 {background: #ff9090;}\n"
3628 ".active1 {background: #ffd020;}\n"
3629 ".active2 {background: #ffffa0;}\n"
3630 ".active3 {background: #c0ffc0;}\n"
3631 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003632 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3633 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3634 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003635 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003636 ".backup0 {background: #ff9090;}\n"
3637 ".backup1 {background: #ff80ff;}\n"
3638 ".backup2 {background: #c060ff;}\n"
3639 ".backup3 {background: #b0d0ff;}\n"
3640 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3641 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003642 ".backup6 {background: #c060ff;}\n"
3643 ".backup7 {background: #cc9900;}\n"
3644 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003645 ".maintain {background: #c07820;}\n"
3646 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3647 "\n"
3648 "a.px:link {color: #ffff40; text-decoration: none;}"
3649 "a.px:visited {color: #ffff40; text-decoration: none;}"
3650 "a.px:hover {color: #ffffff; text-decoration: none;}"
3651 "a.lfsb:link {color: #000000; text-decoration: none;}"
3652 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3653 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3654 "\n"
3655 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3656 "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"
3657 "table.tbl td.ac { text-align: center;}\n"
3658 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3659 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3660 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3661 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3662 "\n"
3663 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3664 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3665 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003666 "table.det { border-collapse: collapse; border-style: none; }\n"
3667 "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 +01003668 "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 +01003669 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003670 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003671 " display:block;\n"
3672 " visibility:hidden;\n"
3673 " z-index:2147483647;\n"
3674 " position:absolute;\n"
3675 " padding:2px 4px 3px;\n"
3676 " background:#f0f060; color:#000000;\n"
3677 " border:1px solid #7040c0;\n"
3678 " white-space:nowrap;\n"
3679 " font-style:normal;font-size:11px;font-weight:normal;\n"
3680 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3681 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3682 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003683 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003684 "-->\n"
3685 "</style></head>\n",
3686 (uri->flags & ST_SHNODE) ? " on " : "",
3687 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3688 );
3689}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003690
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003691/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003692 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003693 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003694 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003695static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003696{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003697 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003698 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003699 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003700
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003701 /* WARNING! this has to fit the first packet too.
3702 * We are around 3.5 kB, add adding entries will
3703 * become tricky if we want to support 4kB buffers !
3704 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003705 chunk_appendf(&trash,
3706 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3707 PRODUCT_NAME "%s</a></h1>\n"
3708 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3709 "<hr width=\"100%%\" class=\"hr\">\n"
3710 "<h3>&gt; General process information</h3>\n"
3711 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3712 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3713 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3714 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3715 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3716 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3717 "Running tasks: %d/%d; idle = %d %%<br>\n"
3718 "</td><td align=\"center\" nowrap>\n"
3719 "<table class=\"lgd\"><tr>\n"
3720 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3721 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3722 "</tr><tr>\n"
3723 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3724 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3725 "</tr><tr>\n"
3726 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3727 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3728 "</tr><tr>\n"
3729 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003730 "</tr><tr>\n"
3731 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003732 "</tr><tr>\n"
3733 "<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 -07003734 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003735 "<td class=\"active7\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003736 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003737 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003738 "</td>"
3739 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3740 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3741 "",
3742 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3743 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3744 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3745 (uri->flags & ST_SHDESC) ? ": " : "",
3746 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3747 pid, relative_pid, global.nbproc,
3748 up / 86400, (up % 86400) / 3600,
3749 (up % 3600) / 60, (up % 60),
3750 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3751 global.rlimit_memmax ? " MB" : "",
3752 global.rlimit_nofile,
3753 global.maxsock, global.maxconn, global.maxpipes,
3754 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3755 run_queue_cur, nb_tasks_cur, idle_pct
3756 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003757
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003758 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3759 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3760 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003761
3762 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003763 "<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 +02003764 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003765 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3766 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3767 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003768 STAT_SCOPE_TXT_MAXLEN);
3769
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003770 /* 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 +02003771 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003772 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003773 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003774 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3775 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003776 }
3777
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003778 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003779 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003780 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003781 uri->uri_prefix,
3782 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003783 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003784 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003785 else
3786 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003787 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003788 uri->uri_prefix,
3789 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003790 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003791 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003792
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003793 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003794 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003795 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003796 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003797 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003798 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003799 "",
3800 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003801 else
3802 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003803 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003804 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003805 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003806 ";norefresh",
3807 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003808 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003809
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003810 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003811 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003812 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003813 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3814 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003815 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003816
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003817 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003818 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003819 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003820 (uri->refresh > 0) ? ";norefresh" : "",
3821 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003822
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003823 chunk_appendf(&trash,
3824 "</ul></td>"
3825 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3826 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3827 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3828 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3829 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3830 "</ul>"
3831 "</td>"
3832 "</tr></table>\n"
3833 ""
3834 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003835
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003836 if (appctx->ctx.stats.st_code) {
3837 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003838 case STAT_STATUS_DONE:
3839 chunk_appendf(&trash,
3840 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003841 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003842 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003843 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003844 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3845 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003846 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003847 break;
3848 case STAT_STATUS_NONE:
3849 chunk_appendf(&trash,
3850 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003851 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003852 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003853 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003854 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3855 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003856 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003857 break;
3858 case STAT_STATUS_PART:
3859 chunk_appendf(&trash,
3860 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003861 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003862 "Action partially processed.<br>"
3863 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003864 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003865 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3866 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003867 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003868 break;
3869 case STAT_STATUS_ERRP:
3870 chunk_appendf(&trash,
3871 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003872 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003873 "Action not processed because of invalid parameters."
3874 "<ul>"
3875 "<li>The action is maybe unknown.</li>"
3876 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3877 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3878 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003879 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003880 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3881 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003882 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003883 break;
3884 case STAT_STATUS_EXCD:
3885 chunk_appendf(&trash,
3886 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003887 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003888 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3889 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003890 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003891 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3892 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003893 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003894 break;
3895 case STAT_STATUS_DENY:
3896 chunk_appendf(&trash,
3897 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003898 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003899 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003900 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003901 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3902 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003903 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003904 break;
3905 default:
3906 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003907 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003908 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003909 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02003910 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003911 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3912 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003913 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003914 }
3915 chunk_appendf(&trash, "<p>\n");
3916 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003917}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003918
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003919/* Dumps the HTML stats trailer block to the trash. The caller is responsible
3920 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003921 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003922static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003923{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003924 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003925}
Willy Tarreau7f062c42009-03-05 18:43:00 +01003926
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003927/* This function dumps statistics onto the stream interface's read buffer in
3928 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02003929 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
3930 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
3931 * and the session must be closed, or -1 in case of any error. This function is
3932 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003933 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003934static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003935{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003936 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003937 struct channel *rep = si->ib;
3938 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01003939
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003940 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02003941
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003942 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003943 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003944 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003945 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01003946
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003947 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003948 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003949 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01003950 else
3951 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01003952
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003953 if (bi_putchk(rep, &trash) == -1)
3954 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01003955
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003956 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003957 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01003958
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003959 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003960 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003961 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003962 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003963 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003964 }
Willy Tarreau91861262007-10-17 17:06:05 +02003965
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003966 appctx->ctx.stats.px = proxy;
3967 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
3968 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02003969 /* fall through */
3970
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003971 case STAT_ST_LIST:
3972 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003973 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003974 if (buffer_almost_full(rep->buf)) {
3975 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003976 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003977 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003978
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003979 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003980 /* skip the disabled proxies, global frontend and non-networked ones */
3981 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003982 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003983 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003984
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003985 appctx->ctx.stats.px = px->next;
3986 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003987 }
3988 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003989
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003990 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003991 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003992
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003993 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003994 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003995 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003996 if (bi_putchk(rep, &trash) == -1)
3997 return 0;
3998 }
Willy Tarreau55058a72012-11-21 08:27:21 +01003999
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004000 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004001 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004002
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004003 case STAT_ST_FIN:
4004 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004005
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004006 default:
4007 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004008 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004009 return -1;
4010 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004011}
Willy Tarreauae526782010-03-04 20:34:23 +01004012
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004013/* We reached the stats page through a POST request. The appctx is
4014 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004015 * Parse the posted data and enable/disable servers if necessary.
4016 * Returns 1 if request was parsed or zero if it needs more data.
4017 */
4018static int stats_process_http_post(struct stream_interface *si)
4019{
4020 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004021 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004022
4023 struct proxy *px = NULL;
4024 struct server *sv = NULL;
4025
4026 char key[LINESIZE];
4027 int action = ST_ADM_ACTION_NONE;
4028 int reprocess = 0;
4029
4030 int total_servers = 0;
4031 int altered_servers = 0;
4032
4033 char *first_param, *cur_param, *next_param, *end_params;
4034 char *st_cur_param = NULL;
4035 char *st_next_param = NULL;
4036
4037 struct chunk *temp;
4038 int reql;
4039
4040 temp = get_trash_chunk();
4041 if (temp->size < s->txn.req.body_len) {
4042 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004043 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004044 goto out;
4045 }
4046
4047 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4048 if (reql <= 0) {
4049 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004050 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004051 return 0;
4052 }
4053
4054 first_param = temp->str;
4055 end_params = temp->str + reql;
4056 cur_param = next_param = end_params;
4057 *end_params = '\0';
4058
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004059 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004060
4061 /*
4062 * Parse the parameters in reverse order to only store the last value.
4063 * From the html form, the backend and the action are at the end.
4064 */
4065 while (cur_param > first_param) {
4066 char *value;
4067 int poffset, plen;
4068
4069 cur_param--;
4070
4071 if ((*cur_param == '&') || (cur_param == first_param)) {
4072 reprocess_servers:
4073 /* Parse the key */
4074 poffset = (cur_param != first_param ? 1 : 0);
4075 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4076 if ((plen > 0) && (plen <= sizeof(key))) {
4077 strncpy(key, cur_param + poffset, plen);
4078 key[plen - 1] = '\0';
4079 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004080 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004081 goto out;
4082 }
4083
4084 /* Parse the value */
4085 value = key;
4086 while (*value != '\0' && *value != '=') {
4087 value++;
4088 }
4089 if (*value == '=') {
4090 /* Ok, a value is found, we can mark the end of the key */
4091 *value++ = '\0';
4092 }
4093 if (url_decode(key) < 0 || url_decode(value) < 0)
4094 break;
4095
4096 /* Now we can check the key to see what to do */
4097 if (!px && (strcmp(key, "b") == 0)) {
4098 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4099 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004100 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004101 goto out;
4102 }
4103 }
4104 else if (!action && (strcmp(key, "action") == 0)) {
4105 if (strcmp(value, "disable") == 0) {
4106 action = ST_ADM_ACTION_DISABLE;
4107 }
4108 else if (strcmp(value, "enable") == 0) {
4109 action = ST_ADM_ACTION_ENABLE;
4110 }
4111 else if (strcmp(value, "stop") == 0) {
4112 action = ST_ADM_ACTION_STOP;
4113 }
4114 else if (strcmp(value, "start") == 0) {
4115 action = ST_ADM_ACTION_START;
4116 }
4117 else if (strcmp(value, "shutdown") == 0) {
4118 action = ST_ADM_ACTION_SHUTDOWN;
4119 }
4120 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004121 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004122 goto out;
4123 }
4124 }
4125 else if (strcmp(key, "s") == 0) {
4126 if (!(px && action)) {
4127 /*
4128 * Indicates that we'll need to reprocess the parameters
4129 * as soon as backend and action are known
4130 */
4131 if (!reprocess) {
4132 st_cur_param = cur_param;
4133 st_next_param = next_param;
4134 }
4135 reprocess = 1;
4136 }
4137 else if ((sv = findserver(px, value)) != NULL) {
4138 switch (action) {
4139 case ST_ADM_ACTION_DISABLE:
4140 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
4141 /* Not already in maintenance, we can change the server state */
4142 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01004143 sv->check.state |= CHK_ST_PAUSED;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004144 set_server_down(&sv->check);
4145 altered_servers++;
4146 total_servers++;
4147 }
4148 break;
4149 case ST_ADM_ACTION_ENABLE:
4150 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Willy Tarreaua3ae9322013-12-28 21:28:49 +01004151 /* Already in maintenance, we can change the server state.
4152 * If this server tracks the status of another one,
4153 * we must restore the good status.
4154 */
4155 if (!sv->track || (sv->track->state & SRV_RUNNING)) {
4156 set_server_up(&sv->check);
4157 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
4158 }
4159 else {
4160 sv->state &= ~SRV_MAINTAIN;
4161 sv->check.state &= ~CHK_ST_PAUSED;
4162 set_server_down(&sv->check);
4163 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004164 altered_servers++;
4165 total_servers++;
4166 }
4167 break;
4168 case ST_ADM_ACTION_STOP:
4169 case ST_ADM_ACTION_START:
4170 if (action == ST_ADM_ACTION_START)
4171 sv->uweight = sv->iweight;
4172 else
4173 sv->uweight = 0;
4174
4175 server_recalc_eweight(sv);
4176 set_server_drain_state(sv);
4177
4178 altered_servers++;
4179 total_servers++;
4180 break;
4181 case ST_ADM_ACTION_SHUTDOWN:
4182 if (px->state != PR_STSTOPPED) {
4183 struct session *sess, *sess_bck;
4184
4185 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4186 if (sess->srv_conn == sv)
4187 session_shutdown(sess, SN_ERR_KILLED);
4188
4189 altered_servers++;
4190 total_servers++;
4191 }
4192 break;
4193 }
4194 } else {
4195 /* the server name is unknown or ambiguous (duplicate names) */
4196 total_servers++;
4197 }
4198 }
4199 if (reprocess && px && action) {
4200 /* Now, we know the backend and the action chosen by the user.
4201 * We can safely restart from the first server parameter
4202 * to reprocess them
4203 */
4204 cur_param = st_cur_param;
4205 next_param = st_next_param;
4206 reprocess = 0;
4207 goto reprocess_servers;
4208 }
4209
4210 next_param = cur_param;
4211 }
4212 }
4213
4214 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004215 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004216 }
4217 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004218 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004219 }
4220 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004221 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004222 }
4223 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004224 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004225 }
4226 out:
4227 return 1;
4228}
4229
4230
4231static int stats_send_http_headers(struct stream_interface *si)
4232{
4233 struct session *s = session_from_task(si->owner);
4234 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004235 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004236
4237 chunk_printf(&trash,
Willy Tarreau51437d22013-12-29 00:43:40 +01004238 "HTTP/1.0 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004239 "Cache-Control: no-cache\r\n"
4240 "Connection: close\r\n"
4241 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004242 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004243
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004244 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004245 chunk_appendf(&trash, "Refresh: %d\r\n",
4246 uri->refresh);
4247
Willy Tarreau51437d22013-12-29 00:43:40 +01004248 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004249
4250 s->txn.status = 200;
4251 s->logs.tv_request = now;
4252
4253 if (bi_putchk(si->ib, &trash) == -1)
4254 return 0;
4255
4256 return 1;
4257}
4258
4259static int stats_send_http_redirect(struct stream_interface *si)
4260{
4261 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4262 struct session *s = session_from_task(si->owner);
4263 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004264 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004265
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004266 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004267 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004268 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004269 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004270 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4271 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004272 }
4273
4274 /* We don't want to land on the posted stats page because a refresh will
4275 * repost the data. We don't want this to happen on accident so we redirect
4276 * the browse to the stats page with a GET.
4277 */
4278 chunk_printf(&trash,
4279 "HTTP/1.1 303 See Other\r\n"
4280 "Cache-Control: no-cache\r\n"
4281 "Content-Type: text/plain\r\n"
4282 "Connection: close\r\n"
4283 "Location: %s;st=%s%s%s%s\r\n"
4284 "\r\n",
4285 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004286 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4287 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4288 stat_status_codes[appctx->ctx.stats.st_code]) ?
4289 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004290 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004291 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4292 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004293 scope_txt);
4294
4295 s->txn.status = 303;
4296 s->logs.tv_request = now;
4297
4298 if (bi_putchk(si->ib, &trash) == -1)
4299 return 0;
4300
4301 return 1;
4302}
4303
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004304/* This I/O handler runs as an applet embedded in a stream interface. It is
4305 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004306 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004307 * automatically unregisters itself once transfer is complete.
4308 */
4309static void http_stats_io_handler(struct stream_interface *si)
4310{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004311 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004312 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004313 struct channel *req = si->ob;
4314 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004315
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004316 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4317 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004318
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004319 /* check that the output is not closed */
4320 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004321 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004322
Willy Tarreau347a35d2013-11-22 17:51:09 +01004323 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004324 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004325 if (stats_send_http_headers(si)) {
4326 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004327 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004328 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004329 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004330 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004331 }
4332
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004333 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004334 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004335 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004336 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004337
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004338 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004339 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004340 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004341 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004342 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004343 }
4344
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004345 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004346 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004347 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004348 }
4349
Willy Tarreau51437d22013-12-29 00:43:40 +01004350 if (appctx->st0 == STAT_HTTP_DONE)
4351 si_shutw(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004352
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004353 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4354 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004355
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004356 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004357 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4358 si_shutr(si);
4359 res->flags |= CF_READ_NULL;
4360 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004361 }
Willy Tarreau91861262007-10-17 17:06:05 +02004362
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004363 /* update all other flags and resync with the other side */
4364 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004365
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004366 /* we don't want to expire timeouts while we're processing requests */
4367 si->ib->rex = TICK_ETERNITY;
4368 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004369
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004370 out:
4371 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4372 /* check that we have released everything then unregister */
4373 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004374 }
4375}
4376
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004377
Willy Tarreau909d5172012-11-26 03:04:41 +01004378static inline const char *get_conn_ctrl_name(const struct connection *conn)
4379{
Willy Tarreau3c728722014-01-23 13:50:42 +01004380 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004381 return "NONE";
4382 return conn->ctrl->name;
4383}
4384
4385static inline const char *get_conn_xprt_name(const struct connection *conn)
4386{
4387 static char ptr[17];
4388
Willy Tarreauaad69382014-01-23 14:21:42 +01004389 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004390 return "NONE";
4391
4392 if (conn->xprt == &raw_sock)
4393 return "RAW";
4394
4395#ifdef USE_OPENSSL
4396 if (conn->xprt == &ssl_sock)
4397 return "SSL";
4398#endif
4399 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4400 return ptr;
4401}
4402
4403static inline const char *get_conn_data_name(const struct connection *conn)
4404{
4405 static char ptr[17];
4406
4407 if (!conn->data)
4408 return "NONE";
4409
4410 if (conn->data == &sess_conn_cb)
4411 return "SESS";
4412
4413 if (conn->data == &si_conn_cb)
4414 return "STRM";
4415
4416 if (conn->data == &check_conn_cb)
4417 return "CHCK";
4418
4419 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4420 return ptr;
4421}
4422
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004423/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004424 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004425 * 0 if the output buffer is full and it needs to be called again, otherwise
4426 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004427 */
Willy Tarreau76153662012-11-26 01:16:39 +01004428static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004429{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004430 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004431 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004432 extern const char *monthname[12];
4433 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004434 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004435 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004436
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004437 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004438
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004439 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004440 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004441 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4442 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004443 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004444 appctx->ctx.sess.uid = 0;
4445 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004446 return 1;
4447 }
4448
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004449 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004450 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004451 appctx->ctx.sess.uid = sess->uniq_id;
4452 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004453 /* fall through */
4454
4455 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004456 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004457 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004458 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004459 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004460 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4461 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004462 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004463 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004464
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004465 conn = objt_conn(sess->si[0].end);
4466 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004467 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004468 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004469 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004470 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004471 break;
4472 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004473 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004474 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004475 default:
4476 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004477 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004478 break;
4479 }
4480
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004481 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004482 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004483 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004484
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004485 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004486 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004487 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4488 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4489 sess->listener ? sess->listener->luid : 0);
4490
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004491 if (conn)
4492 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004493
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004494 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004495 case AF_INET:
4496 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004497 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004498 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004499 break;
4500 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004501 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004502 break;
4503 default:
4504 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004505 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004506 break;
4507 }
4508
Willy Tarreau50943332011-09-02 17:33:05 +02004509 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004510 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004511 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004512 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004513 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4514 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004515 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004516
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004517 conn = objt_conn(sess->si[1].end);
4518 if (conn)
4519 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004520
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004521 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004522 case AF_INET:
4523 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004524 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004525 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004526 break;
4527 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004528 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004529 break;
4530 default:
4531 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004532 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004533 break;
4534 }
4535
4536 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004537 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004538 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004539 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4540 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004541 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004542 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004543
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004544 if (conn)
4545 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004546
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004547 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004548 case AF_INET:
4549 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004550 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004551 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004552 break;
4553 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004554 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004555 break;
4556 default:
4557 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004558 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004559 break;
4560 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004561
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004562 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004563 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004564 sess->task,
4565 sess->task->state,
4566 sess->task->nice, sess->task->calls,
4567 sess->task->expire ?
4568 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4569 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4570 TICKS_TO_MS(1000)) : "<NEVER>",
4571 task_in_rq(sess->task) ? ", running" : "");
4572
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004573 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004574 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004575 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4576
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004577 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004578 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4579 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4580 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4581
4582 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004583 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004584 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004585 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004586 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004587 obj_type_name(sess->si[0].end),
4588 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004589 sess->si[0].exp ?
4590 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4591 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4592 TICKS_TO_MS(1000)) : "<NEVER>",
4593 sess->si[0].err_type);
4594
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004595 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004596 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004597 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004598 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004599 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004600 obj_type_name(sess->si[1].end),
4601 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004602 sess->si[1].exp ?
4603 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4604 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4605 TICKS_TO_MS(1000)) : "<NEVER>",
4606 sess->si[1].err_type);
4607
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004608 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004609 chunk_appendf(&trash,
4610 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004611 conn,
4612 get_conn_ctrl_name(conn),
4613 get_conn_xprt_name(conn),
4614 get_conn_data_name(conn),
4615 obj_type_name(conn->target),
4616 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004617
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004618 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004619 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004620 conn->flags,
4621 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004622 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004623 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004624 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004625 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004626 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4627 chunk_appendf(&trash,
4628 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4629 tmpctx,
4630 tmpctx->st0,
4631 tmpctx->st1,
4632 tmpctx->st2,
4633 tmpctx->applet->name);
4634 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004635
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004636 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004637 chunk_appendf(&trash,
4638 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004639 conn,
4640 get_conn_ctrl_name(conn),
4641 get_conn_xprt_name(conn),
4642 get_conn_data_name(conn),
4643 obj_type_name(conn->target),
4644 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004645
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004646 chunk_appendf(&trash,
4647 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004648 conn->flags,
4649 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004650 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004651 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004652 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004653 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004654 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4655 chunk_appendf(&trash,
4656 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4657 tmpctx,
4658 tmpctx->st0,
4659 tmpctx->st1,
4660 tmpctx->st2,
4661 tmpctx->applet->name);
4662 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004663
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004664 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004665 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004666 " an_exp=%s",
4667 sess->req,
4668 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004669 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004670 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004671 sess->req->analyse_exp ?
4672 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4673 TICKS_TO_MS(1000)) : "<NEVER>");
4674
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004675 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004676 " rex=%s",
4677 sess->req->rex ?
4678 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4679 TICKS_TO_MS(1000)) : "<NEVER>");
4680
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004681 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004682 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004683 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004684 sess->req->wex ?
4685 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4686 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004687 sess->req->buf,
4688 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004689 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004690 sess->txn.req.next, sess->req->buf->i,
4691 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004692
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004693 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004694 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004695 " an_exp=%s",
4696 sess->rep,
4697 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004698 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004699 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004700 sess->rep->analyse_exp ?
4701 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4702 TICKS_TO_MS(1000)) : "<NEVER>");
4703
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004704 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004705 " rex=%s",
4706 sess->rep->rex ?
4707 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4708 TICKS_TO_MS(1000)) : "<NEVER>");
4709
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004710 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004711 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004712 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004713 sess->rep->wex ?
4714 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4715 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004716 sess->rep->buf,
4717 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004718 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004719 sess->txn.rsp.next, sess->rep->buf->i,
4720 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004721
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004722 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004723 return 0;
4724
4725 /* use other states to dump the contents */
4726 }
4727 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004728 appctx->ctx.sess.uid = 0;
4729 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004730 return 1;
4731}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004732
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004733static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004734{
4735 struct appctx *appctx = __objt_appctx(si->end);
4736
4737 switch (appctx->st2) {
4738 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004739
4740 /* Now, we start the browsing of the references lists.
4741 * Note that the following call to LIST_ELEM return bad pointer. The only
4742 * avalaible field of this pointer is <list>. It is used with the function
4743 * pat_list_get_next() for retruning the first avalaible entry
4744 */
4745 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
4746 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
4747 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004748 appctx->st2 = STAT_ST_LIST;
4749 /* fall through */
4750
4751 case STAT_ST_LIST:
4752 while (appctx->ctx.map.ref) {
4753
4754 chunk_reset(&trash);
4755
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004756 /* Build messages. If the reference is used by another category than
4757 * the listed categorie, display the information in the massage.
4758 */
4759 if ((appctx->ctx.map.display_flags & PAT_REF_MAP) &&
4760 (appctx->ctx.map.ref->flags & PAT_REF_ACL)) {
4761 chunk_appendf(&trash, "%s (also used by an ACL)\n",
4762 appctx->ctx.map.ref->reference);
4763 }
4764 else if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
4765 (appctx->ctx.map.ref->flags & PAT_REF_MAP)) {
4766 chunk_appendf(&trash, "%s (also used by a map)\n",
4767 appctx->ctx.map.ref->reference);
4768 }
4769 else
4770 chunk_appendf(&trash, "%s\n", appctx->ctx.map.ref->reference);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004771
4772 if (bi_putchk(si->ib, &trash) == -1) {
4773 /* let's try again later from this session. We add ourselves into
4774 * this session's users so that it can remove us upon termination.
4775 */
4776 return 0;
4777 }
4778
4779 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004780 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
4781 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004782 }
4783
4784 appctx->st2 = STAT_ST_FIN;
4785 /* fall through */
4786
4787 default:
4788 appctx->st2 = STAT_ST_FIN;
4789 return 1;
4790 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004791 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004792}
4793
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004794static int stats_map_lookup(struct stream_interface *si)
4795{
4796 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004797 struct sample sample;
4798 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004799 int match_method;
4800 struct sample_storage *smp;
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004801 struct sockaddr_storage addr;
4802 char s_addr[INET_ADDRSTRLEN];
4803 char s_mask[INET_ADDRSTRLEN];
4804 char s_addr6[INET6_ADDRSTRLEN];
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004805 const char *keystr;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004806
4807 switch (appctx->st2) {
4808 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004809 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01004810 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004811 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004812 appctx->st2 = STAT_ST_LIST;
4813 /* fall through */
4814
4815 case STAT_ST_LIST:
4816 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004817 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004818 /* initialise chunk to build new message */
4819 chunk_reset(&trash);
4820
4821 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004822 sample.type = SMP_T_STR;
4823 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004824 sample.data.str.len = appctx->ctx.map.chunk.len;
4825 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01004826 if (appctx->ctx.map.expr->pat_head->match &&
4827 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004828 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
4829 else
4830 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004831
4832 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004833 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
4834 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
4835 break;
4836 if (match_method >= PAT_MATCH_NUM)
4837 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
4838 else
4839 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004840
4841 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004842 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004843 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
4844 chunk_appendf(&trash, ", found=no");
4845 else
4846 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004847 }
4848
4849 /* Display match and match info */
4850 else {
4851 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004852 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
4853 chunk_appendf(&trash, ", found=yes");
4854 else
4855 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004856
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004857 /* display index mode */
4858 if (pat->flags & PAT_F_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004859 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004860 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004861 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004862
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004863 /* case sensitive */
4864 if (pat->flags & PAT_F_IGNORE_CASE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004865 chunk_appendf(&trash, ", case=insensitive");
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004866 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004867 chunk_appendf(&trash, ", case=sensitive");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004868
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004869 /* display source */
4870 if (pat->flags & PAT_F_FROM_FILE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004871 chunk_appendf(&trash, ", src=file");
Thierry FOURNIERd5723432014-03-11 13:52:44 +01004872 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004873 chunk_appendf(&trash, ", src=conf");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004874
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004875 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
4876 keystr = "key";
4877 else
4878 keystr = "pattern";
4879
4880 switch (match_method) {
4881 case PAT_MATCH_STR:
4882 case PAT_MATCH_BEG:
4883 case PAT_MATCH_SUB:
4884 case PAT_MATCH_DIR:
4885 case PAT_MATCH_DOM:
4886 case PAT_MATCH_END:
4887 /* display string */
4888 chunk_appendf(&trash, ", %s=\"%s\"", keystr, pat->ptr.str);
4889 break;
4890
4891 case PAT_MATCH_IP:
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004892 /* display IPv4/v6 */
4893 if (pat->type == SMP_T_IPV4) {
4894 ((struct sockaddr_in *)&addr)->sin_family = AF_INET;
4895 memcpy(&((struct sockaddr_in *)&addr)->sin_addr, &pat->val.ipv4.addr,
4896 sizeof(pat->val.ipv4.addr));
4897 if (addr_to_str(&addr, s_addr, INET_ADDRSTRLEN)) {
4898 memcpy(&((struct sockaddr_in *)&addr)->sin_addr, &pat->val.ipv4.mask,
4899 sizeof(pat->val.ipv4.mask));
4900 if (addr_to_str(&addr, s_mask, INET_ADDRSTRLEN))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004901 chunk_appendf(&trash, ", %s=\"%s/%s\"", keystr, s_addr, s_mask);
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004902 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004903 }
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004904 else if (pat->type == SMP_T_IPV6) {
4905 ((struct sockaddr_in6 *)&addr)->sin6_family = AF_INET6;
4906 memcpy(&((struct sockaddr_in6 *)&addr)->sin6_addr, &pat->val.ipv6.addr,
4907 sizeof(pat->val.ipv6.addr));
4908 if (addr_to_str(&addr, s_addr6, INET6_ADDRSTRLEN))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004909 chunk_appendf(&trash, ", %s=\"%s/%d\"", keystr, s_addr6, pat->val.ipv6.mask);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004910 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004911 break;
4912
4913 case PAT_MATCH_INT:
Thierry FOURNIERb4be1292014-01-17 15:14:41 +01004914 /* display int */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004915 chunk_appendf(&trash, ", %s=\"", keystr);
Thierry FOURNIERb4be1292014-01-17 15:14:41 +01004916 if (pat->val.range.min_set && pat->val.range.max_set &&
4917 pat->val.range.min == pat->val.range.max) {
4918 chunk_appendf(&trash, "%lld", pat->val.range.min);
4919 }
4920 else {
4921 if (pat->val.range.min_set)
4922 chunk_appendf(&trash, "is >= %lld", pat->val.range.min);
4923 if (pat->val.range.min_set && pat->val.range.max_set)
4924 chunk_appendf(&trash, " and ");
4925 if (pat->val.range.max_set)
4926 chunk_appendf(&trash, "is <= %lld", pat->val.range.max);
4927 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004928 chunk_appendf(&trash, "\"");
4929 break;
4930
4931 /* Dont display other types. */
4932 default:
4933 break;
Thierry FOURNIERb4be1292014-01-17 15:14:41 +01004934 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004935 }
4936
4937 /* display return value */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004938 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
4939 if (!pat || !pat->smp) {
4940 chunk_appendf(&trash, ", value=nothing");
4941 }
4942 else {
4943 smp = pat->smp;
4944 memcpy(&sample.data, &smp->data, sizeof(sample.data));
4945 sample.type = smp->type;
4946 if (sample_casts[sample.type][SMP_T_STR] &&
4947 sample_casts[sample.type][SMP_T_STR](&sample))
4948 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
4949 sample.data.str.str, smp_to_type[smp->type]);
4950 else
4951 chunk_appendf(&trash, ", value=cannot-display, type=\"%s\"",
4952 smp_to_type[smp->type]);
4953 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004954 }
4955
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004956 chunk_appendf(&trash, "\n");
4957
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004958 /* display response */
4959 if (bi_putchk(si->ib, &trash) == -1) {
4960 /* let's try again later from this session. We add ourselves into
4961 * this session's users so that it can remove us upon termination.
4962 */
4963 return 0;
4964 }
4965
4966 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004967 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
4968 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004969 }
4970
4971 appctx->st2 = STAT_ST_FIN;
4972 /* fall through */
4973
4974 default:
4975 appctx->st2 = STAT_ST_FIN;
4976 free(appctx->ctx.map.chunk.str);
4977 return 1;
4978 }
4979}
4980
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004981static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004982{
4983 struct appctx *appctx = __objt_appctx(si->end);
4984
4985 switch (appctx->st2) {
4986
4987 case STAT_ST_INIT:
4988 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004989 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
4990 struct pat_ref_elt *, list);
4991 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
4992 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004993 appctx->st2 = STAT_ST_LIST;
4994 /* fall through */
4995
4996 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004997 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004998 chunk_reset(&trash);
4999
5000 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005001 if (appctx->ctx.map.elt->sample)
5002 chunk_appendf(&trash, "%s %s\n",
5003 appctx->ctx.map.elt->pattern, appctx->ctx.map.elt->sample);
5004 else
5005 chunk_appendf(&trash, "%s\n", appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005006
5007 if (bi_putchk(si->ib, &trash) == -1) {
5008 /* let's try again later from this session. We add ourselves into
5009 * this session's users so that it can remove us upon termination.
5010 */
5011 return 0;
5012 }
5013
5014 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005015 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5016 struct pat_ref_elt *, list);
5017 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005018 break;
5019 }
5020
5021 appctx->st2 = STAT_ST_FIN;
5022 /* fall through */
5023
5024 default:
5025 appctx->st2 = STAT_ST_FIN;
5026 return 1;
5027 }
5028}
5029
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005030/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005031 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005032 * to be called again, otherwise non-zero. It is designed to be called
5033 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005034 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005035static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005036{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005037 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005038 struct connection *conn;
5039
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005040 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005041 /* If we're forced to shut down, we might have to remove our
5042 * reference to the last session being dumped.
5043 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005044 if (appctx->st2 == STAT_ST_LIST) {
5045 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5046 LIST_DEL(&appctx->ctx.sess.bref.users);
5047 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005048 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005049 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005050 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005051 }
5052
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005053 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005054
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005055 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005056 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005057 /* the function had not been called yet, let's prepare the
5058 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005059 * pointer to the first in the global list. When a target
5060 * session is being destroyed, it is responsible for updating
5061 * this pointer. We know we have reached the end when this
5062 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005063 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005064 LIST_INIT(&appctx->ctx.sess.bref.users);
5065 appctx->ctx.sess.bref.ref = sessions.n;
5066 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005067 /* fall through */
5068
Willy Tarreau295a8372011-03-10 11:25:07 +01005069 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005070 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005071 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5072 LIST_DEL(&appctx->ctx.sess.bref.users);
5073 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005074 }
5075
5076 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005077 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005078 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005079 struct session *curr_sess;
5080
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005081 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005082
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005083 if (appctx->ctx.sess.target) {
5084 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005085 goto next_sess;
5086
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005087 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005088 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005089 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005090 return 0;
5091
5092 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005093 LIST_DEL(&appctx->ctx.sess.bref.users);
5094 LIST_INIT(&appctx->ctx.sess.bref.users);
5095 if (appctx->ctx.sess.target != (void *)-1) {
5096 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005097 break;
5098 }
5099 else
5100 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005101 }
5102
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005103 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005104 "%p: proto=%s",
5105 curr_sess,
5106 curr_sess->listener->proto->name);
5107
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005108
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005109 conn = objt_conn(curr_sess->si[0].end);
5110 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005111 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005112 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005113 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005114 " src=%s:%d fe=%s be=%s srv=%s",
5115 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005116 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005117 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005118 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005119 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005120 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005121 break;
5122 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005123 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005124 " src=unix:%d fe=%s be=%s srv=%s",
5125 curr_sess->listener->luid,
5126 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005127 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005128 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005129 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005130 break;
5131 }
5132
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005133 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005134 " ts=%02x age=%s calls=%d",
5135 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005136 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5137 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005138
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005139 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005140 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005141 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005142 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005143 curr_sess->req->analysers,
5144 curr_sess->req->rex ?
5145 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5146 TICKS_TO_MS(1000)) : "");
5147
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005148 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005149 ",wx=%s",
5150 curr_sess->req->wex ?
5151 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5152 TICKS_TO_MS(1000)) : "");
5153
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005154 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005155 ",ax=%s]",
5156 curr_sess->req->analyse_exp ?
5157 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5158 TICKS_TO_MS(1000)) : "");
5159
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005160 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005161 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005162 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005163 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005164 curr_sess->rep->analysers,
5165 curr_sess->rep->rex ?
5166 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5167 TICKS_TO_MS(1000)) : "");
5168
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005169 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005170 ",wx=%s",
5171 curr_sess->rep->wex ?
5172 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5173 TICKS_TO_MS(1000)) : "");
5174
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005175 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005176 ",ax=%s]",
5177 curr_sess->rep->analyse_exp ?
5178 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5179 TICKS_TO_MS(1000)) : "");
5180
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005181 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005182 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005183 " s0=[%d,%1xh,fd=%d,ex=%s]",
5184 curr_sess->si[0].state,
5185 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005186 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005187 curr_sess->si[0].exp ?
5188 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5189 TICKS_TO_MS(1000)) : "");
5190
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005191 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005192 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005193 " s1=[%d,%1xh,fd=%d,ex=%s]",
5194 curr_sess->si[1].state,
5195 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005196 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005197 curr_sess->si[1].exp ?
5198 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5199 TICKS_TO_MS(1000)) : "");
5200
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005201 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005202 " exp=%s",
5203 curr_sess->task->expire ?
5204 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5205 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005206 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005207 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005208
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005209 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005210
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005211 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005212 /* let's try again later from this session. We add ourselves into
5213 * this session's users so that it can remove us upon termination.
5214 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005215 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005216 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005217 }
5218
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005219 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005220 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005221 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005222
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005223 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005224 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005225 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005226 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005227 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005228 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005229
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005230 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005231 return 0;
5232
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005233 appctx->ctx.sess.target = NULL;
5234 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005235 return 1;
5236 }
5237
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005238 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005239 /* fall through */
5240
5241 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005242 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005243 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005244 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005245}
5246
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005247/* This is called when the stream interface is closed. For instance, upon an
5248 * external abort, we won't call the i/o handler anymore so we may need to
5249 * remove back references to the session currently being dumped.
5250 */
Simon Horman74d88312013-02-12 10:45:50 +09005251static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005252{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005253 struct appctx *appctx = __objt_appctx(si->end);
5254
5255 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5256 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5257 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005258 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005259 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5260 free(appctx->ctx.map.chunk.str);
5261 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005262}
5263
Willy Tarreau20e99322013-04-13 09:22:25 +02005264/* This function is used to either dump tables states (when action is set
5265 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005266 * It returns 0 if the output buffer is full and it needs to be called
5267 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005268 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005269static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005270{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005271 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005272 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005273 struct ebmb_node *eb;
5274 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005275 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005276 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005277
5278 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005279 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005280 * - STAT_ST_INIT : the first call
5281 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005282 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005283 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005284 * and the entry pointer points to the next entry to be dumped,
5285 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005286 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005287 * data though.
5288 */
5289
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005290 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005291 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005292 if (appctx->st2 == STAT_ST_LIST) {
5293 appctx->ctx.table.entry->ref_cnt--;
5294 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005295 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005296 return 1;
5297 }
5298
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005299 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005300
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005301 while (appctx->st2 != STAT_ST_FIN) {
5302 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005303 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005304 appctx->ctx.table.proxy = appctx->ctx.table.target;
5305 if (!appctx->ctx.table.proxy)
5306 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005307
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005308 appctx->ctx.table.entry = NULL;
5309 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005310 break;
5311
Willy Tarreau295a8372011-03-10 11:25:07 +01005312 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005313 if (!appctx->ctx.table.proxy ||
5314 (appctx->ctx.table.target &&
5315 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5316 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005317 break;
5318 }
5319
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005320 if (appctx->ctx.table.proxy->table.size) {
5321 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5322 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005323 return 0;
5324
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005325 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005326 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005327 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005328 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005329 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005330 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5331 appctx->ctx.table.entry->ref_cnt++;
5332 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005333 break;
5334 }
5335 }
5336 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005337 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005338 break;
5339
Willy Tarreau295a8372011-03-10 11:25:07 +01005340 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005341 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005342
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005343 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005344 /* we're filtering on some data contents */
5345 void *ptr;
5346 long long data;
5347
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005348 dt = appctx->ctx.table.data_type;
5349 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5350 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005351 dt);
5352
5353 data = 0;
5354 switch (stktable_data_types[dt].std_type) {
5355 case STD_T_SINT:
5356 data = stktable_data_cast(ptr, std_t_sint);
5357 break;
5358 case STD_T_UINT:
5359 data = stktable_data_cast(ptr, std_t_uint);
5360 break;
5361 case STD_T_ULL:
5362 data = stktable_data_cast(ptr, std_t_ull);
5363 break;
5364 case STD_T_FRQP:
5365 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005366 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005367 break;
5368 }
5369
5370 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005371 if ((data < appctx->ctx.table.value &&
5372 (appctx->ctx.table.data_op == STD_OP_EQ ||
5373 appctx->ctx.table.data_op == STD_OP_GT ||
5374 appctx->ctx.table.data_op == STD_OP_GE)) ||
5375 (data == appctx->ctx.table.value &&
5376 (appctx->ctx.table.data_op == STD_OP_NE ||
5377 appctx->ctx.table.data_op == STD_OP_GT ||
5378 appctx->ctx.table.data_op == STD_OP_LT)) ||
5379 (data > appctx->ctx.table.value &&
5380 (appctx->ctx.table.data_op == STD_OP_EQ ||
5381 appctx->ctx.table.data_op == STD_OP_LT ||
5382 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005383 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005384 }
5385
Simon Hormanc88b8872011-06-15 15:18:49 +09005386 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005387 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5388 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005389 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005390
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005391 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005392
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005393 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005394 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005395 struct stksess *old = appctx->ctx.table.entry;
5396 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005397 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005398 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5399 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5400 stksess_kill(&appctx->ctx.table.proxy->table, old);
5401 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005402 break;
5403 }
5404
Simon Hormanc88b8872011-06-15 15:18:49 +09005405
5406 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005407 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5408 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5409 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005410
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005411 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5412 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005413 break;
5414
Willy Tarreau295a8372011-03-10 11:25:07 +01005415 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005416 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005417 break;
5418 }
5419 }
5420 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005421}
5422
Willy Tarreaud426a182010-03-05 14:58:26 +01005423/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005424 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5425 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5426 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5427 * lines are respected within the limit of 70 output chars. Lines that are
5428 * continuation of a previous truncated line begin with "+" instead of " "
5429 * after the offset. The new pointer is returned.
5430 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005431static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5432 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005433{
5434 int end;
5435 unsigned char c;
5436
5437 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005438 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005439 return ptr;
5440
Willy Tarreau77804732012-10-29 16:14:26 +01005441 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005442
Willy Tarreaud426a182010-03-05 14:58:26 +01005443 while (ptr < len && ptr < bsize) {
5444 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005445 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005446 if (out->len > end - 2)
5447 break;
5448 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005449 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005450 if (out->len > end - 3)
5451 break;
5452 out->str[out->len++] = '\\';
5453 switch (c) {
5454 case '\t': c = 't'; break;
5455 case '\n': c = 'n'; break;
5456 case '\r': c = 'r'; break;
5457 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005458 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005459 }
5460 out->str[out->len++] = c;
5461 } else {
5462 if (out->len > end - 5)
5463 break;
5464 out->str[out->len++] = '\\';
5465 out->str[out->len++] = 'x';
5466 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5467 out->str[out->len++] = hextab[c & 0xF];
5468 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005469 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005470 /* we had a line break, let's return now */
5471 out->str[out->len++] = '\n';
5472 *line = ptr;
5473 return ptr;
5474 }
5475 }
5476 /* we have an incomplete line, we return it as-is */
5477 out->str[out->len++] = '\n';
5478 return ptr;
5479}
5480
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005481/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005482 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005483 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005484 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005485static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005486{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005487 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005488 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005489
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005490 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005491 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005492
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005493 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005494
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005495 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005496 /* the function had not been called yet, let's prepare the
5497 * buffer for a response.
5498 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005499 struct tm tm;
5500
5501 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005502 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005503 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5504 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5505 error_snapshot_id);
5506
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005507 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005508 /* Socket buffer full. Let's try again later from the same point */
5509 return 0;
5510 }
5511
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005512 appctx->ctx.errors.px = proxy;
5513 appctx->ctx.errors.buf = 0;
5514 appctx->ctx.errors.bol = 0;
5515 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005516 }
5517
5518 /* we have two inner loops here, one for the proxy, the other one for
5519 * the buffer.
5520 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005521 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005522 struct error_snapshot *es;
5523
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005524 if (appctx->ctx.errors.buf == 0)
5525 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005526 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005527 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005528
5529 if (!es->when.tv_sec)
5530 goto next;
5531
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005532 if (appctx->ctx.errors.iid >= 0 &&
5533 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5534 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005535 goto next;
5536
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005537 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005538 /* just print headers now */
5539
5540 char pn[INET6_ADDRSTRLEN];
5541 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005542 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005543
5544 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005545 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005546 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005547 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005548
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005549 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5550 case AF_INET:
5551 case AF_INET6:
5552 port = get_host_port(&es->src);
5553 break;
5554 default:
5555 port = 0;
5556 }
5557
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005558 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005559 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005560 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005561 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005562 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005563 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005564 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5565 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005566 break;
5567 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005568 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005569 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005570 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005571 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005572 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005573 break;
5574 }
5575
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005576 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005577 ", server %s (#%d), event #%u\n"
5578 " src %s:%d, session #%d, session flags 0x%08x\n"
5579 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5580 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5581 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5582 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5583 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5584 es->ev_id,
5585 pn, port, es->sid, es->s_flags,
5586 es->state, es->m_flags, es->t_flags,
5587 es->m_clen, es->m_blen,
5588 es->b_flags, es->b_out, es->b_tot,
5589 es->len, es->b_wrap, es->pos);
5590
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005591 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005592 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005593 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005594 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005595 appctx->ctx.errors.ptr = 0;
5596 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005597 }
5598
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005599 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005600 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005601 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005602 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005603 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005604 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005605 goto next;
5606 }
5607
5608 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005609 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005610 int newptr;
5611 int newline;
5612
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005613 newline = appctx->ctx.errors.bol;
5614 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5615 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005616 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005617
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005618 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005619 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005620 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005621 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005622 appctx->ctx.errors.ptr = newptr;
5623 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005624 };
5625 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005626 appctx->ctx.errors.bol = 0;
5627 appctx->ctx.errors.ptr = -1;
5628 appctx->ctx.errors.buf++;
5629 if (appctx->ctx.errors.buf > 1) {
5630 appctx->ctx.errors.buf = 0;
5631 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005632 }
5633 }
5634
5635 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005636 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005637}
5638
Willy Tarreaud5781202012-09-22 19:32:35 +02005639/* parse the "level" argument on the bind lines */
5640static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5641{
5642 if (!*args[cur_arg + 1]) {
5643 memprintf(err, "'%s' : missing level", args[cur_arg]);
5644 return ERR_ALERT | ERR_FATAL;
5645 }
5646
5647 if (!strcmp(args[cur_arg+1], "user"))
5648 conf->level = ACCESS_LVL_USER;
5649 else if (!strcmp(args[cur_arg+1], "operator"))
5650 conf->level = ACCESS_LVL_OPER;
5651 else if (!strcmp(args[cur_arg+1], "admin"))
5652 conf->level = ACCESS_LVL_ADMIN;
5653 else {
5654 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5655 args[cur_arg], args[cur_arg+1]);
5656 return ERR_ALERT | ERR_FATAL;
5657 }
5658
5659 return 0;
5660}
5661
Willy Tarreaub24281b2011-02-13 13:16:36 +01005662struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005663 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005664 .name = "<STATS>", /* used for logging */
5665 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005666 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005667};
5668
Simon Horman9bd2c732011-06-15 15:18:44 +09005669static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005670 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005671 .name = "<CLI>", /* used for logging */
5672 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005673 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005674};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005675
Willy Tarreaudc13c112013-06-21 23:16:39 +02005676static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005677 { CFG_GLOBAL, "stats", stats_parse_global },
5678 { 0, NULL, NULL },
5679}};
5680
Willy Tarreaud5781202012-09-22 19:32:35 +02005681static struct bind_kw_list bind_kws = { "STAT", { }, {
5682 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5683 { NULL, NULL, 0 },
5684}};
5685
Willy Tarreau10522fd2008-07-09 20:12:41 +02005686__attribute__((constructor))
5687static void __dumpstats_module_init(void)
5688{
5689 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005690 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005691}
5692
Willy Tarreau91861262007-10-17 17:06:05 +02005693/*
5694 * Local variables:
5695 * c-indent-level: 8
5696 * c-basic-offset: 8
5697 * End:
5698 */