blob: 849e5783cee47fed63a5ed68ecc52c875e7350e9 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
38
Willy Tarreau91861262007-10-17 17:06:05 +020039#include <types/global.h>
Willy Tarreau91861262007-10-17 17:06:05 +020040
41#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020042#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020043#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010044#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020045#include <proto/dumpstats.h>
46#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010047#include <proto/freq_ctr.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020048#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010049#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010050#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020051#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010052#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010053#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020054#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020055#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010056#include <proto/sample.h>
Willy Tarreau91861262007-10-17 17:06:05 +020057#include <proto/session.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020058#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020059#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010060#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010061#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020062
Willy Tarreau7a0169a2012-11-19 17:13:16 +010063#ifdef USE_OPENSSL
64#include <proto/ssl_sock.h>
65#endif
66
Willy Tarreau91b843d2014-01-28 16:27:17 +010067/* stats socket states */
68enum {
69 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
70 STAT_CLI_END, /* final state, let's close */
71 STAT_CLI_GETREQ, /* wait for a request */
72 STAT_CLI_OUTPUT, /* all states after this one are responses */
73 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
74 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010075 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010076 STAT_CLI_O_INFO, /* dump info */
77 STAT_CLI_O_SESS, /* dump sessions */
78 STAT_CLI_O_ERR, /* dump errors */
79 STAT_CLI_O_TAB, /* dump tables */
80 STAT_CLI_O_CLR, /* clear tables */
81 STAT_CLI_O_SET, /* set entries in tables */
82 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010083 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
84 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010085 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010086 STAT_CLI_O_POOLS, /* dump memory pools */
Willy Tarreau91b843d2014-01-28 16:27:17 +010087};
88
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010089static int stats_dump_info_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +010090static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau76153662012-11-26 01:16:39 +010091static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +090092static int stats_dump_sess_to_buffer(struct stream_interface *si);
93static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +010094static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +010095static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
96static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010097static int stats_pats_list(struct stream_interface *si);
98static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010099static int stats_map_lookup(struct stream_interface *si);
Simon Horman9bd2c732011-06-15 15:18:44 +0900100
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100101/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100102 * cli_io_handler()
103 * -> stats_dump_sess_to_buffer() // "show sess"
104 * -> stats_dump_errors_to_buffer() // "show errors"
105 * -> stats_dump_info_to_buffer() // "show info"
106 * -> stats_dump_stat_to_buffer() // "show stat"
107 * -> stats_dump_csv_header()
108 * -> stats_dump_proxy_to_buffer()
109 * -> stats_dump_fe_stats()
110 * -> stats_dump_li_stats()
111 * -> stats_dump_sv_stats()
112 * -> stats_dump_be_stats()
113 *
114 * http_stats_io_handler()
115 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
116 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
117 * -> stats_dump_html_head() // emits the HTML headers
118 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
119 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
120 * -> stats_dump_html_px_hdr()
121 * -> stats_dump_fe_stats()
122 * -> stats_dump_li_stats()
123 * -> stats_dump_sv_stats()
124 * -> stats_dump_be_stats()
125 * -> stats_dump_html_px_end()
126 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100127 */
128
Simon Horman9bd2c732011-06-15 15:18:44 +0900129static struct si_applet cli_applet;
130
131static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200132 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200133 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200134 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200135 " help : this message\n"
136 " prompt : toggle interactive mode with prompt\n"
137 " quit : disconnect\n"
138 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100139 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200140 " show stat : report counters for each proxy and server\n"
141 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100142 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200143 " show table [id]: report table usage stats or dump this table's contents\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200144 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200145 " set weight : change a server's weight\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200146 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100147 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200148 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200149 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200150 " disable : put a server or frontend in maintenance mode\n"
151 " enable : re-enable a server or frontend which is in maintenance mode\n"
152 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100153 " show acl [id] : report avalaible acls or dump an acl's contents\n"
154 " get acl : reports the patterns matching a sample for an ACL\n"
155 " add acl : add acl entry\n"
156 " del acl : delete acl entry\n"
157 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100158 " show map [id] : report avalaible maps or dump a map's contents\n"
159 " get map : reports the keys and values matching a sample for a map\n"
160 " set map : modify map entry\n"
161 " add map : add map entry\n"
162 " del map : delete map entry\n"
163 " clear map <id> : clear the content of this map\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200164 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200165
Simon Horman9bd2c732011-06-15 15:18:44 +0900166static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200167 "Permission denied\n"
168 "";
169
Willy Tarreau295a8372011-03-10 11:25:07 +0100170/* data transmission states for the stats responses */
171enum {
172 STAT_ST_INIT = 0,
173 STAT_ST_HEAD,
174 STAT_ST_INFO,
175 STAT_ST_LIST,
176 STAT_ST_END,
177 STAT_ST_FIN,
178};
179
180/* data transmission states for the stats responses inside a proxy */
181enum {
182 STAT_PX_ST_INIT = 0,
183 STAT_PX_ST_TH,
184 STAT_PX_ST_FE,
185 STAT_PX_ST_LI,
186 STAT_PX_ST_SV,
187 STAT_PX_ST_BE,
188 STAT_PX_ST_END,
189 STAT_PX_ST_FIN,
190};
191
Cyril Bonté19979e12012-04-04 12:57:21 +0200192extern const char *stat_status_codes[];
193
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200194/* This function is called from the session-level accept() in order to instanciate
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200195 * a new stats socket. It returns a positive value upon success, 0 if the session
Willy Tarreaueb472682010-05-28 18:46:57 +0200196 * needs to be closed and ignored, or a negative value upon critical failure.
197 */
Simon Horman9bd2c732011-06-15 15:18:44 +0900198static int stats_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +0200199{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100200 s->target = &cli_applet.obj_type;
Willy Tarreau4171e9e2013-12-01 12:32:30 +0100201 /* no need to initialize the applet, it will start with st0=st1 = 0 */
Willy Tarreaueb472682010-05-28 18:46:57 +0200202
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200203 tv_zero(&s->logs.tv_request);
204 s->logs.t_queue = 0;
205 s->logs.t_connect = 0;
206 s->logs.t_data = 0;
207 s->logs.t_close = 0;
208 s->logs.bytes_in = s->logs.bytes_out = 0;
209 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
210 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaueb472682010-05-28 18:46:57 +0200211
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200212 s->req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreaueb472682010-05-28 18:46:57 +0200213
Willy Tarreaudecd14d2010-06-01 18:03:19 +0200214 if (s->listener->timeout) {
215 s->req->rto = *s->listener->timeout;
216 s->rep->wto = *s->listener->timeout;
Willy Tarreaueb472682010-05-28 18:46:57 +0200217 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200218 return 1;
Willy Tarreaueb472682010-05-28 18:46:57 +0200219}
220
Willy Tarreau07e9e642010-08-17 21:48:17 +0200221/* allocate a new stats frontend named <name>, and return it
222 * (or NULL in case of lack of memory).
223 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200224static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200225{
226 struct proxy *fe;
227
228 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
229 if (!fe)
230 return NULL;
231
Willy Tarreau237250c2011-07-29 01:49:03 +0200232 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200233 fe->next = proxy;
234 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200235 fe->last_change = now.tv_sec;
236 fe->id = strdup("GLOBAL");
237 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200238 fe->maxconn = 10; /* default to 10 concurrent connections */
239 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200240 fe->conf.file = strdup(file);
241 fe->conf.line = line;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200242 fe->accept = stats_accept;
Willy Tarreau050536d2012-10-04 08:47:34 +0200243
244 /* the stats frontend is the only one able to assign ID #0 */
245 fe->conf.id.key = fe->uuid = 0;
246 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200247 return fe;
248}
249
Willy Tarreaufbee7132007-10-18 13:53:22 +0200250/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200251 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
252 * error message into the <err> buffer which will be preallocated. The trailing
253 * '\n' must not be written. The function must be called with <args> pointing to
254 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200255 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200256static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200257 struct proxy *defpx, const char *file, int line,
258 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200259{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200260 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200261 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200262
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200263 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200264 int cur_arg;
265
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200266 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200267 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 +0200268 return -1;
269 }
270
Willy Tarreau89a63132009-08-16 17:41:45 +0200271 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200272 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200273 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200274 return -1;
275 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200276 }
277
Willy Tarreau4348fad2012-09-20 16:48:07 +0200278 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200279 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200280
Willy Tarreauc53d4222012-09-20 20:19:28 +0200281 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
282 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
283 file, line, args[0], args[1], err && *err ? *err : "error");
284 return -1;
285 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200286
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200287 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200288 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200289 static int bind_dumped;
290 struct bind_kw *kw;
291
292 kw = bind_find_kw(args[cur_arg]);
293 if (kw) {
294 if (!kw->parse) {
295 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
296 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200297 return -1;
298 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200299
300 if (kw->parse(args, cur_arg, curpx, bind_conf, err) != 0) {
301 if (err && *err)
302 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
303 else
304 memprintf(err, "'%s %s' : error encountered while processing '%s'",
305 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200306 return -1;
307 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200308
309 cur_arg += 1 + kw->skip;
310 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200311 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200312
313 if (!bind_dumped) {
314 bind_dump_kws(err);
315 indent_msg(err, 4);
316 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200317 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200318
319 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
320 args[0], args[1], args[cur_arg],
321 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
322 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200323 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100324
Willy Tarreauc53d4222012-09-20 20:19:28 +0200325 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
326 l->maxconn = global.stats_fe->maxconn;
327 l->backlog = global.stats_fe->backlog;
328 l->timeout = &global.stats_fe->timeout.client;
329 l->accept = session_accept;
330 l->handler = process_session;
331 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
332 l->nice = -64; /* we want to boost priority for local stats */
333 global.maxsock += l->maxconn;
334 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200335 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200336 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100337 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200338 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100339
340 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200341 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100342 return -1;
343 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200344
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100345 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200346 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200347 return -1;
348 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200349 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200350 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200351 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200352 return -1;
353 }
354 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200355 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200356 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200357 else if (!strcmp(args[1], "maxconn")) {
358 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200359
360 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200361 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200362 return -1;
363 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200364
365 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200366 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200367 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200368 return -1;
369 }
370 }
371 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200372 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200373 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
374 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100375 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200376
Willy Tarreau91319572013-04-20 09:48:50 +0200377 if (!global.stats_fe) {
378 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
379 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
380 return -1;
381 }
382 }
383
Willy Tarreau35b7b162012-10-22 23:17:18 +0200384 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100385 unsigned int low, high;
386
Willy Tarreau35b7b162012-10-22 23:17:18 +0200387 if (strcmp(args[cur_arg], "all") == 0) {
388 set = 0;
389 break;
390 }
391 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100392 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200393 }
394 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100395 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200396 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100397 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100398 char *dash = strchr(args[cur_arg], '-');
399
400 low = high = str2uic(args[cur_arg]);
401 if (dash)
402 high = str2uic(dash + 1);
403
404 if (high < low) {
405 unsigned int swap = low;
406 low = high;
407 high = swap;
408 }
409
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100410 if (low < 1 || high > LONGBITS) {
411 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
412 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200413 return -1;
414 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100415 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100416 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100417 }
418 else {
419 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100420 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
421 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100422 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 FOURNIER65ce6132014-03-20 11:42:45 +01001151 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001152 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001153 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\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 FOURNIER65ce6132014-03-20 11:42:45 +01001250 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001251 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001252 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\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 FOURNIER65ce6132014-03-20 11:42:45 +01001320 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001321 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001322 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\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]);
1358 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001359 appctx->ctx.cli.msg = warning;
1360 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001361 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001362 return 1;
1363 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001364 else if (strcmp(args[1], "timeout") == 0) {
1365 if (strcmp(args[2], "cli") == 0) {
1366 unsigned timeout;
1367 const char *res;
1368
1369 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001370 appctx->ctx.cli.msg = "Expects an integer value.\n";
1371 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001372 return 1;
1373 }
1374
1375 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1376 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001377 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1378 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001379 return 1;
1380 }
1381
1382 s->req->rto = s->rep->wto = 1 + MS_TO_TICKS(timeout*1000);
1383 return 1;
1384 }
1385 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001386 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1387 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001388 return 1;
1389 }
1390 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001391 else if (strcmp(args[1], "maxconn") == 0) {
1392 if (strcmp(args[2], "frontend") == 0) {
1393 struct proxy *px;
1394 struct listener *l;
1395 int v;
1396
Willy Tarreau532a4502011-09-07 22:37:44 +02001397 px = expect_frontend_admin(s, si, args[3]);
1398 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001399 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001400
1401 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001402 appctx->ctx.cli.msg = "Integer value expected.\n";
1403 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001404 return 1;
1405 }
1406
1407 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001408 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001409 appctx->ctx.cli.msg = "Value out of range.\n";
1410 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001411 return 1;
1412 }
1413
1414 /* OK, the value is fine, so we assign it to the proxy and to all of
1415 * its listeners. The blocked ones will be dequeued.
1416 */
1417 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001418 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001419 l->maxconn = v;
1420 if (l->state == LI_FULL)
1421 resume_listener(l);
1422 }
1423
1424 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&s->fe->listener_queue))
1425 dequeue_all_listeners(&s->fe->listener_queue);
1426
1427 return 1;
1428 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001429 else if (strcmp(args[2], "global") == 0) {
1430 int v;
1431
Willy Tarreau290e63a2012-09-20 18:07:14 +02001432 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001433 appctx->ctx.cli.msg = stats_permission_denied_msg;
1434 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001435 return 1;
1436 }
1437
1438 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001439 appctx->ctx.cli.msg = "Expects an integer value.\n";
1440 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001441 return 1;
1442 }
1443
1444 v = atoi(args[3]);
1445 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001446 appctx->ctx.cli.msg = "Value out of range.\n";
1447 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001448 return 1;
1449 }
1450
1451 /* check for unlimited values */
1452 if (v <= 0)
1453 v = global.hardmaxconn;
1454
1455 global.maxconn = v;
1456
1457 /* Dequeues all of the listeners waiting for a resource */
1458 if (!LIST_ISEMPTY(&global_listener_queue))
1459 dequeue_all_listeners(&global_listener_queue);
1460
1461 return 1;
1462 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001463 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001464 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend' and 'global'.\n";
1465 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001466 return 1;
1467 }
1468 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001469 else if (strcmp(args[1], "rate-limit") == 0) {
1470 if (strcmp(args[2], "connections") == 0) {
1471 if (strcmp(args[3], "global") == 0) {
1472 int v;
1473
Willy Tarreau290e63a2012-09-20 18:07:14 +02001474 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001475 appctx->ctx.cli.msg = stats_permission_denied_msg;
1476 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001477 return 1;
1478 }
1479
1480 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001481 appctx->ctx.cli.msg = "Expects an integer value.\n";
1482 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001483 return 1;
1484 }
1485
1486 v = atoi(args[4]);
1487 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001488 appctx->ctx.cli.msg = "Value out of range.\n";
1489 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001490 return 1;
1491 }
1492
1493 global.cps_lim = v;
1494
1495 /* Dequeues all of the listeners waiting for a resource */
1496 if (!LIST_ISEMPTY(&global_listener_queue))
1497 dequeue_all_listeners(&global_listener_queue);
1498
1499 return 1;
1500 }
1501 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001502 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1503 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001504 return 1;
1505 }
1506 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001507 else if (strcmp(args[2], "sessions") == 0) {
1508 if (strcmp(args[3], "global") == 0) {
1509 int v;
1510
1511 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1512 appctx->ctx.cli.msg = stats_permission_denied_msg;
1513 appctx->st0 = STAT_CLI_PRINT;
1514 return 1;
1515 }
1516
1517 if (!*args[4]) {
1518 appctx->ctx.cli.msg = "Expects an integer value.\n";
1519 appctx->st0 = STAT_CLI_PRINT;
1520 return 1;
1521 }
1522
1523 v = atoi(args[4]);
1524 if (v < 0) {
1525 appctx->ctx.cli.msg = "Value out of range.\n";
1526 appctx->st0 = STAT_CLI_PRINT;
1527 return 1;
1528 }
1529
1530 global.sps_lim = v;
1531
1532 /* Dequeues all of the listeners waiting for a resource */
1533 if (!LIST_ISEMPTY(&global_listener_queue))
1534 dequeue_all_listeners(&global_listener_queue);
1535
1536 return 1;
1537 }
1538 else {
1539 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1540 appctx->st0 = STAT_CLI_PRINT;
1541 return 1;
1542 }
1543 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001544#ifdef USE_OPENSSL
1545 else if (strcmp(args[2], "ssl-sessions") == 0) {
1546 if (strcmp(args[3], "global") == 0) {
1547 int v;
1548
1549 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
1550 appctx->ctx.cli.msg = stats_permission_denied_msg;
1551 appctx->st0 = STAT_CLI_PRINT;
1552 return 1;
1553 }
1554
1555 if (!*args[4]) {
1556 appctx->ctx.cli.msg = "Expects an integer value.\n";
1557 appctx->st0 = STAT_CLI_PRINT;
1558 return 1;
1559 }
1560
1561 v = atoi(args[4]);
1562 if (v < 0) {
1563 appctx->ctx.cli.msg = "Value out of range.\n";
1564 appctx->st0 = STAT_CLI_PRINT;
1565 return 1;
1566 }
1567
1568 global.ssl_lim = v;
1569
1570 /* Dequeues all of the listeners waiting for a resource */
1571 if (!LIST_ISEMPTY(&global_listener_queue))
1572 dequeue_all_listeners(&global_listener_queue);
1573
1574 return 1;
1575 }
1576 else {
1577 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1578 appctx->st0 = STAT_CLI_PRINT;
1579 return 1;
1580 }
1581 }
1582#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001583 else if (strcmp(args[2], "http-compression") == 0) {
1584 if (strcmp(args[3], "global") == 0) {
1585 int v;
1586
Willy Tarreau85d47f92012-11-21 00:29:50 +01001587 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001588 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
1589 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01001590 return 1;
1591 }
1592
William Lallemandd85f9172012-11-09 17:05:39 +01001593 v = atoi(args[4]);
1594 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
1595 }
1596 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001597 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
1598 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01001599 return 1;
1600 }
1601 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001602 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001603 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001604 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001605 return 1;
1606 }
1607 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001608 else if (strcmp(args[1], "table") == 0) {
1609 stats_sock_table_request(si, args, STAT_CLI_O_SET);
1610 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001611 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001612 char *err;
1613
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001614 /* Set flags. */
1615 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001616
1617 /* Expect three parameters: map name, key and new value. */
1618 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01001619 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001620 appctx->st0 = STAT_CLI_PRINT;
1621 return 1;
1622 }
1623
1624 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001625 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001626 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001627 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001628 appctx->st0 = STAT_CLI_PRINT;
1629 return 1;
1630 }
1631
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001632 /* If the entry identifier start with a '#', it is considered as
1633 * pointer id
1634 */
1635 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1636 struct pat_ref_elt *ref;
1637 long long int conv;
1638 char *error;
1639
1640 /* Convert argument to integer value. */
1641 conv = strtoll(&args[3][1], &error, 16);
1642 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001643 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001644 appctx->st0 = STAT_CLI_PRINT;
1645 return 1;
1646 }
1647
1648 /* Convert and check integer to pointer. */
1649 ref = (struct pat_ref_elt *)(long)conv;
1650 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001651 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001652 appctx->st0 = STAT_CLI_PRINT;
1653 return 1;
1654 }
1655
1656 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001657 err = NULL;
1658 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
1659 if (err)
1660 memprintf(&err, "%s.\n", err);
1661 appctx->ctx.cli.err = err;
1662 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001663 return 1;
1664 }
1665 }
1666 else {
1667 /* Else, use the entry identifier as pattern
1668 * string, and update the value.
1669 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001670 err = NULL;
1671 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
1672 if (err)
1673 memprintf(&err, "%s.\n", err);
1674 appctx->ctx.cli.err = err;
1675 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001676 return 1;
1677 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001678 }
1679
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001680 /* The set is done, send message. */
1681 appctx->ctx.cli.msg = "Done.\n";
1682 appctx->st0 = STAT_CLI_PRINT;
1683 return 1;
1684 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001685 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02001686 return 0;
1687 }
1688 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001689 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001690 if (strcmp(args[1], "agent") == 0) {
1691 struct server *sv;
1692
1693 sv = expect_server_admin(s, si, args[2]);
1694 if (!sv)
1695 return 1;
1696
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001697 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
1698 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
1699 appctx->st0 = STAT_CLI_PRINT;
1700 return 1;
1701 }
1702
1703 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001704 return 1;
1705 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001706 if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001707 struct server *sv;
1708
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001709 sv = expect_server_admin(s, si, args[2]);
1710 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001711 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001712
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001713 srv_adm_set_ready(sv, SRV_ADMF_FMAINT);
Willy Tarreau532a4502011-09-07 22:37:44 +02001714 return 1;
1715 }
1716 else if (strcmp(args[1], "frontend") == 0) {
1717 struct proxy *px;
1718
1719 px = expect_frontend_admin(s, si, args[2]);
1720 if (!px)
1721 return 1;
1722
1723 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001724 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
1725 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001726 return 1;
1727 }
1728
1729 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001730 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
1731 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001732 return 1;
1733 }
1734
1735 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001736 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
1737 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001738 return 1;
1739 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001740 return 1;
1741 }
1742 else { /* unknown "enable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001743 appctx->ctx.cli.msg = "'enable' only supports 'frontend' and 'server'.\n";
1744 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001745 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001746 }
1747 }
1748 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09001749 if (strcmp(args[1], "agent") == 0) {
1750 struct server *sv;
1751
1752 sv = expect_server_admin(s, si, args[2]);
1753 if (!sv)
1754 return 1;
1755
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01001756 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09001757 return 1;
1758 }
1759 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01001760 struct server *sv;
1761
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001762 sv = expect_server_admin(s, si, args[2]);
1763 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01001764 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01001765
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001766 srv_adm_set_maint(sv, SRV_ADMF_FMAINT);
Willy Tarreau532a4502011-09-07 22:37:44 +02001767 return 1;
1768 }
1769 else if (strcmp(args[1], "frontend") == 0) {
1770 struct proxy *px;
1771
1772 px = expect_frontend_admin(s, si, args[2]);
1773 if (!px)
1774 return 1;
1775
1776 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001777 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
1778 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001779 return 1;
1780 }
1781
1782 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001783 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
1784 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001785 return 1;
1786 }
1787
1788 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001789 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
1790 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001791 return 1;
1792 }
Cyril Bontécd19e512010-01-31 22:34:03 +01001793 return 1;
1794 }
1795 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001796 appctx->ctx.cli.msg = "'disable' only supports 'frontend' and 'server'.\n";
1797 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001798 return 1;
1799 }
1800 }
1801 else if (strcmp(args[0], "shutdown") == 0) {
1802 if (strcmp(args[1], "frontend") == 0) {
1803 struct proxy *px;
1804
1805 px = expect_frontend_admin(s, si, args[2]);
1806 if (!px)
1807 return 1;
1808
1809 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001810 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
1811 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001812 return 1;
1813 }
1814
1815 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1816 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1817 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
1818 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
1819 stop_proxy(px);
1820 return 1;
1821 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02001822 else if (strcmp(args[1], "session") == 0) {
1823 struct session *sess, *ptr;
1824
Willy Tarreau290e63a2012-09-20 18:07:14 +02001825 if (s->listener->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001826 appctx->ctx.cli.msg = stats_permission_denied_msg;
1827 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001828 return 1;
1829 }
1830
1831 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001832 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
1833 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001834 return 1;
1835 }
1836
1837 ptr = (void *)strtoul(args[2], NULL, 0);
1838
1839 /* first, look for the requested session in the session table */
1840 list_for_each_entry(sess, &sessions, list) {
1841 if (sess == ptr)
1842 break;
1843 }
1844
1845 /* do we have the session ? */
1846 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001847 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
1848 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02001849 return 1;
1850 }
1851
1852 session_shutdown(sess, SN_ERR_KILLED);
1853 return 1;
1854 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02001855 else if (strcmp(args[1], "sessions") == 0) {
1856 if (strcmp(args[2], "server") == 0) {
1857 struct server *sv;
1858 struct session *sess, *sess_bck;
1859
1860 sv = expect_server_admin(s, si, args[3]);
1861 if (!sv)
1862 return 1;
1863
1864 /* kill all the session that are on this server */
1865 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
1866 if (sess->srv_conn == sv)
1867 session_shutdown(sess, SN_ERR_KILLED);
1868
1869 return 1;
1870 }
1871 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001872 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
1873 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02001874 return 1;
1875 }
1876 }
Willy Tarreau532a4502011-09-07 22:37:44 +02001877 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001878 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
1879 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001880 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01001881 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001882 }
1883 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001884 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1885 if (args[1][0] == 'm')
1886 appctx->ctx.map.display_flags = PAT_REF_MAP;
1887 else
1888 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001889
1890 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001891 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1892 if (!*args[2] || !*args[3]) {
1893 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
1894 appctx->st0 = STAT_CLI_PRINT;
1895 return 1;
1896 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001897 }
1898
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001899 else {
1900 if (!*args[2] || !*args[3]) {
1901 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
1902 appctx->st0 = STAT_CLI_PRINT;
1903 return 1;
1904 }
1905 }
1906
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001907 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001908 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001909 if (!appctx->ctx.map.ref ||
1910 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001911 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001912 appctx->st0 = STAT_CLI_PRINT;
1913 return 1;
1914 }
1915
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001916 /* If the entry identifier start with a '#', it is considered as
1917 * pointer id
1918 */
1919 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
1920 struct pat_ref_elt *ref;
1921 long long int conv;
1922 char *error;
1923
1924 /* Convert argument to integer value. */
1925 conv = strtoll(&args[3][1], &error, 16);
1926 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001927 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001928 appctx->st0 = STAT_CLI_PRINT;
1929 return 1;
1930 }
1931
1932 /* Convert and check integer to pointer. */
1933 ref = (struct pat_ref_elt *)(long)conv;
1934 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001935 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01001936 appctx->st0 = STAT_CLI_PRINT;
1937 return 1;
1938 }
1939
1940 /* Try to delete the entry. */
1941 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
1942 /* The entry is not found, send message. */
1943 appctx->ctx.cli.msg = "Key not found.\n";
1944 appctx->st0 = STAT_CLI_PRINT;
1945 return 1;
1946 }
1947 }
1948 else {
1949 /* Else, use the entry identifier as pattern
1950 * string and try to delete the entry.
1951 */
1952 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
1953 /* The entry is not found, send message. */
1954 appctx->ctx.cli.msg = "Key not found.\n";
1955 appctx->st0 = STAT_CLI_PRINT;
1956 return 1;
1957 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001958 }
1959
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001960 /* The deletion is done, send message. */
1961 appctx->ctx.cli.msg = "Done.\n";
1962 appctx->st0 = STAT_CLI_PRINT;
1963 return 1;
1964 }
1965 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001966 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001967 appctx->st0 = STAT_CLI_PRINT;
1968 return 1;
1969 }
1970 }
1971 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001972 if (strcmp(args[1], "map") == 0 ||
1973 strcmp(args[1], "acl") == 0) {
1974 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01001975 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001976
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001977 /* Set flags. */
1978 if (args[1][0] == 'm')
1979 appctx->ctx.map.display_flags = PAT_REF_MAP;
1980 else
1981 appctx->ctx.map.display_flags = PAT_REF_ACL;
1982
1983 /* If the keywork is "map", we expect three parameters, if it
1984 * is "acl", we expect only two parameters
1985 */
1986 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
1987 if (!*args[2] || !*args[3] || !*args[4]) {
1988 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
1989 appctx->st0 = STAT_CLI_PRINT;
1990 return 1;
1991 }
1992 }
1993 else {
1994 if (!*args[2] || !*args[3]) {
1995 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
1996 appctx->st0 = STAT_CLI_PRINT;
1997 return 1;
1998 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001999 }
2000
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002001 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002002 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002003 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002004 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002005 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002006 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002007 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002008 appctx->st0 = STAT_CLI_PRINT;
2009 return 1;
2010 }
2011
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002012 /* The command "add acl" is prohibited if the reference
2013 * use samples.
2014 */
2015 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2016 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2017 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2018 "You must use the command 'add map' to add values.\n";
2019 appctx->st0 = STAT_CLI_PRINT;
2020 return 1;
2021 }
2022
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002023 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002024 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002025 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002026 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002027 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002028 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002029 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002030 if (err)
2031 memprintf(&err, "%s.\n", err);
2032 appctx->ctx.cli.err = err;
2033 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002034 return 1;
2035 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002036
2037 /* The add is done, send message. */
2038 appctx->ctx.cli.msg = "Done.\n";
2039 appctx->st0 = STAT_CLI_PRINT;
2040 return 1;
2041 }
2042 else { /* unknown "del" parameter */
2043 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2044 appctx->st0 = STAT_CLI_PRINT;
2045 return 1;
2046 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002047 }
2048 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002049 return 0;
2050 }
2051 return 1;
2052}
2053
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002054/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002055 * used to processes I/O from/to the stats unix socket. The system relies on a
2056 * state machine handling requests and various responses. We read a request,
2057 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002058 * Then we can read again. The state is stored in appctx->st0 and is one of the
2059 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002060 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002061 */
Willy Tarreaub24281b2011-02-13 13:16:36 +01002062static void cli_io_handler(struct stream_interface *si)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002063{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002064 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau7421efb2012-07-02 15:11:27 +02002065 struct channel *req = si->ob;
2066 struct channel *res = si->ib;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002067 int reql;
2068 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002069
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002070 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2071 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002072
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002073 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002074 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002075 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002076 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2077 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002078 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002079 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002080 /* Let's close for real now. We just close the request
2081 * side, the conditions below will complete if needed.
2082 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002083 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002084 break;
2085 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002086 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002087 /* ensure we have some output room left in the event we
2088 * would want to return some info right after parsing.
2089 */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002090 if (buffer_almost_full(si->ib->buf)) {
2091 si->ib->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02002092 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002093 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002094
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002095 reql = bo_getline(si->ob, trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002096 if (reql <= 0) { /* closed or EOL not found */
2097 if (reql == 0)
2098 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002099 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002100 continue;
2101 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002102
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002103 /* seek for a possible semi-colon. If we find one, we
2104 * replace it with an LF and skip only this part.
2105 */
2106 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002107 if (trash.str[len] == ';') {
2108 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002109 reql = len + 1;
2110 break;
2111 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002112
Willy Tarreau816fc222009-10-04 07:36:58 +02002113 /* now it is time to check that we have a full line,
2114 * remove the trailing \n and possibly \r, then cut the
2115 * line.
2116 */
2117 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002118 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002119 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002120 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002121 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002122
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002123 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002124 len--;
2125
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002126 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002127
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002128 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002129 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002130 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002131 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002132 continue;
2133 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002134 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002135 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002136 else if (strcmp(trash.str, "help") == 0 ||
2137 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002138 appctx->ctx.cli.msg = stats_sock_usage_msg;
2139 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002140 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002141 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002142 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002143 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002144 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002145 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002146 /* if prompt is disabled, print help on empty lines,
2147 * so that the user at least knows how to enable
2148 * prompt and find help.
2149 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002150 appctx->ctx.cli.msg = stats_sock_usage_msg;
2151 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002152 }
2153
2154 /* re-adjust req buffer */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002155 bo_skip(si->ob, reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002156 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002157 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002158 else { /* output functions: first check if the output buffer is closed then abort */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002159 if (res->flags & (CF_SHUTR_NOW|CF_SHUTR)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002160 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002161 continue;
2162 }
2163
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002164 switch (appctx->st0) {
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002165 case STAT_CLI_PRINT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002166 if (bi_putstr(si->ib, appctx->ctx.cli.msg) != -1)
2167 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002168 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002169 case STAT_CLI_PRINT_FREE:
2170 if (bi_putstr(si->ib, appctx->ctx.cli.err) != -1) {
2171 free(appctx->ctx.cli.err);
2172 appctx->st0 = STAT_CLI_PROMPT;
2173 }
2174 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002175 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002176 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002177 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002178 break;
2179 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002180 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002181 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002182 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002183 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002184 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002185 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002186 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002187 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002188 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002189 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002190 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002191 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002192 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002193 if (stats_table_request(si, appctx->st0))
2194 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002195 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002196 case STAT_CLI_O_PATS:
2197 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002198 appctx->st0 = STAT_CLI_PROMPT;
2199 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002200 case STAT_CLI_O_PAT:
2201 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002202 appctx->st0 = STAT_CLI_PROMPT;
2203 break;
2204 case STAT_CLI_O_MLOOK:
2205 if (stats_map_lookup(si))
2206 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002207 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002208 case STAT_CLI_O_POOLS:
2209 if (stats_dump_pools_to_buffer(si))
2210 appctx->st0 = STAT_CLI_PROMPT;
2211 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002212 default: /* abnormal state */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002213 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002214 break;
2215 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002216
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002217 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002218 if (appctx->st0 == STAT_CLI_PROMPT) {
2219 if (bi_putstr(si->ib, appctx->st1 ? "\n> " : "\n") != -1)
2220 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002221 }
2222
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002223 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002224 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002225 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002226
2227 /* Now we close the output if one of the writers did so,
2228 * or if we're not in interactive mode and the request
2229 * buffer is empty. This still allows pipelined requests
2230 * to be sent in non-interactive mode.
2231 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002232 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2233 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002234 continue;
2235 }
2236
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002237 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002238 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002239 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002240 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002241
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002242 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST) && (appctx->st0 != STAT_CLI_GETREQ)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002243 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2244 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002245 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002246 * and nothing more to consume. This is comparable to a broken pipe, so
2247 * we forward the close to the request side so that it flows upstream to
2248 * the client.
2249 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002250 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002251 }
2252
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002253 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002254 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2255 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2256 /* We have no more processing to do, and nothing more to send, and
2257 * the client side has closed. So we'll forward this state downstream
2258 * on the response buffer.
2259 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002260 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002261 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002262 }
2263
2264 /* update all other flags and resync with the other side */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002265 si_update(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002266
2267 /* we don't want to expire timeouts while we're processing requests */
2268 si->ib->rex = TICK_ETERNITY;
2269 si->ob->wex = TICK_ETERNITY;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002270
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002271 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002272 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 +02002273 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002274 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 +02002275
2276 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
2277 /* check that we have released everything then unregister */
2278 stream_int_unregister_handler(si);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002279 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002280}
2281
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002282/* This function dumps information onto the stream interface's read buffer.
2283 * It returns 0 as long as it does not complete, non-zero upon completion.
2284 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002285 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002286static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002287{
2288 unsigned int up = (now.tv_sec - start_date.tv_sec);
2289
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002290 chunk_printf(&trash,
2291 "Name: " PRODUCT_NAME "\n"
2292 "Version: " HAPROXY_VERSION "\n"
2293 "Release_date: " HAPROXY_DATE "\n"
2294 "Nbproc: %d\n"
2295 "Process_num: %d\n"
2296 "Pid: %d\n"
2297 "Uptime: %dd %dh%02dm%02ds\n"
2298 "Uptime_sec: %d\n"
2299 "Memmax_MB: %d\n"
2300 "Ulimit-n: %d\n"
2301 "Maxsock: %d\n"
2302 "Maxconn: %d\n"
2303 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002304 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002305 "CumConns: %d\n"
2306 "CumReq: %d\n"
2307#ifdef USE_OPENSSL
2308 "MaxSslConns: %d\n"
2309 "CurrSslConns: %d\n"
2310 "CumSslConns: %d\n"
2311#endif
2312 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002313 "PipesUsed: %d\n"
2314 "PipesFree: %d\n"
2315 "ConnRate: %d\n"
2316 "ConnRateLimit: %d\n"
2317 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002318 "SessRate: %d\n"
2319 "SessRateLimit: %d\n"
2320 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002321#ifdef USE_OPENSSL
2322 "SslRate: %d\n"
2323 "SslRateLimit: %d\n"
2324 "MaxSslRate: %d\n"
2325#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002326 "CompressBpsIn: %u\n"
2327 "CompressBpsOut: %u\n"
2328 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002329#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002330 "ZlibMemUsage: %ld\n"
2331 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002332#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002333 "Tasks: %d\n"
2334 "Run_queue: %d\n"
2335 "Idle_pct: %d\n"
2336 "node: %s\n"
2337 "description: %s\n"
2338 "",
2339 global.nbproc,
2340 relative_pid,
2341 pid,
2342 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2343 up,
2344 global.rlimit_memmax,
2345 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01002346 global.maxsock, global.maxconn, global.hardmaxconn,
2347 actconn, totalconn, global.req_count,
2348#ifdef USE_OPENSSL
2349 global.maxsslconn, sslconns, totalsslconns,
2350#endif
2351 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002352 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02002353 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02002354#ifdef USE_OPENSSL
2355 read_freq_ctr(&global.ssl_per_sec), global.ssl_lim, global.ssl_max,
2356#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002357 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
2358 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002359#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002360 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002361#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002362 nb_tasks_cur, run_queue_cur, idle_pct,
2363 global.node, global.desc ? global.desc : ""
2364 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002365
2366 if (bi_putchk(si->ib, &trash) == -1)
2367 return 0;
2368
2369 return 1;
2370}
2371
Willy Tarreau12833bb2014-01-28 16:49:56 +01002372/* This function dumps memory usage information onto the stream interface's
2373 * read buffer. It returns 0 as long as it does not complete, non-zero upon
2374 * completion. No state is used.
2375 */
2376static int stats_dump_pools_to_buffer(struct stream_interface *si)
2377{
2378 dump_pools_to_trash();
2379 if (bi_putchk(si->ib, &trash) == -1)
2380 return 0;
2381 return 1;
2382}
2383
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002384/* Dumps a frontend's line to the trash for the current proxy <px> and uses
2385 * the state from stream interface <si>. The caller is responsible for clearing
2386 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002387 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002388static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002389{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002390 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002391 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002392
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002393 if (!(px->cap & PR_CAP_FE))
2394 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002395
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002396 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002397 return 0;
2398
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002399 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002400 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002401 /* name, queue */
2402 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002403
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002404 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002405 /* Column sub-heading for Enable or Disable server */
2406 chunk_appendf(&trash, "<td></td>");
2407 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002408
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002409 chunk_appendf(&trash,
2410 "<td class=ac>"
2411 "<a name=\"%s/Frontend\"></a>"
2412 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
2413 "<td colspan=3></td>"
2414 "",
2415 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02002416
Willy Tarreau466c9b52012-12-23 02:25:03 +01002417 chunk_appendf(&trash,
2418 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002419 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002420 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
2421 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
2422 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002423 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
2424 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
2425 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002426
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002427 if (px->mode == PR_MODE_HTTP)
2428 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002429 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002430 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002431
2432 chunk_appendf(&trash,
2433 "</table></div></u></td>"
2434 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002435 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002436 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
2437 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
2438 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002439 U2H(px->fe_counters.sps_max),
2440 U2H(px->fe_counters.cps_max),
2441 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002442
2443 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002444 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002445 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002446 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01002447
2448 chunk_appendf(&trash,
2449 "</table></div></u></td>"
2450 /* sessions rate : limit */
2451 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002452 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002453
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002454 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002455 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002456 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002457 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002458 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
2459 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002460 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002461 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
2462 U2H(px->fe_counters.cum_sess),
2463 U2H(px->fe_counters.cum_conn),
2464 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02002465
Willy Tarreau466c9b52012-12-23 02:25:03 +01002466 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002467 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01002468 chunk_appendf(&trash,
2469 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
2470 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
2471 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
2472 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
2473 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
2474 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
2475 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
2476 "<tr><th>- other responses:</th><td>%s</td></tr>"
2477 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
2478 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002479 U2H(px->fe_counters.p.http.cum_req),
2480 U2H(px->fe_counters.p.http.rsp[1]),
2481 U2H(px->fe_counters.p.http.rsp[2]),
2482 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002483 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01002484 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01002485 U2H(px->fe_counters.p.http.rsp[3]),
2486 U2H(px->fe_counters.p.http.rsp[4]),
2487 U2H(px->fe_counters.p.http.rsp[5]),
2488 U2H(px->fe_counters.p.http.rsp[0]),
2489 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002490 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002491
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002492 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002493 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002494 /* sessions: lbtot, lastsess */
2495 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002496 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002497 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002498 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002499 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002500
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002501 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002502 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002503 "<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 +01002504 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002505 U2H(px->fe_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002506 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp,
2507 px->fe_counters.comp_in ?
2508 (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 +01002509 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002510
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002511 chunk_appendf(&trash,
2512 /* denied: req, resp */
2513 "<td>%s</td><td>%s</td>"
2514 /* errors : request, connect, response */
2515 "<td>%s</td><td></td><td></td>"
2516 /* warnings: retries, redispatches */
2517 "<td></td><td></td>"
2518 /* server status : reflect frontend status */
2519 "<td class=ac>%s</td>"
2520 /* rest of server: nothing */
2521 "<td class=ac colspan=8></td></tr>"
2522 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002523 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
2524 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002525 px->state == PR_STREADY ? "OPEN" :
2526 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002527 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002528 else { /* CSV mode */
2529 chunk_appendf(&trash,
2530 /* pxid, name, queue cur, queue max, */
2531 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002532 /* sessions : current, max, limit, total */
2533 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002534 /* bytes : in, out */
2535 "%lld,%lld,"
2536 /* denied: req, resp */
2537 "%lld,%lld,"
2538 /* errors : request, connect, response */
2539 "%lld,,,"
2540 /* warnings: retries, redispatches */
2541 ",,"
2542 /* server status : reflect frontend status */
2543 "%s,"
2544 /* rest of server: nothing */
2545 ",,,,,,,,"
2546 /* pid, iid, sid, throttle, lbtot, tracked, type */
2547 "%d,%d,0,,,,%d,"
2548 /* rate, rate_lim, rate_max */
2549 "%u,%u,%u,"
2550 /* check_status, check_code, check_duration */
2551 ",,,",
2552 px->id,
2553 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
2554 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
2555 px->fe_counters.denied_req, px->fe_counters.denied_resp,
2556 px->fe_counters.failed_req,
2557 px->state == PR_STREADY ? "OPEN" :
2558 px->state == PR_STFULL ? "FULL" : "STOP",
2559 relative_pid, px->uuid, STATS_TYPE_FE,
2560 read_freq_ctr(&px->fe_sess_per_sec),
2561 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002562
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002563 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2564 if (px->mode == PR_MODE_HTTP) {
2565 for (i=1; i<6; i++)
2566 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
2567 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
2568 }
2569 else
2570 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002571
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002572 /* failed health analyses */
2573 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002574
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002575 /* requests : req_rate, req_rate_max, req_tot, */
2576 chunk_appendf(&trash, "%u,%u,%lld,",
2577 read_freq_ctr(&px->fe_req_per_sec),
2578 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
2579
2580 /* errors: cli_aborts, srv_aborts */
2581 chunk_appendf(&trash, ",,");
2582
2583 /* compression: in, out, bypassed */
2584 chunk_appendf(&trash, "%lld,%lld,%lld,",
2585 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
2586
2587 /* compression: comp_rsp */
2588 chunk_appendf(&trash, "%lld,",
2589 px->fe_counters.p.http.comp_rsp);
2590
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002591 /* lastsess */
2592 chunk_appendf(&trash, ",");
2593
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002594 /* finish with EOL */
2595 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002596 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002597 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02002598}
2599
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002600/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
2601 * from stream interface <si>, and stats flags <flags>. The caller is responsible
2602 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
2603 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02002604 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002605static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02002606{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002607 struct appctx *appctx = __objt_appctx(si->end);
2608
2609 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002610 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002611 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002612 /* Column sub-heading for Enable or Disable server */
2613 chunk_appendf(&trash, "<td></td>");
2614 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002615 chunk_appendf(&trash,
2616 /* frontend name, listener name */
2617 "<td class=ac><a name=\"%s/+%s\"></a>%s"
2618 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
2619 "",
2620 px->id, l->name,
2621 (flags & ST_SHLGNDS)?"<u>":"",
2622 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02002623
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002624 if (flags & ST_SHLGNDS) {
2625 char str[INET6_ADDRSTRLEN];
2626 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02002627
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002628 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02002629
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002630 port = get_host_port(&l->addr);
2631 switch (addr_to_str(&l->addr, str, sizeof(str))) {
2632 case AF_INET:
2633 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
2634 break;
2635 case AF_INET6:
2636 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
2637 break;
2638 case AF_UNIX:
2639 chunk_appendf(&trash, "unix, ");
2640 break;
2641 case -1:
2642 chunk_appendf(&trash, "(%s), ", strerror(errno));
2643 break;
2644 }
Willy Tarreau91861262007-10-17 17:06:05 +02002645
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002646 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002647 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002648 }
Willy Tarreau91861262007-10-17 17:06:05 +02002649
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002650 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002651 /* queue */
2652 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002653 /* sessions rate: current, max, limit */
2654 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002655 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002656 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002657 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002658 /* bytes: in, out */
2659 "<td>%s</td><td>%s</td>"
2660 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002661 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002662 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
2663 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02002664
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002665 chunk_appendf(&trash,
2666 /* denied: req, resp */
2667 "<td>%s</td><td>%s</td>"
2668 /* errors: request, connect, response */
2669 "<td>%s</td><td></td><td></td>"
2670 /* warnings: retries, redispatches */
2671 "<td></td><td></td>"
2672 /* server status: reflect listener status */
2673 "<td class=ac>%s</td>"
2674 /* rest of server: nothing */
2675 "<td class=ac colspan=8></td></tr>"
2676 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002677 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
2678 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002679 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
2680 }
2681 else { /* CSV mode */
2682 chunk_appendf(&trash,
2683 /* pxid, name, queue cur, queue max, */
2684 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002685 /* sessions: current, max, limit, total */
2686 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002687 /* bytes: in, out */
2688 "%lld,%lld,"
2689 /* denied: req, resp */
2690 "%lld,%lld,"
2691 /* errors: request, connect, response */
2692 "%lld,,,"
2693 /* warnings: retries, redispatches */
2694 ",,"
2695 /* server status: reflect listener status */
2696 "%s,"
2697 /* rest of server: nothing */
2698 ",,,,,,,,"
2699 /* pid, iid, sid, throttle, lbtot, tracked, type */
2700 "%d,%d,%d,,,,%d,"
2701 /* rate, rate_lim, rate_max */
2702 ",,,"
2703 /* check_status, check_code, check_duration */
2704 ",,,"
2705 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
2706 ",,,,,,"
2707 /* failed health analyses */
2708 ","
2709 /* requests : req_rate, req_rate_max, req_tot, */
2710 ",,,"
2711 /* errors: cli_aborts, srv_aborts */
2712 ",,"
2713 /* compression: in, out, bypassed, comp_rsp */
2714 ",,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002715 /* lastsess */
2716 ","
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002717 "\n",
2718 px->id, l->name,
2719 l->nbconn, l->counters->conn_max,
2720 l->maxconn, l->counters->cum_conn,
2721 l->counters->bytes_in, l->counters->bytes_out,
2722 l->counters->denied_req, l->counters->denied_resp,
2723 l->counters->failed_req,
2724 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
2725 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
2726 }
2727 return 1;
2728}
Willy Tarreau91861262007-10-17 17:06:05 +02002729
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002730/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
2731 * from stream interface <si>, stats flags <flags>, and server state <state>.
2732 * The caller is responsible for clearing the trash if needed. Returns non-zero
2733 * if it emits anything, zero otherwise. The <state> parameter can take the
Simon Horman8c3d0be2013-11-25 10:46:40 +09002734 * following values : 0=DOWN, 1=going up, 2=going down, 3=UP, 4,5=NOLB,
2735 * 6,7=DRAIN, 8=unchecked.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002736 */
2737static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv, int state)
2738{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002739 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02002740 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002741 char str[INET6_ADDRSTRLEN];
2742 struct chunk src;
2743 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02002744
Willy Tarreau32091232014-05-16 13:52:00 +02002745 /* we have "via" which is the tracked server as described in the configuration,
2746 * and "ref" which is the checked server and the end of the chain.
2747 */
2748 via = sv->track ? sv->track : sv;
2749 ref = via;
2750 while (ref->track)
2751 ref = ref->track;
2752
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002753 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman8c3d0be2013-11-25 10:46:40 +09002754 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002755 "DOWN",
2756 "DN %d/%d &uarr;",
2757 "UP %d/%d &darr;",
2758 "UP",
2759 "NOLB %d/%d &darr;",
2760 "NOLB",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002761 "DRAIN %d/%d &darr;",
2762 "DRAIN",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002763 "<i>no check</i>"
2764 };
Willy Tarreau91861262007-10-17 17:06:05 +02002765
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002766 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002767 chunk_appendf(&trash, "<tr class=\"maintain\">");
2768 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002769 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002770 "<tr class=\"%s%d\">",
Willy Tarreauc93cd162014-05-13 15:54:22 +02002771 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", state);
Willy Tarreau91861262007-10-17 17:06:05 +02002772
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002773 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002774 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002775 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
2776 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002777
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002778 chunk_appendf(&trash,
2779 "<td class=ac><a name=\"%s/%s\"></a>%s"
2780 "<a class=lfsb href=\"#%s/%s\">%s</a>"
2781 "",
2782 px->id, sv->id,
2783 (flags & ST_SHLGNDS) ? "<u>" : "",
2784 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02002785
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002786 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002787 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002788
2789 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
2790 case AF_INET:
2791 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
2792 break;
2793 case AF_INET6:
2794 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
2795 break;
2796 case AF_UNIX:
2797 chunk_appendf(&trash, "unix, ");
2798 break;
2799 case -1:
2800 chunk_appendf(&trash, "(%s), ", strerror(errno));
2801 break;
2802 default: /* address family not supported */
2803 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02002804 }
Willy Tarreau91861262007-10-17 17:06:05 +02002805
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002806 /* id */
2807 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02002808
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002809 /* cookie */
2810 if (sv->cookie) {
2811 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02002812
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002813 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
2814 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01002815
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002816 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02002817 }
2818
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002819 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02002820 }
Willy Tarreau91861262007-10-17 17:06:05 +02002821
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002822 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002823 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002824 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002825 /* sessions rate : current, max, limit */
2826 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002827 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002828 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002829 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
2830 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02002831
Willy Tarreau466c9b52012-12-23 02:25:03 +01002832
2833 chunk_appendf(&trash,
2834 /* sessions: current, max, limit, total */
2835 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002836 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01002837 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
2838 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002839 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
2840 U2H(sv->counters.cum_sess),
2841 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02002842
Willy Tarreau466c9b52012-12-23 02:25:03 +01002843 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
2844 if (px->mode == PR_MODE_HTTP) {
2845 unsigned long long tot;
2846 for (tot = i = 0; i < 6; i++)
2847 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002848
Willy Tarreau466c9b52012-12-23 02:25:03 +01002849 chunk_appendf(&trash,
2850 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
2851 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2852 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2853 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2854 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2855 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2856 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
2857 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002858 U2H(tot),
2859 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
2860 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
2861 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
2862 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
2863 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
2864 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 +02002865 }
Willy Tarreau91861262007-10-17 17:06:05 +02002866
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002867 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01002868 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05002869 /* sessions: lbtot, last */
2870 "<td>%s</td><td>%s</td>",
2871 U2H(sv->counters.cum_lbconn),
2872 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02002873
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002874 chunk_appendf(&trash,
2875 /* bytes : in, out */
2876 "<td>%s</td><td>%s</td>"
2877 /* denied: req, resp */
2878 "<td></td><td>%s</td>"
2879 /* errors : request, connect */
2880 "<td></td><td>%s</td>"
2881 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002882 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002883 /* warnings: retries, redispatches */
2884 "<td>%lld</td><td>%lld</td>"
2885 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01002886 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
2887 U2H(sv->counters.failed_secu),
2888 U2H(sv->counters.failed_conns),
2889 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002890 sv->counters.cli_aborts,
2891 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002892 sv->counters.retries, sv->counters.redispatches);
2893
2894 /* status, lest check */
2895 chunk_appendf(&trash, "<td class=ac>");
2896
Willy Tarreau20125212014-05-13 19:44:56 +02002897 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002898 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
2899 chunk_appendf(&trash, "MAINT");
2900 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01002901 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002902 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
2903 chunk_appendf(&trash,
2904 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02002905 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
2906 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02002907 }
Willy Tarreau91861262007-10-17 17:06:05 +02002908
Willy Tarreauff5ae352013-12-11 20:36:34 +01002909 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002910 chunk_appendf(&trash,
2911 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01002912 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002913 get_check_status_info(sv->check.status));
2914
2915 if (sv->check.status >= HCHK_STATUS_L57DATA)
2916 chunk_appendf(&trash, "/%d", sv->check.code);
2917
2918 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002919 chunk_appendf(&trash, " in %lums", sv->check.duration);
2920
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002921 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002922 get_check_status_description(sv->check.status));
2923 if (*sv->check.desc) {
2924 chunk_appendf(&trash, ": ");
2925 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
2926 chunk_htmlencode(&trash, &src);
2927 }
2928 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002929 }
2930 else
2931 chunk_appendf(&trash, "</td><td>");
2932
2933 chunk_appendf(&trash,
2934 /* weight */
2935 "</td><td class=ac>%d</td>"
2936 /* act, bck */
2937 "<td class=ac>%s</td><td class=ac>%s</td>"
2938 "",
2939 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02002940 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
2941 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002942
2943 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01002944 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002945 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002946
2947 if (ref->observe)
2948 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
2949
2950 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02002951 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002952 "<td>%lld</td><td>%s</td>"
2953 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01002954 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002955 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
2956 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02002957 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
2958 /* tracking a server */
2959 chunk_appendf(&trash,
2960 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02002961 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01002962 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002963 else
2964 chunk_appendf(&trash, "<td colspan=3></td>");
2965
2966 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02002967 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01002968 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002969 else
2970 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
2971 }
2972 else { /* CSV mode */
Simon Horman8c3d0be2013-11-25 10:46:40 +09002973 static char *srv_hlt_st[9] = {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002974 "DOWN,",
2975 "DOWN %d/%d,",
2976 "UP %d/%d,",
2977 "UP,",
2978 "NOLB %d/%d,",
2979 "NOLB,",
Simon Horman8c3d0be2013-11-25 10:46:40 +09002980 "DRAIN %d/%d,",
2981 "DRAIN,",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002982 "no check,"
2983 };
2984
2985 chunk_appendf(&trash,
2986 /* pxid, name */
2987 "%s,%s,"
2988 /* queue : current, max */
2989 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01002990 /* sessions : current, max, limit, total */
2991 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002992 /* bytes : in, out */
2993 "%lld,%lld,"
2994 /* denied: req, resp */
2995 ",%lld,"
2996 /* errors : request, connect, response */
2997 ",%lld,%lld,"
2998 /* warnings: retries, redispatches */
2999 "%lld,%lld,"
3000 "",
3001 px->id, sv->id,
3002 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003003 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003004 sv->counters.bytes_in, sv->counters.bytes_out,
3005 sv->counters.failed_secu,
3006 sv->counters.failed_conns, sv->counters.failed_resp,
3007 sv->counters.retries, sv->counters.redispatches);
3008
3009 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003010 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003011 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003012 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003013 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003014 else
3015 chunk_appendf(&trash,
3016 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003017 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3018 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003019
3020 chunk_appendf(&trash,
3021 /* weight, active, backup */
3022 "%d,%d,%d,"
3023 "",
3024 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003025 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3026 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003027
3028 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003029 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003030 chunk_appendf(&trash,
3031 "%lld,%lld,%d,%d,",
3032 sv->counters.failed_checks, sv->counters.down_trans,
3033 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3034 else
3035 chunk_appendf(&trash, ",,,,");
3036
3037 /* queue limit, pid, iid, sid, */
3038 chunk_appendf(&trash,
3039 "%s,"
3040 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003041 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003042 relative_pid, px->uuid, sv->puid);
3043
3044 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003045 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003046 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003047
3048 /* sessions: lbtot */
3049 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3050
3051 /* tracked */
3052 if (sv->track)
3053 chunk_appendf(&trash, "%s/%s,",
3054 sv->track->proxy->id, sv->track->id);
3055 else
3056 chunk_appendf(&trash, ",");
3057
3058 /* type */
3059 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3060
3061 /* rate */
3062 chunk_appendf(&trash, "%u,,%u,",
3063 read_freq_ctr(&sv->sess_per_sec),
3064 sv->counters.sps_max);
3065
Willy Tarreauff5ae352013-12-11 20:36:34 +01003066 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003067 /* check_status */
3068 chunk_appendf(&trash, "%s,", get_check_status_info(sv->check.status));
3069
3070 /* check_code */
3071 if (sv->check.status >= HCHK_STATUS_L57DATA)
3072 chunk_appendf(&trash, "%u,", sv->check.code);
3073 else
3074 chunk_appendf(&trash, ",");
3075
3076 /* check_duration */
3077 if (sv->check.status >= HCHK_STATUS_CHECKED)
3078 chunk_appendf(&trash, "%lu,", sv->check.duration);
3079 else
3080 chunk_appendf(&trash, ",");
3081
3082 }
3083 else
3084 chunk_appendf(&trash, ",,,");
3085
3086 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3087 if (px->mode == PR_MODE_HTTP) {
3088 for (i=1; i<6; i++)
3089 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3090
3091 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3092 }
3093 else
3094 chunk_appendf(&trash, ",,,,,,");
3095
3096 /* failed health analyses */
3097 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3098
3099 /* requests : req_rate, req_rate_max, req_tot, */
3100 chunk_appendf(&trash, ",,,");
3101
3102 /* errors: cli_aborts, srv_aborts */
3103 chunk_appendf(&trash, "%lld,%lld,",
3104 sv->counters.cli_aborts, sv->counters.srv_aborts);
3105
3106 /* compression: in, out, bypassed, comp_rsp */
3107 chunk_appendf(&trash, ",,,,");
3108
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003109 /* lastsess */
3110 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3111
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003112 /* finish with EOL */
3113 chunk_appendf(&trash, "\n");
3114 }
3115 return 1;
3116}
3117
3118/* Dumps a line for backend <px> to the trash for and uses the state from stream
3119 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3120 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3121 */
3122static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
3123{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003124 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003125 struct chunk src;
3126 int i;
3127
3128 if (!(px->cap & PR_CAP_BE))
3129 return 0;
3130
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003131 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003132 return 0;
3133
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003134 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003135 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003136 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003137 /* Column sub-heading for Enable or Disable server */
3138 chunk_appendf(&trash, "<td></td>");
3139 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003140 chunk_appendf(&trash,
3141 "<td class=ac>"
3142 /* name */
3143 "%s<a name=\"%s/Backend\"></a>"
3144 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3145 "",
3146 (flags & ST_SHLGNDS)?"<u>":"",
3147 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003148
3149 if (flags & ST_SHLGNDS) {
3150 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003151 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003152 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
3153
3154 /* cookie */
3155 if (px->cookie_name) {
3156 chunk_appendf(&trash, ", cookie: '");
3157 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
3158 chunk_htmlencode(&trash, &src);
3159 chunk_appendf(&trash, "'");
3160 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003161 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003162 }
3163
3164 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003165 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003166 /* queue : current, max */
3167 "<td>%s</td><td>%s</td><td></td>"
3168 /* sessions rate : current, max, limit */
3169 "<td>%s</td><td>%s</td><td></td>"
3170 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003171 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003172 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
3173 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003174
3175 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003176 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003177 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003178 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003179 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003180 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003181 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
3182 U2H(px->be_counters.cum_conn),
3183 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003184
Willy Tarreau466c9b52012-12-23 02:25:03 +01003185 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003186 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003187 chunk_appendf(&trash,
3188 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3189 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3190 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3191 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3192 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3193 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3194 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3195 "<tr><th>- other responses:</th><td>%s</td></tr>"
3196 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3197 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003198 U2H(px->be_counters.p.http.cum_req),
3199 U2H(px->be_counters.p.http.rsp[1]),
3200 U2H(px->be_counters.p.http.rsp[2]),
3201 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003202 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003203 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003204 U2H(px->be_counters.p.http.rsp[3]),
3205 U2H(px->be_counters.p.http.rsp[4]),
3206 U2H(px->be_counters.p.http.rsp[5]),
3207 U2H(px->be_counters.p.http.rsp[0]),
3208 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003209 }
3210
3211 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003212 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003213 /* sessions: lbtot, last */
3214 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003215 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003216 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003217 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003218 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003219 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01003220 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003221
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003222 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003223 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003224 "<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 +01003225 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003226 U2H(px->be_counters.bytes_out),
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003227 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp,
3228 px->be_counters.comp_in ?
3229 (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 +01003230 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
3231
3232 chunk_appendf(&trash,
3233 /* denied: req, resp */
3234 "<td>%s</td><td>%s</td>"
3235 /* errors : request, connect */
3236 "<td></td><td>%s</td>"
3237 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003238 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003239 /* warnings: retries, redispatches */
3240 "<td>%lld</td><td>%lld</td>"
3241 /* backend status: reflect backend status (up/down): we display UP
3242 * if the backend has known working servers or if it has no server at
3243 * all (eg: for stats). Then we display the total weight, number of
3244 * active and backups. */
3245 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3246 "<td class=ac>%d</td><td class=ac>%d</td>"
3247 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003248 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
3249 U2H(px->be_counters.failed_conns),
3250 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003251 px->be_counters.cli_aborts,
3252 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003253 px->be_counters.retries, px->be_counters.redispatches,
3254 human_time(now.tv_sec - px->last_change, 1),
3255 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
3256 "<font color=\"red\"><b>DOWN</b></font>",
3257 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3258 px->srv_act, px->srv_bck);
3259
3260 chunk_appendf(&trash,
3261 /* rest of backend: nothing, down transitions, total downtime, throttle */
3262 "<td class=ac>&nbsp;</td><td>%d</td>"
3263 "<td>%s</td>"
3264 "<td></td>"
3265 "</tr>",
3266 px->down_trans,
3267 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
3268 }
3269 else { /* CSV mode */
3270 chunk_appendf(&trash,
3271 /* pxid, name */
3272 "%s,BACKEND,"
3273 /* queue : current, max */
3274 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003275 /* sessions : current, max, limit, total */
3276 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003277 /* bytes : in, out */
3278 "%lld,%lld,"
3279 /* denied: req, resp */
3280 "%lld,%lld,"
3281 /* errors : request, connect, response */
3282 ",%lld,%lld,"
3283 /* warnings: retries, redispatches */
3284 "%lld,%lld,"
3285 /* backend status: reflect backend status (up/down): we display UP
3286 * if the backend has known working servers or if it has no server at
3287 * all (eg: for stats). Then we display the total weight, number of
3288 * active and backups. */
3289 "%s,"
3290 "%d,%d,%d,"
3291 /* rest of backend: nothing, down transitions, last change, total downtime */
3292 ",%d,%d,%d,,"
3293 /* pid, iid, sid, throttle, lbtot, tracked, type */
3294 "%d,%d,0,,%lld,,%d,"
3295 /* rate, rate_lim, rate_max, */
3296 "%u,,%u,"
3297 /* check_status, check_code, check_duration */
3298 ",,,",
3299 px->id,
3300 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
3301 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
3302 px->be_counters.bytes_in, px->be_counters.bytes_out,
3303 px->be_counters.denied_req, px->be_counters.denied_resp,
3304 px->be_counters.failed_conns, px->be_counters.failed_resp,
3305 px->be_counters.retries, px->be_counters.redispatches,
3306 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
3307 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
3308 px->srv_act, px->srv_bck,
3309 px->down_trans, (int)(now.tv_sec - px->last_change),
3310 px->srv?be_downtime(px):0,
3311 relative_pid, px->uuid,
3312 px->be_counters.cum_lbconn, STATS_TYPE_BE,
3313 read_freq_ctr(&px->be_sess_per_sec),
3314 px->be_counters.sps_max);
3315
3316 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3317 if (px->mode == PR_MODE_HTTP) {
3318 for (i=1; i<6; i++)
3319 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
3320 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
3321 }
3322 else
3323 chunk_appendf(&trash, ",,,,,,");
3324
3325 /* failed health analyses */
3326 chunk_appendf(&trash, ",");
3327
3328 /* requests : req_rate, req_rate_max, req_tot, */
3329 chunk_appendf(&trash, ",,,");
3330
3331 /* errors: cli_aborts, srv_aborts */
3332 chunk_appendf(&trash, "%lld,%lld,",
3333 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
3334
3335 /* compression: in, out, bypassed */
3336 chunk_appendf(&trash, "%lld,%lld,%lld,",
3337 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
3338
3339 /* compression: comp_rsp */
3340 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
3341
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003342 /* lastsess */
3343 chunk_appendf(&trash, "%d,", be_lastsession(px));
3344
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003345 /* finish with EOL */
3346 chunk_appendf(&trash, "\n");
3347 }
3348 return 1;
3349}
3350
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003351/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003352 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003353 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003354 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003355static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003356{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003357 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003358 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3359
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003360 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003361 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003362
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003363 /* 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 +02003364 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003365 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003366 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003367 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3368 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003369 }
3370
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003371 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003372 "<form action=\"%s%s%s%s\" method=\"post\">",
3373 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003374 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3375 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003376 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003377 }
3378
3379 /* print a new table */
3380 chunk_appendf(&trash,
3381 "<table class=\"tbl\" width=\"100%%\">\n"
3382 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003383 "<th class=\"pxname\" width=\"10%%\">");
3384
3385 chunk_appendf(&trash,
3386 "<a name=\"%s\"></a>%s"
3387 "<a class=px href=\"#%s\">%s</a>",
3388 px->id,
3389 (uri->flags & ST_SHLGNDS) ? "<u>":"",
3390 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003391
3392 if (uri->flags & ST_SHLGNDS) {
3393 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003394 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003395 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
3396 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003397 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003398 }
3399
3400 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003401 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003402 "<th class=\"%s\" width=\"90%%\">%s</th>"
3403 "</tr>\n"
3404 "</table>\n"
3405 "<table class=\"tbl\" width=\"100%%\">\n"
3406 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003407 (uri->flags & ST_SHLGNDS) ? "</u>":"",
3408 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
3409
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003410 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003411 /* Column heading for Enable or Disable server */
3412 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02003413 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003414
3415 chunk_appendf(&trash,
3416 "<th rowspan=2></th>"
3417 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003418 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003419 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
3420 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
3421 "<th colspan=9>Server</th>"
3422 "</tr>\n"
3423 "<tr class=\"titre\">"
3424 "<th>Cur</th><th>Max</th><th>Limit</th>"
3425 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003426 "<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 +01003427 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
3428 "<th>Resp</th><th>Retr</th><th>Redis</th>"
3429 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
3430 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
3431 "<th>Thrtle</th>\n"
3432 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02003433}
3434
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003435/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003436 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003437 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003438static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003439{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003440 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003441 chunk_appendf(&trash, "</table>");
3442
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003443 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003444 /* close the form used to enable/disable this proxy servers */
3445 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003446 "Choose the action to perform on the checked servers : "
3447 "<select name=action>"
3448 "<option value=\"\"></option>"
3449 "<option value=\"disable\">Disable</option>"
3450 "<option value=\"enable\">Enable</option>"
3451 "<option value=\"stop\">Soft Stop</option>"
3452 "<option value=\"start\">Soft Start</option>"
3453 "<option value=\"shutdown\">Kill Sessions</option>"
3454 "</select>"
3455 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
3456 "&nbsp;<input type=\"submit\" value=\"Apply\">"
3457 "</form>",
3458 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003459 }
3460
3461 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003462}
Willy Tarreau91861262007-10-17 17:06:05 +02003463
3464/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003465 * Dumps statistics for a proxy. The output is sent to the stream interface's
3466 * input buffer. Returns 0 if it had to stop dumping data because of lack of
3467 * buffer space, or non-zero if everything completed. This function is used
3468 * both by the CLI and the HTTP entry points, and is able to dump the output
3469 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02003470 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003471static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02003472{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003473 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02003474 struct session *s = session_from_task(si->owner);
Willy Tarreau7421efb2012-07-02 15:11:27 +02003475 struct channel *rep = si->ib;
Willy Tarreau44267702011-10-28 15:35:33 +02003476 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003477 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02003478
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003479 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02003480
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003481 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003482 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02003483 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02003484 if (uri && uri->scope) {
3485 /* we have a limited scope, we have to check the proxy name */
3486 struct stat_scope *scope;
3487 int len;
3488
3489 len = strlen(px->id);
3490 scope = uri->scope;
3491
3492 while (scope) {
3493 /* match exact proxy name */
3494 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
3495 break;
3496
3497 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02003498 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02003499 break;
3500 scope = scope->next;
3501 }
3502
3503 /* proxy name not found : don't dump anything */
3504 if (scope == NULL)
3505 return 1;
3506 }
3507
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003508 /* if the user has requested a limited output and the proxy
3509 * name does not match, skip it.
3510 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003511 if (appctx->ctx.stats.scope_len &&
3512 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 +02003513 return 1;
3514
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003515 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
3516 (appctx->ctx.stats.iid != -1) &&
3517 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003518 return 1;
3519
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003520 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02003521 /* fall through */
3522
Willy Tarreau295a8372011-03-10 11:25:07 +01003523 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003524 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003525 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003526 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau55bb8452007-10-17 18:44:57 +02003527 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003528 }
Willy Tarreau91861262007-10-17 17:06:05 +02003529
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003530 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02003531 /* fall through */
3532
Willy Tarreau295a8372011-03-10 11:25:07 +01003533 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02003534 /* print the frontend */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003535 if (stats_dump_fe_stats(si, px))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003536 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02003537 return 0;
Willy Tarreau91861262007-10-17 17:06:05 +02003538
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003539 appctx->ctx.stats.l = px->conf.listeners.n;
3540 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003541 /* fall through */
3542
Willy Tarreau295a8372011-03-10 11:25:07 +01003543 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003544 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003545 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003546 if (buffer_almost_full(rep->buf)) {
3547 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003548 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003549 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003550
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003551 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003552 if (!l->counters)
3553 continue;
3554
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003555 if (appctx->ctx.stats.flags & STAT_BOUND) {
3556 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003557 break;
3558
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003559 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003560 continue;
3561 }
3562
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003563 /* print the frontend */
3564 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0))
3565 if (bi_putchk(rep, &trash) == -1)
3566 return 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003567 }
3568
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003569 appctx->ctx.stats.sv = px->srv; /* may be NULL */
3570 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02003571 /* fall through */
3572
Willy Tarreau295a8372011-03-10 11:25:07 +01003573 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02003574 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003575 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreau2ea81932007-11-30 12:04:38 +01003576 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 +02003577
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003578 if (buffer_almost_full(rep->buf)) {
3579 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau4e33d862009-10-11 23:35:10 +02003580 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01003581 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02003582
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003583 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02003584
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003585 if (appctx->ctx.stats.flags & STAT_BOUND) {
3586 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003587 break;
3588
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003589 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01003590 continue;
3591 }
3592
Willy Tarreau32091232014-05-16 13:52:00 +02003593 svs = sv;
3594 while (svs->track)
3595 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01003596
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003597 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
3598 /* server is UP. The possibilities are :
3599 * - UP, draining, going down => state = 6
3600 * - UP, going down => state = 2
3601 * - UP, draining => state = 7
3602 * - UP, checked => state = 3
3603 * - UP, not checked nor tracked => state = 8
3604 */
3605
3606 if ((svs->check.state & CHK_ST_ENABLED) &&
3607 (svs->check.health < svs->check.rise + svs->check.fall - 1))
3608 sv_state = 2;
Willy Tarreau91861262007-10-17 17:06:05 +02003609 else
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003610 sv_state = 3;
Willy Tarreau2ea81932007-11-30 12:04:38 +01003611
Willy Tarreauefe28222014-05-21 17:13:13 +02003612 if (server_is_draining(sv))
Simon Horman8c3d0be2013-11-25 10:46:40 +09003613 sv_state += 4;
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003614
3615 if (sv_state == 3 && !(svs->check.state & CHK_ST_ENABLED))
3616 sv_state = 8; /* unchecked UP */
Willy Tarreau2ea81932007-11-30 12:04:38 +01003617 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003618 else if (sv->state == SRV_ST_STOPPING) {
3619 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
3620 (svs->check.health == svs->check.rise + svs->check.fall - 1))
3621 sv_state = 5; /* NOLB */
Willy Tarreau91861262007-10-17 17:06:05 +02003622 else
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003623 sv_state = 4; /* NOLB going down */
3624 }
3625 else { /* stopped */
3626 if ((svs->check.state & CHK_ST_ENABLED && !svs->check.health) ||
3627 (svs->agent.state & CHK_ST_ENABLED && !svs->agent.health))
Willy Tarreau91861262007-10-17 17:06:05 +02003628 sv_state = 0; /* DOWN */
Willy Tarreauf4e38b32014-05-21 15:04:05 +02003629 else
3630 sv_state = 1; /* going up */
3631 }
Willy Tarreau91861262007-10-17 17:06:05 +02003632
Willy Tarreau20125212014-05-13 19:44:56 +02003633 if (((sv_state == 0) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02003634 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003635 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02003636 continue;
3637 }
3638
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003639 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state))
3640 if (bi_putchk(rep, &trash) == -1)
3641 return 0;
3642 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02003643
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003644 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003645 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02003646
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003647 case STAT_PX_ST_BE:
3648 /* print the backend */
3649 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0))
3650 if (bi_putchk(rep, &trash) == -1)
3651 return 0;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003652
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003653 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003654 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003655
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003656 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003657 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003658 stats_dump_html_px_end(si, px);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003659 if (bi_putchk(rep, &trash) == -1)
3660 return 0;
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003661 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003662
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003663 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003664 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003665
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003666 case STAT_PX_ST_FIN:
3667 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003668
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003669 default:
3670 /* unknown state, we should put an abort() here ! */
3671 return 1;
3672 }
3673}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003674
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003675/* Dumps the HTTP stats head block to the trash for and uses the per-uri
3676 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003677 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003678static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003679{
3680 /* WARNING! This must fit in the first buffer !!! */
3681 chunk_appendf(&trash,
3682 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
3683 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
3684 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
3685 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
3686 "<style type=\"text/css\"><!--\n"
3687 "body {"
3688 " font-family: arial, helvetica, sans-serif;"
3689 " font-size: 12px;"
3690 " font-weight: normal;"
3691 " color: black;"
3692 " background: white;"
3693 "}\n"
3694 "th,td {"
3695 " font-size: 10px;"
3696 "}\n"
3697 "h1 {"
3698 " font-size: x-large;"
3699 " margin-bottom: 0.5em;"
3700 "}\n"
3701 "h2 {"
3702 " font-family: helvetica, arial;"
3703 " font-size: x-large;"
3704 " font-weight: bold;"
3705 " font-style: italic;"
3706 " color: #6020a0;"
3707 " margin-top: 0em;"
3708 " margin-bottom: 0em;"
3709 "}\n"
3710 "h3 {"
3711 " font-family: helvetica, arial;"
3712 " font-size: 16px;"
3713 " font-weight: bold;"
3714 " color: #b00040;"
3715 " background: #e8e8d0;"
3716 " margin-top: 0em;"
3717 " margin-bottom: 0em;"
3718 "}\n"
3719 "li {"
3720 " margin-top: 0.25em;"
3721 " margin-right: 2em;"
3722 "}\n"
3723 ".hr {margin-top: 0.25em;"
3724 " border-color: black;"
3725 " border-bottom-style: solid;"
3726 "}\n"
3727 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
3728 ".total {background: #20D0D0;color: #ffff80;}\n"
3729 ".frontend {background: #e8e8d0;}\n"
3730 ".socket {background: #d0d0d0;}\n"
3731 ".backend {background: #e8e8d0;}\n"
3732 ".active0 {background: #ff9090;}\n"
3733 ".active1 {background: #ffd020;}\n"
3734 ".active2 {background: #ffffa0;}\n"
3735 ".active3 {background: #c0ffc0;}\n"
3736 ".active4 {background: #ffffa0;}\n" /* NOLB state shows same as going down */
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003737 ".active5 {background: #20a0ff;}\n" /* NOLB state shows different to be detected */
3738 ".active6 {background: #ffffa0;}\n" /* DRAIN going down = same as going down */
3739 ".active7 {background: #20a0FF;}\n" /* DRAIN must be detected (weight=0) */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003740 ".active8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003741 ".backup0 {background: #ff9090;}\n"
3742 ".backup1 {background: #ff80ff;}\n"
3743 ".backup2 {background: #c060ff;}\n"
3744 ".backup3 {background: #b0d0ff;}\n"
3745 ".backup4 {background: #c060ff;}\n" /* NOLB state shows same as going down */
3746 ".backup5 {background: #90b0e0;}\n" /* NOLB state shows same as going down */
Simon Horman8c3d0be2013-11-25 10:46:40 +09003747 ".backup6 {background: #c060ff;}\n"
3748 ".backup7 {background: #cc9900;}\n"
3749 ".backup8 {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003750 ".maintain {background: #c07820;}\n"
3751 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
3752 "\n"
3753 "a.px:link {color: #ffff40; text-decoration: none;}"
3754 "a.px:visited {color: #ffff40; text-decoration: none;}"
3755 "a.px:hover {color: #ffffff; text-decoration: none;}"
3756 "a.lfsb:link {color: #000000; text-decoration: none;}"
3757 "a.lfsb:visited {color: #000000; text-decoration: none;}"
3758 "a.lfsb:hover {color: #505050; text-decoration: none;}"
3759 "\n"
3760 "table.tbl { border-collapse: collapse; border-style: none;}\n"
3761 "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"
3762 "table.tbl td.ac { text-align: center;}\n"
3763 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
3764 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
3765 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
3766 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
3767 "\n"
3768 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
3769 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
3770 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003771 "table.det { border-collapse: collapse; border-style: none; }\n"
3772 "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 +01003773 "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 +01003774 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003775 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003776 " display:block;\n"
3777 " visibility:hidden;\n"
3778 " z-index:2147483647;\n"
3779 " position:absolute;\n"
3780 " padding:2px 4px 3px;\n"
3781 " background:#f0f060; color:#000000;\n"
3782 " border:1px solid #7040c0;\n"
3783 " white-space:nowrap;\n"
3784 " font-style:normal;font-size:11px;font-weight:normal;\n"
3785 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
3786 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
3787 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003788 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003789 "-->\n"
3790 "</style></head>\n",
3791 (uri->flags & ST_SHNODE) ? " on " : "",
3792 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
3793 );
3794}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003795
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003796/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003797 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003798 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003799 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003800static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003801{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003802 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003803 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003804 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01003805
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003806 /* WARNING! this has to fit the first packet too.
3807 * We are around 3.5 kB, add adding entries will
3808 * become tricky if we want to support 4kB buffers !
3809 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003810 chunk_appendf(&trash,
3811 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
3812 PRODUCT_NAME "%s</a></h1>\n"
3813 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
3814 "<hr width=\"100%%\" class=\"hr\">\n"
3815 "<h3>&gt; General process information</h3>\n"
3816 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
3817 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
3818 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
3819 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
3820 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
3821 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
3822 "Running tasks: %d/%d; idle = %d %%<br>\n"
3823 "</td><td align=\"center\" nowrap>\n"
3824 "<table class=\"lgd\"><tr>\n"
3825 "<td class=\"active3\">&nbsp;</td><td class=\"noborder\">active UP </td>"
3826 "<td class=\"backup3\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
3827 "</tr><tr>\n"
3828 "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>"
3829 "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>"
3830 "</tr><tr>\n"
3831 "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>"
3832 "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>"
3833 "</tr><tr>\n"
3834 "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
Simon Horman8c3d0be2013-11-25 10:46:40 +09003835 "<td class=\"active8\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003836 "</tr><tr>\n"
3837 "<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 -07003838 "</tr><tr>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003839 "<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 +01003840 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01003841 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003842 "</td>"
3843 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3844 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
3845 "",
3846 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
3847 pid, (uri->flags & ST_SHNODE) ? " on " : "",
3848 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
3849 (uri->flags & ST_SHDESC) ? ": " : "",
3850 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
3851 pid, relative_pid, global.nbproc,
3852 up / 86400, (up % 86400) / 3600,
3853 (up % 3600) / 60, (up % 60),
3854 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
3855 global.rlimit_memmax ? " MB" : "",
3856 global.rlimit_nofile,
3857 global.maxsock, global.maxconn, global.maxpipes,
3858 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
3859 run_queue_cur, nb_tasks_cur, idle_pct
3860 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02003861
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003862 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3863 memcpy(scope_txt, bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3864 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003865
3866 chunk_appendf(&trash,
Cyril Bonté54656842013-04-18 22:38:35 +02003867 "<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 +02003868 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003869 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3870 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3871 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003872 STAT_SCOPE_TXT_MAXLEN);
3873
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003874 /* 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 +02003875 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003876 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003877 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003878 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
3879 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003880 }
3881
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003882 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003883 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003884 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003885 uri->uri_prefix,
3886 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003887 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003888 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003889 else
3890 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003891 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003892 uri->uri_prefix,
3893 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003894 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003895 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02003896
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003897 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003898 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003899 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003900 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003901 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003902 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003903 "",
3904 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003905 else
3906 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003907 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003908 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003909 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003910 ";norefresh",
3911 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003912 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02003913
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003914 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003915 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003916 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003917 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3918 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003919 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02003920
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003921 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003922 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003923 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003924 (uri->refresh > 0) ? ";norefresh" : "",
3925 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003926
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003927 chunk_appendf(&trash,
3928 "</ul></td>"
3929 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
3930 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
3931 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
3932 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
3933 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
3934 "</ul>"
3935 "</td>"
3936 "</tr></table>\n"
3937 ""
3938 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01003939
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003940 if (appctx->ctx.stats.st_code) {
3941 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003942 case STAT_STATUS_DONE:
3943 chunk_appendf(&trash,
3944 "<p><div class=active3>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003945 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003946 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02003947 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003948 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3949 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003950 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003951 break;
3952 case STAT_STATUS_NONE:
3953 chunk_appendf(&trash,
3954 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003955 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003956 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02003957 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003958 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3959 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003960 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003961 break;
3962 case STAT_STATUS_PART:
3963 chunk_appendf(&trash,
3964 "<p><div class=active2>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003965 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003966 "Action partially processed.<br>"
3967 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02003968 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003969 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3970 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003971 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003972 break;
3973 case STAT_STATUS_ERRP:
3974 chunk_appendf(&trash,
3975 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003976 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003977 "Action not processed because of invalid parameters."
3978 "<ul>"
3979 "<li>The action is maybe unknown.</li>"
3980 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
3981 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
3982 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003983 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003984 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3985 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003986 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003987 break;
3988 case STAT_STATUS_EXCD:
3989 chunk_appendf(&trash,
3990 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003991 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003992 "<b>Action not processed : the buffer couldn't store all the data.<br>"
3993 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02003994 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003995 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3996 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02003997 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003998 break;
3999 case STAT_STATUS_DENY:
4000 chunk_appendf(&trash,
4001 "<p><div class=active0>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004002 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004003 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004004 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004005 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4006 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004007 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004008 break;
4009 default:
4010 chunk_appendf(&trash,
Simon Horman8c3d0be2013-11-25 10:46:40 +09004011 "<p><div class=active8>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004012 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004013 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004014 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004015 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4016 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004017 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004018 }
4019 chunk_appendf(&trash, "<p>\n");
4020 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004021}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004022
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004023/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4024 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004025 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004026static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004027{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004028 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004029}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004030
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004031/* This function dumps statistics onto the stream interface's read buffer in
4032 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004033 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4034 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
4035 * and the session must be closed, or -1 in case of any error. This function is
4036 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004037 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004038static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004039{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004040 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004041 struct channel *rep = si->ib;
4042 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004043
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004044 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004045
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004046 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004047 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004048 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004049 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004050
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004051 case STAT_ST_HEAD:
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_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004054 else
4055 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004056
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004057 if (bi_putchk(rep, &trash) == -1)
4058 return 0;
Willy Tarreauae526782010-03-04 20:34:23 +01004059
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004060 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004061 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004062
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004063 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004064 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004065 stats_dump_html_info(si, uri);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004066 if (bi_putchk(rep, &trash) == -1)
Willy Tarreau91861262007-10-17 17:06:05 +02004067 return 0;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004068 }
Willy Tarreau91861262007-10-17 17:06:05 +02004069
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004070 appctx->ctx.stats.px = proxy;
4071 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
4072 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02004073 /* fall through */
4074
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004075 case STAT_ST_LIST:
4076 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004077 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004078 if (buffer_almost_full(rep->buf)) {
4079 rep->flags |= CF_WAKE_WRITE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004080 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004081 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004082
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004083 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004084 /* skip the disabled proxies, global frontend and non-networked ones */
4085 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004086 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004087 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004088
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004089 appctx->ctx.stats.px = px->next;
4090 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004091 }
4092 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004093
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004094 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004095 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004096
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004097 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004098 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004099 stats_dump_html_end();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004100 if (bi_putchk(rep, &trash) == -1)
4101 return 0;
4102 }
Willy Tarreau55058a72012-11-21 08:27:21 +01004103
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004104 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004105 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02004106
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004107 case STAT_ST_FIN:
4108 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01004109
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004110 default:
4111 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004112 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004113 return -1;
4114 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004115}
Willy Tarreauae526782010-03-04 20:34:23 +01004116
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004117/* We reached the stats page through a POST request. The appctx is
4118 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01004119 * Parse the posted data and enable/disable servers if necessary.
4120 * Returns 1 if request was parsed or zero if it needs more data.
4121 */
4122static int stats_process_http_post(struct stream_interface *si)
4123{
4124 struct session *s = session_from_task(si->owner);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004125 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004126
4127 struct proxy *px = NULL;
4128 struct server *sv = NULL;
4129
4130 char key[LINESIZE];
4131 int action = ST_ADM_ACTION_NONE;
4132 int reprocess = 0;
4133
4134 int total_servers = 0;
4135 int altered_servers = 0;
4136
4137 char *first_param, *cur_param, *next_param, *end_params;
4138 char *st_cur_param = NULL;
4139 char *st_next_param = NULL;
4140
4141 struct chunk *temp;
4142 int reql;
4143
4144 temp = get_trash_chunk();
4145 if (temp->size < s->txn.req.body_len) {
4146 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004147 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004148 goto out;
4149 }
4150
4151 reql = bo_getblk(si->ob, temp->str, s->txn.req.body_len, s->txn.req.eoh + 2);
4152 if (reql <= 0) {
4153 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004154 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004155 return 0;
4156 }
4157
4158 first_param = temp->str;
4159 end_params = temp->str + reql;
4160 cur_param = next_param = end_params;
4161 *end_params = '\0';
4162
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004163 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004164
4165 /*
4166 * Parse the parameters in reverse order to only store the last value.
4167 * From the html form, the backend and the action are at the end.
4168 */
4169 while (cur_param > first_param) {
4170 char *value;
4171 int poffset, plen;
4172
4173 cur_param--;
4174
4175 if ((*cur_param == '&') || (cur_param == first_param)) {
4176 reprocess_servers:
4177 /* Parse the key */
4178 poffset = (cur_param != first_param ? 1 : 0);
4179 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
4180 if ((plen > 0) && (plen <= sizeof(key))) {
4181 strncpy(key, cur_param + poffset, plen);
4182 key[plen - 1] = '\0';
4183 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004184 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004185 goto out;
4186 }
4187
4188 /* Parse the value */
4189 value = key;
4190 while (*value != '\0' && *value != '=') {
4191 value++;
4192 }
4193 if (*value == '=') {
4194 /* Ok, a value is found, we can mark the end of the key */
4195 *value++ = '\0';
4196 }
4197 if (url_decode(key) < 0 || url_decode(value) < 0)
4198 break;
4199
4200 /* Now we can check the key to see what to do */
4201 if (!px && (strcmp(key, "b") == 0)) {
4202 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
4203 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004204 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004205 goto out;
4206 }
4207 }
4208 else if (!action && (strcmp(key, "action") == 0)) {
4209 if (strcmp(value, "disable") == 0) {
4210 action = ST_ADM_ACTION_DISABLE;
4211 }
4212 else if (strcmp(value, "enable") == 0) {
4213 action = ST_ADM_ACTION_ENABLE;
4214 }
4215 else if (strcmp(value, "stop") == 0) {
4216 action = ST_ADM_ACTION_STOP;
4217 }
4218 else if (strcmp(value, "start") == 0) {
4219 action = ST_ADM_ACTION_START;
4220 }
4221 else if (strcmp(value, "shutdown") == 0) {
4222 action = ST_ADM_ACTION_SHUTDOWN;
4223 }
4224 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004225 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004226 goto out;
4227 }
4228 }
4229 else if (strcmp(key, "s") == 0) {
4230 if (!(px && action)) {
4231 /*
4232 * Indicates that we'll need to reprocess the parameters
4233 * as soon as backend and action are known
4234 */
4235 if (!reprocess) {
4236 st_cur_param = cur_param;
4237 st_next_param = next_param;
4238 }
4239 reprocess = 1;
4240 }
4241 else if ((sv = findserver(px, value)) != NULL) {
4242 switch (action) {
4243 case ST_ADM_ACTION_DISABLE:
Willy Tarreaua0066dd2014-05-16 11:25:16 +02004244 if ((px->state != PR_STSTOPPED) && !(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004245 altered_servers++;
4246 total_servers++;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02004247 srv_adm_set_maint(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004248 }
4249 break;
4250 case ST_ADM_ACTION_ENABLE:
Willy Tarreaua0066dd2014-05-16 11:25:16 +02004251 if ((px->state != PR_STSTOPPED) && (sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004252 altered_servers++;
4253 total_servers++;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02004254 srv_adm_set_ready(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004255 }
4256 break;
4257 case ST_ADM_ACTION_STOP:
4258 case ST_ADM_ACTION_START:
4259 if (action == ST_ADM_ACTION_START)
4260 sv->uweight = sv->iweight;
4261 else
4262 sv->uweight = 0;
4263
4264 server_recalc_eweight(sv);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004265
4266 altered_servers++;
4267 total_servers++;
4268 break;
4269 case ST_ADM_ACTION_SHUTDOWN:
4270 if (px->state != PR_STSTOPPED) {
4271 struct session *sess, *sess_bck;
4272
4273 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
4274 if (sess->srv_conn == sv)
4275 session_shutdown(sess, SN_ERR_KILLED);
4276
4277 altered_servers++;
4278 total_servers++;
4279 }
4280 break;
4281 }
4282 } else {
4283 /* the server name is unknown or ambiguous (duplicate names) */
4284 total_servers++;
4285 }
4286 }
4287 if (reprocess && px && action) {
4288 /* Now, we know the backend and the action chosen by the user.
4289 * We can safely restart from the first server parameter
4290 * to reprocess them
4291 */
4292 cur_param = st_cur_param;
4293 next_param = st_next_param;
4294 reprocess = 0;
4295 goto reprocess_servers;
4296 }
4297
4298 next_param = cur_param;
4299 }
4300 }
4301
4302 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004303 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004304 }
4305 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004306 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004307 }
4308 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004309 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004310 }
4311 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004312 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004313 }
4314 out:
4315 return 1;
4316}
4317
4318
4319static int stats_send_http_headers(struct stream_interface *si)
4320{
4321 struct session *s = session_from_task(si->owner);
4322 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004323 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004324
4325 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02004326 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01004327 "Cache-Control: no-cache\r\n"
4328 "Connection: close\r\n"
4329 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004330 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004331
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004332 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01004333 chunk_appendf(&trash, "Refresh: %d\r\n",
4334 uri->refresh);
4335
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004336 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
4337
4338 if (appctx->ctx.stats.flags & STAT_CHUNKED)
4339 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
4340 else
4341 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01004342
4343 s->txn.status = 200;
4344 s->logs.tv_request = now;
4345
4346 if (bi_putchk(si->ib, &trash) == -1)
4347 return 0;
4348
4349 return 1;
4350}
4351
4352static int stats_send_http_redirect(struct stream_interface *si)
4353{
4354 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4355 struct session *s = session_from_task(si->owner);
4356 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004357 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01004358
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004359 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004360 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004361 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004362 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004363 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(si->ob->buf) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
4364 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004365 }
4366
4367 /* We don't want to land on the posted stats page because a refresh will
4368 * repost the data. We don't want this to happen on accident so we redirect
4369 * the browse to the stats page with a GET.
4370 */
4371 chunk_printf(&trash,
4372 "HTTP/1.1 303 See Other\r\n"
4373 "Cache-Control: no-cache\r\n"
4374 "Content-Type: text/plain\r\n"
4375 "Connection: close\r\n"
4376 "Location: %s;st=%s%s%s%s\r\n"
4377 "\r\n",
4378 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004379 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
4380 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
4381 stat_status_codes[appctx->ctx.stats.st_code]) ?
4382 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01004383 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004384 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4385 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01004386 scope_txt);
4387
4388 s->txn.status = 303;
4389 s->logs.tv_request = now;
4390
4391 if (bi_putchk(si->ib, &trash) == -1)
4392 return 0;
4393
4394 return 1;
4395}
4396
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004397/* This I/O handler runs as an applet embedded in a stream interface. It is
4398 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004399 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004400 * automatically unregisters itself once transfer is complete.
4401 */
4402static void http_stats_io_handler(struct stream_interface *si)
4403{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004404 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02004405 struct session *s = session_from_task(si->owner);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004406 struct channel *req = si->ob;
4407 struct channel *res = si->ib;
Willy Tarreau55058a72012-11-21 08:27:21 +01004408
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004409 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
4410 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004411
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004412 /* check that the output is not closed */
4413 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004414 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004415
Willy Tarreau347a35d2013-11-22 17:51:09 +01004416 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004417 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004418 if (stats_send_http_headers(si)) {
4419 if (s->txn.meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004420 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004421 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004422 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004423 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004424 }
4425
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004426 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004427 unsigned int prev_len = si->ib->buf->i;
4428 unsigned int data_len;
4429 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02004430 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004431
4432 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4433 /* One difficulty we're facing is that we must prevent
4434 * the input data from being automatically forwarded to
4435 * the output area. For this, we temporarily disable
4436 * forwarding on the channel.
4437 */
4438 last_fwd = si->ib->to_forward;
4439 si->ib->to_forward = 0;
4440 chunk_printf(&trash, "\r\n000000\r\n");
4441 if (bi_putchk(si->ib, &trash) == -1) {
4442 si->ib->to_forward = last_fwd;
4443 goto fail;
4444 }
4445 }
4446
4447 data_len = si->ib->buf->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004448 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004449 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004450
4451 last_len = si->ib->buf->i;
4452
4453 /* Now we must either adjust or remove the chunk size. This is
4454 * not easy because the chunk size might wrap at the end of the
4455 * buffer, so we pretend we have nothing in the buffer, we write
4456 * the size, then restore the buffer's contents. Note that we can
4457 * only do that because no forwarding is scheduled on the stats
4458 * applet.
4459 */
4460 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4461 si->ib->total -= (last_len - prev_len);
4462 si->ib->buf->i -= (last_len - prev_len);
4463
4464 if (last_len != data_len) {
4465 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
4466 bi_putchk(si->ib, &trash);
4467
4468 si->ib->total += (last_len - data_len);
4469 si->ib->buf->i += (last_len - data_len);
4470 }
4471 /* now re-enable forwarding */
4472 channel_forward(si->ib, last_fwd);
4473 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004474 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02004475
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004476 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004477 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004478 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004479 else if (si->ob->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004480 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004481 }
4482
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004483 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01004484 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004485 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01004486 }
4487
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004488 if (appctx->st0 == STAT_HTTP_DONE) {
4489 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
4490 chunk_printf(&trash, "\r\n0\r\n\r\n");
4491 if (bi_putchk(si->ib, &trash) == -1)
4492 goto fail;
4493 }
4494 /* eat the whole request */
4495 bo_skip(si->ob, si->ob->buf->o);
4496 res->flags |= CF_READ_NULL;
4497 si_shutr(si);
4498 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01004499
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004500 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
4501 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004502
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004503 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01004504 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
4505 si_shutr(si);
4506 res->flags |= CF_READ_NULL;
4507 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004508 }
Willy Tarreau91861262007-10-17 17:06:05 +02004509
Willy Tarreauaf3cf702014-04-22 22:19:53 +02004510 fail:
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004511 /* update all other flags and resync with the other side */
4512 si_update(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004513
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004514 /* we don't want to expire timeouts while we're processing requests */
4515 si->ib->rex = TICK_ETERNITY;
4516 si->ob->wex = TICK_ETERNITY;
Willy Tarreau91861262007-10-17 17:06:05 +02004517
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004518 out:
4519 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) {
4520 /* check that we have released everything then unregister */
4521 stream_int_unregister_handler(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004522 }
4523}
4524
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004525
Willy Tarreau909d5172012-11-26 03:04:41 +01004526static inline const char *get_conn_ctrl_name(const struct connection *conn)
4527{
Willy Tarreau3c728722014-01-23 13:50:42 +01004528 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004529 return "NONE";
4530 return conn->ctrl->name;
4531}
4532
4533static inline const char *get_conn_xprt_name(const struct connection *conn)
4534{
4535 static char ptr[17];
4536
Willy Tarreauaad69382014-01-23 14:21:42 +01004537 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01004538 return "NONE";
4539
4540 if (conn->xprt == &raw_sock)
4541 return "RAW";
4542
4543#ifdef USE_OPENSSL
4544 if (conn->xprt == &ssl_sock)
4545 return "SSL";
4546#endif
4547 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
4548 return ptr;
4549}
4550
4551static inline const char *get_conn_data_name(const struct connection *conn)
4552{
4553 static char ptr[17];
4554
4555 if (!conn->data)
4556 return "NONE";
4557
4558 if (conn->data == &sess_conn_cb)
4559 return "SESS";
4560
4561 if (conn->data == &si_conn_cb)
4562 return "STRM";
4563
4564 if (conn->data == &check_conn_cb)
4565 return "CHCK";
4566
4567 snprintf(ptr, sizeof(ptr), "%p", conn->data);
4568 return ptr;
4569}
4570
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004571/* This function dumps a complete session state onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02004572 * read buffer. The session has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01004573 * 0 if the output buffer is full and it needs to be called again, otherwise
4574 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004575 */
Willy Tarreau76153662012-11-26 01:16:39 +01004576static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct session *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004577{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004578 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004579 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004580 extern const char *monthname[12];
4581 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004582 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004583 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004584
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004585 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004587 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004588 /* session changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004589 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
4590 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004591 return 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004592 appctx->ctx.sess.uid = 0;
4593 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004594 return 1;
4595 }
4596
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004597 switch (appctx->ctx.sess.section) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004598 case 0: /* main status of the session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004599 appctx->ctx.sess.uid = sess->uniq_id;
4600 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004601 /* fall through */
4602
4603 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004604 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004605 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004606 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004607 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004608 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
4609 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004610 sess->uniq_id,
Willy Tarreaue95c4ce2013-01-24 00:48:39 +01004611 sess->listener && sess->listener->proto->name ? sess->listener->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004612
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004613 conn = objt_conn(sess->si[0].end);
4614 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004615 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004616 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004617 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004618 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004619 break;
4620 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004621 chunk_appendf(&trash, " source=unix:%d\n", sess->listener->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02004622 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004623 default:
4624 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004625 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004626 break;
4627 }
4628
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004629 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004630 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02004631 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004632
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004633 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004634 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004635 sess->fe->id, sess->fe->uuid, sess->fe->mode ? "http" : "tcp",
4636 sess->listener ? sess->listener->name ? sess->listener->name : "?" : "?",
4637 sess->listener ? sess->listener->luid : 0);
4638
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004639 if (conn)
4640 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004641
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004642 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004643 case AF_INET:
4644 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004645 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004646 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004647 break;
4648 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004649 chunk_appendf(&trash, " addr=unix:%d\n", sess->listener->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07004650 break;
4651 default:
4652 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004653 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004654 break;
4655 }
4656
Willy Tarreau50943332011-09-02 17:33:05 +02004657 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004658 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004659 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02004660 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07004661 sess->be->uuid, sess->be->mode ? "http" : "tcp");
4662 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004663 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004664
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004665 conn = objt_conn(sess->si[1].end);
4666 if (conn)
4667 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004668
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004669 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004670 case AF_INET:
4671 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004672 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004673 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07004674 break;
4675 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004676 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004677 break;
4678 default:
4679 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004680 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004681 break;
4682 }
4683
4684 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004685 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07004686 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004687 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
4688 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02004689 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004690 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07004691
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004692 if (conn)
4693 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004694
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004695 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07004696 case AF_INET:
4697 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004698 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004699 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07004700 break;
4701 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004702 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004703 break;
4704 default:
4705 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004706 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07004707 break;
4708 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004709
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004710 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004711 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004712 sess->task,
4713 sess->task->state,
4714 sess->task->nice, sess->task->calls,
4715 sess->task->expire ?
4716 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
4717 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
4718 TICKS_TO_MS(1000)) : "<NEVER>",
4719 task_in_rq(sess->task) ? ", running" : "");
4720
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004721 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004722 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004723 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
4724
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004725 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004726 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s\n",
4727 &sess->txn, sess->txn.flags, sess->txn.meth, sess->txn.status,
4728 http_msg_state_str(sess->txn.req.msg_state), http_msg_state_str(sess->txn.rsp.msg_state));
4729
4730 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004731 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004732 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004733 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004734 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004735 obj_type_name(sess->si[0].end),
4736 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004737 sess->si[0].exp ?
4738 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
4739 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
4740 TICKS_TO_MS(1000)) : "<NEVER>",
4741 sess->si[0].err_type);
4742
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004743 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004744 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004745 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01004746 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004747 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004748 obj_type_name(sess->si[1].end),
4749 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004750 sess->si[1].exp ?
4751 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
4752 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
4753 TICKS_TO_MS(1000)) : "<NEVER>",
4754 sess->si[1].err_type);
4755
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004756 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004757 chunk_appendf(&trash,
4758 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004759 conn,
4760 get_conn_ctrl_name(conn),
4761 get_conn_xprt_name(conn),
4762 get_conn_data_name(conn),
4763 obj_type_name(conn->target),
4764 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004765
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004766 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01004767 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004768 conn->flags,
4769 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004770 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004771 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004772 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004773 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004774 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
4775 chunk_appendf(&trash,
4776 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
4777 tmpctx,
4778 tmpctx->st0,
4779 tmpctx->st1,
4780 tmpctx->st2,
4781 tmpctx->applet->name);
4782 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01004783
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004784 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004785 chunk_appendf(&trash,
4786 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004787 conn,
4788 get_conn_ctrl_name(conn),
4789 get_conn_xprt_name(conn),
4790 get_conn_data_name(conn),
4791 obj_type_name(conn->target),
4792 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01004793
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004794 chunk_appendf(&trash,
4795 " flags=0x%08x fd=%d fd_spec_e=%02x fd_spec_p=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004796 conn->flags,
4797 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01004798 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01004799 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004800 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02004801 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01004802 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
4803 chunk_appendf(&trash,
4804 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
4805 tmpctx,
4806 tmpctx->st0,
4807 tmpctx->st1,
4808 tmpctx->st2,
4809 tmpctx->applet->name);
4810 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004811
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004812 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004813 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004814 " an_exp=%s",
4815 sess->req,
4816 sess->req->flags, sess->req->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004817 sess->req->pipe ? sess->req->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004818 sess->req->to_forward, sess->req->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004819 sess->req->analyse_exp ?
4820 human_time(TICKS_TO_MS(sess->req->analyse_exp - now_ms),
4821 TICKS_TO_MS(1000)) : "<NEVER>");
4822
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004823 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004824 " rex=%s",
4825 sess->req->rex ?
4826 human_time(TICKS_TO_MS(sess->req->rex - now_ms),
4827 TICKS_TO_MS(1000)) : "<NEVER>");
4828
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004829 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004830 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004831 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004832 sess->req->wex ?
4833 human_time(TICKS_TO_MS(sess->req->wex - now_ms),
4834 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004835 sess->req->buf,
4836 sess->req->buf->data, sess->req->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004837 (int)(sess->req->buf->p - sess->req->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004838 sess->txn.req.next, sess->req->buf->i,
4839 sess->req->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004840
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004841 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01004842 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004843 " an_exp=%s",
4844 sess->rep,
4845 sess->rep->flags, sess->rep->analysers,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004846 sess->rep->pipe ? sess->rep->pipe->data : 0,
Willy Tarreau909d5172012-11-26 03:04:41 +01004847 sess->rep->to_forward, sess->rep->total,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004848 sess->rep->analyse_exp ?
4849 human_time(TICKS_TO_MS(sess->rep->analyse_exp - now_ms),
4850 TICKS_TO_MS(1000)) : "<NEVER>");
4851
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004852 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004853 " rex=%s",
4854 sess->rep->rex ?
4855 human_time(TICKS_TO_MS(sess->rep->rex - now_ms),
4856 TICKS_TO_MS(1000)) : "<NEVER>");
4857
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004858 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004859 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01004860 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004861 sess->rep->wex ?
4862 human_time(TICKS_TO_MS(sess->rep->wex - now_ms),
4863 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau909d5172012-11-26 03:04:41 +01004864 sess->rep->buf,
4865 sess->rep->buf->data, sess->rep->buf->o,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004866 (int)(sess->rep->buf->p - sess->rep->buf->data),
Willy Tarreau909d5172012-11-26 03:04:41 +01004867 sess->txn.rsp.next, sess->rep->buf->i,
4868 sess->rep->buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004869
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004870 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004871 return 0;
4872
4873 /* use other states to dump the contents */
4874 }
4875 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004876 appctx->ctx.sess.uid = 0;
4877 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01004878 return 1;
4879}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01004880
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004881static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004882{
4883 struct appctx *appctx = __objt_appctx(si->end);
4884
4885 switch (appctx->st2) {
4886 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004887 /* Display the column headers. If the message cannot be sent,
4888 * quit the fucntion with returning 0. The function is called
4889 * later and restart at the state "STAT_ST_INIT".
4890 */
4891 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01004892 chunk_appendf(&trash, "# id (file) description\n");
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004893 if (bi_putchk(si->ib, &trash) == -1)
4894 return 0;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004895
4896 /* Now, we start the browsing of the references lists.
4897 * Note that the following call to LIST_ELEM return bad pointer. The only
4898 * avalaible field of this pointer is <list>. It is used with the function
4899 * pat_list_get_next() for retruning the first avalaible entry
4900 */
4901 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
4902 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
4903 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004904 appctx->st2 = STAT_ST_LIST;
4905 /* fall through */
4906
4907 case STAT_ST_LIST:
4908 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004909 chunk_reset(&trash);
4910
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004911 /* Build messages. If the reference is used by another category than
4912 * the listed categorie, display the information in the massage.
4913 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01004914 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01004915 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
4916 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01004917
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004918 if (bi_putchk(si->ib, &trash) == -1) {
4919 /* let's try again later from this session. We add ourselves into
4920 * this session's users so that it can remove us upon termination.
4921 */
4922 return 0;
4923 }
4924
4925 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004926 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
4927 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004928 }
4929
4930 appctx->st2 = STAT_ST_FIN;
4931 /* fall through */
4932
4933 default:
4934 appctx->st2 = STAT_ST_FIN;
4935 return 1;
4936 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004937 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004938}
4939
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004940static int stats_map_lookup(struct stream_interface *si)
4941{
4942 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004943 struct sample sample;
4944 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004945 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004946
4947 switch (appctx->st2) {
4948 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004949 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01004950 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004951 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004952 appctx->st2 = STAT_ST_LIST;
4953 /* fall through */
4954
4955 case STAT_ST_LIST:
4956 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004957 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004958 /* initialise chunk to build new message */
4959 chunk_reset(&trash);
4960
4961 /* execute pattern matching */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01004962 sample.type = SMP_T_STR;
4963 sample.flags |= SMP_F_CONST;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004964 sample.data.str.len = appctx->ctx.map.chunk.len;
4965 sample.data.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01004966 if (appctx->ctx.map.expr->pat_head->match &&
4967 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004968 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
4969 else
4970 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004971
4972 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004973 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
4974 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
4975 break;
4976 if (match_method >= PAT_MATCH_NUM)
4977 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
4978 else
4979 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004980
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02004981 /* case sensitive */
4982 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
4983 chunk_appendf(&trash, ", case=insensitive");
4984 else
4985 chunk_appendf(&trash, ", case=sensitive");
4986
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004987 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01004988 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004989 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
4990 chunk_appendf(&trash, ", found=no");
4991 else
4992 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01004993 }
4994
4995 /* Display match and match info */
4996 else {
4997 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01004998 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
4999 chunk_appendf(&trash, ", found=yes");
5000 else
5001 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005002
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01005003 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02005004 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005005 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005006 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005007 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005008
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005009 /* display pattern */
5010 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5011 if (pat->ref && pat->ref->pattern)
5012 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
5013 else
5014 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005015 }
5016 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005017 if (pat->ref && pat->ref->pattern)
5018 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
5019 else
5020 chunk_appendf(&trash, ", pattern=unknown");
5021 }
5022
5023 /* display return value */
5024 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
5025 if (pat->smp && pat->ref && pat->ref->sample)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005026 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"",
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005027 pat->ref->sample, smp_to_type[pat->smp->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005028 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01005029 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005030 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005031 }
5032
Thierry FOURNIERb9903842014-03-11 18:48:17 +01005033 chunk_appendf(&trash, "\n");
5034
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005035 /* display response */
5036 if (bi_putchk(si->ib, &trash) == -1) {
5037 /* let's try again later from this session. We add ourselves into
5038 * this session's users so that it can remove us upon termination.
5039 */
5040 return 0;
5041 }
5042
5043 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005044 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
5045 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005046 }
5047
5048 appctx->st2 = STAT_ST_FIN;
5049 /* fall through */
5050
5051 default:
5052 appctx->st2 = STAT_ST_FIN;
5053 free(appctx->ctx.map.chunk.str);
5054 return 1;
5055 }
5056}
5057
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005058static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005059{
5060 struct appctx *appctx = __objt_appctx(si->end);
5061
5062 switch (appctx->st2) {
5063
5064 case STAT_ST_INIT:
5065 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005066 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
5067 struct pat_ref_elt *, list);
5068 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
5069 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005070 appctx->st2 = STAT_ST_LIST;
5071 /* fall through */
5072
5073 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005074 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005075 chunk_reset(&trash);
5076
5077 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005078 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005079 chunk_appendf(&trash, "%p %s %s\n",
5080 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
5081 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005082 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01005083 chunk_appendf(&trash, "%p %s\n",
5084 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005085
5086 if (bi_putchk(si->ib, &trash) == -1) {
5087 /* let's try again later from this session. We add ourselves into
5088 * this session's users so that it can remove us upon termination.
5089 */
5090 return 0;
5091 }
5092
5093 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005094 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
5095 struct pat_ref_elt *, list);
5096 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01005097 break;
5098 }
5099
5100 appctx->st2 = STAT_ST_FIN;
5101 /* fall through */
5102
5103 default:
5104 appctx->st2 = STAT_ST_FIN;
5105 return 1;
5106 }
5107}
5108
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005109/* This function dumps all sessions' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005110 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005111 * to be called again, otherwise non-zero. It is designed to be called
5112 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005113 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005114static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005115{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005116 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005117 struct connection *conn;
5118
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005119 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005120 /* If we're forced to shut down, we might have to remove our
5121 * reference to the last session being dumped.
5122 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005123 if (appctx->st2 == STAT_ST_LIST) {
5124 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5125 LIST_DEL(&appctx->ctx.sess.bref.users);
5126 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005127 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005128 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005129 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005130 }
5131
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005132 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005133
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005134 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005135 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005136 /* the function had not been called yet, let's prepare the
5137 * buffer for a response. We initialize the current session
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005138 * pointer to the first in the global list. When a target
5139 * session is being destroyed, it is responsible for updating
5140 * this pointer. We know we have reached the end when this
5141 * pointer points back to the head of the sessions list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005142 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005143 LIST_INIT(&appctx->ctx.sess.bref.users);
5144 appctx->ctx.sess.bref.ref = sessions.n;
5145 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005146 /* fall through */
5147
Willy Tarreau295a8372011-03-10 11:25:07 +01005148 case STAT_ST_LIST:
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005149 /* first, let's detach the back-ref from a possible previous session */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005150 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
5151 LIST_DEL(&appctx->ctx.sess.bref.users);
5152 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005153 }
5154
5155 /* and start from where we stopped */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005156 while (appctx->ctx.sess.bref.ref != &sessions) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01005157 char pn[INET6_ADDRSTRLEN];
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005158 struct session *curr_sess;
5159
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005160 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct session *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005161
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005162 if (appctx->ctx.sess.target) {
5163 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005164 goto next_sess;
5165
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005166 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005167 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01005168 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005169 return 0;
5170
5171 /* session dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005172 LIST_DEL(&appctx->ctx.sess.bref.users);
5173 LIST_INIT(&appctx->ctx.sess.bref.users);
5174 if (appctx->ctx.sess.target != (void *)-1) {
5175 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01005176 break;
5177 }
5178 else
5179 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005180 }
5181
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005182 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005183 "%p: proto=%s",
5184 curr_sess,
5185 curr_sess->listener->proto->name);
5186
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005187
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005188 conn = objt_conn(curr_sess->si[0].end);
5189 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02005190 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005191 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005192 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005193 " src=%s:%d fe=%s be=%s srv=%s",
5194 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005195 get_host_port(&conn->addr.from),
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005196 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005197 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005198 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005199 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005200 break;
5201 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005202 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02005203 " src=unix:%d fe=%s be=%s srv=%s",
5204 curr_sess->listener->luid,
5205 curr_sess->fe->id,
Willy Tarreau50943332011-09-02 17:33:05 +02005206 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005207 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02005208 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005209 break;
5210 }
5211
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005212 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02005213 " ts=%02x age=%s calls=%d",
5214 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01005215 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
5216 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005217
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005218 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005219 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005220 curr_sess->req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005221 curr_sess->req->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005222 curr_sess->req->analysers,
5223 curr_sess->req->rex ?
5224 human_time(TICKS_TO_MS(curr_sess->req->rex - now_ms),
5225 TICKS_TO_MS(1000)) : "");
5226
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005227 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005228 ",wx=%s",
5229 curr_sess->req->wex ?
5230 human_time(TICKS_TO_MS(curr_sess->req->wex - now_ms),
5231 TICKS_TO_MS(1000)) : "");
5232
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005233 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005234 ",ax=%s]",
5235 curr_sess->req->analyse_exp ?
5236 human_time(TICKS_TO_MS(curr_sess->req->analyse_exp - now_ms),
5237 TICKS_TO_MS(1000)) : "");
5238
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005239 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005240 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005241 curr_sess->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005242 curr_sess->rep->buf->i,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005243 curr_sess->rep->analysers,
5244 curr_sess->rep->rex ?
5245 human_time(TICKS_TO_MS(curr_sess->rep->rex - now_ms),
5246 TICKS_TO_MS(1000)) : "");
5247
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005248 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005249 ",wx=%s",
5250 curr_sess->rep->wex ?
5251 human_time(TICKS_TO_MS(curr_sess->rep->wex - now_ms),
5252 TICKS_TO_MS(1000)) : "");
5253
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005254 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005255 ",ax=%s]",
5256 curr_sess->rep->analyse_exp ?
5257 human_time(TICKS_TO_MS(curr_sess->rep->analyse_exp - now_ms),
5258 TICKS_TO_MS(1000)) : "");
5259
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005260 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005261 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005262 " s0=[%d,%1xh,fd=%d,ex=%s]",
5263 curr_sess->si[0].state,
5264 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005265 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005266 curr_sess->si[0].exp ?
5267 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
5268 TICKS_TO_MS(1000)) : "");
5269
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005270 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005271 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005272 " s1=[%d,%1xh,fd=%d,ex=%s]",
5273 curr_sess->si[1].state,
5274 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005275 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005276 curr_sess->si[1].exp ?
5277 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
5278 TICKS_TO_MS(1000)) : "");
5279
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005280 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005281 " exp=%s",
5282 curr_sess->task->expire ?
5283 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
5284 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01005285 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005286 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01005287
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005288 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005289
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005290 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +01005291 /* let's try again later from this session. We add ourselves into
5292 * this session's users so that it can remove us upon termination.
5293 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005294 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005295 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005296 }
5297
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005298 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005299 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005300 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005301
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005302 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005303 /* specified session not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005304 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005305 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005306 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005307 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005308
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005309 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005310 return 0;
5311
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005312 appctx->ctx.sess.target = NULL;
5313 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005314 return 1;
5315 }
5316
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005317 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005318 /* fall through */
5319
5320 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005321 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02005322 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005323 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02005324}
5325
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005326/* This is called when the stream interface is closed. For instance, upon an
5327 * external abort, we won't call the i/o handler anymore so we may need to
5328 * remove back references to the session currently being dumped.
5329 */
Simon Horman74d88312013-02-12 10:45:50 +09005330static void cli_release_handler(struct stream_interface *si)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005331{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005332 struct appctx *appctx = __objt_appctx(si->end);
5333
5334 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
5335 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
5336 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005337 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01005338 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
5339 free(appctx->ctx.cli.err);
5340 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01005341 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
5342 free(appctx->ctx.map.chunk.str);
5343 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005344}
5345
Willy Tarreau20e99322013-04-13 09:22:25 +02005346/* This function is used to either dump tables states (when action is set
5347 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02005348 * It returns 0 if the output buffer is full and it needs to be called
5349 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02005350 */
Willy Tarreau20e99322013-04-13 09:22:25 +02005351static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02005352{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005353 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau306f8302013-07-08 15:53:06 +02005354 struct session *s = session_from_task(si->owner);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005355 struct ebmb_node *eb;
5356 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01005357 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02005358 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005359
5360 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005361 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01005362 * - STAT_ST_INIT : the first call
5363 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02005364 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005365 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02005366 * and the entry pointer points to the next entry to be dumped,
5367 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01005368 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02005369 * data though.
5370 */
5371
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005372 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005373 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005374 if (appctx->st2 == STAT_ST_LIST) {
5375 appctx->ctx.table.entry->ref_cnt--;
5376 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02005377 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02005378 return 1;
5379 }
5380
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005381 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005382
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005383 while (appctx->st2 != STAT_ST_FIN) {
5384 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01005385 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005386 appctx->ctx.table.proxy = appctx->ctx.table.target;
5387 if (!appctx->ctx.table.proxy)
5388 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005389
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005390 appctx->ctx.table.entry = NULL;
5391 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005392 break;
5393
Willy Tarreau295a8372011-03-10 11:25:07 +01005394 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005395 if (!appctx->ctx.table.proxy ||
5396 (appctx->ctx.table.target &&
5397 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
5398 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005399 break;
5400 }
5401
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005402 if (appctx->ctx.table.proxy->table.size) {
5403 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
5404 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02005405 return 0;
5406
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005407 if (appctx->ctx.table.target &&
Willy Tarreau290e63a2012-09-20 18:07:14 +02005408 s->listener->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02005409 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005410 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005411 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005412 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
5413 appctx->ctx.table.entry->ref_cnt++;
5414 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005415 break;
5416 }
5417 }
5418 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005419 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005420 break;
5421
Willy Tarreau295a8372011-03-10 11:25:07 +01005422 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01005423 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09005424
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005425 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005426 /* we're filtering on some data contents */
5427 void *ptr;
5428 long long data;
5429
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005430 dt = appctx->ctx.table.data_type;
5431 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
5432 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005433 dt);
5434
5435 data = 0;
5436 switch (stktable_data_types[dt].std_type) {
5437 case STD_T_SINT:
5438 data = stktable_data_cast(ptr, std_t_sint);
5439 break;
5440 case STD_T_UINT:
5441 data = stktable_data_cast(ptr, std_t_uint);
5442 break;
5443 case STD_T_ULL:
5444 data = stktable_data_cast(ptr, std_t_ull);
5445 break;
5446 case STD_T_FRQP:
5447 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005448 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005449 break;
5450 }
5451
5452 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005453 if ((data < appctx->ctx.table.value &&
5454 (appctx->ctx.table.data_op == STD_OP_EQ ||
5455 appctx->ctx.table.data_op == STD_OP_GT ||
5456 appctx->ctx.table.data_op == STD_OP_GE)) ||
5457 (data == appctx->ctx.table.value &&
5458 (appctx->ctx.table.data_op == STD_OP_NE ||
5459 appctx->ctx.table.data_op == STD_OP_GT ||
5460 appctx->ctx.table.data_op == STD_OP_LT)) ||
5461 (data > appctx->ctx.table.value &&
5462 (appctx->ctx.table.data_op == STD_OP_EQ ||
5463 appctx->ctx.table.data_op == STD_OP_LT ||
5464 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01005465 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02005466 }
5467
Simon Hormanc88b8872011-06-15 15:18:49 +09005468 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005469 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
5470 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09005471 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005472
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005473 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005474
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005475 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02005476 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005477 struct stksess *old = appctx->ctx.table.entry;
5478 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01005479 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005480 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
5481 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5482 stksess_kill(&appctx->ctx.table.proxy->table, old);
5483 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005484 break;
5485 }
5486
Simon Hormanc88b8872011-06-15 15:18:49 +09005487
5488 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005489 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
5490 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
5491 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09005492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005493 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
5494 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005495 break;
5496
Willy Tarreau295a8372011-03-10 11:25:07 +01005497 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005498 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02005499 break;
5500 }
5501 }
5502 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005503}
5504
Willy Tarreaud426a182010-03-05 14:58:26 +01005505/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01005506 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
5507 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
5508 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
5509 * lines are respected within the limit of 70 output chars. Lines that are
5510 * continuation of a previous truncated line begin with "+" instead of " "
5511 * after the offset. The new pointer is returned.
5512 */
Willy Tarreaud426a182010-03-05 14:58:26 +01005513static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
5514 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005515{
5516 int end;
5517 unsigned char c;
5518
5519 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02005520 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005521 return ptr;
5522
Willy Tarreau77804732012-10-29 16:14:26 +01005523 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01005524
Willy Tarreaud426a182010-03-05 14:58:26 +01005525 while (ptr < len && ptr < bsize) {
5526 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01005527 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005528 if (out->len > end - 2)
5529 break;
5530 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005531 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005532 if (out->len > end - 3)
5533 break;
5534 out->str[out->len++] = '\\';
5535 switch (c) {
5536 case '\t': c = 't'; break;
5537 case '\n': c = 'n'; break;
5538 case '\r': c = 'r'; break;
5539 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01005540 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005541 }
5542 out->str[out->len++] = c;
5543 } else {
5544 if (out->len > end - 5)
5545 break;
5546 out->str[out->len++] = '\\';
5547 out->str[out->len++] = 'x';
5548 out->str[out->len++] = hextab[(c >> 4) & 0xF];
5549 out->str[out->len++] = hextab[c & 0xF];
5550 }
Willy Tarreaud426a182010-03-05 14:58:26 +01005551 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005552 /* we had a line break, let's return now */
5553 out->str[out->len++] = '\n';
5554 *line = ptr;
5555 return ptr;
5556 }
5557 }
5558 /* we have an incomplete line, we return it as-is */
5559 out->str[out->len++] = '\n';
5560 return ptr;
5561}
5562
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005563/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02005564 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005565 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01005566 */
Simon Horman9bd2c732011-06-15 15:18:44 +09005567static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005568{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005569 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005570 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01005571
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005572 if (unlikely(si->ib->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02005573 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005574
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005575 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005576
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005577 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005578 /* the function had not been called yet, let's prepare the
5579 * buffer for a response.
5580 */
Willy Tarreau10479e42010-12-12 14:00:34 +01005581 struct tm tm;
5582
5583 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005584 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01005585 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5586 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
5587 error_snapshot_id);
5588
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005589 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01005590 /* Socket buffer full. Let's try again later from the same point */
5591 return 0;
5592 }
5593
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005594 appctx->ctx.errors.px = proxy;
5595 appctx->ctx.errors.buf = 0;
5596 appctx->ctx.errors.bol = 0;
5597 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005598 }
5599
5600 /* we have two inner loops here, one for the proxy, the other one for
5601 * the buffer.
5602 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005603 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005604 struct error_snapshot *es;
5605
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005606 if (appctx->ctx.errors.buf == 0)
5607 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005608 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005609 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005610
5611 if (!es->when.tv_sec)
5612 goto next;
5613
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005614 if (appctx->ctx.errors.iid >= 0 &&
5615 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
5616 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01005617 goto next;
5618
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005619 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005620 /* just print headers now */
5621
5622 char pn[INET6_ADDRSTRLEN];
5623 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005624 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005625
5626 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005627 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01005628 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02005629 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01005630
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005631 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
5632 case AF_INET:
5633 case AF_INET6:
5634 port = get_host_port(&es->src);
5635 break;
5636 default:
5637 port = 0;
5638 }
5639
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005640 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005641 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005642 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005643 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005644 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005645 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005646 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
5647 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005648 break;
5649 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005650 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005651 " backend %s (#%d) : invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005652 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005653 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005654 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01005655 break;
5656 }
5657
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005658 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02005659 ", server %s (#%d), event #%u\n"
5660 " src %s:%d, session #%d, session flags 0x%08x\n"
5661 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
5662 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
5663 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
5664 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
5665 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
5666 es->ev_id,
5667 pn, port, es->sid, es->s_flags,
5668 es->state, es->m_flags, es->t_flags,
5669 es->m_clen, es->m_blen,
5670 es->b_flags, es->b_out, es->b_tot,
5671 es->len, es->b_wrap, es->pos);
5672
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005673 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005674 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005675 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005676 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005677 appctx->ctx.errors.ptr = 0;
5678 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005679 }
5680
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005681 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005682 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005683 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01005684 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005685 if (bi_putchk(si->ib, &trash) == -1)
Willy Tarreau61b34732009-10-03 23:49:35 +02005686 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005687 goto next;
5688 }
5689
5690 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005691 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005692 int newptr;
5693 int newline;
5694
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005695 newline = appctx->ctx.errors.bol;
5696 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
5697 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02005698 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005699
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005700 if (bi_putchk(si->ib, &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01005701 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreau61b34732009-10-03 23:49:35 +02005702 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005703 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005704 appctx->ctx.errors.ptr = newptr;
5705 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005706 };
5707 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005708 appctx->ctx.errors.bol = 0;
5709 appctx->ctx.errors.ptr = -1;
5710 appctx->ctx.errors.buf++;
5711 if (appctx->ctx.errors.buf > 1) {
5712 appctx->ctx.errors.buf = 0;
5713 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005714 }
5715 }
5716
5717 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02005718 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01005719}
5720
Willy Tarreaud5781202012-09-22 19:32:35 +02005721/* parse the "level" argument on the bind lines */
5722static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
5723{
5724 if (!*args[cur_arg + 1]) {
5725 memprintf(err, "'%s' : missing level", args[cur_arg]);
5726 return ERR_ALERT | ERR_FATAL;
5727 }
5728
5729 if (!strcmp(args[cur_arg+1], "user"))
5730 conf->level = ACCESS_LVL_USER;
5731 else if (!strcmp(args[cur_arg+1], "operator"))
5732 conf->level = ACCESS_LVL_OPER;
5733 else if (!strcmp(args[cur_arg+1], "admin"))
5734 conf->level = ACCESS_LVL_ADMIN;
5735 else {
5736 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
5737 args[cur_arg], args[cur_arg+1]);
5738 return ERR_ALERT | ERR_FATAL;
5739 }
5740
5741 return 0;
5742}
5743
Willy Tarreaub24281b2011-02-13 13:16:36 +01005744struct si_applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005745 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005746 .name = "<STATS>", /* used for logging */
5747 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07005748 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005749};
5750
Simon Horman9bd2c732011-06-15 15:18:44 +09005751static struct si_applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005752 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005753 .name = "<CLI>", /* used for logging */
5754 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01005755 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01005756};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005757
Willy Tarreaudc13c112013-06-21 23:16:39 +02005758static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02005759 { CFG_GLOBAL, "stats", stats_parse_global },
5760 { 0, NULL, NULL },
5761}};
5762
Willy Tarreaud5781202012-09-22 19:32:35 +02005763static struct bind_kw_list bind_kws = { "STAT", { }, {
5764 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
5765 { NULL, NULL, 0 },
5766}};
5767
Willy Tarreau10522fd2008-07-09 20:12:41 +02005768__attribute__((constructor))
5769static void __dumpstats_module_init(void)
5770{
5771 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02005772 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02005773}
5774
Willy Tarreau91861262007-10-17 17:06:05 +02005775/*
5776 * Local variables:
5777 * c-indent-level: 8
5778 * c-basic-offset: 8
5779 * End:
5780 */