blob: d9632b7697d08a2c2481336fdf20f7dfc6a5061c [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 FOURNIERaf5a29d2014-03-11 14:29:22 +0100985static inline
986struct pat_ref *pat_ref_lookup_ref(const char *reference)
987{
988 int id;
989 char *error;
990
991 /* If the reference starts by a '#', this is numeric id. */
992 if (reference[0] == '#') {
993 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
994 id = strtol(reference + 1, &error, 10);
995 if (*error != '\0')
996 return NULL;
997
998 /* Perform the unique id lookup. */
999 return pat_ref_lookupid(id);
1000 }
1001
1002 /* Perform the string lookup. */
1003 return pat_ref_lookup(reference);
1004}
1005
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001006/* This function is used with map and acl management. It permits to browse
1007 * each reference.
1008 */
1009static inline
1010struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1011{
1012 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001013 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1014 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001015 return NULL;
1016 return expr;
1017}
1018
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001019/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001020 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001021 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001022 * designating the function which will have to process the request, which can
1023 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001024 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001025static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001026{
Willy Tarreau306f8302013-07-08 15:53:06 +02001027 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001028 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001029 char *args[MAX_STATS_ARGS + 1];
1030 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001031 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001032
1033 while (isspace((unsigned char)*line))
1034 line++;
1035
1036 arg = 0;
1037 args[arg] = line;
1038
1039 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001040 if (*line == '\\') {
1041 line++;
1042 if (*line == '\0')
1043 break;
1044 }
1045 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001046 *line++ = '\0';
1047
1048 while (isspace((unsigned char)*line))
1049 line++;
1050
1051 args[++arg] = line;
1052 continue;
1053 }
1054
1055 line++;
1056 }
1057
1058 while (++arg <= MAX_STATS_ARGS)
1059 args[arg] = line;
1060
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001061 /* remove \ */
1062 arg = 0;
1063 while (*args[arg] != '\0') {
1064 j = 0;
1065 for (i=0; args[arg][i] != '\0'; i++) {
1066 if (args[arg][i] == '\\')
1067 continue;
1068 args[arg][j] = args[arg][i];
1069 j++;
1070 }
1071 args[arg][j] = '\0';
1072 arg++;
1073 }
1074
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001075 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001076 if (strcmp(args[0], "show") == 0) {
1077 if (strcmp(args[1], "stat") == 0) {
1078 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001079 appctx->ctx.stats.flags |= STAT_BOUND;
1080 appctx->ctx.stats.iid = atoi(args[2]);
1081 appctx->ctx.stats.type = atoi(args[3]);
1082 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001083 }
1084
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001085 appctx->st2 = STAT_ST_INIT;
1086 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001087 }
1088 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001089 appctx->st2 = STAT_ST_INIT;
1090 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001091 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001092 else if (strcmp(args[1], "pools") == 0) {
1093 appctx->st2 = STAT_ST_INIT;
1094 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1095 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001096 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001097 appctx->st2 = STAT_ST_INIT;
Willy Tarreau290e63a2012-09-20 18:07:14 +02001098 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001099 appctx->ctx.cli.msg = stats_permission_denied_msg;
1100 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001101 return 1;
1102 }
Willy Tarreau76153662012-11-26 01:16:39 +01001103 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001104 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001105 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001106 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001107 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001108 appctx->ctx.sess.target = NULL;
1109 appctx->ctx.sess.section = 0; /* start with session status */
1110 appctx->ctx.sess.pos = 0;
1111 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001112 }
1113 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreau290e63a2012-09-20 18:07:14 +02001114 if (s->listener->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001115 appctx->ctx.cli.msg = stats_permission_denied_msg;
1116 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001117 return 1;
1118 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001119 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001120 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001121 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001122 appctx->ctx.errors.iid = -1;
1123 appctx->ctx.errors.px = NULL;
1124 appctx->st2 = STAT_ST_INIT;
1125 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001126 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001127 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001128 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001129 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001130 else if (strcmp(args[1], "map") == 0 ||
1131 strcmp(args[1], "acl") == 0) {
1132
1133 /* Set ACL or MAP flags. */
1134 if (args[1][0] == 'm')
1135 appctx->ctx.map.display_flags = PAT_REF_MAP;
1136 else
1137 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001138
1139 /* no parameter: display all map avalaible */
1140 if (!*args[2]) {
1141 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001142 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001143 return 1;
1144 }
1145
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001146 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001147 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001148 if (!appctx->ctx.map.ref ||
1149 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1150 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001151 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <name>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001152 else
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001153 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001154 appctx->st0 = STAT_CLI_PRINT;
1155 return 1;
1156 }
1157 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001158 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001159 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001160 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001161 return 0;
1162 }
1163 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001164 else if (strcmp(args[0], "clear") == 0) {
1165 if (strcmp(args[1], "counters") == 0) {
1166 struct proxy *px;
1167 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001168 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001169 int clrall = 0;
1170
1171 if (strcmp(args[2], "all") == 0)
1172 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001173
Willy Tarreau6162db22009-10-10 17:13:00 +02001174 /* check permissions */
Willy Tarreau290e63a2012-09-20 18:07:14 +02001175 if (s->listener->bind_conf->level < ACCESS_LVL_OPER ||
1176 (clrall && s->listener->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001177 appctx->ctx.cli.msg = stats_permission_denied_msg;
1178 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001179 return 1;
1180 }
1181
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001182 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001183 if (clrall) {
1184 memset(&px->be_counters, 0, sizeof(px->be_counters));
1185 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1186 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001187 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001188 px->be_counters.conn_max = 0;
1189 px->be_counters.p.http.rps_max = 0;
1190 px->be_counters.sps_max = 0;
1191 px->be_counters.cps_max = 0;
1192 px->be_counters.nbpend_max = 0;
1193
1194 px->fe_counters.conn_max = 0;
1195 px->fe_counters.p.http.rps_max = 0;
1196 px->fe_counters.sps_max = 0;
1197 px->fe_counters.cps_max = 0;
1198 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001199 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001200
1201 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001202 if (clrall)
1203 memset(&sv->counters, 0, sizeof(sv->counters));
1204 else {
1205 sv->counters.cur_sess_max = 0;
1206 sv->counters.nbpend_max = 0;
1207 sv->counters.sps_max = 0;
1208 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001209
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001210 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001211 if (li->counters) {
1212 if (clrall)
1213 memset(li->counters, 0, sizeof(*li->counters));
1214 else
1215 li->counters->conn_max = 0;
1216 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001217 }
1218
Willy Tarreau81c25d02011-09-07 15:17:21 +02001219 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001220 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001221 return 1;
1222 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001223 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001224 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001225 /* end of processing */
1226 return 1;
1227 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001228 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1229 /* Set ACL or MAP flags. */
1230 if (args[1][0] == 'm')
1231 appctx->ctx.map.display_flags = PAT_REF_MAP;
1232 else
1233 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001234
1235 /* no parameter */
1236 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001237 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1238 appctx->ctx.cli.msg = "Missing map identifier.\n";
1239 else
1240 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001241 appctx->st0 = STAT_CLI_PRINT;
1242 return 1;
1243 }
1244
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001245 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001246 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001247 if (!appctx->ctx.map.ref ||
1248 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1249 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001250 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <name>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001251 else
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001252 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001253 appctx->st0 = STAT_CLI_PRINT;
1254 return 1;
1255 }
1256
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001257 /* Clear all. */
1258 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001259
1260 /* return response */
1261 appctx->ctx.cli.msg = "Done.\n";
1262 appctx->st0 = STAT_CLI_PRINT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001263 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001264 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001265 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001266 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001267 return 0;
1268 }
1269 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001270 else if (strcmp(args[0], "get") == 0) {
1271 if (strcmp(args[1], "weight") == 0) {
1272 struct proxy *px;
1273 struct server *sv;
1274
1275 /* split "backend/server" and make <line> point to server */
1276 for (line = args[2]; *line; line++)
1277 if (*line == '/') {
1278 *line++ = '\0';
1279 break;
1280 }
1281
1282 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001283 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1284 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001285 return 1;
1286 }
1287
1288 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001289 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1290 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001291 return 1;
1292 }
1293
1294 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001295 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
1296 bi_putstr(si->ib, trash.str);
Willy Tarreau38338fa2009-10-10 18:37:29 +02001297 return 1;
1298 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001299 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1300 /* Set flags. */
1301 if (args[1][0] == 'm')
1302 appctx->ctx.map.display_flags = PAT_REF_MAP;
1303 else
1304 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001305
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001306 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001307 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001308 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1309 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1310 else
1311 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001312 appctx->st0 = STAT_CLI_PRINT;
1313 return 1;
1314 }
1315
1316 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001317 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001318 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001319 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001320 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <name>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001321 else
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001322 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001323 appctx->st0 = STAT_CLI_PRINT;
1324 return 1;
1325 }
1326
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001327 /* copy input string. The string must be allocated because
1328 * it may be used over multiple iterations. It's released
1329 * at the end and upon abort anyway.
1330 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001331 appctx->ctx.map.chunk.len = strlen(args[3]);
1332 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1333 appctx->ctx.map.chunk.str = strdup(args[3]);
1334 if (!appctx->ctx.map.chunk.str) {
1335 appctx->ctx.cli.msg = "Out of memory error.\n";
1336 appctx->st0 = STAT_CLI_PRINT;
1337 return 1;
1338 }
1339
1340 /* prepare response */
1341 appctx->st2 = STAT_ST_INIT;
1342 appctx->st0 = STAT_CLI_O_MLOOK;
1343 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001344 else { /* not "get weight" */
1345 return 0;
1346 }
1347 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001348 else if (strcmp(args[0], "set") == 0) {
1349 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001350 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001351 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001352
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001353 sv = expect_server_admin(s, si, args[2]);
1354 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001355 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001356
Simon Horman7d09b9a2013-02-12 10:45:51 +09001357 warning = server_parse_weight_change_request(sv, args[3]);
Simon Horman8c3d0be2013-11-25 10:46:40 +09001358 /*
1359 * The user-weight may now be zero and thus
1360 * the server considered to be draining.
1361 * Update the server's drain state as necessary.
1362 */
1363 set_server_drain_state(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001364 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001365 appctx->ctx.cli.msg = warning;
1366 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001367 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001368 return 1;
1369 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001370 else if (strcmp(args[1], "timeout") == 0) {
1371 if (strcmp(args[2], "cli") == 0) {
1372 unsigned timeout;
1373 const char *res;
1374
1375 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001376 appctx->ctx.cli.msg = "Expects an integer value.\n";
1377 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001378 return 1;
1379 }
1380
1381 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1382 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001383 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1384 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001385 return 1;
1386 }
1387
1388 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1389 return 1;
1390 }
1391 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001392 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1393 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001394 return 1;
1395 }
1396 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001397 else if (strcmp(args[1], "maxconn") == 0) {
1398 if (strcmp(args[2], "frontend") == 0) {
1399 struct proxy *px;
1400 struct listener *l;
1401 int v;
1402
Willy Tarreau532a4502011-09-07 22:37:44 +02001403 px = expect_frontend_admin(s, si, args[3]);
1404 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001405 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001406
1407 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001408 appctx->ctx.cli.msg = "Integer value expected.\n";
1409 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001410 return 1;
1411 }
1412
1413 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001414 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001415 appctx->ctx.cli.msg = "Value out of range.\n";
1416 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001417 return 1;
1418 }
1419
1420 /* OK, the value is fine, so we assign it to the proxy and to all of
1421 * its listeners. The blocked ones will be dequeued.
1422 */
1423 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001424 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001425 l->maxconn = v;
1426 if (l->state == LI_FULL)
1427 resume_listener(l);
1428 }
1429
1430 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1431 dequeue_all_listeners(&s->fe->listener_queue);
1432
1433 return 1;
1434 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001435 else if (strcmp(args[2], "global") == 0) {
1436 int v;
1437
Willy Tarreau290e63a2012-09-20 18:07:14 +02001438 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001439 appctx->ctx.cli.msg = stats_permission_denied_msg;
1440 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001441 return 1;
1442 }
1443
1444 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001445 appctx->ctx.cli.msg = "Expects an integer value.\n";
1446 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001447 return 1;
1448 }
1449
1450 v = atoi(args[3]);
1451 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001452 appctx->ctx.cli.msg = "Value out of range.\n";
1453 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001454 return 1;
1455 }
1456
1457 /* check for unlimited values */
1458 if (v <= 0)
1459 v = global.hardmaxconn;
1460
1461 global.maxconn = v;
1462
1463 /* Dequeues all of the listeners waiting for a resource */
1464 if (!LIST_ISEMPTY(&global_listener_queue))
1465 dequeue_all_listeners(&global_listener_queue);
1466
1467 return 1;
1468 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001469 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001470 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1471 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001472 return 1;
1473 }
1474 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001475 else if (strcmp(args[1], "rate-limit") == 0) {
1476 if (strcmp(args[2], "connections") == 0) {
1477 if (strcmp(args[3], "global") == 0) {
1478 int v;
1479
Willy Tarreau290e63a2012-09-20 18:07:14 +02001480 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001481 appctx->ctx.cli.msg = stats_permission_denied_msg;
1482 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001483 return 1;
1484 }
1485
1486 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001487 appctx->ctx.cli.msg = "Expects an integer value.\n";
1488 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001489 return 1;
1490 }
1491
1492 v = atoi(args[4]);
1493 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001494 appctx->ctx.cli.msg = "Value out of range.\n";
1495 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001496 return 1;
1497 }
1498
1499 global.cps_lim = v;
1500
1501 /* Dequeues all of the listeners waiting for a resource */
1502 if (!LIST_ISEMPTY(&global_listener_queue))
1503 dequeue_all_listeners(&global_listener_queue);
1504
1505 return 1;
1506 }
1507 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001508 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1509 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001510 return 1;
1511 }
1512 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001513 else if (strcmp(args[2], "sessions") == 0) {
1514 if (strcmp(args[3], "global") == 0) {
1515 int v;
1516
1517 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1518 appctx->ctx.cli.msg = stats_permission_denied_msg;
1519 appctx->st0 = STAT_CLI_PRINT;
1520 return 1;
1521 }
1522
1523 if (!*args[4]) {
1524 appctx->ctx.cli.msg = "Expects an integer value.\n";
1525 appctx->st0 = STAT_CLI_PRINT;
1526 return 1;
1527 }
1528
1529 v = atoi(args[4]);
1530 if (v < 0) {
1531 appctx->ctx.cli.msg = "Value out of range.\n";
1532 appctx->st0 = STAT_CLI_PRINT;
1533 return 1;
1534 }
1535
1536 global.sps_lim = v;
1537
1538 /* Dequeues all of the listeners waiting for a resource */
1539 if (!LIST_ISEMPTY(&global_listener_queue))
1540 dequeue_all_listeners(&global_listener_queue);
1541
1542 return 1;
1543 }
1544 else {
1545 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1546 appctx->st0 = STAT_CLI_PRINT;
1547 return 1;
1548 }
1549 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001550#ifdef USE_OPENSSL
1551 else if (strcmp(args[2], "ssl-sessions") == 0) {
1552 if (strcmp(args[3], "global") == 0) {
1553 int v;
1554
1555 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1556 appctx->ctx.cli.msg = stats_permission_denied_msg;
1557 appctx->st0 = STAT_CLI_PRINT;
1558 return 1;
1559 }
1560
1561 if (!*args[4]) {
1562 appctx->ctx.cli.msg = "Expects an integer value.\n";
1563 appctx->st0 = STAT_CLI_PRINT;
1564 return 1;
1565 }
1566
1567 v = atoi(args[4]);
1568 if (v < 0) {
1569 appctx->ctx.cli.msg = "Value out of range.\n";
1570 appctx->st0 = STAT_CLI_PRINT;
1571 return 1;
1572 }
1573
1574 global.ssl_lim = v;
1575
1576 /* Dequeues all of the listeners waiting for a resource */
1577 if (!LIST_ISEMPTY(&global_listener_queue))
1578 dequeue_all_listeners(&global_listener_queue);
1579
1580 return 1;
1581 }
1582 else {
1583 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1584 appctx->st0 = STAT_CLI_PRINT;
1585 return 1;
1586 }
1587 }
1588#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001589 else if (strcmp(args[2], "http-compression") == 0) {
1590 if (strcmp(args[3], "global") == 0) {
1591 int v;
1592
Willy Tarreau85d47f92012-11-21 00:29:50 +01001593 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001594 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1595 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001596 return 1;
1597 }
1598
William Lallemandd85f9172012-11-09 17:05:39 +01001599 v = atoi(args[4]);
1600 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1601 }
1602 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001603 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1604 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001605 return 1;
1606 }
1607 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001608 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001609 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001610 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001611 return 1;
1612 }
1613 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001614 else if (strcmp(args[1], "table") == 0) {
1615 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1616 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001617 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001618 /* Set flags. */
1619 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001620
1621 /* Expect three parameters: map name, key and new value. */
1622 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001623 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001624 appctx->st0 = STAT_CLI_PRINT;
1625 return 1;
1626 }
1627
1628 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001629 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001630 if (!appctx->ctx.map.ref) {
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001631 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001632 appctx->st0 = STAT_CLI_PRINT;
1633 return 1;
1634 }
1635
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001636 /* If the entry identifier start with a '#', it is considered as
1637 * pointer id
1638 */
1639 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1640 struct pat_ref_elt *ref;
1641 long long int conv;
1642 char *error;
1643
1644 /* Convert argument to integer value. */
1645 conv = strtoll(&args[3][1], &error, 16);
1646 if (*error != '\0') {
1647 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <name>.\n";
1648 appctx->st0 = STAT_CLI_PRINT;
1649 return 1;
1650 }
1651
1652 /* Convert and check integer to pointer. */
1653 ref = (struct pat_ref_elt *)(long)conv;
1654 if ((long long int)(long)ref != conv) {
1655 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <name>.\n";
1656 appctx->st0 = STAT_CLI_PRINT;
1657 return 1;
1658 }
1659
1660 /* Try to delete the entry. */
1661 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4])) {
1662 appctx->ctx.cli.msg = "Pattern not found.\n";
1663 appctx->st0 = STAT_CLI_PRINT;
1664 return 1;
1665 }
1666 }
1667 else {
1668 /* Else, use the entry identifier as pattern
1669 * string, and update the value.
1670 */
1671 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4])) {
1672 appctx->ctx.cli.msg = "Pattern not found.\n";
1673 appctx->st0 = STAT_CLI_PRINT;
1674 return 1;
1675 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001676 }
1677
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001678 /* The set is done, send message. */
1679 appctx->ctx.cli.msg = "Done.\n";
1680 appctx->st0 = STAT_CLI_PRINT;
1681 return 1;
1682 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001683 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001684 return 0;
1685 }
1686 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001687 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001688 if (strcmp(args[1], "agent") == 0) {
1689 struct server *sv;
1690
1691 sv = expect_server_admin(s, si, args[2]);
1692 if (!sv)
1693 return 1;
1694
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001695 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1696 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1697 appctx->st0 = STAT_CLI_PRINT;
1698 return 1;
1699 }
1700
1701 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001702 return 1;
1703 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001704 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001705 struct server *sv;
1706
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001707 sv = expect_server_admin(s, si, args[2]);
1708 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001709 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001710
Cyril Bontécd19e512010-01-31 22:34:03 +01001711 if (sv->state & SRV_MAINTAIN) {
1712 /* The server is really in maintenance, we can change the server state */
Willy Tarreau44267702011-10-28 15:35:33 +02001713 if (sv->track) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001714 /* If this server tracks the status of another one,
1715 * we must restore the good status.
1716 */
Willy Tarreau44267702011-10-28 15:35:33 +02001717 if (sv->track->state & SRV_RUNNING) {
Simon Horman4a741432013-02-23 15:35:38 +09001718 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001719 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001720 } else {
1721 sv->state &= ~SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001722 sv->check.state &= ~CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001723 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001724 }
1725 } else {
Simon Horman4a741432013-02-23 15:35:38 +09001726 set_server_up(&sv->check);
Simon Horman58c32972013-11-25 10:46:38 +09001727 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
Cyril Bontécd19e512010-01-31 22:34:03 +01001728 }
1729 }
1730
Willy Tarreau532a4502011-09-07 22:37:44 +02001731 return 1;
1732 }
1733 else if (strcmp(args[1], "frontend") == 0) {
1734 struct proxy *px;
1735
1736 px = expect_frontend_admin(s, si, args[2]);
1737 if (!px)
1738 return 1;
1739
1740 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001741 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1742 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001743 return 1;
1744 }
1745
1746 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001747 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1748 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001749 return 1;
1750 }
1751
1752 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001753 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1754 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001755 return 1;
1756 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001757 return 1;
1758 }
1759 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001760 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1761 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001762 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001763 }
1764 }
1765 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001766 if (strcmp(args[1], "agent") == 0) {
1767 struct server *sv;
1768
1769 sv = expect_server_admin(s, si, args[2]);
1770 if (!sv)
1771 return 1;
1772
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001773 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001774 return 1;
1775 }
1776 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001777 struct server *sv;
1778
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001779 sv = expect_server_admin(s, si, args[2]);
1780 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001781 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001782
Cyril Bontécd19e512010-01-31 22:34:03 +01001783 if (! (sv->state & SRV_MAINTAIN)) {
1784 /* Not already in maintenance, we can change the server state */
1785 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01001786 sv->check.state |= CHK_ST_PAUSED;
Simon Horman4a741432013-02-23 15:35:38 +09001787 set_server_down(&sv->check);
Cyril Bontécd19e512010-01-31 22:34:03 +01001788 }
1789
Willy Tarreau532a4502011-09-07 22:37:44 +02001790 return 1;
1791 }
1792 else if (strcmp(args[1], "frontend") == 0) {
1793 struct proxy *px;
1794
1795 px = expect_frontend_admin(s, si, args[2]);
1796 if (!px)
1797 return 1;
1798
1799 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001800 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1801 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001802 return 1;
1803 }
1804
1805 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001806 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1807 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001808 return 1;
1809 }
1810
1811 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001812 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1813 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001814 return 1;
1815 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001816 return 1;
1817 }
1818 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001819 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1820 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001821 return 1;
1822 }
1823 }
1824 else if (strcmp(args[0], "shutdown") == 0) {
1825 if (strcmp(args[1], "frontend") == 0) {
1826 struct proxy *px;
1827
1828 px = expect_frontend_admin(s, si, args[2]);
1829 if (!px)
1830 return 1;
1831
1832 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001833 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1834 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001835 return 1;
1836 }
1837
1838 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1839 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1840 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1841 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1842 stop_proxy(px);
1843 return 1;
1844 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001845 else if (strcmp(args[1], "session") == 0) {
1846 struct session *sess, *ptr;
1847
Willy Tarreau290e63a2012-09-20 18:07:14 +02001848 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001849 appctx->ctx.cli.msg = stats_permission_denied_msg;
1850 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001851 return 1;
1852 }
1853
1854 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001855 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1856 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001857 return 1;
1858 }
1859
1860 ptr = (void *)strtoul(args[2], NULL, 0);
1861
1862 /* first, look for the requested session in the session table */
1863 list_for_each_entry(sess, &sessions, list) {
1864 if (sess == ptr)
1865 break;
1866 }
1867
1868 /* do we have the session ? */
1869 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001870 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1871 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001872 return 1;
1873 }
1874
1875 session_shutdown(sess, SN_ERR_KILLED);
1876 return 1;
1877 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001878 else if (strcmp(args[1], "sessions") == 0) {
1879 if (strcmp(args[2], "server") == 0) {
1880 struct server *sv;
1881 struct session *sess, *sess_bck;
1882
1883 sv = expect_server_admin(s, si, args[3]);
1884 if (!sv)
1885 return 1;
1886
1887 /* kill all the session that are on this server */
1888 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1889 if (sess->srv_conn == sv)
1890 session_shutdown(sess, SN_ERR_KILLED);
1891
1892 return 1;
1893 }
1894 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001895 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1896 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001897 return 1;
1898 }
1899 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001900 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001901 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1902 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001903 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001904 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001905 }
1906 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001907 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
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;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001912
1913 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001914 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1915 if (!*args[2] || !*args[3]) {
1916 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
1917 appctx->st0 = STAT_CLI_PRINT;
1918 return 1;
1919 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001920 }
1921
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001922 else {
1923 if (!*args[2] || !*args[3]) {
1924 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
1925 appctx->st0 = STAT_CLI_PRINT;
1926 return 1;
1927 }
1928 }
1929
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001930 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001931 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001932 if (!appctx->ctx.map.ref ||
1933 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001934 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001935 appctx->st0 = STAT_CLI_PRINT;
1936 return 1;
1937 }
1938
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001939 /* If the entry identifier start with a '#', it is considered as
1940 * pointer id
1941 */
1942 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1943 struct pat_ref_elt *ref;
1944 long long int conv;
1945 char *error;
1946
1947 /* Convert argument to integer value. */
1948 conv = strtoll(&args[3][1], &error, 16);
1949 if (*error != '\0') {
1950 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <name>.\n";
1951 appctx->st0 = STAT_CLI_PRINT;
1952 return 1;
1953 }
1954
1955 /* Convert and check integer to pointer. */
1956 ref = (struct pat_ref_elt *)(long)conv;
1957 if ((long long int)(long)ref != conv) {
1958 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <name>.\n";
1959 appctx->st0 = STAT_CLI_PRINT;
1960 return 1;
1961 }
1962
1963 /* Try to delete the entry. */
1964 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
1965 /* The entry is not found, send message. */
1966 appctx->ctx.cli.msg = "Key not found.\n";
1967 appctx->st0 = STAT_CLI_PRINT;
1968 return 1;
1969 }
1970 }
1971 else {
1972 /* Else, use the entry identifier as pattern
1973 * string and try to delete the entry.
1974 */
1975 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
1976 /* The entry is not found, send message. */
1977 appctx->ctx.cli.msg = "Key not found.\n";
1978 appctx->st0 = STAT_CLI_PRINT;
1979 return 1;
1980 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001981 }
1982
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001983 /* The deletion is done, send message. */
1984 appctx->ctx.cli.msg = "Done.\n";
1985 appctx->st0 = STAT_CLI_PRINT;
1986 return 1;
1987 }
1988 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001989 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001990 appctx->st0 = STAT_CLI_PRINT;
1991 return 1;
1992 }
1993 }
1994 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001995 if (strcmp(args[1], "map") == 0 ||
1996 strcmp(args[1], "acl") == 0) {
1997 int ret;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001998
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001999 /* Set flags. */
2000 if (args[1][0] == 'm')
2001 appctx->ctx.map.display_flags = PAT_REF_MAP;
2002 else
2003 appctx->ctx.map.display_flags = PAT_REF_ACL;
2004
2005 /* If the keywork is "map", we expect three parameters, if it
2006 * is "acl", we expect only two parameters
2007 */
2008 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2009 if (!*args[2] || !*args[3] || !*args[4]) {
2010 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2011 appctx->st0 = STAT_CLI_PRINT;
2012 return 1;
2013 }
2014 }
2015 else {
2016 if (!*args[2] || !*args[3]) {
2017 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2018 appctx->st0 = STAT_CLI_PRINT;
2019 return 1;
2020 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002021 }
2022
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002023 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002024 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002025 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002026 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002027 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <name>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002028 else
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002029 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <name>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002030 appctx->st0 = STAT_CLI_PRINT;
2031 return 1;
2032 }
2033
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002034 /* Add value. */
2035 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
2036 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], NULL);
2037 else
2038 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, NULL);
2039 if (!ret) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002040 appctx->ctx.cli.msg = "Out of memory error.\n";
2041 appctx->st0 = STAT_CLI_PRINT;
2042 return 1;
2043 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002044
2045 /* The add is done, send message. */
2046 appctx->ctx.cli.msg = "Done.\n";
2047 appctx->st0 = STAT_CLI_PRINT;
2048 return 1;
2049 }
2050 else { /* unknown "del" parameter */
2051 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2052 appctx->st0 = STAT_CLI_PRINT;
2053 return 1;
2054 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002055 }
2056 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002057 return 0;
2058 }
2059 return 1;
2060}
2061
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002062/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002063 * used to processes I/O from/to the stats unix socket. The system relies on a
2064 * state machine handling requests and various responses. We read a request,
2065 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002066 * Then we can read again. The state is stored in appctx->st0 and is one of the
2067 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002068 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002069 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002070static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002071{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002072 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002073 struct channel *req = si->ob;
2074 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002075 int reql;
2076 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002077
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002078 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2079 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002080
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002081 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002082 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002083 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002084 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2085 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002086 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002087 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002088 /* Let's close for real now. We just close the request
2089 * side, the conditions below will complete if needed.
2090 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002091 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002092 break;
2093 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002094 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002095 /* ensure we have some output room left in the event we
2096 * would want to return some info right after parsing.
2097 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002098 if (buffer_almost_full(si->ib->buf)) {
2099 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002100 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002101 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002102
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002103 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002104 if (reql <= 0) { /* closed or EOL not found */
2105 if (reql == 0)
2106 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002107 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002108 continue;
2109 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002110
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002111 /* seek for a possible semi-colon. If we find one, we
2112 * replace it with an LF and skip only this part.
2113 */
2114 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002115 if (trash.str[len] == ';') {
2116 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002117 reql = len + 1;
2118 break;
2119 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002120
Willy Tarreau816fc222009-10-04 07:36:58 +02002121 /* now it is time to check that we have a full line,
2122 * remove the trailing \n and possibly \r, then cut the
2123 * line.
2124 */
2125 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002126 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002127 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002128 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002129 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002130
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002131 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002132 len--;
2133
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002134 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002135
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002136 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002137 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002138 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002139 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002140 continue;
2141 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002142 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002143 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002144 else if (strcmp(trash.str, "help") == 0 ||
2145 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002146 appctx->ctx.cli.msg = stats_sock_usage_msg;
2147 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002148 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002149 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002150 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002151 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002152 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002153 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002154 /* if prompt is disabled, print help on empty lines,
2155 * so that the user at least knows how to enable
2156 * prompt and find help.
2157 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002158 appctx->ctx.cli.msg = stats_sock_usage_msg;
2159 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002160 }
2161
2162 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002163 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002164 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002165 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002166 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002167 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002168 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002169 continue;
2170 }
2171
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002172 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002173 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002174 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2175 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002176 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002177 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002178 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002179 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002180 break;
2181 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002182 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002183 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002184 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002185 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002186 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002187 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002188 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002189 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002190 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002191 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002192 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002193 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002194 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002195 if (stats_table_request(si, appctx->st0))
2196 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002197 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002198 case STAT_CLI_O_PATS:
2199 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002200 appctx->st0 = STAT_CLI_PROMPT;
2201 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002202 case STAT_CLI_O_PAT:
2203 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002204 appctx->st0 = STAT_CLI_PROMPT;
2205 break;
2206 case STAT_CLI_O_MLOOK:
2207 if (stats_map_lookup(si))
2208 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002209 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002210 case STAT_CLI_O_POOLS:
2211 if (stats_dump_pools_to_buffer(si))
2212 appctx->st0 = STAT_CLI_PROMPT;
2213 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002214 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002215 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002216 break;
2217 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002218
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002219 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002220 if (appctx->st0 == STAT_CLI_PROMPT) {
2221 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2222 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002223 }
2224
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002225 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002226 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002227 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002228
2229 /* Now we close the output if one of the writers did so,
2230 * or if we're not in interactive mode and the request
2231 * buffer is empty. This still allows pipelined requests
2232 * to be sent in non-interactive mode.
2233 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002234 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2235 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002236 continue;
2237 }
2238
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002239 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002240 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002241 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002242 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002243
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002244 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002245 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2246 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002247 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002248 * and nothing more to consume. This is comparable to a broken pipe, so
2249 * we forward the close to the request side so that it flows upstream to
2250 * the client.
2251 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002252 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002253 }
2254
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002255 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002256 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2257 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2258 /* We have no more processing to do, and nothing more to send, and
2259 * the client side has closed. So we'll forward this state downstream
2260 * on the response buffer.
2261 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002262 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002263 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002264 }
2265
2266 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002267 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002268
2269 /* we don't want to expire timeouts while we're processing requests */
2270 si->ib->rex = TICK_ETERNITY;
2271 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002272
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002273 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002274 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 +02002275 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002276 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 +02002277
2278 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2279 /* check that we have released everything then unregister */
2280 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002281 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002282}
2283
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002284/* This function dumps information onto the stream interface's read buffer.
2285 * It returns 0 as long as it does not complete, non-zero upon completion.
2286 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002287 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002288static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002289{
2290 unsigned int up = (now.tv_sec - start_date.tv_sec);
2291
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002292 chunk_printf(&trash,
2293 "Name: " PRODUCT_NAME "\n"
2294 "Version: " HAPROXY_VERSION "\n"
2295 "Release_date: " HAPROXY_DATE "\n"
2296 "Nbproc: %d\n"
2297 "Process_num: %d\n"
2298 "Pid: %d\n"
2299 "Uptime: %dd %dh%02dm%02ds\n"
2300 "Uptime_sec: %d\n"
2301 "Memmax_MB: %d\n"
2302 "Ulimit-n: %d\n"
2303 "Maxsock: %d\n"
2304 "Maxconn: %d\n"
2305 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002306 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002307 "CumConns: %d\n"
2308 "CumReq: %d\n"
2309#ifdef USE_OPENSSL
2310 "MaxSslConns: %d\n"
2311 "CurrSslConns: %d\n"
2312 "CumSslConns: %d\n"
2313#endif
2314 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002315 "PipesUsed: %d\n"
2316 "PipesFree: %d\n"
2317 "ConnRate: %d\n"
2318 "ConnRateLimit: %d\n"
2319 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002320 "SessRate: %d\n"
2321 "SessRateLimit: %d\n"
2322 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002323#ifdef USE_OPENSSL
2324 "SslRate: %d\n"
2325 "SslRateLimit: %d\n"
2326 "MaxSslRate: %d\n"
2327#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002328 "CompressBpsIn: %u\n"
2329 "CompressBpsOut: %u\n"
2330 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002331#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002332 "ZlibMemUsage: %ld\n"
2333 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002334#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002335 "Tasks: %d\n"
2336 "Run_queue: %d\n"
2337 "Idle_pct: %d\n"
2338 "node: %s\n"
2339 "description: %s\n"
2340 "",
2341 global.nbproc,
2342 relative_pid,
2343 pid,
2344 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2345 up,
2346 global.rlimit_memmax,
2347 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002348 global.maxsock, global.maxconn, global.hardmaxconn,
2349 actconn, totalconn, global.req_count,
2350#ifdef USE_OPENSSL
2351 global.maxsslconn, sslconns, totalsslconns,
2352#endif
2353 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002354 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002355 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002356#ifdef USE_OPENSSL
2357 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2358#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002359 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2360 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002361#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002362 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002363#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002364 nb_tasks_cur, run_queue_cur, idle_pct,
2365 global.node, global.desc ? global.desc : ""
2366 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002367
2368 if (bi_putchk(si->ib, &trash) == -1)
2369 return 0;
2370
2371 return 1;
2372}
2373
Willy Tarreau12833bb2014-01-28 16:49:56 +01002374/* This function dumps memory usage information onto the stream interface's
2375 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2376 * completion. No state is used.
2377 */
2378static int stats_dump_pools_to_buffer(struct stream_interface *si)
2379{
2380 dump_pools_to_trash();
2381 if (bi_putchk(si->ib, &trash) == -1)
2382 return 0;
2383 return 1;
2384}
2385
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002386/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2387 * the state from stream interface <si>. The caller is responsible for clearing
2388 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002389 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002390static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002391{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002392 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002393 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002394
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002395 if (!(px->cap & PR_CAP_FE))
2396 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002397
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002398 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002399 return 0;
2400
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002401 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002402 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002403 /* name, queue */
2404 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002405
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002406 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002407 /* Column sub-heading for Enable or Disable server */
2408 chunk_appendf(&trash, "<td></td>");
2409 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002410
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002411 chunk_appendf(&trash,
2412 "<td class=ac>"
2413 "<a name=\"%s/Frontend\"></a>"
2414 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2415 "<td colspan=3></td>"
2416 "",
2417 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002418
Willy Tarreau466c9b52012-12-23 02:25:03 +01002419 chunk_appendf(&trash,
2420 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002421 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002422 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2423 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2424 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002425 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2426 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2427 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002428
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002429 if (px->mode == PR_MODE_HTTP)
2430 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002431 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002432 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002433
2434 chunk_appendf(&trash,
2435 "</table></div></u></td>"
2436 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002437 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002438 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2439 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2440 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002441 U2H(px->fe_counters.sps_max),
2442 U2H(px->fe_counters.cps_max),
2443 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002444
2445 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002446 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002447 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002448 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002449
2450 chunk_appendf(&trash,
2451 "</table></div></u></td>"
2452 /* sessions rate : limit */
2453 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002454 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002455
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002456 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002457 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002458 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002459 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002460 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2461 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002462 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002463 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2464 U2H(px->fe_counters.cum_sess),
2465 U2H(px->fe_counters.cum_conn),
2466 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002467
Willy Tarreau466c9b52012-12-23 02:25:03 +01002468 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002469 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002470 chunk_appendf(&trash,
2471 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2472 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2473 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2474 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2475 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2476 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2477 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2478 "<tr><th>- other responses:</th><td>%s</td></tr>"
2479 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2480 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002481 U2H(px->fe_counters.p.http.cum_req),
2482 U2H(px->fe_counters.p.http.rsp[1]),
2483 U2H(px->fe_counters.p.http.rsp[2]),
2484 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002485 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002486 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002487 U2H(px->fe_counters.p.http.rsp[3]),
2488 U2H(px->fe_counters.p.http.rsp[4]),
2489 U2H(px->fe_counters.p.http.rsp[5]),
2490 U2H(px->fe_counters.p.http.rsp[0]),
2491 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002492 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002493
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002494 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002495 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002496 /* sessions: lbtot, lastsess */
2497 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002498 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002499 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002500 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002501 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002502
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002503 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002504 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002505 "<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 +01002506 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002507 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002508 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2509 px->fe_counters.comp_in ?
2510 (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 +01002511 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002512
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002513 chunk_appendf(&trash,
2514 /* denied: req, resp */
2515 "<td>%s</td><td>%s</td>"
2516 /* errors : request, connect, response */
2517 "<td>%s</td><td></td><td></td>"
2518 /* warnings: retries, redispatches */
2519 "<td></td><td></td>"
2520 /* server status : reflect frontend status */
2521 "<td class=ac>%s</td>"
2522 /* rest of server: nothing */
2523 "<td class=ac colspan=8></td></tr>"
2524 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002525 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2526 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002527 px->state == PR_STREADY ? "OPEN" :
2528 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002529 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002530 else { /* CSV mode */
2531 chunk_appendf(&trash,
2532 /* pxid, name, queue cur, queue max, */
2533 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002534 /* sessions : current, max, limit, total */
2535 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002536 /* bytes : in, out */
2537 "%lld,%lld,"
2538 /* denied: req, resp */
2539 "%lld,%lld,"
2540 /* errors : request, connect, response */
2541 "%lld,,,"
2542 /* warnings: retries, redispatches */
2543 ",,"
2544 /* server status : reflect frontend status */
2545 "%s,"
2546 /* rest of server: nothing */
2547 ",,,,,,,,"
2548 /* pid, iid, sid, throttle, lbtot, tracked, type */
2549 "%d,%d,0,,,,%d,"
2550 /* rate, rate_lim, rate_max */
2551 "%u,%u,%u,"
2552 /* check_status, check_code, check_duration */
2553 ",,,",
2554 px->id,
2555 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2556 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2557 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2558 px->fe_counters.failed_req,
2559 px->state == PR_STREADY ? "OPEN" :
2560 px->state == PR_STFULL ? "FULL" : "STOP",
2561 relative_pid, px->uuid, STATS_TYPE_FE,
2562 read_freq_ctr(&px->fe_sess_per_sec),
2563 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002564
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002565 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2566 if (px->mode == PR_MODE_HTTP) {
2567 for (i=1; i<6; i++)
2568 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2569 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2570 }
2571 else
2572 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002573
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002574 /* failed health analyses */
2575 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002576
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002577 /* requests : req_rate, req_rate_max, req_tot, */
2578 chunk_appendf(&trash, "%u,%u,%lld,",
2579 read_freq_ctr(&px->fe_req_per_sec),
2580 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2581
2582 /* errors: cli_aborts, srv_aborts */
2583 chunk_appendf(&trash, ",,");
2584
2585 /* compression: in, out, bypassed */
2586 chunk_appendf(&trash, "%lld,%lld,%lld,",
2587 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2588
2589 /* compression: comp_rsp */
2590 chunk_appendf(&trash, "%lld,",
2591 px->fe_counters.p.http.comp_rsp);
2592
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002593 /* lastsess */
2594 chunk_appendf(&trash, ",");
2595
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002596 /* finish with EOL */
2597 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002598 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002599 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002600}
2601
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002602/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2603 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2604 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2605 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002606 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002607static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002608{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002609 struct appctx *appctx = __objt_appctx(si->end);
2610
2611 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002612 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002613 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002614 /* Column sub-heading for Enable or Disable server */
2615 chunk_appendf(&trash, "<td></td>");
2616 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002617 chunk_appendf(&trash,
2618 /* frontend name, listener name */
2619 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2620 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2621 "",
2622 px->id, l->name,
2623 (flags & ST_SHLGNDS)?"<u>":"",
2624 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002625
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002626 if (flags & ST_SHLGNDS) {
2627 char str[INET6_ADDRSTRLEN];
2628 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002629
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002630 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002631
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002632 port = get_host_port(&l->addr);
2633 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2634 case AF_INET:
2635 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2636 break;
2637 case AF_INET6:
2638 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2639 break;
2640 case AF_UNIX:
2641 chunk_appendf(&trash, "unix, ");
2642 break;
2643 case -1:
2644 chunk_appendf(&trash, "(%s), ", strerror(errno));
2645 break;
2646 }
Willy Tarreau91861262007-10-17 17:06:05 +02002647
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002648 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002649 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002650 }
Willy Tarreau91861262007-10-17 17:06:05 +02002651
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002652 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002653 /* queue */
2654 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002655 /* sessions rate: current, max, limit */
2656 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002657 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002658 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002659 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002660 /* bytes: in, out */
2661 "<td>%s</td><td>%s</td>"
2662 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002663 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002664 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2665 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002666
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002667 chunk_appendf(&trash,
2668 /* denied: req, resp */
2669 "<td>%s</td><td>%s</td>"
2670 /* errors: request, connect, response */
2671 "<td>%s</td><td></td><td></td>"
2672 /* warnings: retries, redispatches */
2673 "<td></td><td></td>"
2674 /* server status: reflect listener status */
2675 "<td class=ac>%s</td>"
2676 /* rest of server: nothing */
2677 "<td class=ac colspan=8></td></tr>"
2678 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002679 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2680 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002681 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2682 }
2683 else { /* CSV mode */
2684 chunk_appendf(&trash,
2685 /* pxid, name, queue cur, queue max, */
2686 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002687 /* sessions: current, max, limit, total */
2688 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002689 /* bytes: in, out */
2690 "%lld,%lld,"
2691 /* denied: req, resp */
2692 "%lld,%lld,"
2693 /* errors: request, connect, response */
2694 "%lld,,,"
2695 /* warnings: retries, redispatches */
2696 ",,"
2697 /* server status: reflect listener status */
2698 "%s,"
2699 /* rest of server: nothing */
2700 ",,,,,,,,"
2701 /* pid, iid, sid, throttle, lbtot, tracked, type */
2702 "%d,%d,%d,,,,%d,"
2703 /* rate, rate_lim, rate_max */
2704 ",,,"
2705 /* check_status, check_code, check_duration */
2706 ",,,"
2707 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2708 ",,,,,,"
2709 /* failed health analyses */
2710 ","
2711 /* requests : req_rate, req_rate_max, req_tot, */
2712 ",,,"
2713 /* errors: cli_aborts, srv_aborts */
2714 ",,"
2715 /* compression: in, out, bypassed, comp_rsp */
2716 ",,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002717 /* lastsess */
2718 ","
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002719 "\n",
2720 px->id, l->name,
2721 l->nbconn, l->counters->conn_max,
2722 l->maxconn, l->counters->cum_conn,
2723 l->counters->bytes_in, l->counters->bytes_out,
2724 l->counters->denied_req, l->counters->denied_resp,
2725 l->counters->failed_req,
2726 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2727 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2728 }
2729 return 1;
2730}
Willy Tarreau91861262007-10-17 17:06:05 +02002731
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002732/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2733 * from stream interface <si>, stats flags <flags>, and server state <state>.
2734 * The caller is responsible for clearing the trash if needed. Returns non-zero
2735 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002736 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2737 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002738 */
2739static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2740{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002741 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002742 struct server *ref = sv->track ? sv->track : sv;
2743 char str[INET6_ADDRSTRLEN];
2744 struct chunk src;
2745 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002746
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002747 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002748 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002749 "DOWN",
2750 "DN %d/%d &uarr;",
2751 "UP %d/%d &darr;",
2752 "UP",
2753 "NOLB %d/%d &darr;",
2754 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002755 "DRAIN %d/%d &darr;",
2756 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002757 "<i>no check</i>"
2758 };
Willy Tarreau91861262007-10-17 17:06:05 +02002759
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002760 if ((sv->state & SRV_MAINTAIN) || (ref->state & SRV_MAINTAIN))
2761 chunk_appendf(&trash, "<tr class=\"maintain\">");
2762 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002763 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002764 "<tr class=\"%s%d\">",
2765 (sv->state & SRV_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002766
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002767 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002768 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002769 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2770 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002771
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002772 chunk_appendf(&trash,
2773 "<td class=ac><a name=\"%s/%s\"></a>%s"
2774 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2775 "",
2776 px->id, sv->id,
2777 (flags & ST_SHLGNDS) ? "<u>" : "",
2778 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002779
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002780 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002781 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002782
2783 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2784 case AF_INET:
2785 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2786 break;
2787 case AF_INET6:
2788 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2789 break;
2790 case AF_UNIX:
2791 chunk_appendf(&trash, "unix, ");
2792 break;
2793 case -1:
2794 chunk_appendf(&trash, "(%s), ", strerror(errno));
2795 break;
2796 default: /* address family not supported */
2797 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002798 }
Willy Tarreau91861262007-10-17 17:06:05 +02002799
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002800 /* id */
2801 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002802
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002803 /* cookie */
2804 if (sv->cookie) {
2805 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002806
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002807 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2808 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002809
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002810 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002811 }
2812
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002813 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002814 }
Willy Tarreau91861262007-10-17 17:06:05 +02002815
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002816 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002817 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002818 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002819 /* sessions rate : current, max, limit */
2820 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002821 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002822 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002823 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2824 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002825
Willy Tarreau466c9b52012-12-23 02:25:03 +01002826
2827 chunk_appendf(&trash,
2828 /* sessions: current, max, limit, total */
2829 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002830 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002831 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2832 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002833 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2834 U2H(sv->counters.cum_sess),
2835 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002836
Willy Tarreau466c9b52012-12-23 02:25:03 +01002837 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2838 if (px->mode == PR_MODE_HTTP) {
2839 unsigned long long tot;
2840 for (tot = i = 0; i < 6; i++)
2841 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002842
Willy Tarreau466c9b52012-12-23 02:25:03 +01002843 chunk_appendf(&trash,
2844 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2845 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2846 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2847 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2848 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2849 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2850 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2851 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002852 U2H(tot),
2853 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2854 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2855 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2856 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2857 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2858 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 +02002859 }
Willy Tarreau91861262007-10-17 17:06:05 +02002860
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002861 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002862 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002863 /* sessions: lbtot, last */
2864 "<td>%s</td><td>%s</td>",
2865 U2H(sv->counters.cum_lbconn),
2866 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02002867
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002868 chunk_appendf(&trash,
2869 /* bytes : in, out */
2870 "<td>%s</td><td>%s</td>"
2871 /* denied: req, resp */
2872 "<td></td><td>%s</td>"
2873 /* errors : request, connect */
2874 "<td></td><td>%s</td>"
2875 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002876 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002877 /* warnings: retries, redispatches */
2878 "<td>%lld</td><td>%lld</td>"
2879 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002880 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2881 U2H(sv->counters.failed_secu),
2882 U2H(sv->counters.failed_conns),
2883 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002884 sv->counters.cli_aborts,
2885 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002886 sv->counters.retries, sv->counters.redispatches);
2887
2888 /* status, lest check */
2889 chunk_appendf(&trash, "<td class=ac>");
2890
2891 if (sv->state & SRV_MAINTAIN) {
2892 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2893 chunk_appendf(&trash, "MAINT");
2894 }
2895 else if (ref != sv && ref->state & SRV_MAINTAIN) {
2896 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2897 chunk_appendf(&trash, "MAINT(via)");
2898 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002899 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002900 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2901 chunk_appendf(&trash,
2902 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09002903 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2904 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002905 }
Willy Tarreau91861262007-10-17 17:06:05 +02002906
Willy Tarreauff5ae352013-12-11 20:36:34 +01002907 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002908 chunk_appendf(&trash,
2909 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002910 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002911 get_check_status_info(sv->check.status));
2912
2913 if (sv->check.status >= HCHK_STATUS_L57DATA)
2914 chunk_appendf(&trash, "/%d", sv->check.code);
2915
2916 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002917 chunk_appendf(&trash, " in %lums", sv->check.duration);
2918
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002919 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002920 get_check_status_description(sv->check.status));
2921 if (*sv->check.desc) {
2922 chunk_appendf(&trash, ": ");
2923 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2924 chunk_htmlencode(&trash, &src);
2925 }
2926 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002927 }
2928 else
2929 chunk_appendf(&trash, "</td><td>");
2930
2931 chunk_appendf(&trash,
2932 /* weight */
2933 "</td><td class=ac>%d</td>"
2934 /* act, bck */
2935 "<td class=ac>%s</td><td class=ac>%s</td>"
2936 "",
2937 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
2938 (sv->state & SRV_BACKUP) ? "-" : "Y",
2939 (sv->state & SRV_BACKUP) ? "Y" : "-");
2940
2941 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002942 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002943 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002944
2945 if (ref->observe)
2946 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2947
2948 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002949 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002950 "<td>%lld</td><td>%s</td>"
2951 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002952 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002953 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2954 }
Willy Tarreauf4659942013-11-28 10:50:06 +01002955 else if (sv != ref) {
2956 if (sv->state & SRV_MAINTAIN)
2957 chunk_appendf(&trash,
2958 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\"><a></td>",
2959 ref->proxy->id, ref->id);
2960 else
2961 chunk_appendf(&trash,
2962 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s<a></td>",
2963 ref->proxy->id, ref->id, ref->proxy->id, ref->id);
2964 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002965 else
2966 chunk_appendf(&trash, "<td colspan=3></td>");
2967
2968 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01002969 if (sv->state & SRV_WARMINGUP)
2970 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002971 else
2972 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2973 }
2974 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002975 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002976 "DOWN,",
2977 "DOWN %d/%d,",
2978 "UP %d/%d,",
2979 "UP,",
2980 "NOLB %d/%d,",
2981 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002982 "DRAIN %d/%d,",
2983 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002984 "no check,"
2985 };
2986
2987 chunk_appendf(&trash,
2988 /* pxid, name */
2989 "%s,%s,"
2990 /* queue : current, max */
2991 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002992 /* sessions : current, max, limit, total */
2993 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002994 /* bytes : in, out */
2995 "%lld,%lld,"
2996 /* denied: req, resp */
2997 ",%lld,"
2998 /* errors : request, connect, response */
2999 ",%lld,%lld,"
3000 /* warnings: retries, redispatches */
3001 "%lld,%lld,"
3002 "",
3003 px->id, sv->id,
3004 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003005 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003006 sv->counters.bytes_in, sv->counters.bytes_out,
3007 sv->counters.failed_secu,
3008 sv->counters.failed_conns, sv->counters.failed_resp,
3009 sv->counters.retries, sv->counters.redispatches);
3010
3011 /* status */
3012 if (sv->state & SRV_MAINTAIN)
3013 chunk_appendf(&trash, "MAINT,");
3014 else if (ref != sv && ref->state & SRV_MAINTAIN)
3015 chunk_appendf(&trash, "MAINT(via),");
3016 else
3017 chunk_appendf(&trash,
3018 srv_hlt_st[state],
Simon Horman58c32972013-11-25 10:46:38 +09003019 (ref->state & SRV_RUNNING) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3020 (ref->state & SRV_RUNNING) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003021
3022 chunk_appendf(&trash,
3023 /* weight, active, backup */
3024 "%d,%d,%d,"
3025 "",
3026 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3027 (sv->state & SRV_BACKUP) ? 0 : 1,
3028 (sv->state & SRV_BACKUP) ? 1 : 0);
3029
3030 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003031 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003032 chunk_appendf(&trash,
3033 "%lld,%lld,%d,%d,",
3034 sv->counters.failed_checks, sv->counters.down_trans,
3035 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3036 else
3037 chunk_appendf(&trash, ",,,,");
3038
3039 /* queue limit, pid, iid, sid, */
3040 chunk_appendf(&trash,
3041 "%s,"
3042 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003043 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003044 relative_pid, px->uuid, sv->puid);
3045
3046 /* throttle */
Willy Tarreaud32c3992013-11-21 15:30:45 +01003047 if (sv->state & SRV_WARMINGUP)
3048 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003049
3050 /* sessions: lbtot */
3051 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3052
3053 /* tracked */
3054 if (sv->track)
3055 chunk_appendf(&trash, "%s/%s,",
3056 sv->track->proxy->id, sv->track->id);
3057 else
3058 chunk_appendf(&trash, ",");
3059
3060 /* type */
3061 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3062
3063 /* rate */
3064 chunk_appendf(&trash, "%u,,%u,",
3065 read_freq_ctr(&sv->sess_per_sec),
3066 sv->counters.sps_max);
3067
Willy Tarreauff5ae352013-12-11 20:36:34 +01003068 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003069 /* check_status */
3070 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3071
3072 /* check_code */
3073 if (sv->check.status >= HCHK_STATUS_L57DATA)
3074 chunk_appendf(&trash, "%u,", sv->check.code);
3075 else
3076 chunk_appendf(&trash, ",");
3077
3078 /* check_duration */
3079 if (sv->check.status >= HCHK_STATUS_CHECKED)
3080 chunk_appendf(&trash, "%lu,", sv->check.duration);
3081 else
3082 chunk_appendf(&trash, ",");
3083
3084 }
3085 else
3086 chunk_appendf(&trash, ",,,");
3087
3088 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3089 if (px->mode == PR_MODE_HTTP) {
3090 for (i=1; i<6; i++)
3091 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3092
3093 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3094 }
3095 else
3096 chunk_appendf(&trash, ",,,,,,");
3097
3098 /* failed health analyses */
3099 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3100
3101 /* requests : req_rate, req_rate_max, req_tot, */
3102 chunk_appendf(&trash, ",,,");
3103
3104 /* errors: cli_aborts, srv_aborts */
3105 chunk_appendf(&trash, "%lld,%lld,",
3106 sv->counters.cli_aborts, sv->counters.srv_aborts);
3107
3108 /* compression: in, out, bypassed, comp_rsp */
3109 chunk_appendf(&trash, ",,,,");
3110
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003111 /* lastsess */
3112 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3113
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003114 /* finish with EOL */
3115 chunk_appendf(&trash, "\n");
3116 }
3117 return 1;
3118}
3119
3120/* Dumps a line for backend <px> to the trash for and uses the state from stream
3121 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3122 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3123 */
3124static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3125{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003126 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003127 struct chunk src;
3128 int i;
3129
3130 if (!(px->cap & PR_CAP_BE))
3131 return 0;
3132
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003133 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003134 return 0;
3135
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003136 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003137 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003138 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003139 /* Column sub-heading for Enable or Disable server */
3140 chunk_appendf(&trash, "<td></td>");
3141 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003142 chunk_appendf(&trash,
3143 "<td class=ac>"
3144 /* name */
3145 "%s<a name=\"%s/Backend\"></a>"
3146 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3147 "",
3148 (flags & ST_SHLGNDS)?"<u>":"",
3149 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003150
3151 if (flags & ST_SHLGNDS) {
3152 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003153 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003154 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3155
3156 /* cookie */
3157 if (px->cookie_name) {
3158 chunk_appendf(&trash, ", cookie: '");
3159 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3160 chunk_htmlencode(&trash, &src);
3161 chunk_appendf(&trash, "'");
3162 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003163 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003164 }
3165
3166 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003167 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003168 /* queue : current, max */
3169 "<td>%s</td><td>%s</td><td></td>"
3170 /* sessions rate : current, max, limit */
3171 "<td>%s</td><td>%s</td><td></td>"
3172 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003173 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003174 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3175 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003176
3177 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003178 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003179 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003180 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003181 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003182 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003183 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3184 U2H(px->be_counters.cum_conn),
3185 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003186
Willy Tarreau466c9b52012-12-23 02:25:03 +01003187 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003188 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003189 chunk_appendf(&trash,
3190 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3191 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3192 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3193 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3194 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3195 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3196 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3197 "<tr><th>- other responses:</th><td>%s</td></tr>"
3198 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3199 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003200 U2H(px->be_counters.p.http.cum_req),
3201 U2H(px->be_counters.p.http.rsp[1]),
3202 U2H(px->be_counters.p.http.rsp[2]),
3203 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003204 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003205 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003206 U2H(px->be_counters.p.http.rsp[3]),
3207 U2H(px->be_counters.p.http.rsp[4]),
3208 U2H(px->be_counters.p.http.rsp[5]),
3209 U2H(px->be_counters.p.http.rsp[0]),
3210 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003211 }
3212
3213 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003214 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003215 /* sessions: lbtot, last */
3216 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003217 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003218 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003219 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003220 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003221 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003222 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003223
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003224 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003225 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003226 "<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 +01003227 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003228 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003229 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3230 px->be_counters.comp_in ?
3231 (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 +01003232 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3233
3234 chunk_appendf(&trash,
3235 /* denied: req, resp */
3236 "<td>%s</td><td>%s</td>"
3237 /* errors : request, connect */
3238 "<td></td><td>%s</td>"
3239 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003240 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003241 /* warnings: retries, redispatches */
3242 "<td>%lld</td><td>%lld</td>"
3243 /* backend status: reflect backend status (up/down): we display UP
3244 * if the backend has known working servers or if it has no server at
3245 * all (eg: for stats). Then we display the total weight, number of
3246 * active and backups. */
3247 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3248 "<td class=ac>%d</td><td class=ac>%d</td>"
3249 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003250 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3251 U2H(px->be_counters.failed_conns),
3252 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003253 px->be_counters.cli_aborts,
3254 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003255 px->be_counters.retries, px->be_counters.redispatches,
3256 human_time(now.tv_sec - px->last_change, 1),
3257 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3258 "<font color=\"red\"><b>DOWN</b></font>",
3259 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3260 px->srv_act, px->srv_bck);
3261
3262 chunk_appendf(&trash,
3263 /* rest of backend: nothing, down transitions, total downtime, throttle */
3264 "<td class=ac>&nbsp;</td><td>%d</td>"
3265 "<td>%s</td>"
3266 "<td></td>"
3267 "</tr>",
3268 px->down_trans,
3269 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3270 }
3271 else { /* CSV mode */
3272 chunk_appendf(&trash,
3273 /* pxid, name */
3274 "%s,BACKEND,"
3275 /* queue : current, max */
3276 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003277 /* sessions : current, max, limit, total */
3278 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003279 /* bytes : in, out */
3280 "%lld,%lld,"
3281 /* denied: req, resp */
3282 "%lld,%lld,"
3283 /* errors : request, connect, response */
3284 ",%lld,%lld,"
3285 /* warnings: retries, redispatches */
3286 "%lld,%lld,"
3287 /* backend status: reflect backend status (up/down): we display UP
3288 * if the backend has known working servers or if it has no server at
3289 * all (eg: for stats). Then we display the total weight, number of
3290 * active and backups. */
3291 "%s,"
3292 "%d,%d,%d,"
3293 /* rest of backend: nothing, down transitions, last change, total downtime */
3294 ",%d,%d,%d,,"
3295 /* pid, iid, sid, throttle, lbtot, tracked, type */
3296 "%d,%d,0,,%lld,,%d,"
3297 /* rate, rate_lim, rate_max, */
3298 "%u,,%u,"
3299 /* check_status, check_code, check_duration */
3300 ",,,",
3301 px->id,
3302 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3303 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3304 px->be_counters.bytes_in, px->be_counters.bytes_out,
3305 px->be_counters.denied_req, px->be_counters.denied_resp,
3306 px->be_counters.failed_conns, px->be_counters.failed_resp,
3307 px->be_counters.retries, px->be_counters.redispatches,
3308 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3309 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3310 px->srv_act, px->srv_bck,
3311 px->down_trans, (int)(now.tv_sec - px->last_change),
3312 px->srv?be_downtime(px):0,
3313 relative_pid, px->uuid,
3314 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3315 read_freq_ctr(&px->be_sess_per_sec),
3316 px->be_counters.sps_max);
3317
3318 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3319 if (px->mode == PR_MODE_HTTP) {
3320 for (i=1; i<6; i++)
3321 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3322 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3323 }
3324 else
3325 chunk_appendf(&trash, ",,,,,,");
3326
3327 /* failed health analyses */
3328 chunk_appendf(&trash, ",");
3329
3330 /* requests : req_rate, req_rate_max, req_tot, */
3331 chunk_appendf(&trash, ",,,");
3332
3333 /* errors: cli_aborts, srv_aborts */
3334 chunk_appendf(&trash, "%lld,%lld,",
3335 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3336
3337 /* compression: in, out, bypassed */
3338 chunk_appendf(&trash, "%lld,%lld,%lld,",
3339 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3340
3341 /* compression: comp_rsp */
3342 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3343
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003344 /* lastsess */
3345 chunk_appendf(&trash, "%d,", be_lastsession(px));
3346
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003347 /* finish with EOL */
3348 chunk_appendf(&trash, "\n");
3349 }
3350 return 1;
3351}
3352
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003353/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003354 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003355 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003356 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003357static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003358{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003359 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003360 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3361
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003362 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003364
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003365 /* 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 +02003366 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003367 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003368 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003369 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3370 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003371 }
3372
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003374 "<form action=\"%s%s%s%s\" method=\"post\">",
3375 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003376 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3377 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003378 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003379 }
3380
3381 /* print a new table */
3382 chunk_appendf(&trash,
3383 "<table class=\"tbl\" width=\"100%%\">\n"
3384 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003385 "<th class=\"pxname\" width=\"10%%\">");
3386
3387 chunk_appendf(&trash,
3388 "<a name=\"%s\"></a>%s"
3389 "<a class=px href=\"#%s\">%s</a>",
3390 px->id,
3391 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3392 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003393
3394 if (uri->flags & ST_SHLGNDS) {
3395 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003396 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003397 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3398 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003399 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003400 }
3401
3402 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003403 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003404 "<th class=\"%s\" width=\"90%%\">%s</th>"
3405 "</tr>\n"
3406 "</table>\n"
3407 "<table class=\"tbl\" width=\"100%%\">\n"
3408 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003409 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3410 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3411
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003412 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003413 /* Column heading for Enable or Disable server */
3414 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003415 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003416
3417 chunk_appendf(&trash,
3418 "<th rowspan=2></th>"
3419 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003420 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003421 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3422 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3423 "<th colspan=9>Server</th>"
3424 "</tr>\n"
3425 "<tr class=\"titre\">"
3426 "<th>Cur</th><th>Max</th><th>Limit</th>"
3427 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003428 "<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 +01003429 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3430 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3431 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3432 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3433 "<th>Thrtle</th>\n"
3434 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003435}
3436
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003437/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003439 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003440static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003441{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003442 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003443 chunk_appendf(&trash, "</table>");
3444
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003445 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003446 /* close the form used to enable/disable this proxy servers */
3447 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003448 "Choose the action to perform on the checked servers : "
3449 "<select name=action>"
3450 "<option value=\"\"></option>"
3451 "<option value=\"disable\">Disable</option>"
3452 "<option value=\"enable\">Enable</option>"
3453 "<option value=\"stop\">Soft Stop</option>"
3454 "<option value=\"start\">Soft Start</option>"
3455 "<option value=\"shutdown\">Kill Sessions</option>"
3456 "</select>"
3457 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3458 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3459 "</form>",
3460 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003461 }
3462
3463 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003464}
Willy Tarreau91861262007-10-17 17:06:05 +02003465
3466/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003467 * Dumps statistics for a proxy. The output is sent to the stream interface's
3468 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3469 * buffer space, or non-zero if everything completed. This function is used
3470 * both by the CLI and the HTTP entry points, and is able to dump the output
3471 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003472 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003473static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003474{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003475 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003476 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003477 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003478 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003479 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003480
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003481 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003482
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003483 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003484 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003485 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003486 if (uri && uri->scope) {
3487 /* we have a limited scope, we have to check the proxy name */
3488 struct stat_scope *scope;
3489 int len;
3490
3491 len = strlen(px->id);
3492 scope = uri->scope;
3493
3494 while (scope) {
3495 /* match exact proxy name */
3496 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3497 break;
3498
3499 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003500 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003501 break;
3502 scope = scope->next;
3503 }
3504
3505 /* proxy name not found : don't dump anything */
3506 if (scope == NULL)
3507 return 1;
3508 }
3509
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003510 /* if the user has requested a limited output and the proxy
3511 * name does not match, skip it.
3512 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003513 if (appctx->ctx.stats.scope_len &&
3514 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 +02003515 return 1;
3516
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003517 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3518 (appctx->ctx.stats.iid != -1) &&
3519 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003520 return 1;
3521
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003522 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003523 /* fall through */
3524
Willy Tarreau295a8372011-03-10 11:25:07 +01003525 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003526 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003527 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003528 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003529 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003530 }
Willy Tarreau91861262007-10-17 17:06:05 +02003531
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003532 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003533 /* fall through */
3534
Willy Tarreau295a8372011-03-10 11:25:07 +01003535 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003536 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003537 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003538 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003539 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003540
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003541 appctx->ctx.stats.l = px->conf.listeners.n;
3542 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003543 /* fall through */
3544
Willy Tarreau295a8372011-03-10 11:25:07 +01003545 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003546 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003547 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003548 if (buffer_almost_full(rep->buf)) {
3549 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003550 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003551 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003552
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003553 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003554 if (!l->counters)
3555 continue;
3556
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003557 if (appctx->ctx.stats.flags & STAT_BOUND) {
3558 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003559 break;
3560
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003561 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003562 continue;
3563 }
3564
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003565 /* print the frontend */
3566 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3567 if (bi_putchk(rep, &trash) == -1)
3568 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003569 }
3570
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003571 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3572 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003573 /* fall through */
3574
Willy Tarreau295a8372011-03-10 11:25:07 +01003575 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003576 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003577 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003578 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 +02003579
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003580 if (buffer_almost_full(rep->buf)) {
3581 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003582 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003583 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003584
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003585 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003587 if (appctx->ctx.stats.flags & STAT_BOUND) {
3588 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003589 break;
3590
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003591 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003592 continue;
3593 }
3594
Willy Tarreau44267702011-10-28 15:35:33 +02003595 if (sv->track)
3596 svs = sv->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003597 else
3598 svs = sv;
3599
Willy Tarreau91861262007-10-17 17:06:05 +02003600 /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003601 if (!(svs->check.state & CHK_ST_ENABLED))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003602 sv_state = 8;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003603 else if (svs->state & SRV_RUNNING) {
Simon Horman58c32972013-11-25 10:46:38 +09003604 if (svs->check.health == svs->check.rise + svs->check.fall - 1)
Willy Tarreau91861262007-10-17 17:06:05 +02003605 sv_state = 3; /* UP */
3606 else
3607 sv_state = 2; /* going down */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003608
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003609 if (svs->state & SRV_DRAIN)
Simon Horman8c3d0be2013-11-25 10:46:40 +09003610 sv_state += 4;
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003611 else if (svs->state & SRV_GOINGDOWN)
3612 sv_state += 2;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003613 }
Willy Tarreau91861262007-10-17 17:06:05 +02003614 else
Simon Horman125d0992013-02-24 17:23:38 +09003615 if (svs->check.health)
Willy Tarreau91861262007-10-17 17:06:05 +02003616 sv_state = 1; /* going up */
3617 else
3618 sv_state = 0; /* DOWN */
3619
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003620 if (((sv_state == 0) || (sv->state & SRV_MAINTAIN)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003621 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003622 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003623 continue;
3624 }
3625
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003626 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3627 if (bi_putchk(rep, &trash) == -1)
3628 return 0;
3629 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003630
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003631 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003632 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003633
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003634 case STAT_PX_ST_BE:
3635 /* print the backend */
3636 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3637 if (bi_putchk(rep, &trash) == -1)
3638 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003639
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003640 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003641 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003642
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003643 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003644 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003645 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003646 if (bi_putchk(rep, &trash) == -1)
3647 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003648 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003649
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003650 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003651 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003652
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003653 case STAT_PX_ST_FIN:
3654 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003655
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003656 default:
3657 /* unknown state, we should put an abort() here ! */
3658 return 1;
3659 }
3660}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003661
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003662/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3663 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003664 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003665static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003666{
3667 /* WARNING! This must fit in the first buffer !!! */
3668 chunk_appendf(&trash,
3669 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3670 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3671 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3672 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3673 "<style type=\"text/css\"><!--\n"
3674 "body {"
3675 " font-family: arial, helvetica, sans-serif;"
3676 " font-size: 12px;"
3677 " font-weight: normal;"
3678 " color: black;"
3679 " background: white;"
3680 "}\n"
3681 "th,td {"
3682 " font-size: 10px;"
3683 "}\n"
3684 "h1 {"
3685 " font-size: x-large;"
3686 " margin-bottom: 0.5em;"
3687 "}\n"
3688 "h2 {"
3689 " font-family: helvetica, arial;"
3690 " font-size: x-large;"
3691 " font-weight: bold;"
3692 " font-style: italic;"
3693 " color: #6020a0;"
3694 " margin-top: 0em;"
3695 " margin-bottom: 0em;"
3696 "}\n"
3697 "h3 {"
3698 " font-family: helvetica, arial;"
3699 " font-size: 16px;"
3700 " font-weight: bold;"
3701 " color: #b00040;"
3702 " background: #e8e8d0;"
3703 " margin-top: 0em;"
3704 " margin-bottom: 0em;"
3705 "}\n"
3706 "li {"
3707 " margin-top: 0.25em;"
3708 " margin-right: 2em;"
3709 "}\n"
3710 ".hr {margin-top: 0.25em;"
3711 " border-color: black;"
3712 " border-bottom-style: solid;"
3713 "}\n"
3714 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3715 ".total {background: #20D0D0;color: #ffff80;}\n"
3716 ".frontend {background: #e8e8d0;}\n"
3717 ".socket {background: #d0d0d0;}\n"
3718 ".backend {background: #e8e8d0;}\n"
3719 ".active0 {background: #ff9090;}\n"
3720 ".active1 {background: #ffd020;}\n"
3721 ".active2 {background: #ffffa0;}\n"
3722 ".active3 {background: #c0ffc0;}\n"
3723 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003724 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3725 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3726 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003727 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003728 ".backup0 {background: #ff9090;}\n"
3729 ".backup1 {background: #ff80ff;}\n"
3730 ".backup2 {background: #c060ff;}\n"
3731 ".backup3 {background: #b0d0ff;}\n"
3732 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3733 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003734 ".backup6 {background: #c060ff;}\n"
3735 ".backup7 {background: #cc9900;}\n"
3736 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003737 ".maintain {background: #c07820;}\n"
3738 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3739 "\n"
3740 "a.px:link {color: #ffff40; text-decoration: none;}"
3741 "a.px:visited {color: #ffff40; text-decoration: none;}"
3742 "a.px:hover {color: #ffffff; text-decoration: none;}"
3743 "a.lfsb:link {color: #000000; text-decoration: none;}"
3744 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3745 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3746 "\n"
3747 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3748 "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"
3749 "table.tbl td.ac { text-align: center;}\n"
3750 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3751 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3752 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3753 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3754 "\n"
3755 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3756 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3757 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003758 "table.det { border-collapse: collapse; border-style: none; }\n"
3759 "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 +01003760 "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 +01003761 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003762 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003763 " display:block;\n"
3764 " visibility:hidden;\n"
3765 " z-index:2147483647;\n"
3766 " position:absolute;\n"
3767 " padding:2px 4px 3px;\n"
3768 " background:#f0f060; color:#000000;\n"
3769 " border:1px solid #7040c0;\n"
3770 " white-space:nowrap;\n"
3771 " font-style:normal;font-size:11px;font-weight:normal;\n"
3772 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3773 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3774 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003775 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003776 "-->\n"
3777 "</style></head>\n",
3778 (uri->flags & ST_SHNODE) ? " on " : "",
3779 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3780 );
3781}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003782
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003783/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003784 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003785 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003786 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003787static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003788{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003789 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003790 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003791 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003792
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003793 /* WARNING! this has to fit the first packet too.
3794 * We are around 3.5 kB, add adding entries will
3795 * become tricky if we want to support 4kB buffers !
3796 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003797 chunk_appendf(&trash,
3798 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3799 PRODUCT_NAME "%s</a></h1>\n"
3800 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3801 "<hr width=\"100%%\" class=\"hr\">\n"
3802 "<h3>&gt; General process information</h3>\n"
3803 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3804 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3805 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3806 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3807 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3808 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3809 "Running tasks: %d/%d; idle = %d %%<br>\n"
3810 "</td><td align=\"center\" nowrap>\n"
3811 "<table class=\"lgd\"><tr>\n"
3812 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3813 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3814 "</tr><tr>\n"
3815 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3816 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3817 "</tr><tr>\n"
3818 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3819 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3820 "</tr><tr>\n"
3821 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003822 "</tr><tr>\n"
3823 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003824 "</tr><tr>\n"
3825 "<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 -07003826 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003827 "<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 +01003828 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003829 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003830 "</td>"
3831 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3832 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3833 "",
3834 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3835 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3836 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3837 (uri->flags & ST_SHDESC) ? ": " : "",
3838 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3839 pid, relative_pid, global.nbproc,
3840 up / 86400, (up % 86400) / 3600,
3841 (up % 3600) / 60, (up % 60),
3842 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3843 global.rlimit_memmax ? " MB" : "",
3844 global.rlimit_nofile,
3845 global.maxsock, global.maxconn, global.maxpipes,
3846 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3847 run_queue_cur, nb_tasks_cur, idle_pct
3848 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003849
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003850 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3851 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3852 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003853
3854 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003855 "<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 +02003856 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003857 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3858 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3859 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003860 STAT_SCOPE_TXT_MAXLEN);
3861
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003862 /* 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 +02003863 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003864 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003865 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003866 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3867 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003868 }
3869
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003870 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003871 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003872 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003873 uri->uri_prefix,
3874 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003875 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003876 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003877 else
3878 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003879 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003880 uri->uri_prefix,
3881 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003882 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003883 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003884
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003885 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003886 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003887 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003888 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003889 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003890 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003891 "",
3892 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003893 else
3894 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003895 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003896 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003897 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003898 ";norefresh",
3899 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003900 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003901
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003902 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003903 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003904 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003905 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3906 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003907 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003908
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003909 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003910 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003911 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003912 (uri->refresh > 0) ? ";norefresh" : "",
3913 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003914
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003915 chunk_appendf(&trash,
3916 "</ul></td>"
3917 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3918 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3919 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3920 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3921 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3922 "</ul>"
3923 "</td>"
3924 "</tr></table>\n"
3925 ""
3926 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003927
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003928 if (appctx->ctx.stats.st_code) {
3929 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003930 case STAT_STATUS_DONE:
3931 chunk_appendf(&trash,
3932 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003933 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003934 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003935 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003936 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3937 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003938 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003939 break;
3940 case STAT_STATUS_NONE:
3941 chunk_appendf(&trash,
3942 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003943 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003944 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003945 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003946 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3947 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003948 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003949 break;
3950 case STAT_STATUS_PART:
3951 chunk_appendf(&trash,
3952 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003953 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003954 "Action partially processed.<br>"
3955 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003956 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003957 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3958 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003959 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003960 break;
3961 case STAT_STATUS_ERRP:
3962 chunk_appendf(&trash,
3963 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003964 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003965 "Action not processed because of invalid parameters."
3966 "<ul>"
3967 "<li>The action is maybe unknown.</li>"
3968 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3969 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3970 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003971 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003972 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3973 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003974 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003975 break;
3976 case STAT_STATUS_EXCD:
3977 chunk_appendf(&trash,
3978 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003979 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003980 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3981 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003982 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003983 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3984 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003985 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003986 break;
3987 case STAT_STATUS_DENY:
3988 chunk_appendf(&trash,
3989 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003990 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003991 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003992 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003993 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3994 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003995 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003996 break;
3997 default:
3998 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09003999 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004000 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004001 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004002 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004003 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4004 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004005 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004006 }
4007 chunk_appendf(&trash, "<p>\n");
4008 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004009}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004010
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004011/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4012 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004013 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004014static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004015{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004016 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004017}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004018
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004019/* This function dumps statistics onto the stream interface's read buffer in
4020 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004021 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4022 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
4023 * and the session must be closed, or -1 in case of any error. This function is
4024 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004025 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004026static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004027{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004028 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004029 struct channel *rep = si->ib;
4030 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004031
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004032 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004033
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004034 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004035 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004036 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004037 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004038
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004039 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004040 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004041 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004042 else
4043 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004044
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004045 if (bi_putchk(rep, &trash) == -1)
4046 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004047
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004048 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004049 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004050
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004051 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004052 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004053 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004054 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004055 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004056 }
Willy Tarreau91861262007-10-17 17:06:05 +02004057
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004058 appctx->ctx.stats.px = proxy;
4059 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4060 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004061 /* fall through */
4062
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004063 case STAT_ST_LIST:
4064 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004065 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004066 if (buffer_almost_full(rep->buf)) {
4067 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004068 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004069 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004070
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004071 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004072 /* skip the disabled proxies, global frontend and non-networked ones */
4073 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004074 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004075 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004076
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004077 appctx->ctx.stats.px = px->next;
4078 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004079 }
4080 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004081
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004082 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004083 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004084
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004085 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004086 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004087 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004088 if (bi_putchk(rep, &trash) == -1)
4089 return 0;
4090 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004091
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004092 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004093 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004094
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004095 case STAT_ST_FIN:
4096 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004097
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004098 default:
4099 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004100 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004101 return -1;
4102 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004103}
Willy Tarreauae526782010-03-04 20:34:23 +01004104
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004105/* We reached the stats page through a POST request. The appctx is
4106 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004107 * Parse the posted data and enable/disable servers if necessary.
4108 * Returns 1 if request was parsed or zero if it needs more data.
4109 */
4110static int stats_process_http_post(struct stream_interface *si)
4111{
4112 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004113 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004114
4115 struct proxy *px = NULL;
4116 struct server *sv = NULL;
4117
4118 char key[LINESIZE];
4119 int action = ST_ADM_ACTION_NONE;
4120 int reprocess = 0;
4121
4122 int total_servers = 0;
4123 int altered_servers = 0;
4124
4125 char *first_param, *cur_param, *next_param, *end_params;
4126 char *st_cur_param = NULL;
4127 char *st_next_param = NULL;
4128
4129 struct chunk *temp;
4130 int reql;
4131
4132 temp = get_trash_chunk();
4133 if (temp->size < s->txn.req.body_len) {
4134 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004135 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004136 goto out;
4137 }
4138
4139 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4140 if (reql <= 0) {
4141 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004142 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004143 return 0;
4144 }
4145
4146 first_param = temp->str;
4147 end_params = temp->str + reql;
4148 cur_param = next_param = end_params;
4149 *end_params = '\0';
4150
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004151 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004152
4153 /*
4154 * Parse the parameters in reverse order to only store the last value.
4155 * From the html form, the backend and the action are at the end.
4156 */
4157 while (cur_param > first_param) {
4158 char *value;
4159 int poffset, plen;
4160
4161 cur_param--;
4162
4163 if ((*cur_param == '&') || (cur_param == first_param)) {
4164 reprocess_servers:
4165 /* Parse the key */
4166 poffset = (cur_param != first_param ? 1 : 0);
4167 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4168 if ((plen > 0) && (plen <= sizeof(key))) {
4169 strncpy(key, cur_param + poffset, plen);
4170 key[plen - 1] = '\0';
4171 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004172 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004173 goto out;
4174 }
4175
4176 /* Parse the value */
4177 value = key;
4178 while (*value != '\0' && *value != '=') {
4179 value++;
4180 }
4181 if (*value == '=') {
4182 /* Ok, a value is found, we can mark the end of the key */
4183 *value++ = '\0';
4184 }
4185 if (url_decode(key) < 0 || url_decode(value) < 0)
4186 break;
4187
4188 /* Now we can check the key to see what to do */
4189 if (!px && (strcmp(key, "b") == 0)) {
4190 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4191 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004192 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004193 goto out;
4194 }
4195 }
4196 else if (!action && (strcmp(key, "action") == 0)) {
4197 if (strcmp(value, "disable") == 0) {
4198 action = ST_ADM_ACTION_DISABLE;
4199 }
4200 else if (strcmp(value, "enable") == 0) {
4201 action = ST_ADM_ACTION_ENABLE;
4202 }
4203 else if (strcmp(value, "stop") == 0) {
4204 action = ST_ADM_ACTION_STOP;
4205 }
4206 else if (strcmp(value, "start") == 0) {
4207 action = ST_ADM_ACTION_START;
4208 }
4209 else if (strcmp(value, "shutdown") == 0) {
4210 action = ST_ADM_ACTION_SHUTDOWN;
4211 }
4212 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004213 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004214 goto out;
4215 }
4216 }
4217 else if (strcmp(key, "s") == 0) {
4218 if (!(px && action)) {
4219 /*
4220 * Indicates that we'll need to reprocess the parameters
4221 * as soon as backend and action are known
4222 */
4223 if (!reprocess) {
4224 st_cur_param = cur_param;
4225 st_next_param = next_param;
4226 }
4227 reprocess = 1;
4228 }
4229 else if ((sv = findserver(px, value)) != NULL) {
4230 switch (action) {
4231 case ST_ADM_ACTION_DISABLE:
4232 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
4233 /* Not already in maintenance, we can change the server state */
4234 sv->state |= SRV_MAINTAIN;
Willy Tarreau33a08db2013-12-11 21:03:31 +01004235 sv->check.state |= CHK_ST_PAUSED;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004236 set_server_down(&sv->check);
4237 altered_servers++;
4238 total_servers++;
4239 }
4240 break;
4241 case ST_ADM_ACTION_ENABLE:
4242 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Willy Tarreaua3ae9322013-12-28 21:28:49 +01004243 /* Already in maintenance, we can change the server state.
4244 * If this server tracks the status of another one,
4245 * we must restore the good status.
4246 */
4247 if (!sv->track || (sv->track->state & SRV_RUNNING)) {
4248 set_server_up(&sv->check);
4249 sv->check.health = sv->check.rise; /* up, but will fall down at first failure */
4250 }
4251 else {
4252 sv->state &= ~SRV_MAINTAIN;
4253 sv->check.state &= ~CHK_ST_PAUSED;
4254 set_server_down(&sv->check);
4255 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004256 altered_servers++;
4257 total_servers++;
4258 }
4259 break;
4260 case ST_ADM_ACTION_STOP:
4261 case ST_ADM_ACTION_START:
4262 if (action == ST_ADM_ACTION_START)
4263 sv->uweight = sv->iweight;
4264 else
4265 sv->uweight = 0;
4266
4267 server_recalc_eweight(sv);
4268 set_server_drain_state(sv);
4269
4270 altered_servers++;
4271 total_servers++;
4272 break;
4273 case ST_ADM_ACTION_SHUTDOWN:
4274 if (px->state != PR_STSTOPPED) {
4275 struct session *sess, *sess_bck;
4276
4277 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4278 if (sess->srv_conn == sv)
4279 session_shutdown(sess, SN_ERR_KILLED);
4280
4281 altered_servers++;
4282 total_servers++;
4283 }
4284 break;
4285 }
4286 } else {
4287 /* the server name is unknown or ambiguous (duplicate names) */
4288 total_servers++;
4289 }
4290 }
4291 if (reprocess && px && action) {
4292 /* Now, we know the backend and the action chosen by the user.
4293 * We can safely restart from the first server parameter
4294 * to reprocess them
4295 */
4296 cur_param = st_cur_param;
4297 next_param = st_next_param;
4298 reprocess = 0;
4299 goto reprocess_servers;
4300 }
4301
4302 next_param = cur_param;
4303 }
4304 }
4305
4306 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004307 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004308 }
4309 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004310 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004311 }
4312 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004313 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004314 }
4315 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004316 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004317 }
4318 out:
4319 return 1;
4320}
4321
4322
4323static int stats_send_http_headers(struct stream_interface *si)
4324{
4325 struct session *s = session_from_task(si->owner);
4326 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004327 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004328
4329 chunk_printf(&trash,
Willy Tarreau51437d22013-12-29 00:43:40 +01004330 "HTTP/1.0 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004331 "Cache-Control: no-cache\r\n"
4332 "Connection: close\r\n"
4333 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004334 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004335
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004336 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004337 chunk_appendf(&trash, "Refresh: %d\r\n",
4338 uri->refresh);
4339
Willy Tarreau51437d22013-12-29 00:43:40 +01004340 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004341
4342 s->txn.status = 200;
4343 s->logs.tv_request = now;
4344
4345 if (bi_putchk(si->ib, &trash) == -1)
4346 return 0;
4347
4348 return 1;
4349}
4350
4351static int stats_send_http_redirect(struct stream_interface *si)
4352{
4353 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4354 struct session *s = session_from_task(si->owner);
4355 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004356 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004357
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004358 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004359 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004360 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004361 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004362 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4363 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004364 }
4365
4366 /* We don't want to land on the posted stats page because a refresh will
4367 * repost the data. We don't want this to happen on accident so we redirect
4368 * the browse to the stats page with a GET.
4369 */
4370 chunk_printf(&trash,
4371 "HTTP/1.1 303 See Other\r\n"
4372 "Cache-Control: no-cache\r\n"
4373 "Content-Type: text/plain\r\n"
4374 "Connection: close\r\n"
4375 "Location: %s;st=%s%s%s%s\r\n"
4376 "\r\n",
4377 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004378 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4379 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4380 stat_status_codes[appctx->ctx.stats.st_code]) ?
4381 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004382 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004383 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4384 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004385 scope_txt);
4386
4387 s->txn.status = 303;
4388 s->logs.tv_request = now;
4389
4390 if (bi_putchk(si->ib, &trash) == -1)
4391 return 0;
4392
4393 return 1;
4394}
4395
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004396/* This I/O handler runs as an applet embedded in a stream interface. It is
4397 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004398 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004399 * automatically unregisters itself once transfer is complete.
4400 */
4401static void http_stats_io_handler(struct stream_interface *si)
4402{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004403 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004404 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004405 struct channel *req = si->ob;
4406 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004407
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004408 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4409 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004410
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004411 /* check that the output is not closed */
4412 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004413 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004414
Willy Tarreau347a35d2013-11-22 17:51:09 +01004415 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004416 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004417 if (stats_send_http_headers(si)) {
4418 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004419 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004420 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004421 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004422 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004423 }
4424
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004425 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004426 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004427 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004428 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004429
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004430 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004431 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004432 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004433 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004434 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004435 }
4436
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004437 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004438 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004439 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004440 }
4441
Willy Tarreau51437d22013-12-29 00:43:40 +01004442 if (appctx->st0 == STAT_HTTP_DONE)
4443 si_shutw(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004444
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004445 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4446 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004447
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004448 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004449 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4450 si_shutr(si);
4451 res->flags |= CF_READ_NULL;
4452 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004453 }
Willy Tarreau91861262007-10-17 17:06:05 +02004454
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004455 /* update all other flags and resync with the other side */
4456 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004457
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004458 /* we don't want to expire timeouts while we're processing requests */
4459 si->ib->rex = TICK_ETERNITY;
4460 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004461
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004462 out:
4463 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4464 /* check that we have released everything then unregister */
4465 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004466 }
4467}
4468
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004469
Willy Tarreau909d5172012-11-26 03:04:41 +01004470static inline const char *get_conn_ctrl_name(const struct connection *conn)
4471{
Willy Tarreau3c728722014-01-23 13:50:42 +01004472 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004473 return "NONE";
4474 return conn->ctrl->name;
4475}
4476
4477static inline const char *get_conn_xprt_name(const struct connection *conn)
4478{
4479 static char ptr[17];
4480
Willy Tarreauaad69382014-01-23 14:21:42 +01004481 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004482 return "NONE";
4483
4484 if (conn->xprt == &raw_sock)
4485 return "RAW";
4486
4487#ifdef USE_OPENSSL
4488 if (conn->xprt == &ssl_sock)
4489 return "SSL";
4490#endif
4491 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4492 return ptr;
4493}
4494
4495static inline const char *get_conn_data_name(const struct connection *conn)
4496{
4497 static char ptr[17];
4498
4499 if (!conn->data)
4500 return "NONE";
4501
4502 if (conn->data == &sess_conn_cb)
4503 return "SESS";
4504
4505 if (conn->data == &si_conn_cb)
4506 return "STRM";
4507
4508 if (conn->data == &check_conn_cb)
4509 return "CHCK";
4510
4511 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4512 return ptr;
4513}
4514
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004515/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004516 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004517 * 0 if the output buffer is full and it needs to be called again, otherwise
4518 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004519 */
Willy Tarreau76153662012-11-26 01:16:39 +01004520static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004521{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004522 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004523 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004524 extern const char *monthname[12];
4525 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004526 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004527 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004528
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004529 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004530
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004531 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004532 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004533 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4534 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004535 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004536 appctx->ctx.sess.uid = 0;
4537 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004538 return 1;
4539 }
4540
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004541 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004542 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004543 appctx->ctx.sess.uid = sess->uniq_id;
4544 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004545 /* fall through */
4546
4547 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004548 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004549 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004550 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004551 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004552 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4553 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004554 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004555 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004556
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004557 conn = objt_conn(sess->si[0].end);
4558 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004559 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004560 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004561 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004562 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004563 break;
4564 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004565 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004566 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004567 default:
4568 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004569 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004570 break;
4571 }
4572
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004573 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004574 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004575 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004576
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004577 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004578 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004579 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4580 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4581 sess->listener ? sess->listener->luid : 0);
4582
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004583 if (conn)
4584 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004585
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004586 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004587 case AF_INET:
4588 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004589 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004590 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004591 break;
4592 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004593 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004594 break;
4595 default:
4596 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004597 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004598 break;
4599 }
4600
Willy Tarreau50943332011-09-02 17:33:05 +02004601 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004602 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004603 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004604 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004605 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4606 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004607 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004608
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004609 conn = objt_conn(sess->si[1].end);
4610 if (conn)
4611 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004612
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004613 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004614 case AF_INET:
4615 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004616 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004617 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004618 break;
4619 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004620 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004621 break;
4622 default:
4623 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004624 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004625 break;
4626 }
4627
4628 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004629 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004630 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004631 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4632 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004633 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004634 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004635
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004636 if (conn)
4637 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004638
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004639 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004640 case AF_INET:
4641 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004642 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004643 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004644 break;
4645 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004646 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004647 break;
4648 default:
4649 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004650 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004651 break;
4652 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004653
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004654 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004655 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004656 sess->task,
4657 sess->task->state,
4658 sess->task->nice, sess->task->calls,
4659 sess->task->expire ?
4660 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4661 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4662 TICKS_TO_MS(1000)) : "<NEVER>",
4663 task_in_rq(sess->task) ? ", running" : "");
4664
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004665 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004666 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004667 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4668
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004669 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004670 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4671 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4672 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4673
4674 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004675 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004676 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004677 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004678 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004679 obj_type_name(sess->si[0].end),
4680 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004681 sess->si[0].exp ?
4682 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4683 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4684 TICKS_TO_MS(1000)) : "<NEVER>",
4685 sess->si[0].err_type);
4686
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004687 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004688 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004689 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004690 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004691 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004692 obj_type_name(sess->si[1].end),
4693 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004694 sess->si[1].exp ?
4695 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4696 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4697 TICKS_TO_MS(1000)) : "<NEVER>",
4698 sess->si[1].err_type);
4699
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004700 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004701 chunk_appendf(&trash,
4702 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004703 conn,
4704 get_conn_ctrl_name(conn),
4705 get_conn_xprt_name(conn),
4706 get_conn_data_name(conn),
4707 obj_type_name(conn->target),
4708 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004709
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004710 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004711 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004712 conn->flags,
4713 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004714 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004715 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004716 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004717 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004718 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4719 chunk_appendf(&trash,
4720 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4721 tmpctx,
4722 tmpctx->st0,
4723 tmpctx->st1,
4724 tmpctx->st2,
4725 tmpctx->applet->name);
4726 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004727
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004728 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004729 chunk_appendf(&trash,
4730 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004731 conn,
4732 get_conn_ctrl_name(conn),
4733 get_conn_xprt_name(conn),
4734 get_conn_data_name(conn),
4735 obj_type_name(conn->target),
4736 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004737
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004738 chunk_appendf(&trash,
4739 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004740 conn->flags,
4741 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004742 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004743 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004744 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004745 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004746 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4747 chunk_appendf(&trash,
4748 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4749 tmpctx,
4750 tmpctx->st0,
4751 tmpctx->st1,
4752 tmpctx->st2,
4753 tmpctx->applet->name);
4754 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004755
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004756 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004757 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004758 " an_exp=%s",
4759 sess->req,
4760 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004761 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004762 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004763 sess->req->analyse_exp ?
4764 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4765 TICKS_TO_MS(1000)) : "<NEVER>");
4766
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004767 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004768 " rex=%s",
4769 sess->req->rex ?
4770 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4771 TICKS_TO_MS(1000)) : "<NEVER>");
4772
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004773 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004774 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004775 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004776 sess->req->wex ?
4777 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4778 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004779 sess->req->buf,
4780 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004781 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004782 sess->txn.req.next, sess->req->buf->i,
4783 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004784
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004785 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004786 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004787 " an_exp=%s",
4788 sess->rep,
4789 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004790 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004791 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004792 sess->rep->analyse_exp ?
4793 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4794 TICKS_TO_MS(1000)) : "<NEVER>");
4795
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004796 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004797 " rex=%s",
4798 sess->rep->rex ?
4799 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4800 TICKS_TO_MS(1000)) : "<NEVER>");
4801
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004802 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004803 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004804 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004805 sess->rep->wex ?
4806 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4807 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004808 sess->rep->buf,
4809 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004810 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004811 sess->txn.rsp.next, sess->rep->buf->i,
4812 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004813
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004814 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004815 return 0;
4816
4817 /* use other states to dump the contents */
4818 }
4819 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004820 appctx->ctx.sess.uid = 0;
4821 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004822 return 1;
4823}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004824
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004825static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004826{
4827 struct appctx *appctx = __objt_appctx(si->end);
4828
4829 switch (appctx->st2) {
4830 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004831 /* Display the column headers. If the message cannot be sent,
4832 * quit the fucntion with returning 0. The function is called
4833 * later and restart at the state "STAT_ST_INIT".
4834 */
4835 chunk_reset(&trash);
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01004836 chunk_appendf(&trash, "# id (name) description\n");
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004837 if (bi_putchk(si->ib, &trash) == -1)
4838 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004839
4840 /* Now, we start the browsing of the references lists.
4841 * Note that the following call to LIST_ELEM return bad pointer. The only
4842 * avalaible field of this pointer is <list>. It is used with the function
4843 * pat_list_get_next() for retruning the first avalaible entry
4844 */
4845 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
4846 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
4847 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004848 appctx->st2 = STAT_ST_LIST;
4849 /* fall through */
4850
4851 case STAT_ST_LIST:
4852 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004853 chunk_reset(&trash);
4854
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004855 /* Build messages. If the reference is used by another category than
4856 * the listed categorie, display the information in the massage.
4857 */
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01004858 chunk_appendf(&trash, "%d (%s) %s", appctx->ctx.map.ref->unique_id,
4859 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
4860 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004861
4862 if (appctx->ctx.map.display_flags & PAT_REF_MAP) {
4863 if (appctx->ctx.map.ref->flags & PAT_REF_ACL)
4864 chunk_appendf(&trash, " - also used by an ACL");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004865 }
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004866 else {
4867 if (appctx->ctx.map.ref->flags & PAT_REF_MAP)
4868 chunk_appendf(&trash, " - also used by a map");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004869 }
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004870 chunk_appendf(&trash, "\n");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004871
4872 if (bi_putchk(si->ib, &trash) == -1) {
4873 /* let's try again later from this session. We add ourselves into
4874 * this session's users so that it can remove us upon termination.
4875 */
4876 return 0;
4877 }
4878
4879 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004880 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
4881 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004882 }
4883
4884 appctx->st2 = STAT_ST_FIN;
4885 /* fall through */
4886
4887 default:
4888 appctx->st2 = STAT_ST_FIN;
4889 return 1;
4890 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004891 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004892}
4893
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004894static int stats_map_lookup(struct stream_interface *si)
4895{
4896 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004897 struct sample sample;
4898 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004899 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004900
4901 switch (appctx->st2) {
4902 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004903 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01004904 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004905 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004906 appctx->st2 = STAT_ST_LIST;
4907 /* fall through */
4908
4909 case STAT_ST_LIST:
4910 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004911 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004912 /* initialise chunk to build new message */
4913 chunk_reset(&trash);
4914
4915 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004916 sample.type = SMP_T_STR;
4917 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004918 sample.data.str.len = appctx->ctx.map.chunk.len;
4919 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01004920 if (appctx->ctx.map.expr->pat_head->match &&
4921 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004922 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
4923 else
4924 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004925
4926 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004927 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
4928 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
4929 break;
4930 if (match_method >= PAT_MATCH_NUM)
4931 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
4932 else
4933 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004934
4935 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004936 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004937 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
4938 chunk_appendf(&trash, ", found=no");
4939 else
4940 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004941 }
4942
4943 /* Display match and match info */
4944 else {
4945 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004946 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
4947 chunk_appendf(&trash, ", found=yes");
4948 else
4949 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004950
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004951 /* display index mode */
4952 if (pat->flags & PAT_F_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004953 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004954 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004955 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004956
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004957 /* case sensitive */
4958 if (pat->flags & PAT_F_IGNORE_CASE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004959 chunk_appendf(&trash, ", case=insensitive");
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004960 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004961 chunk_appendf(&trash, ", case=sensitive");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004962
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004963 /* display source */
4964 if (pat->flags & PAT_F_FROM_FILE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004965 chunk_appendf(&trash, ", src=file");
Thierry FOURNIERd5723432014-03-11 13:52:44 +01004966 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004967 chunk_appendf(&trash, ", src=conf");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004968
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01004969 /* display pattern */
4970 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
4971 if (pat->ref && pat->ref->pattern)
4972 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
4973 else
4974 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004975 }
4976 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01004977 if (pat->ref && pat->ref->pattern)
4978 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
4979 else
4980 chunk_appendf(&trash, ", pattern=unknown");
4981 }
4982
4983 /* display return value */
4984 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
4985 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004986 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01004987 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004988 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01004989 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004990 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004991 }
4992
Thierry FOURNIERb9903842014-03-11 18:48:17 +01004993 chunk_appendf(&trash, "\n");
4994
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004995 /* display response */
4996 if (bi_putchk(si->ib, &trash) == -1) {
4997 /* let's try again later from this session. We add ourselves into
4998 * this session's users so that it can remove us upon termination.
4999 */
5000 return 0;
5001 }
5002
5003 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005004 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5005 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005006 }
5007
5008 appctx->st2 = STAT_ST_FIN;
5009 /* fall through */
5010
5011 default:
5012 appctx->st2 = STAT_ST_FIN;
5013 free(appctx->ctx.map.chunk.str);
5014 return 1;
5015 }
5016}
5017
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005018static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005019{
5020 struct appctx *appctx = __objt_appctx(si->end);
5021
5022 switch (appctx->st2) {
5023
5024 case STAT_ST_INIT:
5025 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005026 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5027 struct pat_ref_elt *, list);
5028 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5029 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005030 appctx->st2 = STAT_ST_LIST;
5031 /* fall through */
5032
5033 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005034 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005035 chunk_reset(&trash);
5036
5037 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005038 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005039 chunk_appendf(&trash, "%p %s %s\n",
5040 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5041 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005042 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005043 chunk_appendf(&trash, "%p %s\n",
5044 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005045
5046 if (bi_putchk(si->ib, &trash) == -1) {
5047 /* let's try again later from this session. We add ourselves into
5048 * this session's users so that it can remove us upon termination.
5049 */
5050 return 0;
5051 }
5052
5053 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005054 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5055 struct pat_ref_elt *, list);
5056 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005057 break;
5058 }
5059
5060 appctx->st2 = STAT_ST_FIN;
5061 /* fall through */
5062
5063 default:
5064 appctx->st2 = STAT_ST_FIN;
5065 return 1;
5066 }
5067}
5068
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005069/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005070 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005071 * to be called again, otherwise non-zero. It is designed to be called
5072 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005073 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005074static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005075{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005076 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005077 struct connection *conn;
5078
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005079 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005080 /* If we're forced to shut down, we might have to remove our
5081 * reference to the last session being dumped.
5082 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005083 if (appctx->st2 == STAT_ST_LIST) {
5084 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5085 LIST_DEL(&appctx->ctx.sess.bref.users);
5086 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005087 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005088 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005089 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005090 }
5091
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005092 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005093
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005094 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005095 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005096 /* the function had not been called yet, let's prepare the
5097 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005098 * pointer to the first in the global list. When a target
5099 * session is being destroyed, it is responsible for updating
5100 * this pointer. We know we have reached the end when this
5101 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005102 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005103 LIST_INIT(&appctx->ctx.sess.bref.users);
5104 appctx->ctx.sess.bref.ref = sessions.n;
5105 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005106 /* fall through */
5107
Willy Tarreau295a8372011-03-10 11:25:07 +01005108 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005109 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005110 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5111 LIST_DEL(&appctx->ctx.sess.bref.users);
5112 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005113 }
5114
5115 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005116 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005117 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005118 struct session *curr_sess;
5119
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005120 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005121
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005122 if (appctx->ctx.sess.target) {
5123 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005124 goto next_sess;
5125
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005126 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005127 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005128 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005129 return 0;
5130
5131 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005132 LIST_DEL(&appctx->ctx.sess.bref.users);
5133 LIST_INIT(&appctx->ctx.sess.bref.users);
5134 if (appctx->ctx.sess.target != (void *)-1) {
5135 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005136 break;
5137 }
5138 else
5139 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005140 }
5141
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005142 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005143 "%p: proto=%s",
5144 curr_sess,
5145 curr_sess->listener->proto->name);
5146
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005147
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005148 conn = objt_conn(curr_sess->si[0].end);
5149 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005150 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005151 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005152 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005153 " src=%s:%d fe=%s be=%s srv=%s",
5154 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005155 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005156 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005157 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005158 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005159 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005160 break;
5161 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005162 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005163 " src=unix:%d fe=%s be=%s srv=%s",
5164 curr_sess->listener->luid,
5165 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005166 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005167 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005168 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005169 break;
5170 }
5171
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005172 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005173 " ts=%02x age=%s calls=%d",
5174 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005175 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5176 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005177
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005178 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005179 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005180 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005181 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005182 curr_sess->req->analysers,
5183 curr_sess->req->rex ?
5184 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5185 TICKS_TO_MS(1000)) : "");
5186
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005187 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005188 ",wx=%s",
5189 curr_sess->req->wex ?
5190 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5191 TICKS_TO_MS(1000)) : "");
5192
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005193 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005194 ",ax=%s]",
5195 curr_sess->req->analyse_exp ?
5196 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5197 TICKS_TO_MS(1000)) : "");
5198
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005199 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005200 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005201 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005202 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005203 curr_sess->rep->analysers,
5204 curr_sess->rep->rex ?
5205 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5206 TICKS_TO_MS(1000)) : "");
5207
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005208 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005209 ",wx=%s",
5210 curr_sess->rep->wex ?
5211 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5212 TICKS_TO_MS(1000)) : "");
5213
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005214 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005215 ",ax=%s]",
5216 curr_sess->rep->analyse_exp ?
5217 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5218 TICKS_TO_MS(1000)) : "");
5219
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005220 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005221 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005222 " s0=[%d,%1xh,fd=%d,ex=%s]",
5223 curr_sess->si[0].state,
5224 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005225 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005226 curr_sess->si[0].exp ?
5227 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5228 TICKS_TO_MS(1000)) : "");
5229
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005230 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005231 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005232 " s1=[%d,%1xh,fd=%d,ex=%s]",
5233 curr_sess->si[1].state,
5234 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005235 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005236 curr_sess->si[1].exp ?
5237 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5238 TICKS_TO_MS(1000)) : "");
5239
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005240 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005241 " exp=%s",
5242 curr_sess->task->expire ?
5243 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5244 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005245 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005246 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005247
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005248 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005249
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005250 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005251 /* let's try again later from this session. We add ourselves into
5252 * this session's users so that it can remove us upon termination.
5253 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005254 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005255 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005256 }
5257
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005258 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005259 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005260 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005261
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005262 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005263 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005264 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005265 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005266 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005267 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005268
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005269 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005270 return 0;
5271
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005272 appctx->ctx.sess.target = NULL;
5273 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005274 return 1;
5275 }
5276
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005277 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005278 /* fall through */
5279
5280 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005281 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005282 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005283 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005284}
5285
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005286/* This is called when the stream interface is closed. For instance, upon an
5287 * external abort, we won't call the i/o handler anymore so we may need to
5288 * remove back references to the session currently being dumped.
5289 */
Simon Horman74d88312013-02-12 10:45:50 +09005290static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005291{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005292 struct appctx *appctx = __objt_appctx(si->end);
5293
5294 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5295 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5296 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005297 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005298 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5299 free(appctx->ctx.map.chunk.str);
5300 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005301}
5302
Willy Tarreau20e99322013-04-13 09:22:25 +02005303/* This function is used to either dump tables states (when action is set
5304 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005305 * It returns 0 if the output buffer is full and it needs to be called
5306 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005307 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005308static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005309{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005310 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005311 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005312 struct ebmb_node *eb;
5313 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005314 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005315 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005316
5317 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005318 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005319 * - STAT_ST_INIT : the first call
5320 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005321 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005322 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005323 * and the entry pointer points to the next entry to be dumped,
5324 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005325 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005326 * data though.
5327 */
5328
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005329 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005330 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005331 if (appctx->st2 == STAT_ST_LIST) {
5332 appctx->ctx.table.entry->ref_cnt--;
5333 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005334 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005335 return 1;
5336 }
5337
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005338 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005339
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005340 while (appctx->st2 != STAT_ST_FIN) {
5341 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005342 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005343 appctx->ctx.table.proxy = appctx->ctx.table.target;
5344 if (!appctx->ctx.table.proxy)
5345 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005346
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005347 appctx->ctx.table.entry = NULL;
5348 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005349 break;
5350
Willy Tarreau295a8372011-03-10 11:25:07 +01005351 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005352 if (!appctx->ctx.table.proxy ||
5353 (appctx->ctx.table.target &&
5354 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5355 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005356 break;
5357 }
5358
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005359 if (appctx->ctx.table.proxy->table.size) {
5360 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5361 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005362 return 0;
5363
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005364 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005365 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005366 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005367 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005368 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005369 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5370 appctx->ctx.table.entry->ref_cnt++;
5371 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005372 break;
5373 }
5374 }
5375 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005376 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005377 break;
5378
Willy Tarreau295a8372011-03-10 11:25:07 +01005379 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005380 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005381
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005382 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005383 /* we're filtering on some data contents */
5384 void *ptr;
5385 long long data;
5386
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005387 dt = appctx->ctx.table.data_type;
5388 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5389 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005390 dt);
5391
5392 data = 0;
5393 switch (stktable_data_types[dt].std_type) {
5394 case STD_T_SINT:
5395 data = stktable_data_cast(ptr, std_t_sint);
5396 break;
5397 case STD_T_UINT:
5398 data = stktable_data_cast(ptr, std_t_uint);
5399 break;
5400 case STD_T_ULL:
5401 data = stktable_data_cast(ptr, std_t_ull);
5402 break;
5403 case STD_T_FRQP:
5404 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005405 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005406 break;
5407 }
5408
5409 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005410 if ((data < appctx->ctx.table.value &&
5411 (appctx->ctx.table.data_op == STD_OP_EQ ||
5412 appctx->ctx.table.data_op == STD_OP_GT ||
5413 appctx->ctx.table.data_op == STD_OP_GE)) ||
5414 (data == appctx->ctx.table.value &&
5415 (appctx->ctx.table.data_op == STD_OP_NE ||
5416 appctx->ctx.table.data_op == STD_OP_GT ||
5417 appctx->ctx.table.data_op == STD_OP_LT)) ||
5418 (data > appctx->ctx.table.value &&
5419 (appctx->ctx.table.data_op == STD_OP_EQ ||
5420 appctx->ctx.table.data_op == STD_OP_LT ||
5421 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005422 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005423 }
5424
Simon Hormanc88b8872011-06-15 15:18:49 +09005425 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005426 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5427 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005428 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005429
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005430 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005431
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005432 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005433 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005434 struct stksess *old = appctx->ctx.table.entry;
5435 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005436 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005437 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5438 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5439 stksess_kill(&appctx->ctx.table.proxy->table, old);
5440 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005441 break;
5442 }
5443
Simon Hormanc88b8872011-06-15 15:18:49 +09005444
5445 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005446 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5447 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5448 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005449
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005450 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5451 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005452 break;
5453
Willy Tarreau295a8372011-03-10 11:25:07 +01005454 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005455 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005456 break;
5457 }
5458 }
5459 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005460}
5461
Willy Tarreaud426a182010-03-05 14:58:26 +01005462/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005463 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5464 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5465 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5466 * lines are respected within the limit of 70 output chars. Lines that are
5467 * continuation of a previous truncated line begin with "+" instead of " "
5468 * after the offset. The new pointer is returned.
5469 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005470static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5471 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005472{
5473 int end;
5474 unsigned char c;
5475
5476 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005477 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005478 return ptr;
5479
Willy Tarreau77804732012-10-29 16:14:26 +01005480 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005481
Willy Tarreaud426a182010-03-05 14:58:26 +01005482 while (ptr < len && ptr < bsize) {
5483 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005484 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005485 if (out->len > end - 2)
5486 break;
5487 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005488 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005489 if (out->len > end - 3)
5490 break;
5491 out->str[out->len++] = '\\';
5492 switch (c) {
5493 case '\t': c = 't'; break;
5494 case '\n': c = 'n'; break;
5495 case '\r': c = 'r'; break;
5496 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005497 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005498 }
5499 out->str[out->len++] = c;
5500 } else {
5501 if (out->len > end - 5)
5502 break;
5503 out->str[out->len++] = '\\';
5504 out->str[out->len++] = 'x';
5505 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5506 out->str[out->len++] = hextab[c & 0xF];
5507 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005508 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005509 /* we had a line break, let's return now */
5510 out->str[out->len++] = '\n';
5511 *line = ptr;
5512 return ptr;
5513 }
5514 }
5515 /* we have an incomplete line, we return it as-is */
5516 out->str[out->len++] = '\n';
5517 return ptr;
5518}
5519
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005520/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005521 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005522 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005523 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005524static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005525{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005526 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005527 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005528
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005529 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005530 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005531
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005532 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005533
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005534 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005535 /* the function had not been called yet, let's prepare the
5536 * buffer for a response.
5537 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005538 struct tm tm;
5539
5540 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005541 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005542 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5543 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5544 error_snapshot_id);
5545
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005546 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005547 /* Socket buffer full. Let's try again later from the same point */
5548 return 0;
5549 }
5550
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005551 appctx->ctx.errors.px = proxy;
5552 appctx->ctx.errors.buf = 0;
5553 appctx->ctx.errors.bol = 0;
5554 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005555 }
5556
5557 /* we have two inner loops here, one for the proxy, the other one for
5558 * the buffer.
5559 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005560 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005561 struct error_snapshot *es;
5562
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005563 if (appctx->ctx.errors.buf == 0)
5564 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005565 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005566 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005567
5568 if (!es->when.tv_sec)
5569 goto next;
5570
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005571 if (appctx->ctx.errors.iid >= 0 &&
5572 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5573 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005574 goto next;
5575
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005576 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005577 /* just print headers now */
5578
5579 char pn[INET6_ADDRSTRLEN];
5580 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005581 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005582
5583 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005584 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005585 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005586 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005587
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005588 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5589 case AF_INET:
5590 case AF_INET6:
5591 port = get_host_port(&es->src);
5592 break;
5593 default:
5594 port = 0;
5595 }
5596
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005597 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005598 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005599 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005600 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005601 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005602 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005603 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5604 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005605 break;
5606 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005607 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005608 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005609 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005610 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005611 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005612 break;
5613 }
5614
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005615 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005616 ", server %s (#%d), event #%u\n"
5617 " src %s:%d, session #%d, session flags 0x%08x\n"
5618 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5619 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5620 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5621 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5622 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5623 es->ev_id,
5624 pn, port, es->sid, es->s_flags,
5625 es->state, es->m_flags, es->t_flags,
5626 es->m_clen, es->m_blen,
5627 es->b_flags, es->b_out, es->b_tot,
5628 es->len, es->b_wrap, es->pos);
5629
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005630 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005631 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005632 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005633 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005634 appctx->ctx.errors.ptr = 0;
5635 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005636 }
5637
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005638 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005639 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005640 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005641 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005642 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005643 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005644 goto next;
5645 }
5646
5647 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005648 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005649 int newptr;
5650 int newline;
5651
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005652 newline = appctx->ctx.errors.bol;
5653 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5654 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005655 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005656
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005657 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005658 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005659 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005660 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005661 appctx->ctx.errors.ptr = newptr;
5662 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005663 };
5664 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005665 appctx->ctx.errors.bol = 0;
5666 appctx->ctx.errors.ptr = -1;
5667 appctx->ctx.errors.buf++;
5668 if (appctx->ctx.errors.buf > 1) {
5669 appctx->ctx.errors.buf = 0;
5670 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005671 }
5672 }
5673
5674 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005675 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005676}
5677
Willy Tarreaud5781202012-09-22 19:32:35 +02005678/* parse the "level" argument on the bind lines */
5679static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5680{
5681 if (!*args[cur_arg + 1]) {
5682 memprintf(err, "'%s' : missing level", args[cur_arg]);
5683 return ERR_ALERT | ERR_FATAL;
5684 }
5685
5686 if (!strcmp(args[cur_arg+1], "user"))
5687 conf->level = ACCESS_LVL_USER;
5688 else if (!strcmp(args[cur_arg+1], "operator"))
5689 conf->level = ACCESS_LVL_OPER;
5690 else if (!strcmp(args[cur_arg+1], "admin"))
5691 conf->level = ACCESS_LVL_ADMIN;
5692 else {
5693 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5694 args[cur_arg], args[cur_arg+1]);
5695 return ERR_ALERT | ERR_FATAL;
5696 }
5697
5698 return 0;
5699}
5700
Willy Tarreaub24281b2011-02-13 13:16:36 +01005701struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005702 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005703 .name = "<STATS>", /* used for logging */
5704 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005705 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005706};
5707
Simon Horman9bd2c732011-06-15 15:18:44 +09005708static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005709 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005710 .name = "<CLI>", /* used for logging */
5711 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005712 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005713};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005714
Willy Tarreaudc13c112013-06-21 23:16:39 +02005715static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005716 { CFG_GLOBAL, "stats", stats_parse_global },
5717 { 0, NULL, NULL },
5718}};
5719
Willy Tarreaud5781202012-09-22 19:32:35 +02005720static struct bind_kw_list bind_kws = { "STAT", { }, {
5721 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5722 { NULL, NULL, 0 },
5723}};
5724
Willy Tarreau10522fd2008-07-09 20:12:41 +02005725__attribute__((constructor))
5726static void __dumpstats_module_init(void)
5727{
5728 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005729 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005730}
5731
Willy Tarreau91861262007-10-17 17:06:05 +02005732/*
5733 * Local variables:
5734 * c-indent-level: 8
5735 * c-basic-offset: 8
5736 * End:
5737 */